This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 273
/
account.ts
180 lines (157 loc) · 5.23 KB
/
account.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
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
const assert = require('assert')
const ethjsUtil = require('ethjs-util')
const secp256k1 = require('secp256k1')
import BN = require('bn.js')
import { toBuffer, addHexPrefix, zeros, bufferToHex, unpad } from './bytes'
import { keccak, keccak256, rlphash } from './hash'
/**
* Returns a zero address.
*/
export const zeroAddress = function(): string {
const addressLength = 20
const addr = zeros(addressLength)
return bufferToHex(addr)
}
/**
* Checks if the address is a valid. Accepts checksummed addresses too.
*/
export const isValidAddress = function(address: string): boolean {
return /^0x[0-9a-fA-F]{40}$/.test(address)
}
/**
* Checks if a given address is a zero address.
*/
export const isZeroAddress = function(address: string): boolean {
const zeroAddr = zeroAddress()
return zeroAddr === addHexPrefix(address)
}
/**
* Returns a checksummed address.
*/
export const toChecksumAddress = function(address: string): string {
address = ethjsUtil.stripHexPrefix(address).toLowerCase()
const hash = keccak(address).toString('hex')
let ret = '0x'
for (let i = 0; i < address.length; i++) {
if (parseInt(hash[i], 16) >= 8) {
ret += address[i].toUpperCase()
} else {
ret += address[i]
}
}
return ret
}
/**
* Checks if the address is a valid checksummed address.
*/
export const isValidChecksumAddress = function(address: string): boolean {
return isValidAddress(address) && toChecksumAddress(address) === address
}
/**
* Generates an address of a newly created contract.
* @param from The address which is creating this new address
* @param nonce The nonce of the from account
*/
export const generateAddress = function(from: Buffer, nonce: Buffer): Buffer {
from = toBuffer(from)
const nonceBN = new BN(nonce)
if (nonceBN.isZero()) {
// in RLP we want to encode null in the case of zero nonce
// read the RLP documentation for an answer if you dare
return rlphash([from, null]).slice(-20)
}
// Only take the lower 160bits of the hash
return rlphash([from, Buffer.from(nonceBN.toArray())]).slice(-20)
}
/**
* 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
*/
export const generateAddress2 = function(
from: Buffer | string,
salt: Buffer | string,
initCode: Buffer | string,
): Buffer {
const fromBuf = toBuffer(from)
const saltBuf = toBuffer(salt)
const initCodeBuf = toBuffer(initCode)
assert(fromBuf.length === 20)
assert(saltBuf.length === 32)
const address = keccak256(
Buffer.concat([Buffer.from('ff', 'hex'), fromBuf, saltBuf, keccak256(initCodeBuf)]),
)
return address.slice(-20)
}
/**
* Returns true if the supplied address belongs to a precompiled account (Byzantium).
*/
export const isPrecompiled = function(address: Buffer | string): boolean {
const a = unpad(address)
return a.length === 1 && a[0] >= 1 && a[0] <= 8
}
/**
* Checks if the private key satisfies the rules of the curve secp256k1.
*/
export const isValidPrivate = function(privateKey: Buffer): boolean {
return secp256k1.privateKeyVerify(privateKey)
}
/**
* Checks if the public key satisfies the rules of the curve secp256k1
* and the requirements of Ethereum.
* @param publicKey The two points of an uncompressed key, unless sanitize is enabled
* @param sanitize Accept public keys in other formats
*/
export const isValidPublic = function(publicKey: Buffer, sanitize: boolean = false): boolean {
if (publicKey.length === 64) {
// Convert to SEC1 for secp256k1
return secp256k1.publicKeyVerify(Buffer.concat([Buffer.from([4]), publicKey]))
}
if (!sanitize) {
return false
}
return secp256k1.publicKeyVerify(publicKey)
}
/**
* Returns the ethereum address of a given public key.
* Accepts "Ethereum public keys" and SEC1 encoded keys.
* @param pubKey The two points of an uncompressed key, unless sanitize is enabled
* @param sanitize Accept public keys in other formats
*/
export const pubToAddress = function(pubKey: Buffer, sanitize: boolean = false): Buffer {
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 keccak(pubKey).slice(-20)
}
export const publicToAddress = pubToAddress
/**
* Returns the ethereum address of a given private key.
* @param privateKey A private key must be 256 bits wide
*/
export const privateToAddress = function(privateKey: Buffer): Buffer {
return publicToAddress(privateToPublic(privateKey))
}
/**
* Returns the ethereum public key of a given private key.
* @param privateKey A private key must be 256 bits wide
*/
export const privateToPublic = function(privateKey: Buffer): Buffer {
privateKey = toBuffer(privateKey)
// skip the type flag and use the X, Y points
return secp256k1.publicKeyCreate(privateKey, false).slice(1)
}
/**
* Converts a public key to the Ethereum format.
*/
export const importPublic = function(publicKey: Buffer): Buffer {
publicKey = toBuffer(publicKey)
if (publicKey.length !== 64) {
publicKey = secp256k1.publicKeyConvert(publicKey, false).slice(1)
}
return publicKey
}