wallet-account: fix change derivation path
This commit is contained in:
parent
64ba4c7567
commit
ef43dad9ee
@ -365,7 +365,7 @@ class Account {
|
|||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
key = this.deriveChange(this.changeDepth);
|
key = this.deriveChange(this.changeDepth);
|
||||||
lookahead = this.deriveReceive(this.changeDepth + this.lookahead);
|
lookahead = this.deriveChange(this.changeDepth + this.lookahead);
|
||||||
await this.saveKey(b, lookahead);
|
await this.saveKey(b, lookahead);
|
||||||
this.changeDepth += 1;
|
this.changeDepth += 1;
|
||||||
this.change = key;
|
this.change = key;
|
||||||
|
|||||||
@ -228,11 +228,18 @@ async function testP2SH(witness, nesting) {
|
|||||||
describe('Wallet', function() {
|
describe('Wallet', function() {
|
||||||
this.timeout(5000);
|
this.timeout(5000);
|
||||||
|
|
||||||
it('should open walletdb', async () => {
|
before(async () => {
|
||||||
consensus.COINBASE_MATURITY = 0;
|
|
||||||
await wdb.open();
|
await wdb.open();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
after(async () => {
|
||||||
|
await wdb.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should open walletdb', () => {
|
||||||
|
consensus.COINBASE_MATURITY = 0;
|
||||||
|
});
|
||||||
|
|
||||||
it('should generate new key and address', async () => {
|
it('should generate new key and address', async () => {
|
||||||
const wallet = await wdb.create();
|
const wallet = await wdb.create();
|
||||||
|
|
||||||
@ -1538,6 +1545,79 @@ describe('Wallet', function() {
|
|||||||
assert(!await wdb.get('alice100'));
|
assert(!await wdb.get('alice100'));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const keyTypes = [
|
||||||
|
{
|
||||||
|
name: 'receive',
|
||||||
|
method: 'createReceive',
|
||||||
|
branch: 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'change',
|
||||||
|
method: 'createChange',
|
||||||
|
branch: 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'nested',
|
||||||
|
method: 'createNested',
|
||||||
|
branch: 2
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const type of keyTypes) {
|
||||||
|
it(`should create ${type.name} addresses`, async () => {
|
||||||
|
const account = 0;
|
||||||
|
const wallet = await wdb.create({
|
||||||
|
witness: true
|
||||||
|
});
|
||||||
|
const addresses = new Set();
|
||||||
|
|
||||||
|
for (let i = 0; i < 100; i++) {
|
||||||
|
const key = await wallet[type.method](account);
|
||||||
|
addresses.add(key.getAddress('string'));
|
||||||
|
assert.strictEqual(key.account, account);
|
||||||
|
assert.strictEqual(key.branch, type.branch);
|
||||||
|
assert.strictEqual(key.index, i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.strictEqual(addresses.size, 100);
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should create ${type.name} addresses and get their keys`, async () => {
|
||||||
|
const account = 0;
|
||||||
|
const wallet = await wdb.create({
|
||||||
|
witness: true
|
||||||
|
});
|
||||||
|
|
||||||
|
const addresses = new Set();
|
||||||
|
|
||||||
|
for (let i = 0; i < 100; i++) {
|
||||||
|
const key1 = await wallet[type.method](account);
|
||||||
|
const address = key1.getAddress();
|
||||||
|
|
||||||
|
assert(key1, `Could not get ${type.name}`);
|
||||||
|
addresses.add(address);
|
||||||
|
|
||||||
|
assert.strictEqual(key1.account, account);
|
||||||
|
assert.strictEqual(key1.branch, type.branch);
|
||||||
|
assert.strictEqual(key1.index, i + 1);
|
||||||
|
|
||||||
|
const key2 = await wallet.getKey(address);
|
||||||
|
assert(key2, `Could not get key for ${address.toString()}` +
|
||||||
|
`, Key: xpub/${type.branch}/${i+1}`);
|
||||||
|
|
||||||
|
assert.strictEqual(key2.name, key1.name);
|
||||||
|
assert.strictEqual(key2.account, key1.account);
|
||||||
|
assert.strictEqual(key2.branch, key1.branch);
|
||||||
|
assert.strictEqual(key2.witness, key1.witness);
|
||||||
|
assert.strictEqual(key2.nested, key1.nested);
|
||||||
|
assert.bufferEqual(key2.publicKey, key1.publicKey);
|
||||||
|
assert.strictEqual(key2.getType(), key1.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.strictEqual(addresses.size, 100);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
it('should cleanup', () => {
|
it('should cleanup', () => {
|
||||||
consensus.COINBASE_MATURITY = 100;
|
consensus.COINBASE_MATURITY = 100;
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user