@@ -2,38 +2,38 @@ import {
22 CreateTransactionResponse ,
33 FireblocksSDK ,
44 PeerType ,
5+ SigningAlgorithm ,
56 TransactionArguments ,
67 TransactionOperation ,
78 TransactionResponse ,
89 TransactionStatus ,
9- } from ' fireblocks-sdk' ;
10+ } from " fireblocks-sdk" ;
1011
11- import { utils } from ' ethers' ;
12- import { EthTx } from ' ../types/eth' ;
12+ import { utils } from " ethers" ;
13+ import { EthTx } from " ../types/eth" ;
1314import { MaticTx } from "../types/matic" ;
1415
1516type AssetId =
16- 'SOL_TEST'
17- | 'SOL'
18- | 'ETH_TEST3'
19- | 'ETH_TEST6'
20- | 'ETH'
21- | 'ATOM_COS_TEST'
22- | 'ATOM_COS'
23- | 'OSMO_TEST'
24- | 'OSMO'
25- | 'ADA_TEST'
26- | 'ADA'
27- | 'NEAR_TEST'
28- | 'NEAR'
29- | 'XTZ_TEST'
30- | 'XTZ'
31- | 'WND'
32- | 'DOT'
33- | 'DV4TNT_TEST'
34- | 'DYDX_DYDX'
35- | 'CELESTIA'
36- ;
17+ | "SOL_TEST"
18+ | "SOL"
19+ | "ETH_TEST3"
20+ | "ETH_TEST6"
21+ | "ETH"
22+ | "ATOM_COS_TEST"
23+ | "ATOM_COS"
24+ | "OSMO_TEST"
25+ | "OSMO"
26+ | "ADA_TEST"
27+ | "ADA"
28+ | "NEAR_TEST"
29+ | "NEAR"
30+ | "XTZ_TEST"
31+ | "XTZ"
32+ | "WND"
33+ | "DOT"
34+ | "DV4TNT_TEST"
35+ | "DYDX_DYDX"
36+ | "CELESTIA" ;
3737
3838export class FbSigner {
3939 protected fireblocks : FireblocksSDK ;
@@ -42,38 +42,52 @@ export class FbSigner {
4242 constructor ( fireblocks : FireblocksSDK , vaultId : number ) {
4343 this . fireblocks = fireblocks ;
4444 this . vaultId = vaultId ;
45- } ;
46-
45+ }
4746
4847 /**
4948 * Wait for given transaction to be completed
5049 * @param fbTx: fireblocks transaction
5150 * @private
5251 */
53- protected async waitForTxCompletion ( fbTx : CreateTransactionResponse ) : Promise < TransactionResponse > {
52+ protected async waitForTxCompletion (
53+ fbTx : CreateTransactionResponse
54+ ) : Promise < TransactionResponse > {
5455 try {
5556 let tx = fbTx ;
5657 while ( tx . status != TransactionStatus . COMPLETED ) {
57- if ( tx . status == TransactionStatus . BLOCKED || tx . status == TransactionStatus . FAILED || tx . status == TransactionStatus . REJECTED || tx . status == TransactionStatus . CANCELLED ) {
58- throw Error ( `Fireblocks signer: the transaction has been ${ tx . status } ` ) ;
58+ if (
59+ tx . status == TransactionStatus . BLOCKED ||
60+ tx . status == TransactionStatus . FAILED ||
61+ tx . status == TransactionStatus . CANCELLED
62+ ) {
63+ throw Error (
64+ `Fireblocks signer: the transaction has been ${ tx . status } `
65+ ) ;
66+ } else if ( tx . status == TransactionStatus . REJECTED ) {
67+ throw Error (
68+ `Fireblocks signer: the transaction has been rejected, make sure that the TAP security policy is not blocking the transaction`
69+ ) ;
5970 }
6071 tx = await this . fireblocks . getTransactionById ( fbTx . id ) ;
6172 }
6273
63- return ( await this . fireblocks . getTransactionById ( fbTx . id ) ) ;
74+ return await this . fireblocks . getTransactionById ( fbTx . id ) ;
6475 } catch ( err : any ) {
65- throw new Error ( ' Fireblocks signer (waitForTxCompletion): ' + err ) ;
76+ throw new Error ( " Fireblocks signer (waitForTxCompletion): " + err ) ;
6677 }
6778 }
6879
69-
7080 /**
7181 * Sign a transaction with fireblocks using Fireblocks raw message signing feature
7282 * @param payloadToSign: transaction data in hexadecimal
7383 * @param assetId: fireblocks asset id
7484 * @param note: optional fireblocks custom note
7585 */
76- public async signWithFB ( payloadToSign : any , assetId : AssetId , note ?: string ) : Promise < TransactionResponse > {
86+ public async signWithFB (
87+ payloadToSign : any ,
88+ assetId : AssetId ,
89+ note ?: string
90+ ) : Promise < TransactionResponse > {
7791 try {
7892 const tx : TransactionArguments = {
7993 assetId : assetId ,
@@ -86,10 +100,47 @@ export class FbSigner {
86100 extraParameters : payloadToSign ,
87101 } ;
88102 const fbTx = await this . fireblocks . createTransaction ( tx ) ;
89- return ( await this . waitForTxCompletion ( fbTx ) ) ;
103+ return await this . waitForTxCompletion ( fbTx ) ;
104+ } catch ( err : any ) {
105+ console . log ( err ) ;
106+ throw new Error ( "Fireblocks signer (signWithFB): " + err ) ;
107+ }
108+ }
109+
110+ /**
111+ * Sign a generic transaction with fireblocks using Fireblocks raw message signing feature.
112+ * @param payloadToSign: transaction data in hexadecimal
113+ * @param derivationPath: derivation path of the token to sign
114+ * @param algorithm: algorithm of the token to sign
115+ * @param note: optional fireblocks custom note
116+ */
117+ public async signGenericWithFB (
118+ payloadContent : string ,
119+ derivationPath : number [ ] ,
120+ algorithm : SigningAlgorithm ,
121+ note ?: string
122+ ) : Promise < TransactionResponse > {
123+ try {
124+ const payloadToSign = {
125+ operation : TransactionOperation . RAW ,
126+ note,
127+ extraParameters : {
128+ rawMessageData : {
129+ messages : [
130+ {
131+ content : payloadContent ,
132+ derivationPath,
133+ } ,
134+ ] ,
135+ algorithm,
136+ } ,
137+ } ,
138+ } ;
139+ const fbTx = await this . fireblocks . createTransaction ( payloadToSign ) ;
140+ return await this . waitForTxCompletion ( fbTx ) ;
90141 } catch ( err : any ) {
91142 console . log ( err ) ;
92- throw new Error ( ' Fireblocks signer (signWithFB ): ' + err ) ;
143+ throw new Error ( " Fireblocks signer (signGenericWithFB ): " + err ) ;
93144 }
94145 }
95146
@@ -102,7 +153,14 @@ export class FbSigner {
102153 * @param destinationId Fireblocks destination id, this corresponds to the Fireblocks whitelisted contract address id
103154 * @param sendAmount send the amount in tx to smart contract
104155 */
105- public async signAndBroadcastWithFB ( payloadToSign : any , assetId : AssetId , tx : EthTx | MaticTx , destinationId : string , sendAmount : boolean = true , note ?: string ) : Promise < TransactionResponse > {
156+ public async signAndBroadcastWithFB (
157+ payloadToSign : any ,
158+ assetId : AssetId ,
159+ tx : EthTx | MaticTx ,
160+ destinationId : string ,
161+ sendAmount : boolean = true ,
162+ note ?: string
163+ ) : Promise < TransactionResponse > {
106164 try {
107165 const txArgs : TransactionArguments = {
108166 assetId : assetId ,
@@ -115,18 +173,23 @@ export class FbSigner {
115173 type : PeerType . EXTERNAL_WALLET ,
116174 id : destinationId ,
117175 } ,
118- amount : tx . data . amount_wei && sendAmount ? utils . formatEther ( tx . data . amount_wei ) : "0" ,
176+ amount :
177+ tx . data . amount_wei && sendAmount
178+ ? utils . formatEther ( tx . data . amount_wei )
179+ : "0" ,
119180 note,
120181 extraParameters : payloadToSign ,
121182 gasLimit : tx . data . gas_limit ,
122- priorityFee : utils . formatUnits ( tx . data . max_priority_fee_per_gas_wei , 'gwei' ) ,
123- maxFee : utils . formatUnits ( tx . data . max_fee_per_gas_wei , 'gwei' ) ,
183+ priorityFee : utils . formatUnits (
184+ tx . data . max_priority_fee_per_gas_wei ,
185+ "gwei"
186+ ) ,
187+ maxFee : utils . formatUnits ( tx . data . max_fee_per_gas_wei , "gwei" ) ,
124188 } ;
125189 const fbTx = await this . fireblocks . createTransaction ( txArgs ) ;
126- return ( await this . waitForTxCompletion ( fbTx ) ) ;
190+ return await this . waitForTxCompletion ( fbTx ) ;
127191 } catch ( err : any ) {
128- throw new Error ( ' Fireblocks signer (signAndBroadcastWithFB): ' + err ) ;
192+ throw new Error ( " Fireblocks signer (signAndBroadcastWithFB): " + err ) ;
129193 }
130194 }
131195}
132-
0 commit comments