-
Notifications
You must be signed in to change notification settings - Fork 1
/
address.js
93 lines (73 loc) · 3.02 KB
/
address.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
var bt = require("bitcoinjs-lib")
var rnd = require('randombytes')
const argv = require('yargs')
.alias('T', 'testnet').alias('i', 'index').alias('t', 'type')
.number('i').string('t').boolean('T').default('t', 'normal').argv
if (argv.T) {
var network = bt.networks.testnet
var cointype = 1
} else {
var network = bt.networks.bitcoin
var cointype = 0
}
var seed = process.env.SEED
if (!seed) {
throw new Error("no seed specified")
}
var master = bt.HDNode.fromSeedBuffer(Buffer.from(seed, 'base64'), network)
var index = argv.i
if (index === undefined) {
throw new Error("Address index not specified")
}
var pubkeysFor = function(purpose, accounts) {
var n = master.deriveHardened(purpose).deriveHardened(cointype)
return accounts.map(function(ai) {
console.log("Path(" + ai + "): m/" + purpose + "'/" + cointype + "'/" + ai + "'/0/" + index)
return n.deriveHardened(ai).derive(0).derive(index).getPublicKeyBuffer()
})
}
var type = argv.t
if (type === 'normal') {
var pubkey = pubkeysFor(44, [0])[0]
var pkscr = bt.script.pubKeyHash.output.encode(bt.crypto.hash160(pubkey))
} else if (type === 'multisig') {
var pubkeys = pubkeysFor(44, [1, 2, 3])
var redeem = bt.script.multisig.output.encode(2, pubkeys)
var pkscr = bt.script.scriptHash.output.encode(bt.crypto.hash160(redeem))
} else if (type === 'p2shsegwit') {
var pubkey = pubkeysFor(49, [0])[0]
var redeem = bt.script.witnessPubKeyHash.output.encode(bt.crypto.hash160(pubkey))
var pkscr = bt.script.scriptHash.output.encode(bt.crypto.hash160(redeem))
} else if (type === 'p2shsegwit-multisig') {
var pubkeys = pubkeysFor(49, [1, 2, 3])
var witscr = bt.script.multisig.output.encode(2, pubkeys)
var redeem = bt.script.witnessScriptHash.output.encode(bt.crypto.sha256(witscr))
var pkscr = bt.script.scriptHash.output.encode(bt.crypto.hash160(redeem))
} else if (type === 'segwit') {
var pubkey = pubkeysFor(173, [0])[0]
var pkscr = bt.script.witnessPubKeyHash.output.encode(bt.crypto.hash160(pubkey))
} else if (type === 'segwit-multisig') {
var pubkeys = pubkeysFor(173, [1, 2, 3])
var witscr = bt.script.multisig.output.encode(2, pubkeys)
var pkscr = bt.script.witnessScriptHash.output.encode(bt.crypto.sha256(witscr))
}
if (witscr) {
console.log("-------------------------------------------------------------")
console.log("Witness Script (for P2WSH):");
console.log(" " + witscr.toString('hex'))
console.log(" " + bt.script.toASM(witscr))
}
if (redeem) {
console.log("-------------------------------------------------------------")
console.log("Redeem Script (for P2SH):");
console.log(" " + redeem.toString('hex'))
console.log(" " + bt.script.toASM(redeem))
}
if (pkscr) {
console.log("-------------------------------------------------------------")
console.log("Pubkey Script:");
console.log(" " + pkscr.toString('hex'))
console.log(" " + bt.script.toASM(pkscr))
console.log("-------------------------------------------------------------")
console.log("Address: " + bt.address.fromOutputScript(pkscr, network))
}