Skip to content

Commit

Permalink
fix signKavaTx and add kava example
Browse files Browse the repository at this point in the history
  • Loading branch information
nooxx committed Nov 14, 2024
1 parent e3f137b commit 7245432
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
48 changes: 48 additions & 0 deletions examples/kava.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { kavaToUkava, Kiln } from "../src/kiln";
import fs from "node:fs";
import 'dotenv/config'
import type { FireblocksIntegration } from "../src/fireblocks.ts";


const apiSecret = fs.readFileSync(`${__dirname}/fireblocks_secret_prod.key`, 'utf8');

const k = new Kiln({
baseUrl: process.env.KILN_API_URL as string,
apiToken: process.env.KILN_API_KEY as string,
});

const vault: FireblocksIntegration = {
provider: 'fireblocks',
fireblocksApiKey: process.env.FIREBLOCKS_API_KEY as string,
fireblocksSecretKey: apiSecret,
vaultId: 37
};

try {
console.log('crafting...');
const tx = await k.client.POST(
'/v1/kava/transaction/stake',
{
body: {
account_id: process.env.KILN_ACCOUNT_ID as string,
pubkey: '0233335b6c68a85e01b85055d0e8c2fcef42fed977898422ef3a5f6baf9a9a413e',
validator: 'kavavaloper1djqecw6nn5tydxq0shan7srv8j65clsf79myt8',
amount_ukava: kavaToUkava('0.01').toString(),
}
}
);
console.log('signing...');
if(!tx.data?.data) throw new Error('No data in response');
const signResponse = await k.fireblocks.signKavaTx(vault, tx.data.data);
console.log('broadcasting...');
if(!signResponse.signed_tx?.data?.signed_tx_serialized) throw new Error('No signed_tx in response');
const broadcastedTx = await k.client.POST("/v1/kava/transaction/broadcast", {
body: {
tx_serialized: signResponse.signed_tx.data.signed_tx_serialized,
}
});
console.log(broadcastedTx);

} catch (err) {
console.log(err);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kilnfi/sdk",
"version": "3.1.15",
"version": "3.1.16",
"autor": "Kiln <[email protected]> (https://kiln.fi)",
"license": "BUSL-1.1",
"description": "JavaScript sdk for Kiln API",
Expand Down
4 changes: 3 additions & 1 deletion src/fireblocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,18 +414,20 @@ export class FireblocksService {
messages: [
{
content: tx.unsigned_tx_hash,
derivationPath: [44, 459, integration.vaultId, 0, 0],
preHash: {
content: tx.unsigned_tx_serialized,
hashAlgorithm: 'SHA256',
},
},
],
algorithm: SigningAlgorithm.MPC_ECDSA_SECP256K1,
},
};

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

if (!signature) {
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ export const tonToNanoton = (ton: string): bigint => {
return parseUnits(ton, 9);
};

/**
* Convert KAVA to uKAVA
*/
export const kavaToUkava = (kava: string): bigint => {
return parseUnits(kava, 6);
}

/**
* Convert uZETA to ZETA
*/
Expand Down

0 comments on commit 7245432

Please sign in to comment.