Skip to content

Commit

Permalink
update fireblocks to ts-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
LeTamanoir committed Jan 7, 2025
1 parent 65a152f commit d67aa54
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 108 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
},
"homepage": "https://github.com/kilnfi/sdk-js#readme",
"dependencies": {
"@fireblocks/ts-sdk": "^6.0.0",
"@types/bun": "^1.1.11",
"bech32": "^2.0.0",
"fireblocks-sdk": "^5.32.0",
"openapi-fetch": "^0.12.0",
"viem": "^2.21.29"
},
Expand Down
121 changes: 59 additions & 62 deletions src/fireblocks.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import {
type AssetTypeResponse,
FireblocksSDK,
type PublicKeyResponse,
type SDKOptions,
SigningAlgorithm,
type ConfigurationOptions,
Fireblocks,
type PublicKeyInformation,
SignedMessageAlgorithmEnum,
type TransactionResponse,
} from 'fireblocks-sdk';
import type { IAuthProvider } from 'fireblocks-sdk/dist/src/iauth-provider.js';
} from '@fireblocks/ts-sdk';

import type { Client } from 'openapi-fetch';
import { FireblocksSigner } from './fireblocks_signer.js';
import type { components, paths } from './openapi/schema.js';

export type FireblocksIntegration = {
provider: 'fireblocks';
fireblocksApiKey: string;
fireblocksSecretKey: string;
vaultId: number;
name?: string;
fireblocksDestinationId?: string;
fireblocksApiBaseUrl?: string;
fireblocksAuthProvider?: IAuthProvider;
fireblocksSdkOptions?: SDKOptions;
export type FireblocksIntegration = (
| { config?: never; instance: Fireblocks }
| { config: ConfigurationOptions; instance?: never }
) & {
vaultId: `${number}`;
};

export class FireblocksService {
Expand All @@ -33,14 +28,11 @@ export class FireblocksService {
/**
* Retrieve a fireblocks SDK from a Fireblocks integration
*/
getSdk(integration: FireblocksIntegration): FireblocksSDK {
return new FireblocksSDK(
integration.fireblocksSecretKey,
integration.fireblocksApiKey,
integration.fireblocksApiBaseUrl,
integration.fireblocksAuthProvider,
integration.fireblocksSdkOptions,
);
getSdk(integration: FireblocksIntegration): Fireblocks {
if (integration.instance) {
return integration.instance;
}
return new Fireblocks(integration.config);
}

/**
Expand All @@ -54,24 +46,24 @@ export class FireblocksService {
/**
* Get fireblocks wallet pubkey compressed
*/
async getPubkey(integration: FireblocksIntegration, assetId: string): Promise<PublicKeyResponse> {
async getPubkey(integration: FireblocksIntegration, assetId: string): Promise<PublicKeyInformation> {
const fbSdk = this.getSdk(integration);
const data = await fbSdk.getPublicKeyInfoForVaultAccount({
const data = await fbSdk.vaults.getPublicKeyInfoForAddress({
assetId: assetId,
vaultAccountId: integration.vaultId,
change: 0,
addressIndex: 0,
compressed: true,
});
return data;
return data.data;
}

/**
* List Fireblocks supported assets
*/
async getAssets(integration: FireblocksIntegration): Promise<AssetTypeResponse[]> {
const fbSdk = this.getSdk(integration);
return await fbSdk.getSupportedAssets();
return (await fbSdk.blockchainsAssets.getSupportedAssets()).data;
}

/**
Expand Down Expand Up @@ -101,8 +93,9 @@ export class FireblocksService {
const fbTx = await fbSigner.sign(payload, assetId, fbNote);

const signatures = fbTx.signedMessages
?.filter((signedMessage) => signedMessage.derivationPath[3] === 0)
.map((signedMessage) => signedMessage.signature.fullSig);
?.filter((signedMessage) => signedMessage.derivationPath?.[3] === 0)
.map((signedMessage) => signedMessage.signature?.fullSig)
.filter((s) => s !== undefined);
if (!signatures) {
throw new Error('Fireblocks signature is missing');
}
Expand Down Expand Up @@ -156,12 +149,10 @@ export class FireblocksService {
const fbNote = note ? note : 'ADA tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, 'ADA', fbNote);

const signedMessages = fbTx.signedMessages?.map((message) => {
return {
pubkey: message.publicKey,
signature: message.signature.fullSig,
};
});
const signedMessages = fbTx.signedMessages?.map((message) => ({
pubkey: message.publicKey as string,
signature: message.signature?.fullSig as string,
}));
if (!signedMessages) {
throw new Error('Fireblocks signature is missing');
}
Expand Down Expand Up @@ -211,7 +202,7 @@ export class FireblocksService {
const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'ATOM tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, 'ATOM_COS', fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down Expand Up @@ -264,7 +255,7 @@ export class FireblocksService {
const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'DYDX tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, 'DYDX_DYDX', fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down Expand Up @@ -312,14 +303,14 @@ export class FireblocksService {
},
},
],
algorithm: SigningAlgorithm.MPC_ECDSA_SECP256K1,
algorithm: SignedMessageAlgorithmEnum.EcdsaSecp256K1,
},
};

const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'FET tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, undefined, fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down Expand Up @@ -367,14 +358,14 @@ export class FireblocksService {
},
},
],
algorithm: SigningAlgorithm.MPC_ECDSA_SECP256K1,
algorithm: SignedMessageAlgorithmEnum.EcdsaSecp256K1,
},
};

const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'OM tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, undefined, fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down Expand Up @@ -427,7 +418,7 @@ export class FireblocksService {
const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'INJ tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, 'INJ_INJ', fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down Expand Up @@ -475,14 +466,14 @@ export class FireblocksService {
},
},
],
algorithm: SigningAlgorithm.MPC_ECDSA_SECP256K1,
algorithm: SignedMessageAlgorithmEnum.EcdsaSecp256K1,
},
};

const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'KAVA tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, undefined, fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down Expand Up @@ -530,14 +521,14 @@ export class FireblocksService {
},
},
],
algorithm: SigningAlgorithm.MPC_ECDSA_SECP256K1,
algorithm: SignedMessageAlgorithmEnum.EcdsaSecp256K1,
},
};

const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'NOBLE tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, undefined, fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down Expand Up @@ -590,7 +581,7 @@ export class FireblocksService {
const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'OSMO tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, 'OSMO', fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down Expand Up @@ -643,7 +634,7 @@ export class FireblocksService {
const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'TIA tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, 'CELESTIA', fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down Expand Up @@ -691,14 +682,14 @@ export class FireblocksService {
},
},
],
algorithm: SigningAlgorithm.MPC_ECDSA_SECP256K1,
algorithm: SignedMessageAlgorithmEnum.EcdsaSecp256K1,
},
};

const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'ZETA tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, undefined, fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down Expand Up @@ -747,6 +738,11 @@ export class FireblocksService {
const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'DOT tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, 'DOT', fbNote);

if (!fbTx.signedMessages?.[0]?.signature?.fullSig) {
throw new Error('Fireblocks signature is missing');
}

const signature = `0x00${fbTx.signedMessages?.[0]?.signature.fullSig}`;

const preparedTx = await this.client.POST('/dot/transaction/prepare', {
Expand Down Expand Up @@ -790,6 +786,11 @@ export class FireblocksService {
const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'KSM tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, 'KSM', fbNote);

if (!fbTx.signedMessages?.[0]?.signature?.fullSig) {
throw new Error('Fireblocks signature is missing');
}

const signature = `0x00${fbTx.signedMessages?.[0]?.signature.fullSig}`;

const preparedTx = await this.client.POST('/ksm/transaction/prepare', {
Expand Down Expand Up @@ -871,18 +872,16 @@ export class FireblocksService {
integration: FireblocksIntegration,
tx: components['schemas']['ETHUnsignedTx'],
assetId: 'ETH_TEST6' | 'ETH',
fireblocksDestinationId: string,
note?: string,
) {
if (!integration.fireblocksDestinationId) {
throw new Error('Fireblocks destination id is missing');
}
const payload = {
contractCallData: tx.contract_call_data,
};

const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'ETH tx from @kilnfi/sdk';
return await fbSigner.signAndBroadcastWith(payload, assetId, tx, integration.fireblocksDestinationId, true, fbNote);
return await fbSigner.signAndBroadcastWith(payload, assetId, tx, fireblocksDestinationId, true, fbNote);
}

/**
Expand Down Expand Up @@ -947,18 +946,16 @@ export class FireblocksService {
integration: FireblocksIntegration,
tx: components['schemas']['POLUnsignedTx'],
assetId: 'ETH_TEST5' | 'ETH',
fireblocksDestinationId: string,
note?: string,
) {
if (!integration.fireblocksDestinationId) {
throw new Error('Fireblocks destination id is missing');
}
const payload = {
contractCallData: tx.contract_call_data,
};

const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'POL tx from @kilnfi/sdk';
return await fbSigner.signAndBroadcastWith(payload, assetId, tx, integration.fireblocksDestinationId, true, fbNote);
return await fbSigner.signAndBroadcastWith(payload, assetId, tx, fireblocksDestinationId, true, fbNote);
}

/**
Expand Down Expand Up @@ -986,7 +983,7 @@ export class FireblocksService {
const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'TON tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, assetId, fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down Expand Up @@ -1035,7 +1032,7 @@ export class FireblocksService {
const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'XTZ tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, assetId, fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down Expand Up @@ -1083,7 +1080,7 @@ export class FireblocksService {
const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'NEAR tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, assetId, fbNote);
const signature = fbTx.signedMessages?.[0]?.signature.fullSig;
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;

if (!signature) {
throw new Error('Fireblocks signature is missing');
Expand Down
Loading

0 comments on commit d67aa54

Please sign in to comment.