Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix signKavaTx and add kava example #154

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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