@steemit/steem-js 0.7.11 → 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.
Files changed (132) hide show
  1. package/README.md +203 -87
  2. package/dist/api/index.d.ts +128 -0
  3. package/dist/api/methods.d.ts +9 -0
  4. package/dist/api/rpc-auth.d.ts +43 -0
  5. package/dist/api/transports/base.d.ts +13 -0
  6. package/dist/api/transports/http.d.ts +9 -0
  7. package/dist/api/transports/index.d.ts +9 -0
  8. package/dist/api/transports/types.d.ts +35 -0
  9. package/dist/api/transports/ws.d.ts +18 -0
  10. package/dist/auth/ecc/index.d.ts +9 -0
  11. package/dist/auth/ecc/src/address.d.ts +13 -0
  12. package/dist/auth/ecc/src/aes.d.ts +16 -0
  13. package/dist/auth/ecc/src/brain_key.d.ts +1 -0
  14. package/dist/auth/ecc/src/ecdsa.d.ts +28 -0
  15. package/dist/auth/ecc/src/ecsignature.d.ts +19 -0
  16. package/dist/auth/ecc/src/enforce_types.d.ts +5 -0
  17. package/dist/auth/ecc/src/hash.d.ts +25 -0
  18. package/dist/auth/ecc/src/index.d.ts +9 -0
  19. package/dist/auth/ecc/src/key_private.d.ts +38 -0
  20. package/dist/auth/ecc/src/key_public.d.ts +41 -0
  21. package/dist/auth/ecc/src/key_utils.d.ts +9 -0
  22. package/dist/auth/ecc/src/signature.d.ts +18 -0
  23. package/dist/auth/ecc.d.ts +3 -0
  24. package/dist/auth/index.d.ts +48 -0
  25. package/dist/auth/key_classes.d.ts +3 -0
  26. package/dist/auth/serializer.d.ts +19 -0
  27. package/dist/broadcast/helpers.d.ts +11 -0
  28. package/dist/broadcast/index.d.ts +43 -0
  29. package/dist/broadcast/operations.d.ts +6 -0
  30. package/dist/config.d.ts +25 -0
  31. package/dist/crypto/index.d.ts +25 -0
  32. package/dist/formatter/index.d.ts +92 -0
  33. package/dist/index.cjs +25848 -0
  34. package/dist/index.cjs.map +1 -0
  35. package/dist/index.d.ts +30 -0
  36. package/dist/index.js +25812 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/index.umd.js +57513 -0
  39. package/dist/index.umd.js.map +1 -0
  40. package/dist/memo/index.d.ts +11 -0
  41. package/dist/operations/index.d.ts +44 -0
  42. package/dist/serializer/convert.d.ts +12 -0
  43. package/dist/serializer/index.d.ts +11 -0
  44. package/dist/serializer/number_utils.d.ts +8 -0
  45. package/dist/serializer/precision.d.ts +5 -0
  46. package/dist/serializer/types.d.ts +36 -0
  47. package/dist/types/index.d.ts +131 -0
  48. package/dist/types.d.ts +34 -0
  49. package/dist/utils/index.d.ts +8 -0
  50. package/dist/utils.d.ts +2 -0
  51. package/package.json +72 -67
  52. package/.circleci/config.yml +0 -23
  53. package/.dockerignore +0 -12
  54. package/.editorconfig +0 -20
  55. package/.eslintrc +0 -27
  56. package/LICENSE +0 -21
  57. package/circle.yml +0 -3
  58. package/config.json +0 -10
  59. package/dist/statistics.html +0 -208
  60. package/dist/steem-tests.min.js +0 -23
  61. package/dist/steem-tests.min.js.gz +0 -0
  62. package/dist/steem-tests.min.js.map +0 -1
  63. package/dist/steem.min.js +0 -18
  64. package/dist/steem.min.js.gz +0 -0
  65. package/dist/steem.min.js.map +0 -1
  66. package/lib/api/index.js +0 -422
  67. package/lib/api/methods.js +0 -413
  68. package/lib/api/transports/base.js +0 -72
  69. package/lib/api/transports/http.js +0 -172
  70. package/lib/api/transports/index.js +0 -20
  71. package/lib/api/transports/ws.js +0 -228
  72. package/lib/auth/ecc/README.md +0 -20
  73. package/lib/auth/ecc/index.js +0 -13
  74. package/lib/auth/ecc/package.json +0 -36
  75. package/lib/auth/ecc/src/address.js +0 -84
  76. package/lib/auth/ecc/src/aes.js +0 -184
  77. package/lib/auth/ecc/src/brain_key.js +0 -13
  78. package/lib/auth/ecc/src/ecdsa.js +0 -219
  79. package/lib/auth/ecc/src/ecsignature.js +0 -128
  80. package/lib/auth/ecc/src/enforce_types.js +0 -49
  81. package/lib/auth/ecc/src/hash.js +0 -67
  82. package/lib/auth/ecc/src/key_private.js +0 -212
  83. package/lib/auth/ecc/src/key_public.js +0 -215
  84. package/lib/auth/ecc/src/key_utils.js +0 -112
  85. package/lib/auth/ecc/src/signature.js +0 -187
  86. package/lib/auth/index.js +0 -126
  87. package/lib/auth/memo.js +0 -147
  88. package/lib/auth/serializer/README.md +0 -13
  89. package/lib/auth/serializer/index.js +0 -21
  90. package/lib/auth/serializer/package.json +0 -32
  91. package/lib/auth/serializer/src/ChainTypes.js +0 -90
  92. package/lib/auth/serializer/src/convert.js +0 -37
  93. package/lib/auth/serializer/src/error_with_cause.js +0 -42
  94. package/lib/auth/serializer/src/fast_parser.js +0 -77
  95. package/lib/auth/serializer/src/number_utils.js +0 -67
  96. package/lib/auth/serializer/src/object_id.js +0 -68
  97. package/lib/auth/serializer/src/operations.js +0 -787
  98. package/lib/auth/serializer/src/precision.js +0 -95
  99. package/lib/auth/serializer/src/serializer.js +0 -211
  100. package/lib/auth/serializer/src/template.js +0 -17
  101. package/lib/auth/serializer/src/types.js +0 -1238
  102. package/lib/auth/serializer/src/validation.js +0 -360
  103. package/lib/broadcast/helpers.js +0 -177
  104. package/lib/broadcast/index.js +0 -127
  105. package/lib/broadcast/operations.js +0 -255
  106. package/lib/browser.js +0 -29
  107. package/lib/config.js +0 -45
  108. package/lib/formatter.js +0 -189
  109. package/lib/index.js +0 -19
  110. package/lib/utils.js +0 -55
  111. package/node-4.dockerfile +0 -6
  112. package/node-6.dockerfile +0 -6
  113. package/test/Crypto.js +0 -100
  114. package/test/KeyFormats.js +0 -105
  115. package/test/all_types.js +0 -115
  116. package/test/api.test.js +0 -326
  117. package/test/broadcast.test.js +0 -225
  118. package/test/browser/BrowserTests.js +0 -55
  119. package/test/comment.test.js +0 -69
  120. package/test/hf20-accounts.test.js +0 -76
  121. package/test/hf21-sps.test.js +0 -78
  122. package/test/memo.test.js +0 -37
  123. package/test/number_utils.js +0 -28
  124. package/test/operations_test.js +0 -39
  125. package/test/smt.test.js +0 -347
  126. package/test/test-post.json +0 -13
  127. package/test/test.html +0 -13
  128. package/test/test_helper.js +0 -19
  129. package/test/types_test.js +0 -139
  130. package/webpack/makeConfig.js +0 -105
  131. package/webpack.config.js +0 -2
  132. package/yarn.lock +0 -3336
@@ -1,219 +0,0 @@
1
- 'use strict';
2
-
3
- var assert = require('assert'); // from github.com/bitcoinjs/bitcoinjs-lib from github.com/cryptocoinjs/ecdsa
4
- var crypto = require('./hash');
5
- var enforceType = require('./enforce_types');
6
-
7
- var BigInteger = require('bigi');
8
- var ECSignature = require('./ecsignature');
9
-
10
- // https://tools.ietf.org/html/rfc6979#section-3.2
11
- function deterministicGenerateK(curve, hash, d, checkSig, nonce) {
12
-
13
- enforceType('Buffer', hash);
14
- enforceType(BigInteger, d);
15
-
16
- if (nonce) {
17
- hash = crypto.sha256(Buffer.concat([hash, new Buffer(nonce)]));
18
- }
19
-
20
- // sanity check
21
- assert.equal(hash.length, 32, 'Hash must be 256 bit');
22
-
23
- var x = d.toBuffer(32);
24
- var k = new Buffer(32);
25
- var v = new Buffer(32);
26
-
27
- // Step B
28
- v.fill(1);
29
-
30
- // Step C
31
- k.fill(0);
32
-
33
- // Step D
34
- k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([0]), x, hash]), k);
35
-
36
- // Step E
37
- v = crypto.HmacSHA256(v, k);
38
-
39
- // Step F
40
- k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([1]), x, hash]), k);
41
-
42
- // Step G
43
- v = crypto.HmacSHA256(v, k);
44
-
45
- // Step H1/H2a, ignored as tlen === qlen (256 bit)
46
- // Step H2b
47
- v = crypto.HmacSHA256(v, k);
48
-
49
- var T = BigInteger.fromBuffer(v);
50
-
51
- // Step H3, repeat until T is within the interval [1, n - 1]
52
- while (T.signum() <= 0 || T.compareTo(curve.n) >= 0 || !checkSig(T)) {
53
- k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([0])]), k);
54
- v = crypto.HmacSHA256(v, k);
55
-
56
- // Step H1/H2a, again, ignored as tlen === qlen (256 bit)
57
- // Step H2b again
58
- v = crypto.HmacSHA256(v, k);
59
-
60
- T = BigInteger.fromBuffer(v);
61
- }
62
-
63
- return T;
64
- }
65
-
66
- function sign(curve, hash, d, nonce) {
67
-
68
- var e = BigInteger.fromBuffer(hash);
69
- var n = curve.n;
70
- var G = curve.G;
71
-
72
- var r, s;
73
- var k = deterministicGenerateK(curve, hash, d, function (k) {
74
- // find canonically valid signature
75
- var Q = G.multiply(k);
76
-
77
- if (curve.isInfinity(Q)) return false;
78
-
79
- r = Q.affineX.mod(n);
80
- if (r.signum() === 0) return false;
81
-
82
- s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n);
83
- if (s.signum() === 0) return false;
84
-
85
- return true;
86
- }, nonce);
87
-
88
- var N_OVER_TWO = n.shiftRight(1);
89
-
90
- // enforce low S values, see bip62: 'low s values in signatures'
91
- if (s.compareTo(N_OVER_TWO) > 0) {
92
- s = n.subtract(s);
93
- }
94
-
95
- return new ECSignature(r, s);
96
- }
97
-
98
- function verifyRaw(curve, e, signature, Q) {
99
- var n = curve.n;
100
- var G = curve.G;
101
-
102
- var r = signature.r;
103
- var s = signature.s;
104
-
105
- // 1.4.1 Enforce r and s are both integers in the interval [1, n − 1]
106
- if (r.signum() <= 0 || r.compareTo(n) >= 0) return false;
107
- if (s.signum() <= 0 || s.compareTo(n) >= 0) return false;
108
-
109
- // c = s^-1 mod n
110
- var c = s.modInverse(n);
111
-
112
- // 1.4.4 Compute u1 = es^−1 mod n
113
- // u2 = rs^−1 mod n
114
- var u1 = e.multiply(c).mod(n);
115
- var u2 = r.multiply(c).mod(n);
116
-
117
- // 1.4.5 Compute R = (xR, yR) = u1G + u2Q
118
- var R = G.multiplyTwo(u1, Q, u2);
119
-
120
- // 1.4.5 (cont.) Enforce R is not at infinity
121
- if (curve.isInfinity(R)) return false;
122
-
123
- // 1.4.6 Convert the field element R.x to an integer
124
- var xR = R.affineX;
125
-
126
- // 1.4.7 Set v = xR mod n
127
- var v = xR.mod(n);
128
-
129
- // 1.4.8 If v = r, output "valid", and if v != r, output "invalid"
130
- return v.equals(r);
131
- }
132
-
133
- function verify(curve, hash, signature, Q) {
134
- // 1.4.2 H = Hash(M), already done by the user
135
- // 1.4.3 e = H
136
- var e = BigInteger.fromBuffer(hash);
137
- return verifyRaw(curve, e, signature, Q);
138
- }
139
-
140
- /**
141
- * Recover a public key from a signature.
142
- *
143
- * See SEC 1: Elliptic Curve Cryptography, section 4.1.6, "Public
144
- * Key Recovery Operation".
145
- *
146
- * http://www.secg.org/download/aid-780/sec1-v2.pdf
147
- */
148
- function recoverPubKey(curve, e, signature, i) {
149
- assert.strictEqual(i & 3, i, 'Recovery param is more than two bits');
150
-
151
- var n = curve.n;
152
- var G = curve.G;
153
-
154
- var r = signature.r;
155
- var s = signature.s;
156
-
157
- assert(r.signum() > 0 && r.compareTo(n) < 0, 'Invalid r value');
158
- assert(s.signum() > 0 && s.compareTo(n) < 0, 'Invalid s value');
159
-
160
- // A set LSB signifies that the y-coordinate is odd
161
- var isYOdd = i & 1;
162
-
163
- // The more significant bit specifies whether we should use the
164
- // first or second candidate key.
165
- var isSecondKey = i >> 1;
166
-
167
- // 1.1 Let x = r + jn
168
- var x = isSecondKey ? r.add(n) : r;
169
- var R = curve.pointFromX(isYOdd, x);
170
-
171
- // 1.4 Check that nR is at infinity
172
- var nR = R.multiply(n);
173
- assert(curve.isInfinity(nR), 'nR is not a valid curve point');
174
-
175
- // Compute -e from e
176
- var eNeg = e.negate().mod(n);
177
-
178
- // 1.6.1 Compute Q = r^-1 (sR - eG)
179
- // Q = r^-1 (sR + -eG)
180
- var rInv = r.modInverse(n);
181
-
182
- var Q = R.multiplyTwo(s, G, eNeg).multiply(rInv);
183
- curve.validate(Q);
184
-
185
- return Q;
186
- }
187
-
188
- /**
189
- * Calculate pubkey extraction parameter.
190
- *
191
- * When extracting a pubkey from a signature, we have to
192
- * distinguish four different cases. Rather than putting this
193
- * burden on the verifier, Bitcoin includes a 2-bit value with the
194
- * signature.
195
- *
196
- * This function simply tries all four cases and returns the value
197
- * that resulted in a successful pubkey recovery.
198
- */
199
- function calcPubKeyRecoveryParam(curve, e, signature, Q) {
200
- for (var i = 0; i < 4; i++) {
201
- var Qprime = recoverPubKey(curve, e, signature, i);
202
-
203
- // 1.6.2 Verify Q
204
- if (Qprime.equals(Q)) {
205
- return i;
206
- }
207
- }
208
-
209
- throw new Error('Unable to find valid recovery factor');
210
- }
211
-
212
- module.exports = {
213
- calcPubKeyRecoveryParam: calcPubKeyRecoveryParam,
214
- deterministicGenerateK: deterministicGenerateK,
215
- recoverPubKey: recoverPubKey,
216
- sign: sign,
217
- verify: verify,
218
- verifyRaw: verifyRaw
219
- };
@@ -1,128 +0,0 @@
1
- 'use strict';
2
-
3
- var assert = require('assert'); // from https://github.com/bitcoinjs/bitcoinjs-lib
4
- var enforceType = require('./enforce_types');
5
-
6
- var BigInteger = require('bigi');
7
-
8
- function ECSignature(r, s) {
9
- enforceType(BigInteger, r);
10
- enforceType(BigInteger, s);
11
-
12
- this.r = r;
13
- this.s = s;
14
- }
15
-
16
- // Import operations
17
- ECSignature.parseCompact = function (buffer) {
18
- assert.equal(buffer.length, 65, 'Invalid signature length');
19
- var i = buffer.readUInt8(0) - 27;
20
-
21
- // At most 3 bits
22
- assert.equal(i, i & 7, 'Invalid signature parameter');
23
- var compressed = !!(i & 4);
24
-
25
- // Recovery param only
26
- i = i & 3;
27
-
28
- var r = BigInteger.fromBuffer(buffer.slice(1, 33));
29
- var s = BigInteger.fromBuffer(buffer.slice(33));
30
-
31
- return {
32
- compressed: compressed,
33
- i: i,
34
- signature: new ECSignature(r, s)
35
- };
36
- };
37
-
38
- ECSignature.fromDER = function (buffer) {
39
- assert.equal(buffer.readUInt8(0), 0x30, 'Not a DER sequence');
40
- assert.equal(buffer.readUInt8(1), buffer.length - 2, 'Invalid sequence length');
41
- assert.equal(buffer.readUInt8(2), 0x02, 'Expected a DER integer');
42
-
43
- var rLen = buffer.readUInt8(3);
44
- assert(rLen > 0, 'R length is zero');
45
-
46
- var offset = 4 + rLen;
47
- assert.equal(buffer.readUInt8(offset), 0x02, 'Expected a DER integer (2)');
48
-
49
- var sLen = buffer.readUInt8(offset + 1);
50
- assert(sLen > 0, 'S length is zero');
51
-
52
- var rB = buffer.slice(4, offset);
53
- var sB = buffer.slice(offset + 2);
54
- offset += 2 + sLen;
55
-
56
- if (rLen > 1 && rB.readUInt8(0) === 0x00) {
57
- assert(rB.readUInt8(1) & 0x80, 'R value excessively padded');
58
- }
59
-
60
- if (sLen > 1 && sB.readUInt8(0) === 0x00) {
61
- assert(sB.readUInt8(1) & 0x80, 'S value excessively padded');
62
- }
63
-
64
- assert.equal(offset, buffer.length, 'Invalid DER encoding');
65
- var r = BigInteger.fromDERInteger(rB);
66
- var s = BigInteger.fromDERInteger(sB);
67
-
68
- assert(r.signum() >= 0, 'R value is negative');
69
- assert(s.signum() >= 0, 'S value is negative');
70
-
71
- return new ECSignature(r, s);
72
- };
73
-
74
- // FIXME: 0x00, 0x04, 0x80 are SIGHASH_* boundary constants, importing Transaction causes a circular dependency
75
- ECSignature.parseScriptSignature = function (buffer) {
76
- var hashType = buffer.readUInt8(buffer.length - 1);
77
- var hashTypeMod = hashType & ~0x80;
78
-
79
- assert(hashTypeMod > 0x00 && hashTypeMod < 0x04, 'Invalid hashType');
80
-
81
- return {
82
- signature: ECSignature.fromDER(buffer.slice(0, -1)),
83
- hashType: hashType
84
- };
85
- };
86
-
87
- // Export operations
88
- ECSignature.prototype.toCompact = function (i, compressed) {
89
- if (compressed) i += 4;
90
- i += 27;
91
-
92
- var buffer = new Buffer(65);
93
- buffer.writeUInt8(i, 0);
94
-
95
- this.r.toBuffer(32).copy(buffer, 1);
96
- this.s.toBuffer(32).copy(buffer, 33);
97
-
98
- return buffer;
99
- };
100
-
101
- ECSignature.prototype.toDER = function () {
102
- var rBa = this.r.toDERInteger();
103
- var sBa = this.s.toDERInteger();
104
-
105
- var sequence = [];
106
-
107
- // INTEGER
108
- sequence.push(0x02, rBa.length);
109
- sequence = sequence.concat(rBa);
110
-
111
- // INTEGER
112
- sequence.push(0x02, sBa.length);
113
- sequence = sequence.concat(sBa);
114
-
115
- // SEQUENCE
116
- sequence.unshift(0x30, sequence.length);
117
-
118
- return new Buffer(sequence);
119
- };
120
-
121
- ECSignature.prototype.toScriptSignature = function (hashType) {
122
- var hashTypeBuffer = new Buffer(1);
123
- hashTypeBuffer.writeUInt8(hashType, 0);
124
-
125
- return Buffer.concat([this.toDER(), hashTypeBuffer]);
126
- };
127
-
128
- module.exports = ECSignature;
@@ -1,49 +0,0 @@
1
- 'use strict';
2
-
3
- module.exports = function enforce(type, value) {
4
- // Copied from https://github.com/bitcoinjs/bitcoinjs-lib
5
- switch (type) {
6
- case 'Array':
7
- {
8
- if (Array.isArray(value)) return;
9
- break;
10
- }
11
-
12
- case 'Boolean':
13
- {
14
- if (typeof value === 'boolean') return;
15
- break;
16
- }
17
-
18
- case 'Buffer':
19
- {
20
- if (Buffer.isBuffer(value)) return;
21
- break;
22
- }
23
-
24
- case 'Number':
25
- {
26
- if (typeof value === 'number') return;
27
- break;
28
- }
29
-
30
- case 'String':
31
- {
32
- if (typeof value === 'string') return;
33
- break;
34
- }
35
-
36
- default:
37
- {
38
- if (getName(value.constructor) === getName(type)) return;
39
- }
40
- }
41
-
42
- throw new TypeError('Expected ' + (getName(type) || type) + ', got ' + value);
43
- };
44
-
45
- function getName(fn) {
46
- // Why not fn.name: https://kangax.github.io/compat-table/es6/#function_name_property
47
- var match = fn.toString().match(/function (.*?)\(/);
48
- return match ? match[1] : null;
49
- }
@@ -1,67 +0,0 @@
1
- 'use strict';
2
-
3
- var _createHash = require('create-hash');
4
-
5
- var _createHash2 = _interopRequireDefault(_createHash);
6
-
7
- var _createHmac = require('create-hmac');
8
-
9
- var _createHmac2 = _interopRequireDefault(_createHmac);
10
-
11
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
-
13
- /** @arg {string|Buffer} data
14
- @arg {string} [digest = null] - 'hex', 'binary' or 'base64'
15
- @return {string|Buffer} - Buffer when digest is null, or string
16
- */
17
- function sha1(data, encoding) {
18
- return (0, _createHash2.default)('sha1').update(data).digest(encoding);
19
- }
20
-
21
- /** @arg {string|Buffer} data
22
- @arg {string} [digest = null] - 'hex', 'binary' or 'base64'
23
- @return {string|Buffer} - Buffer when digest is null, or string
24
- */
25
- function sha256(data, encoding) {
26
- return (0, _createHash2.default)('sha256').update(data).digest(encoding);
27
- }
28
-
29
- /** @arg {string|Buffer} data
30
- @arg {string} [digest = null] - 'hex', 'binary' or 'base64'
31
- @return {string|Buffer} - Buffer when digest is null, or string
32
- */
33
- function sha512(data, encoding) {
34
- return (0, _createHash2.default)('sha512').update(data).digest(encoding);
35
- }
36
-
37
- function HmacSHA256(buffer, secret) {
38
- return (0, _createHmac2.default)('sha256', secret).update(buffer).digest();
39
- }
40
-
41
- function ripemd160(data) {
42
- return (0, _createHash2.default)('rmd160').update(data).digest();
43
- }
44
-
45
- // function hash160(buffer) {
46
- // return ripemd160(sha256(buffer))
47
- // }
48
- //
49
- // function hash256(buffer) {
50
- // return sha256(sha256(buffer))
51
- // }
52
-
53
- //
54
- // function HmacSHA512(buffer, secret) {
55
- // return crypto.createHmac('sha512', secret).update(buffer).digest()
56
- // }
57
-
58
- module.exports = {
59
- sha1: sha1,
60
- sha256: sha256,
61
- sha512: sha512,
62
- HmacSHA256: HmacSHA256,
63
- ripemd160: ripemd160
64
- // hash160: hash160,
65
- // hash256: hash256,
66
- // HmacSHA512: HmacSHA512
67
- };
@@ -1,212 +0,0 @@
1
- 'use strict';
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"); } }
6
-
7
- var ecurve = require('ecurve');
8
- var Point = ecurve.Point;
9
- var secp256k1 = ecurve.getCurveByName('secp256k1');
10
- var BigInteger = require('bigi');
11
- var base58 = require('bs58');
12
- var assert = require('assert');
13
- var hash = require('./hash');
14
- var PublicKey = require('./key_public');
15
-
16
- var G = secp256k1.G;
17
- var n = secp256k1.n;
18
-
19
- var PrivateKey = function () {
20
-
21
- /**
22
- @private see static functions
23
- @param {BigInteger}
24
- */
25
- function PrivateKey(d) {
26
- _classCallCheck(this, PrivateKey);
27
-
28
- this.d = d;
29
- }
30
-
31
- _createClass(PrivateKey, [{
32
- key: 'toWif',
33
- value: function toWif() {
34
- var private_key = this.toBuffer();
35
- // checksum includes the version
36
- private_key = Buffer.concat([new Buffer([0x80]), private_key]);
37
- var checksum = hash.sha256(private_key);
38
- checksum = hash.sha256(checksum);
39
- checksum = checksum.slice(0, 4);
40
- var private_wif = Buffer.concat([private_key, checksum]);
41
- return base58.encode(private_wif);
42
- }
43
-
44
- /** Alias for {@link toWif} */
45
-
46
- }, {
47
- key: 'toString',
48
- value: function toString() {
49
- return this.toWif();
50
- }
51
-
52
- /**
53
- @return {Point}
54
- */
55
-
56
- }, {
57
- key: 'toPublicKeyPoint',
58
- value: function toPublicKeyPoint() {
59
- var Q;
60
- return Q = secp256k1.G.multiply(this.d);
61
- }
62
- }, {
63
- key: 'toPublic',
64
- value: function toPublic() {
65
- if (this.public_key) {
66
- return this.public_key;
67
- }
68
- return this.public_key = PublicKey.fromPoint(this.toPublicKeyPoint());
69
- }
70
- }, {
71
- key: 'toBuffer',
72
- value: function toBuffer() {
73
- return this.d.toBuffer(32);
74
- }
75
-
76
- /** ECIES */
77
-
78
- }, {
79
- key: 'get_shared_secret',
80
- value: function get_shared_secret(public_key) {
81
- public_key = toPublic(public_key);
82
- var KB = public_key.toUncompressed().toBuffer();
83
- var KBP = Point.fromAffine(secp256k1, BigInteger.fromBuffer(KB.slice(1, 33)), // x
84
- BigInteger.fromBuffer(KB.slice(33, 65)) // y
85
- );
86
- var r = this.toBuffer();
87
- var P = KBP.multiply(BigInteger.fromBuffer(r));
88
- var S = P.affineX.toBuffer({ size: 32 });
89
- // SHA512 used in ECIES
90
- return hash.sha512(S);
91
- }
92
-
93
- // /** ECIES (does not always match the Point.fromAffine version above) */
94
- // get_shared_secret(public_key){
95
- // public_key = toPublic(public_key)
96
- // var P = public_key.Q.multiply( this.d );
97
- // var S = P.affineX.toBuffer({size: 32});
98
- // // ECIES, adds an extra sha512
99
- // return hash.sha512(S);
100
- // }
101
-
102
- /** @throws {Error} - overflow of the key could not be derived */
103
-
104
- }, {
105
- key: 'child',
106
- value: function child(offset) {
107
- offset = Buffer.concat([this.toPublicKey().toBuffer(), offset]);
108
- offset = hash.sha256(offset);
109
- var c = BigInteger.fromBuffer(offset);
110
-
111
- if (c.compareTo(n) >= 0) throw new Error("Child offset went out of bounds, try again");
112
-
113
- var derived = this.d.add(c); //.mod(n)
114
-
115
- if (derived.signum() === 0) throw new Error("Child offset derived to an invalid key, try again");
116
-
117
- return new PrivateKey(derived);
118
- }
119
-
120
- // toByteBuffer() {
121
- // var b = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN);
122
- // this.appendByteBuffer(b);
123
- // return b.copy(0, b.offset);
124
- // }
125
-
126
- }, {
127
- key: 'toHex',
128
- value: function toHex() {
129
- return this.toBuffer().toString('hex');
130
- }
131
- }, {
132
- key: 'toPublicKey',
133
- value: function toPublicKey() {
134
- return this.toPublic();
135
- }
136
-
137
- /* </helper_functions> */
138
-
139
- }], [{
140
- key: 'fromBuffer',
141
- value: function fromBuffer(buf) {
142
- if (!Buffer.isBuffer(buf)) {
143
- throw new Error("Expecting paramter to be a Buffer type");
144
- }
145
- if (32 !== buf.length) {
146
- console.log('WARN: Expecting 32 bytes, instead got ' + buf.length + ', stack trace:', new Error().stack);
147
- }
148
- if (buf.length === 0) {
149
- throw new Error("Empty buffer");
150
- }
151
- return new PrivateKey(BigInteger.fromBuffer(buf));
152
- }
153
-
154
- /** @arg {string} seed - any length string. This is private, the same seed produces the same private key every time. */
155
-
156
- }, {
157
- key: 'fromSeed',
158
- value: function fromSeed(seed) {
159
- // generate_private_key
160
- if (!(typeof seed === 'string')) {
161
- throw new Error('seed must be of type string');
162
- }
163
- return PrivateKey.fromBuffer(hash.sha256(seed));
164
- }
165
- }, {
166
- key: 'isWif',
167
- value: function isWif(text) {
168
- try {
169
- this.fromWif(text);
170
- return true;
171
- } catch (e) {
172
- return false;
173
- }
174
- }
175
-
176
- /**
177
- @throws {AssertError|Error} parsing key
178
- @return {string} Wallet Import Format (still a secret, Not encrypted)
179
- */
180
-
181
- }, {
182
- key: 'fromWif',
183
- value: function fromWif(_private_wif) {
184
- var private_wif = new Buffer(base58.decode(_private_wif));
185
- var version = private_wif.readUInt8(0);
186
- assert.equal(0x80, version, 'Expected version ' + 0x80 + ', instead got ' + version);
187
- // checksum includes the version
188
- var private_key = private_wif.slice(0, -4);
189
- var checksum = private_wif.slice(-4);
190
- var new_checksum = hash.sha256(private_key);
191
- new_checksum = hash.sha256(new_checksum);
192
- new_checksum = new_checksum.slice(0, 4);
193
- if (checksum.toString() !== new_checksum.toString()) throw new Error('Invalid WIF key (checksum miss-match)');
194
-
195
- private_key = private_key.slice(1);
196
- return PrivateKey.fromBuffer(private_key);
197
- }
198
- }, {
199
- key: 'fromHex',
200
- value: function fromHex(hex) {
201
- return PrivateKey.fromBuffer(new Buffer(hex, 'hex'));
202
- }
203
- }]);
204
-
205
- return PrivateKey;
206
- }();
207
-
208
- module.exports = PrivateKey;
209
-
210
- var toPublic = function toPublic(data) {
211
- return data == null ? data : data.Q ? data : PublicKey.fromStringOrThrow(data);
212
- };