-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-wallet.js
80 lines (61 loc) · 2.24 KB
/
create-wallet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"use strict"
const fs = require("fs")
const BB = require("bitbox-sdk/lib/bitbox-sdk").default
const { Command, flags } = require("@oclif/command")
class CreateWallet extends Command {
async run() {
const { flags } = this.parse(CreateWallet)
const name = flags.name
if (!name) {
this.log(`Please supply a name for the wallet with the -n argument.`)
this.exit(1)
}
const testnet = flags.testnet
await createWallet(testnet, name)
}
}
CreateWallet.description = `Generate a new HD Wallet.`
CreateWallet.flags = {
testnet: flags.boolean({ char: "t", description: "Create a testnet wallet" }),
name: flags.string({ char: "n", description: "Name of wallet" })
}
async function createWallet(testnet, name) {
if (testnet) var BITBOX = new BB({ restURL: "https://trest.bitcoin.com/v1/" })
else var BITBOX = new BB({ restURL: "https://rest.bitcoin.com/v1/" })
// Initialize the wallet data object that will be saved to a file.
const walletData = {}
if (testnet) walletData.network = "testnet"
else walletData.network = "mainnet"
// create 256 bit BIP39 mnemonic
const mnemonic = BITBOX.Mnemonic.generate(
256,
BITBOX.Mnemonic.wordLists().english
)
walletData.mnemonic = mnemonic
// root seed buffer
const rootSeed = BITBOX.Mnemonic.toSeed(mnemonic)
// master HDNode
if (testnet) var masterHDNode = BITBOX.HDNode.fromSeed(rootSeed, "testnet")
else var masterHDNode = BITBOX.HDNode.fromSeed(rootSeed)
// HDNode of BIP44 account
const account = BITBOX.HDNode.derivePath(masterHDNode, "m/44'/145'/0'")
// derive the first external change address HDNode which is going to spend utxo
const change = BITBOX.HDNode.derivePath(account, "0/0")
// get the cash address
walletData.address = BITBOX.HDNode.toCashAddress(change)
// Initialize other data.
walletData.balance = 0
walletData.addressUsed = []
walletData.hasBalance = []
// Write out the basic information into a json file for other apps to use.
fs.writeFile(
`./wallets/${name}.json`,
JSON.stringify(walletData, null, 2),
function(err) {
if (err) return console.error(err)
console.log(`${name}.json written successfully.`)
}
)
//console.log(`mnemonic: ${mnemonic}`)
}
module.exports = CreateWallet