hd-auth-wallet/lib/commands/random-xpub.js
2019-11-26 22:43:24 +02:00

54 lines
2.0 KiB
JavaScript

const persistence = require("../core/persistence/persistence");
const cryptoUtil = require("../core/cryptoUtil");
const bip32 = require('bip32');
module.exports.description = "Get random xpub to use on a service"
module.exports.builder = (yargs) => {
return yargs.argv;
}
module.exports.handler = (argv) => {
console.log("Get random xpub to use on a service");
persistence().LoadDB()
.then(db => {
db.Key.findOne({
where: {
name: "FirstKey"
}
})
.then(key => {
// TODO: Load password from config...
if(key) {
const password = "vanished";
const iv = key.iv;
const decryptedPrivateKey = cryptoUtil.decrypt(key.encryptedPrivateKey, password, iv);
const masterNode = bip32.fromBase58(decryptedPrivateKey);
const derivationPath = `m/${cryptoUtil.randomDerivationPath(true)}`
const xpubNode = masterNode.derivePath(derivationPath);
// TODO save xpub and derivation path in db...
return db.ExtendedPublicKey.create({
xpub: xpubNode.neutered().toBase58(),
derivationPath: derivationPath,
keyId: key.id // TODO rename this to walletID
// TODO add name if availabe...
}).catch(error => {
console.error("Failed to create extended public key: ", error);
})
} else {
console.error("Couldn't create xpub without key")
}
})
.then(extendedPublicKey => {
console.log("Extened public key: ", extendedPublicKey.xpub);
})
.catch(error => {
console.error("Failed to find key: ", error);
})
})
}