Skip to content

Commit 54a0c99

Browse files
committed
Sdk add cronos (#192)
* add cronos sign fx and example * bump v4.1.4
1 parent 4abda2a commit 54a0c99

File tree

5 files changed

+161
-1
lines changed

5 files changed

+161
-1
lines changed

examples/cronos.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { Kiln, KILN_VALIDATORS, croToBasecro } from '../src/kiln.ts';
2+
import type { FireblocksIntegration } from '../src/fireblocks.ts';
3+
import { loadEnv } from './env.ts';
4+
5+
const { kilnApiKey, kilnAccountId, kilnApiUrl, fireblocksApiKey, fireblocksApiSecret, fireblocksVaultId } =
6+
await loadEnv();
7+
8+
const k = new Kiln({
9+
baseUrl: kilnApiUrl,
10+
apiToken: kilnApiKey,
11+
});
12+
13+
const vault: FireblocksIntegration = {
14+
config: {
15+
apiKey: fireblocksApiKey,
16+
secretKey: fireblocksApiSecret,
17+
basePath: 'https://api.fireblocks.io/v1',
18+
},
19+
vaultId: fireblocksVaultId,
20+
};
21+
22+
//
23+
// Get the pubkey from Fireblocks
24+
//
25+
const fireblocksPubkey = (
26+
await k.fireblocks.getSdk(vault).vaults.getPublicKeyInfo({
27+
algorithm: 'MPC_ECDSA_SECP256K1',
28+
derivationPath: JSON.stringify([44, 394, Number(vault.vaultId), 0, 0]),
29+
compressed: true,
30+
})
31+
).data.publicKey;
32+
if (!fireblocksPubkey) {
33+
console.log('Failed to get pubkey');
34+
process.exit(0);
35+
}
36+
37+
//
38+
// Craft the transaction
39+
//
40+
console.log('Crafting transaction...');
41+
const txRequest = await k.client.POST('/cro/transaction/stake', {
42+
body: {
43+
account_id: kilnAccountId,
44+
pubkey: fireblocksPubkey,
45+
validator: KILN_VALIDATORS.CRO.mainnet.KILN,
46+
amount_basecro: croToBasecro('0.01').toString(),
47+
restake_rewards: false,
48+
},
49+
});
50+
if (txRequest.error) {
51+
console.log('Failed to craft transaction:', txRequest);
52+
process.exit(1);
53+
} else {
54+
console.log('Crafted transaction:', txRequest.data);
55+
}
56+
console.log('\n\n\n');
57+
58+
//
59+
// Sign the transaction
60+
//
61+
console.log('Signing transaction...');
62+
const signRequest = await (async () => {
63+
try {
64+
return await k.fireblocks.signCroTx(vault, txRequest.data.data);
65+
} catch (err) {
66+
console.log('Failed to sign transaction:', err);
67+
process.exit(1);
68+
}
69+
})();
70+
console.log('Signed transaction:', signRequest);
71+
console.log('\n\n\n');
72+
73+
//
74+
// Broadcast the transaction
75+
//
76+
console.log('Broadcasting transaction...');
77+
const broadcastedRequest = await k.client.POST('/cro/transaction/broadcast', {
78+
body: {
79+
tx_serialized: signRequest.signed_tx.data.signed_tx_serialized,
80+
},
81+
});
82+
if (broadcastedRequest.error) {
83+
console.log('Failed to broadcast transaction:', broadcastedRequest);
84+
process.exit(1);
85+
} else {
86+
console.log('Broadcasted transaction:', broadcastedRequest.data);
87+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kilnfi/sdk",
3-
"version": "4.1.3",
3+
"version": "4.1.4",
44
"autor": "Kiln <[email protected]> (https://kiln.fi)",
55
"license": "BUSL-1.1",
66
"description": "JavaScript sdk for Kiln API",

src/fireblocks.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,61 @@ export class FireblocksService {
506506
};
507507
}
508508

509+
/**
510+
* Sign a CRO transaction on Fireblocks
511+
*/
512+
async signCroTx(
513+
integration: FireblocksIntegration,
514+
tx: components['schemas']['CROUnsignedTx'] | components['schemas']['CROStakeUnsignedTx'],
515+
note?: string,
516+
): Promise<{
517+
signed_tx: { data: components['schemas']['CROSignedTx'] };
518+
fireblocks_tx: TransactionResponse;
519+
}> {
520+
const payload = {
521+
rawMessageData: {
522+
messages: [
523+
{
524+
content: tx.unsigned_tx_hash,
525+
derivationPath: [44, 394, integration.vaultId, 0, 0],
526+
preHash: {
527+
content: tx.unsigned_tx_serialized,
528+
hashAlgorithm: 'SHA256',
529+
},
530+
},
531+
],
532+
algorithm: SignedMessageAlgorithmEnum.EcdsaSecp256K1,
533+
},
534+
};
535+
536+
const fbSigner = this.getSigner(integration);
537+
const fbNote = note ? note : 'CRO tx from @kilnfi/sdk';
538+
const fbTx = await fbSigner.sign(payload, undefined, fbNote);
539+
const signature = fbTx.signedMessages?.[0]?.signature?.fullSig;
540+
541+
if (!signature) {
542+
throw new Error(ERRORS.MISSING_SIGNATURE);
543+
}
544+
545+
const preparedTx = await this.client.POST('/cro/transaction/prepare', {
546+
body: {
547+
pubkey: tx.pubkey,
548+
tx_body: tx.tx_body,
549+
tx_auth_info: tx.tx_auth_info,
550+
signature: signature,
551+
},
552+
});
553+
554+
if (preparedTx.error) {
555+
throw new Error(ERRORS.FAILED_TO_PREPARE);
556+
}
557+
558+
return {
559+
signed_tx: preparedTx.data,
560+
fireblocks_tx: fbTx,
561+
};
562+
}
563+
509564
/**
510565
* Sign a NOBLE transaction on Fireblocks
511566
*/

src/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ export const omToUom = (om: string): bigint => {
7878
return parseUnits(om, 6);
7979
};
8080

81+
/**
82+
* Convert CRO to basecro
83+
*/
84+
export const croToBasecro = (cro: string): bigint => {
85+
return parseUnits(cro, 8);
86+
};
8187
/**
8288
* Convert uZETA to ZETA
8389
*/
@@ -204,6 +210,13 @@ export const uomToOm = (uom: bigint): string => {
204210
return formatUnits(uom, 6);
205211
};
206212

213+
/**
214+
* Convert basecro to CRO
215+
*/
216+
export const basecroToCro = (basecro: bigint): string => {
217+
return formatUnits(basecro, 8);
218+
};
219+
207220
/**
208221
* Get a cosmos address from its public key and prefix
209222
* @param pubkey

src/validators.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ export const KILN_VALIDATORS = {
9090
KILN: 'mantravaloper146mj09yzu3mvz7pmy4dvs4z9wr2mst7ram37xw',
9191
},
9292
},
93+
CRO: {
94+
mainnet: {
95+
KILN: 'crocncl1lqm9hhzuqgutgswlff5yf9kmn7vx6pmksk4kvs',
96+
},
97+
},
9398
OSMO: {
9499
mainnet: {
95100
INTEROP: 'osmovaloper146mj09yzu3mvz7pmy4dvs4z9wr2mst7rq8p8gy',

0 commit comments

Comments
 (0)