@steemit/steem-js 0.7.10 → 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 -1110
- 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 +11 -11
- 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 ecdsa = require('./ecdsa');
|
|
8
4
|
var hash = require('./hash');
|
|
@@ -11,177 +7,129 @@ var assert = require('assert');
|
|
|
11
7
|
var BigInteger = require('bigi');
|
|
12
8
|
var PublicKey = require('./key_public');
|
|
13
9
|
var PrivateKey = require('./key_private');
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
10
|
+
class Signature {
|
|
11
|
+
constructor(r1, s1, i1) {
|
|
12
|
+
this.r = r1;
|
|
13
|
+
this.s = s1;
|
|
14
|
+
this.i = i1;
|
|
15
|
+
assert.equal(this.r != null, true, 'Missing parameter');
|
|
16
|
+
assert.equal(this.s != null, true, 'Missing parameter');
|
|
17
|
+
assert.equal(this.i != null, true, 'Missing parameter');
|
|
18
|
+
}
|
|
19
|
+
static fromBuffer(buf) {
|
|
20
|
+
var i, r, s;
|
|
21
|
+
assert.equal(buf.length, 65, 'Invalid signature length');
|
|
22
|
+
i = buf.readUInt8(0);
|
|
23
|
+
assert.equal(i - 27, i - 27 & 7, 'Invalid signature parameter');
|
|
24
|
+
r = BigInteger.fromBuffer(buf.slice(1, 33));
|
|
25
|
+
s = BigInteger.fromBuffer(buf.slice(33));
|
|
26
|
+
return new Signature(r, s, i);
|
|
27
|
+
}
|
|
28
|
+
toBuffer() {
|
|
29
|
+
var buf;
|
|
30
|
+
buf = new Buffer.alloc(65);
|
|
31
|
+
buf.writeUInt8(this.i, 0);
|
|
32
|
+
this.r.toBuffer(32).copy(buf, 1);
|
|
33
|
+
this.s.toBuffer(32).copy(buf, 33);
|
|
34
|
+
return buf;
|
|
35
|
+
}
|
|
36
|
+
recoverPublicKeyFromBuffer(buffer) {
|
|
37
|
+
return this.recoverPublicKey(hash.sha256(buffer));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
@return {PublicKey}
|
|
41
|
+
*/
|
|
42
|
+
recoverPublicKey(sha256_buffer) {
|
|
43
|
+
let Q, e, i;
|
|
44
|
+
e = BigInteger.fromBuffer(sha256_buffer);
|
|
45
|
+
i = this.i;
|
|
46
|
+
i -= 27;
|
|
47
|
+
i = i & 3;
|
|
48
|
+
Q = ecdsa.recoverPubKey(curve, e, this, i);
|
|
49
|
+
return PublicKey.fromPoint(Q);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
@param {Buffer} buf
|
|
53
|
+
@param {PrivateKey} private_key
|
|
54
|
+
@return {Signature}
|
|
55
|
+
*/
|
|
56
|
+
static signBuffer(buf, private_key) {
|
|
57
|
+
var _hash = hash.sha256(buf);
|
|
58
|
+
return Signature.signBufferSha256(_hash, private_key);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Sign a buffer of exactally 32 bytes in size (sha256(text))
|
|
62
|
+
@param {Buffer} buf - 32 bytes binary
|
|
63
|
+
@param {PrivateKey} private_key
|
|
64
|
+
@return {Signature}
|
|
65
|
+
*/
|
|
66
|
+
static signBufferSha256(buf_sha256, private_key) {
|
|
67
|
+
if (buf_sha256.length !== 32 || !Buffer.isBuffer(buf_sha256)) throw new Error("buf_sha256: 32 byte buffer required");
|
|
68
|
+
private_key = toPrivateObj(private_key);
|
|
69
|
+
assert(private_key, 'private_key required');
|
|
70
|
+
var der, e, ecsignature, i, lenR, lenS, nonce;
|
|
71
|
+
i = null;
|
|
72
|
+
nonce = 0;
|
|
73
|
+
e = BigInteger.fromBuffer(buf_sha256);
|
|
74
|
+
while (true) {
|
|
75
|
+
ecsignature = ecdsa.sign(curve, buf_sha256, private_key.d, nonce++);
|
|
76
|
+
der = ecsignature.toDER();
|
|
77
|
+
lenR = der[3];
|
|
78
|
+
lenS = der[5 + lenR];
|
|
79
|
+
if (lenR === 32 && lenS === 32) {
|
|
80
|
+
i = ecdsa.calcPubKeyRecoveryParam(curve, e, ecsignature, private_key.toPublicKey().Q);
|
|
81
|
+
i += 4; // compressed
|
|
82
|
+
i += 27; // compact // 24 or 27 :( forcing odd-y 2nd key candidate)
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
if (nonce % 10 === 0) {
|
|
86
|
+
console.log("WARN: " + nonce + " attempts to find canonical signature");
|
|
87
|
+
}
|
|
25
88
|
}
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
}, {
|
|
74
|
-
key: 'verifyHash',
|
|
75
|
-
value: function verifyHash(hash, public_key) {
|
|
76
|
-
assert.equal(hash.length, 32, "A SHA 256 should be 32 bytes long, instead got " + hash.length);
|
|
77
|
-
return ecdsa.verify(curve, hash, {
|
|
78
|
-
r: this.r,
|
|
79
|
-
s: this.s
|
|
80
|
-
}, public_key.Q);
|
|
81
|
-
}
|
|
82
|
-
}, {
|
|
83
|
-
key: 'toHex',
|
|
84
|
-
value: function toHex() {
|
|
85
|
-
return this.toBuffer().toString("hex");
|
|
86
|
-
}
|
|
87
|
-
}, {
|
|
88
|
-
key: 'verifyHex',
|
|
89
|
-
value: function verifyHex(hex, public_key) {
|
|
90
|
-
var buf;
|
|
91
|
-
buf = new Buffer(hex, 'hex');
|
|
92
|
-
return this.verifyBuffer(buf, public_key);
|
|
93
|
-
}
|
|
94
|
-
}], [{
|
|
95
|
-
key: 'fromBuffer',
|
|
96
|
-
value: function fromBuffer(buf) {
|
|
97
|
-
var i, r, s;
|
|
98
|
-
assert.equal(buf.length, 65, 'Invalid signature length');
|
|
99
|
-
i = buf.readUInt8(0);
|
|
100
|
-
assert.equal(i - 27, i - 27 & 7, 'Invalid signature parameter');
|
|
101
|
-
r = BigInteger.fromBuffer(buf.slice(1, 33));
|
|
102
|
-
s = BigInteger.fromBuffer(buf.slice(33));
|
|
103
|
-
return new Signature(r, s, i);
|
|
104
|
-
}
|
|
105
|
-
}, {
|
|
106
|
-
key: 'signBuffer',
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
@param {Buffer} buf
|
|
111
|
-
@param {PrivateKey} private_key
|
|
112
|
-
@return {Signature}
|
|
113
|
-
*/
|
|
114
|
-
value: function signBuffer(buf, private_key) {
|
|
115
|
-
var _hash = hash.sha256(buf);
|
|
116
|
-
return Signature.signBufferSha256(_hash, private_key);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** Sign a buffer of exactally 32 bytes in size (sha256(text))
|
|
120
|
-
@param {Buffer} buf - 32 bytes binary
|
|
121
|
-
@param {PrivateKey} private_key
|
|
122
|
-
@return {Signature}
|
|
123
|
-
*/
|
|
124
|
-
|
|
125
|
-
}, {
|
|
126
|
-
key: 'signBufferSha256',
|
|
127
|
-
value: function signBufferSha256(buf_sha256, private_key) {
|
|
128
|
-
if (buf_sha256.length !== 32 || !Buffer.isBuffer(buf_sha256)) throw new Error("buf_sha256: 32 byte buffer requred");
|
|
129
|
-
private_key = toPrivateObj(private_key);
|
|
130
|
-
assert(private_key, 'private_key required');
|
|
131
|
-
|
|
132
|
-
var der, e, ecsignature, i, lenR, lenS, nonce;
|
|
133
|
-
i = null;
|
|
134
|
-
nonce = 0;
|
|
135
|
-
e = BigInteger.fromBuffer(buf_sha256);
|
|
136
|
-
while (true) {
|
|
137
|
-
ecsignature = ecdsa.sign(curve, buf_sha256, private_key.d, nonce++);
|
|
138
|
-
der = ecsignature.toDER();
|
|
139
|
-
lenR = der[3];
|
|
140
|
-
lenS = der[5 + lenR];
|
|
141
|
-
if (lenR === 32 && lenS === 32) {
|
|
142
|
-
i = ecdsa.calcPubKeyRecoveryParam(curve, e, ecsignature, private_key.toPublicKey().Q);
|
|
143
|
-
i += 4; // compressed
|
|
144
|
-
i += 27; // compact // 24 or 27 :( forcing odd-y 2nd key candidate)
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
if (nonce % 10 === 0) {
|
|
148
|
-
console.log("WARN: " + nonce + " attempts to find canonical signature");
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return new Signature(ecsignature.r, ecsignature.s, i);
|
|
152
|
-
}
|
|
153
|
-
}, {
|
|
154
|
-
key: 'sign',
|
|
155
|
-
value: function sign(string, private_key) {
|
|
156
|
-
return Signature.signBuffer(new Buffer(string), private_key);
|
|
157
|
-
}
|
|
158
|
-
}, {
|
|
159
|
-
key: 'fromHex',
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
// toByteBuffer() {
|
|
163
|
-
// var b;
|
|
164
|
-
// b = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN);
|
|
165
|
-
// this.appendByteBuffer(b);
|
|
166
|
-
// return b.copy(0, b.offset);
|
|
167
|
-
// };
|
|
168
|
-
|
|
169
|
-
value: function fromHex(hex) {
|
|
170
|
-
return Signature.fromBuffer(new Buffer(hex, "hex"));
|
|
171
|
-
}
|
|
172
|
-
}, {
|
|
173
|
-
key: 'signHex',
|
|
174
|
-
value: function signHex(hex, private_key) {
|
|
175
|
-
var buf;
|
|
176
|
-
buf = new Buffer(hex, 'hex');
|
|
177
|
-
return Signature.signBuffer(buf, private_key);
|
|
178
|
-
}
|
|
179
|
-
}]);
|
|
180
|
-
|
|
181
|
-
return Signature;
|
|
182
|
-
}();
|
|
183
|
-
|
|
184
|
-
var toPrivateObj = function toPrivateObj(o) {
|
|
185
|
-
return o ? o.d ? o : PrivateKey.fromWif(o) : o /*null or undefined*/;
|
|
186
|
-
};
|
|
89
|
+
return new Signature(ecsignature.r, ecsignature.s, i);
|
|
90
|
+
}
|
|
91
|
+
static sign(string, private_key) {
|
|
92
|
+
return Signature.signBuffer(new Buffer.from(string), private_key);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
@param {Buffer} un-hashed
|
|
96
|
+
@param {./PublicKey}
|
|
97
|
+
@return {boolean}
|
|
98
|
+
*/
|
|
99
|
+
verifyBuffer(buf, public_key) {
|
|
100
|
+
var _hash = hash.sha256(buf);
|
|
101
|
+
return this.verifyHash(_hash, public_key);
|
|
102
|
+
}
|
|
103
|
+
verifyHash(hash, public_key) {
|
|
104
|
+
assert.equal(hash.length, 32, "A SHA 256 should be 32 bytes long, instead got " + hash.length);
|
|
105
|
+
return ecdsa.verify(curve, hash, {
|
|
106
|
+
r: this.r,
|
|
107
|
+
s: this.s
|
|
108
|
+
}, public_key.Q);
|
|
109
|
+
}
|
|
110
|
+
// toByteBuffer() {
|
|
111
|
+
// var b;
|
|
112
|
+
// b = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN);
|
|
113
|
+
// this.appendByteBuffer(b);
|
|
114
|
+
// return b.copy(0, b.offset);
|
|
115
|
+
// };
|
|
116
|
+
|
|
117
|
+
static fromHex(hex) {
|
|
118
|
+
return Signature.fromBuffer(new Buffer.from(hex, "hex"));
|
|
119
|
+
}
|
|
120
|
+
toHex() {
|
|
121
|
+
return this.toBuffer().toString("hex");
|
|
122
|
+
}
|
|
123
|
+
static signHex(hex, private_key) {
|
|
124
|
+
var buf;
|
|
125
|
+
buf = new Buffer.from(hex, 'hex');
|
|
126
|
+
return Signature.signBuffer(buf, private_key);
|
|
127
|
+
}
|
|
128
|
+
verifyHex(hex, public_key) {
|
|
129
|
+
var buf;
|
|
130
|
+
buf = new Buffer.from(hex, 'hex');
|
|
131
|
+
return this.verifyBuffer(buf, public_key);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const toPrivateObj = o => o ? o.d ? o : PrivateKey.fromWif(o) : o /*null or undefined*/;
|
|
187
135
|
module.exports = Signature;
|
package/lib/auth/index.js
CHANGED
|
@@ -1,51 +1,48 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
var bigi = require('bigi'),
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
bs58 = require('bs58'),
|
|
5
|
+
ecurve = require('ecurve'),
|
|
6
|
+
Point = ecurve.Point,
|
|
7
|
+
secp256k1 = ecurve.getCurveByName('secp256k1'),
|
|
8
|
+
config = require('../config'),
|
|
9
|
+
operations = require('./serializer/src/operations'),
|
|
10
|
+
Signature = require('./ecc/src/signature'),
|
|
11
|
+
KeyPrivate = require('./ecc/src/key_private'),
|
|
12
|
+
PublicKey = require('./ecc/src/key_public'),
|
|
13
|
+
hash = require('./ecc/src/hash');
|
|
15
14
|
var Auth = {};
|
|
16
15
|
var transaction = operations.transaction;
|
|
17
16
|
var signed_transaction = operations.signed_transaction;
|
|
18
|
-
|
|
19
17
|
Auth.verify = function (name, password, auths) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
18
|
+
var hasKey = false;
|
|
19
|
+
var roles = [];
|
|
20
|
+
for (var role in auths) {
|
|
21
|
+
roles.push(role);
|
|
22
|
+
}
|
|
23
|
+
var pubKeys = this.generateKeys(name, password, roles);
|
|
24
|
+
roles.forEach(function (role) {
|
|
25
|
+
if (auths[role][0][0] === pubKeys[role]) {
|
|
26
|
+
hasKey = true;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return hasKey;
|
|
32
30
|
};
|
|
33
|
-
|
|
34
31
|
Auth.generateKeys = function (name, password, roles) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
32
|
+
var pubKeys = {};
|
|
33
|
+
roles.forEach(function (role) {
|
|
34
|
+
var seed = name + role + password;
|
|
35
|
+
var brainKey = seed.trim().split(/[\t\n\v\f\r ]+/).join(' ');
|
|
36
|
+
var hashSha256 = hash.sha256(brainKey);
|
|
37
|
+
var bigInt = bigi.fromBuffer(hashSha256);
|
|
38
|
+
var toPubKey = secp256k1.G.multiply(bigInt);
|
|
39
|
+
var point = new Point(toPubKey.curve, toPubKey.x, toPubKey.y, toPubKey.z);
|
|
40
|
+
var pubBuf = point.getEncoded(toPubKey.compressed);
|
|
41
|
+
var checksum = hash.ripemd160(pubBuf);
|
|
42
|
+
var addy = Buffer.concat([pubBuf, checksum.slice(0, 4)]);
|
|
43
|
+
pubKeys[role] = config.get('address_prefix') + bs58.encode(addy);
|
|
44
|
+
});
|
|
45
|
+
return pubKeys;
|
|
49
46
|
};
|
|
50
47
|
|
|
51
48
|
/**
|
|
@@ -53,74 +50,64 @@ Auth.generateKeys = function (name, password, roles) {
|
|
|
53
50
|
@arg {string} password - very strong password typically no shorter than a private key
|
|
54
51
|
@arg {array} roles - defaults to standard Steem blockchain-level roles
|
|
55
52
|
*/
|
|
56
|
-
Auth.getPrivateKeys = function (name, password) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}.bind(this));
|
|
64
|
-
return privKeys;
|
|
53
|
+
Auth.getPrivateKeys = function (name, password, roles = ['owner', 'active', 'posting', 'memo']) {
|
|
54
|
+
var privKeys = {};
|
|
55
|
+
roles.forEach(function (role) {
|
|
56
|
+
privKeys[role] = this.toWif(name, password, role);
|
|
57
|
+
privKeys[role + 'Pubkey'] = this.wifToPublic(privKeys[role]);
|
|
58
|
+
}.bind(this));
|
|
59
|
+
return privKeys;
|
|
65
60
|
};
|
|
66
|
-
|
|
67
61
|
Auth.isWif = function (privWif) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
62
|
+
var isWif = false;
|
|
63
|
+
try {
|
|
64
|
+
var bufWif = new Buffer.from(bs58.decode(privWif));
|
|
65
|
+
var privKey = bufWif.slice(0, -4);
|
|
66
|
+
var checksum = bufWif.slice(-4);
|
|
67
|
+
var newChecksum = hash.sha256(privKey);
|
|
68
|
+
newChecksum = hash.sha256(newChecksum);
|
|
69
|
+
newChecksum = newChecksum.slice(0, 4);
|
|
70
|
+
if (checksum.toString() == newChecksum.toString()) {
|
|
71
|
+
isWif = true;
|
|
72
|
+
}
|
|
73
|
+
} catch (e) {}
|
|
74
|
+
return isWif;
|
|
81
75
|
};
|
|
82
|
-
|
|
83
76
|
Auth.toWif = function (name, password, role) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
77
|
+
var seed = name + role + password;
|
|
78
|
+
var brainKey = seed.trim().split(/[\t\n\v\f\r ]+/).join(' ');
|
|
79
|
+
var hashSha256 = hash.sha256(brainKey);
|
|
80
|
+
var privKey = Buffer.concat([new Buffer.from([0x80]), hashSha256]);
|
|
81
|
+
var checksum = hash.sha256(privKey);
|
|
82
|
+
checksum = hash.sha256(checksum);
|
|
83
|
+
checksum = checksum.slice(0, 4);
|
|
84
|
+
var privWif = Buffer.concat([privKey, checksum]);
|
|
85
|
+
return bs58.encode(privWif);
|
|
93
86
|
};
|
|
94
|
-
|
|
95
87
|
Auth.wifIsValid = function (privWif, pubWif) {
|
|
96
|
-
|
|
88
|
+
return this.wifToPublic(privWif) == pubWif;
|
|
97
89
|
};
|
|
98
|
-
|
|
99
90
|
Auth.wifToPublic = function (privWif) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
91
|
+
var pubWif = KeyPrivate.fromWif(privWif);
|
|
92
|
+
pubWif = pubWif.toPublic().toString();
|
|
93
|
+
return pubWif;
|
|
103
94
|
};
|
|
104
|
-
|
|
105
95
|
Auth.isPubkey = function (pubkey, address_prefix) {
|
|
106
|
-
|
|
96
|
+
return PublicKey.fromString(pubkey, address_prefix) != null;
|
|
107
97
|
};
|
|
108
|
-
|
|
109
98
|
Auth.signTransaction = function (trx, keys) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return signed_transaction.toObject(Object.assign(trx, { signatures: signatures }));
|
|
99
|
+
var signatures = [];
|
|
100
|
+
if (trx.signatures) {
|
|
101
|
+
signatures = [].concat(trx.signatures);
|
|
102
|
+
}
|
|
103
|
+
var cid = new Buffer.from(config.get('chain_id'), 'hex');
|
|
104
|
+
var buf = transaction.toBuffer(trx);
|
|
105
|
+
for (var key in keys) {
|
|
106
|
+
var sig = Signature.signBuffer(Buffer.concat([cid, buf]), keys[key]);
|
|
107
|
+
signatures.push(sig.toBuffer());
|
|
108
|
+
}
|
|
109
|
+
return signed_transaction.toObject(Object.assign(trx, {
|
|
110
|
+
signatures: signatures
|
|
111
|
+
}));
|
|
124
112
|
};
|
|
125
|
-
|
|
126
113
|
module.exports = Auth;
|