-
Notifications
You must be signed in to change notification settings - Fork 2
/
xpriv.js
215 lines (213 loc) · 8.32 KB
/
xpriv.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = require("assert");
const bip32_1 = __importDefault(require("bip32"));
const bitcoin = __importStar(require("bitcoinjs-lib"));
const bs58_1 = __importDefault(require("bs58"));
const curve = __importStar(require("tiny-secp256k1"));
const btc_1 = require("./btc");
const bip32 = (0, bip32_1.default)(curve);
const pad = (s, len) => s + ' '.repeat(len - s.length);
const color = (...colors) => (colors.length ? `\x1b[${colors.join(';')}m` : '');
const checksum = (key) => bitcoin.crypto.hash256(key.subarray(0, 78)).copy(key, 78, 0, 4);
const RESET = 0;
const BOLD = 1;
const REVERSED = 7;
const RED = 31;
const CYAN = 36;
const versions = [
{ network: 'mainnet', version: Buffer.from('0488b21e', 'hex'), private: false, script: 'p2pkh or p2sh' },
{ network: 'mainnet', version: Buffer.from('0488ade4', 'hex'), private: true, script: 'p2pkh or p2sh' },
{ network: 'mainnet', version: Buffer.from('049d7cb2', 'hex'), private: false, script: 'p2wpkh-p2sh' },
{ network: 'mainnet', version: Buffer.from('049d7878', 'hex'), private: true, script: 'p2wpkh-p2sh' },
{ network: 'mainnet', version: Buffer.from('0295b43f', 'hex'), private: false, script: 'p2wsh-p2sh' },
{ network: 'mainnet', version: Buffer.from('0295b005', 'hex'), private: true, script: 'p2wsh-p2sh' },
{ network: 'mainnet', version: Buffer.from('04b24746', 'hex'), private: false, script: 'p2wpkh' },
{ network: 'mainnet', version: Buffer.from('04b2430c', 'hex'), private: true, script: 'p2wpkh' },
{ network: 'mainnet', version: Buffer.from('02aa7ed3', 'hex'), private: false, script: 'p2wsh' },
{ network: 'mainnet', version: Buffer.from('02aa7a99', 'hex'), private: true, script: 'p2wsh' },
{ network: 'testnet', version: Buffer.from('043587cf', 'hex'), private: false, script: 'p2pkh or p2sh' },
{ network: 'testnet', version: Buffer.from('04358394', 'hex'), private: true, script: 'p2pkh or p2sh' },
{ network: 'testnet', version: Buffer.from('044a5262', 'hex'), private: false, script: 'p2wpkh-p2sh' },
{ network: 'testnet', version: Buffer.from('044a4e28', 'hex'), private: true, script: 'p2wpkh-p2sh' },
{ network: 'testnet', version: Buffer.from('024289ef', 'hex'), private: false, script: 'p2wsh-p2sh' },
{ network: 'testnet', version: Buffer.from('024285b5', 'hex'), private: true, script: 'p2wsh-p2sh' },
{ network: 'testnet', version: Buffer.from('045f1cf6', 'hex'), private: false, script: 'p2wpkh' },
{ network: 'testnet', version: Buffer.from('045f18bc', 'hex'), private: true, script: 'p2wpkh' },
{ network: 'testnet', version: Buffer.from('02575483', 'hex'), private: false, script: 'p2wsh' },
{ network: 'testnet', version: Buffer.from('02575048', 'hex'), private: true, script: 'p2wsh' },
];
let i, k, ver, depth, fingerprint, n, chain, key, type, bip32key;
async function main() {
while (true) {
i = await (0, btc_1.input)(`\nEnter master/extended key to load or a command${i ? '' : ` (type 'help' for a list of commands)`}\n> `);
if (!i) {
continue;
}
const args = i.split(' ');
if (args[0] == 'help') {
console.log(`
Commands:
help: show this list
exit: exit
load <key>: loads a key
derive <path>: derive the loaded key to a new one. loads the new one
info: displays information about the loaded key`);
}
else if (args[0] == 'exit') {
process.exit(0);
}
else if (args[0] == 'load') {
if (args.length > 1) {
if (readKey(args[1])) {
console.log('Key loaded');
}
}
else {
console.log('load requires an argument <key>');
}
}
else if (args[0] == 'derive') {
if (args.length > 1) {
if (derive(args[1])) {
console.log(`New key loaded: ${bs58_1.default.encode(k)}`);
}
}
else {
console.log('derive requires an argument <path>');
}
}
else if (args[0] == 'info') {
displayKey();
}
else {
if (readKey(args[0])) {
console.log('Key loaded');
}
}
}
}
function derive(path) {
try {
bip32key = bip32key.derivePath(path);
}
catch (e) {
console.log(e.message);
return false;
}
readKey(bip32key.toBase58(), true);
return true;
}
function loadKey(s) {
try {
k = Buffer.from(bs58_1.default.decode(s));
(0, assert_1.strict)(k.length == 82);
}
catch (e) {
console.log('Invalid input');
k = undefined;
return false;
}
if (bitcoin.crypto.hash256(k.subarray(0, 78)).compare(k, 78, 82, 0, 4)) {
console.log(color(BOLD, RED) + 'Error: Invalid checksum' + color(RESET));
checksum(k);
console.log('Key with recalculated checksum (only use if you know what you are doing!):\n' + bs58_1.default.encode(k));
k = undefined;
return false;
}
return true;
}
function readKey(s, useOldVersion = false) {
if (!loadKey(s)) {
return false;
}
if (useOldVersion) {
ver.copy(k);
checksum(k);
}
else {
ver = k.subarray(0, 4);
}
depth = k.readUint8(4);
fingerprint = k.subarray(5, 9);
n = k.readUint32BE(9);
chain = k.subarray(13, 45);
key = k.subarray(45, 78);
type = versions.find(v => !v.version.compare(ver));
if (!type) {
console.log(`Invalid version: 0x${ver.toString('hex')}`);
return false;
}
if (!useOldVersion) {
const clone = Buffer.allocUnsafe(82);
k.copy(clone, 4, 4, 78);
versions.find(v => v.private == type.private && v.network == type.network).version.copy(clone);
checksum(clone);
bip32key = bip32.fromBase58(bs58_1.default.encode(clone), type.network === 'mainnet' ? bitcoin.networks.bitcoin : bitcoin.networks.testnet);
}
return true;
}
function displayKey() {
if (!k) {
console.log('No key loaded');
return;
}
console.log(`
Version: ${ver.toString('hex')}
Depth: ${depth}
Master fingerprint: ${fingerprint.toString('hex')}
Child number: ${n & 0x7fffffff}${n & 0x80000000 ? `'` : ''}
Chain code: ${chain.toString('hex')}
Key: ${(key[0] ? key : key.subarray(1)).toString('hex')}
Network: ${type.network}
Key type: ${type.private ? 'private' : 'public'}
Electrum script type: ${type.script}
All Electrum defined master key versions:
Network Key type Script type Key`);
versions
.filter(v => v.private == type.private)
.forEach((v, i) => {
v.version.copy(k);
checksum(k);
const colors = [];
if (i & 1) {
colors.push(REVERSED);
}
if (v == type) {
colors.push(BOLD, CYAN);
}
console.log(color(...colors) +
pad(v.network, 12) +
pad(v.private ? 'private' : 'public', 13) +
pad(v.script, 15) +
bs58_1.default.encode(k) +
color(RESET));
});
}
main();