Skip to content

Commit 82d635b

Browse files
authored
cleanup fireblocks signer (#100)
* refacto fet FB signing * refacto fet FB signing * wip * fix injective tx crafting * nits
1 parent fac5856 commit 82d635b

File tree

16 files changed

+1566
-2181
lines changed

16 files changed

+1566
-2181
lines changed

pnpm-lock.yaml

Lines changed: 1501 additions & 2118 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/integrations/fb_signer.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
CreateTransactionResponse,
33
FireblocksSDK,
44
PeerType,
5-
SigningAlgorithm,
65
TransactionArguments,
76
TransactionOperation,
87
TransactionResponse,
@@ -47,7 +46,7 @@ export class FbSigner {
4746

4847
/**
4948
* Wait for given transaction to be completed
50-
* @param fbTx: fireblocks transaction
49+
* @param fbTx fireblocks transaction
5150
* @private
5251
*/
5352
protected async waitForTxCompletion(fbTx: CreateTransactionResponse): Promise<TransactionResponse> {
@@ -76,19 +75,25 @@ export class FbSigner {
7675

7776
/**
7877
* Sign a transaction with fireblocks using Fireblocks raw message signing feature
79-
* @param payloadToSign: transaction data in hexadecimal
80-
* @param assetId: fireblocks asset id
81-
* @param note: optional fireblocks custom note
78+
* @param payloadToSign transaction data in hexadecimal
79+
* @param assetId fireblocks asset id
80+
* @param note optional fireblocks custom note
8281
*/
83-
public async signWithFB(payloadToSign: any, assetId: AssetId, note?: string): Promise<TransactionResponse> {
82+
public async sign(payloadToSign: any, assetId?: AssetId, note?: string): Promise<TransactionResponse> {
8483
try {
84+
const assetArgs = assetId
85+
? {
86+
assetId,
87+
source: {
88+
type: PeerType.VAULT_ACCOUNT,
89+
id: this.vaultId.toString(),
90+
},
91+
}
92+
: {};
93+
8594
const tx: TransactionArguments = {
86-
assetId: assetId,
95+
...assetArgs,
8796
operation: TransactionOperation.RAW,
88-
source: {
89-
type: PeerType.VAULT_ACCOUNT,
90-
id: this.vaultId.toString(),
91-
},
9297
note,
9398
extraParameters: payloadToSign,
9499
};
@@ -101,52 +106,54 @@ export class FbSigner {
101106
}
102107

103108
/**
104-
* Sign a generic transaction with fireblocks using Fireblocks raw message signing feature.
105-
* @param payloadToSign: transaction data in hexadecimal
106-
* @param derivationPath: derivation path of the token to sign
107-
* @param algorithm: algorithm of the token to sign
108-
* @param note: optional fireblocks custom note
109+
* Sign an EIP-712 Ethereum typed message with fireblocks
110+
* @param eip712message eip712message to sign
111+
* @param assetId fireblocks asset id
112+
* @param note optional fireblocks custom note
109113
*/
110-
public async signGenericWithFB(
111-
payloadContent: string,
112-
derivationPath: number[],
113-
algorithm: SigningAlgorithm,
114+
public async signTypedMessage(
115+
eip712message: any,
116+
assetId: "ETH" | "ETH_TEST3" | "ETH_TEST6",
114117
note?: string,
115118
): Promise<TransactionResponse> {
116119
try {
117-
const payloadToSign = {
118-
operation: TransactionOperation.RAW,
120+
const tx: TransactionArguments = {
121+
assetId: assetId,
122+
operation: TransactionOperation.TYPED_MESSAGE,
123+
source: {
124+
type: PeerType.VAULT_ACCOUNT,
125+
id: this.vaultId.toString(),
126+
},
119127
note,
120128
extraParameters: {
121129
rawMessageData: {
122130
messages: [
123131
{
124-
content: payloadContent,
125-
derivationPath,
132+
content: eip712message,
133+
type: "EIP712",
126134
},
127135
],
128-
algorithm,
129136
},
130137
},
131138
};
132-
const fbTx = await this.fireblocks.createTransaction(payloadToSign);
139+
const fbTx = await this.fireblocks.createTransaction(tx);
133140
return await this.waitForTxCompletion(fbTx);
134141
} catch (err: any) {
135142
console.log(err);
136-
throw new Error("Fireblocks signer (signGenericWithFB): " + err);
143+
throw new Error("Fireblocks signer (signWithFB): " + err);
137144
}
138145
}
139146

140147
/**
141148
* Sign and broadcast a transaction with fireblocks using Fireblocks contract call feature
142-
* @param payloadToSign: transaction data in hexadecimal
143-
* @param assetId: fireblocks asset id
144-
* @param note: optional fireblocks custom note
149+
* @param payloadToSign transaction data in hexadecimal
150+
* @param assetId fireblocks asset id
151+
* @param note optional fireblocks custom note
145152
* @param tx Ethereum transaction
146153
* @param destinationId Fireblocks destination id, this corresponds to the Fireblocks whitelisted contract address id
147154
* @param sendAmount send the amount in tx to smart contract
148155
*/
149-
public async signAndBroadcastWithFB(
156+
public async signAndBroadcastWith(
150157
payloadToSign: any,
151158
assetId: AssetId,
152159
tx: EthTx | MaticTx,

src/services/ada.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class AdaService extends Service {
9393
};
9494

9595
const fbNote = note ? note : "ADA tx from @kilnfi/sdk";
96-
const fbTx = await fbSigner.signWithFB(payload, this.testnet ? "ADA_TEST" : "ADA", fbNote);
96+
const fbTx = await fbSigner.sign(payload, this.testnet ? "ADA_TEST" : "ADA", fbNote);
9797

9898
if (!fbTx.signedMessages) {
9999
throw new Error(`Could not sign the transaction.`);

src/services/atom.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export class AtomService extends Service {
6262
/**
6363
* Craft atom restake rewards transaction
6464
* @param pubkey wallet pubkey, this is different from the wallet address
65-
* @param validatorAccount validator account address (wallet controlling the validator)
6665
* @param validatorAddress validator address to which the delegation has been made
6766
*/
6867
async craftRestakeRewardsTx(pubkey: string, validatorAddress: string): Promise<CosmosTx> {
@@ -131,7 +130,7 @@ export class AtomService extends Service {
131130
};
132131
const fbNote = note ? note : "ATOM tx from @kilnfi/sdk";
133132
const signer = this.getFbSigner(integration);
134-
const fbTx = await signer.signWithFB(payload, this.testnet ? "ATOM_COS_TEST" : "ATOM_COS", fbNote);
133+
const fbTx = await signer.sign(payload, this.testnet ? "ATOM_COS_TEST" : "ATOM_COS", fbNote);
135134
const signature: string = fbTx.signedMessages![0].signature.fullSig;
136135
const { data } = await api.post<CosmosSignedTx>(`/v1/atom/transaction/prepare`, {
137136
pubkey: tx.data.pubkey,

src/services/dot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ export class DotService extends Service {
267267

268268
const fbSigner = this.getFbSigner(integration);
269269
const fbNote = note ? note : "DOT tx from @kilnfi/sdk";
270-
const fbTx = await fbSigner.signWithFB(payload, this.testnet ? "WND" : "DOT", fbNote);
270+
const fbTx = await fbSigner.sign(payload, this.testnet ? "WND" : "DOT", fbNote);
271271
const signature = `0x00${fbTx.signedMessages![0].signature.fullSig}`;
272272

273273
const { data } = await api.post<DotSignedTx>(`/v1/dot/transaction/prepare`, {

src/services/dydx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class DydxService extends Service {
143143
};
144144
const fbNote = note ? note : "DYDX tx from @kilnfi/sdk";
145145
const signer = this.getFbSigner(integration);
146-
const fbTx = await signer.signWithFB(payload, "DYDX_DYDX", fbNote);
146+
const fbTx = await signer.sign(payload, "DYDX_DYDX", fbNote);
147147
const signature: string = fbTx.signedMessages![0].signature.fullSig;
148148
const { data } = await api.post<CosmosSignedTx>(`/v1/dydx/transaction/prepare`, {
149149
pubkey: tx.data.pubkey,

src/services/eth.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class EthService extends Service {
6969
const fbSigner = this.getFbSigner(integration);
7070
const assetId = this.testnet ? "ETH_TEST6" : "ETH";
7171
const fbNote = note ? note : "ETH tx from @kilnfi/sdk";
72-
const fbTx = await fbSigner.signWithFB(payload, assetId, fbNote);
72+
const fbTx = await fbSigner.sign(payload, assetId, fbNote);
7373
const { data } = await api.post<EthSignedTx>(`/v1/eth/transaction/prepare`, {
7474
unsigned_tx_serialized: tx.data.unsigned_tx_serialized,
7575
r: `0x${fbTx?.signedMessages?.[0].signature.r}`,
@@ -98,14 +98,7 @@ export class EthService extends Service {
9898
const fbSigner = this.getFbSigner(integration);
9999
const assetId = this.testnet ? "ETH_TEST6" : "ETH";
100100
const fbNote = note ? note : "ETH tx from @kilnfi/sdk";
101-
return await fbSigner.signAndBroadcastWithFB(
102-
payload,
103-
assetId,
104-
tx,
105-
integration.fireblocksDestinationId,
106-
true,
107-
fbNote,
108-
);
101+
return await fbSigner.signAndBroadcastWith(payload, assetId, tx, integration.fireblocksDestinationId, true, fbNote);
109102
}
110103

111104
/**

src/services/fet.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export class FetService extends Service {
6262
/**
6363
* Craft fetch.ai restake rewards transaction
6464
* @param pubkey wallet pubkey, this is different from the wallet address
65-
* @param validatorAccount validator account address (wallet controlling the validator)
6665
* @param validatorAddress validator address to which the delegation has been made
6766
*/
6867
async craftRestakeRewardsTx(pubkey: string, validatorAddress: string): Promise<CosmosTx> {
@@ -120,13 +119,20 @@ export class FetService extends Service {
120119
* @param note note to identify the transaction in your custody solution
121120
*/
122121
async sign(integration: Integration, tx: CosmosTx, note?: string): Promise<CosmosSignedTx> {
123-
const payloadContent = tx.data.unsigned_tx_hash;
124-
const derivationPath = [44, 118, integration.vaultId, 0, 0];
125-
const signingAlgorithm = SigningAlgorithm.MPC_ECDSA_SECP256K1;
126122
const fbNote = note ? note : "FET tx from @kilnfi/sdk";
127-
128123
const signer = this.getFbSigner(integration);
129-
const fbTx = await signer.signGenericWithFB(payloadContent, derivationPath, signingAlgorithm, fbNote);
124+
const payload = {
125+
rawMessageData: {
126+
messages: [
127+
{
128+
content: tx.data.unsigned_tx_hash,
129+
derivationPath: [44, 118, integration.vaultId, 0, 0],
130+
},
131+
],
132+
algorithm: SigningAlgorithm.MPC_ECDSA_SECP256K1,
133+
},
134+
};
135+
const fbTx = await signer.sign(payload, undefined, fbNote);
130136
const signature: string = fbTx.signedMessages![0].signature.fullSig;
131137
const { data } = await api.post<CosmosSignedTx>(`/v1/fet/transaction/prepare`, {
132138
pubkey: tx.data.pubkey,

src/services/inj.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ export class InjService extends Service {
6161
/**
6262
* Craft inj restake rewards transaction
6363
* @param pubkey wallet pubkey, this is different from the wallet address
64-
* @param validatorAccount validator account address (wallet controlling the validator)
6564
* @param validatorAddress validator address to which the delegation has been made
6665
*/
6766
async craftRestakeRewardsTx(pubkey: string, validatorAddress: string): Promise<CosmosTx> {
@@ -130,7 +129,7 @@ export class InjService extends Service {
130129
};
131130
const fbNote = note ? note : "INJ tx from @kilnfi/sdk";
132131
const signer = this.getFbSigner(integration);
133-
const fbTx = await signer.signWithFB(payload, "INJ_INJ", fbNote);
132+
const fbTx = await signer.sign(payload, "INJ_INJ", fbNote);
134133
const signature: string = fbTx.signedMessages![0].signature.fullSig;
135134
const { data } = await api.post<CosmosSignedTx>(`/v1/inj/transaction/prepare`, {
136135
pubkey: tx.data.pubkey,

src/services/matic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class MaticService extends Service {
135135

136136
const fbSigner = this.getFbSigner(integration);
137137
const fbNote = note ? note : "MATIC tx from @kilnfi/sdk";
138-
const fbTx = await fbSigner.signWithFB(payload, this.testnet ? "ETH_TEST3" : "ETH", fbNote);
138+
const fbTx = await fbSigner.sign(payload, this.testnet ? "ETH_TEST3" : "ETH", fbNote);
139139
const { data } = await api.post<MaticSignedTx>(`/v1/matic/transaction/prepare`, {
140140
unsigned_tx_serialized: tx.data.unsigned_tx_serialized,
141141
r: `0x${fbTx?.signedMessages?.[0].signature.r}`,
@@ -164,7 +164,7 @@ export class MaticService extends Service {
164164
const fbSigner = this.getFbSigner(integration);
165165
const fbNote = note ? note : "MATIC tx from @kilnfi/sdk";
166166
const assetId = this.testnet ? "ETH_TEST3" : "ETH";
167-
return await fbSigner.signAndBroadcastWithFB(
167+
return await fbSigner.signAndBroadcastWith(
168168
payload,
169169
assetId,
170170
tx,

0 commit comments

Comments
 (0)