From cb47f123290e7b9fb3ec6c066d05f02140457b26 Mon Sep 17 00:00:00 2001 From: Arno Simon Date: Thu, 14 Nov 2024 17:18:06 +0100 Subject: [PATCH] add signPolTx with raw signing --- src/fireblocks.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/fireblocks.ts b/src/fireblocks.ts index d125855..373b3b3 100644 --- a/src/fireblocks.ts +++ b/src/fireblocks.ts @@ -756,7 +756,7 @@ export class FireblocksService { } /** - * Sign and broadcast an ETH transaction with given integration using Fireblocks contract call feature + * Sign an ETH transaction with given integration using Fireblocks raw signing */ async signEthTx( integration: FireblocksIntegration, @@ -831,6 +831,61 @@ export class FireblocksService { return await fbSigner.signAndBroadcastWith(payload, assetId, tx, integration.fireblocksDestinationId, true, fbNote); } + /** + * Sign a POL transaction with given integration using Fireblocks raw signing + */ + async signPolTx( + integration: FireblocksIntegration, + tx: components['schemas']['POLUnsignedTx'], + assetId: 'ETH_TEST5' | 'ETH', + note?: string, + ): Promise<{ + signed_tx: { data: components['schemas']['POLSignedTx'] }; + fireblocks_tx: TransactionResponse; + }> { + const payload = { + rawMessageData: { + messages: [ + { + content: tx.unsigned_tx_hash, + preHash: { + content: tx.unsigned_tx_serialized, + hashAlgorithm: 'KECCAK256', + }, + }, + ], + }, + }; + + const fbSigner = this.getSigner(integration); + const fbNote = note ? note : 'POL tx from @kilnfi/sdk'; + const fbTx = await fbSigner.sign(payload, assetId, fbNote); + + const signature = fbTx?.signedMessages?.[0]?.signature; + + if (!signature) { + throw new Error('Fireblocks signature is missing'); + } + + const preparedTx = await this.client.POST('/v1/pol/transaction/prepare', { + body: { + unsigned_tx_serialized: tx.unsigned_tx_serialized, + r: `0x${signature.r}`, + s: `0x${signature.s}`, + v: signature.v ?? 0, + }, + }); + + if (preparedTx.error) { + throw new Error('Failed to prepare transaction'); + } + + return { + signed_tx: preparedTx.data, + fireblocks_tx: fbTx, + }; + } + /** * Sign and broadcast a POL transaction with given integration using Fireblocks contract call feature */