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