wallet: migrate paths.

This commit is contained in:
Christopher Jeffrey 2017-11-27 12:36:12 -08:00
parent e2ef35b24b
commit efca42b138
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 91 additions and 26 deletions

View File

@ -860,9 +860,9 @@ class Address {
*/
Address.types = {
PUBKEYHASH: 2,
SCRIPTHASH: 3,
WITNESS: 4
PUBKEYHASH: 0,
SCRIPTHASH: 1,
WITNESS: 2
};
/**
@ -871,8 +871,6 @@ Address.types = {
*/
Address.typesByVal = [
null,
null,
'PUBKEYHASH',
'SCRIPTHASH',
'WITNESS'

View File

@ -32,15 +32,16 @@ class Path {
this.name = null; // Passed in by caller.
this.account = 0;
this.type = Address.types.PUBKEYHASH;
this.version = -1;
this.branch = -1;
this.index = -1;
this.encrypted = false;
this.data = null;
// Currently unused.
this.type = Address.types.PUBKEYHASH;
this.version = -1;
this.hash = null; // Passed in by caller.
if (options)
@ -119,6 +120,9 @@ class Path {
this.account = br.readU32();
this.keyType = br.readU8();
this.type = br.readU8();
this.version = br.readI8();
switch (this.keyType) {
case Path.types.HD:
this.branch = br.readU32();
@ -136,12 +140,6 @@ class Path {
break;
}
this.version = br.readI8();
this.type = br.readU8();
if (this.type === 129 || this.type === 130)
this.type = 4;
return this;
}
@ -192,6 +190,9 @@ class Path {
bw.writeU32(this.account);
bw.writeU8(this.keyType);
bw.writeU8(this.type);
bw.writeI8(this.version);
switch (this.keyType) {
case Path.types.HD:
assert(!this.data);
@ -214,9 +215,6 @@ class Path {
break;
}
bw.writeI8(this.version);
bw.writeU8(this.type);
return bw.render();
}

View File

@ -18,6 +18,9 @@ const {encoding} = bio;
// coin `own` flag - no longer a soft fork
// tx map - for unconfirmed
// balances - index account balances
// wallet - serialization
// account - serialization
// path - serialization
let file = process.argv[2];
let batch;
@ -144,7 +147,7 @@ async function updateCoins(wid) {
});
await iter.each((key, value) => {
const br = bio.read(value);
const br = bio.read(value, true);
Coin.fromReader(br);
br.readU8();
@ -173,7 +176,7 @@ async function updateTX(wid) {
if (!raw) {
map = new Set();
} else {
const br = bio.read(raw);
const br = bio.read(raw, true);
map = parseMap(br);
}
@ -205,7 +208,7 @@ async function updateWalletBalance(wid) {
});
await iter.each((key, value) => {
const br = bio.read(value);
const br = bio.read(value, true);
const coin = Coin.fromReader(br);
const spent = br.readU8() === 1;
@ -225,7 +228,7 @@ async function updateAccountBalances(wid) {
const raw = await db.get(layout.w(wid));
assert(raw);
const br = bio.read(raw);
const br = bio.read(raw, true);
br.readU32();
br.readU32();
@ -262,7 +265,7 @@ async function updateAccountBalance(wid, acct) {
const [, hash, index] = tlayout.Cc(key);
const raw = await db.get(c(pre, tlayout.c(hash, index)));
assert(raw);
const br = bio.read(raw);
const br = bio.read(raw, true);
const coin = Coin.fromReader(br);
const spent = br.readU8() === 1;
@ -285,7 +288,7 @@ async function updateWallet(wid) {
const br = bio.read(raw, true);
br.readU32(); // Skip network.
const wid = br.readU32();
br.readU32(); // Skip wid.
const id = br.readVarString('ascii');
const initialized = br.readU8() === 1;
const watchOnly = br.readU8() === 1;
@ -447,14 +450,79 @@ async function updateAccount(wid, acct) {
async function updatePaths() {
const iter = db.iterator({
gte: layout.p(encoding.NULL_HASH),
lte: layout.p(encoding.HIGH_HASH),
gte: layout.P(0, encoding.NULL_HASH),
lte: layout.P(0xffffffff, encoding.HIGH_HASH),
keys: true,
values: true
});
await iter.each((key, value) => {
const br = bio.read(value);
const br = bio.read(value, true);
const account = br.readU32();
const keyType = br.readU8();
let branch = -1;
let index = -1;
let encrypted = false;
let data = null;
switch (keyType) {
case 0:
branch = br.readU32();
index = br.readU32();
break;
case 1:
encrypted = br.readU8() === 1;
data = br.readVarBytes();
break;
case 2:
break;
default:
assert(false);
break;
}
const version = br.readI8();
let type = br.readU8();
if (type === 129 || type === 130)
type = 4;
type -= 2;
const bw = bio.write();
bw.writeU32(account);
bw.writeU8(keyType);
bw.writeU8(type);
bw.writeI8(version);
switch (keyType) {
case 0:
assert(!data);
assert(index !== -1);
bw.writeU32(branch);
bw.writeU32(index);
break;
case 1:
assert(data);
assert(index === -1);
bw.writeU8(encrypted ? 1 : 0);
bw.writeVarBytes(data);
break;
case 2:
assert(!data);
assert(index === -1);
break;
default:
assert(false);
break;
}
batch.put(key, bw.render());
});
}
@ -663,6 +731,7 @@ function serializeBalance(bal) {
await updateState();
await updateBlockMap();
await updateTXDB();
await updatePaths();
await batch.write();
await db.close();