This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
73 lines (61 loc) · 1.84 KB
/
index.js
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
const createKeccakHash = require('keccak')
/**
* Validates checksummed address.
* @param {string} address
* @param {number} chainId where checksummed address should be valid.
* @returns {bool} if the address is valid on the specified chain.
*/
function isValidChecksumAddress(address, chainId) {
return isValidAddress(address) && (toChecksumAddress(address, chainId) === address)
}
/**
* Validates address.
* @param {string} address
* @returns {bool} if the address is valid.
*/
function isValidAddress(address) {
return /^0x[0-9a-fA-F]{40}$/.test(address)
}
/**
* Generates checksummed address.
* @param {string} address
* @param {number} chain where checksummed address should be valid.
* @returns {string} address with checksum applied.
*/
function toChecksumAddress(address, chainId = null) {
if (typeof address !== 'string') {
throw new Error("stripHexPrefix param must be type 'string', is currently type " + (typeof address) + ".");
}
const strip_address = stripHexPrefix(address).toLowerCase()
const prefix = chainId != null ? (chainId.toString() + '0x') : ''
const keccak_hash = keccak(prefix + strip_address).toString('hex')
let output = '0x'
for (let i = 0; i < strip_address.length; i++)
output += parseInt(keccak_hash[i], 16) >= 8 ?
strip_address[i].toUpperCase() :
strip_address[i]
return output
}
/**
* Removes prefix from address if exists.
* @param {string} address
* @returns {string} address without prefix
*/
function stripHexPrefix(str) {
return str.slice(0, 2) === '0x' ? str.slice(2) : str
}
/**
* Generates keccak sha256
* @param {string} keccak input
* @returns {string} keccak sha256
*/
function keccak(a) {
return createKeccakHash('keccak256').update(a).digest()
}
module.exports = {
isValidChecksumAddress,
isValidAddress,
toChecksumAddress,
stripHexPrefix,
keccak
}