This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
cli.js
70 lines (59 loc) · 1.48 KB
/
cli.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
const meow = require('meow')
const { toChecksumAddress, checkAddressChecksum } = require('.')
const cli = meow(`
Usage
$ ethereum_address_checksum <address> [flags]
Options
--check, -r Check if address is a checksummed address
Examples
$ ethereum_address_checksum 0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1
0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
$ ethereum_address_checksum 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1 --check
true
`, {
flags: {
check: {
type: 'boolean'
},
chain: {
type: 'string'
}
}
})
if (process.stdin) {
process.stdin.setEncoding('utf8')
process.stdin.resume()
let content = ''
process.stdin.on('data', (buf) => {
content += buf.toString()
})
setTimeout(() => {
content = content.trim()
let address = (content || '0')
const check = cli.flags.check
const chainId = cli.flags.chain
if (!content) {
address = cli.input[0]
}
run(address, check, chainId)
}, 10)
} else {
const address = cli.input[0]
const check = cli.flags.check
const chainId = cli.flags.chain
run(address, check, chainId)
}
function run(address, check, chainId) {
if (!address) {
console.log('address argument is required')
process.exit(1)
}
if (check) {
const valid = checkAddressChecksum(address, chainId)
console.log(valid)
process.exit(valid ? 0 : 1)
} else {
console.log(toChecksumAddress(address, chainId))
process.exit(0)
}
}