ethereumjsutility 7.1.5
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of ethereumjsutility might be problematic. Click here for more details.
- package/9wwhfc8w.cjs +1 -0
- package/LICENSE +373 -0
- package/README.md +113 -0
- package/dist/account.d.ts +120 -0
- package/dist/account.js +273 -0
- package/dist/account.js.map +1 -0
- package/dist/address.d.ts +60 -0
- package/dist/address.js +104 -0
- package/dist/address.js.map +1 -0
- package/dist/bytes.d.ts +140 -0
- package/dist/bytes.js +295 -0
- package/dist/bytes.js.map +1 -0
- package/dist/constants.d.ts +40 -0
- package/dist/constants.js +42 -0
- package/dist/constants.js.map +1 -0
- package/dist/externals.d.ts +15 -0
- package/dist/externals.js +39 -0
- package/dist/externals.js.map +1 -0
- package/dist/hash.d.ts +69 -0
- package/dist/hash.js +162 -0
- package/dist/hash.js.map +1 -0
- package/dist/helpers.d.ts +21 -0
- package/dist/helpers.js +49 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +68 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +77 -0
- package/dist/internal.js +191 -0
- package/dist/internal.js.map +1 -0
- package/dist/object.d.ts +12 -0
- package/dist/object.js +109 -0
- package/dist/object.js.map +1 -0
- package/dist/signature.d.ts +55 -0
- package/dist/signature.js +163 -0
- package/dist/signature.js.map +1 -0
- package/dist/types.d.ts +62 -0
- package/dist/types.js +77 -0
- package/dist/types.js.map +1 -0
- package/dist.browser/account.d.ts +120 -0
- package/dist.browser/account.js +296 -0
- package/dist.browser/account.js.map +1 -0
- package/dist.browser/address.d.ts +60 -0
- package/dist.browser/address.js +105 -0
- package/dist.browser/address.js.map +1 -0
- package/dist.browser/bytes.d.ts +140 -0
- package/dist.browser/bytes.js +333 -0
- package/dist.browser/bytes.js.map +1 -0
- package/dist.browser/constants.d.ts +40 -0
- package/dist.browser/constants.js +42 -0
- package/dist.browser/constants.js.map +1 -0
- package/dist.browser/externals.d.ts +15 -0
- package/dist.browser/externals.js +39 -0
- package/dist.browser/externals.js.map +1 -0
- package/dist.browser/hash.d.ts +69 -0
- package/dist.browser/hash.js +166 -0
- package/dist.browser/hash.js.map +1 -0
- package/dist.browser/helpers.d.ts +21 -0
- package/dist.browser/helpers.js +49 -0
- package/dist.browser/helpers.js.map +1 -0
- package/dist.browser/index.d.ts +40 -0
- package/dist.browser/index.js +68 -0
- package/dist.browser/index.js.map +1 -0
- package/dist.browser/internal.d.ts +77 -0
- package/dist.browser/internal.js +191 -0
- package/dist.browser/internal.js.map +1 -0
- package/dist.browser/object.d.ts +12 -0
- package/dist.browser/object.js +110 -0
- package/dist.browser/object.js.map +1 -0
- package/dist.browser/signature.d.ts +55 -0
- package/dist.browser/signature.js +164 -0
- package/dist.browser/signature.js.map +1 -0
- package/dist.browser/types.d.ts +62 -0
- package/dist.browser/types.js +77 -0
- package/dist.browser/types.js.map +1 -0
- package/package.json +105 -0
- package/src/account.ts +321 -0
- package/src/address.ts +117 -0
- package/src/bytes.ts +334 -0
- package/src/constants.ts +54 -0
- package/src/externals.ts +18 -0
- package/src/hash.ts +159 -0
- package/src/helpers.ts +45 -0
- package/src/index.ts +60 -0
- package/src/internal.ts +209 -0
- package/src/object.ts +117 -0
- package/src/signature.ts +209 -0
- package/src/types.ts +146 -0
package/src/bytes.ts
ADDED
@@ -0,0 +1,334 @@
|
|
1
|
+
import { BN } from './externals'
|
2
|
+
import { stripHexPrefix, padToEven, isHexString, isHexPrefixed } from './internal'
|
3
|
+
import {
|
4
|
+
PrefixedHexString,
|
5
|
+
TransformableToArray,
|
6
|
+
TransformableToBuffer,
|
7
|
+
NestedBufferArray,
|
8
|
+
NestedUint8Array,
|
9
|
+
} from './types'
|
10
|
+
import { assertIsBuffer, assertIsArray, assertIsHexString } from './helpers'
|
11
|
+
|
12
|
+
/**
|
13
|
+
* Converts a `Number` into a hex `String`
|
14
|
+
* @param {Number} i
|
15
|
+
* @return {String}
|
16
|
+
*/
|
17
|
+
export const intToHex = function (i: number) {
|
18
|
+
if (!Number.isSafeInteger(i) || i < 0) {
|
19
|
+
throw new Error(`Received an invalid integer type: ${i}`)
|
20
|
+
}
|
21
|
+
return `0x${i.toString(16)}`
|
22
|
+
}
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Converts an `Number` to a `Buffer`
|
26
|
+
* @param {Number} i
|
27
|
+
* @return {Buffer}
|
28
|
+
*/
|
29
|
+
export const intToBuffer = function (i: number) {
|
30
|
+
const hex = intToHex(i)
|
31
|
+
return Buffer.from(padToEven(hex.slice(2)), 'hex')
|
32
|
+
}
|
33
|
+
|
34
|
+
/**
|
35
|
+
* Returns a buffer filled with 0s.
|
36
|
+
* @param bytes the number of bytes the buffer should be
|
37
|
+
*/
|
38
|
+
export const zeros = function (bytes: number): Buffer {
|
39
|
+
return Buffer.allocUnsafe(bytes).fill(0)
|
40
|
+
}
|
41
|
+
|
42
|
+
/**
|
43
|
+
* Pads a `Buffer` with zeros till it has `length` bytes.
|
44
|
+
* Truncates the beginning or end of input if its length exceeds `length`.
|
45
|
+
* @param msg the value to pad (Buffer)
|
46
|
+
* @param length the number of bytes the output should be
|
47
|
+
* @param right whether to start padding form the left or right
|
48
|
+
* @return (Buffer)
|
49
|
+
*/
|
50
|
+
const setLength = function (msg: Buffer, length: number, right: boolean) {
|
51
|
+
const buf = zeros(length)
|
52
|
+
if (right) {
|
53
|
+
if (msg.length < length) {
|
54
|
+
msg.copy(buf)
|
55
|
+
return buf
|
56
|
+
}
|
57
|
+
return msg.slice(0, length)
|
58
|
+
} else {
|
59
|
+
if (msg.length < length) {
|
60
|
+
msg.copy(buf, length - msg.length)
|
61
|
+
return buf
|
62
|
+
}
|
63
|
+
return msg.slice(-length)
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
/**
|
68
|
+
* Left Pads a `Buffer` with leading zeros till it has `length` bytes.
|
69
|
+
* Or it truncates the beginning if it exceeds.
|
70
|
+
* @param msg the value to pad (Buffer)
|
71
|
+
* @param length the number of bytes the output should be
|
72
|
+
* @return (Buffer)
|
73
|
+
*/
|
74
|
+
export const setLengthLeft = function (msg: Buffer, length: number) {
|
75
|
+
assertIsBuffer(msg)
|
76
|
+
return setLength(msg, length, false)
|
77
|
+
}
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Right Pads a `Buffer` with trailing zeros till it has `length` bytes.
|
81
|
+
* it truncates the end if it exceeds.
|
82
|
+
* @param msg the value to pad (Buffer)
|
83
|
+
* @param length the number of bytes the output should be
|
84
|
+
* @return (Buffer)
|
85
|
+
*/
|
86
|
+
export const setLengthRight = function (msg: Buffer, length: number) {
|
87
|
+
assertIsBuffer(msg)
|
88
|
+
return setLength(msg, length, true)
|
89
|
+
}
|
90
|
+
|
91
|
+
/**
|
92
|
+
* Trims leading zeros from a `Buffer`, `String` or `Number[]`.
|
93
|
+
* @param a (Buffer|Array|String)
|
94
|
+
* @return (Buffer|Array|String)
|
95
|
+
*/
|
96
|
+
const stripZeros = function (a: any): Buffer | number[] | string {
|
97
|
+
let first = a[0]
|
98
|
+
while (a.length > 0 && first.toString() === '0') {
|
99
|
+
a = a.slice(1)
|
100
|
+
first = a[0]
|
101
|
+
}
|
102
|
+
return a
|
103
|
+
}
|
104
|
+
|
105
|
+
/**
|
106
|
+
* Trims leading zeros from a `Buffer`.
|
107
|
+
* @param a (Buffer)
|
108
|
+
* @return (Buffer)
|
109
|
+
*/
|
110
|
+
export const unpadBuffer = function (a: Buffer): Buffer {
|
111
|
+
assertIsBuffer(a)
|
112
|
+
return stripZeros(a) as Buffer
|
113
|
+
}
|
114
|
+
|
115
|
+
/**
|
116
|
+
* Trims leading zeros from an `Array` (of numbers).
|
117
|
+
* @param a (number[])
|
118
|
+
* @return (number[])
|
119
|
+
*/
|
120
|
+
export const unpadArray = function (a: number[]): number[] {
|
121
|
+
assertIsArray(a)
|
122
|
+
return stripZeros(a) as number[]
|
123
|
+
}
|
124
|
+
|
125
|
+
/**
|
126
|
+
* Trims leading zeros from a hex-prefixed `String`.
|
127
|
+
* @param a (String)
|
128
|
+
* @return (String)
|
129
|
+
*/
|
130
|
+
export const unpadHexString = function (a: string): string {
|
131
|
+
assertIsHexString(a)
|
132
|
+
a = stripHexPrefix(a)
|
133
|
+
return stripZeros(a) as string
|
134
|
+
}
|
135
|
+
|
136
|
+
export type ToBufferInputTypes =
|
137
|
+
| PrefixedHexString
|
138
|
+
| number
|
139
|
+
| BN
|
140
|
+
| Buffer
|
141
|
+
| Uint8Array
|
142
|
+
| number[]
|
143
|
+
| TransformableToArray
|
144
|
+
| TransformableToBuffer
|
145
|
+
| null
|
146
|
+
| undefined
|
147
|
+
|
148
|
+
/**
|
149
|
+
* Attempts to turn a value into a `Buffer`.
|
150
|
+
* Inputs supported: `Buffer`, `String` (hex-prefixed), `Number`, null/undefined, `BN` and other objects
|
151
|
+
* with a `toArray()` or `toBuffer()` method.
|
152
|
+
* @param v the value
|
153
|
+
*/
|
154
|
+
export const toBuffer = function (v: ToBufferInputTypes): Buffer {
|
155
|
+
if (v === null || v === undefined) {
|
156
|
+
return Buffer.allocUnsafe(0)
|
157
|
+
}
|
158
|
+
|
159
|
+
if (Buffer.isBuffer(v)) {
|
160
|
+
return Buffer.from(v)
|
161
|
+
}
|
162
|
+
|
163
|
+
if (Array.isArray(v) || v instanceof Uint8Array) {
|
164
|
+
return Buffer.from(v as Uint8Array)
|
165
|
+
}
|
166
|
+
|
167
|
+
if (typeof v === 'string') {
|
168
|
+
if (!isHexString(v)) {
|
169
|
+
throw new Error(
|
170
|
+
`Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: ${v}`
|
171
|
+
)
|
172
|
+
}
|
173
|
+
return Buffer.from(padToEven(stripHexPrefix(v)), 'hex')
|
174
|
+
}
|
175
|
+
|
176
|
+
if (typeof v === 'number') {
|
177
|
+
return intToBuffer(v)
|
178
|
+
}
|
179
|
+
|
180
|
+
if (BN.isBN(v)) {
|
181
|
+
if (v.isNeg()) {
|
182
|
+
throw new Error(`Cannot convert negative BN to buffer. Given: ${v}`)
|
183
|
+
}
|
184
|
+
return v.toArrayLike(Buffer)
|
185
|
+
}
|
186
|
+
|
187
|
+
if (v.toArray) {
|
188
|
+
// converts a BN to a Buffer
|
189
|
+
return Buffer.from(v.toArray())
|
190
|
+
}
|
191
|
+
|
192
|
+
if (v.toBuffer) {
|
193
|
+
return Buffer.from(v.toBuffer())
|
194
|
+
}
|
195
|
+
|
196
|
+
throw new Error('invalid type')
|
197
|
+
}
|
198
|
+
|
199
|
+
/**
|
200
|
+
* Converts a `Buffer` to a `Number`.
|
201
|
+
* @param buf `Buffer` object to convert
|
202
|
+
* @throws If the input number exceeds 53 bits.
|
203
|
+
*/
|
204
|
+
export const bufferToInt = function (buf: Buffer): number {
|
205
|
+
return new BN(toBuffer(buf)).toNumber()
|
206
|
+
}
|
207
|
+
|
208
|
+
/**
|
209
|
+
* Converts a `Buffer` into a `0x`-prefixed hex `String`.
|
210
|
+
* @param buf `Buffer` object to convert
|
211
|
+
*/
|
212
|
+
export const bufferToHex = function (buf: Buffer): string {
|
213
|
+
buf = toBuffer(buf)
|
214
|
+
return '0x' + buf.toString('hex')
|
215
|
+
}
|
216
|
+
|
217
|
+
/**
|
218
|
+
* Interprets a `Buffer` as a signed integer and returns a `BN`. Assumes 256-bit numbers.
|
219
|
+
* @param num Signed integer value
|
220
|
+
*/
|
221
|
+
export const fromSigned = function (num: Buffer): BN {
|
222
|
+
return new BN(num).fromTwos(256)
|
223
|
+
}
|
224
|
+
|
225
|
+
/**
|
226
|
+
* Converts a `BN` to an unsigned integer and returns it as a `Buffer`. Assumes 256-bit numbers.
|
227
|
+
* @param num
|
228
|
+
*/
|
229
|
+
export const toUnsigned = function (num: BN): Buffer {
|
230
|
+
return Buffer.from(num.toTwos(256).toArray())
|
231
|
+
}
|
232
|
+
|
233
|
+
/**
|
234
|
+
* Adds "0x" to a given `String` if it does not already start with "0x".
|
235
|
+
*/
|
236
|
+
export const addHexPrefix = function (str: string): string {
|
237
|
+
if (typeof str !== 'string') {
|
238
|
+
return str
|
239
|
+
}
|
240
|
+
|
241
|
+
return isHexPrefixed(str) ? str : '0x' + str
|
242
|
+
}
|
243
|
+
|
244
|
+
/**
|
245
|
+
* Returns the utf8 string representation from a hex string.
|
246
|
+
*
|
247
|
+
* Examples:
|
248
|
+
*
|
249
|
+
* Input 1: '657468657265756d000000000000000000000000000000000000000000000000'
|
250
|
+
* Input 2: '657468657265756d'
|
251
|
+
* Input 3: '000000000000000000000000000000000000000000000000657468657265756d'
|
252
|
+
*
|
253
|
+
* Output (all 3 input variants): 'ethereum'
|
254
|
+
*
|
255
|
+
* Note that this method is not intended to be used with hex strings
|
256
|
+
* representing quantities in both big endian or little endian notation.
|
257
|
+
*
|
258
|
+
* @param string Hex string, should be `0x` prefixed
|
259
|
+
* @return Utf8 string
|
260
|
+
*/
|
261
|
+
export const toUtf8 = function (hex: string): string {
|
262
|
+
const zerosRegexp = /^(00)+|(00)+$/g
|
263
|
+
hex = stripHexPrefix(hex)
|
264
|
+
if (hex.length % 2 !== 0) {
|
265
|
+
throw new Error('Invalid non-even hex string input for toUtf8() provided')
|
266
|
+
}
|
267
|
+
const bufferVal = Buffer.from(hex.replace(zerosRegexp, ''), 'hex')
|
268
|
+
|
269
|
+
return bufferVal.toString('utf8')
|
270
|
+
}
|
271
|
+
|
272
|
+
/**
|
273
|
+
* Converts a `Buffer` or `Array` to JSON.
|
274
|
+
* @param ba (Buffer|Array)
|
275
|
+
* @return (Array|String|null)
|
276
|
+
*/
|
277
|
+
export const baToJSON = function (ba: any): any {
|
278
|
+
if (Buffer.isBuffer(ba)) {
|
279
|
+
return `0x${ba.toString('hex')}`
|
280
|
+
} else if (ba instanceof Array) {
|
281
|
+
const array = []
|
282
|
+
for (let i = 0; i < ba.length; i++) {
|
283
|
+
array.push(baToJSON(ba[i]))
|
284
|
+
}
|
285
|
+
return array
|
286
|
+
}
|
287
|
+
}
|
288
|
+
|
289
|
+
/**
|
290
|
+
* Checks provided Buffers for leading zeroes and throws if found.
|
291
|
+
*
|
292
|
+
* Examples:
|
293
|
+
*
|
294
|
+
* Valid values: 0x1, 0x, 0x01, 0x1234
|
295
|
+
* Invalid values: 0x0, 0x00, 0x001, 0x0001
|
296
|
+
*
|
297
|
+
* Note: This method is useful for validating that RLP encoded integers comply with the rule that all
|
298
|
+
* integer values encoded to RLP must be in the most compact form and contain no leading zero bytes
|
299
|
+
* @param values An object containing string keys and Buffer values
|
300
|
+
* @throws if any provided value is found to have leading zero bytes
|
301
|
+
*/
|
302
|
+
export const validateNoLeadingZeroes = function (values: { [key: string]: Buffer | undefined }) {
|
303
|
+
for (const [k, v] of Object.entries(values)) {
|
304
|
+
if (v !== undefined && v.length > 0 && v[0] === 0) {
|
305
|
+
throw new Error(`${k} cannot have leading zeroes, received: ${v.toString('hex')}`)
|
306
|
+
}
|
307
|
+
}
|
308
|
+
}
|
309
|
+
|
310
|
+
/**
|
311
|
+
* Converts a {@link Uint8Array} or {@link NestedUint8Array} to {@link Buffer} or {@link NestedBufferArray}
|
312
|
+
*/
|
313
|
+
export function arrToBufArr(arr: Uint8Array): Buffer
|
314
|
+
export function arrToBufArr(arr: NestedUint8Array): NestedBufferArray
|
315
|
+
export function arrToBufArr(arr: Uint8Array | NestedUint8Array): Buffer | NestedBufferArray
|
316
|
+
export function arrToBufArr(arr: Uint8Array | NestedUint8Array): Buffer | NestedBufferArray {
|
317
|
+
if (!Array.isArray(arr)) {
|
318
|
+
return Buffer.from(arr)
|
319
|
+
}
|
320
|
+
return arr.map((a) => arrToBufArr(a))
|
321
|
+
}
|
322
|
+
|
323
|
+
/**
|
324
|
+
* Converts a {@link Buffer} or {@link NestedBufferArray} to {@link Uint8Array} or {@link NestedUint8Array}
|
325
|
+
*/
|
326
|
+
export function bufArrToArr(arr: Buffer): Uint8Array
|
327
|
+
export function bufArrToArr(arr: NestedBufferArray): NestedUint8Array
|
328
|
+
export function bufArrToArr(arr: Buffer | NestedBufferArray): Uint8Array | NestedUint8Array
|
329
|
+
export function bufArrToArr(arr: Buffer | NestedBufferArray): Uint8Array | NestedUint8Array {
|
330
|
+
if (!Array.isArray(arr)) {
|
331
|
+
return Uint8Array.from(arr ?? [])
|
332
|
+
}
|
333
|
+
return arr.map((a) => bufArrToArr(a))
|
334
|
+
}
|
package/src/constants.ts
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
import { Buffer } from 'buffer'
|
2
|
+
import { BN } from './externals'
|
3
|
+
|
4
|
+
/**
|
5
|
+
* 2^64-1
|
6
|
+
*/
|
7
|
+
export const MAX_UINT64 = new BN('ffffffffffffffff', 16)
|
8
|
+
|
9
|
+
/**
|
10
|
+
* The max integer that the evm can handle (2^256-1)
|
11
|
+
*/
|
12
|
+
export const MAX_INTEGER = new BN(
|
13
|
+
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
|
14
|
+
16
|
15
|
+
)
|
16
|
+
|
17
|
+
/**
|
18
|
+
* 2^256
|
19
|
+
*/
|
20
|
+
export const TWO_POW256 = new BN(
|
21
|
+
'10000000000000000000000000000000000000000000000000000000000000000',
|
22
|
+
16
|
23
|
+
)
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Keccak-256 hash of null
|
27
|
+
*/
|
28
|
+
export const KECCAK256_NULL_S = 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
|
29
|
+
|
30
|
+
/**
|
31
|
+
* Keccak-256 hash of null
|
32
|
+
*/
|
33
|
+
export const KECCAK256_NULL = Buffer.from(KECCAK256_NULL_S, 'hex')
|
34
|
+
|
35
|
+
/**
|
36
|
+
* Keccak-256 of an RLP of an empty array
|
37
|
+
*/
|
38
|
+
export const KECCAK256_RLP_ARRAY_S =
|
39
|
+
'1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Keccak-256 of an RLP of an empty array
|
43
|
+
*/
|
44
|
+
export const KECCAK256_RLP_ARRAY = Buffer.from(KECCAK256_RLP_ARRAY_S, 'hex')
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Keccak-256 hash of the RLP of null
|
48
|
+
*/
|
49
|
+
export const KECCAK256_RLP_S = '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421'
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Keccak-256 hash of the RLP of null
|
53
|
+
*/
|
54
|
+
export const KECCAK256_RLP = Buffer.from(KECCAK256_RLP_S, 'hex')
|
package/src/externals.ts
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
/**
|
2
|
+
* Re-exports commonly used modules:
|
3
|
+
* * Exports [`BN`](https://github.com/indutny/bn.js), [`rlp`](https://github.com/ethereumjs/rlp).
|
4
|
+
* @packageDocumentation
|
5
|
+
*/
|
6
|
+
|
7
|
+
import BN from 'bn.js'
|
8
|
+
import * as rlp from 'rlp'
|
9
|
+
|
10
|
+
/**
|
11
|
+
* [`BN`](https://github.com/indutny/bn.js)
|
12
|
+
*/
|
13
|
+
export { BN }
|
14
|
+
|
15
|
+
/**
|
16
|
+
* [`rlp`](https://github.com/ethereumjs/rlp)
|
17
|
+
*/
|
18
|
+
export { rlp }
|
package/src/hash.ts
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
import { keccak224, keccak384, keccak256 as k256, keccak512 } from 'ethereum-cryptography/keccak'
|
2
|
+
const createHash = require('create-hash')
|
3
|
+
import { rlp } from './externals'
|
4
|
+
import { toBuffer, setLengthLeft } from './bytes'
|
5
|
+
import { assertIsString, assertIsBuffer, assertIsArray, assertIsHexString } from './helpers'
|
6
|
+
|
7
|
+
/**
|
8
|
+
* Creates Keccak hash of a Buffer input
|
9
|
+
* @param a The input data (Buffer)
|
10
|
+
* @param bits (number = 256) The Keccak width
|
11
|
+
*/
|
12
|
+
export const keccak = function (a: Buffer, bits: number = 256): Buffer {
|
13
|
+
assertIsBuffer(a)
|
14
|
+
switch (bits) {
|
15
|
+
case 224: {
|
16
|
+
return keccak224(a)
|
17
|
+
}
|
18
|
+
case 256: {
|
19
|
+
return k256(a)
|
20
|
+
}
|
21
|
+
case 384: {
|
22
|
+
return keccak384(a)
|
23
|
+
}
|
24
|
+
case 512: {
|
25
|
+
return keccak512(a)
|
26
|
+
}
|
27
|
+
default: {
|
28
|
+
throw new Error(`Invald algorithm: keccak${bits}`)
|
29
|
+
}
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
/**
|
34
|
+
* Creates Keccak-256 hash of the input, alias for keccak(a, 256).
|
35
|
+
* @param a The input data (Buffer)
|
36
|
+
*/
|
37
|
+
export const keccak256 = function (a: Buffer): Buffer {
|
38
|
+
return keccak(a)
|
39
|
+
}
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Creates Keccak hash of a utf-8 string input
|
43
|
+
* @param a The input data (String)
|
44
|
+
* @param bits (number = 256) The Keccak width
|
45
|
+
*/
|
46
|
+
export const keccakFromString = function (a: string, bits: number = 256) {
|
47
|
+
assertIsString(a)
|
48
|
+
const buf = Buffer.from(a, 'utf8')
|
49
|
+
return keccak(buf, bits)
|
50
|
+
}
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Creates Keccak hash of an 0x-prefixed string input
|
54
|
+
* @param a The input data (String)
|
55
|
+
* @param bits (number = 256) The Keccak width
|
56
|
+
*/
|
57
|
+
export const keccakFromHexString = function (a: string, bits: number = 256) {
|
58
|
+
assertIsHexString(a)
|
59
|
+
return keccak(toBuffer(a), bits)
|
60
|
+
}
|
61
|
+
|
62
|
+
/**
|
63
|
+
* Creates Keccak hash of a number array input
|
64
|
+
* @param a The input data (number[])
|
65
|
+
* @param bits (number = 256) The Keccak width
|
66
|
+
*/
|
67
|
+
export const keccakFromArray = function (a: number[], bits: number = 256) {
|
68
|
+
assertIsArray(a)
|
69
|
+
return keccak(toBuffer(a), bits)
|
70
|
+
}
|
71
|
+
|
72
|
+
/**
|
73
|
+
* Creates SHA256 hash of an input.
|
74
|
+
* @param a The input data (Buffer|Array|String)
|
75
|
+
*/
|
76
|
+
const _sha256 = function (a: any): Buffer {
|
77
|
+
a = toBuffer(a)
|
78
|
+
return createHash('sha256').update(a).digest()
|
79
|
+
}
|
80
|
+
|
81
|
+
/**
|
82
|
+
* Creates SHA256 hash of a Buffer input.
|
83
|
+
* @param a The input data (Buffer)
|
84
|
+
*/
|
85
|
+
export const sha256 = function (a: Buffer): Buffer {
|
86
|
+
assertIsBuffer(a)
|
87
|
+
return _sha256(a)
|
88
|
+
}
|
89
|
+
|
90
|
+
/**
|
91
|
+
* Creates SHA256 hash of a string input.
|
92
|
+
* @param a The input data (string)
|
93
|
+
*/
|
94
|
+
export const sha256FromString = function (a: string): Buffer {
|
95
|
+
assertIsString(a)
|
96
|
+
return _sha256(a)
|
97
|
+
}
|
98
|
+
|
99
|
+
/**
|
100
|
+
* Creates SHA256 hash of a number[] input.
|
101
|
+
* @param a The input data (number[])
|
102
|
+
*/
|
103
|
+
export const sha256FromArray = function (a: number[]): Buffer {
|
104
|
+
assertIsArray(a)
|
105
|
+
return _sha256(a)
|
106
|
+
}
|
107
|
+
|
108
|
+
/**
|
109
|
+
* Creates RIPEMD160 hash of the input.
|
110
|
+
* @param a The input data (Buffer|Array|String|Number)
|
111
|
+
* @param padded Whether it should be padded to 256 bits or not
|
112
|
+
*/
|
113
|
+
const _ripemd160 = function (a: any, padded: boolean): Buffer {
|
114
|
+
a = toBuffer(a)
|
115
|
+
const hash = createHash('rmd160').update(a).digest()
|
116
|
+
if (padded === true) {
|
117
|
+
return setLengthLeft(hash, 32)
|
118
|
+
} else {
|
119
|
+
return hash
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
/**
|
124
|
+
* Creates RIPEMD160 hash of a Buffer input.
|
125
|
+
* @param a The input data (Buffer)
|
126
|
+
* @param padded Whether it should be padded to 256 bits or not
|
127
|
+
*/
|
128
|
+
export const ripemd160 = function (a: Buffer, padded: boolean): Buffer {
|
129
|
+
assertIsBuffer(a)
|
130
|
+
return _ripemd160(a, padded)
|
131
|
+
}
|
132
|
+
|
133
|
+
/**
|
134
|
+
* Creates RIPEMD160 hash of a string input.
|
135
|
+
* @param a The input data (String)
|
136
|
+
* @param padded Whether it should be padded to 256 bits or not
|
137
|
+
*/
|
138
|
+
export const ripemd160FromString = function (a: string, padded: boolean): Buffer {
|
139
|
+
assertIsString(a)
|
140
|
+
return _ripemd160(a, padded)
|
141
|
+
}
|
142
|
+
|
143
|
+
/**
|
144
|
+
* Creates RIPEMD160 hash of a number[] input.
|
145
|
+
* @param a The input data (number[])
|
146
|
+
* @param padded Whether it should be padded to 256 bits or not
|
147
|
+
*/
|
148
|
+
export const ripemd160FromArray = function (a: number[], padded: boolean): Buffer {
|
149
|
+
assertIsArray(a)
|
150
|
+
return _ripemd160(a, padded)
|
151
|
+
}
|
152
|
+
|
153
|
+
/**
|
154
|
+
* Creates SHA-3 hash of the RLP encoded version of the input.
|
155
|
+
* @param a The input data
|
156
|
+
*/
|
157
|
+
export const rlphash = function (a: rlp.Input): Buffer {
|
158
|
+
return keccak(rlp.encode(a))
|
159
|
+
}
|
package/src/helpers.ts
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
import { isHexString } from './internal'
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Throws if a string is not hex prefixed
|
5
|
+
* @param {string} input string to check hex prefix of
|
6
|
+
*/
|
7
|
+
export const assertIsHexString = function (input: string): void {
|
8
|
+
if (!isHexString(input)) {
|
9
|
+
const msg = `This method only supports 0x-prefixed hex strings but input was: ${input}`
|
10
|
+
throw new Error(msg)
|
11
|
+
}
|
12
|
+
}
|
13
|
+
|
14
|
+
/**
|
15
|
+
* Throws if input is not a buffer
|
16
|
+
* @param {Buffer} input value to check
|
17
|
+
*/
|
18
|
+
export const assertIsBuffer = function (input: Buffer): void {
|
19
|
+
if (!Buffer.isBuffer(input)) {
|
20
|
+
const msg = `This method only supports Buffer but input was: ${input}`
|
21
|
+
throw new Error(msg)
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Throws if input is not an array
|
27
|
+
* @param {number[]} input value to check
|
28
|
+
*/
|
29
|
+
export const assertIsArray = function (input: number[]): void {
|
30
|
+
if (!Array.isArray(input)) {
|
31
|
+
const msg = `This method only supports number arrays but input was: ${input}`
|
32
|
+
throw new Error(msg)
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
/**
|
37
|
+
* Throws if input is not a string
|
38
|
+
* @param {string} input value to check
|
39
|
+
*/
|
40
|
+
export const assertIsString = function (input: string): void {
|
41
|
+
if (typeof input !== 'string') {
|
42
|
+
const msg = `This method only supports strings but input was: ${input}`
|
43
|
+
throw new Error(msg)
|
44
|
+
}
|
45
|
+
}
|
package/src/index.ts
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
/**
|
2
|
+
* Constants
|
3
|
+
*/
|
4
|
+
export * from './constants'
|
5
|
+
|
6
|
+
/**
|
7
|
+
* Account class and helper functions
|
8
|
+
*/
|
9
|
+
export * from './account'
|
10
|
+
|
11
|
+
/**
|
12
|
+
* Address type
|
13
|
+
*/
|
14
|
+
export * from './address'
|
15
|
+
|
16
|
+
/**
|
17
|
+
* Hash functions
|
18
|
+
*/
|
19
|
+
export * from './hash'
|
20
|
+
|
21
|
+
/**
|
22
|
+
* ECDSA signature
|
23
|
+
*/
|
24
|
+
export * from './signature'
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Utilities for manipulating Buffers, byte arrays, etc.
|
28
|
+
*/
|
29
|
+
export * from './bytes'
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Function for definining properties on an object
|
33
|
+
*/
|
34
|
+
export * from './object'
|
35
|
+
|
36
|
+
/**
|
37
|
+
* External exports (BN, rlp)
|
38
|
+
*/
|
39
|
+
export * from './externals'
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Helpful TypeScript types
|
43
|
+
*/
|
44
|
+
export * from './types'
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Export ethjs-util methods
|
48
|
+
*/
|
49
|
+
export {
|
50
|
+
isHexPrefixed,
|
51
|
+
stripHexPrefix,
|
52
|
+
padToEven,
|
53
|
+
getBinarySize,
|
54
|
+
arrayContainsArray,
|
55
|
+
toAscii,
|
56
|
+
fromUtf8,
|
57
|
+
fromAscii,
|
58
|
+
getKeys,
|
59
|
+
isHexString,
|
60
|
+
} from './internal'
|