@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,296 @@
1
+ 'use strict';
2
+
3
+ var BN = require('./bn');
4
+ var Point = require('./point');
5
+ var Signature = require('./signature');
6
+ var PublicKey = require('../publickey');
7
+ var Random = require('./random');
8
+ var Hash = require('./hash');
9
+ var BufferUtil = require('../util/buffer');
10
+ var _ = require('lodash');
11
+ var $ = require('../util/preconditions');
12
+
13
+ var ECDSA = function ECDSA(obj) {
14
+ if (!(this instanceof ECDSA)) {
15
+ return new ECDSA(obj);
16
+ }
17
+ if (obj) {
18
+ this.set(obj);
19
+ }
20
+ };
21
+
22
+ /* jshint maxcomplexity: 9 */
23
+ ECDSA.prototype.set = function(obj) {
24
+ this.hashbuf = obj.hashbuf || this.hashbuf;
25
+ this.endian = obj.endian || this.endian; //the endianness of hashbuf
26
+ this.privkey = obj.privkey || this.privkey;
27
+ this.pubkey = obj.pubkey || (this.privkey ? this.privkey.publicKey : this.pubkey);
28
+ this.sig = obj.sig || this.sig;
29
+ this.k = obj.k || this.k;
30
+ this.verified = obj.verified || this.verified;
31
+ return this;
32
+ };
33
+
34
+ ECDSA.prototype.privkey2pubkey = function() {
35
+ this.pubkey = this.privkey.toPublicKey();
36
+ };
37
+
38
+ ECDSA.prototype.calci = function() {
39
+ for (var i = 0; i < 4; i++) {
40
+ this.sig.i = i;
41
+ var Qprime;
42
+ try {
43
+ Qprime = this.toPublicKey();
44
+ } catch (e) {
45
+ console.error(e);
46
+ continue;
47
+ }
48
+
49
+ if (Qprime.point.eq(this.pubkey.point)) {
50
+ this.sig.compressed = this.pubkey.compressed;
51
+ return this;
52
+ }
53
+ }
54
+
55
+ this.sig.i = undefined;
56
+ throw new Error('Unable to find valid recovery factor');
57
+ };
58
+
59
+ ECDSA.fromString = function(str) {
60
+ var obj = JSON.parse(str);
61
+ return new ECDSA(obj);
62
+ };
63
+
64
+ ECDSA.prototype.randomK = function() {
65
+ var N = Point.getN();
66
+ var k;
67
+ do {
68
+ k = BN.fromBuffer(Random.getRandomBuffer(32));
69
+ } while (!(k.lt(N) && k.gt(BN.Zero)));
70
+ this.k = k;
71
+ return this;
72
+ };
73
+
74
+
75
+ // https://tools.ietf.org/html/rfc6979#section-3.2
76
+ ECDSA.prototype.deterministicK = function(badrs) {
77
+ /* jshint maxstatements: 25 */
78
+ // if r or s were invalid when this function was used in signing,
79
+ // we do not want to actually compute r, s here for efficiency, so,
80
+ // we can increment badrs. explained at end of RFC 6979 section 3.2
81
+ if (_.isUndefined(badrs)) {
82
+ badrs = 0;
83
+ }
84
+ var v = Buffer.alloc(32);
85
+ v.fill(0x01);
86
+ var k = Buffer.alloc(32);
87
+ k.fill(0x00);
88
+ var x = this.privkey.bn.toBuffer({
89
+ size: 32
90
+ });
91
+ var hashbuf = this.endian === 'little' ? BufferUtil.reverse(this.hashbuf) : this.hashbuf
92
+ k = Hash.sha256hmac(Buffer.concat([v, Buffer.from([0x00]), x, hashbuf]), k);
93
+ v = Hash.sha256hmac(v, k);
94
+ k = Hash.sha256hmac(Buffer.concat([v, Buffer.from([0x01]), x, hashbuf]), k);
95
+ v = Hash.sha256hmac(v, k);
96
+ v = Hash.sha256hmac(v, k);
97
+ var T = BN.fromBuffer(v);
98
+ var N = Point.getN();
99
+
100
+ // also explained in 3.2, we must ensure T is in the proper range (0, N)
101
+ for (var i = 0; i < badrs || !(T.lt(N) && T.gt(BN.Zero)); i++) {
102
+ k = Hash.sha256hmac(Buffer.concat([v, Buffer.from([0x00])]), k);
103
+ v = Hash.sha256hmac(v, k);
104
+ v = Hash.sha256hmac(v, k);
105
+ T = BN.fromBuffer(v);
106
+ }
107
+
108
+ this.k = T;
109
+ return this;
110
+ };
111
+
112
+ // Information about public key recovery:
113
+ // https://bitcointalk.org/index.php?topic=6430.0
114
+ // http://stackoverflow.com/questions/19665491/how-do-i-get-an-ecdsa-public-key-from-just-a-bitcoin-signature-sec1-4-1-6-k
115
+ ECDSA.prototype.toPublicKey = function() {
116
+ /* jshint maxstatements: 25 */
117
+ var i = this.sig.i;
118
+ $.checkArgument(i === 0 || i === 1 || i === 2 || i === 3, new Error('i must be equal to 0, 1, 2, or 3'));
119
+
120
+ var e = BN.fromBuffer(this.hashbuf);
121
+ var r = this.sig.r;
122
+ var s = this.sig.s;
123
+
124
+ // A set LSB signifies that the y-coordinate is odd
125
+ var isYOdd = i & 1;
126
+
127
+ // The more significant bit specifies whether we should use the
128
+ // first or second candidate key.
129
+ var isSecondKey = i >> 1;
130
+
131
+ var n = Point.getN();
132
+ var G = Point.getG();
133
+
134
+ // 1.1 Let x = r + jn
135
+ var x = isSecondKey ? r.add(n) : r;
136
+ var R = Point.fromX(isYOdd, x);
137
+
138
+ // 1.4 Check that nR is at infinity
139
+ var nR = R.mul(n);
140
+
141
+ if (!nR.isInfinity()) {
142
+ throw new Error('nR is not a valid curve point');
143
+ }
144
+
145
+ // Compute -e from e
146
+ var eNeg = e.neg().umod(n);
147
+
148
+ // 1.6.1 Compute Q = r^-1 (sR - eG)
149
+ // Q = r^-1 (sR + -eG)
150
+ var rInv = r.invm(n);
151
+
152
+ //var Q = R.multiplyTwo(s, G, eNeg).mul(rInv);
153
+ var Q = R.mul(s).add(G.mul(eNeg)).mul(rInv);
154
+
155
+ var pubkey = PublicKey.fromPoint(Q, this.sig.compressed);
156
+
157
+ return pubkey;
158
+ };
159
+
160
+ ECDSA.prototype.sigError = function() {
161
+ /* jshint maxstatements: 25 */
162
+ if (!BufferUtil.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) {
163
+ return 'hashbuf must be a 32 byte buffer';
164
+ }
165
+
166
+ var r = this.sig.r;
167
+ var s = this.sig.s;
168
+ if (!(r.gt(BN.Zero) && r.lt(Point.getN())) || !(s.gt(BN.Zero) && s.lt(Point.getN()))) {
169
+ return 'r and s not in range';
170
+ }
171
+
172
+ var e = BN.fromBuffer(this.hashbuf, this.endian ? {
173
+ endian: this.endian
174
+ } : undefined);
175
+ var n = Point.getN();
176
+ var sinv = s.invm(n);
177
+ var u1 = sinv.mul(e).umod(n);
178
+ var u2 = sinv.mul(r).umod(n);
179
+
180
+ var p = Point.getG().mulAdd(u1, this.pubkey.point, u2);
181
+ if (p.isInfinity()) {
182
+ return 'p is infinity';
183
+ }
184
+
185
+ if (p.getX().umod(n).cmp(r) !== 0) {
186
+ return 'Invalid signature';
187
+ } else {
188
+ return false;
189
+ }
190
+ };
191
+
192
+ ECDSA.toLowS = function(s) {
193
+ //enforce low s
194
+ //see BIP 62, "low S values in signatures"
195
+ if (s.gt(BN.fromBuffer(Buffer.from('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 'hex')))) {
196
+ s = Point.getN().sub(s);
197
+ }
198
+ return s;
199
+ };
200
+
201
+ ECDSA.prototype._findSignature = function(d, e) {
202
+ var N = Point.getN();
203
+ var G = Point.getG();
204
+ // try different values of k until r, s are valid
205
+ var badrs = 0;
206
+ var k, Q, r, s;
207
+ do {
208
+ if (!this.k || badrs > 0) {
209
+ this.deterministicK(badrs);
210
+ }
211
+ badrs++;
212
+ k = this.k;
213
+ Q = G.mul(k);
214
+ r = Q.x.umod(N);
215
+ s = k.invm(N).mul(e.add(d.mul(r))).umod(N);
216
+ } while (r.cmp(BN.Zero) <= 0 || s.cmp(BN.Zero) <= 0);
217
+
218
+ s = ECDSA.toLowS(s);
219
+ return {
220
+ s: s,
221
+ r: r
222
+ };
223
+
224
+ };
225
+
226
+ ECDSA.prototype.sign = function() {
227
+ var hashbuf = this.hashbuf;
228
+ var privkey = this.privkey;
229
+ var d = privkey.bn;
230
+
231
+ $.checkState(hashbuf && privkey && d, new Error('invalid parameters'));
232
+ $.checkState(BufferUtil.isBuffer(hashbuf) && hashbuf.length === 32, new Error('hashbuf must be a 32 byte buffer'));
233
+
234
+ var e = BN.fromBuffer(hashbuf, this.endian ? {
235
+ endian: this.endian
236
+ } : undefined);
237
+
238
+ var obj = this._findSignature(d, e);
239
+ obj.compressed = this.pubkey.compressed;
240
+
241
+ this.sig = new Signature(obj);
242
+ return this;
243
+ };
244
+
245
+ ECDSA.prototype.signRandomK = function() {
246
+ this.randomK();
247
+ return this.sign();
248
+ };
249
+
250
+ ECDSA.prototype.toString = function() {
251
+ var obj = {};
252
+ if (this.hashbuf) {
253
+ obj.hashbuf = this.hashbuf.toString('hex');
254
+ }
255
+ if (this.privkey) {
256
+ obj.privkey = this.privkey.toString();
257
+ }
258
+ if (this.pubkey) {
259
+ obj.pubkey = this.pubkey.toString();
260
+ }
261
+ if (this.sig) {
262
+ obj.sig = this.sig.toString();
263
+ }
264
+ if (this.k) {
265
+ obj.k = this.k.toString();
266
+ }
267
+ return JSON.stringify(obj);
268
+ };
269
+
270
+ ECDSA.prototype.verify = function() {
271
+ if (!this.sigError()) {
272
+ this.verified = true;
273
+ } else {
274
+ this.verified = false;
275
+ }
276
+ return this;
277
+ };
278
+
279
+ ECDSA.sign = function(hashbuf, privkey, endian) {
280
+ return ECDSA().set({
281
+ hashbuf: hashbuf,
282
+ endian: endian,
283
+ privkey: privkey
284
+ }).sign().sig;
285
+ };
286
+
287
+ ECDSA.verify = function(hashbuf, sig, pubkey, endian) {
288
+ return ECDSA().set({
289
+ hashbuf: hashbuf,
290
+ endian: endian,
291
+ sig: sig,
292
+ pubkey: pubkey
293
+ }).verify().verified;
294
+ };
295
+
296
+ module.exports = ECDSA;
@@ -0,0 +1,85 @@
1
+ 'use strict';
2
+
3
+ var crypto = require('crypto');
4
+ var BufferUtil = require('../util/buffer');
5
+ var $ = require('../util/preconditions');
6
+
7
+ var Hash = module.exports;
8
+
9
+ Hash.sha1 = function(buf) {
10
+ $.checkArgument(BufferUtil.isBuffer(buf));
11
+ return crypto.createHash('sha1').update(buf).digest();
12
+ };
13
+
14
+ Hash.sha1.blocksize = 512;
15
+
16
+ Hash.sha256 = function(buf) {
17
+ $.checkArgument(BufferUtil.isBuffer(buf));
18
+ return crypto.createHash('sha256').update(buf).digest();
19
+ };
20
+
21
+ Hash.sha256.blocksize = 512;
22
+
23
+ Hash.sha256sha256 = function(buf) {
24
+ $.checkArgument(BufferUtil.isBuffer(buf));
25
+ return Hash.sha256(Hash.sha256(buf));
26
+ };
27
+
28
+ Hash.ripemd160 = function(buf) {
29
+ $.checkArgument(BufferUtil.isBuffer(buf));
30
+ return crypto.createHash('ripemd160').update(buf).digest();
31
+ };
32
+
33
+ Hash.sha256ripemd160 = function(buf) {
34
+ $.checkArgument(BufferUtil.isBuffer(buf));
35
+ return Hash.ripemd160(Hash.sha256(buf));
36
+ };
37
+
38
+ Hash.sha512 = function(buf) {
39
+ $.checkArgument(BufferUtil.isBuffer(buf));
40
+ return crypto.createHash('sha512').update(buf).digest();
41
+ };
42
+
43
+ Hash.sha512.blocksize = 1024;
44
+
45
+ Hash.hmac = function(hashf, data, key) {
46
+ //http://en.wikipedia.org/wiki/Hash-based_message_authentication_code
47
+ //http://tools.ietf.org/html/rfc4868#section-2
48
+ $.checkArgument(BufferUtil.isBuffer(data));
49
+ $.checkArgument(BufferUtil.isBuffer(key));
50
+ $.checkArgument(hashf.blocksize);
51
+
52
+ var blocksize = hashf.blocksize / 8;
53
+
54
+ if (key.length > blocksize) {
55
+ key = hashf(key);
56
+ } else if (key < blocksize) {
57
+ var fill = Buffer.alloc(blocksize);
58
+ fill.fill(0);
59
+ key.copy(fill);
60
+ key = fill;
61
+ }
62
+
63
+ var o_key = Buffer.alloc(blocksize);
64
+ o_key.fill(0x5c);
65
+
66
+ var i_key = Buffer.alloc(blocksize);
67
+ i_key.fill(0x36);
68
+
69
+ var o_key_pad = Buffer.alloc(blocksize);
70
+ var i_key_pad = Buffer.alloc(blocksize);
71
+ for (var i = 0; i < blocksize; i++) {
72
+ o_key_pad[i] = o_key[i] ^ key[i];
73
+ i_key_pad[i] = i_key[i] ^ key[i];
74
+ }
75
+
76
+ return hashf(Buffer.concat([o_key_pad, hashf(Buffer.concat([i_key_pad, data]))]));
77
+ };
78
+
79
+ Hash.sha256hmac = function(data, key) {
80
+ return Hash.hmac(Hash.sha256, data, key);
81
+ };
82
+
83
+ Hash.sha512hmac = function(data, key) {
84
+ return Hash.hmac(Hash.sha512, data, key);
85
+ };
@@ -0,0 +1,166 @@
1
+ 'use strict';
2
+
3
+ var BN = require('./bn');
4
+ var BufferUtil = require('../util/buffer');
5
+
6
+ var EC = require('elliptic').ec;
7
+ var ec = new EC('secp256k1');
8
+ var ecPoint = ec.curve.point.bind(ec.curve);
9
+ var ecPointFromX = ec.curve.pointFromX.bind(ec.curve);
10
+
11
+ /**
12
+ *
13
+ * Instantiate a valid secp256k1 Point from the X and Y coordinates.
14
+ *
15
+ * @param {BN|String} x - The X coordinate
16
+ * @param {BN|String} y - The Y coordinate
17
+ * @link https://github.com/indutny/elliptic
18
+ * @augments elliptic.curve.point
19
+ * @throws {Error} A validation error if exists
20
+ * @returns {Point} An instance of Point
21
+ * @constructor
22
+ */
23
+ var Point = function Point(x, y, isRed) {
24
+ try {
25
+ var point = ecPoint(x, y, isRed);
26
+ } catch (e) {
27
+ throw new Error('Invalid Point');
28
+ }
29
+ point.validate();
30
+ return point;
31
+ };
32
+
33
+ Point.prototype = Object.getPrototypeOf(ec.curve.point());
34
+
35
+ /**
36
+ *
37
+ * Instantiate a valid secp256k1 Point from only the X coordinate
38
+ *
39
+ * @param {boolean} odd - If the Y coordinate is odd
40
+ * @param {BN|String} x - The X coordinate
41
+ * @throws {Error} A validation error if exists
42
+ * @returns {Point} An instance of Point
43
+ */
44
+ Point.fromX = function fromX(odd, x){
45
+ try {
46
+ var point = ecPointFromX(x, odd);
47
+ } catch (e) {
48
+ throw new Error('Invalid X');
49
+ }
50
+ point.validate();
51
+ return point;
52
+ };
53
+
54
+ /**
55
+ *
56
+ * Will return a secp256k1 ECDSA base point.
57
+ *
58
+ * @link https://en.bitcoin.it/wiki/Secp256k1
59
+ * @returns {Point} An instance of the base point.
60
+ */
61
+ Point.getG = function getG() {
62
+ return ec.curve.g;
63
+ };
64
+
65
+ /**
66
+ *
67
+ * Will return the max of range of valid private keys as governed by the secp256k1 ECDSA standard.
68
+ *
69
+ * @link https://en.bitcoin.it/wiki/Private_key#Range_of_valid_ECDSA_private_keys
70
+ * @returns {BN} A BN instance of the number of points on the curve
71
+ */
72
+ Point.getN = function getN() {
73
+ return new BN(ec.curve.n.toArray());
74
+ };
75
+
76
+ if (!Point.prototype._getX)
77
+ Point.prototype._getX = Point.prototype.getX;
78
+
79
+ /**
80
+ *
81
+ * Will return the X coordinate of the Point
82
+ *
83
+ * @returns {BN} A BN instance of the X coordinate
84
+ */
85
+ Point.prototype.getX = function getX() {
86
+ return new BN(this._getX().toArray());
87
+ };
88
+
89
+ if (!Point.prototype._getY)
90
+ Point.prototype._getY = Point.prototype.getY;
91
+
92
+ /**
93
+ *
94
+ * Will return the Y coordinate of the Point
95
+ *
96
+ * @returns {BN} A BN instance of the Y coordinate
97
+ */
98
+ Point.prototype.getY = function getY() {
99
+ return new BN(this._getY().toArray());
100
+ };
101
+
102
+ /**
103
+ *
104
+ * Will determine if the point is valid
105
+ *
106
+ * @link https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf
107
+ * @param {Point} An instance of Point
108
+ * @throws {Error} A validation error if exists
109
+ * @returns {Point} An instance of the same Point
110
+ */
111
+ Point.prototype.validate = function validate() {
112
+
113
+ if (this.isInfinity()){
114
+ throw new Error('Point cannot be equal to Infinity');
115
+ }
116
+
117
+ var p2;
118
+ try {
119
+ p2 = ecPointFromX(this.getX(), this.getY().isOdd());
120
+ } catch (e) {
121
+ throw new Error('Point does not lie on the curve');
122
+ }
123
+
124
+ if (p2.y.cmp(this.y) !== 0) {
125
+ throw new Error('Invalid y value for curve.');
126
+ }
127
+
128
+
129
+ //todo: needs test case
130
+ if (!(this.mul(Point.getN()).isInfinity())) {
131
+ throw new Error('Point times N must be infinity');
132
+ }
133
+
134
+ return this;
135
+
136
+ };
137
+
138
+ Point.pointToCompressed = function pointToCompressed(point) {
139
+ var xbuf = point.getX().toBuffer({size: 32});
140
+ var ybuf = point.getY().toBuffer({size: 32});
141
+
142
+ var prefix;
143
+ var odd = ybuf[ybuf.length - 1] % 2;
144
+ if (odd) {
145
+ prefix = Buffer.from([0x03]);
146
+ } else {
147
+ prefix = Buffer.from([0x02]);
148
+ }
149
+ return BufferUtil.concat([prefix, xbuf]);
150
+ };
151
+
152
+ // todo: needs test case
153
+ Point.prototype.hasSquare = function() {
154
+ return !this.isInfinity() && this.isSquare(this.getY());
155
+ }
156
+
157
+ // todo: needs test cases
158
+ Point.prototype.isSquare = function(x) {
159
+ let p = new BN('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 'hex');
160
+ let x0 = new BN(x);
161
+ let base = x0.toRed(BN.red(p));
162
+ let res = base.redPow(p.sub(BN.One).div(new BN(2))).fromRed(); //refactor to BN arithmetic operations
163
+ return res.eq(new BN(1));
164
+ }
165
+
166
+ module.exports = Point;
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ function Random() {
4
+ }
5
+
6
+ /* secure random bytes that sometimes throws an error due to lack of entropy */
7
+ Random.getRandomBuffer = function(size) {
8
+ if (process.browser)
9
+ return Random.getRandomBufferBrowser(size);
10
+ else
11
+ return Random.getRandomBufferNode(size);
12
+ };
13
+
14
+ Random.getRandomBufferNode = function(size) {
15
+ var crypto = require('crypto');
16
+ return crypto.randomBytes(size);
17
+ };
18
+
19
+ Random.getRandomBufferBrowser = function(size) {
20
+ if (!window.crypto && !window.msCrypto)
21
+ throw new Error('window.crypto not available');
22
+
23
+ if (window.crypto && window.crypto.getRandomValues)
24
+ var crypto = window.crypto;
25
+ else if (window.msCrypto && window.msCrypto.getRandomValues) //internet explorer
26
+ var crypto = window.msCrypto;
27
+ else
28
+ throw new Error('window.crypto.getRandomValues not available');
29
+
30
+ var bbuf = new Uint8Array(size);
31
+ crypto.getRandomValues(bbuf);
32
+ var buf = Buffer.from(bbuf);
33
+
34
+ return buf;
35
+ };
36
+
37
+ /* insecure random bytes, but it never fails */
38
+ Random.getPseudoRandomBuffer = function(size) {
39
+ var b32 = 0x100000000;
40
+ var b = Buffer.alloc(size);
41
+ var r;
42
+
43
+ for (var i = 0; i <= size; i++) {
44
+ var j = Math.floor(i / 4);
45
+ var k = i - j * 4;
46
+ if (k === 0) {
47
+ r = Math.random() * b32;
48
+ b[i] = r & 0xff;
49
+ } else {
50
+ b[i] = (r = r >>> 8) & 0xff;
51
+ }
52
+ }
53
+
54
+ return b;
55
+ };
56
+
57
+ module.exports = Random;