-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5a769e
commit 4992ee5
Showing
4 changed files
with
125 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import test from 'ava'; | ||
import Vain from '..'; | ||
|
||
const invalidCharsets = [ | ||
{ | ||
addressFormat: 'p2pkh', | ||
prefix: '0' | ||
}, | ||
{ | ||
addressFormat: 'p2wpkh-p2sh', | ||
prefix: '0' | ||
}, | ||
{ | ||
addressFormat: 'p2wpkh', | ||
prefix: '1' | ||
} | ||
]; | ||
|
||
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)); | ||
t.true(error.message.includes('Invalid characters for address format')); | ||
}); | ||
}); | ||
|
||
test('Vain throws when `options.prefix` is `undefined`', t => { | ||
const options = {}; | ||
const error = t.throws(() => new Vain(options)); | ||
t.is(error.message, 'Prefix must be set'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import test from 'ava'; | ||
import * as bitcoin from 'bitcoinjs-lib'; | ||
import Vain from '..'; | ||
|
||
test('Vain defaults to p2pkh if no address format is set', t => { | ||
const options = { | ||
prefix: 'A' | ||
}; | ||
const vain = new Vain(options); | ||
const {address, wif} = vain.generate(); | ||
|
||
const keyPair = bitcoin.ECPair.fromWIF(wif); | ||
const {address: wifAddress} = bitcoin.payments.p2pkh({pubkey: keyPair.publicKey}); | ||
|
||
t.true(address.startsWith(`1${options.prefix}`)); | ||
t.is(address, wifAddress); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import test from 'ava'; | ||
import * as bitcoin from 'bitcoinjs-lib'; | ||
import * as bip39 from 'bip39'; | ||
import Vain from '..'; | ||
|
||
const deriveAddress = { | ||
p2pkh: pubkey => { | ||
return bitcoin.payments.p2pkh({pubkey}).address; | ||
}, | ||
'p2wpkh-p2sh': pubkey => { | ||
return bitcoin.payments.p2sh({ | ||
redeem: bitcoin.payments.p2wpkh({pubkey}) | ||
}).address; | ||
}, | ||
p2wpkh: pubkey => { | ||
return bitcoin.payments.p2wpkh({pubkey}).address; | ||
} | ||
}; | ||
|
||
const keyFormats = ['wif', 'bip39']; | ||
|
||
const testCases = [ | ||
{ | ||
options: { | ||
addressFormat: 'p2pkh', | ||
prefix: 'A' | ||
}, | ||
expectedPrefix: '1A' | ||
}, | ||
{ | ||
options: { | ||
addressFormat: 'p2wpkh-p2sh', | ||
prefix: 'A' | ||
}, | ||
expectedPrefix: '3A' | ||
}, | ||
{ | ||
options: { | ||
addressFormat: 'p2wpkh', | ||
prefix: 'a' | ||
}, | ||
expectedPrefix: 'bc1qa' | ||
} | ||
]; | ||
|
||
keyFormats.forEach(keyFormat => { | ||
testCases.forEach(({options, expectedPrefix}) => { | ||
options = {...options, keyFormat}; | ||
|
||
test(`Vain generates a valid ${options.keyFormat} ${options.addressFormat} vanity address`, async t => { | ||
const vain = new Vain(options); | ||
const {address, ...keyData} = vain.generate(); | ||
|
||
let publicKey; | ||
|
||
switch (options.keyFormat) { | ||
case 'wif': { | ||
({publicKey} = bitcoin.ECPair.fromWIF(keyData.wif)); | ||
break; | ||
} | ||
|
||
case 'bip39': { | ||
const seed = await bip39.mnemonicToSeed(keyData.mnemonic); | ||
const node = bitcoin.bip32.fromSeed(seed); | ||
({publicKey} = node.derivePath(keyData.derivationPath)); | ||
break; | ||
} | ||
|
||
default: | ||
} | ||
|
||
const keyAddress = deriveAddress[options.addressFormat](publicKey); | ||
|
||
t.true(address.startsWith(expectedPrefix)); | ||
t.is(address, keyAddress); | ||
}); | ||
}); | ||
}); |