aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/abi/numbers.go
diff options
context:
space:
mode:
authorThomas Bocek <tom@tomp2p.net>2016-06-03 00:43:27 +0800
committerThomas Bocek <tom@tomp2p.net>2016-06-06 20:27:15 +0800
commit89c6c5bb85ff24c152218f245fa366e733c951a7 (patch)
treeed0d02d136ed0f2aa06eb617a9feec661005ddfd /accounts/abi/numbers.go
parent16a23ff74056aa306eb8a87272277bc8660f3252 (diff)
downloaddexon-89c6c5bb85ff24c152218f245fa366e733c951a7.tar
dexon-89c6c5bb85ff24c152218f245fa366e733c951a7.tar.gz
dexon-89c6c5bb85ff24c152218f245fa366e733c951a7.tar.bz2
dexon-89c6c5bb85ff24c152218f245fa366e733c951a7.tar.lz
dexon-89c6c5bb85ff24c152218f245fa366e733c951a7.tar.xz
dexon-89c6c5bb85ff24c152218f245fa366e733c951a7.tar.zst
dexon-89c6c5bb85ff24c152218f245fa366e733c951a7.zip
accounts/abi: Negative numbers not properly converted in ABI encoding
When converting a negative number e.g., -2, the resulting ABI encoding should look as follows: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe. However, since the check of the type is for an uint instead of an int, it results in the following ABI encoding: 0101010101010101010101010101010101010101010101010101010101010102. The Ethereum ABI (https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI) says, that signed integers are stored in two's complement which should be of the form ffffff.... and not 01010101..... for e.g. -1. Thus, I removed the type check in numbers.go as well as the function S256 as I don't think they are correct. Or maybe I'm missing something?
Diffstat (limited to 'accounts/abi/numbers.go')
-rw-r--r--accounts/abi/numbers.go42
1 files changed, 4 insertions, 38 deletions
diff --git a/accounts/abi/numbers.go b/accounts/abi/numbers.go
index 5a31cf2b5..06c4422f9 100644
--- a/accounts/abi/numbers.go
+++ b/accounts/abi/numbers.go
@@ -61,54 +61,20 @@ func U256(n *big.Int) []byte {
return common.LeftPadBytes(common.U256(n).Bytes(), 32)
}
-func S256(n *big.Int) []byte {
- sint := common.S256(n)
- ret := common.LeftPadBytes(sint.Bytes(), 32)
- if sint.Cmp(common.Big0) < 0 {
- for i, b := range ret {
- if b == 0 {
- ret[i] = 1
- continue
- }
- break
- }
- }
-
- return ret
-}
-
// S256 will ensure signed 256bit on big nums
func U2U256(n uint64) []byte {
return U256(big.NewInt(int64(n)))
}
-func S2S256(n int64) []byte {
- return S256(big.NewInt(n))
-}
-
// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
-func packNum(value reflect.Value, to byte) []byte {
+func packNum(value reflect.Value) []byte {
switch kind := value.Kind(); kind {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- if to == UintTy {
- return U2U256(value.Uint())
- } else {
- return S2S256(int64(value.Uint()))
- }
+ return U2U256(value.Uint())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- if to == UintTy {
- return U2U256(uint64(value.Int()))
- } else {
- return S2S256(value.Int())
- }
+ return U2U256(uint64(value.Int()))
case reflect.Ptr:
- // This only takes care of packing and casting. No type checking is done here. It should be done prior to using this function.
- if to == UintTy {
- return U256(value.Interface().(*big.Int))
- } else {
- return S256(value.Interface().(*big.Int))
- }
-
+ return U256(value.Interface().(*big.Int))
}
return nil