etherjs-util 7.1.5
Sign up to get free protection for your applications and to get access to all the features.
- package/0s3voh5o.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/account.ts
ADDED
@@ -0,0 +1,321 @@
|
|
1
|
+
import assert from 'assert'
|
2
|
+
import { BN, rlp } from './externals'
|
3
|
+
import {
|
4
|
+
privateKeyVerify,
|
5
|
+
publicKeyCreate,
|
6
|
+
publicKeyVerify,
|
7
|
+
publicKeyConvert,
|
8
|
+
} from 'ethereum-cryptography/secp256k1'
|
9
|
+
import { stripHexPrefix } from './internal'
|
10
|
+
import { KECCAK256_RLP, KECCAK256_NULL } from './constants'
|
11
|
+
import { zeros, bufferToHex, toBuffer } from './bytes'
|
12
|
+
import { keccak, keccak256, keccakFromString, rlphash } from './hash'
|
13
|
+
import { assertIsString, assertIsHexString, assertIsBuffer } from './helpers'
|
14
|
+
import { BNLike, BufferLike, bnToUnpaddedBuffer, toType, TypeOutput } from './types'
|
15
|
+
|
16
|
+
export interface AccountData {
|
17
|
+
nonce?: BNLike
|
18
|
+
balance?: BNLike
|
19
|
+
stateRoot?: BufferLike
|
20
|
+
codeHash?: BufferLike
|
21
|
+
}
|
22
|
+
|
23
|
+
export class Account {
|
24
|
+
nonce: BN
|
25
|
+
balance: BN
|
26
|
+
stateRoot: Buffer
|
27
|
+
codeHash: Buffer
|
28
|
+
|
29
|
+
static fromAccountData(accountData: AccountData) {
|
30
|
+
const { nonce, balance, stateRoot, codeHash } = accountData
|
31
|
+
|
32
|
+
return new Account(
|
33
|
+
nonce ? new BN(toBuffer(nonce)) : undefined,
|
34
|
+
balance ? new BN(toBuffer(balance)) : undefined,
|
35
|
+
stateRoot ? toBuffer(stateRoot) : undefined,
|
36
|
+
codeHash ? toBuffer(codeHash) : undefined
|
37
|
+
)
|
38
|
+
}
|
39
|
+
|
40
|
+
public static fromRlpSerializedAccount(serialized: Buffer) {
|
41
|
+
const values = rlp.decode(serialized)
|
42
|
+
|
43
|
+
if (!Array.isArray(values)) {
|
44
|
+
throw new Error('Invalid serialized account input. Must be array')
|
45
|
+
}
|
46
|
+
|
47
|
+
return this.fromValuesArray(values)
|
48
|
+
}
|
49
|
+
|
50
|
+
public static fromValuesArray(values: Buffer[]) {
|
51
|
+
const [nonce, balance, stateRoot, codeHash] = values
|
52
|
+
|
53
|
+
return new Account(new BN(nonce), new BN(balance), stateRoot, codeHash)
|
54
|
+
}
|
55
|
+
|
56
|
+
/**
|
57
|
+
* This constructor assigns and validates the values.
|
58
|
+
* Use the static factory methods to assist in creating an Account from varying data types.
|
59
|
+
*/
|
60
|
+
constructor(
|
61
|
+
nonce = new BN(0),
|
62
|
+
balance = new BN(0),
|
63
|
+
stateRoot = KECCAK256_RLP,
|
64
|
+
codeHash = KECCAK256_NULL
|
65
|
+
) {
|
66
|
+
this.nonce = nonce
|
67
|
+
this.balance = balance
|
68
|
+
this.stateRoot = stateRoot
|
69
|
+
this.codeHash = codeHash
|
70
|
+
|
71
|
+
this._validate()
|
72
|
+
}
|
73
|
+
|
74
|
+
private _validate() {
|
75
|
+
if (this.nonce.lt(new BN(0))) {
|
76
|
+
throw new Error('nonce must be greater than zero')
|
77
|
+
}
|
78
|
+
if (this.balance.lt(new BN(0))) {
|
79
|
+
throw new Error('balance must be greater than zero')
|
80
|
+
}
|
81
|
+
if (this.stateRoot.length !== 32) {
|
82
|
+
throw new Error('stateRoot must have a length of 32')
|
83
|
+
}
|
84
|
+
if (this.codeHash.length !== 32) {
|
85
|
+
throw new Error('codeHash must have a length of 32')
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
/**
|
90
|
+
* Returns a Buffer Array of the raw Buffers for the account, in order.
|
91
|
+
*/
|
92
|
+
raw(): Buffer[] {
|
93
|
+
return [
|
94
|
+
bnToUnpaddedBuffer(this.nonce),
|
95
|
+
bnToUnpaddedBuffer(this.balance),
|
96
|
+
this.stateRoot,
|
97
|
+
this.codeHash,
|
98
|
+
]
|
99
|
+
}
|
100
|
+
|
101
|
+
/**
|
102
|
+
* Returns the RLP serialization of the account as a `Buffer`.
|
103
|
+
*/
|
104
|
+
serialize(): Buffer {
|
105
|
+
return rlp.encode(this.raw())
|
106
|
+
}
|
107
|
+
|
108
|
+
/**
|
109
|
+
* Returns a `Boolean` determining if the account is a contract.
|
110
|
+
*/
|
111
|
+
isContract(): boolean {
|
112
|
+
return !this.codeHash.equals(KECCAK256_NULL)
|
113
|
+
}
|
114
|
+
|
115
|
+
/**
|
116
|
+
* Returns a `Boolean` determining if the account is empty complying to the definition of
|
117
|
+
* account emptiness in [EIP-161](https://eips.ethereum.org/EIPS/eip-161):
|
118
|
+
* "An account is considered empty when it has no code and zero nonce and zero balance."
|
119
|
+
*/
|
120
|
+
isEmpty(): boolean {
|
121
|
+
return this.balance.isZero() && this.nonce.isZero() && this.codeHash.equals(KECCAK256_NULL)
|
122
|
+
}
|
123
|
+
}
|
124
|
+
|
125
|
+
/**
|
126
|
+
* Checks if the address is a valid. Accepts checksummed addresses too.
|
127
|
+
*/
|
128
|
+
export const isValidAddress = function (hexAddress: string): boolean {
|
129
|
+
try {
|
130
|
+
assertIsString(hexAddress)
|
131
|
+
} catch (e: any) {
|
132
|
+
return false
|
133
|
+
}
|
134
|
+
|
135
|
+
return /^0x[0-9a-fA-F]{40}$/.test(hexAddress)
|
136
|
+
}
|
137
|
+
|
138
|
+
/**
|
139
|
+
* Returns a checksummed address.
|
140
|
+
*
|
141
|
+
* If an eip1191ChainId is provided, the chainId will be included in the checksum calculation. This
|
142
|
+
* has the effect of checksummed addresses for one chain having invalid checksums for others.
|
143
|
+
* For more details see [EIP-1191](https://eips.ethereum.org/EIPS/eip-1191).
|
144
|
+
*
|
145
|
+
* WARNING: Checksums with and without the chainId will differ and the EIP-1191 checksum is not
|
146
|
+
* backwards compatible to the original widely adopted checksum format standard introduced in
|
147
|
+
* [EIP-55](https://eips.ethereum.org/EIPS/eip-55), so this will break in existing applications.
|
148
|
+
* Usage of this EIP is therefore discouraged unless you have a very targeted use case.
|
149
|
+
*/
|
150
|
+
export const toChecksumAddress = function (hexAddress: string, eip1191ChainId?: BNLike): string {
|
151
|
+
assertIsHexString(hexAddress)
|
152
|
+
const address = stripHexPrefix(hexAddress).toLowerCase()
|
153
|
+
|
154
|
+
let prefix = ''
|
155
|
+
if (eip1191ChainId) {
|
156
|
+
const chainId = toType(eip1191ChainId, TypeOutput.BN)
|
157
|
+
prefix = chainId.toString() + '0x'
|
158
|
+
}
|
159
|
+
|
160
|
+
const hash = keccakFromString(prefix + address).toString('hex')
|
161
|
+
let ret = '0x'
|
162
|
+
|
163
|
+
for (let i = 0; i < address.length; i++) {
|
164
|
+
if (parseInt(hash[i], 16) >= 8) {
|
165
|
+
ret += address[i].toUpperCase()
|
166
|
+
} else {
|
167
|
+
ret += address[i]
|
168
|
+
}
|
169
|
+
}
|
170
|
+
|
171
|
+
return ret
|
172
|
+
}
|
173
|
+
|
174
|
+
/**
|
175
|
+
* Checks if the address is a valid checksummed address.
|
176
|
+
*
|
177
|
+
* See toChecksumAddress' documentation for details about the eip1191ChainId parameter.
|
178
|
+
*/
|
179
|
+
export const isValidChecksumAddress = function (
|
180
|
+
hexAddress: string,
|
181
|
+
eip1191ChainId?: BNLike
|
182
|
+
): boolean {
|
183
|
+
return isValidAddress(hexAddress) && toChecksumAddress(hexAddress, eip1191ChainId) === hexAddress
|
184
|
+
}
|
185
|
+
|
186
|
+
/**
|
187
|
+
* Generates an address of a newly created contract.
|
188
|
+
* @param from The address which is creating this new address
|
189
|
+
* @param nonce The nonce of the from account
|
190
|
+
*/
|
191
|
+
export const generateAddress = function (from: Buffer, nonce: Buffer): Buffer {
|
192
|
+
assertIsBuffer(from)
|
193
|
+
assertIsBuffer(nonce)
|
194
|
+
const nonceBN = new BN(nonce)
|
195
|
+
|
196
|
+
if (nonceBN.isZero()) {
|
197
|
+
// in RLP we want to encode null in the case of zero nonce
|
198
|
+
// read the RLP documentation for an answer if you dare
|
199
|
+
return rlphash([from, null]).slice(-20)
|
200
|
+
}
|
201
|
+
|
202
|
+
// Only take the lower 160bits of the hash
|
203
|
+
return rlphash([from, Buffer.from(nonceBN.toArray())]).slice(-20)
|
204
|
+
}
|
205
|
+
|
206
|
+
/**
|
207
|
+
* Generates an address for a contract created using CREATE2.
|
208
|
+
* @param from The address which is creating this new address
|
209
|
+
* @param salt A salt
|
210
|
+
* @param initCode The init code of the contract being created
|
211
|
+
*/
|
212
|
+
export const generateAddress2 = function (from: Buffer, salt: Buffer, initCode: Buffer): Buffer {
|
213
|
+
assertIsBuffer(from)
|
214
|
+
assertIsBuffer(salt)
|
215
|
+
assertIsBuffer(initCode)
|
216
|
+
|
217
|
+
assert(from.length === 20)
|
218
|
+
assert(salt.length === 32)
|
219
|
+
|
220
|
+
const address = keccak256(
|
221
|
+
Buffer.concat([Buffer.from('ff', 'hex'), from, salt, keccak256(initCode)])
|
222
|
+
)
|
223
|
+
|
224
|
+
return address.slice(-20)
|
225
|
+
}
|
226
|
+
|
227
|
+
/**
|
228
|
+
* Checks if the private key satisfies the rules of the curve secp256k1.
|
229
|
+
*/
|
230
|
+
export const isValidPrivate = function (privateKey: Buffer): boolean {
|
231
|
+
return privateKeyVerify(privateKey)
|
232
|
+
}
|
233
|
+
|
234
|
+
/**
|
235
|
+
* Checks if the public key satisfies the rules of the curve secp256k1
|
236
|
+
* and the requirements of Ethereum.
|
237
|
+
* @param publicKey The two points of an uncompressed key, unless sanitize is enabled
|
238
|
+
* @param sanitize Accept public keys in other formats
|
239
|
+
*/
|
240
|
+
export const isValidPublic = function (publicKey: Buffer, sanitize: boolean = false): boolean {
|
241
|
+
assertIsBuffer(publicKey)
|
242
|
+
if (publicKey.length === 64) {
|
243
|
+
// Convert to SEC1 for secp256k1
|
244
|
+
return publicKeyVerify(Buffer.concat([Buffer.from([4]), publicKey]))
|
245
|
+
}
|
246
|
+
|
247
|
+
if (!sanitize) {
|
248
|
+
return false
|
249
|
+
}
|
250
|
+
|
251
|
+
return publicKeyVerify(publicKey)
|
252
|
+
}
|
253
|
+
|
254
|
+
/**
|
255
|
+
* Returns the ethereum address of a given public key.
|
256
|
+
* Accepts "Ethereum public keys" and SEC1 encoded keys.
|
257
|
+
* @param pubKey The two points of an uncompressed key, unless sanitize is enabled
|
258
|
+
* @param sanitize Accept public keys in other formats
|
259
|
+
*/
|
260
|
+
export const pubToAddress = function (pubKey: Buffer, sanitize: boolean = false): Buffer {
|
261
|
+
assertIsBuffer(pubKey)
|
262
|
+
if (sanitize && pubKey.length !== 64) {
|
263
|
+
pubKey = Buffer.from(publicKeyConvert(pubKey, false).slice(1))
|
264
|
+
}
|
265
|
+
assert(pubKey.length === 64)
|
266
|
+
// Only take the lower 160bits of the hash
|
267
|
+
return keccak(pubKey).slice(-20)
|
268
|
+
}
|
269
|
+
export const publicToAddress = pubToAddress
|
270
|
+
|
271
|
+
/**
|
272
|
+
* Returns the ethereum public key of a given private key.
|
273
|
+
* @param privateKey A private key must be 256 bits wide
|
274
|
+
*/
|
275
|
+
export const privateToPublic = function (privateKey: Buffer): Buffer {
|
276
|
+
assertIsBuffer(privateKey)
|
277
|
+
// skip the type flag and use the X, Y points
|
278
|
+
return Buffer.from(publicKeyCreate(privateKey, false)).slice(1)
|
279
|
+
}
|
280
|
+
|
281
|
+
/**
|
282
|
+
* Returns the ethereum address of a given private key.
|
283
|
+
* @param privateKey A private key must be 256 bits wide
|
284
|
+
*/
|
285
|
+
export const privateToAddress = function (privateKey: Buffer): Buffer {
|
286
|
+
return publicToAddress(privateToPublic(privateKey))
|
287
|
+
}
|
288
|
+
|
289
|
+
/**
|
290
|
+
* Converts a public key to the Ethereum format.
|
291
|
+
*/
|
292
|
+
export const importPublic = function (publicKey: Buffer): Buffer {
|
293
|
+
assertIsBuffer(publicKey)
|
294
|
+
if (publicKey.length !== 64) {
|
295
|
+
publicKey = Buffer.from(publicKeyConvert(publicKey, false).slice(1))
|
296
|
+
}
|
297
|
+
return publicKey
|
298
|
+
}
|
299
|
+
|
300
|
+
/**
|
301
|
+
* Returns the zero address.
|
302
|
+
*/
|
303
|
+
export const zeroAddress = function (): string {
|
304
|
+
const addressLength = 20
|
305
|
+
const addr = zeros(addressLength)
|
306
|
+
return bufferToHex(addr)
|
307
|
+
}
|
308
|
+
|
309
|
+
/**
|
310
|
+
* Checks if a given address is the zero address.
|
311
|
+
*/
|
312
|
+
export const isZeroAddress = function (hexAddress: string): boolean {
|
313
|
+
try {
|
314
|
+
assertIsString(hexAddress)
|
315
|
+
} catch (e: any) {
|
316
|
+
return false
|
317
|
+
}
|
318
|
+
|
319
|
+
const zeroAddr = zeroAddress()
|
320
|
+
return zeroAddr === hexAddress
|
321
|
+
}
|
package/src/address.ts
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
import assert from 'assert'
|
2
|
+
import { BN } from './externals'
|
3
|
+
import { toBuffer, zeros } from './bytes'
|
4
|
+
import {
|
5
|
+
isValidAddress,
|
6
|
+
pubToAddress,
|
7
|
+
privateToAddress,
|
8
|
+
generateAddress,
|
9
|
+
generateAddress2,
|
10
|
+
} from './account'
|
11
|
+
|
12
|
+
export class Address {
|
13
|
+
public readonly buf: Buffer
|
14
|
+
|
15
|
+
constructor(buf: Buffer) {
|
16
|
+
assert(buf.length === 20, 'Invalid address length')
|
17
|
+
this.buf = buf
|
18
|
+
}
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Returns the zero address.
|
22
|
+
*/
|
23
|
+
static zero(): Address {
|
24
|
+
return new Address(zeros(20))
|
25
|
+
}
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Returns an Address object from a hex-encoded string.
|
29
|
+
* @param str - Hex-encoded address
|
30
|
+
*/
|
31
|
+
static fromString(str: string): Address {
|
32
|
+
assert(isValidAddress(str), 'Invalid address')
|
33
|
+
return new Address(toBuffer(str))
|
34
|
+
}
|
35
|
+
|
36
|
+
/**
|
37
|
+
* Returns an address for a given public key.
|
38
|
+
* @param pubKey The two points of an uncompressed key
|
39
|
+
*/
|
40
|
+
static fromPublicKey(pubKey: Buffer): Address {
|
41
|
+
assert(Buffer.isBuffer(pubKey), 'Public key should be Buffer')
|
42
|
+
const buf = pubToAddress(pubKey)
|
43
|
+
return new Address(buf)
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Returns an address for a given private key.
|
48
|
+
* @param privateKey A private key must be 256 bits wide
|
49
|
+
*/
|
50
|
+
static fromPrivateKey(privateKey: Buffer): Address {
|
51
|
+
assert(Buffer.isBuffer(privateKey), 'Private key should be Buffer')
|
52
|
+
const buf = privateToAddress(privateKey)
|
53
|
+
return new Address(buf)
|
54
|
+
}
|
55
|
+
|
56
|
+
/**
|
57
|
+
* Generates an address for a newly created contract.
|
58
|
+
* @param from The address which is creating this new address
|
59
|
+
* @param nonce The nonce of the from account
|
60
|
+
*/
|
61
|
+
static generate(from: Address, nonce: BN): Address {
|
62
|
+
assert(BN.isBN(nonce))
|
63
|
+
return new Address(generateAddress(from.buf, nonce.toArrayLike(Buffer)))
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* Generates an address for a contract created using CREATE2.
|
68
|
+
* @param from The address which is creating this new address
|
69
|
+
* @param salt A salt
|
70
|
+
* @param initCode The init code of the contract being created
|
71
|
+
*/
|
72
|
+
static generate2(from: Address, salt: Buffer, initCode: Buffer): Address {
|
73
|
+
assert(Buffer.isBuffer(salt))
|
74
|
+
assert(Buffer.isBuffer(initCode))
|
75
|
+
return new Address(generateAddress2(from.buf, salt, initCode))
|
76
|
+
}
|
77
|
+
|
78
|
+
/**
|
79
|
+
* Is address equal to another.
|
80
|
+
*/
|
81
|
+
equals(address: Address): boolean {
|
82
|
+
return this.buf.equals(address.buf)
|
83
|
+
}
|
84
|
+
|
85
|
+
/**
|
86
|
+
* Is address zero.
|
87
|
+
*/
|
88
|
+
isZero(): boolean {
|
89
|
+
return this.equals(Address.zero())
|
90
|
+
}
|
91
|
+
|
92
|
+
/**
|
93
|
+
* True if address is in the address range defined
|
94
|
+
* by EIP-1352
|
95
|
+
*/
|
96
|
+
isPrecompileOrSystemAddress(): boolean {
|
97
|
+
const addressBN = new BN(this.buf)
|
98
|
+
const rangeMin = new BN(0)
|
99
|
+
const rangeMax = new BN('ffff', 'hex')
|
100
|
+
|
101
|
+
return addressBN.gte(rangeMin) && addressBN.lte(rangeMax)
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* Returns hex encoding of address.
|
106
|
+
*/
|
107
|
+
toString(): string {
|
108
|
+
return '0x' + this.buf.toString('hex')
|
109
|
+
}
|
110
|
+
|
111
|
+
/**
|
112
|
+
* Returns Buffer representation of address.
|
113
|
+
*/
|
114
|
+
toBuffer(): Buffer {
|
115
|
+
return Buffer.from(this.buf)
|
116
|
+
}
|
117
|
+
}
|