/
balance.js
180 lines (168 loc) · 5.37 KB
/
balance.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* eslint new-cap: 0, max-depth: 0 */
const { Command, flags } = require('@oclif/command')
const { red, white, black } = require('kleur')
const ora = require('ora')
const validateQrlAddress = require('@theqrl/validate-qrl-address')
const BigNumber = require('bignumber.js')
const fs = require('fs')
const aes256 = require('aes256')
const { cli } = require('cli-ux')
const Qrlnode = require('../functions/grpc')
const shorPerQuanta = 10 ** 9
const openWalletFile = (path) => {
const contents = fs.readFileSync(path)
return JSON.parse(contents)[0]
}
/* eslint-disable */
const addressForAPI = (address) => {
return Buffer.from(address.substring(1), 'hex')
}
/* eslint-enable */
class Balance extends Command {
async run() {
const { args, flags } = this.parse(Balance)
let {address} = args
if (!validateQrlAddress.hexString(address).result) {
// not a valid address - is it a file?
let isFile = false
let isValidFile = false
const path = address
try {
if (fs.existsSync(path)) {
isFile = true
}
} catch (error) {
this.log(`${red('⨉')} Unable to get a balance: invalid QRL address/wallet file`)
this.exit(1)
}
if (isFile === false) {
this.log(`${red('⨉')} Unable to get a balance: invalid QRL address/wallet file`)
this.exit(1)
} else {
const walletJson = openWalletFile(path)
try {
if (walletJson.encrypted === false) {
isValidFile = true
address = walletJson.address
}
if (walletJson.encrypted === true) {
let password = ''
if (flags.password) {
password = flags.password
} else {
password = await cli.prompt('Enter password for wallet file', { type: 'hide' })
}
address = aes256.decrypt(password, walletJson.address)
if (validateQrlAddress.hexString(address).result) {
isValidFile = true
} else {
this.log(`${red('⨉')} Unable to open wallet file: invalid password`)
this.exit(1)
}
}
} catch (error) {
this.exit(1)
}
this.log(`${black().bgWhite(address)}`)
}
if (isValidFile === false) {
this.log(`${red('⨉')} Unable to get a balance: invalid QRL address/wallet file`)
this.exit(1)
}
}
let grpcEndpoint = 'mainnet-1.automated.theqrl.org:19009'
let network = 'Mainnet'
if (flags.grpc) {
grpcEndpoint = flags.grpc
network = `Custom GRPC endpoint: [${flags.grpc}]`
}
if (flags.testnet) {
grpcEndpoint = 'testnet-1.automated.theqrl.org:19009'
network = 'Testnet'
}
if (flags.mainnet) {
grpcEndpoint = 'mainnet-1.automated.theqrl.org:19009'
network = 'Mainnet'
}
this.log(white().bgBlue(network))
const spinner = ora({ text: 'Fetching balance from node...' }).start()
const Qrlnetwork = await new Qrlnode(grpcEndpoint)
try {
await Qrlnetwork.connect()
// verify we have connected and try again if not
let i = 0
const count = 5
while (Qrlnetwork.connection === false && i < count) {
spinner.succeed(`retry connection attempt: ${i}...`)
// eslint-disable-next-line no-await-in-loop
await Qrlnetwork.connect()
// eslint-disable-next-line no-plusplus
i++
}
} catch (e) {
spinner.fail(`Failed to connect to node. Check network connection & parameters.\n${e}`)
this.exit(1)
}
const request = {
address: addressForAPI(address),
}
const response = await Qrlnetwork.api('GetOptimizedAddressState', request)
const balance = new BigNumber(parseInt(response.state.balance, 10))
if (flags.shor) {
spinner.succeed(`Balance: ${balance} Shor`)
}
if (flags.quanta || !flags.shor) {
// default to showing balance in Quanta if no flags
spinner.succeed(`Balance: ${balance / shorPerQuanta} Quanta`)
}
if (flags.quanta && flags.shor) {
this.log(`${red('⨉')} Please enter one, shor (-s) or quanta (-q)`)
this.exit(1)
}
}
}
Balance.description = `Get a wallet balance from the network for an address
Queries the balance of the wallet.json file or address.
Use the (-p) flag to pass the password of encrypted wallet file.
Documentation at https://docs.theqrl.org/developers/qrl-cli
`
Balance.args = [
{
name: 'address',
description: 'QRL address or wallet.json file to return a balance for',
required: true,
},
]
Balance.flags = {
testnet: flags.boolean({
char: 't',
default: false,
description: 'Queries testnet network for the address balance'
}),
mainnet: flags.boolean({
char: 'm',
default: false,
description: 'Queries mainnet network for the address balance'
}),
shor: flags.boolean({
char: 's',
default: false,
description: 'Reports the QRL address balance in Shor'
}),
quanta: flags.boolean({
char: 'q',
default: false,
description: 'Reports the QRL address balance in Quanta'
}),
grpc: flags.string({
char: 'g',
required: false,
description: 'Custom grcp endpoint to connect a hosted QRL node (-g 127.0.0.1:19009)',
}),
password: flags.string({
char: 'p',
required: false,
description: 'Encrypted QRL wallet.json password to decrypt',
}),
}
module.exports = { Balance }