@steemit/steem-js 0.7.11 → 0.8.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/README.md +22 -3
- package/circle.yml +1 -1
- package/config.json +1 -1
- package/dist/steem-tests.min.js +4097 -23
- package/dist/steem.min.js +2089 -18
- package/docker-webpack.config.js +44 -0
- package/lib/api/index.js +305 -412
- package/lib/api/methods.js +16 -1
- package/lib/api/rpc-auth.js +135 -0
- package/lib/api/transports/base.js +25 -66
- package/lib/api/transports/http.js +114 -129
- package/lib/api/transports/index.js +8 -15
- package/lib/api/transports/ws.js +107 -207
- package/lib/auth/ecc/index.js +9 -9
- package/lib/auth/ecc/src/address.js +48 -78
- package/lib/auth/ecc/src/aes.js +93 -129
- package/lib/auth/ecc/src/brain_key.js +7 -7
- package/lib/auth/ecc/src/ecdsa.js +7 -33
- package/lib/auth/ecc/src/ecsignature.js +4 -30
- package/lib/auth/ecc/src/enforce_types.js +1 -8
- package/lib/auth/ecc/src/hash.js +16 -25
- package/lib/auth/ecc/src/key_private.js +146 -199
- package/lib/auth/ecc/src/key_public.js +130 -202
- package/lib/auth/ecc/src/key_utils.js +64 -106
- package/lib/auth/ecc/src/signature.js +125 -177
- package/lib/auth/index.js +84 -97
- package/lib/auth/memo.js +90 -118
- package/lib/auth/serializer/index.js +12 -18
- package/lib/auth/serializer/src/ChainTypes.js +0 -3
- package/lib/auth/serializer/src/convert.js +29 -32
- package/lib/auth/serializer/src/error_with_cause.js +22 -37
- package/lib/auth/serializer/src/fast_parser.js +54 -74
- package/lib/auth/serializer/src/number_utils.js +30 -54
- package/lib/auth/serializer/src/object_id.js +37 -62
- package/lib/auth/serializer/src/operations.js +597 -689
- package/lib/auth/serializer/src/precision.js +55 -73
- package/lib/auth/serializer/src/serializer.js +158 -204
- package/lib/auth/serializer/src/template.js +13 -8
- package/lib/auth/serializer/src/types.js +949 -1102
- package/lib/auth/serializer/src/validation.js +268 -328
- package/lib/broadcast/helpers.js +61 -98
- package/lib/broadcast/index.js +61 -82
- package/lib/browser.js +15 -19
- package/lib/config.js +16 -38
- package/lib/formatter.js +89 -115
- package/lib/index.js +19 -17
- package/lib/utils.js +4 -9
- package/node-18.dockerfile +28 -0
- package/package.json +62 -38
- package/test/Crypto.js +16 -16
- package/test/KeyFormats.js +1 -1
- package/test/api.test.js +37 -0
- package/test/broadcast.test.js +14 -8
- package/test/comment.test.js +17 -3
- package/test/operations_test.js +1 -1
- package/test/promise-broadcast.test.js +86 -0
- package/test/reputation.test.js +68 -0
- package/test/smt.test.js +10 -10
- package/test-github-workflow.bat +19 -0
- package/test-github-workflow.sh +15 -0
- package/webpack/makeConfig.js +25 -17
- package/.circleci/config.yml +0 -23
- package/dist/statistics.html +0 -208
- package/dist/steem-tests.min.js.gz +0 -0
- package/dist/steem-tests.min.js.map +0 -1
- package/dist/steem.min.js.gz +0 -0
- package/dist/steem.min.js.map +0 -1
- package/lib/auth/ecc/README.md +0 -20
- package/lib/auth/ecc/package.json +0 -36
- package/lib/auth/serializer/README.md +0 -13
- package/lib/auth/serializer/package.json +0 -32
- package/node-4.dockerfile +0 -6
- package/node-6.dockerfile +0 -6
- package/yarn.lock +0 -3336
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
4
|
-
|
|
5
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
1
|
+
"use strict";
|
|
6
2
|
|
|
7
3
|
var BigInteger = require('bigi');
|
|
8
4
|
var ecurve = require('ecurve');
|
|
@@ -12,204 +8,136 @@ var base58 = require('bs58');
|
|
|
12
8
|
var hash = require('./hash');
|
|
13
9
|
var config = require('../../../config');
|
|
14
10
|
var assert = require('assert');
|
|
15
|
-
|
|
16
11
|
var G = secp256k1.G;
|
|
17
12
|
var n = secp256k1.n;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
13
|
+
class PublicKey {
|
|
14
|
+
/** @param {ecurve.Point} public key */
|
|
15
|
+
constructor(Q) {
|
|
16
|
+
this.Q = Q;
|
|
17
|
+
}
|
|
18
|
+
static fromBinary(bin) {
|
|
19
|
+
return PublicKey.fromBuffer(new Buffer.from(bin, 'binary'));
|
|
20
|
+
}
|
|
21
|
+
static fromBuffer(buffer) {
|
|
22
|
+
if (buffer.toString("hex") === "000000000000000000000000000000000000000000000000000000000000000000") return new PublicKey(null);
|
|
23
|
+
return new PublicKey(ecurve.Point.decodeFrom(secp256k1, buffer));
|
|
24
|
+
}
|
|
25
|
+
toBuffer(compressed = this.Q ? this.Q.compressed : null) {
|
|
26
|
+
if (this.Q === null) return Buffer.from("000000000000000000000000000000000000000000000000000000000000000000", "hex");
|
|
27
|
+
return this.Q.getEncoded(compressed);
|
|
28
|
+
}
|
|
29
|
+
static fromPoint(point) {
|
|
30
|
+
return new PublicKey(point);
|
|
31
|
+
}
|
|
32
|
+
toUncompressed() {
|
|
33
|
+
var buf = this.Q.getEncoded(false);
|
|
34
|
+
var point = ecurve.Point.decodeFrom(secp256k1, buf);
|
|
35
|
+
return PublicKey.fromPoint(point);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** bts::blockchain::address (unique but not a full public key) */
|
|
39
|
+
toBlockchainAddress() {
|
|
40
|
+
var pub_buf = this.toBuffer();
|
|
41
|
+
var pub_sha = hash.sha512(pub_buf);
|
|
42
|
+
return hash.ripemd160(pub_sha);
|
|
43
|
+
}
|
|
44
|
+
toString(address_prefix = config.get('address_prefix')) {
|
|
45
|
+
return this.toPublicKeyString(address_prefix);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
Full public key
|
|
50
|
+
{return} string
|
|
51
|
+
*/
|
|
52
|
+
toPublicKeyString(address_prefix = config.get('address_prefix')) {
|
|
53
|
+
if (this.pubdata) return address_prefix + this.pubdata;
|
|
54
|
+
const pub_buf = this.toBuffer();
|
|
55
|
+
const checksum = hash.ripemd160(pub_buf);
|
|
56
|
+
const addy = Buffer.concat([pub_buf, checksum.slice(0, 4)]);
|
|
57
|
+
this.pubdata = base58.encode(addy);
|
|
58
|
+
return address_prefix + this.pubdata;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
@arg {string} public_key - like STMXyz...
|
|
63
|
+
@arg {string} address_prefix - like STM
|
|
64
|
+
@return PublicKey or `null` (if the public_key string is invalid)
|
|
65
|
+
@deprecated fromPublicKeyString (use fromString instead)
|
|
66
|
+
*/
|
|
67
|
+
static fromString(public_key, address_prefix = config.get('address_prefix')) {
|
|
68
|
+
try {
|
|
69
|
+
return PublicKey.fromStringOrThrow(public_key, address_prefix);
|
|
70
|
+
} catch (e) {
|
|
71
|
+
return null;
|
|
26
72
|
}
|
|
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
|
-
key: 'toPtsAddy',
|
|
100
|
-
value: function toPtsAddy() {
|
|
101
|
-
var pub_buf = this.toBuffer();
|
|
102
|
-
var pub_sha = hash.sha256(pub_buf);
|
|
103
|
-
var addy = hash.ripemd160(pub_sha);
|
|
104
|
-
addy = Buffer.concat([new Buffer([0x38]), addy]); //version 56(decimal)
|
|
105
|
-
|
|
106
|
-
var checksum = hash.sha256(addy);
|
|
107
|
-
checksum = hash.sha256(checksum);
|
|
108
|
-
|
|
109
|
-
addy = Buffer.concat([addy, checksum.slice(0, 4)]);
|
|
110
|
-
return base58.encode(addy);
|
|
111
|
-
}
|
|
112
|
-
}, {
|
|
113
|
-
key: 'child',
|
|
114
|
-
value: function child(offset) {
|
|
115
|
-
|
|
116
|
-
assert(Buffer.isBuffer(offset), "Buffer required: offset");
|
|
117
|
-
assert.equal(offset.length, 32, "offset length");
|
|
118
|
-
|
|
119
|
-
offset = Buffer.concat([this.toBuffer(), offset]);
|
|
120
|
-
offset = hash.sha256(offset);
|
|
121
|
-
|
|
122
|
-
var c = BigInteger.fromBuffer(offset);
|
|
123
|
-
|
|
124
|
-
if (c.compareTo(n) >= 0) throw new Error("Child offset went out of bounds, try again");
|
|
125
|
-
|
|
126
|
-
var cG = G.multiply(c);
|
|
127
|
-
var Qprime = this.Q.add(cG);
|
|
128
|
-
|
|
129
|
-
if (secp256k1.isInfinity(Qprime)) throw new Error("Child offset derived to an invalid key, try again");
|
|
130
|
-
|
|
131
|
-
return PublicKey.fromPoint(Qprime);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// toByteBuffer() {
|
|
135
|
-
// var b = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN);
|
|
136
|
-
// this.appendByteBuffer(b);
|
|
137
|
-
// return b.copy(0, b.offset);
|
|
138
|
-
// }
|
|
139
|
-
|
|
140
|
-
}, {
|
|
141
|
-
key: 'toHex',
|
|
142
|
-
value: function toHex() {
|
|
143
|
-
return this.toBuffer().toString('hex');
|
|
144
|
-
}
|
|
145
|
-
}], [{
|
|
146
|
-
key: 'fromBinary',
|
|
147
|
-
value: function fromBinary(bin) {
|
|
148
|
-
return PublicKey.fromBuffer(new Buffer(bin, 'binary'));
|
|
149
|
-
}
|
|
150
|
-
}, {
|
|
151
|
-
key: 'fromBuffer',
|
|
152
|
-
value: function fromBuffer(buffer) {
|
|
153
|
-
if (buffer.toString("hex") === "000000000000000000000000000000000000000000000000000000000000000000") return new PublicKey(null);
|
|
154
|
-
return new PublicKey(ecurve.Point.decodeFrom(secp256k1, buffer));
|
|
155
|
-
}
|
|
156
|
-
}, {
|
|
157
|
-
key: 'fromPoint',
|
|
158
|
-
value: function fromPoint(point) {
|
|
159
|
-
return new PublicKey(point);
|
|
160
|
-
}
|
|
161
|
-
}, {
|
|
162
|
-
key: 'fromString',
|
|
163
|
-
value: function fromString(public_key) {
|
|
164
|
-
var address_prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : config.get('address_prefix');
|
|
165
|
-
|
|
166
|
-
try {
|
|
167
|
-
return PublicKey.fromStringOrThrow(public_key, address_prefix);
|
|
168
|
-
} catch (e) {
|
|
169
|
-
return null;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
@arg {string} public_key - like STMXyz...
|
|
175
|
-
@arg {string} address_prefix - like STM
|
|
176
|
-
@throws {Error} if public key is invalid
|
|
177
|
-
@return PublicKey
|
|
178
|
-
*/
|
|
179
|
-
|
|
180
|
-
}, {
|
|
181
|
-
key: 'fromStringOrThrow',
|
|
182
|
-
value: function fromStringOrThrow(public_key) {
|
|
183
|
-
var address_prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : config.get('address_prefix');
|
|
184
|
-
|
|
185
|
-
var prefix = public_key.slice(0, address_prefix.length);
|
|
186
|
-
assert.equal(address_prefix, prefix, 'Expecting key to begin with ' + address_prefix + ', instead got ' + prefix);
|
|
187
|
-
public_key = public_key.slice(address_prefix.length);
|
|
188
|
-
|
|
189
|
-
public_key = new Buffer(base58.decode(public_key), 'binary');
|
|
190
|
-
var checksum = public_key.slice(-4);
|
|
191
|
-
public_key = public_key.slice(0, -4);
|
|
192
|
-
var new_checksum = hash.ripemd160(public_key);
|
|
193
|
-
new_checksum = new_checksum.slice(0, 4);
|
|
194
|
-
assert.deepEqual(checksum, new_checksum, 'Checksum did not match');
|
|
195
|
-
return PublicKey.fromBuffer(public_key);
|
|
196
|
-
}
|
|
197
|
-
}, {
|
|
198
|
-
key: 'fromHex',
|
|
199
|
-
value: function fromHex(hex) {
|
|
200
|
-
return PublicKey.fromBuffer(new Buffer(hex, 'hex'));
|
|
201
|
-
}
|
|
202
|
-
}, {
|
|
203
|
-
key: 'fromStringHex',
|
|
204
|
-
value: function fromStringHex(hex) {
|
|
205
|
-
return PublicKey.fromString(new Buffer(hex, 'hex'));
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/* </HEX> */
|
|
209
|
-
|
|
210
|
-
}]);
|
|
211
|
-
|
|
212
|
-
return PublicKey;
|
|
213
|
-
}();
|
|
214
|
-
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
@arg {string} public_key - like STMXyz...
|
|
77
|
+
@arg {string} address_prefix - like STM
|
|
78
|
+
@throws {Error} if public key is invalid
|
|
79
|
+
@return PublicKey
|
|
80
|
+
*/
|
|
81
|
+
static fromStringOrThrow(public_key, address_prefix = config.get('address_prefix')) {
|
|
82
|
+
var prefix = public_key.slice(0, address_prefix.length);
|
|
83
|
+
assert.equal(address_prefix, prefix, `Expecting key to begin with ${address_prefix}, instead got ${prefix}`);
|
|
84
|
+
public_key = public_key.slice(address_prefix.length);
|
|
85
|
+
public_key = new Buffer.from(base58.decode(public_key), 'binary');
|
|
86
|
+
var checksum = public_key.slice(-4);
|
|
87
|
+
public_key = public_key.slice(0, -4);
|
|
88
|
+
var new_checksum = hash.ripemd160(public_key);
|
|
89
|
+
new_checksum = new_checksum.slice(0, 4);
|
|
90
|
+
assert.deepEqual(checksum, new_checksum, 'Checksum did not match');
|
|
91
|
+
return PublicKey.fromBuffer(public_key);
|
|
92
|
+
}
|
|
93
|
+
toAddressString(address_prefix = config.get('address_prefix')) {
|
|
94
|
+
var pub_buf = this.toBuffer();
|
|
95
|
+
var pub_sha = hash.sha512(pub_buf);
|
|
96
|
+
var addy = hash.ripemd160(pub_sha);
|
|
97
|
+
var checksum = hash.ripemd160(addy);
|
|
98
|
+
addy = Buffer.concat([addy, checksum.slice(0, 4)]);
|
|
99
|
+
return address_prefix + base58.encode(addy);
|
|
100
|
+
}
|
|
101
|
+
toPtsAddy() {
|
|
102
|
+
var pub_buf = this.toBuffer();
|
|
103
|
+
var pub_sha = hash.sha256(pub_buf);
|
|
104
|
+
var addy = hash.ripemd160(pub_sha);
|
|
105
|
+
addy = Buffer.concat([new Buffer.from([0x38]), addy]); //version 56(decimal)
|
|
106
|
+
|
|
107
|
+
var checksum = hash.sha256(addy);
|
|
108
|
+
checksum = hash.sha256(checksum);
|
|
109
|
+
addy = Buffer.concat([addy, checksum.slice(0, 4)]);
|
|
110
|
+
return base58.encode(addy);
|
|
111
|
+
}
|
|
112
|
+
child(offset) {
|
|
113
|
+
assert(Buffer.isBuffer(offset), "Buffer required: offset");
|
|
114
|
+
assert.equal(offset.length, 32, "offset length");
|
|
115
|
+
offset = Buffer.concat([this.toBuffer(), offset]);
|
|
116
|
+
offset = hash.sha256(offset);
|
|
117
|
+
let c = BigInteger.fromBuffer(offset);
|
|
118
|
+
if (c.compareTo(n) >= 0) throw new Error("Child offset went out of bounds, try again");
|
|
119
|
+
let cG = G.multiply(c);
|
|
120
|
+
let Qprime = this.Q.add(cG);
|
|
121
|
+
if (secp256k1.isInfinity(Qprime)) throw new Error("Child offset derived to an invalid key, try again");
|
|
122
|
+
return PublicKey.fromPoint(Qprime);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// toByteBuffer() {
|
|
126
|
+
// var b = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN);
|
|
127
|
+
// this.appendByteBuffer(b);
|
|
128
|
+
// return b.copy(0, b.offset);
|
|
129
|
+
// }
|
|
130
|
+
|
|
131
|
+
static fromHex(hex) {
|
|
132
|
+
return PublicKey.fromBuffer(new Buffer.from(hex, 'hex'));
|
|
133
|
+
}
|
|
134
|
+
toHex() {
|
|
135
|
+
return this.toBuffer().toString('hex');
|
|
136
|
+
}
|
|
137
|
+
static fromStringHex(hex) {
|
|
138
|
+
return PublicKey.fromString(new Buffer.from(hex, 'hex'));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/* </HEX> */
|
|
142
|
+
}
|
|
215
143
|
module.exports = PublicKey;
|
|
@@ -1,112 +1,70 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const PrivateKey = require('./key_private');
|
|
4
|
+
const hash = require('./hash');
|
|
5
|
+
const secureRandom = require('secure-random');
|
|
6
6
|
|
|
7
7
|
// hash for .25 second
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var entropyArray = secureRandom.randomBuffer(101);
|
|
13
|
-
|
|
8
|
+
const HASH_POWER_MILLS = 250;
|
|
9
|
+
let entropyPos = 0,
|
|
10
|
+
entropyCount = 0;
|
|
11
|
+
const entropyArray = secureRandom.randomBuffer(101);
|
|
14
12
|
module.exports = {
|
|
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
|
-
var start_t = Date.now();
|
|
68
|
-
|
|
69
|
-
while (Date.now() - start_t < HASH_POWER_MILLS) {
|
|
70
|
-
entropy = hash.sha256(entropy);
|
|
71
|
-
}var hash_array = [];
|
|
72
|
-
hash_array.push(entropy);
|
|
73
|
-
|
|
74
|
-
// Hashing for 1 second may helps the computer is not low on entropy (this method may be called back-to-back).
|
|
75
|
-
hash_array.push(secureRandom.randomBuffer(32));
|
|
76
|
-
|
|
77
|
-
return hash.sha256(Buffer.concat(hash_array));
|
|
78
|
-
},
|
|
79
|
-
get_random_key: function get_random_key(entropy) {
|
|
80
|
-
return PrivateKey.fromBuffer(this.random32ByteBuffer(entropy));
|
|
81
|
-
},
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
// Turn invisible space like characters into a single space
|
|
85
|
-
// normalize_brain_key(brain_key){
|
|
86
|
-
// if (!(typeof brain_key === 'string')) {
|
|
87
|
-
// throw new Error("string required for brain_key");
|
|
88
|
-
// }
|
|
89
|
-
// brain_key = brain_key.trim();
|
|
90
|
-
// return brain_key.split(/[\t\n\v\f\r ]+/).join(' ');
|
|
91
|
-
// },
|
|
92
|
-
|
|
93
|
-
browserEntropy: function browserEntropy() {
|
|
94
|
-
var entropyStr = Array(entropyArray).join();
|
|
95
|
-
try {
|
|
96
|
-
entropyStr += new Date().toString() + " " + window.screen.height + " " + window.screen.width + " " + window.screen.colorDepth + " " + " " + window.screen.availHeight + " " + window.screen.availWidth + " " + window.screen.pixelDepth + navigator.language + " " + window.location + " " + window.history.length;
|
|
97
|
-
|
|
98
|
-
for (var i = 0, mimeType; i < navigator.mimeTypes.length; i++) {
|
|
99
|
-
mimeType = navigator.mimeTypes[i];
|
|
100
|
-
entropyStr += mimeType.description + " " + mimeType.type + " " + mimeType.suffixes + " ";
|
|
101
|
-
}
|
|
102
|
-
console.log("INFO\tbrowserEntropy gathered", entropyCount, 'events');
|
|
103
|
-
} catch (error) {
|
|
104
|
-
//nodejs:ReferenceError: window is not defined
|
|
105
|
-
entropyStr += hash.sha256(new Date().toString());
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
var b = new Buffer(entropyStr);
|
|
109
|
-
entropyStr += b.toString('binary') + " " + new Date().toString();
|
|
110
|
-
return entropyStr;
|
|
13
|
+
addEntropy(...ints) {
|
|
14
|
+
entropyCount++;
|
|
15
|
+
for (const i of ints) {
|
|
16
|
+
const pos = entropyPos++ % 101;
|
|
17
|
+
const i2 = entropyArray[pos] += i;
|
|
18
|
+
if (i2 > 9007199254740991) entropyArray[pos] = 0;
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
/**
|
|
22
|
+
A week random number generator can run out of entropy. This should ensure even the worst random number implementation will be reasonably safe.
|
|
23
|
+
@param1 string entropy of at least 32 bytes
|
|
24
|
+
*/
|
|
25
|
+
random32ByteBuffer(entropy = this.browserEntropy()) {
|
|
26
|
+
if (!(typeof entropy === 'string')) {
|
|
27
|
+
throw new Error("string required for entropy");
|
|
28
|
+
}
|
|
29
|
+
if (entropy.length < 32) {
|
|
30
|
+
throw new Error("expecting at least 32 bytes of entropy");
|
|
31
|
+
}
|
|
32
|
+
const start_t = Date.now();
|
|
33
|
+
while (Date.now() - start_t < HASH_POWER_MILLS) entropy = hash.sha256(entropy);
|
|
34
|
+
const hash_array = [];
|
|
35
|
+
hash_array.push(entropy);
|
|
36
|
+
|
|
37
|
+
// Hashing for 1 second may helps the computer is not low on entropy (this method may be called back-to-back).
|
|
38
|
+
hash_array.push(secureRandom.randomBuffer(32));
|
|
39
|
+
return hash.sha256(Buffer.concat(hash_array));
|
|
40
|
+
},
|
|
41
|
+
get_random_key(entropy) {
|
|
42
|
+
return PrivateKey.fromBuffer(this.random32ByteBuffer(entropy));
|
|
43
|
+
},
|
|
44
|
+
// Turn invisible space like characters into a single space
|
|
45
|
+
// normalize_brain_key(brain_key){
|
|
46
|
+
// if (!(typeof brain_key === 'string')) {
|
|
47
|
+
// throw new Error("string required for brain_key");
|
|
48
|
+
// }
|
|
49
|
+
// brain_key = brain_key.trim();
|
|
50
|
+
// return brain_key.split(/[\t\n\v\f\r ]+/).join(' ');
|
|
51
|
+
// },
|
|
52
|
+
|
|
53
|
+
browserEntropy() {
|
|
54
|
+
let entropyStr = Array(entropyArray).join();
|
|
55
|
+
try {
|
|
56
|
+
entropyStr += new Date().toString() + " " + window.screen.height + " " + window.screen.width + " " + window.screen.colorDepth + " " + " " + window.screen.availHeight + " " + window.screen.availWidth + " " + window.screen.pixelDepth + navigator.language + " " + window.location + " " + window.history.length;
|
|
57
|
+
for (let i = 0, mimeType; i < navigator.mimeTypes.length; i++) {
|
|
58
|
+
mimeType = navigator.mimeTypes[i];
|
|
59
|
+
entropyStr += mimeType.description + " " + mimeType.type + " " + mimeType.suffixes + " ";
|
|
60
|
+
}
|
|
61
|
+
console.log("INFO\tbrowserEntropy gathered", entropyCount, 'events');
|
|
62
|
+
} catch (error) {
|
|
63
|
+
//nodejs:ReferenceError: window is not defined
|
|
64
|
+
entropyStr += hash.sha256(new Date().toString());
|
|
111
65
|
}
|
|
66
|
+
const b = new Buffer(entropyStr);
|
|
67
|
+
entropyStr += b.toString('binary') + " " + new Date().toString();
|
|
68
|
+
return entropyStr;
|
|
69
|
+
}
|
|
112
70
|
};
|