From 87d4d4b0adc8dd0f71f24bf49c38d8dc81c1f719 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 20 Dec 2015 16:06:36 -0800 Subject: [PATCH] refactor script and wallet. better redeem script error. --- lib/bcoin/script.js | 6 +++--- lib/bcoin/tx.js | 2 +- lib/bcoin/wallet.js | 24 ++++++------------------ 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index d5c05459..2b0123f7 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -844,9 +844,9 @@ script.exec = function exec(input, output, tx, i, recurse) { return true; }; -script.multisig = function multisig(keys, m, n) { +script.redeem = function redeem(keys, m, n) { if (keys.length !== n) - throw new Error('Wrong amount of pubkeys for multisig script'); + throw new Error(n + ' keys are required to generate redeem script'); assert(m >= 1 && m <= n); assert(n >= 1 && n <= 15); @@ -1079,7 +1079,7 @@ script.isMultisigInput = function isMultisigInput(s, pubs, tx, i) { } if (pubs && pubs.length >= 2) { - o = script.multisig(pubs, 2, pubs.length); + o = script.redeem(pubs, 2, pubs.length); return script.exec(s, o, tx, i); } diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 58e3ce94..f5281cd4 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -486,7 +486,7 @@ TX.prototype.scriptOutput = function scriptOutput(output, options) { else assert(n >= 1 && n <= 3); - script = bcoin.script.multisig(keys, m, n); + script = bcoin.script.redeem(keys, m, n); // make it p2sh if (options.scripthash) { diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 6a464d33..1d5d0a89 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -35,9 +35,7 @@ function Wallet(options, passphrase) { options = {}; this.options = options; - this.compressed = typeof options.compressed !== 'undefined' - ? options.compressed - : true; + this.compressed = options.compressed !== false; this.storage = options.storage; this.label = options.label || ''; this.key = null; @@ -160,9 +158,6 @@ Wallet.prototype.multisig = function multisig(options) { if (this.n < 1 || this.n > this.nmax) throw new Error('n ranges between 1 and ' + this.nmax); - - if (this.keys.length > this.n) - throw new Error('No more than ' + this.n + ' keys are necessary'); }; Wallet.prototype.addKey = function addKey(key) { @@ -177,9 +172,6 @@ Wallet.prototype.addKey = function addKey(key) { this.keys.push(key); - if (this.keys.length > this.n) - throw new Error('No more than ' + this.n + ' keys are necessary'); - this.keys = utils.sortKeys(this.keys); }; @@ -215,11 +207,11 @@ Wallet.prototype.getPrivateKey = function getPrivateKey(enc) { var priv = this.key.getPrivate(); var arr, chk; - if (priv) - priv = priv.toArray(); - else + if (!priv) return; + priv = priv.toArray(); + if (!enc) return priv; @@ -247,12 +239,8 @@ Wallet.prototype.getPrivateKey = function getPrivateKey(enc) { Wallet.prototype.getFullPublicKey = function getFullPublicKey(enc) { var pub = this.getOwnPublicKey(); - if (this.type === 'scripthash') { - if (this.n !== this.keys.length) - throw new Error('Not enough keys to generate address'); - - pub = bcoin.script.encode(bcoin.script.multisig(this.keys, this.m, this.n)); - } + if (this.type === 'scripthash') + pub = bcoin.script.encode(bcoin.script.redeem(this.keys, this.m, this.n)); if (enc === 'base58') return utils.toBase58(pub);