forked from nch-bowstave/minerid-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·150 lines (141 loc) · 3.86 KB
/
Copy pathcli.js
File metadata and controls
executable file
·150 lines (141 loc) · 3.86 KB
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
#!/usr/bin/env node
const commandLineArgs = require('command-line-args')
const commandLineUsage = require('command-line-usage')
const config = require('config')
const network = config.get('network')
const coinbaseDocService = require('./services/coinbaseDocumentService')
const fm = require('./utils/filemanager')
; (async () => {
const optionDefinitions = [
{
name: 'command',
type: String,
defaultOption: true,
multiple: true,
description: 'generateminerid, generatevctx, rotateminerid, config'
},
{
name: 'help',
alias: 'h',
type: Boolean,
description: 'Display this usage guide.'
},
{
name: 'height',
alias: 't',
type: Number,
description: 'The block height that will be included in the coinbase document.'
},
{
name: 'name',
alias: 'n',
type: String,
description: 'The name associated with the minerId.'
}
]
const usage = commandLineUsage([
{
header: 'Miner Id Client',
content: 'Generate a minerId or get a signed coinbase document for a minerId.'
},
{
header: 'Options',
optionList: optionDefinitions
},
{
header: 'Examples',
content: [
{
desc: 'Generate a minerId',
example: 'npm run cli -- generateminerid --name [alias]'
},
{
desc: 'Generate VCTx',
example: 'npm run cli -- generatevctx -n [alias]'
},
{
desc: 'Add minerContact data',
example: 'npm run cli -- config email=s@aliasDomain.com -n [alias]'
},
{
desc: 'Generate op_return with signed coinbase document',
example: 'npm run cli -- --height 5123123 --name [alias]'
},
{
desc: 'Rotate minerId',
example: 'npm run cli -- rotateminerid --name [alias]'
}
]
}
])
let options
try {
options = commandLineArgs(optionDefinitions)
} catch (e) {
console.log('Unknown argument')
console.log(usage)
process.exit(0)
}
if (options.help || (!options.name && !options.height)) {
console.log(usage)
process.exit(0)
}
if (!options.name) {
console.log('You must specify a name:')
console.log('--name [name]')
console.log('-n [name]')
process.exit(0)
}
switch (network) {
case 'livenet':
console.log('--- MAIN NETWORK ---')
break
case 'testnet':
console.log('--- TEST NETWORK ---')
break
case 'regtest':
console.log('--- REGTEST ---')
break
default:
console.log('Network cofiguration not properly set in config.json file')
break
}
if (options.command) {
switch (options.command[0].toLowerCase()) {
case 'config': {
if (options.command.length < 2 || options.command[1].indexOf('=') === -1) {
console.log(fm.getOptionalMinerData(options.name))
break
}
const nvp = options.command[1].split('=')
fm.updateOptionalMinerData(options.name, nvp[0], nvp[1])
break
}
case 'generateminerid':
coinbaseDocService.generateMinerId(options.name)
break
case 'generatevctx':
await coinbaseDocService.generateVcTx(options.name)
break
case 'rotateminerid':
coinbaseDocService.rotateMinerId(options.name)
console.log('Rotated minerId')
break
default:
console.log(`Unknown command: ${options.command}`)
console.log(usage)
break
}
process.exit(0)
} else if (!options.height) {
console.log('You must specify a height')
console.log('--height [heightNumber]')
console.log('-h [heightNumber]')
process.exit(0)
}
const opReturn = await coinbaseDocService.createMinerIdOpReturn(options.height, options.name)
if (opReturn) {
console.log('OP_RETURN hex:')
console.log(opReturn)
}
})()