diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 2e03b69d..d7adaa78 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -270,67 +270,6 @@ Wallet.prototype.ownInput = function ownInput(tx, index) { return inputs; }; -Wallet.prototype.sign = function sign(tx, type, inputs, off) { - if (!type) - type = 'all'; - - if (!off) - off = 0; - - var pub = this.getPublicKey(); - inputs = inputs || tx.inputs; - - // Add signature script to each input - inputs = inputs.filter(function(input, i) { - // Filter inputs that this wallet own - if (!input.out.tx || !this.ownOutput(input.out.tx)) - return false; - - // Get the previous output's subscript - var s = input.out.tx.getSubscript(input.out.index); - - // Get the hash of the current tx, minus the other inputs, plus the sighash. - // `off` is used here in a case where we have multiple wallet objects - // signing the same tx. - var hash = tx.subscriptHash(off + i, s, type); - - // Sign the transaction with our one input - var signature = bcoin.ecdsa.sign(hash, this.key).toDER(); - - // Add the sighash as a single byte to the signature - signature = signature.concat(bcoin.protocol.constants.hashType[type]); - - // P2PKH and simple tx - if (bcoin.script.isPubkeyhash(s) || bcoin.script.isSimplePubkeyhash(s)) { - input.script = [ signature, pub ]; - return true; - } - - // Multisig - // empty array == OP_FALSE == OP_0 - // raw format: OP_FALSE [sig-1] [sig-2] ... - // p2sh format: OP_FALSE [sig-1] [sig-2] ... [redeem-script] - if (bcoin.script.isMultisig(s) || bcoin.script.isScripthash(s)) { - if (!input.script || !input.script.length) { - input.script = [ [], signature ]; - } else if (!~input.script.indexOf(signature)) { - input.script.push(signature); - } - } - - // P2SH requires a redeem script after signatures - if (bcoin.script.isScripthash(s)) { - if (input.script.length - 1 === this.m) { - input.script.push(this.getRedemption()); - } - } - - return true; - }, this); - - return inputs.length; -}; - Wallet.prototype.sign = function sign(tx, type, inputs) { if (!type) type = 'all'; @@ -375,81 +314,6 @@ Wallet.prototype.balance = function balance() { return this.tx.balance(); }; -Wallet.prototype.fill = function fill(tx, cb) { - cb = utils.asyncify(cb); - - // NOTE: tx should be prefilled with all outputs - var cost = tx.funds('out'); - - // Use initial fee for starters - var fee = 1; - - // total = cost + fee - var total = cost.add(new bn(this.fee)); - - var lastAdded = 0; - function addInput(unspent, i) { - // Add new inputs until TX will have enough funds to cover both - // minimum post cost and fee - tx.input(unspent); - lastAdded++; - return tx.funds('in').cmp(total) < 0; - } - - // Transfer `total` funds maximum - var unspent = this.unspent(); - unspent.every(addInput, this); - - // Add dummy output (for `left`) to calculate maximum TX size - tx.out(this, new bn(0)); - - // Change fee value if it is more than 1024 bytes - // (10000 satoshi for every 1024 bytes) - do { - // Calculate maximum possible size after signing - var byteSize = tx.maxSize(); - - var addFee = Math.ceil(byteSize / 1024) - fee; - total.iadd(new bn(addFee * this.fee)); - fee += addFee; - - // Failed to get enough funds, add more inputs - if (tx.funds('in').cmp(total) < 0) - unspent.slice(lastAdded).every(addInput, this); - } while (tx.funds('in').cmp(total) < 0 && lastAdded < unspent.length); - - // Still failing to get enough funds, notify caller - if (tx.funds('in').cmp(total) < 0) { - var err = new Error('Not enough funds'); - err.minBalance = total; - return cb(err); - } - - // How much money is left after sending outputs - var left = tx.funds('in').sub(total); - - // Not enough money, transfer everything to owner - if (left.cmpn(this.dust) < 0) { - // NOTE: that this output is either `postCost` or one of the `dust` values - tx.outputs[tx.outputs.length - 2].value.iadd(left); - left = new bn(0); - } - - // Change or remove last output if there is some money left - if (left.cmpn(0) === 0) - tx.outputs.pop(); - else - tx.outputs[tx.outputs.length - 1].value = left; - - // Sign transaction - // XXX Do not necessarily call this here - this.sign(tx); - - cb(null, tx); - - return tx; -}; - Wallet.prototype.fill = function fill(tx, cb) { cb = utils.asyncify(cb); tx.fillUnspent(this.unspent(), this.getAddress());