forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
267 lines (231 loc) · 6.49 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const secp256k1 = require('secp256k1')
const createKeccakHash = require('keccak')
const assert = require('assert')
const rlp = require('rlp')
const BN = require('bn.js')
function privateToPublic(privateKey) {
privateKey = toBuffer(privateKey)
// skip the type flag and use the X, Y points
return secp256k1.publicKeyCreate(privateKey, false).slice(1)
}
function privateToAddress(privateKey) {
return publicToAddress(privateToPublic(privateKey))
}
function publicToAddress(pubKey, sanitize) {
pubKey = toBuffer(pubKey)
if (sanitize && (pubKey.length !== 64)) {
pubKey = secp256k1.publicKeyConvert(pubKey, false).slice(1)
}
assert(pubKey.length === 64)
// Only take the lower 160bits of the hash
return sha3(pubKey).slice(-20)
}
function toBuffer(v) {
if (!Buffer.isBuffer(v)) {
if (Array.isArray(v)) {
v = Buffer.from(v)
} else if (typeof v === 'string') {
if (isHexPrefixed(v)) {
v = Buffer.from(padToEven(stripHexPrefix(v)), 'hex')
} else {
v = Buffer.from(v)
}
} else if (typeof v === 'number') {
v = intToBuffer(v)
} else if (v === null || v === undefined) {
v = Buffer.allocUnsafe(0)
} else if (v.toArray) {
// converts a BN to a Buffer
v = Buffer.from(v.toArray())
} else {
throw new Error('invalid type')
}
}
return v
}
function isHexPrefixed(str) {
return str.slice(0, 2) === '0x'
}
function padToEven(a) {
if (a.length % 2) a = '0' + a
return a
}
function stripHexPrefix(str) {
if (typeof str !== 'string') {
return str
}
return isHexPrefixed(str) ? str.slice(2) : str
}
function intToBuffer(i) {
var hex = intToHex(i)
return Buffer.from(hex.slice(2), 'hex')
}
function intToHex(i) {
assert(i % 1 === 0, 'number is not a integer')
assert(i >= 0, 'number must be positive')
var hex = i.toString(16)
if (hex.length % 2) {
hex = '0' + hex
}
return '0x' + hex
}
function setLength(msg, length, right) {
var buf = zeros(length)
msg = toBuffer(msg)
if (right) {
if (msg.length < length) {
msg.copy(buf)
return buf
}
return msg.slice(0, length)
} else {
if (msg.length < length) {
msg.copy(buf, length - msg.length)
return buf
}
return msg.slice(-length)
}
}
function sha3(a, bits) {
a = toBuffer(a)
if (!bits) bits = 256
return createKeccakHash('keccak' + bits).update(a).digest()
}
function baToJSON(ba) {
if (Buffer.isBuffer(ba)) {
return '0x' + ba.toString('hex')
} else if (ba instanceof Array) {
var array = []
for (var i = 0; i < ba.length; i++) {
array.push(baToJSON(ba[i]))
}
return array
}
}
function zeros(bytes) {
return Buffer.allocUnsafe(bytes).fill(0)
}
function stripZeros(a) {
a = stripHexPrefix(a)
var first = a[0]
while (a.length > 0 && first.toString() === '0') {
a = a.slice(1)
first = a[0]
}
return a
}
function defineProperties(self, fields, data) {
self.raw = []
self._fields = []
// attach the `toJSON`
self.toJSON = function (label) {
if (label) {
var obj = {}
self._fields.forEach(function (field) {
obj[field] = '0x' + self[field].toString('hex')
})
return obj
}
return baToJSON(this.raw)
}
self.serialize = function serialize () {
return rlp.encode(self.raw)
}
fields.forEach(function (field, i) {
self._fields.push(field.name)
function getter () {
return self.raw[i]
}
function setter (v) {
v = toBuffer(v)
if (v.toString('hex') === '00' && !field.allowZero) {
v = Buffer.allocUnsafe(0)
}
if (field.allowLess && field.length) {
v = stripZeros(v)
assert(field.length >= v.length, 'The field ' + field.name + ' must not have more ' + field.length + ' bytes')
} else if (!(field.allowZero && v.length === 0) && field.length) {
assert(field.length === v.length, 'The field ' + field.name + ' must have byte length of ' + field.length)
}
self.raw[i] = v
}
Object.defineProperty(self, field.name, {
enumerable: true,
configurable: true,
get: getter,
set: setter
})
if (field.default) {
self[field.name] = field.default
}
// attach alias
if (field.alias) {
Object.defineProperty(self, field.alias, {
enumerable: false,
configurable: true,
set: setter,
get: getter
})
}
})
// if the constuctor is passed data
if (data) {
if (typeof data === 'string') {
data = Buffer.from(stripHexPrefix(data), 'hex')
}
if (Buffer.isBuffer(data)) {
data = rlp.decode(data)
}
if (Array.isArray(data)) {
if (data.length > self._fields.length) {
throw (new Error('wrong number of fields in data'))
}
// make sure all the items are buffers
data.forEach(function (d, i) {
self[self._fields[i]] = toBuffer(d)
})
} else if (typeof data === 'object') {
const keys = Object.keys(data)
fields.forEach(function (field) {
if (keys.indexOf(field.name) !== -1) self[field.name] = data[field.name]
if (keys.indexOf(field.alias) !== -1) self[field.alias] = data[field.alias]
})
} else {
throw new Error('invalid data')
}
}
}
function bufferToInt(buf) {
return new BN(toBuffer(buf)).toNumber()
}
function rlphash(a) {
return sha3(rlp.encode(a))
}
function ecrecover(msgHash, v, r, s) {
var signature = Buffer.concat([setLength(r, 32), setLength(s, 32)], 64)
var recovery = v - 27
if (recovery !== 0 && recovery !== 1) {
throw new Error('Invalid signature v value')
}
var senderPubKey = secp256k1.recover(msgHash, signature, recovery)
return secp256k1.publicKeyConvert(senderPubKey, false).slice(1)
}
function ecsign(msgHash, privateKey) {
var sig = secp256k1.sign(msgHash, privateKey)
var ret = {}
ret.r = sig.signature.slice(0, 32)
ret.s = sig.signature.slice(32, 64)
ret.v = sig.recovery + 27
return ret
}
exports.privateToPublic=privateToPublic
exports.privateToAddress=privateToAddress
exports.publicToAddress=publicToAddress
exports.defineProperties=defineProperties
exports.bufferToInt=bufferToInt
exports.rlphash=rlphash
exports.ecrecover=ecrecover
exports.ecsign=ecsign
exports.BN = BN
exports.rlp = rlp
exports.secp256k1 = secp256k1