Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet: support import of x/y/zpubkey (BIP49 and BIP84) #616

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
add test for BIP49 and BIP84
  • Loading branch information
pinheadmz committed Nov 20, 2019
commit df0a0fd7564aa90864617501f0ac3b2fdf4a2940
1 change: 1 addition & 0 deletions lib/wallet/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ class Account {
this.nestedDepth = br.readU32();
this.lookahead = br.readU8();
this.accountKey = readKey(br);
this.accountKey.purpose = this.purpose;
assert(this.type < Account.typesByVal.length);

const count = br.readU8();
Expand Down
8 changes: 8 additions & 0 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ class Wallet extends EventEmitter {
if (!options)
return this;

if (options.purpose) {
assert(common.purposes[options.purpose] !== undefined, 'Bad purpose');
this.purpose = options.purpose;
} else {
this.purpose = 'x';
}

let key = options.master;
let id, token, mnemonic;

Expand All @@ -99,6 +106,7 @@ class Wallet extends EventEmitter {
} else {
mnemonic = new Mnemonic(options.mnemonic);
key = HD.fromMnemonic(mnemonic, options.password);
key.purpose = this.purpose;
}

this.master.fromKey(key, mnemonic);
Expand Down
52 changes: 52 additions & 0 deletions test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,58 @@ describe('Wallet', function() {
assert.strictEqual(recAddr, 'bc1qcr8te4kr609gcawutmrza0j4xv80jy8z306fyu');
});

// https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki#test-vectors
it('should create BIP49 wallet and addresses', async () => {
// test vectors provided for testnet only
const twdb = new WalletDB({ workers, network: 'testnet' });
await twdb.open();
const wallet = await twdb.create({
id: 'bip49test',
mnemonic: 'abandon abandon abandon abandon abandon abandon' +
' abandon abandon abandon abandon abandon about',
purpose: 'y'
});

const account = await wallet.getAccount(0);
const recAddr = account.toJSON().receiveAddress;
assert.strictEqual(recAddr, '2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2');
await twdb.close();
});

// https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki#test-vectors
it('should import BIP84 zprvkey', async () => {
const wallet = await wdb.create({
id: 'zprvtest',
master: 'zprvAWgYBBk7JR8Gjrh4UJQ2uJdG1r3WNRRfURiABBE3RvMXYSrRJL' +
'62XuezvGdPvG6GFBZduosCc1YP5wixPox7zhZLfiUm8aunE96BBa4Kei5'
});

const account = await wallet.getAccount(0);
const recAddr1 = account.toJSON().receiveAddress;
const changeAddr = account.toJSON().changeAddress;
const accountKey = account.toJSON().accountKey;
let recAddr2 = await wallet.createReceive(0);
recAddr2 = recAddr2.toJSON().address;

assert.strictEqual(
recAddr1,
'bc1qcr8te4kr609gcawutmrza0j4xv80jy8z306fyu'
);
assert.strictEqual(
changeAddr,
'bc1q8c6fshw2dlwun7ekn9qwf37cu2rn755upcp6el'
);
assert.strictEqual(
accountKey,
'zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1' +
'r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs'
);
assert.strictEqual(
recAddr2,
'bc1qnjg0jd8228aq7egyzacy8cys3knf9xvrerkf9g'
);
});

it('should import address', async () => {
const key = KeyRing.generate();

Expand Down