@bcpros/crypto-wallet-core
Version:
A multi-currency support library for address derivation, private key creation, and transaction creation
142 lines • 6.14 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BTCTxProvider = void 0;
var _ = __importStar(require("lodash"));
var BTCTxProvider = (function () {
function BTCTxProvider() {
this.lib = require('@bcpros/bitcore-lib');
}
BTCTxProvider.prototype.selectCoins = function (recipients, utxos, fee) {
utxos = utxos.sort(function (a, b) {
return a.mintHeight - b.mintHeight;
});
var index = 0;
var utxoSum = 0;
var recepientSum = recipients.reduce(function (sum, cur) { return sum + Number(cur.amount); }, fee || 0);
while (utxoSum < recepientSum) {
var utxo = utxos[index];
utxoSum += Number(utxo.value);
index += 1;
}
var filteredUtxos = utxos.slice(0, index);
return filteredUtxos;
};
BTCTxProvider.prototype.create = function (_a) {
var _this = this;
var recipients = _a.recipients, _b = _a.utxos, utxos = _b === void 0 ? [] : _b, change = _a.change, feeRate = _a.feeRate, fee = _a.fee, isSweep = _a.isSweep, replaceByFee = _a.replaceByFee, lockUntilDate = _a.lockUntilDate, lockUntilBlock = _a.lockUntilBlock;
var filteredUtxos = isSweep ? utxos : this.selectCoins(recipients, utxos, fee);
var btcUtxos = filteredUtxos.map(function (utxo) {
var btcUtxo = Object.assign({}, utxo, {
amount: utxo.value / 1e8,
txid: utxo.mintTxid,
outputIndex: utxo.mintIndex
});
return new _this.lib.Transaction.UnspentOutput(btcUtxo);
});
var tx = new this.lib.Transaction().from(btcUtxos);
if (fee) {
tx.fee(fee);
}
if (feeRate) {
tx.feePerByte(Number(feeRate));
}
if (change) {
tx.change(change);
}
for (var _i = 0, recipients_1 = recipients; _i < recipients_1.length; _i++) {
var recipient = recipients_1[_i];
tx.to(recipient.address, parseInt(recipient.amount));
}
if (replaceByFee && typeof tx.enableRBF === 'function') {
tx.enableRBF();
}
if (lockUntilBlock > 0) {
tx.lockUntilBlockHeight(lockUntilBlock);
}
else if (lockUntilDate > 0) {
tx.lockUntilDate(lockUntilDate);
}
return tx.uncheckedSerialize();
};
BTCTxProvider.prototype.getSignature = function (params) {
throw new Error('function getSignature not implemented for UTXO coins');
};
BTCTxProvider.prototype.applySignature = function (params) {
throw new Error('function applySignature not implemented for UTXO coins');
};
BTCTxProvider.prototype.getHash = function (params) {
var bitcoreTx = new this.lib.Transaction(params.tx);
return bitcoreTx.hash;
};
BTCTxProvider.prototype.sign = function (params) {
var tx = params.tx, keys = params.keys, pubkeys = params.pubkeys, threshold = params.threshold, opts = params.opts;
var utxos = params.utxos || [];
var inputAddresses = this.getSigningAddresses({ tx: tx, utxos: utxos });
var bitcoreTx = new this.lib.Transaction(tx);
var applicableUtxos = this.getRelatedUtxos({
outputs: bitcoreTx.inputs,
utxos: utxos
});
bitcoreTx.associateInputs(applicableUtxos, pubkeys, threshold, opts);
var privKeys = _.uniq(keys.map(function (key) { return key.privKey.toString(); }));
var signedTx = bitcoreTx.sign(privKeys).toString();
return signedTx;
};
BTCTxProvider.prototype.getRelatedUtxos = function (_a) {
var _this = this;
var outputs = _a.outputs, utxos = _a.utxos;
var txids = outputs.map(function (output) { return output.toObject().prevTxId; });
var applicableUtxos = utxos.filter(function (utxo) { return txids.includes(utxo.txid || utxo.mintTxid); });
return applicableUtxos.map(function (utxo) {
var btcUtxo = Object.assign({}, utxo, {
amount: utxo.value / Math.pow(10, 8),
txid: utxo.mintTxid,
outputIndex: utxo.mintIndex
});
return new _this.lib.Transaction.UnspentOutput(btcUtxo);
});
};
BTCTxProvider.prototype.getOutputsFromTx = function (_a) {
var tx = _a.tx;
return tx.outputs.map(function (_a) {
var script = _a.script, satoshis = _a.satoshis;
var address = script;
return { address: address, satoshis: satoshis };
});
};
BTCTxProvider.prototype.getSigningAddresses = function (_a) {
var tx = _a.tx, utxos = _a.utxos;
var bitcoreTx = new this.lib.Transaction(tx);
var applicableUtxos = this.getRelatedUtxos({
outputs: bitcoreTx.inputs,
utxos: utxos
});
return applicableUtxos.map(function (utxo) { return utxo.address; });
};
return BTCTxProvider;
}());
exports.BTCTxProvider = BTCTxProvider;
//# sourceMappingURL=index.js.map