@onekeyfe/kaspa-core-lib 1.0.0-alpha.1

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.
Files changed (53) hide show
  1. package/LICENSE +32 -0
  2. package/README.md +59 -0
  3. package/index.d.ts +410 -0
  4. package/index.js +93 -0
  5. package/lib/address.js +792 -0
  6. package/lib/block/block.js +281 -0
  7. package/lib/block/blockheader.js +296 -0
  8. package/lib/block/index.js +4 -0
  9. package/lib/block/merkleblock.js +313 -0
  10. package/lib/crypto/bn.js +218 -0
  11. package/lib/crypto/ecdsa.js +296 -0
  12. package/lib/crypto/hash.js +85 -0
  13. package/lib/crypto/point.js +166 -0
  14. package/lib/crypto/random.js +57 -0
  15. package/lib/crypto/schnorr.js +287 -0
  16. package/lib/crypto/signature.js +441 -0
  17. package/lib/encoding/base58.js +70 -0
  18. package/lib/encoding/base58check.js +95 -0
  19. package/lib/encoding/bufferreader.js +202 -0
  20. package/lib/encoding/bufferwriter.js +155 -0
  21. package/lib/encoding/varint.js +72 -0
  22. package/lib/errors/index.js +61 -0
  23. package/lib/errors/spec.js +191 -0
  24. package/lib/hdprivatekey.js +646 -0
  25. package/lib/hdpublickey.js +496 -0
  26. package/lib/networks.js +277 -0
  27. package/lib/opcode.js +261 -0
  28. package/lib/privatekey.js +431 -0
  29. package/lib/publickey.js +375 -0
  30. package/lib/script/index.js +3 -0
  31. package/lib/script/interpreter.js +2107 -0
  32. package/lib/script/script.js +1163 -0
  33. package/lib/transaction/index.js +7 -0
  34. package/lib/transaction/input/index.js +6 -0
  35. package/lib/transaction/input/input.js +375 -0
  36. package/lib/transaction/input/multisig.js +221 -0
  37. package/lib/transaction/input/multisigscripthash.js +178 -0
  38. package/lib/transaction/input/publickey.js +91 -0
  39. package/lib/transaction/input/publickeyhash.js +100 -0
  40. package/lib/transaction/output.js +188 -0
  41. package/lib/transaction/sighash.js +534 -0
  42. package/lib/transaction/signature.js +89 -0
  43. package/lib/transaction/transaction.js +1384 -0
  44. package/lib/transaction/unspentoutput.js +109 -0
  45. package/lib/unit.js +238 -0
  46. package/lib/uri.js +223 -0
  47. package/lib/util/base32.js +63 -0
  48. package/lib/util/buffer.js +162 -0
  49. package/lib/util/convertBits.js +62 -0
  50. package/lib/util/js.js +110 -0
  51. package/lib/util/log.js +10 -0
  52. package/lib/util/preconditions.js +34 -0
  53. package/package.json +62 -0
@@ -0,0 +1,441 @@
1
+ 'use strict';
2
+
3
+ var BN = require('./bn');
4
+ var _ = require('lodash');
5
+ var $ = require('../util/preconditions');
6
+ var BufferUtil = require('../util/buffer');
7
+ var JSUtil = require('../util/js');
8
+
9
+ var Signature = function Signature(r, s, isSchnorr) {
10
+ if (!(this instanceof Signature)) {
11
+ return new Signature(r, s, isSchnorr);
12
+ }
13
+ if (r instanceof BN) {
14
+ this.set({
15
+ r: r,
16
+ s: s,
17
+ isSchnorr: isSchnorr,
18
+ });
19
+ } else if (r) {
20
+ var obj = r;
21
+ this.set(obj);
22
+ }
23
+ };
24
+
25
+ /* jshint maxcomplexity: 7 */
26
+ Signature.prototype.set = function(obj) {
27
+ this.r = obj.r || this.r || undefined;
28
+ this.s = obj.s || this.s || undefined;
29
+
30
+ this.i = typeof obj.i !== 'undefined' ? obj.i : this.i; //public key recovery parameter in range [0, 3]
31
+ this.compressed = typeof obj.compressed !== 'undefined' ?
32
+ obj.compressed : this.compressed; // whether the recovered pubkey is compressed
33
+ this.isSchnorr = obj.isSchnorr;
34
+ this.nhashtype = obj.nhashtype || this.nhashtype || undefined;
35
+ return this;
36
+ };
37
+
38
+ Signature.fromCompact = function(buf) {
39
+ $.checkArgument(BufferUtil.isBuffer(buf), 'Argument is expected to be a Buffer');
40
+
41
+ var sig = new Signature();
42
+
43
+ var compressed = true;
44
+ var i = buf.slice(0, 1)[0] - 27 - 4;
45
+ if (i < 0) {
46
+ compressed = false;
47
+ i = i + 4;
48
+ }
49
+
50
+ var b2 = buf.slice(1, 33);
51
+ var b3 = buf.slice(33, 65);
52
+
53
+ $.checkArgument(i === 0 || i === 1 || i === 2 || i === 3, new Error('i must be 0, 1, 2, or 3'));
54
+ $.checkArgument(b2.length === 32, new Error('r must be 32 bytes'));
55
+ $.checkArgument(b3.length === 32, new Error('s must be 32 bytes'));
56
+
57
+ sig.compressed = compressed;
58
+ sig.i = i;
59
+ sig.r = BN.fromBuffer(b2);
60
+ sig.s = BN.fromBuffer(b3);
61
+
62
+ return sig;
63
+ };
64
+
65
+ Signature.fromDER = Signature.fromBuffer = function(buf, strict) {
66
+ // Schnorr Signatures use 65 byte for in tx r [len] 32 , s [len] 32, nhashtype
67
+ //if((buf.length === 64 || buf.length === 65) && buf[0] != 0x30) {
68
+ let obj = Signature.parseSchnorrEncodedSig(buf);
69
+ let sig = new Signature();
70
+ sig.r = obj.r;
71
+ sig.s = obj.s;
72
+ sig.isSchnorr = true;
73
+ return sig;
74
+ //} if (buf.length === 64 && buf[0] === 0x30) {
75
+ // return "64 DER (ecdsa) signatures not allowed";
76
+ //}
77
+ /*
78
+ var obj = Signature.parseDER(buf, strict);
79
+ var sig = new Signature();
80
+
81
+ sig.r = obj.r;
82
+ sig.s = obj.s;
83
+
84
+ return sig;*/
85
+ };
86
+
87
+ // The format used in a tx
88
+ Signature.fromTxFormat = function(buf) {
89
+ var nhashtype = buf.readUInt8(buf.length - 1);
90
+ var derbuf = buf.slice(0, buf.length - 1);
91
+ var sig = new Signature.fromDER(derbuf, false);
92
+ sig.nhashtype = nhashtype;
93
+ return sig;
94
+ };
95
+
96
+
97
+ // The format used in a tx
98
+ Signature.fromDataFormat = function(buf) {
99
+ var derbuf = buf.slice(0, buf.length);
100
+ var sig = new Signature.fromDER(derbuf, false);
101
+ return sig;
102
+ };
103
+
104
+
105
+ Signature.fromString = function(str) {
106
+ var buf = Buffer.from(str, 'hex');
107
+
108
+ return Signature.fromDER(buf);
109
+ };
110
+
111
+
112
+ Signature.parseSchnorrEncodedSig = function(buf) {
113
+ let r = buf.slice(0,32);
114
+ let s = buf.slice(32, 64);
115
+ let hashtype;
116
+ if (buf.length === 65) {
117
+ hashtype = buf.slice(64,65);
118
+ this.nhashtype = hashtype;
119
+ }
120
+
121
+ var obj = {
122
+ r: BN.fromBuffer(r),
123
+ s: BN.fromBuffer(s),
124
+ nhashtype: hashtype
125
+ };
126
+
127
+ return obj;
128
+ };
129
+
130
+ /**
131
+ * In order to mimic the non-strict DER encoding of OpenSSL, set strict = false.
132
+ */
133
+ Signature.parseDER = function(buf, strict) {
134
+ $.checkArgument(BufferUtil.isBuffer(buf), new Error('DER formatted signature should be a buffer'));
135
+ if (_.isUndefined(strict)) {
136
+ strict = true;
137
+ }
138
+
139
+ var header = buf[0];
140
+ $.checkArgument(header === 0x30, new Error('Header byte should be 0x30'));
141
+
142
+ var length = buf[1];
143
+ var buflength = buf.slice(2).length;
144
+ $.checkArgument(!strict || length === buflength, new Error('Length byte should length of what follows'));
145
+
146
+ length = length < buflength ? length : buflength;
147
+
148
+ var rheader = buf[2 + 0];
149
+ $.checkArgument(rheader === 0x02, new Error('Integer byte for r should be 0x02'));
150
+
151
+ var rlength = buf[2 + 1];
152
+ var rbuf = buf.slice(2 + 2, 2 + 2 + rlength);
153
+ var r = BN.fromBuffer(rbuf);
154
+ var rneg = buf[2 + 1 + 1] === 0x00 ? true : false;
155
+ $.checkArgument(rlength === rbuf.length, new Error('Length of r incorrect'));
156
+
157
+ var sheader = buf[2 + 2 + rlength + 0];
158
+ $.checkArgument(sheader === 0x02, new Error('Integer byte for s should be 0x02'));
159
+
160
+ var slength = buf[2 + 2 + rlength + 1];
161
+ var sbuf = buf.slice(2 + 2 + rlength + 2, 2 + 2 + rlength + 2 + slength);
162
+ var s = BN.fromBuffer(sbuf);
163
+ var sneg = buf[2 + 2 + rlength + 2 + 2] === 0x00 ? true : false;
164
+ $.checkArgument(slength === sbuf.length, new Error('Length of s incorrect'));
165
+
166
+ var sumlength = 2 + 2 + rlength + 2 + slength;
167
+ $.checkArgument(length === sumlength - 2, new Error('Length of signature incorrect'));
168
+
169
+ var obj = {
170
+ header: header,
171
+ length: length,
172
+ rheader: rheader,
173
+ rlength: rlength,
174
+ rneg: rneg,
175
+ rbuf: rbuf,
176
+ r: r,
177
+ sheader: sheader,
178
+ slength: slength,
179
+ sneg: sneg,
180
+ sbuf: sbuf,
181
+ s: s
182
+ };
183
+
184
+ return obj;
185
+ };
186
+
187
+
188
+ Signature.prototype.toCompact = function(i, compressed) {
189
+ i = typeof i === 'number' ? i : this.i;
190
+ compressed = typeof compressed === 'boolean' ? compressed : this.compressed;
191
+
192
+ if (!(i === 0 || i === 1 || i === 2 || i === 3)) {
193
+ throw new Error('i must be equal to 0, 1, 2, or 3');
194
+ }
195
+
196
+ var val = i + 27 + 4;
197
+ if (compressed === false) {
198
+ val = val - 4;
199
+ }
200
+ var b1 = Buffer.from([val]);
201
+ var b2 = this.r.toBuffer({
202
+ size: 32
203
+ });
204
+ var b3 = this.s.toBuffer({
205
+ size: 32
206
+ });
207
+ return Buffer.concat([b1, b2, b3]);
208
+ };
209
+
210
+ Signature.prototype.toBuffer = Signature.prototype.toDER = function(signingMethod) {
211
+
212
+ // Schnorr signatures use a 64 byte r,s format, where as ECDSA takes the form decribed
213
+ // below, above the isDER function signature.
214
+
215
+ signingMethod = signingMethod || "ecdsa";
216
+
217
+
218
+
219
+ if(signingMethod === "schnorr") {
220
+ let rnbuf = this.r.toBuffer({size:32});
221
+ let snbuf = this.s.toBuffer({size:32});
222
+ return Buffer.concat([rnbuf, snbuf]);
223
+ }
224
+
225
+ let rnbuf = this.r.toBuffer();
226
+ let snbuf = this.s.toBuffer();
227
+
228
+
229
+ var rneg = rnbuf[0] & 0x80 ? true : false;
230
+ var sneg = snbuf[0] & 0x80 ? true : false;
231
+
232
+ var rbuf = rneg ? Buffer.concat([Buffer.from([0x00]), rnbuf]) : rnbuf;
233
+ var sbuf = sneg ? Buffer.concat([Buffer.from([0x00]), snbuf]) : snbuf;
234
+
235
+ var rlength = rbuf.length;
236
+ var slength = sbuf.length;
237
+ var length = 2 + rlength + 2 + slength;
238
+ var rheader = 0x02;
239
+ var sheader = 0x02;
240
+ var header = 0x30;
241
+
242
+ var der = Buffer.concat([Buffer.from([header, length, rheader, rlength]), rbuf, Buffer.from([sheader, slength]), sbuf]);
243
+ return der;
244
+ };
245
+
246
+ Signature.prototype.toString = function() {
247
+ var buf = this.toDER();
248
+ return buf.toString('hex');
249
+ };
250
+
251
+
252
+ Signature.isTxDER = function(buf) {
253
+ return Signature.isDER(buf.slice(0, buf.length-1));
254
+ }
255
+
256
+ /**
257
+ * This function is translated from bitcoind's IsDERSignature and is used in
258
+ * the script interpreter. This "DER" format actually includes an extra byte,
259
+ * the nhashtype, at the end. It is really the tx format, not DER format.
260
+ *
261
+ * A canonical signature exists of: [30] [total len] [02] [len R] [R] [02] [len S] [S]
262
+ * Where R and S are not negative (their first byte has its highest bit not set), and not
263
+ * excessively padded (do not start with a 0 byte, unless an otherwise negative number follows,
264
+ * in which case a single 0 byte is necessary and even required).
265
+ *
266
+ * See https://bitcointalk.org/index.php?topic=8392.msg127623#msg127623
267
+ */
268
+ Signature.isDER = function(buf) {
269
+ // Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S]
270
+ // * total-length: 1-byte length descriptor of everything that follows,
271
+ // excluding the sighash byte.
272
+ // * R-length: 1-byte length descriptor of the R value that follows.
273
+ // * R: arbitrary-length big-endian encoded R value. It must use the
274
+ // shortest possible encoding for a positive integers (which means no null
275
+ // bytes at the start, except a single one when the next byte has its
276
+ // highest bit set).
277
+ // * S-length: 1-byte length descriptor of the S value that follows.
278
+ // * S: arbitrary-length big-endian encoded S value. The same rules apply.
279
+
280
+ // Minimum and maximum size constraints.
281
+ if (buf.length < 8 || buf.length > 72) {
282
+ return false;
283
+ }
284
+
285
+ //
286
+ // Check that the signature is a compound structure of proper size.
287
+ //
288
+
289
+ // A signature is of type 0x30 (compound).
290
+ if (buf[0] != 0x30) {
291
+ return false;
292
+ }
293
+
294
+ // Make sure the length covers the entire signature.
295
+ // Remove:
296
+ // * 1 byte for the coupound type.
297
+ // * 1 byte for the length of the signature.
298
+ if (buf[1] != buf.length - 2) {
299
+ return false;
300
+ }
301
+
302
+ //
303
+ // Check that R is an positive integer of sensible size.
304
+ //
305
+
306
+ // Check whether the R element is an integer.
307
+ if (buf[2] != 0x02) {
308
+ return false;
309
+ }
310
+
311
+ // Extract the length of the R element.
312
+ var lenR = buf[3];
313
+
314
+ // Zero-length integers are not allowed for R.
315
+ if (lenR == 0) {
316
+ return false;
317
+ }
318
+
319
+ // Negative numbers are not allowed for R.
320
+ if (buf[4] & 0x80) {
321
+ return false;
322
+ }
323
+
324
+ // Make sure the length of the R element is consistent with the signature
325
+ // size.
326
+ // Remove:
327
+ // * 1 byte for the coumpound type.
328
+ // * 1 byte for the length of the signature.
329
+ // * 2 bytes for the integer type of R and S.
330
+ // * 2 bytes for the size of R and S.
331
+ // * 1 byte for S itself.
332
+ if (lenR > (buf.length - 7)) {
333
+ return false;
334
+ }
335
+
336
+ // Null bytes at the start of R are not allowed, unless R would otherwise be
337
+ // interpreted as a negative number.
338
+ //
339
+ // /!\ This check can only be performed after we checked that lenR is
340
+ // consistent with the size of the signature or we risk to access out of
341
+ // bound elements.
342
+ if (lenR > 1 && (buf[4] == 0x00) && !(buf[5] & 0x80)) {
343
+ return false;
344
+ }
345
+
346
+ //
347
+ // Check that S is an positive integer of sensible size.
348
+ //
349
+
350
+ // S's definition starts after R's definition:
351
+ // * 1 byte for the coumpound type.
352
+ // * 1 byte for the length of the signature.
353
+ // * 1 byte for the size of R.
354
+ // * lenR bytes for R itself.
355
+ // * 1 byte to get to S.
356
+ var startS = lenR + 4;
357
+
358
+ // Check whether the S element is an integer.
359
+ if (buf[startS] != 0x02) {
360
+ return false;
361
+ }
362
+
363
+ // Extract the length of the S element.
364
+ var lenS = buf[startS + 1];
365
+
366
+ // Zero-length integers are not allowed for S.
367
+ if (lenS == 0) {
368
+ return false;
369
+ }
370
+
371
+ // Negative numbers are not allowed for S.
372
+ if (buf[startS + 2] & 0x80) {
373
+ return false;
374
+ }
375
+
376
+ // Verify that the length of S is consistent with the size of the signature
377
+ // including metadatas:
378
+ // * 1 byte for the integer type of S.
379
+ // * 1 byte for the size of S.
380
+ if (startS + lenS + 2 != buf.length) {
381
+ return false;
382
+ }
383
+
384
+ // Null bytes at the start of S are not allowed, unless S would otherwise be
385
+ // interpreted as a negative number.
386
+ //
387
+ // /!\ This check can only be performed after we checked that lenR and lenS
388
+ // are consistent with the size of the signature or we risk to access
389
+ // out of bound elements.
390
+ if (lenS > 1 && (buf[startS + 2] == 0x00) && !(buf[startS + 3] & 0x80)) {
391
+ return false;
392
+ }
393
+
394
+ return true;
395
+ };
396
+
397
+ /**
398
+ * Compares to bitcoind's IsLowDERSignature
399
+ * See also ECDSA signature algorithm which enforces this.
400
+ * See also BIP 62, "low S values in signatures"
401
+ */
402
+ Signature.prototype.hasLowS = function() {
403
+ if (this.s.lt(new BN(1)) ||
404
+ this.s.gt(new BN('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 'hex'))) {
405
+ return false;
406
+ }
407
+ return true;
408
+ };
409
+
410
+ /**
411
+ * @returns true if the nhashtype is exactly equal to one of the standard options or combinations thereof.
412
+ * Translated from bitcoind's IsDefinedHashtypeSignature
413
+ */
414
+ Signature.prototype.hasDefinedHashtype = function() {
415
+ if (!JSUtil.isNaturalNumber(this.nhashtype)) {
416
+ return false;
417
+ }
418
+ // accept with or without Signature.SIGHASH_ANYONECANPAY by ignoring the bit
419
+ // base mask was 1F
420
+ var mask = ~(Signature.SIGHASH_FORKID | Signature.SIGHASH_ANYONECANPAY) >>>0;
421
+ var temp = this.nhashtype & mask;
422
+ if (temp < Signature.SIGHASH_ALL || temp > Signature.SIGHASH_SINGLE) {
423
+ return false;
424
+ }
425
+ return true;
426
+ };
427
+
428
+ Signature.prototype.toTxFormat = function(signingMethod) {
429
+ var derbuf = this.toDER(signingMethod);
430
+ var buf = Buffer.alloc(1);
431
+ buf.writeUInt8(this.nhashtype, 0);
432
+ return Buffer.concat([derbuf, buf]);
433
+ };
434
+
435
+ Signature.SIGHASH_ALL = 0x01;
436
+ Signature.SIGHASH_NONE = 0x02;
437
+ Signature.SIGHASH_SINGLE = 0x03;
438
+ Signature.SIGHASH_FORKID = 0x40;
439
+ Signature.SIGHASH_ANYONECANPAY = 0x80;
440
+
441
+ module.exports = Signature;
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ var _ = require('lodash');
4
+ var bs58 = require('bs58');
5
+ var buffer = require('buffer');
6
+
7
+ var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.split('');
8
+
9
+ var Base58 = function Base58(obj) {
10
+ /* jshint maxcomplexity: 8 */
11
+ if (!(this instanceof Base58)) {
12
+ return new Base58(obj);
13
+ }
14
+ if (Buffer.isBuffer(obj)) {
15
+ var buf = obj;
16
+ this.fromBuffer(buf);
17
+ } else if (typeof obj === 'string') {
18
+ var str = obj;
19
+ this.fromString(str);
20
+ } else if (obj) {
21
+ this.set(obj);
22
+ }
23
+ };
24
+
25
+ Base58.validCharacters = function validCharacters(chars) {
26
+ if (buffer.Buffer.isBuffer(chars)) {
27
+ chars = chars.toString();
28
+ }
29
+ return _.every(_.map(chars, function(char) { return _.includes(ALPHABET, char); }));
30
+ };
31
+
32
+ Base58.prototype.set = function(obj) {
33
+ this.buf = obj.buf || this.buf || undefined;
34
+ return this;
35
+ };
36
+
37
+ Base58.encode = function(buf) {
38
+ if (!buffer.Buffer.isBuffer(buf)) {
39
+ throw new Error('Input should be a buffer');
40
+ }
41
+ return bs58.encode(buf);
42
+ };
43
+
44
+ Base58.decode = function(str) {
45
+ if (typeof str !== 'string') {
46
+ throw new Error('Input should be a string');
47
+ }
48
+ return Buffer.from(bs58.decode(str));
49
+ };
50
+
51
+ Base58.prototype.fromBuffer = function(buf) {
52
+ this.buf = buf;
53
+ return this;
54
+ };
55
+
56
+ Base58.prototype.fromString = function(str) {
57
+ var buf = Base58.decode(str);
58
+ this.buf = buf;
59
+ return this;
60
+ };
61
+
62
+ Base58.prototype.toBuffer = function() {
63
+ return this.buf;
64
+ };
65
+
66
+ Base58.prototype.toString = function() {
67
+ return Base58.encode(this.buf);
68
+ };
69
+
70
+ module.exports = Base58;
@@ -0,0 +1,95 @@
1
+ 'use strict';
2
+
3
+ var _ = require('lodash');
4
+ var Base58 = require('./base58');
5
+ var buffer = require('buffer');
6
+ var sha256sha256 = require('../crypto/hash').sha256sha256;
7
+
8
+ var Base58Check = function Base58Check(obj) {
9
+ if (!(this instanceof Base58Check))
10
+ return new Base58Check(obj);
11
+ if (Buffer.isBuffer(obj)) {
12
+ var buf = obj;
13
+ this.fromBuffer(buf);
14
+ } else if (typeof obj === 'string') {
15
+ var str = obj;
16
+ this.fromString(str);
17
+ } else if (obj) {
18
+ this.set(obj);
19
+ }
20
+ };
21
+
22
+ Base58Check.prototype.set = function(obj) {
23
+ this.buf = obj.buf || this.buf || undefined;
24
+ return this;
25
+ };
26
+
27
+ Base58Check.validChecksum = function validChecksum(data, checksum) {
28
+ if (_.isString(data)) {
29
+ data = Buffer.from(Base58.decode(data));
30
+ }
31
+ if (_.isString(checksum)) {
32
+ checksum = Buffer.from(Base58.decode(checksum));
33
+ }
34
+ if (!checksum) {
35
+ checksum = data.slice(-4);
36
+ data = data.slice(0, -4);
37
+ }
38
+ return Base58Check.checksum(data).toString('hex') === checksum.toString('hex');
39
+ };
40
+
41
+ Base58Check.decode = function(s) {
42
+ if (typeof s !== 'string')
43
+ throw new Error('Input must be a string');
44
+
45
+ var buf = Buffer.from(Base58.decode(s));
46
+
47
+ if (buf.length < 4)
48
+ throw new Error("Input string too short");
49
+
50
+ var data = buf.slice(0, -4);
51
+ var csum = buf.slice(-4);
52
+
53
+ var hash = sha256sha256(data);
54
+ var hash4 = hash.slice(0, 4);
55
+
56
+ if (csum.toString('hex') !== hash4.toString('hex'))
57
+ throw new Error("Checksum mismatch");
58
+
59
+ return data;
60
+ };
61
+
62
+ Base58Check.checksum = function(buffer) {
63
+ return sha256sha256(buffer).slice(0, 4);
64
+ };
65
+
66
+ Base58Check.encode = function(buf) {
67
+ if (!Buffer.isBuffer(buf))
68
+ throw new Error('Input must be a buffer');
69
+ var checkedBuf = Buffer.alloc(buf.length + 4);
70
+ var hash = Base58Check.checksum(buf);
71
+ buf.copy(checkedBuf);
72
+ hash.copy(checkedBuf, buf.length);
73
+ return Base58.encode(checkedBuf);
74
+ };
75
+
76
+ Base58Check.prototype.fromBuffer = function(buf) {
77
+ this.buf = buf;
78
+ return this;
79
+ };
80
+
81
+ Base58Check.prototype.fromString = function(str) {
82
+ var buf = Base58Check.decode(str);
83
+ this.buf = buf;
84
+ return this;
85
+ };
86
+
87
+ Base58Check.prototype.toBuffer = function() {
88
+ return this.buf;
89
+ };
90
+
91
+ Base58Check.prototype.toString = function() {
92
+ return Base58Check.encode(this.buf);
93
+ };
94
+
95
+ module.exports = Base58Check;