-
Notifications
You must be signed in to change notification settings - Fork 0
/
rareaddressjs-lib.eckey.js
373 lines (328 loc) · 11.3 KB
/
rareaddressjs-lib.eckey.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
Bitcoin.KeyPool = (function () {
var KeyPool = function () {
this.keyArray = [];
this.push = function (item) {
if (item == null || item.priv == null) return;
var doAdd = true;
// prevent duplicates from being added to the array
for (var index in this.keyArray) {
var currentItem = this.keyArray[index];
if (currentItem != null && currentItem.priv != null && item.getBitcoinAddress() == currentItem.getBitcoinAddress()) {
doAdd = false;
break;
}
}
if (doAdd) this.keyArray.push(item);
};
this.reset = function () {
this.keyArray = [];
};
this.getArray = function () {
// copy array
return this.keyArray.slice(0);
};
this.setArray = function (ka) {
this.keyArray = ka;
};
this.length = function () {
return this.keyArray.length;
};
this.toString = function () {
var keyPoolString = "# = " + this.length() + "\n";
var pool = this.getArray();
for (var index in pool) {
var item = pool[index];
if (Bitcoin.Util.hasMethods(item, 'getBitcoinAddress', 'toString')) {
if (item != null) {
keyPoolString += "\"" + item.getBitcoinAddress() + "\"" + ", \"" + item.toString("wif") + "\"\n";
}
}
}
return keyPoolString;
};
return this;
};
return new KeyPool();
})();
Bitcoin.Bip38Key = (function () {
var Bip38 = function (address, encryptedKey) {
this.address = address;
this.priv = encryptedKey;
};
Bip38.prototype.getBitcoinAddress = function () {
return this.address;
};
Bip38.prototype.toString = function () {
return this.priv;
};
return Bip38;
})();
//https://raw.github.com/pointbiz/bitcoinjs-lib/9b2f94a028a7bc9bed94e0722563e9ff1d8e8db8/src/eckey.js
Bitcoin.ECKey = (function () {
var ECDSA = Bitcoin.ECDSA;
var KeyPool = Bitcoin.KeyPool;
var ecparams = EllipticCurve.getSECCurveByName("secp256k1");
var ECKey = function (input) {
if (!input) {
// Generate new key
var n = ecparams.getN();
this.priv = ECDSA.getBigRandom(n);
} else if (input instanceof BigInteger) {
// Input is a private key value
this.priv = input;
} else if (Bitcoin.Util.isArray(input)) {
// Prepend zero byte to prevent interpretation as negative integer
this.priv = BigInteger.fromByteArrayUnsigned(input);
} else if ("string" == typeof input) {
var bytes = null;
try{
if (ECKey.isWalletImportFormat(input)) {
bytes = ECKey.decodeWalletImportFormat(input);
} else if (ECKey.isCompressedWalletImportFormat(input)) {
bytes = ECKey.decodeCompressedWalletImportFormat(input);
this.compressed = true;
} else if (ECKey.isMiniFormat(input)) {
bytes = Crypto.SHA256(input, { asBytes: true });
} else if (ECKey.isHexFormat(input)) {
bytes = Crypto.util.hexToBytes(input);
} else if (ECKey.isBase64Format(input)) {
bytes = Crypto.util.base64ToBytes(input);
}
} catch (exc1) {
this.setError(exc1);
}
if (ECKey.isBase6Format(input)) {
this.priv = new BigInteger(input, 6);
} else if (bytes == null || bytes.length != 32) {
this.priv = null;
} else {
// Prepend zero byte to prevent interpretation as negative integer
this.priv = BigInteger.fromByteArrayUnsigned(bytes);
}
}
this.compressed = (this.compressed == undefined) ? !!ECKey.compressByDefault : this.compressed;
try {
// check not zero
if (this.priv != null && BigInteger.ZERO.compareTo(this.priv) == 0) this.setError("Error: BigInteger equal to zero.");
// valid range [0x1, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140])
var hexKeyRangeLimit = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140";
var rangeLimitBytes = Crypto.util.hexToBytes(hexKeyRangeLimit);
var limitBigInt = BigInteger.fromByteArrayUnsigned(rangeLimitBytes);
if (this.priv != null && limitBigInt.compareTo(this.priv) < 0) this.setError("Error: BigInteger outside of curve range.")
if (this.priv != null) {
KeyPool.push(this);
}
} catch (exc2) {
this.setError(exc2);
}
};
ECKey.privateKeyPrefix = 0x80; // mainnet 0x80 testnet 0xEF
/**
* Whether public keys should be returned compressed by default.
*/
ECKey.compressByDefault = false;
/**
* Set whether the public key should be returned compressed or not.
*/
ECKey.prototype.setError = function (err) {
this.error = err;
this.priv = null;
return this;
};
/**
* Set whether the public key should be returned compressed or not.
*/
ECKey.prototype.setCompressed = function (v) {
this.compressed = !!v;
if (this.pubPoint) this.pubPoint.compressed = this.compressed;
return this;
};
/*
* Return public key as a byte array in DER encoding
*/
ECKey.prototype.getPub = function () {
if (this.compressed) {
if (this.pubComp) return this.pubComp;
return this.pubComp = this.getPubPoint().getEncoded(1);
} else {
if (this.pubUncomp) return this.pubUncomp;
return this.pubUncomp = this.getPubPoint().getEncoded(0);
}
};
/**
* Return public point as ECPoint object.
*/
ECKey.prototype.getPubPoint = function () {
if (!this.pubPoint) {
this.pubPoint = ecparams.getG().multiply(this.priv);
this.pubPoint.compressed = this.compressed;
}
return this.pubPoint;
};
ECKey.prototype.getPubKeyHex = function () {
if (this.compressed) {
if (this.pubKeyHexComp) return this.pubKeyHexComp;
return this.pubKeyHexComp = Crypto.util.bytesToHex(this.getPub()).toString().toUpperCase();
} else {
if (this.pubKeyHexUncomp) return this.pubKeyHexUncomp;
return this.pubKeyHexUncomp = Crypto.util.bytesToHex(this.getPub()).toString().toUpperCase();
}
};
/**
* Get the pubKeyHash for this key.
*
* This is calculated as RIPE160(SHA256([encoded pubkey])) and returned as
* a byte array.
*/
ECKey.prototype.getPubKeyHash = function () {
if (this.compressed) {
if (this.pubKeyHashComp) return this.pubKeyHashComp;
return this.pubKeyHashComp = Bitcoin.Util.sha256ripe160(this.getPub());
} else {
if (this.pubKeyHashUncomp) return this.pubKeyHashUncomp;
return this.pubKeyHashUncomp = Bitcoin.Util.sha256ripe160(this.getPub());
}
};
ECKey.prototype.getBitcoinAddress = function () {
var hash = this.getPubKeyHash();
var addr = new Bitcoin.Address(hash);
return addr.toString();
};
/*
* Takes a public point as a hex string or byte array
*/
ECKey.prototype.setPub = function (pub) {
// byte array
if (Bitcoin.Util.isArray(pub)) {
pub = Crypto.util.bytesToHex(pub).toString().toUpperCase();
}
var ecPoint = ecparams.getCurve().decodePointHex(pub);
this.setCompressed(ecPoint.compressed);
this.pubPoint = ecPoint;
return this;
};
// Sipa Private Key Wallet Import Format
ECKey.prototype.getBitcoinWalletImportFormat = function () {
var bytes = this.getBitcoinPrivateKeyByteArray();
if (bytes == null) return "";
bytes.unshift(ECKey.privateKeyPrefix); // prepend 0x80 byte
if (this.compressed) bytes.push(0x01); // append 0x01 byte for compressed format
var checksum = Crypto.SHA256(Crypto.SHA256(bytes, { asBytes: true }), { asBytes: true });
bytes = bytes.concat(checksum.slice(0, 4));
var privWif = Bitcoin.Base58.encode(bytes);
return privWif;
};
// Private Key Hex Format
ECKey.prototype.getBitcoinHexFormat = function () {
return Crypto.util.bytesToHex(this.getBitcoinPrivateKeyByteArray()).toString().toUpperCase();
};
// Private Key Base64 Format
ECKey.prototype.getBitcoinBase64Format = function () {
return Crypto.util.bytesToBase64(this.getBitcoinPrivateKeyByteArray());
};
ECKey.prototype.getBitcoinPrivateKeyByteArray = function () {
if (this.priv == null) return null;
// Get a copy of private key as a byte array
var bytes = this.priv.toByteArrayUnsigned();
// zero pad if private key is less than 32 bytes
while (bytes.length < 32) bytes.unshift(0x00);
return bytes;
};
ECKey.prototype.toString = function (format) {
format = format || "";
if (format.toString().toLowerCase() == "base64" || format.toString().toLowerCase() == "b64") {
return this.getBitcoinBase64Format();
}
// Wallet Import Format
else if (format.toString().toLowerCase() == "wif") {
return this.getBitcoinWalletImportFormat();
}
else {
return this.getBitcoinHexFormat();
}
};
ECKey.prototype.sign = function (hash) {
return ECDSA.sign(hash, this.priv);
};
ECKey.prototype.verify = function (hash, sig) {
return ECDSA.verify(hash, sig, this.getPub());
};
/**
* Parse a wallet import format private key contained in a string.
*/
ECKey.decodeWalletImportFormat = function (privStr) {
var bytes = Bitcoin.Base58.decode(privStr);
var hash = bytes.slice(0, 33);
var checksum = Crypto.SHA256(Crypto.SHA256(hash, { asBytes: true }), { asBytes: true });
if (checksum[0] != bytes[33] ||
checksum[1] != bytes[34] ||
checksum[2] != bytes[35] ||
checksum[3] != bytes[36]) {
throw "Checksum validation failed!";
}
var version = hash.shift();
if (version != ECKey.privateKeyPrefix) {
throw "Version " + version + " not supported!";
}
return hash;
};
/**
* Parse a compressed wallet import format private key contained in a string.
*/
ECKey.decodeCompressedWalletImportFormat = function (privStr) {
var bytes = Bitcoin.Base58.decode(privStr);
var hash = bytes.slice(0, 34);
var checksum = Crypto.SHA256(Crypto.SHA256(hash, { asBytes: true }), { asBytes: true });
if (checksum[0] != bytes[34] ||
checksum[1] != bytes[35] ||
checksum[2] != bytes[36] ||
checksum[3] != bytes[37]) {
throw "Checksum validation failed!";
}
var version = hash.shift();
if (version != ECKey.privateKeyPrefix) {
throw "Version " + version + " not supported!";
}
hash.pop();
return hash;
};
// 64 characters [0-9A-F]
ECKey.isHexFormat = function (key) {
key = key.toString();
return /^[A-Fa-f0-9]{64}$/.test(key);
};
// 51 characters base58, always starts with a '5'
ECKey.isWalletImportFormat = function (key) {
key = key.toString();
return (ECKey.privateKeyPrefix == 0x80) ?
(/^5[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{50}$/.test(key)) :
(/^9[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{50}$/.test(key));
};
// 52 characters base58
ECKey.isCompressedWalletImportFormat = function (key) {
key = key.toString();
return (ECKey.privateKeyPrefix == 0x80) ?
(/^[LK][123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{51}$/.test(key)) :
(/^c[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{51}$/.test(key));
};
// 44 characters
ECKey.isBase64Format = function (key) {
key = key.toString();
return (/^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789=+\/]{44}$/.test(key));
};
// 99 characters, 1=1, if using dice convert 6 to 0
ECKey.isBase6Format = function (key) {
key = key.toString();
return (/^[012345]{99}$/.test(key));
};
// 22, 26 or 30 characters, always starts with an 'S'
ECKey.isMiniFormat = function (key) {
key = key.toString();
var validChars22 = /^S[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{21}$/.test(key);
var validChars26 = /^S[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{25}$/.test(key);
var validChars30 = /^S[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{29}$/.test(key);
var testBytes = Crypto.SHA256(key + "?", { asBytes: true });
return ((testBytes[0] === 0x00 || testBytes[0] === 0x01) && (validChars22 || validChars26 || validChars30));
};
return ECKey;
})();