From d3284cb1e5e69d4b1b21ecb25af78e6ba0347b29 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Sat, 15 Feb 2020 00:51:47 -0400 Subject: [PATCH] fix: checksum address --- package.json | 11 ++++++----- src/subspace.js | 5 +++-- src/utils.js | 37 +++++++++++++++++++------------------ yarn.lock | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index 9814588..c5fe0d4 100644 --- a/package.json +++ b/package.json @@ -41,29 +41,30 @@ "@babel/cli": "^7.8.4", "@babel/core": "^7.8.4", "@babel/plugin-proposal-class-properties": "^7.8.3", - "@babel/plugin-transform-runtime": "^7.8.3", - "@babel/plugin-proposal-private-methods": "^7.8.3", "@babel/plugin-proposal-nullish-coalescing-operator": "^7.8.3", "@babel/plugin-proposal-optional-chaining": "^7.8.3", + "@babel/plugin-proposal-private-methods": "^7.8.3", + "@babel/plugin-transform-runtime": "^7.8.3", "@babel/preset-env": "^7.8.4", "babel-eslint": "^10.0.3", + "cross-env": "^7.0.0", "eslint": "^6.8.0", "eslint-plugin-babel": "^5.3.0", "eslint-plugin-import": "^2.20.1", "eslint-plugin-standard": "^4.0.1", - "cross-env": "^7.0.0", "ganache-core": "^2.10.1", "npm-run-all": "^4.1.5", "rimraf": "^3.0.1" }, "dependencies": { - "babel-jest": "24.9.0", - "jest": "24.9.0", "@babel/runtime": "^7.8.4", "@babel/runtime-corejs3": "^7.8.4", + "babel-jest": "24.9.0", "core-js": "^3.6.4", "fast-deep-equal": "^3.1.1", "hex2dec": "^1.1.2", + "jest": "24.9.0", + "keccak": "^3.0.0", "lokijs": "^1.5.6", "object-hash": "^2.0.1", "rxjs": "^6.5.2", diff --git a/src/subspace.js b/src/subspace.js index aaf43b8..5640f9b 100644 --- a/src/subspace.js +++ b/src/subspace.js @@ -5,7 +5,7 @@ import Database from "./database/database.js"; import NullDatabase from "./database/nullDatabase.js"; import Events from "events"; import Web3Eth from "web3-eth"; -import {isAddress} from "./utils"; +import {isAddress, toChecksumAddress} from "./utils"; import stripHexPrefix from "strip-hex-prefix"; import {hexToDec} from "hex2dec"; import EventSyncer from "./eventSyncer"; @@ -280,10 +280,11 @@ export default class Subspace { if (erc20Address && !isAddress(erc20Address)) throw "invalid ERC20 contract address"; address = toChecksumAddress(address); - erc20Address = toChecksumAddress(address); + erc20Address = erc20Address ? toChecksumAddress(erc20Address) : null; return this._getDistinctObservableFromPromise(hash({address, erc20Address}), () => { if (!erc20Address) { + this.web3.getBalance(address).then(balance => console.log("Balance: ", balance)); return this.web3.getBalance(address); } else { // balanceOf diff --git a/src/utils.js b/src/utils.js index d9440a3..054ce74 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,25 +1,26 @@ +import createKeccakHash from "keccak"; + export function isAddress(address) { return /^(0x)?[0-9a-fA-F]{40}$/i.test(address); } -export function toChecksumAddress(address) { - if (typeof address === "undefined") return ""; - - address = address.toLowerCase().replace(/^0x/i, ""); - var addressHash = utils.sha3(address).replace(/^0x/i, ""); - var checksumAddress = "0x"; - - for (var i = 0; i < address.length; i++) { - // If ith character is 9 to f then make it uppercase - if (parseInt(addressHash[i], 16) > 7) { - checksumAddress += address[i].toUpperCase(); - } else { - checksumAddress += address[i]; - } - } - return checksumAddress; -} - export function sleep(milliseconds) { return new Promise(resolve => setTimeout(resolve, milliseconds)); } + +export function toChecksumAddress(address) { + address = address.toLowerCase().replace("0x", ""); + const hash = createKeccakHash("keccak256") + .update(address) + .digest("hex"); + let ret = "0x"; + + for (var i = 0; i < address.length; i++) { + if (parseInt(hash[i], 16) >= 8) { + ret += address[i].toUpperCase(); + } else { + ret += address[i]; + } + } + return ret; +} diff --git a/yarn.lock b/yarn.lock index 8b2519e..d74eaf3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5587,6 +5587,14 @@ keccak@^2.0.0: nan "^2.14.0" safe-buffer "^5.2.0" +keccak@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/keccak/-/keccak-3.0.0.tgz#420d1de4a38a04f33ff8401f0535fb93756861d4" + integrity sha512-/4h4FIfFEpTEuySXi/nVFM5rqSKPnnhI7cL4K3MFSwoI3VyM7AhPSq3SsysARtnEBEeIKMBUWD8cTh9nHE8AkA== + dependencies: + node-addon-api "^2.0.0" + node-gyp-build "^4.2.0" + keccakjs@^0.2.0, keccakjs@^0.2.1: version "0.2.3" resolved "https://registry.yarnpkg.com/keccakjs/-/keccakjs-0.2.3.tgz#5e4e969ce39689a3861f445d7752ee3477f9fe72" @@ -6209,6 +6217,11 @@ nice-try@^1.0.4: resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== +node-addon-api@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-2.0.0.tgz#f9afb8d777a91525244b01775ea0ddbe1125483b" + integrity sha512-ASCL5U13as7HhOExbT6OlWJJUV/lLzL2voOSP1UVehpRD8FbSrSDjfScK/KwAvVTI5AS6r4VwbOMlIqtvRidnA== + node-fetch@2.1.2: version "2.1.2" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.1.2.tgz#ab884e8e7e57e38a944753cec706f788d1768bb5" @@ -6222,6 +6235,11 @@ node-fetch@~1.7.1: encoding "^0.1.11" is-stream "^1.0.1" +node-gyp-build@^4.2.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/node-gyp-build/-/node-gyp-build-4.2.0.tgz#2c2b05f461f4178641a6ce2d7159f04094e9376d" + integrity sha512-4oiumOLhCDU9Rronz8PZ5S4IvT39H5+JEv/hps9V8s7RSLhsac0TCP78ulnHXOo8X1wdpPiTayGlM1jr4IbnaQ== + node-int64@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"