UNPKG

@fireblocks/psbt-sdk

Version:

SDK for signing Partially Signed Bitcoin Transactions (PSBTs) using Fireblocks

142 lines 6.07 kB
"use strict"; 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 }); exports.PsbtSigner = void 0; const fireblocksSigner_1 = require("./fireblocksSigner"); const fireblocksUtils_1 = require("./fireblocksUtils"); const constants_1 = require("./constants"); const bitcoin = __importStar(require("bitcoinjs-lib")); const secp256k1_1 = __importDefault(require("@bitcoinerlab/secp256k1")); bitcoin.initEccLib(secp256k1_1.default); class PsbtSigner { constructor(fireblocks, signers, note, batch = true) { this.fireblocks = fireblocks; this.signers = signers; this.note = note; this.batch = batch; } static async create({ fireblocks, assetId, vaultId, note, batch, limit, addressIndexes, }) { const fireblocksSDK = (0, fireblocksUtils_1.createFireblocksClient)(fireblocks || {}); if (addressIndexes && limit) { throw new Error("Cannot specify both bip44AddressIndexes and limit"); } const bip44AddressIndexes = addressIndexes !== null && addressIndexes !== void 0 ? addressIndexes : (await getAddressIndexes(fireblocksSDK, vaultId, assetId, limit)); const signers = await Promise.all(bip44AddressIndexes.map((addressIndex) => fireblocksSigner_1.FireblocksSigner.create({ fireblocks: fireblocks || {}, assetId, vaultId, addressIndex, }))); return new PsbtSigner(fireblocksSDK, signers, note, batch !== null && batch !== void 0 ? batch : true); } async signPsbt(psbt) { const note = this.note ? this.note : `${constants_1.PSBT_NOTE_PREFIX}${psbt.toBase64()}`; const signatureRequests = await this.gatherSignatureRequests(psbt); if (!this.batch || signatureRequests.length <= 1) { this.signers.map((signer) => (signer.note = note)); await signPsbtWithAllSigners(psbt, this.signers); } else { const signatures = await (0, fireblocksUtils_1.batchRawSign)({ fireblocks: this.fireblocks, signatureRequests: signatureRequests, note: note, }); await applySignatures(psbt, signatures); } return psbt; } async signHex(psbt) { const psbtObject = bitcoin.Psbt.fromHex(psbt.replace("0x", "")); await this.signPsbt(psbtObject); return psbtObject.toHex(); } async signBase64(psbt) { const psbtObject = bitcoin.Psbt.fromBase64(psbt); await this.signPsbt(psbtObject); return psbtObject.toBase64(); } async gatherSignatureRequests(psbt) { const signatureRequests = []; const fakeSigners = this.signers.map((signer) => ({ publicKey: signer.publicKey, sign: async (hash) => { signatureRequests.push({ signer, hash, }); }, })); await signPsbtWithAllSigners(psbt, fakeSigners); return signatureRequests; } } exports.PsbtSigner = PsbtSigner; async function applySignatures(psbt, signatures) { const presignedSigners = signatures.reduce((signers, { signer }) => { if (!signers.find((s) => s.publicKey.equals(signer.publicKey))) { signers.push({ publicKey: signer.publicKey, sign: async (hash) => { const matchingSignature = signatures.find((s) => s.hash.equals(hash) && s.signer.publicKey.equals(signer.publicKey)); if (matchingSignature) { return matchingSignature.signature; } throw new Error(`No matching signature found for hash ${hash.toString("hex")}`); }, }); } return signers; }, []); await signPsbtWithAllSigners(psbt, presignedSigners); } async function signPsbtWithAllSigners(psbt, signers) { await Promise.all(signers.map((signer) => psbt.signAllInputsAsync(signer).catch((error) => { if (error.message !== "No inputs were signed") { throw error; } }))); } async function getAddressIndexes(fireblocksSDK, vaultId, assetId, limit) { var _a, _b; const addressesResponse = await fireblocksSDK.vaults.getVaultAccountAssetAddressesPaginated({ vaultAccountId: vaultId, assetId, limit: limit !== null && limit !== void 0 ? limit : 10, }); const addressIndexes = [ ...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 (addressIndexes.length == 0) { throw new Error(`No addresses found for vault ${vaultId} and asset ${assetId}`); } return addressIndexes; } //# sourceMappingURL=psbtSigner.js.map