bitcore-wallet-service
Version:
A service for Mutisig HD Bitcoin Wallets
151 lines • 5.15 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TxProposalLegacy = void 0;
const lodash_1 = __importDefault(require("lodash"));
const common_1 = require("../common");
const logger_1 = __importDefault(require("../logger"));
const $ = require('preconditions').singleton();
const Constants = common_1.Common.Constants;
const Defaults = common_1.Common.Defaults;
const txproposalaction_1 = require("./txproposalaction");
function throwUnsupportedError() {
const msg = 'Unsupported operation on this transaction proposal';
logger_1.default.warn('DEPRECATED: ' + msg);
throw new Error(msg);
}
class TxProposalLegacy {
static fromObj(obj) {
const x = new TxProposalLegacy();
x.version = obj.version;
if (obj.version === '1.0.0') {
x.type = TxProposalLegacy.Types.SIMPLE;
}
else {
x.type = obj.type;
}
x.createdOn = obj.createdOn;
x.id = obj.id;
x.walletId = obj.walletId;
x.creatorId = obj.creatorId;
x.outputs = obj.outputs;
x.toAddress = obj.toAddress;
x.amount = obj.amount;
x.message = obj.message;
x.payProUrl = obj.payProUrl;
x.proposalSignature = obj.proposalSignature;
x.changeAddress = obj.changeAddress;
x.inputs = obj.inputs;
x.requiredSignatures = obj.requiredSignatures;
x.requiredRejections = obj.requiredRejections;
x.walletN = obj.walletN;
x.status = obj.status;
x.txid = obj.txid;
x.broadcastedOn = obj.broadcastedOn;
x.inputPaths = obj.inputPaths;
x.actions = lodash_1.default.map(obj.actions, function (action) {
return txproposalaction_1.TxProposalAction.fromObj(action);
});
x.outputOrder = obj.outputOrder;
x.coin = obj.coin || Defaults.COIN;
x.network = obj.network;
x.fee = obj.fee;
x.feePerKb = obj.feePerKb;
x.excludeUnconfirmedUtxos = obj.excludeUnconfirmedUtxos;
x.proposalSignaturePubKey = obj.proposalSignaturePubKey;
x.proposalSignaturePubKeySig = obj.proposalSignaturePubKeySig;
x.addressType = obj.addressType || Constants.SCRIPT_TYPES.P2SH;
x.derivationStrategy = obj.derivationStrategy || Constants.DERIVATION_STRATEGIES.BIP45;
x.customData = obj.customData;
return x;
}
toObject() {
const x = lodash_1.default.cloneDeep(this);
x.isPending = this.isPending();
return x;
}
_updateStatus() {
if (this.status != 'pending')
return;
if (this.isRejected()) {
this.status = 'rejected';
}
else if (this.isAccepted()) {
this.status = 'accepted';
}
}
getBitcoreTx() {
throwUnsupportedError();
}
getRawTx() {
throwUnsupportedError();
}
getTotalAmount() {
if (this.type == TxProposalLegacy.Types.MULTIPLEOUTPUTS || this.type == TxProposalLegacy.Types.EXTERNAL) {
return lodash_1.default.map(this.outputs, 'amount').reduce(function (total, n) {
return total + n;
}, 0);
}
else {
return this.amount;
}
}
getActors() {
return lodash_1.default.map(this.actions, 'copayerId');
}
getApprovers() {
return lodash_1.default.map(lodash_1.default.filter(this.actions, a => {
return a.type == 'accept';
}), 'copayerId');
}
getActionBy(copayerId) {
return lodash_1.default.find(this.actions, {
copayerId
});
}
addAction(copayerId, type, comment, signatures, xpub) {
const action = txproposalaction_1.TxProposalAction.create({
copayerId,
type,
signatures,
xpub,
comment
});
this.actions.push(action);
this._updateStatus();
}
sign() {
throwUnsupportedError();
}
reject(copayerId, reason) {
this.addAction(copayerId, 'reject', reason);
}
isPending() {
return !lodash_1.default.includes(['broadcasted', 'rejected'], this.status);
}
isAccepted() {
const votes = lodash_1.default.countBy(this.actions, 'type');
return votes['accept'] >= this.requiredSignatures;
}
isRejected() {
const votes = lodash_1.default.countBy(this.actions, 'type');
return votes['reject'] >= this.requiredRejections;
}
isBroadcasted() {
return this.status == 'broadcasted';
}
setBroadcasted() {
$.checkState(this.txid, 'Failed state: this.txid at setBroadcasted()');
this.status = 'broadcasted';
this.broadcastedOn = Math.floor(Date.now() / 1000);
}
}
exports.TxProposalLegacy = TxProposalLegacy;
TxProposalLegacy.Types = {
SIMPLE: 'simple',
MULTIPLEOUTPUTS: 'multiple_outputs',
EXTERNAL: 'external'
};
//# sourceMappingURL=txproposal_legacy.js.map