Skip to content

Commit

Permalink
[v2] add babylon craft stake + sign (#166)
Browse files Browse the repository at this point in the history
* add `@fireblocks/psbt-sdk` + `bitcoinjs-lib`

* add babylon functions

* nit

* fix
  • Loading branch information
LeTamanoir authored Dec 2, 2024
1 parent 11ddfa0 commit 40fe838
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
"@cosmjs/proto-signing": "^0.30.1",
"@cosmjs/stargate": "^0.30.1",
"@emurgo/cardano-serialization-lib-nodejs": "^11.5.0",
"@fireblocks/psbt-sdk": "^0.0.5",
"@solana/web3.js": "^1.93.0",
"@substrate/txwrapper-polkadot": "^7.5.1",
"@taquito/local-forging": "^17.5.2",
"@taquito/rpc": "^16.2.0",
"@types/bn.js": "^5.1.5",
"axios": "^1.7.2",
"bitcoinjs-lib": "^6.1.6",
"bn.js": "^5.2.1",
"ethers": "^5.7.2",
"fireblocks-sdk": "^4.2.0",
Expand Down
3 changes: 3 additions & 0 deletions src/kiln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { ZetaService } from "./services/zeta";
import { KILN_VALIDATORS as v } from "./validators";
import { KavaService } from "./services/kava";
import { PolService } from "./services/pol";
import { BabylonService } from "./services/babylon";

type Config = {
apiToken: string;
Expand Down Expand Up @@ -52,6 +53,7 @@ export class Kiln {
zeta: ZetaService;
kava: KavaService;
pol: PolService;
babylon: BabylonService;

constructor({ testnet, apiToken, baseUrl }: Config) {
api.defaults.headers.common.Authorization = `Bearer ${apiToken}`;
Expand Down Expand Up @@ -79,5 +81,6 @@ export class Kiln {
this.zeta = new ZetaService({ testnet });
this.kava = new KavaService({ testnet });
this.pol = new PolService({ testnet });
this.babylon = new BabylonService({ testnet });
}
}
67 changes: 67 additions & 0 deletions src/services/babylon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Service } from "./service";
import type { BitcoinTx, BitcoinSignedTx, BitcoinTxHash } from "../types/bitcoin";
import api from "../api";
import { Psbt } from "bitcoinjs-lib";
import type { ServiceProps } from "../types/service";
import type { Integration } from "../types/integrations";
import { FireblocksSigner as FireblocksPsbtSigner } from "@fireblocks/psbt-sdk";

export class BabylonService extends Service {
constructor({ testnet }: ServiceProps) {
super({ testnet });
}

/**
* Craft a babylon stake transaction
*/
async craftStakeTx(accountId: string, publicKey: string, amountSatoshi: number, timeLock: number, feeRate: number): Promise<BitcoinTx> {
const { data } = await api.post<BitcoinTx>('/v1/babylon/transaction/stake', {
account_id: accountId,
public_key: publicKey,
amount_satoshi: amountSatoshi,
time_lock: timeLock,
fee_rate: feeRate,
});
return data;
}

/**
* Sign transaction with given integration
* @param integration custody solution to sign with
* @param tx raw babylon transaction
* @param note note to identify the transaction in your custody solution
*/
async sign(integration: Integration, tx: BitcoinTx, note?: string): Promise<BitcoinSignedTx> {
const fbSigner = await FireblocksPsbtSigner.create({
fireblocks: {
basePath: "https://api.fireblocks.io/v1",
apiKey: integration.fireblocksApiKey,
secretKey: integration.fireblocksSecretKey,
},
assetId: this.testnet ? "BTC_TEST" : "BTC",
vaultId: integration.vaultId.toString(),
addressIndex: 0,
note: note ?? "BTC tx from @kilnfi/sdk",
});

const psbt = Psbt.fromHex(tx.data.unsigned_tx_serialized);

await psbt.signAllInputsAsync(fbSigner);

psbt.finalizeAllInputs();
const signedTransaction = psbt.extractTransaction().toHex();

return { data: { signed_tx_serialized: signedTransaction } };
}

/**
* Broadcast transaction to the network
* @param signedTx the transaction to broadcast
*/
async broadcast(signedTx: BitcoinSignedTx): Promise<BitcoinTxHash> {
const { data } = await api.post<BitcoinTxHash>('/v1/babylon/transaction/broadcast', {
tx_serialized: signedTx.data.signed_tx_serialized,
});
return data;
}
}
17 changes: 17 additions & 0 deletions src/types/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export type BitcoinTx = {
data: {
unsigned_tx_serialized: string;
};
};

export type BitcoinSignedTx = {
data: {
signed_tx_serialized: string;
};
};

export type BitcoinTxHash = {
data: {
tx_id: string;
};
};

0 comments on commit 40fe838

Please sign in to comment.