Skip to content

Commit

Permalink
Update addresses, add isValidAddress & searchChecksummedNetworkss
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliorizzo committed Nov 27, 2019
1 parent 5c19b2b commit 7289113
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .doc/makeReadme.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('fs')

// const files = path.resolve(__dirname, '../src') + '/*.js'
const basePath = path.resolve(__dirname, '../src')
const files = fs.readdirSync(basePath).map(f => {
const files = fs.readdirSync(basePath).filter(f => f.endsWith('.js')).map(f => {
return { path: `${basePath}/${f}`, name: titleFromFile(f) }
})
const parser = Jsdoc2md({ 'heading-depth': 3 })
Expand Down
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ module.exports = {
"env": {
"node": true,
"mocha": true
}
},
"plugins": [
"json"
]
};
62 changes: 53 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"test:browser": "npx karma start",
"test": "npm run test:node && npm run test:browser",
"doc": "node ./.doc/makeReadme.js > README.md",
"build": "npm run lint && npm run doc && npx babel src -d dist"
"build": "npm run lint && npm run doc && npx babel src -d dist --copy-files"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -43,6 +43,7 @@
"eslint": "^4.19.1",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-json": "^2.0.1",
"eslint-plugin-mocha": "^5.3.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.8.0",
Expand Down
27 changes: 27 additions & 0 deletions src/addresses.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { keccak256 } from './hashes'
import { stripHexPrefix } from './strings'
import nets from './networks.json'

/**
* @description Check if a string is an address
Expand Down Expand Up @@ -33,3 +34,29 @@ export function toChecksumAddress (address, chainId) {
export function isValidChecksumAddress (address, chainId) {
return isAddress(address) && toChecksumAddress(address, chainId) === address
}

/**
* @description Checks if an address is valid.
* @param {String} address
* @param {Integer|String} chainId
* @returns {Boolean}
*/
export function isValidAddress (address, chainId) {
if (typeof address !== 'string') return false
if (address.match(/[A-F]/)) {
return isValidChecksumAddress(address, chainId)
}
return isAddress(address)
}

/**
* @description Search network info of checksummed address
* @param {String} address
* @param {Array} [networks], chainId list
* see: https://chainid.network/chains.json
* @returns {Array}
*/
export function searchChecksummedNetworks (address, networks) {
networks = networks || nets
return networks.filter(net => toChecksumAddress(address, net.chainId) === address)
}
167 changes: 167 additions & 0 deletions src/networks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
[
{
"name": "Ethereum Mainnet",
"chainId": 1,
"shortName": "eth",
"chain": "ETH",
"network": "mainnet",
"networkId": 1,
"nativeCurrency": {
"name": "Ether",
"symbol": "ETH",
"decimals": 18
},
"rpc": [
"https://mainnet.infura.io/v3/${INFURA_API_KEY}",
"https://api.mycryptoapi.com/eth"
],
"faucets": [],
"infoURL": "https://ethereum.org",
"explorers": [
"https://etherscan.io"
]
},
{
"name": "Ethereum Testnet Ropsten",
"chainId": 3,
"shortName": "rop",
"chain": "ETH",
"network": "ropsten",
"networkId": 3,
"nativeCurrency": {
"name": "Ropsten Ether",
"symbol": "ROP",
"decimals": 18
},
"rpc": [
"https://ropsten.infura.io/v3/${INFURA_API_KEY}"
],
"faucets": [
"https://faucet.ropsten.be?${ADDRESS}"
],
"infoURL": "https://github.com/ethereum/ropsten",
"explorers": [
"https://ropsten.etherscan.io"
]
},
{
"name": "RSK Mainnet",
"chainId": 30,
"shortName": "rsk",
"chain": "RSK",
"network": "mainnet",
"networkId": 30,
"nativeCurrency": {
"name": "RSK Mainnet Ether",
"symbol": "RSK",
"decimals": 18
},
"rpc": [
"https://public-node.rsk.co",
"https://mycrypto.rsk.co"
],
"faucets": [],
"infoURL": "https://rsk.co",
"explorers": [
"https://explorer.rsk.co",
"https://blockscout.com/rsk/mainnet"
]
},
{
"name": "RSK Testnet",
"chainId": 31,
"shortName": "trsk",
"chain": "RSK",
"network": "testnet",
"networkId": 31,
"nativeCurrency": {
"name": "RSK Testnet Ether",
"symbol": "TRSK",
"decimals": 18
},
"rpc": [
"https://public-node.testnet.rsk.co",
"https://mycrypto.testnet.rsk.co"
],
"faucets": [
"https://faucet.testnet.rsk.co"
],
"infoURL": "https://rsk.co",
"explorers": [
"https://explorer.testnet.rsk.co"
]
},
{
"name": "Ethereum Testnet Rinkeby",
"chainId": 4,
"shortName": "rin",
"chain": "ETH",
"network": "rinkeby",
"networkId": 4,
"nativeCurrency": {
"name": "Rinkeby Ether",
"symbol": "RIN",
"decimals": 18
},
"rpc": [
"https://rinkeby.infura.io/v3/${INFURA_API_KEY}"
],
"faucets": [
"https://faucet.rinkeby.io"
],
"infoURL": "https://www.rinkeby.io",
"explorers": [
"https://rinkeby.etherscan.io"
]
},
{
"name": "Ethereum Testnet Kovan",
"chainId": 42,
"shortName": "kov",
"chain": "ETH",
"network": "kovan",
"networkId": 42,
"nativeCurrency": {
"name": "Kovan Ether",
"symbol": "KOV",
"decimals": 18
},
"rpc": [
"https://kovan.infura.io/v3/${INFURA_API_KEY}"
],
"faucets": [
"https://faucet.kovan.network",
"https://gitter.im/kovan-testnet/faucet"
],
"infoURL": "https://kovan-testnet.github.io/website",
"explorers": [
"https://kovan.etherscan.io"
]
},
{
"name": "Ethereum Testnet Görli",
"chainId": 5,
"shortName": "gor",
"chain": "ETH",
"network": "goerli",
"networkId": 5,
"nativeCurrency": {
"name": "Görli Ether",
"symbol": "GOR",
"decimals": 18
},
"rpc": [
"https://rpc.goerli.mudit.blog/",
"https://rpc.slock.it/goerli ",
"https://goerli.prylabs.net/"
],
"faucets": [
"https://goerli-faucet.slock.it/?address=${ADDRESS}",
"https://faucet.goerli.mudit.blog"
],
"infoURL": "https://goerli.net/#about",
"explorers": [
"https://goerli.etherscan.io"
]
}
]
35 changes: 32 additions & 3 deletions test/addresses.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { isAddress, isValidChecksumAddress, toChecksumAddress } from '../src/addresses'
import {
isAddress,
isValidChecksumAddress,
toChecksumAddress,
isValidAddress,
searchChecksummedNetworks
} from '../src/addresses'

import { assert } from 'chai'

const invalidAddresses = [
Expand Down Expand Up @@ -116,15 +123,37 @@ const allAddresses = [
describe(`# Addresses`, function () {
test({ isAddress }, allAddresses, true)
test({ isAddress }, invalidAddresses, false)
test({ isValidAddress }, invalidAddresses, false)
test({ isValidAddress }, plainAddresses, true)

for (let id in netAddresses) {
test({ isValidChecksumAddress }, netAddresses[id].map(a => [a, id]), true)
let addrs = netAddresses[id].map(a => [a, id])
test({ isValidChecksumAddress }, addrs, true)
test({ isValidAddress }, addrs, true)
// validateAddress with changed ids
test({ isValidAddress }, netAddresses[id].map(a => [a, (parseInt(id) + 2)]), false)
}

for (let id in eip1191ChecksummAddresses) {
test({ isValidChecksumAddress }, eip1191ChecksummAddresses[id].map(a => [a, id]), true)
let addrs = eip1191ChecksummAddresses[id].map(a => [a, id])
test({ isValidChecksumAddress }, addrs, true)
test({ isValidAddress }, addrs, true)
}
test({ toChecksumAddress }, EIP1191ethMainnet.map(a => [a, undefined]), EIP1191ethMainnet)
})

describe(`searchChecksummedNetworks()`, function () {
for (let id in netAddresses) {
for (let address of netAddresses[id]) {
it(`should return a network info`, () => {
let result = searchChecksummedNetworks(address)
assert.equal(Array.isArray(result), true)
assert.isTrue(result.map(r => r.chainId).includes(parseInt(id)))
})
}
}
})

function test (payload, value, expected) {
value = (!Array.isArray(value)) ? [value] : value
for (let method in payload) {
Expand Down

0 comments on commit 7289113

Please sign in to comment.