forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.ts
117 lines (103 loc) · 2.87 KB
/
address.ts
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
import assert from 'assert'
import { BN } from './externals'
import { toBuffer, zeros } from './bytes'
import {
isValidAddress,
pubToAddress,
privateToAddress,
generateAddress,
generateAddress2,
} from './account'
export class Address {
public readonly buf: Buffer
constructor(buf: Buffer) {
assert(buf.length === 20, 'Invalid address length')
this.buf = buf
}
/**
* Returns the zero address.
*/
static zero(): Address {
return new Address(zeros(20))
}
/**
* Returns an Address object from a hex-encoded string.
* @param str - Hex-encoded address
*/
static fromString(str: string): Address {
assert(isValidAddress(str), 'Invalid address')
return new Address(toBuffer(str))
}
/**
* Returns an address for a given public key.
* @param pubKey The two points of an uncompressed key
*/
static fromPublicKey(pubKey: Buffer): Address {
assert(Buffer.isBuffer(pubKey), 'Public key should be Buffer')
const buf = pubToAddress(pubKey)
return new Address(buf)
}
/**
* Returns an address for a given private key.
* @param privateKey A private key must be 256 bits wide
*/
static fromPrivateKey(privateKey: Buffer): Address {
assert(Buffer.isBuffer(privateKey), 'Private key should be Buffer')
const buf = privateToAddress(privateKey)
return new Address(buf)
}
/**
* Generates an address for a newly created contract.
* @param from The address which is creating this new address
* @param nonce The nonce of the from account
*/
static generate(from: Address, nonce: BN): Address {
assert(BN.isBN(nonce))
return new Address(generateAddress(from.buf, nonce.toArrayLike(Buffer)))
}
/**
* Generates an address for a contract created using CREATE2.
* @param from The address which is creating this new address
* @param salt A salt
* @param initCode The init code of the contract being created
*/
static generate2(from: Address, salt: Buffer, initCode: Buffer): Address {
assert(Buffer.isBuffer(salt))
assert(Buffer.isBuffer(initCode))
return new Address(generateAddress2(from.buf, salt, initCode))
}
/**
* Is address equal to another.
*/
equals(address: Address): boolean {
return this.buf.equals(address.buf)
}
/**
* Is address zero.
*/
isZero(): boolean {
return this.equals(Address.zero())
}
/**
* True if address is in the address range defined
* by EIP-1352
*/
isPrecompileOrSystemAddress(): boolean {
const addressBN = new BN(this.buf)
const rangeMin = new BN(0)
const rangeMax = new BN('ffff', 'hex')
return addressBN.gte(rangeMin) && addressBN.lte(rangeMax)
}
/**
* Returns hex encoding of address.
*/
toString(): string {
return '0x' + this.buf.toString('hex')
}
/**
* Returns Buffer representation of address.
*/
toBuffer(): Buffer {
return Buffer.from(this.buf)
}
}