@fireblocks/psbt-sdk
Version:
SDK for signing Partially Signed Bitcoin Transactions (PSBTs) using Fireblocks
95 lines • 4.21 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 });
exports.signatureValidator = signatureValidator;
exports.estimateTransactionSize = estimateTransactionSize;
exports.calculateFee = calculateFee;
exports.createTestPsbt = createTestPsbt;
const bitcoin = __importStar(require("bitcoinjs-lib"));
const secp256k1_1 = __importDefault(require("@bitcoinerlab/secp256k1"));
bitcoin.initEccLib(secp256k1_1.default);
const ecpair_1 = __importDefault(require("ecpair"));
const mempool_js_1 = __importDefault(require("@mempool/mempool.js"));
const ECPair = (0, ecpair_1.default)(secp256k1_1.default);
function signatureValidator(pubkey, msghash, signature) {
return ECPair.fromPublicKey(pubkey).verify(msghash, signature);
}
function estimateTransactionSize(numInputs, numOutputs) {
return numInputs * 148 + numOutputs * 34 + 10;
}
function calculateFee(feeRate, numInputs, numOutputs) {
const txSize = estimateTransactionSize(numInputs, numOutputs);
return Math.floor(feeRate * txSize);
}
async function createTestPsbt(fireblocks, vaultId, assetId) {
var _a, _b;
const { bitcoin: mempool } = (0, mempool_js_1.default)({
hostname: "mempool.space",
network: assetId === "BTC_TEST" ? "testnet" : "mainnet",
});
// Fetch all address indexes for the vault
const addressesResponse = await fireblocks.vaults.getVaultAccountAssetAddressesPaginated({
vaultAccountId: vaultId,
assetId,
});
const addresses = (_b = (_a = addressesResponse.data) === null || _a === void 0 ? void 0 : _a.addresses) === null || _b === void 0 ? void 0 : _b.map((addr) => addr.address);
if (!addresses || !addresses.length) {
throw new Error(`No addresses found for vault ${vaultId} and asset ${assetId}`);
}
const utxos = await Promise.all(addresses.map(async (address) => {
const addressUtxos = await mempool.addresses.getAddressTxsUtxo({
address: address,
});
return addressUtxos;
})).then((results) => results.flat());
// Create a PSBT
const network = assetId === "BTC_TEST" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
const testPsbt = new bitcoin.Psbt({ network });
// Add inputs
await Promise.all(utxos.map(async (utxo) => {
const txHex = await mempool.transactions.getTxHex({ txid: utxo.txid });
testPsbt.addInput({
hash: utxo.txid,
index: utxo.vout,
nonWitnessUtxo: Buffer.from(txHex, "hex"),
});
}));
// Calculate fee using the calculateFee function
const feeRate = await fireblocks.transactions.estimateNetworkFee({
assetId,
});
const fee = calculateFee(parseFloat(feeRate.data.medium.feePerByte), utxos.length, 1);
// Add output (sending to the first address)
const totalAmount = utxos.reduce((sum, utxo) => sum + utxo.value, 0);
testPsbt.addOutput({
address: addresses[0],
value: totalAmount - fee,
});
return testPsbt;
}
//# sourceMappingURL=utils.js.map