Skip to content

Commit

Permalink
Merge branch 'main' of github.com:kilnfi/sdk-js
Browse files Browse the repository at this point in the history
  • Loading branch information
nooxx committed Sep 24, 2024
2 parents 73c3fe1 + c8cfafd commit c57ee8c
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 55 deletions.
6 changes: 4 additions & 2 deletions examples/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
KILN_API_KEY=
FIREBLOCKS_API_KEY=
KILN_API_URL=""
KILN_ACCOUNT_ID=""
KILN_API_KEY=""
FIREBLOCKS_API_KEY=""
52 changes: 52 additions & 0 deletions examples/eth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Kiln } from "../src/kiln";
import type { Integration } from "../lib/types/integrations";
import fs from "node:fs";
import 'dotenv/config'


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

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

const vault: Integration = {
provider: 'fireblocks',
fireblocksApiKey: process.env.FIREBLOCKS_API_KEY,
fireblocksSecretKey: apiSecret,
vaultId: 14,
fireblocksDestinationId: '07df91b4-7788-4833-a8f4-428facef68cc',
};

try {
console.log('crafting...');
const tx = await k.client.POST(
'/v1/eth/transaction/stake',
{
body: {
account_id: process.env.KILN_ACCOUNT_ID,
wallet: '0x91CcA1b774350232391d337213C0dE544DF1Ed75',
amount_wei: '32000000000000000000'
}
}
);
// Sign and broadcast using Fireblocks contract calls
// console.log('signing and broadcasting...');
// const res = await k.fireblocks.signAndBroadcastEthTx(vault, tx.data.data, 'ETH_TEST6');
// console.log(res);

// Sign and broadcast using Fireblocks raw signing and Kiln Connect to broadcast
console.log('signing...');
const signResponse = await k.fireblocks.signEthTx(vault, tx.data.data, "ETH_TEST6");
// console.log('broadcasting...');
// const broadcastedTx = await k.client.POST("/v1/eth/transaction/broadcast", {
// body: {
// tx_serialized: signResponse.signed_tx.data.signed_tx_serialized,
// }
// });
// console.log(broadcastedTx);

} catch (err) {
console.log(err);
}
72 changes: 34 additions & 38 deletions examples/near.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,43 @@ import fs from "node:fs";
import 'dotenv/config'


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

const f = async () => {
const k = new Kiln({
baseUrl: 'https://api.testnet.kiln.fi',
apiToken: process.env.KILN_API_KEY,
});
const k = new Kiln({
baseUrl: 'https://api.testnet.kiln.fi',
apiToken: process.env.KILN_API_KEY,
});

const vault: Integration = {
provider: 'fireblocks',
fireblocksApiKey: process.env.FIREBLOCKS_API_KEY,
fireblocksSecretKey: apiSecret,
vaultId: 14
};
const vault: Integration = {
provider: 'fireblocks',
fireblocksApiKey: process.env.FIREBLOCKS_API_KEY,
fireblocksSecretKey: apiSecret,
vaultId: 14
};

try {
console.log('crafting...');
const tx = await k.client.POST(
'/v1/near/transaction/stake',
{
body: {
account_id: 'd3f1b917-72b1-4982-a4dd-93fce579a708',
wallet: 'c36b1a5da2e60d1fd5d3a6b46f7399eb26571457f3272f3c978bc9527ad2335f',
pool_id: 'kiln.pool.f863973.m0',
amount_yocto: '1000000000000000000000000',
}
}
);
console.log('signing...');
const signResponse = await k.fireblocks.signNearTx(vault, tx.data.data, "NEAR_TEST");
console.log('broadcasting...');
const broadcastedTx = await k.client.POST("/v1/near/transaction/broadcast", {
try {
console.log('crafting...');
const tx = await k.client.POST(
'/v1/near/transaction/stake',
{
body: {
signed_tx_serialized: signResponse.signed_tx.data.signed_tx_serialized,
account_id: 'd3f1b917-72b1-4982-a4dd-93fce579a708',
wallet: 'c36b1a5da2e60d1fd5d3a6b46f7399eb26571457f3272f3c978bc9527ad2335f',
pool_id: 'kiln.pool.f863973.m0',
amount_yocto: '1000000000000000000000000',
}
});
console.log(broadcastedTx);

} catch (err) {
console.log(err);
}
};
}
);
console.log('signing...');
const signResponse = await k.fireblocks.signNearTx(vault, tx.data.data, "NEAR_TEST");
console.log('broadcasting...');
const broadcastedTx = await k.client.POST("/v1/near/transaction/broadcast", {
body: {
signed_tx_serialized: signResponse.signed_tx.data.signed_tx_serialized,
}
});
console.log(broadcastedTx);

f();
} catch (err) {
console.log(err);
}
12 changes: 0 additions & 12 deletions src/api.ts

This file was deleted.

46 changes: 46 additions & 0 deletions src/fireblocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,52 @@ export class FireblocksService {
};
}

/**
* Sign and broadcast an ETH transaction with given integration using Fireblocks contract call feature
* @param integration
* @param tx
* @param assetId
* @param note
*/
async signEthTx(
integration: FireblocksIntegration,
tx: components['schemas']['ETHUnsignedTx'],
assetId: 'ETH_TEST6' | 'ETH',
note?: string,
) {
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 : 'ETH tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, assetId, fbNote);

const preparedTx = await this.client.POST('/v1/eth/transaction/prepare', {
body: {
unsigned_tx_serialized: tx.unsigned_tx_serialized,
r: `0x${fbTx?.signedMessages?.[0].signature.r}`,
s: `0x${fbTx?.signedMessages?.[0].signature.s}`,
v: fbTx?.signedMessages?.[0].signature.v ?? 0,
},
});

return {
signed_tx: preparedTx.data,
fireblocks_tx: fbTx,
};
}

/**
* Sign and broadcast an ETH transaction with given integration using Fireblocks contract call feature
* @param integration
Expand Down
4 changes: 2 additions & 2 deletions src/kiln.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { paths } from './openapi/schema';
export * from './validators';
export type * from './openapi/schema.d.ts';
export * from './openapi/schema';
export * from './utils';
import createClient, { type Client } from 'openapi-fetch';
import { FireblocksService } from './fireblocks';
import type { paths } from './openapi/schema.d.ts';

type Config = {
baseUrl: string;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"declaration": true,
"outDir": "./lib",
"strict": true,
"esModuleInterop": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"verbatimModuleSyntax": false,
Expand Down

0 comments on commit c57ee8c

Please sign in to comment.