multisig options.

This commit is contained in:
Christopher Jeffrey 2015-12-08 15:00:34 -08:00
parent 5f8ad78e4d
commit e4cfa878f1
2 changed files with 58 additions and 40 deletions

View File

@ -56,29 +56,12 @@ function Wallet(options, passphrase) {
this.key = bcoin.ecdsa.genKeyPair();
}
this.addressType = options.addressType || 'normal';
this.addressType = 'normal';
this.sharedKeys = [];
this.m = 1;
this.n = 1;
// Multisig
this.sharedKeys = (options.sharedKeys || []).map(utils.toKeyArray);
this.m = options.m || 1;
this.n = options.n || 1;
// Use p2sh multisig by default
if (!options.addressType && this.sharedKeys.length) {
this.addressType = 'p2sh';
}
if (this.m < 1 || this.m > this.n) {
throw new Error('m ranges between 1 and n');
}
if (this.n < 1 || this.n > 7) {
throw new Error('n ranges between 1 and 7');
}
if (this.sharedKeys.length < this.m - 1) {
throw new Error(this.m + ' public keys required');
}
this.multisig(options.multisig || {});
this.prefix = 'bt/' + this.getAddress() + '/';
this.tx = new bcoin.txPool(this);
@ -124,6 +107,41 @@ Wallet.prototype._init = function init() {
});
};
Wallet.prototype.multisig = function(options) {
var pub = this.key.getPublic(this.compressed, 'array');
options.type = options.type || options.addressType;
options.keys = options.keys || options.sharedKeys;
this.addressType = options.type || 'normal';
// Multisig
this.sharedKeys = (options.keys || []).map(utils.toKeyArray);
this.m = options.m || 1;
this.n = options.n || 1;
this.sharedKeys = this.sharedKeys.filter(function(key) {
return !utils.isEqual(key, pub);
});
// Use p2sh multisig by default
if (!options.addressType && this.sharedKeys.length) {
this.addressType = 'p2sh';
}
if (this.m < 1 || this.m > this.n) {
throw new Error('m ranges between 1 and n');
}
if (this.n < 1 || this.n > 7) {
throw new Error('n ranges between 1 and 7');
}
if (this.sharedKeys.length < this.m - 1) {
throw new Error(this.m + ' public keys required');
}
};
Wallet.prototype.getPrivateKey = function getPrivateKey(enc) {
var priv = this.key.getPrivate();
if (priv)

View File

@ -267,30 +267,30 @@ describe('Wallet', function() {
// Create 3 2-of-3 wallets with our pubkeys as "shared keys"
var w1 = bcoin.wallet({
key: key1,
addressType: 'p2sh',
sharedKeys: [pub2, pub3],
m: 2,
n: 3
multisig: {
type: 'p2sh',
keys: [pub2, pub3],
m: 2,
n: 3
}
});
var w2 = bcoin.wallet({
key: key2,
addressType: 'p2sh',
sharedKeys: [pub1, pub3],
m: 2,
n: 3
multisig: {
type: 'p2sh',
keys: [pub1, pub3],
m: 2,
n: 3
}
});
var w3 = bcoin.wallet({
key: key3,
addressType: 'p2sh',
sharedKeys: [pub1, pub2],
m: 2,
n: 3
// multisig: {
// type: 'p2sh',
// keys: [pub1, pub2],
// m: 2,
// n: 3
// }
multisig: {
type: 'p2sh',
keys: [pub1, pub2],
m: 2,
n: 3
}
});
var receive = bcoin.wallet();