Last active
January 1, 2023 09:52
-
-
Save rithvikvibhu/ffabb70e41308eb6d017d8a2741e9036 to your computer and use it in GitHub Desktop.
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
const path = require('node:path'); | |
const bdb = require('bdb'); | |
const bio = require('bufio'); | |
const MasterKey = require('hsd/lib/wallet/masterkey'); | |
const layouts = require('hsd/lib/wallet/layout'); | |
const layout = layouts.wdb; | |
// SET THESE VALUES: | |
const FILTER_SECRETS = true; | |
const HSD_DATA_LOC = '/path/to/any/hsd_data'; | |
const db = bdb.create({ | |
createIfMissing: false, // do not create new db if wrong path | |
location: path.join(HSD_DATA_LOC, 'wallet'), | |
}); | |
console.log('Loading wallet with bdb from:', db.location); | |
(async() => { | |
await db.open(); | |
const walletIds = {}; | |
{ | |
const iter = db.iterator({ | |
gte: layout.W.min(), | |
lte: layout.W.max(), | |
values: true, | |
}); | |
await iter.each((key, value) => { | |
const wid = layout.W.decode(key)[0]; // int | |
walletIds[wid] = value.toString(); // str | |
}); | |
console.log('walletIds:', walletIds); | |
} | |
const wallets = {}; | |
{ | |
const iter = db.iterator({ | |
gte: layout.w.min(), | |
lte: layout.w.max(), | |
values: true, | |
}); | |
await iter.each((key, value) => { | |
const wid = layout.w.decode(key)[0]; // int | |
const br = bio.read(value); | |
const flags = br.readU8(); | |
const watchOnly = (flags & 1) !== 0; | |
const accountDepth = br.readU32(); | |
const token = br.readBytes(32); | |
const tokenDepth = br.readU32(); | |
const master = MasterKey.read(br).getJSON(null, true); | |
wallets[wid] = { watchOnly, accountDepth, tokenDepth, | |
token: sanitize(token.toString('hex')), | |
master: { | |
encrypted: master.encrypted, | |
until: master.until, | |
iv: sanitize(master.iv), | |
ciphertext: sanitize(master.ciphertext), | |
algorithm: master.algorithm, | |
n: master.n, | |
r: master.r, | |
p: master.p, | |
}, | |
}; | |
}); | |
console.log('wallets:', wallets); | |
} | |
})(); | |
function sanitize(string, len=2) { | |
if (!FILTER_SECRETS) return string; | |
if (!string) return string; | |
// string = string.toString(); | |
if (string.length && string.length > (len*2)) { | |
return string.slice(0, 2) + '*'.repeat(string.length-len*2) + string.slice(string.length-len); | |
} | |
return string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment