|
| 1 | +const assert = require('assert') |
| 2 | +const ethjsUtil = require('ethjs-util') |
| 3 | +const secp256k1 = require('secp256k1') |
| 4 | +import BN = require('bn.js') |
| 5 | +import { toBuffer, addHexPrefix, zeros, bufferToHex, unpad } from './buffer' |
| 6 | +import { keccak, keccak256, rlphash } from './hash' |
| 7 | + |
| 8 | +/** |
| 9 | + * Returns a zero address. |
| 10 | + */ |
| 11 | +export const zeroAddress = function(): string { |
| 12 | + const addressLength = 20 |
| 13 | + const addr = zeros(addressLength) |
| 14 | + return bufferToHex(addr) |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Checks if the address is a valid. Accepts checksummed addresses too. |
| 19 | + */ |
| 20 | +export const isValidAddress = function(address: string): boolean { |
| 21 | + return /^0x[0-9a-fA-F]{40}$/.test(address) |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Checks if a given address is a zero address. |
| 26 | + */ |
| 27 | +export const isZeroAddress = function(address: string): boolean { |
| 28 | + const zeroAddr = zeroAddress() |
| 29 | + return zeroAddr === addHexPrefix(address) |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Returns a checksummed address. |
| 34 | + */ |
| 35 | +export const toChecksumAddress = function(address: string): string { |
| 36 | + address = ethjsUtil.stripHexPrefix(address).toLowerCase() |
| 37 | + const hash = keccak(address).toString('hex') |
| 38 | + let ret = '0x' |
| 39 | + |
| 40 | + for (let i = 0; i < address.length; i++) { |
| 41 | + if (parseInt(hash[i], 16) >= 8) { |
| 42 | + ret += address[i].toUpperCase() |
| 43 | + } else { |
| 44 | + ret += address[i] |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return ret |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Checks if the address is a valid checksummed address. |
| 53 | + */ |
| 54 | +export const isValidChecksumAddress = function(address: string): boolean { |
| 55 | + return isValidAddress(address) && toChecksumAddress(address) === address |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Generates an address of a newly created contract. |
| 60 | + * @param from The address which is creating this new address |
| 61 | + * @param nonce The nonce of the from account |
| 62 | + */ |
| 63 | +export const generateAddress = function(from: Buffer, nonce: Buffer): Buffer { |
| 64 | + from = toBuffer(from) |
| 65 | + const nonceBN = new BN(nonce) |
| 66 | + |
| 67 | + if (nonceBN.isZero()) { |
| 68 | + // in RLP we want to encode null in the case of zero nonce |
| 69 | + // read the RLP documentation for an answer if you dare |
| 70 | + return rlphash([from, null]).slice(-20) |
| 71 | + } |
| 72 | + |
| 73 | + // Only take the lower 160bits of the hash |
| 74 | + return rlphash([from, Buffer.from(nonceBN.toArray())]).slice(-20) |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Generates an address for a contract created using CREATE2. |
| 79 | + * @param from The address which is creating this new address |
| 80 | + * @param salt A salt |
| 81 | + * @param initCode The init code of the contract being created |
| 82 | + */ |
| 83 | +export const generateAddress2 = function( |
| 84 | + from: Buffer | string, |
| 85 | + salt: Buffer | string, |
| 86 | + initCode: Buffer | string, |
| 87 | +): Buffer { |
| 88 | + const fromBuf = toBuffer(from) |
| 89 | + const saltBuf = toBuffer(salt) |
| 90 | + const initCodeBuf = toBuffer(initCode) |
| 91 | + |
| 92 | + assert(fromBuf.length === 20) |
| 93 | + assert(saltBuf.length === 32) |
| 94 | + |
| 95 | + const address = keccak256( |
| 96 | + Buffer.concat([Buffer.from('ff', 'hex'), fromBuf, saltBuf, keccak256(initCodeBuf)]), |
| 97 | + ) |
| 98 | + |
| 99 | + return address.slice(-20) |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Returns true if the supplied address belongs to a precompiled account (Byzantium). |
| 104 | + */ |
| 105 | +export const isPrecompiled = function(address: Buffer | string): boolean { |
| 106 | + const a = unpad(address) |
| 107 | + return a.length === 1 && a[0] >= 1 && a[0] <= 8 |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Checks if the private key satisfies the rules of the curve secp256k1. |
| 112 | + */ |
| 113 | +export const isValidPrivate = function(privateKey: Buffer): boolean { |
| 114 | + return secp256k1.privateKeyVerify(privateKey) |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Checks if the public key satisfies the rules of the curve secp256k1 |
| 119 | + * and the requirements of Ethereum. |
| 120 | + * @param publicKey The two points of an uncompressed key, unless sanitize is enabled |
| 121 | + * @param sanitize Accept public keys in other formats |
| 122 | + */ |
| 123 | +export const isValidPublic = function(publicKey: Buffer, sanitize: boolean = false): boolean { |
| 124 | + if (publicKey.length === 64) { |
| 125 | + // Convert to SEC1 for secp256k1 |
| 126 | + return secp256k1.publicKeyVerify(Buffer.concat([Buffer.from([4]), publicKey])) |
| 127 | + } |
| 128 | + |
| 129 | + if (!sanitize) { |
| 130 | + return false |
| 131 | + } |
| 132 | + |
| 133 | + return secp256k1.publicKeyVerify(publicKey) |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Returns the ethereum address of a given public key. |
| 138 | + * Accepts "Ethereum public keys" and SEC1 encoded keys. |
| 139 | + * @param pubKey The two points of an uncompressed key, unless sanitize is enabled |
| 140 | + * @param sanitize Accept public keys in other formats |
| 141 | + */ |
| 142 | +export const pubToAddress = function(pubKey: Buffer, sanitize: boolean = false): Buffer { |
| 143 | + pubKey = toBuffer(pubKey) |
| 144 | + if (sanitize && pubKey.length !== 64) { |
| 145 | + pubKey = secp256k1.publicKeyConvert(pubKey, false).slice(1) |
| 146 | + } |
| 147 | + assert(pubKey.length === 64) |
| 148 | + // Only take the lower 160bits of the hash |
| 149 | + return keccak(pubKey).slice(-20) |
| 150 | +} |
| 151 | +export const publicToAddress = pubToAddress |
| 152 | + |
| 153 | +/** |
| 154 | + * Returns the ethereum address of a given private key. |
| 155 | + * @param privateKey A private key must be 256 bits wide |
| 156 | + */ |
| 157 | +export const privateToAddress = function(privateKey: Buffer): Buffer { |
| 158 | + return publicToAddress(privateToPublic(privateKey)) |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Returns the ethereum public key of a given private key. |
| 163 | + * @param privateKey A private key must be 256 bits wide |
| 164 | + */ |
| 165 | +export const privateToPublic = function(privateKey: Buffer): Buffer { |
| 166 | + privateKey = toBuffer(privateKey) |
| 167 | + // skip the type flag and use the X, Y points |
| 168 | + return secp256k1.publicKeyCreate(privateKey, false).slice(1) |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * Converts a public key to the Ethereum format. |
| 173 | + */ |
| 174 | +export const importPublic = function(publicKey: Buffer): Buffer { |
| 175 | + publicKey = toBuffer(publicKey) |
| 176 | + if (publicKey.length !== 64) { |
| 177 | + publicKey = secp256k1.publicKeyConvert(publicKey, false).slice(1) |
| 178 | + } |
| 179 | + return publicKey |
| 180 | +} |
0 commit comments