Skip to content

Commit c57ee8c

Browse files
committed
Merge branch 'main' of github.com:kilnfi/sdk-js
2 parents 73c3fe1 + c8cfafd commit c57ee8c

File tree

8 files changed

+139
-55
lines changed

8 files changed

+139
-55
lines changed

examples/.env.example

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
KILN_API_KEY=
2-
FIREBLOCKS_API_KEY=
1+
KILN_API_URL=""
2+
KILN_ACCOUNT_ID=""
3+
KILN_API_KEY=""
4+
FIREBLOCKS_API_KEY=""

examples/eth.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Kiln } from "../src/kiln";
2+
import type { Integration } from "../lib/types/integrations";
3+
import fs from "node:fs";
4+
import 'dotenv/config'
5+
6+
7+
const apiSecret = fs.readFileSync(`${__dirname}/fireblocks_secret.key`, 'utf8');
8+
9+
const k = new Kiln({
10+
baseUrl: process.env.KILN_API_URL,
11+
apiToken: process.env.KILN_API_KEY,
12+
});
13+
14+
const vault: Integration = {
15+
provider: 'fireblocks',
16+
fireblocksApiKey: process.env.FIREBLOCKS_API_KEY,
17+
fireblocksSecretKey: apiSecret,
18+
vaultId: 14,
19+
fireblocksDestinationId: '07df91b4-7788-4833-a8f4-428facef68cc',
20+
};
21+
22+
try {
23+
console.log('crafting...');
24+
const tx = await k.client.POST(
25+
'/v1/eth/transaction/stake',
26+
{
27+
body: {
28+
account_id: process.env.KILN_ACCOUNT_ID,
29+
wallet: '0x91CcA1b774350232391d337213C0dE544DF1Ed75',
30+
amount_wei: '32000000000000000000'
31+
}
32+
}
33+
);
34+
// Sign and broadcast using Fireblocks contract calls
35+
// console.log('signing and broadcasting...');
36+
// const res = await k.fireblocks.signAndBroadcastEthTx(vault, tx.data.data, 'ETH_TEST6');
37+
// console.log(res);
38+
39+
// Sign and broadcast using Fireblocks raw signing and Kiln Connect to broadcast
40+
console.log('signing...');
41+
const signResponse = await k.fireblocks.signEthTx(vault, tx.data.data, "ETH_TEST6");
42+
// console.log('broadcasting...');
43+
// const broadcastedTx = await k.client.POST("/v1/eth/transaction/broadcast", {
44+
// body: {
45+
// tx_serialized: signResponse.signed_tx.data.signed_tx_serialized,
46+
// }
47+
// });
48+
// console.log(broadcastedTx);
49+
50+
} catch (err) {
51+
console.log(err);
52+
}

examples/near.ts

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,43 @@ import fs from "node:fs";
44
import 'dotenv/config'
55

66

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

9-
const f = async () => {
10-
const k = new Kiln({
11-
baseUrl: 'https://api.testnet.kiln.fi',
12-
apiToken: process.env.KILN_API_KEY,
13-
});
9+
const k = new Kiln({
10+
baseUrl: 'https://api.testnet.kiln.fi',
11+
apiToken: process.env.KILN_API_KEY,
12+
});
1413

15-
const vault: Integration = {
16-
provider: 'fireblocks',
17-
fireblocksApiKey: process.env.FIREBLOCKS_API_KEY,
18-
fireblocksSecretKey: apiSecret,
19-
vaultId: 14
20-
};
14+
const vault: Integration = {
15+
provider: 'fireblocks',
16+
fireblocksApiKey: process.env.FIREBLOCKS_API_KEY,
17+
fireblocksSecretKey: apiSecret,
18+
vaultId: 14
19+
};
2120

22-
try {
23-
console.log('crafting...');
24-
const tx = await k.client.POST(
25-
'/v1/near/transaction/stake',
26-
{
27-
body: {
28-
account_id: 'd3f1b917-72b1-4982-a4dd-93fce579a708',
29-
wallet: 'c36b1a5da2e60d1fd5d3a6b46f7399eb26571457f3272f3c978bc9527ad2335f',
30-
pool_id: 'kiln.pool.f863973.m0',
31-
amount_yocto: '1000000000000000000000000',
32-
}
33-
}
34-
);
35-
console.log('signing...');
36-
const signResponse = await k.fireblocks.signNearTx(vault, tx.data.data, "NEAR_TEST");
37-
console.log('broadcasting...');
38-
const broadcastedTx = await k.client.POST("/v1/near/transaction/broadcast", {
21+
try {
22+
console.log('crafting...');
23+
const tx = await k.client.POST(
24+
'/v1/near/transaction/stake',
25+
{
3926
body: {
40-
signed_tx_serialized: signResponse.signed_tx.data.signed_tx_serialized,
27+
account_id: 'd3f1b917-72b1-4982-a4dd-93fce579a708',
28+
wallet: 'c36b1a5da2e60d1fd5d3a6b46f7399eb26571457f3272f3c978bc9527ad2335f',
29+
pool_id: 'kiln.pool.f863973.m0',
30+
amount_yocto: '1000000000000000000000000',
4131
}
42-
});
43-
console.log(broadcastedTx);
44-
45-
} catch (err) {
46-
console.log(err);
47-
}
48-
};
32+
}
33+
);
34+
console.log('signing...');
35+
const signResponse = await k.fireblocks.signNearTx(vault, tx.data.data, "NEAR_TEST");
36+
console.log('broadcasting...');
37+
const broadcastedTx = await k.client.POST("/v1/near/transaction/broadcast", {
38+
body: {
39+
signed_tx_serialized: signResponse.signed_tx.data.signed_tx_serialized,
40+
}
41+
});
42+
console.log(broadcastedTx);
4943

50-
f();
44+
} catch (err) {
45+
console.log(err);
46+
}

src/api.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/fireblocks.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,52 @@ export class FireblocksService {
619619
};
620620
}
621621

622+
/**
623+
* Sign and broadcast an ETH transaction with given integration using Fireblocks contract call feature
624+
* @param integration
625+
* @param tx
626+
* @param assetId
627+
* @param note
628+
*/
629+
async signEthTx(
630+
integration: FireblocksIntegration,
631+
tx: components['schemas']['ETHUnsignedTx'],
632+
assetId: 'ETH_TEST6' | 'ETH',
633+
note?: string,
634+
) {
635+
const payload = {
636+
rawMessageData: {
637+
messages: [
638+
{
639+
content: tx.unsigned_tx_hash,
640+
preHash: {
641+
content: tx.unsigned_tx_serialized,
642+
hashAlgorithm: 'KECCAK256',
643+
},
644+
},
645+
],
646+
},
647+
};
648+
649+
const fbSigner = this.getSigner(integration);
650+
const fbNote = note ? note : 'ETH tx from @kilnfi/sdk';
651+
const fbTx = await fbSigner.sign(payload, assetId, fbNote);
652+
653+
const preparedTx = await this.client.POST('/v1/eth/transaction/prepare', {
654+
body: {
655+
unsigned_tx_serialized: tx.unsigned_tx_serialized,
656+
r: `0x${fbTx?.signedMessages?.[0].signature.r}`,
657+
s: `0x${fbTx?.signedMessages?.[0].signature.s}`,
658+
v: fbTx?.signedMessages?.[0].signature.v ?? 0,
659+
},
660+
});
661+
662+
return {
663+
signed_tx: preparedTx.data,
664+
fireblocks_tx: fbTx,
665+
};
666+
}
667+
622668
/**
623669
* Sign and broadcast an ETH transaction with given integration using Fireblocks contract call feature
624670
* @param integration

src/kiln.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import type { paths } from './openapi/schema';
12
export * from './validators';
2-
export type * from './openapi/schema.d.ts';
3+
export * from './openapi/schema';
34
export * from './utils';
45
import createClient, { type Client } from 'openapi-fetch';
56
import { FireblocksService } from './fireblocks';
6-
import type { paths } from './openapi/schema.d.ts';
77

88
type Config = {
99
baseUrl: string;

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"declaration": true,
66
"outDir": "./lib",
77
"strict": true,
8-
"esModuleInterop": false,
8+
"esModuleInterop": true,
99
"allowSyntheticDefaultImports": true,
1010
"forceConsistentCasingInFileNames": true,
1111
"verbatimModuleSyntax": false,

0 commit comments

Comments
 (0)