Browse Source

Enforce multisig options

master
Luke Childs 6 years ago
parent
commit
d99447f5d8
  1. 12
      src/index.js
  2. 42
      test/errors.js

12
src/index.js

@ -45,6 +45,18 @@ class Vain extends Emitter {
if (this.options.keyFormat === 'xpub' && typeof this.options.xpub !== 'string') {
throw new Error('An xpub string must be passed in');
}
if (this.options.keyFormat === 'multisig' && this.options.addressFormat !== 'p2sh') {
throw new Error('When using key format "multisig" address format must be "p2sh"');
}
if (this.options.keyFormat === 'multisig' && typeof this.options.pubkeys === 'undefined') {
throw new Error('When using key format "multisig" a "pubkeys" array must be passed in');
}
if (this.options.keyFormat === 'multisig' && typeof this.options.m !== 'number') {
throw new Error('When using key format "multisig" an "m" value must be passed in');
}
}
generate() {

42
test/errors.js

@ -16,6 +16,18 @@ const invalidCharsets = [
}
];
const multisigOptions = {
keyFormat: 'multisig',
addressFormat: 'p2sh',
prefix: 'A',
pubkeys: [
Buffer.from('030000000000000000000000000000000000000000000000000000000000000001', 'hex'),
Buffer.from('030000000000000000000000000000000000000000000000000000000000000002', 'hex'),
Buffer.from('030000000000000000000000000000000000000000000000000000000000000003', 'hex')
],
m: 2
};
invalidCharsets.forEach(options => {
test(`Vain throws when \`options.prefix\` doesn't match the charset for ${options.addressFormat} addresses`, t => {
const error = t.throws(() => new Vain(options));
@ -40,3 +52,33 @@ test('Vain throws when `options.xpub` is `undefined` when `options.keyFormat` is
options.xpub = 'xpub6EDZZg3os4RaLxfPpnGBb7ajm6ccyjRs3PGZ5jNK31rPnbpyKb7dc87cEPaLEjFYDBGCQT8VMm8q8MVj2tj7HPBu8syxu82cdHLCNaQmT42';
t.notThrows(() => new Vain(options));
});
test('Vain throws when `options.keyFormat` is `multisig` when `options.addressFormat` is not `p2sh`', t => {
const options = {...multisigOptions};
delete options.addressFormat;
const error = t.throws(() => new Vain(options));
t.is(error.message, 'When using key format "multisig" address format must be "p2sh"');
options.addressFormat = multisigOptions.addressFormat;
t.notThrows(() => new Vain(options));
});
test('Vain throws when `options.keyFormat` is `multisig` when `options.pubkeys` is undefined', t => {
const options = {...multisigOptions};
delete options.pubkeys;
const error = t.throws(() => new Vain(options));
t.is(error.message, 'When using key format "multisig" a "pubkeys" array must be passed in');
options.pubkeys = multisigOptions.pubkeys;
t.notThrows(() => new Vain(options));
});
test('Vain throws when `options.keyFormat` is `multisig` when `options.m` is not a number', t => {
const options = {...multisigOptions};
delete options.m;
const error = t.throws(() => new Vain(options));
t.is(error.message, 'When using key format "multisig" an "m" value must be passed in');
options.m = multisigOptions.m;
t.notThrows(() => new Vain(options));
});

Loading…
Cancel
Save