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"; console.log("Encrypted: ", key.encryptedPrivateKey); const decryptedPrivateKey = cryptoUtil.decrypt(key.encryptedPrivateKey, password); console.log("Decrypted: ", decryptedPrivateKey); const masterNode = bip32.fromBase58(decryptedPrivateKey); console.log("Node: ", masterNode.toBase58()); const derivationPath = `m/${cryptoUtil.randomDerivationPath(true)}` const xpubNode = masterNode.derivePath(derivationPath); console.log("Xpub: ", xpubNode.neutered().toBase58()); // 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); }) }) }