Browse Source

Unroll vanity address test cases

master
Luke Childs 5 years ago
parent
commit
7e9e798950
  1. 80
      test/vanity-addresses.js

80
test/vanity-addresses.js

@ -3,93 +3,99 @@ import * as bitcoin from 'bitcoinjs-lib';
import * as bip39 from 'bip39'; import * as bip39 from 'bip39';
import Vain from '..'; import Vain from '..';
const deriveAddress = { const addressFormats = {
p2pkh: pubkey => { p2pkh: require('../src/address-formats/p2pkh'),
return bitcoin.payments.p2pkh({pubkey}).address; 'p2wpkh-p2sh': require('../src/address-formats/p2wpkh-p2sh'),
}, p2wpkh: require('../src/address-formats/p2wpkh'),
'p2wpkh-p2sh': pubkey => { p2sh: require('../src/address-formats/p2sh')
return bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({pubkey})
}).address;
},
p2wpkh: pubkey => {
return bitcoin.payments.p2wpkh({pubkey}).address;
}
}; };
const xpub = 'xpub6EDZZg3os4RaLxfPpnGBb7ajm6ccyjRs3PGZ5jNK31rPnbpyKb7dc87cEPaLEjFYDBGCQT8VMm8q8MVj2tj7HPBu8syxu82cdHLCNaQmT42';
const testCases = [ const testCases = [
{ {
options: { keyFormat: 'wif',
addressFormat: 'p2pkh', addressFormat: 'p2pkh',
prefix: 'A' prefix: 'A'
}, },
expectedPrefix: '1A'
},
{ {
options: { keyFormat: 'wif',
addressFormat: 'p2wpkh-p2sh', addressFormat: 'p2wpkh-p2sh',
prefix: 'A' prefix: 'A'
}, },
expectedPrefix: '3A' {
keyFormat: 'wif',
addressFormat: 'p2wpkh',
prefix: 'a'
}, },
{ {
options: { keyFormat: 'bip39',
addressFormat: 'p2pkh',
prefix: 'A'
},
{
keyFormat: 'bip39',
addressFormat: 'p2wpkh-p2sh',
prefix: 'A'
},
{
keyFormat: 'bip39',
addressFormat: 'p2wpkh', addressFormat: 'p2wpkh',
prefix: 'a' prefix: 'a'
}, },
expectedPrefix: 'bc1qa'
}
];
const additionalOptionCombos = [
{ {
keyFormat: 'wif' keyFormat: 'xpub',
xpub,
addressFormat: 'p2pkh',
prefix: 'A'
}, },
{ {
keyFormat: 'bip39' keyFormat: 'xpub',
xpub,
addressFormat: 'p2wpkh-p2sh',
prefix: 'A'
}, },
{ {
keyFormat: 'xpub', keyFormat: 'xpub',
xpub: 'xpub6EDZZg3os4RaLxfPpnGBb7ajm6ccyjRs3PGZ5jNK31rPnbpyKb7dc87cEPaLEjFYDBGCQT8VMm8q8MVj2tj7HPBu8syxu82cdHLCNaQmT42' xpub,
addressFormat: 'p2wpkh',
prefix: 'a'
} }
]; ];
additionalOptionCombos.forEach(additionalOptions => { testCases.forEach(options => {
testCases.forEach(({options, expectedPrefix}) => {
options = {...options, ...additionalOptions};
test(`Vain generates a valid ${options.keyFormat} ${options.addressFormat} vanity address`, async t => { test(`Vain generates a valid ${options.keyFormat} ${options.addressFormat} vanity address`, async t => {
const addressFormat = addressFormats[options.addressFormat];
const vain = new Vain(options); const vain = new Vain(options);
const {address, ...keyData} = vain.generate(); const {address, ...keyData} = vain.generate();
let publicKey; let key;
switch (options.keyFormat) { switch (options.keyFormat) {
case 'wif': { case 'wif': {
({publicKey} = bitcoin.ECPair.fromWIF(keyData.wif)); key = bitcoin.ECPair.fromWIF(keyData.wif);
break; break;
} }
case 'bip39': { case 'bip39': {
const seed = await bip39.mnemonicToSeed(keyData.mnemonic); const seed = await bip39.mnemonicToSeed(keyData.mnemonic);
const node = bitcoin.bip32.fromSeed(seed); const node = bitcoin.bip32.fromSeed(seed);
({publicKey} = node.derivePath(keyData.derivationPath)); key = node.derivePath(keyData.derivationPath);
break; break;
} }
case 'xpub': { case 'xpub': {
const node = bitcoin.bip32.fromBase58(keyData.xpub); const node = bitcoin.bip32.fromBase58(keyData.xpub);
({publicKey} = node.derivePath(keyData.derivationPath)); key = node.derivePath(keyData.derivationPath);
break; break;
} }
default: default:
} }
const keyAddress = deriveAddress[options.addressFormat](publicKey); const keyAddress = addressFormat.derive(key);
t.true(address.startsWith(expectedPrefix)); t.true(address.startsWith(addressFormat.prefix + options.prefix));
t.is(address, keyAddress); t.is(address, keyAddress);
}); });
});
}); });

Loading…
Cancel
Save