Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Celestia and CoreDao #74

Merged
merged 11 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/afraid-buses-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fireblocks/e2e-tests': patch
---

raise error if invalid address format
7 changes: 7 additions & 0 deletions .changeset/fair-nails-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@fireblocks/recovery-utility': patch
'@fireblocks/asset-config': patch
'@fireblocks/recovery-relay': patch
---

Re-enabled transfer of and fixed ATOM
5 changes: 5 additions & 0 deletions .changeset/funny-knives-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fireblocks/asset-config': patch
---

Added celestia and coredao to native patches]
5 changes: 5 additions & 0 deletions .changeset/late-experts-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fireblocks/recovery-relay': patch
---

yarn file and version bump
5 changes: 5 additions & 0 deletions .changeset/proud-camels-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fireblocks/e2e-tests': patch
---

added .env example for e2e test
5 changes: 5 additions & 0 deletions .changeset/proud-tables-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fireblocks/recovery-shared': minor
---

fixed address validation for cosmos based chains
5 changes: 5 additions & 0 deletions .changeset/rare-owls-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fireblocks/asset-config': minor
---

refactor(@fireblocks/asset-config): :wrench: updated global assets
7 changes: 7 additions & 0 deletions .changeset/slow-apples-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@fireblocks/wallet-derivation': patch
'@fireblocks/recovery-utility': patch
'@fireblocks/recovery-relay': patch
---

Added celestia and coredao logic
5 changes: 5 additions & 0 deletions .changeset/strong-files-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fireblocks/e2e-tests': patch
---

updated asset list for test
5 changes: 5 additions & 0 deletions .changeset/tame-humans-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fireblocks/e2e-tests': minor
---

raise error if couldn't get balance
6 changes: 6 additions & 0 deletions .changeset/tough-fans-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@fireblocks/wallet-derivation': minor
'@fireblocks/recovery-utility': patch
---

Added flag to force legacy transaction format
16 changes: 3 additions & 13 deletions apps/recovery-relay/lib/wallets/ATOM/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Cosmos as BaseCosmos, Input } from '@fireblocks/wallet-derivation';
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import { StargateClient } from '@cosmjs/stargate';
import { SignDoc, TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
import { AccountData, RawSignature } from '../types';
import { AccountData } from '../types';
import { ConnectedWallet } from '../ConnectedWallet';

export class Cosmos extends BaseCosmos implements ConnectedWallet {
Expand All @@ -16,7 +16,7 @@ export class Cosmos extends BaseCosmos implements ConnectedWallet {
super(input);

this.tendermintClient = undefined;
this.rpcURL = input.isTestnet ? 'https://cosmos-testnet-rpc.allthatnode.com:26657' : 'https://cosmos-lcd.quickapi.com/';
this.rpcURL = input.isTestnet ? 'https://rpc.sentry-01.theta-testnet.polypore.xyz' : 'https://cosmos-rpc.publicnode.com';
}

public async getBalance(): Promise<number> {
Expand All @@ -43,17 +43,7 @@ export class Cosmos extends BaseCosmos implements ConnectedWallet {
public async broadcastTx(txHex: string): Promise<string> {
await this.prepareClients();

// TODO: Serialize tx with signature
// const sig = sigs[0];
// const signature: string = `${sig.r}${sig.s}`;
const signDoc: SignDoc = SignDoc.fromJSON(JSON.parse(Buffer.from(txHex, 'hex').toString()));
// Continuation of SigningStargateClient.signDirect from after "this.sign...."
const txRaw: TxRaw = TxRaw.fromPartial({
bodyBytes: signDoc.bodyBytes,
authInfoBytes: signDoc.authInfoBytes,
// signatures: [Buffer.from(signature, 'hex')],
});

const txRaw: TxRaw = TxRaw.decode(Uint8Array.from(Buffer.from(txHex, 'hex')));
try {
const txRes = await this.stargateClient!.broadcastTx(TxRaw.encode(txRaw).finish());
this.relayLogger.debug(`Cosmos: Broadcasted tx: ${txRes.transactionHash}`);
Expand Down
62 changes: 62 additions & 0 deletions apps/recovery-relay/lib/wallets/CELESTIA/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Celestia as BaseCelestia, Input } from '@fireblocks/wallet-derivation';
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import { StargateClient } from '@cosmjs/stargate';
import { SignDoc, TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
import { AccountData } from '../types';
import { ConnectedWallet } from '../ConnectedWallet';

export class Celestia extends BaseCelestia implements ConnectedWallet {
private tendermintClient: Tendermint34Client | undefined;

private stargateClient: StargateClient | undefined;

private rpcURL: string;

constructor(input: Input) {
super(input);

this.tendermintClient = undefined;
this.rpcURL = input.isTestnet ? 'https://celestia-mocha-rpc.publicnode.com' : 'https://celestia-rpc.publicnode.com';
}

public async getBalance(): Promise<number> {
const balanceCoin = await this.stargateClient!.getBalance(this.address, 'utia');
return parseInt(balanceCoin.amount, 10) / 1_000_000;
}

public async prepare(): Promise<AccountData> {
await this.prepareClients();
const balanceCoin = await this.stargateClient!.getBalance(this.address, 'utia');
const { accountNumber, sequence } = await this.stargateClient!.getSequence(this.address);
const chainId = await this.stargateClient!.getChainId();
const extraParams = new Map<string, any>();
extraParams.set(this.KEY_ACCOUNT_NUMBER, accountNumber);
extraParams.set(this.KEY_CHAIN_ID, chainId);
extraParams.set(this.KEY_SEQUENCE, sequence);
// TODO: Add option for cusom fee
const balance = parseInt(balanceCoin.amount, 10) / 1_000_000;
const preparedData = { balance, extraParams, insufficientBalance: balance < 0.001 };
this.relayLogger.logPreparedData('Celestia', preparedData);
return preparedData;
}

public async broadcastTx(txHex: string): Promise<string> {
await this.prepareClients();
const txRaw: TxRaw = TxRaw.decode(Uint8Array.from(Buffer.from(txHex, 'hex')));
try {
const txRes = await this.stargateClient!.broadcastTx(TxRaw.encode(txRaw).finish());
this.relayLogger.debug(`Celestia: Broadcasted tx: ${txRes.transactionHash}`);
return txRes.transactionHash;
} catch (e) {
this.relayLogger.error(`Celestia: Error broadcasting tx: ${e}`);
throw e;
}
}

private async prepareClients(): Promise<void> {
if (!this.tendermintClient || !this.stargateClient) {
this.tendermintClient = await Tendermint34Client.connect(this.rpcURL);
this.stargateClient = await StargateClient.connect(this.rpcURL);
}
}
}
22 changes: 22 additions & 0 deletions apps/recovery-relay/lib/wallets/EVM/CORE_COREDAO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Input } from '@fireblocks/wallet-derivation';
import { ConnectedWallet } from '../ConnectedWallet';
import { EVM } from '.';
import { AccountData } from '../types';

export class CoreDAO extends EVM implements ConnectedWallet {
constructor(input: Input) {
super(input, input.isTestnet ? 'https://rpc.test.btcs.network ' : 'https://rpc.coredao.org', input.isTestnet ? 1115 : 1116);
}

public override async prepare(): Promise<AccountData> {
const res = await super.prepare();
if (!res.extraParams) {
const extraParams = new Map();
extraParams.set(this.KEY_EVM_FORCE_LEGACY_TX, true);
res.extraParams = extraParams;
} else {
res.extraParams.set(this.KEY_EVM_FORCE_LEGACY_TX, true);
}
return res;
}
}
6 changes: 6 additions & 0 deletions apps/recovery-relay/lib/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { Stellar } from './XLM';
import { Ripple } from './XRP';
import { Tezos } from './XTZ';
import { Algorand } from './ALGO';
import { Celestia } from './CELESTIA';
import { CoreDAO } from './EVM/CORE_COREDAO';
export { ConnectedWallet } from './ConnectedWallet';

export const WalletClasses = {
Expand Down Expand Up @@ -94,6 +96,8 @@ export const WalletClasses = {
'ETH-AETH_RIN': Arbitrum,
BNB_BSC: BinanceSmartChain,
BNB_TEST: BinanceSmartChain,
CORE_COREDAO: CoreDAO,
CORE_COREDAO_TEST: CoreDAO,
EOS,
EOS_TEST: EOS,
ATOM_COS: Cosmos,
Expand All @@ -112,6 +116,8 @@ export const WalletClasses = {
XEM_TEST: NEM,
HBAR: Hedera,
HBAR_TEST: Hedera,
CELESTIA: Celestia,
CELESTIA_TEST: Celestia,
} as const;

type WalletClass = (typeof WalletClasses)[keyof typeof WalletClasses];
Expand Down
2 changes: 1 addition & 1 deletion apps/recovery-relay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"axios": "^1.4.0",
"base58": "^2.0.1",
"bigint-buffer": "^1.1.5",
"bignumber.js": "^9.1.1",
"bignumber.js": "^9.1.2",
"bitcoinjs-lib": "^6.1.3",
"bitcore-lib-cash": "^10.0.5",
"bs58": "^5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/recovery-utility/renderer/lib/wallets/ATOM/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Cosmos extends BaseCosmos implements SigningWallet {
toAddress: to,
amount: [
{
amount: `${amount * 1_000_000}`,
amount: `${amount * 1_000_000 - 2000}`,
denom: 'uatom',
},
],
Expand Down
78 changes: 78 additions & 0 deletions apps/recovery-utility/renderer/lib/wallets/CELESTIA/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Celestia as BaseCelestia } from '@fireblocks/wallet-derivation';
import { defaultRegistryTypes } from '@cosmjs/stargate';
import { encodeSecp256k1Pubkey } from '@cosmjs/amino';
import {
encodePubkey,
Registry,
makeAuthInfoBytes,
makeSignDoc,
DirectSecp256k1Wallet,
GeneratedType,
} from '@cosmjs/proto-signing';
import { Int53 } from '@cosmjs/math';
import { fromBase64 } from '@cosmjs/encoding';
import { SignDoc, TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
import { SigningWallet } from '../SigningWallet';
import { GenerateTxInput, TxPayload } from '../types';

export class Celestia extends BaseCelestia implements SigningWallet {
public async generateTx({ to, amount, extraParams, memo }: GenerateTxInput): Promise<TxPayload> {
const wallet = await DirectSecp256k1Wallet.fromKey(Buffer.from(this.privateKey!.replace('0x', ''), 'hex'), 'celestia');
const sendMsg = {
typeUrl: '/cosmos.bank.v1beta1.MsgSend',
value: {
fromAddress: this.address,
toAddress: to,
amount: [
{
amount: `${amount * 1_000_000 - 20000}`,
denom: 'utia',
},
],
},
};

const fee = extraParams?.get(this.KEY_FEE) ?? {
amount: [
{
denom: 'utia',
amount: '20000',
},
],
gas: '200000',
};

const accountNumber = extraParams?.get(this.KEY_ACCOUNT_NUMBER);
const sequence = extraParams?.get(this.KEY_SEQUENCE);
const chainId = extraParams?.get(this.KEY_CHAIN_ID);

// Same as SigningStargateClient.signDirect
const registry = new Registry(defaultRegistryTypes as Iterable<[string, GeneratedType]>);
const pubKey: any = encodePubkey(encodeSecp256k1Pubkey(Buffer.from(this.publicKey.replace('0x', ''), 'hex')));
const txEncoded = {
typeUrl: '/cosmos.tx.v1beta1.TxBody',
value: {
messages: [sendMsg],
memo,
},
};

this.utilityLogger.logSigningTx('Celestia', txEncoded);

const txBodyBytes = registry.encode(txEncoded);
const gasLimit = Int53.fromString(fee.gas).toNumber();
const authInfoBytes = makeAuthInfoBytes([{ pubkey: pubKey, sequence }], fee.amount, gasLimit, undefined, undefined);
const signDoc: SignDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber);
const signedTx = await wallet.signDirect(this.address, signDoc);
const txRaw = TxRaw.fromPartial({
bodyBytes: signedTx.signed.bodyBytes,
authInfoBytes: signedTx.signed.authInfoBytes,
signatures: [fromBase64(signedTx.signature.signature)],
});

const tx = Buffer.from(TxRaw.encode(txRaw).finish()).toString('hex');
return {
tx,
};
}
}
26 changes: 14 additions & 12 deletions apps/recovery-utility/renderer/lib/wallets/EVM/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Wallet, parseEther } from 'ethers';
/* eslint-disable no-unneeded-ternary */
/* eslint-disable no-nested-ternary */
import { Wallet } from 'ethers';
import { EVMWallet as EVMBase, Input } from '@fireblocks/wallet-derivation';
import { TxPayload, GenerateTxInput } from '../types';
import { SigningWallet } from '../SigningWallet';
Expand All @@ -21,26 +23,26 @@ export class EVM extends EVMBase implements SigningWallet {
}

const balanceHex = extraParams?.get(this.KEY_EVM_WEI_BALANCE);
const forceLegacyTx = extraParams?.get(this.KEY_EVM_FORCE_LEGACY_TX);

this.utilityLogger.logSigningTx('EVM', {
const txObject = {
from: this.address,
to,
nonce,
gasLimit: 21000,
gasPrice,
value: BigInt(`0x${balanceHex}`),
chainId: chainId ? chainId : this.path.coinType === 1 ? 5 : 1,
});
type: forceLegacyTx ? 0 : undefined,
};

const serialized = await new Wallet(this.privateKey).signTransaction({
from: this.address,
to,
nonce,
gasLimit: 21000,
gasPrice,
value: BigInt(`0x${balanceHex}`),
chainId: chainId ? chainId : this.path.coinType === 1 ? 5 : 1,
});
if (txObject.type === undefined) {
delete txObject.type;
}

this.utilityLogger.logSigningTx('EVM', txObject);

const serialized = await new Wallet(this.privateKey).signTransaction(txObject);

return {
tx: serialized,
Expand Down
3 changes: 3 additions & 0 deletions apps/recovery-utility/renderer/lib/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { NEM } from './NEM';
import { Hedera } from './HBAR';
import { Algorand } from './ALGO';
import { Bitcoin, BitcoinSV, LiteCoin, Dash, ZCash, Doge } from './BTC';
import { Celestia } from './CELESTIA';

const fillEVMs = () => {
const evms = Object.keys(assets).reduce(
Expand Down Expand Up @@ -73,6 +74,8 @@ export const WalletClasses = {
XRP_TEST: Ripple,
LUNA2: Luna,
LUNA2_TEST: Luna,
CELESTIA: Celestia,
CELESTIA_TEST: Celestia,

// EDDSA
SOL: Solana,
Expand Down
Loading