[fix] don't lose big number precision above 53 bits in abi-spec

- convert to string instead of numbers in hex-to-number
- remove usage of full web3 lib

Signed-off-by: yenda <eric@status.im>
This commit is contained in:
yenda 2019-04-04 10:46:37 +02:00
parent 6aae0b76b9
commit 9b47275249
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
2 changed files with 17 additions and 3 deletions

View File

@ -41,7 +41,11 @@
(defn hex-to-number [x] (defn hex-to-number [x]
(when x (when x
(.toNumber (dependencies/Web3.prototype.toBigNumber (str "0x" x) 16)))) (let [hex-x (str "0x" x)]
(try
(.hexToNumber utils hex-x)
(catch :default err
(.hexToNumberString utils hex-x))))))
(defn sha3 [s] (defn sha3 [s]
(.sha3 utils (str s))) (.sha3 utils (str s)))

View File

@ -2,6 +2,16 @@
(:require [cljs.test :refer-macros [deftest is testing]] (:require [cljs.test :refer-macros [deftest is testing]]
[status-im.utils.ethereum.abi-spec :as abi-spec])) [status-im.utils.ethereum.abi-spec :as abi-spec]))
(deftest hex-to-number
(testing "hex number is less than 53 bits, it returns a number")
(is (= (abi-spec/hex-to-number "9")
9))
(is (= (abi-spec/hex-to-number "99999999")
2576980377))
(testing "hex number is less than 53 bits, it returns a string")
(is (= (abi-spec/hex-to-number "9999999999999999")
"11068046444225730969")))
(deftest enc (deftest enc
(is (= "000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" (abi-spec/enc {:type :address :value "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"})))) (is (= "000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" (abi-spec/enc {:type :address :value "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"}))))
@ -33,9 +43,9 @@
0 0
0))) 0)))
(is (= (abi-spec/decode "0x0000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000d7621dc58210000" (is (= (abi-spec/decode "0x0000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000d7621dc58210001"
["uint256" "uint256"]) ["uint256" "uint256"])
'(1000000000000000000 970000000000000000))) '("1000000000000000000" "970000000000000001")))
(is (= (abi-spec/decode "0x000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000000000000003e8" (is (= (abi-spec/decode "0x000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000000000000003e8"
["address" "address" "uint256"]) ["address" "address" "uint256"])