-
Notifications
You must be signed in to change notification settings - Fork 255
/
eckey.js
191 lines (161 loc) · 5.31 KB
/
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
var BigInteger = require('./jsbn/jsbn');
var sec = require('./jsbn/sec');
var base58 = require('./base58');
var Crypto = require('./crypto-js/crypto');
var util = require('./util');
var conv = require('./convert');
var Address = require('./address');
var ecdsa = require('./ecdsa');
var ecparams = sec("secp256k1");
var networkTypes = {
prod: 128,
testnet: 239
};
// input can be nothing, array of bytes, hex string, or base58 string
var ECKey = function (input) {
if (!(this instanceof ECKey)) {
return new ECKey(input);
}
this.compressed = !!ECKey.compressByDefault;
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 (util.isArray(input)) {
// Prepend zero byte to prevent interpretation as negative integer
this.priv = BigInteger.fromByteArrayUnsigned(input);
this.compressed = false;
} else if ("string" == typeof input) {
// A list of base58 encoded prefixes is at https://en.bitcoin.it/wiki/List_of_address_prefixes.
if (input.length == 51 && input[0] == '5') {
// Base58 encoded private key
this.priv = BigInteger.fromByteArrayUnsigned(ECKey.decodeString(input, networkTypes.prod));
this.compressed = false;
}
else if (input.length == 51 && input[0] == '9') {
this.priv = BigInteger.fromByteArrayUnsigned(ECKey.decodeString(input, networkTypes.testnet));
this.compressed = false;
}
else if (input.length == 52 && (input[0] === 'K' || input[0] === 'L')) {
// Base58 encoded private key
this.priv = BigInteger.fromByteArrayUnsigned(ECKey.decodeString(input, networkTypes.prod));
this.compressed = true;
}
else if (input.length == 52 && input[0] === 'c') {
// Base58 encoded private key
this.priv = BigInteger.fromByteArrayUnsigned(ECKey.decodeString(input, networkTypes.testnet));
this.compressed = true;
} else {
// hex string?
// //wtf is base64 here for?
// Prepend zero byte to prevent interpretation as negative integer
this.priv = BigInteger.fromByteArrayUnsigned(conv.base64ToBytes(input));
}
}
};
// TODO(shtylman) methods
// wallet import format (base58 check with meta info)
// fromWIF
// toWIF
// fromBytes
// toBytes
// fromHex
// toHex
/**
* 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.setCompressed = function (v) {
this.compressed = !!v;
};
/**
* Return public key in DER encoding.
*/
ECKey.prototype.getPub = function () {
return this.getPubPoint().getEncoded(this.compressed);
};
/**
* Return public point as ECPoint object.
*/
ECKey.prototype.getPubPoint = function () {
if (!this.pub) this.pub = ecparams.getG().multiply(this.priv);
return this.pub;
};
/**
* 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.pubKeyHash) return this.pubKeyHash;
return this.pubKeyHash = util.sha256ripe160(this.getPub());
};
ECKey.prototype.getBitcoinAddress = function (address_type) {
var hash = this.getPubKeyHash();
var addr = new Address(hash, address_type);
return addr;
};
ECKey.prototype.getExportedPrivateKey = function (bitcoinNetwork) {
bitcoinNetwork = bitcoinNetwork || 'prod';
var hash = this.priv.toByteArrayUnsigned();
while (hash.length < 32) hash.unshift(0);
hash.unshift(networkTypes[bitcoinNetwork]);
var checksum = Crypto.SHA256(Crypto.SHA256(hash, {asBytes: true}), {asBytes: true});
var bytes = hash.concat(checksum.slice(0,4));
return base58.encode(bytes);
};
ECKey.prototype.setPub = function (pub) {
this.pub = ECPointFp.decodeFrom(ecparams.getCurve(), pub);
};
ECKey.prototype.toString = function (format) {
if (format === "base64") {
return conv.bytesToBase64(this.priv.toByteArrayUnsigned());
} else {
return conv.bytesToHex(this.priv.toByteArrayUnsigned());
}
};
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 an exported private key contained in a string.
*/
ECKey.decodeString = function (string, expectedVersion) {
var bytes = base58.decode(string);
if (bytes.length !== 37 && bytes.length !== 38) {
throw new Error('not a valid base58 encoded private key');
}
//Format:
//* uncompressed: 0x80 + [32-byte secret] + [4 bytes of Hash() of
//previous 33 bytes], base58 encoded
//* compressed: 0x80 + [32-byte secret] + 0x01 + [4 bytes of Hash()
//previous 34 bytes], base58 encoded
if (bytes[33] === 0x01) {
// compressed
}
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 !== expectedVersion)
throw "Version "+version+" not expected, expected " + expectedVersion + "!";
return hash;
};
module.exports = ECKey;