@fireblocks/psbt-sdk
Version:
SDK for signing Partially Signed Bitcoin Transactions (PSBTs) using Fireblocks
127 lines • 6.04 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const dotenv_1 = require("dotenv");
(0, dotenv_1.config)();
const src_1 = require("../src/");
const assert = __importStar(require("assert"));
const bitcoin = __importStar(require("bitcoinjs-lib"));
const secp256k1_1 = __importDefault(require("@bitcoinerlab/secp256k1"));
bitcoin.initEccLib(secp256k1_1.default);
const ts_sdk_1 = require("@fireblocks/ts-sdk");
const utils_1 = require("./utils");
const debug_1 = __importDefault(require("debug"));
const debugLog = (0, debug_1.default)("fireblocks_psbt_sdk:tests");
describe("Fireblocks PSBT SDK", function () {
this.timeout(600000);
const vaultId = process.env.FIREBLOCKS_VAULT_ID || "0";
const assetId = (process.env.FIREBLOCKS_ASSET_ID || "BTC_TEST");
const fireblocksConfig = {
apiKey: process.env.FIREBLOCKS_API_KEY,
secretKey: process.env.FIREBLOCKS_SECRET_KEY,
};
const fireblocks = new ts_sdk_1.Fireblocks(fireblocksConfig);
let testPsbt;
this.beforeAll(async function () {
testPsbt = await (0, utils_1.createTestPsbt)(fireblocks, vaultId, assetId);
});
describe("PsbtSigner", function () {
it("Sign with all keys of a vault", async function () {
const psbtSigner = await src_1.PsbtSigner.create({
assetId,
vaultId,
});
const signedPsbt = await psbtSigner.signBase64(testPsbt.toBase64());
const psbt = bitcoin.Psbt.fromBase64(signedPsbt);
debugLog(`PsbtSigner - Signed PSBT:\n${psbt.toBase64()}`);
assert.equal(psbt.validateSignaturesOfAllInputs(utils_1.signatureValidator), true);
psbt.finalizeAllInputs();
debugLog(`PsbtSigner (Sign with all keys of a vault) - Signed transaction:\n${psbt
.extractTransaction()
.toHex()}`);
});
});
describe("FireblocksSigner", function () {
it("Sign with a single key", async function () {
const psbt = testPsbt.clone();
// Create FireblocksSigner for the first address of the vault
const fireblocksSigner = await src_1.FireblocksSigner.create({
assetId,
vaultId,
addressIndex: 0,
note: `Signing PSBT with FireblocksSigner: ${psbt.toBase64()}`,
});
// Sign the PSBT
await psbt.signAllInputsAsync(fireblocksSigner).catch((error) => {
if (error.message !== "No inputs were signed") {
throw error;
}
});
debugLog(`FireblocksSigner (Sign with a single key) - Signed PSBT:\n${psbt.toBase64()}`);
assert.equal(psbt.validateSignaturesOfInput(psbt.data.inputs.findIndex((i) => i.partialSig), utils_1.signatureValidator), true);
});
it("Sign with all keys of a vault", async function () {
const psbt = testPsbt.clone();
const vaultAddressIndexes = await getVaultAddresses(fireblocks, vaultId, assetId);
// Create FireblocksSigner for each key in the vault
const fireblocksSigners = await Promise.all(vaultAddressIndexes.map((addressIndex) => src_1.FireblocksSigner.create({
assetId,
vaultId,
addressIndex,
note: `Signing PSBT with FireblocksSigner: ${psbt.toBase64()}`,
})));
// Sign the PSBT with all signers
await Promise.all(fireblocksSigners.map((signer) => psbt.signAllInputsAsync(signer).catch((error) => {
if (error.message !== "No inputs were signed") {
throw error;
}
})));
debugLog(`FireblocksSigner (Sign with all keys of a vault) - Signed PSBT:\n${psbt.toBase64()}`);
assert.equal(psbt.validateSignaturesOfAllInputs(utils_1.signatureValidator), true);
psbt.finalizeAllInputs();
debugLog(`FireblocksSigner (Sign with all keys of a vault) - Signed transaction:\n${psbt
.extractTransaction()
.toHex()}`);
});
});
});
async function getVaultAddresses(fireblocks, vaultId, assetId) {
var _a, _b;
const addressesResponse = await fireblocks.vaults.getVaultAccountAssetAddressesPaginated({
vaultAccountId: vaultId,
assetId,
});
const vaultAddressIndexes = [
...new Set((_b = (_a = addressesResponse.data) === null || _a === void 0 ? void 0 : _a.addresses) === null || _b === void 0 ? void 0 : _b.map((addr) => addr.bip44AddressIndex)),
];
if (!vaultAddressIndexes || !vaultAddressIndexes.length) {
throw new Error(`No addresses found for vault ${vaultId} and asset ${assetId}`);
}
return vaultAddressIndexes;
}
//# sourceMappingURL=index.test.js.map