@opcat-labs/opcat 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.yaml +3 -0
- package/index.d.ts +1541 -0
- package/index.js +74 -0
- package/lib/address.js +478 -0
- package/lib/block/block.js +277 -0
- package/lib/block/blockheader.js +295 -0
- package/lib/block/index.js +4 -0
- package/lib/block/merkleblock.js +323 -0
- package/lib/bn.js +3423 -0
- package/lib/crypto/bn.js +278 -0
- package/lib/crypto/ecdsa.js +339 -0
- package/lib/crypto/hash.browser.js +171 -0
- package/lib/crypto/hash.js +2 -0
- package/lib/crypto/hash.node.js +171 -0
- package/lib/crypto/point.js +221 -0
- package/lib/crypto/random.browser.js +28 -0
- package/lib/crypto/random.js +2 -0
- package/lib/crypto/random.node.js +11 -0
- package/lib/crypto/signature.js +325 -0
- package/lib/encoding/base58.js +111 -0
- package/lib/encoding/base58check.js +121 -0
- package/lib/encoding/bufferreader.js +212 -0
- package/lib/encoding/bufferwriter.js +140 -0
- package/lib/encoding/decode-asm.js +24 -0
- package/lib/encoding/decode-hex.js +32 -0
- package/lib/encoding/decode-script-chunks.js +43 -0
- package/lib/encoding/encode-hex.js +284 -0
- package/lib/encoding/is-hex.js +7 -0
- package/lib/encoding/varint.js +75 -0
- package/lib/errors/index.js +54 -0
- package/lib/errors/spec.js +314 -0
- package/lib/hash-cache.js +50 -0
- package/lib/hdprivatekey.js +678 -0
- package/lib/hdpublickey.js +525 -0
- package/lib/message/message.js +191 -0
- package/lib/mnemonic/mnemonic.js +303 -0
- package/lib/mnemonic/pbkdf2.browser.js +68 -0
- package/lib/mnemonic/pbkdf2.js +2 -0
- package/lib/mnemonic/pbkdf2.node.js +68 -0
- package/lib/mnemonic/words/chinese.js +2054 -0
- package/lib/mnemonic/words/english.js +2054 -0
- package/lib/mnemonic/words/french.js +2054 -0
- package/lib/mnemonic/words/index.js +8 -0
- package/lib/mnemonic/words/italian.js +2054 -0
- package/lib/mnemonic/words/japanese.js +2054 -0
- package/lib/mnemonic/words/spanish.js +2054 -0
- package/lib/networks.js +379 -0
- package/lib/opcode.js +255 -0
- package/lib/privatekey.js +374 -0
- package/lib/publickey.js +386 -0
- package/lib/script/index.js +5 -0
- package/lib/script/interpreter.js +1834 -0
- package/lib/script/script.js +1074 -0
- package/lib/script/stack.js +109 -0
- package/lib/script/write-i32-le.js +17 -0
- package/lib/script/write-push-data.js +35 -0
- package/lib/script/write-u16-le.js +12 -0
- package/lib/script/write-u32-le.js +16 -0
- package/lib/script/write-u64-le.js +24 -0
- package/lib/script/write-u8-le.js +8 -0
- package/lib/script/write-varint.js +46 -0
- package/lib/transaction/index.js +7 -0
- package/lib/transaction/input/index.js +5 -0
- package/lib/transaction/input/input.js +354 -0
- package/lib/transaction/input/multisig.js +242 -0
- package/lib/transaction/input/publickey.js +100 -0
- package/lib/transaction/input/publickeyhash.js +118 -0
- package/lib/transaction/output.js +231 -0
- package/lib/transaction/sighash.js +167 -0
- package/lib/transaction/signature.js +97 -0
- package/lib/transaction/transaction.js +1639 -0
- package/lib/transaction/unspentoutput.js +113 -0
- package/lib/util/_.js +47 -0
- package/lib/util/js.js +90 -0
- package/lib/util/preconditions.js +33 -0
- package/package.json +26 -0
- package/test/address.js +509 -0
- package/test/block/block.js +251 -0
- package/test/block/blockheader.js +275 -0
- package/test/block/merklebloack.js +211 -0
- package/test/crypto/bn.js +177 -0
- package/test/crypto/ecdsa.js +391 -0
- package/test/crypto/hash.browser.js +135 -0
- package/test/crypto/hash.js +136 -0
- package/test/crypto/point.js +224 -0
- package/test/crypto/random.js +32 -0
- package/test/crypto/signature.js +409 -0
- package/test/data/bip69.json +215 -0
- package/test/data/bitcoind/base58_keys_invalid.json +52 -0
- package/test/data/bitcoind/base58_keys_valid.json +335 -0
- package/test/data/bitcoind/blocks.json +22 -0
- package/test/data/bitcoind/script_tests.json +3822 -0
- package/test/data/bitcoind/sig_canonical.json +7 -0
- package/test/data/bitcoind/sig_noncanonical.json +36 -0
- package/test/data/bitcoind/tx_invalid.json +445 -0
- package/test/data/bitcoind/tx_valid.json +44 -0
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +14 -0
- package/test/data/blk86756-testnet.json +684 -0
- package/test/data/block.hex +1 -0
- package/test/data/ecdsa.json +230 -0
- package/test/data/merkleblocks.js +488 -0
- package/test/data/messages.json +22 -0
- package/test/data/sighash.json +12 -0
- package/test/data/tx_creation.json +95 -0
- package/test/encoding/base58.js +131 -0
- package/test/encoding/base58check.js +136 -0
- package/test/encoding/bufferreader.js +337 -0
- package/test/encoding/bufferwriter.js +172 -0
- package/test/encoding/varint.js +104 -0
- package/test/hashCache.js +67 -0
- package/test/hdkeys.js +445 -0
- package/test/hdprivatekey.js +332 -0
- package/test/hdpublickey.js +304 -0
- package/test/index.js +16 -0
- package/test/message/message.js +204 -0
- package/test/mnemonic/data/fixtures.json +300 -0
- package/test/mnemonic/mnemonic.js +259 -0
- package/test/mnemonic/mocha.opts +1 -0
- package/test/mnemonic/pbkdf2.test.js +59 -0
- package/test/networks.js +159 -0
- package/test/opcode.js +161 -0
- package/test/privatekey.js +439 -0
- package/test/publickey.js +554 -0
- package/test/script/interpreter.js +734 -0
- package/test/script/script.js +1437 -0
- package/test/transaction/deserialize.js +34 -0
- package/test/transaction/input/input.js +90 -0
- package/test/transaction/input/multisig.js +90 -0
- package/test/transaction/input/publickey.js +68 -0
- package/test/transaction/input/publickeyhash.js +51 -0
- package/test/transaction/output.js +185 -0
- package/test/transaction/sighash.js +65 -0
- package/test/transaction/signature.js +114 -0
- package/test/transaction/transaction.js +1109 -0
- package/test/transaction/unspentoutput.js +110 -0
- package/test/util/js.js +76 -0
- package/test/util/preconditions.js +79 -0
package/lib/publickey.js
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var BN = require('./crypto/bn');
|
|
4
|
+
var Point = require('./crypto/point');
|
|
5
|
+
var Hash = require('./crypto/hash');
|
|
6
|
+
var JSUtil = require('./util/js');
|
|
7
|
+
var Network = require('./networks');
|
|
8
|
+
var _ = require('./util/_');
|
|
9
|
+
var $ = require('./util/preconditions');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Instantiate a PublicKey from a {@link PrivateKey}, {@link Point}, `string`, or `Buffer`.
|
|
13
|
+
*
|
|
14
|
+
* There are two internal properties, `network` and `compressed`, that deal with importing
|
|
15
|
+
* a PublicKey from a PrivateKey in WIF format. More details described on {@link PrivateKey}
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```javascript
|
|
19
|
+
* // instantiate from a private key
|
|
20
|
+
* var key = PublicKey(privateKey, true);
|
|
21
|
+
*
|
|
22
|
+
* // export to as a DER hex encoded string
|
|
23
|
+
* var exported = key.toString();
|
|
24
|
+
*
|
|
25
|
+
* // import the public key
|
|
26
|
+
* var imported = PublicKey.fromString(exported);
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @param {string} data - The encoded data in various formats
|
|
30
|
+
* @param {Object} extra - additional options
|
|
31
|
+
* @param {Network=} extra.network - Which network should the address for this public key be for
|
|
32
|
+
* @param {String=} extra.compressed - If the public key is compressed
|
|
33
|
+
* @returns {PublicKey} A new valid instance of an PublicKey
|
|
34
|
+
* @constructor
|
|
35
|
+
*/
|
|
36
|
+
function PublicKey(data, extra) {
|
|
37
|
+
if (!(this instanceof PublicKey)) {
|
|
38
|
+
return new PublicKey(data, extra);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
$.checkArgument(data, 'First argument is required, please include public key data.');
|
|
42
|
+
|
|
43
|
+
if (data instanceof PublicKey) {
|
|
44
|
+
// Return copy, but as it's an immutable object, return same argument
|
|
45
|
+
return data;
|
|
46
|
+
}
|
|
47
|
+
extra = extra || {};
|
|
48
|
+
|
|
49
|
+
var info = this._classifyArgs(data, extra);
|
|
50
|
+
|
|
51
|
+
// validation
|
|
52
|
+
info.point.validate();
|
|
53
|
+
|
|
54
|
+
JSUtil.defineImmutable(this, {
|
|
55
|
+
point: info.point,
|
|
56
|
+
compressed: info.compressed,
|
|
57
|
+
network: info.network || Network.defaultNetwork,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Internal function to differentiate between arguments passed to the constructor
|
|
65
|
+
* @param {*} data
|
|
66
|
+
* @param {Object} extra
|
|
67
|
+
*/
|
|
68
|
+
PublicKey.prototype._classifyArgs = function (data, extra) {
|
|
69
|
+
var info = {
|
|
70
|
+
compressed: _.isUndefined(extra.compressed) || extra.compressed,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// detect type of data
|
|
74
|
+
if (data instanceof Point) {
|
|
75
|
+
info.point = data;
|
|
76
|
+
} else if (data.x && data.y) {
|
|
77
|
+
info = PublicKey._transformObject(data);
|
|
78
|
+
} else if (typeof data === 'string') {
|
|
79
|
+
info = PublicKey._transformDER(Buffer.from(data, 'hex'));
|
|
80
|
+
} else if (PublicKey._isBuffer(data)) {
|
|
81
|
+
info = PublicKey._transformDER(data);
|
|
82
|
+
} else if (PublicKey._isPrivateKey(data)) {
|
|
83
|
+
info = PublicKey._transformPrivateKey(data);
|
|
84
|
+
} else {
|
|
85
|
+
throw new TypeError('First argument is an unrecognized data format.');
|
|
86
|
+
}
|
|
87
|
+
if (!info.network) {
|
|
88
|
+
info.network = _.isUndefined(extra.network) ? undefined : Network.get(extra.network);
|
|
89
|
+
}
|
|
90
|
+
return info;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Internal function to detect if an object is a {@link PrivateKey}
|
|
95
|
+
*
|
|
96
|
+
* @param {*} param - object to test
|
|
97
|
+
* @returns {boolean}
|
|
98
|
+
* @private
|
|
99
|
+
*/
|
|
100
|
+
PublicKey._isPrivateKey = function (param) {
|
|
101
|
+
var PrivateKey = require('./privatekey');
|
|
102
|
+
return param instanceof PrivateKey;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Internal function to detect if an object is a Buffer
|
|
107
|
+
*
|
|
108
|
+
* @param {*} param - object to test
|
|
109
|
+
* @returns {boolean}
|
|
110
|
+
* @private
|
|
111
|
+
*/
|
|
112
|
+
PublicKey._isBuffer = function (param) {
|
|
113
|
+
return param instanceof Buffer || param instanceof Uint8Array;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Internal function to transform a private key into a public key point
|
|
118
|
+
*
|
|
119
|
+
* @param {PrivateKey} privkey - An instance of PrivateKey
|
|
120
|
+
* @returns {Object} An object with keys: point and compressed
|
|
121
|
+
* @private
|
|
122
|
+
*/
|
|
123
|
+
PublicKey._transformPrivateKey = function (privkey) {
|
|
124
|
+
$.checkArgument(PublicKey._isPrivateKey(privkey), 'Must be an instance of PrivateKey');
|
|
125
|
+
var info = {};
|
|
126
|
+
info.point = Point.getG().mul(privkey.bn);
|
|
127
|
+
info.compressed = privkey.compressed;
|
|
128
|
+
info.network = privkey.network;
|
|
129
|
+
return info;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Internal function to transform DER into a public key point
|
|
134
|
+
*
|
|
135
|
+
* @param {Buffer} buf - An DER buffer
|
|
136
|
+
* @param {bool=} strict - if set to false, will loosen some conditions
|
|
137
|
+
* @returns {Object} An object with keys: point and compressed
|
|
138
|
+
* @private
|
|
139
|
+
*/
|
|
140
|
+
PublicKey._transformDER = function (buf, strict) {
|
|
141
|
+
$.checkArgument(PublicKey._isBuffer(buf), 'Must be a buffer of DER encoded public key');
|
|
142
|
+
var info = {};
|
|
143
|
+
|
|
144
|
+
strict = _.isUndefined(strict) ? true : strict;
|
|
145
|
+
|
|
146
|
+
var x;
|
|
147
|
+
var y;
|
|
148
|
+
var xbuf;
|
|
149
|
+
var ybuf;
|
|
150
|
+
|
|
151
|
+
if (buf[0] === 0x04 || (!strict && (buf[0] === 0x06 || buf[0] === 0x07))) {
|
|
152
|
+
xbuf = buf.slice(1, 33);
|
|
153
|
+
ybuf = buf.slice(33, 65);
|
|
154
|
+
if (xbuf.length !== 32 || ybuf.length !== 32 || buf.length !== 65) {
|
|
155
|
+
throw new TypeError('Length of x and y must be 32 bytes');
|
|
156
|
+
}
|
|
157
|
+
x = new BN(xbuf);
|
|
158
|
+
y = new BN(ybuf);
|
|
159
|
+
info.point = new Point(x, y);
|
|
160
|
+
info.compressed = false;
|
|
161
|
+
} else if (buf[0] === 0x03) {
|
|
162
|
+
xbuf = buf.slice(1);
|
|
163
|
+
x = new BN(xbuf);
|
|
164
|
+
info = PublicKey._transformX(true, x);
|
|
165
|
+
info.compressed = true;
|
|
166
|
+
} else if (buf[0] === 0x02) {
|
|
167
|
+
xbuf = buf.slice(1);
|
|
168
|
+
x = new BN(xbuf);
|
|
169
|
+
info = PublicKey._transformX(false, x);
|
|
170
|
+
info.compressed = true;
|
|
171
|
+
} else {
|
|
172
|
+
throw new TypeError('Invalid DER format public key');
|
|
173
|
+
}
|
|
174
|
+
return info;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Internal function to transform X into a public key point
|
|
179
|
+
*
|
|
180
|
+
* @param {Boolean} odd - If the point is above or below the x axis
|
|
181
|
+
* @param {Point} x - The x point
|
|
182
|
+
* @returns {Object} An object with keys: point and compressed
|
|
183
|
+
* @private
|
|
184
|
+
*/
|
|
185
|
+
PublicKey._transformX = function (odd, x) {
|
|
186
|
+
$.checkArgument(typeof odd === 'boolean', 'Must specify whether y is odd or not (true or false)');
|
|
187
|
+
var info = {};
|
|
188
|
+
info.point = Point.fromX(odd, x);
|
|
189
|
+
return info;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Internal function to transform a JSON into a public key point
|
|
194
|
+
*
|
|
195
|
+
* @param {String|Object} json - a JSON string or plain object
|
|
196
|
+
* @returns {Object} An object with keys: point and compressed
|
|
197
|
+
* @private
|
|
198
|
+
*/
|
|
199
|
+
PublicKey._transformObject = function (json) {
|
|
200
|
+
var x = new BN(json.x, 'hex');
|
|
201
|
+
var y = new BN(json.y, 'hex');
|
|
202
|
+
var point = new Point(x, y);
|
|
203
|
+
return new PublicKey(point, {
|
|
204
|
+
compressed: json.compressed,
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Instantiate a PublicKey from a PrivateKey
|
|
210
|
+
*
|
|
211
|
+
* @param {PrivateKey} privkey - An instance of PrivateKey
|
|
212
|
+
* @returns {PublicKey} A new valid instance of PublicKey
|
|
213
|
+
*/
|
|
214
|
+
PublicKey.fromPrivateKey = function (privkey) {
|
|
215
|
+
$.checkArgument(PublicKey._isPrivateKey(privkey), 'Must be an instance of PrivateKey');
|
|
216
|
+
var info = PublicKey._transformPrivateKey(privkey);
|
|
217
|
+
return new PublicKey(info.point, {
|
|
218
|
+
compressed: info.compressed,
|
|
219
|
+
network: info.network,
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Instantiate a PublicKey from a Buffer
|
|
225
|
+
* @param {Buffer} buf - A DER buffer
|
|
226
|
+
* @param {bool=} strict - if set to false, will loosen some conditions
|
|
227
|
+
* @returns {PublicKey} A new valid instance of PublicKey
|
|
228
|
+
*/
|
|
229
|
+
PublicKey.fromDER = PublicKey.fromBuffer = function (buf, strict) {
|
|
230
|
+
$.checkArgument(PublicKey._isBuffer(buf), 'Must be a buffer of DER encoded public key');
|
|
231
|
+
var info = PublicKey._transformDER(buf, strict);
|
|
232
|
+
return new PublicKey(info.point, {
|
|
233
|
+
compressed: info.compressed,
|
|
234
|
+
});
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Instantiate a PublicKey from a Point
|
|
239
|
+
*
|
|
240
|
+
* @param {Point} point - A Point instance
|
|
241
|
+
* @param {boolean=} compressed - whether to store this public key as compressed format
|
|
242
|
+
* @returns {PublicKey} A new valid instance of PublicKey
|
|
243
|
+
*/
|
|
244
|
+
PublicKey.fromPoint = function (point, compressed) {
|
|
245
|
+
$.checkArgument(point instanceof Point, 'First argument must be an instance of Point.');
|
|
246
|
+
return new PublicKey(point, {
|
|
247
|
+
compressed: compressed,
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Instantiate a PublicKey from a DER hex encoded string
|
|
253
|
+
*
|
|
254
|
+
* @param {string} str - A DER hex string
|
|
255
|
+
* @param {String=} encoding - The type of string encoding
|
|
256
|
+
* @returns {PublicKey} A new valid instance of PublicKey
|
|
257
|
+
*/
|
|
258
|
+
PublicKey.fromHex = PublicKey.fromString = function (str, encoding) {
|
|
259
|
+
var buf = Buffer.from(str, encoding || 'hex');
|
|
260
|
+
var info = PublicKey._transformDER(buf);
|
|
261
|
+
return new PublicKey(info.point, {
|
|
262
|
+
compressed: info.compressed,
|
|
263
|
+
});
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Instantiate a PublicKey from an X Point
|
|
268
|
+
*
|
|
269
|
+
* @param {Boolean} odd - If the point is above or below the x axis
|
|
270
|
+
* @param {Point} x - The x point
|
|
271
|
+
* @returns {PublicKey} A new valid instance of PublicKey
|
|
272
|
+
*/
|
|
273
|
+
PublicKey.fromX = function (odd, x) {
|
|
274
|
+
var info = PublicKey._transformX(odd, x);
|
|
275
|
+
return new PublicKey(info.point, {
|
|
276
|
+
compressed: info.compressed,
|
|
277
|
+
});
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Check if there would be any errors when initializing a PublicKey
|
|
282
|
+
*
|
|
283
|
+
* @param {string} data - The encoded data in various formats
|
|
284
|
+
* @returns {null|Error} An error if exists
|
|
285
|
+
*/
|
|
286
|
+
PublicKey.getValidationError = function (data) {
|
|
287
|
+
var error;
|
|
288
|
+
try {
|
|
289
|
+
new PublicKey(data);
|
|
290
|
+
} catch (e) {
|
|
291
|
+
error = e;
|
|
292
|
+
}
|
|
293
|
+
return error;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Check if the parameters are valid
|
|
298
|
+
*
|
|
299
|
+
* @param {string} data - The encoded data in various formats
|
|
300
|
+
* @returns {Boolean} If the public key would be valid
|
|
301
|
+
*/
|
|
302
|
+
PublicKey.isValid = function (data) {
|
|
303
|
+
return !PublicKey.getValidationError(data);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* @returns {Object} A plain object of the PublicKey
|
|
308
|
+
*/
|
|
309
|
+
PublicKey.prototype.toObject = PublicKey.prototype.toJSON = function toObject() {
|
|
310
|
+
return {
|
|
311
|
+
x: this.point.getX().toString('hex', 2),
|
|
312
|
+
y: this.point.getY().toString('hex', 2),
|
|
313
|
+
compressed: this.compressed,
|
|
314
|
+
};
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Will output the PublicKey to a DER Buffer
|
|
319
|
+
*
|
|
320
|
+
* @returns {Buffer} A DER hex encoded buffer
|
|
321
|
+
*/
|
|
322
|
+
PublicKey.prototype.toBuffer = PublicKey.prototype.toDER = function () {
|
|
323
|
+
var x = this.point.getX();
|
|
324
|
+
var y = this.point.getY();
|
|
325
|
+
|
|
326
|
+
var xbuf = x.toBuffer({
|
|
327
|
+
size: 32,
|
|
328
|
+
});
|
|
329
|
+
var ybuf = y.toBuffer({
|
|
330
|
+
size: 32,
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
var prefix;
|
|
334
|
+
if (!this.compressed) {
|
|
335
|
+
prefix = Buffer.from([0x04]);
|
|
336
|
+
return Buffer.concat([prefix, xbuf, ybuf]);
|
|
337
|
+
} else {
|
|
338
|
+
var odd = ybuf[ybuf.length - 1] % 2;
|
|
339
|
+
if (odd) {
|
|
340
|
+
prefix = Buffer.from([0x03]);
|
|
341
|
+
} else {
|
|
342
|
+
prefix = Buffer.from([0x02]);
|
|
343
|
+
}
|
|
344
|
+
return Buffer.concat([prefix, xbuf]);
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Will return a sha256 + ripemd160 hash of the serialized public key
|
|
350
|
+
* @see https://github.com/bitcoin/bitcoin/blob/master/src/pubkey.h#L141
|
|
351
|
+
* @returns {Buffer}
|
|
352
|
+
*/
|
|
353
|
+
PublicKey.prototype._getID = function _getID() {
|
|
354
|
+
return Hash.sha256ripemd160(this.toBuffer());
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Will return an address for the public key
|
|
359
|
+
*
|
|
360
|
+
* @param {String|Network=} network - Which network should the address be for
|
|
361
|
+
* @returns {Address} An address generated from the public key
|
|
362
|
+
*/
|
|
363
|
+
PublicKey.prototype.toAddress = function (network) {
|
|
364
|
+
var Address = require('./address');
|
|
365
|
+
return Address.fromPublicKey(this, network || this.network);
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Will output the PublicKey to a DER encoded hex string
|
|
370
|
+
*
|
|
371
|
+
* @returns {string} A DER hex encoded string
|
|
372
|
+
*/
|
|
373
|
+
PublicKey.prototype.toString = PublicKey.prototype.toHex = function () {
|
|
374
|
+
return this.toDER().toString('hex');
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Will return a string formatted for the console
|
|
379
|
+
*
|
|
380
|
+
* @returns {string} Public key
|
|
381
|
+
*/
|
|
382
|
+
PublicKey.prototype.inspect = function () {
|
|
383
|
+
return '<PublicKey: ' + this.toHex() + (this.compressed ? '' : ', uncompressed') + '>';
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
module.exports = PublicKey;
|