/
create-wallet.js
159 lines (133 loc) · 4.52 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Creates a new HD wallet. Save the 12-word Mnemonic private key to a .json file.
https://developer.bitcoin.com/mastering-bitcoin-cash/3-keys-addresses-wallets/#mnemonic-code-words
*/
'use strict'
// Global npm libraries
const { Command, flags } = require('@oclif/command')
const fs = require('fs')
// Local libraries
const AppUtils = require('../util')
const globalConfig = require('../../config')
const CreateWalletLib = require('../lib/create-wallet')
// Global variables
const appUtils = new AppUtils()
// Mainnet by default
const bchjs = new globalConfig.BCHLIB({
restURL: globalConfig.MAINNET_REST,
apiToken: globalConfig.JWT
})
class CreateWallet extends Command {
constructor (argv, config) {
super(argv, config)
// _this = this
// Encapsulate dependencies
this.bchjs = bchjs
this.fs = fs
this.localConfig = globalConfig
this.createWalletLib = new CreateWalletLib()
}
async run () {
try {
const { flags } = this.parse(CreateWallet)
// Validate input flags
this.validateFlags(flags)
// Determine if this is a testnet wallet or a mainnet wallet.
if (flags.testnet) {
this.bchjs = new this.localConfig.BCHLIB({
restURL: this.localConfig.TESTNET_REST
})
}
const filename = `${__dirname.toString()}/../../wallets/${
flags.name
}.json`
if (!flags.description) flags.description = ''
return this.createWallet(filename, flags.testnet, flags.description)
} catch (err) {
if (err.message) console.log(err.message)
else console.log('Error in create-wallet.js/run(): ', err)
return 0
}
}
// testnet is a boolean.
async createWallet (filename, testnet, desc) {
try {
if (!filename || filename === '') throw new Error('filename required.')
if (this.fs.existsSync(filename)) { throw new Error('filename already exist') }
const createWalletObj = {
testnet,
desc
}
const walletData = await this.createWalletLib.createWallet(createWalletObj)
// console.log(filename)
// Initialize the wallet data object that will be saved to a file.
// const walletData = {}
// if (testnet) walletData.network = 'testnet'
// else walletData.network = 'mainnet'
//
// // create 128 bit (12 word) BIP39 mnemonic
// const mnemonic = this.bchjs.Mnemonic.generate(
// 128,
// this.bchjs.Mnemonic.wordLists().english
// )
// walletData.mnemonic = mnemonic
//
// // root seed buffer
// const rootSeed = await this.bchjs.Mnemonic.toSeed(mnemonic)
//
// // master HDNode
// let masterHDNode = this.bchjs.HDNode.fromSeed(rootSeed)
// if (testnet) {
// masterHDNode = this.bchjs.HDNode.fromSeed(rootSeed, 'testnet')
// }
//
// // Use the 245 derivation path by default.
// walletData.derivation = 245
//
// // HDNode of BIP44 account
// const account = this.bchjs.HDNode.derivePath(
// masterHDNode,
// `m/44'/${walletData.derivation}'/0'`
// )
//
// // derive the first external change address HDNode which is going to spend utxo
// const change = this.bchjs.HDNode.derivePath(account, '0/0')
//
// // get the cash address
// walletData.rootAddress = this.bchjs.HDNode.toCashAddress(change)
//
// // Initialize other data.
// walletData.balance = 0
// walletData.nextAddress = 1
// walletData.hasBalance = []
// walletData.addresses = []
// walletData.description = desc
// Write out the basic information into a json file for other apps to use.
// const filename = `${__dirname.toString()}/../../wallets/${name}.json`
await appUtils.saveWallet(filename, walletData)
return walletData
} catch (err) {
if (err.code !== 'EEXIT') console.log('Error in createWallet().')
throw err
}
}
// Validate the proper flags are passed in.
validateFlags (flags) {
// Exit if wallet not specified.
const name = flags.name
if (!name || name === '') {
throw new Error('You must specify a wallet with the -n flag.')
}
return true
}
}
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' }),
description: flags.string({
char: 'd',
description: 'Description of the wallet'
})
}
module.exports = CreateWallet