aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil/rlp.go
diff options
context:
space:
mode:
Diffstat (limited to 'ethutil/rlp.go')
-rw-r--r--ethutil/rlp.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/ethutil/rlp.go b/ethutil/rlp.go
index 1fff2b28a..1bc1a58a7 100644
--- a/ethutil/rlp.go
+++ b/ethutil/rlp.go
@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"math/big"
+ "reflect"
)
type RlpEncode interface {
@@ -97,6 +98,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 +177,31 @@ 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 {
+ ilen := byte(intlen(int64(blen)))
+ buff.WriteByte(ilen + 0xf7)
+ t := make([]byte, ilen)
+ for i := byte(0); i < ilen; i++ {
+ t[ilen-i-1] = byte(blen >> (i * 8))
+ }
+ buff.Write(t)
+ }
+ buff.ReadFrom(&b)
+ }
}
} else {
// Empty list for nil