diff options
author | Jeffrey Wilcke <obscuren@users.noreply.github.com> | 2014-12-17 20:00:05 +0800 |
---|---|---|
committer | Jeffrey Wilcke <obscuren@users.noreply.github.com> | 2014-12-17 20:00:05 +0800 |
commit | 8da07e91e40c1d1bb43763b7e959ae92e5770af2 (patch) | |
tree | 0d2631ec0b9324f08fcd2e82cec797fab75e9d4d /ethutil/rlp.go | |
parent | ef4135eabe5cb25f8972371c5681e1611ce0cde9 (diff) | |
parent | b1c58b76a9588a90db5a773a997bb70265c378d3 (diff) | |
download | dexon-8da07e91e40c1d1bb43763b7e959ae92e5770af2.tar dexon-8da07e91e40c1d1bb43763b7e959ae92e5770af2.tar.gz dexon-8da07e91e40c1d1bb43763b7e959ae92e5770af2.tar.bz2 dexon-8da07e91e40c1d1bb43763b7e959ae92e5770af2.tar.lz dexon-8da07e91e40c1d1bb43763b7e959ae92e5770af2.tar.xz dexon-8da07e91e40c1d1bb43763b7e959ae92e5770af2.tar.zst dexon-8da07e91e40c1d1bb43763b7e959ae92e5770af2.zip |
Merge pull request #213 from ethereum/develop
moved err check
Diffstat (limited to 'ethutil/rlp.go')
-rw-r--r-- | ethutil/rlp.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/ethutil/rlp.go b/ethutil/rlp.go index 1fff2b28a..157dd4dd9 100644 --- a/ethutil/rlp.go +++ b/ethutil/rlp.go @@ -2,8 +2,10 @@ package ethutil import ( "bytes" + "encoding/binary" "fmt" "math/big" + "reflect" ) type RlpEncode interface { @@ -97,6 +99,14 @@ var ( zeroRlp = big.NewInt(0x0) ) +func intlen(i int64) (length int) { + for i > 0 { + i = i >> 8 + length++ + } + return +} + func Encode(object interface{}) []byte { var buff bytes.Buffer @@ -168,6 +178,26 @@ func Encode(object interface{}) []byte { } WriteSliceHeader(len(b.Bytes())) buff.Write(b.Bytes()) + default: + // This is how it should have been from the start + // needs refactoring (@fjl) + v := reflect.ValueOf(t) + switch v.Kind() { + case reflect.Slice: + var b bytes.Buffer + for i := 0; i < v.Len(); i++ { + b.Write(Encode(v.Index(i).Interface())) + } + + blen := b.Len() + if blen < 56 { + buff.WriteByte(byte(blen) + 0xc0) + } else { + buff.WriteByte(byte(intlen(int64(blen))) + 0xf7) + binary.Write(&buff, binary.BigEndian, int64(blen)) + } + buff.ReadFrom(&b) + } } } else { // Empty list for nil |