multisig options.
This commit is contained in:
parent
5f8ad78e4d
commit
e4cfa878f1
@ -56,29 +56,12 @@ function Wallet(options, passphrase) {
|
|||||||
this.key = bcoin.ecdsa.genKeyPair();
|
this.key = bcoin.ecdsa.genKeyPair();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addressType = options.addressType || 'normal';
|
this.addressType = 'normal';
|
||||||
|
this.sharedKeys = [];
|
||||||
|
this.m = 1;
|
||||||
|
this.n = 1;
|
||||||
|
|
||||||
// Multisig
|
this.multisig(options.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.prefix = 'bt/' + this.getAddress() + '/';
|
this.prefix = 'bt/' + this.getAddress() + '/';
|
||||||
this.tx = new bcoin.txPool(this);
|
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) {
|
Wallet.prototype.getPrivateKey = function getPrivateKey(enc) {
|
||||||
var priv = this.key.getPrivate();
|
var priv = this.key.getPrivate();
|
||||||
if (priv)
|
if (priv)
|
||||||
|
|||||||
@ -267,30 +267,30 @@ describe('Wallet', function() {
|
|||||||
// Create 3 2-of-3 wallets with our pubkeys as "shared keys"
|
// Create 3 2-of-3 wallets with our pubkeys as "shared keys"
|
||||||
var w1 = bcoin.wallet({
|
var w1 = bcoin.wallet({
|
||||||
key: key1,
|
key: key1,
|
||||||
addressType: 'p2sh',
|
multisig: {
|
||||||
sharedKeys: [pub2, pub3],
|
type: 'p2sh',
|
||||||
m: 2,
|
keys: [pub2, pub3],
|
||||||
n: 3
|
m: 2,
|
||||||
|
n: 3
|
||||||
|
}
|
||||||
});
|
});
|
||||||
var w2 = bcoin.wallet({
|
var w2 = bcoin.wallet({
|
||||||
key: key2,
|
key: key2,
|
||||||
addressType: 'p2sh',
|
multisig: {
|
||||||
sharedKeys: [pub1, pub3],
|
type: 'p2sh',
|
||||||
m: 2,
|
keys: [pub1, pub3],
|
||||||
n: 3
|
m: 2,
|
||||||
|
n: 3
|
||||||
|
}
|
||||||
});
|
});
|
||||||
var w3 = bcoin.wallet({
|
var w3 = bcoin.wallet({
|
||||||
key: key3,
|
key: key3,
|
||||||
addressType: 'p2sh',
|
multisig: {
|
||||||
sharedKeys: [pub1, pub2],
|
type: 'p2sh',
|
||||||
m: 2,
|
keys: [pub1, pub2],
|
||||||
n: 3
|
m: 2,
|
||||||
// multisig: {
|
n: 3
|
||||||
// type: 'p2sh',
|
}
|
||||||
// keys: [pub1, pub2],
|
|
||||||
// m: 2,
|
|
||||||
// n: 3
|
|
||||||
// }
|
|
||||||
});
|
});
|
||||||
var receive = bcoin.wallet();
|
var receive = bcoin.wallet();
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user