-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathxpub.js
More file actions
executable file
·146 lines (132 loc) · 3.67 KB
/
xpub.js
File metadata and controls
executable file
·146 lines (132 loc) · 3.67 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
#!/usr/bin/env node
import {
NETWORKS,
isValidAddress,
isValidExtPubKey,
addressFromExtPubKey,
addressesFromExtPubKey,
Purpose,
} from "@swan-bitcoin/xpub-lib"
import { version } from "../package.json"
function parsePurpose(purpose) {
switch (purpose.toLowerCase()) {
case "p2pkh": {
return Purpose.P2PKH
}
case "p2sh": {
return Purpose.P2SH
}
case "p2wpkh": {
return Purpose.P2WPKH
}
default: {
return undefined
}
}
}
function printAddress(address, verbose = false) {
if (verbose) {
console.log(address)
} else {
console.log(address.address)
}
}
const { program } = require("commander")
program.version(`${version}`)
program
.command("derive [extPubKey]")
.description("derive address(es) from an extended public key")
.option(
"-p, --purpose <purpose>",
"derivation purpose which dictates the address type ['p2pkh', 'p2sh', 'p2wpkh']",
"p2wpkh"
) // use `choices` once this feature is released: https://github.com/tj/commander.js/issues/518
.option(
"-n, --addressCount <addressCount>",
"number of addresses to generate",
1
)
.option(
"-c, --accountNumber <accountNumber>",
"the account number as defined in BIP 44",
0
)
.option(
"-i, --keyIndex <keyIndex>",
"index of the address to generate (ignored if `addressCount` is set)",
0
)
.option("-t, --testnet", "use TESTNET")
.option("-v, --verbose", "verbose output")
.action((extPubKey, cmdObj) => {
if (!extPubKey) {
cmdObj.help()
}
const network = cmdObj.testnet ? NETWORKS.TESTNET : NETWORKS.MAINNET
if (!isValidExtPubKey(extPubKey, network)) {
console.error(`error: invalid extended public key '${extPubKey}'`)
process.exitCode = 1
return
}
const purpose = cmdObj.purpose
? parsePurpose(cmdObj.purpose)
: Purpose.P2WPKH // default to P2WPKH
const keyIndex = cmdObj.keyIndex ? cmdObj.keyIndex : 0 // default to P2WPKH
const accountNumber = cmdObj.accountNumber ? cmdObj.accountNumber : 0 // default to P2WPKH
if (cmdObj.addressCount > 1) {
// Multiple addresses
const { addressCount } = cmdObj
const addresses = addressesFromExtPubKey({
extPubKey,
addressCount,
accountNumber,
purpose,
network,
})
addresses.forEach(address => {
printAddress(address, cmdObj.verbose)
})
} else {
// Single address
const address = addressFromExtPubKey({
extPubKey,
accountNumber,
keyIndex,
purpose,
network,
})
printAddress(address, cmdObj.verbose)
}
})
program
.command("validate [encoded]")
.description(
"check an encoded bitcoin address or extended public key for validity"
)
.option("-a, --check-address", "check bitcoin address for validity")
.option("-x, --check-ext", "check extended public key for validity")
.option("-t, --testnet", "use TESTNET")
.option("-v, --verbose", "verbose output")
.action((encoded, cmdObj) => {
if (!encoded) {
cmdObj.help()
}
const network = cmdObj.testnet ? NETWORKS.TESTNET : NETWORKS.MAINNET
let isValid = false
let type = ""
if (cmdObj.checkAddress) {
isValid = isValidAddress(encoded, network)
type = "address"
} else if (cmdObj.checkExt) {
isValid = isValidExtPubKey(encoded, network)
type = "extPubKey"
} else {
isValid =
isValidExtPubKey(encoded, network) || isValidAddress(encoded, network)
}
if (cmdObj.verbose) {
console.log(`${isValid ? "valid" : "invalid"} ${type} ${encoded}`)
}
process.exitCode = isValid ? 0 : 1
})
program.parse(process.argv)