refactor script and wallet. better redeem script error.

This commit is contained in:
Christopher Jeffrey 2015-12-20 16:06:36 -08:00
parent c0d0423f14
commit 87d4d4b0ad
3 changed files with 10 additions and 22 deletions

View File

@ -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);
}

View File

@ -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) {

View File

@ -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);