diff options
author | obscuren <obscuren@obscura.com> | 2013-12-28 04:20:47 +0800 |
---|---|---|
committer | obscuren <obscuren@obscura.com> | 2013-12-28 04:20:47 +0800 |
commit | d5d2efbaf3c3a84f0084058eacd535d077c363e1 (patch) | |
tree | f96fff4f901a42cff27b488b77ee2c7f2b6b8f98 /rlp.go | |
parent | b40013ac30006fe5a43abdca574b438cf60db86e (diff) | |
download | dexon-d5d2efbaf3c3a84f0084058eacd535d077c363e1.tar dexon-d5d2efbaf3c3a84f0084058eacd535d077c363e1.tar.gz dexon-d5d2efbaf3c3a84f0084058eacd535d077c363e1.tar.bz2 dexon-d5d2efbaf3c3a84f0084058eacd535d077c363e1.tar.lz dexon-d5d2efbaf3c3a84f0084058eacd535d077c363e1.tar.xz dexon-d5d2efbaf3c3a84f0084058eacd535d077c363e1.tar.zst dexon-d5d2efbaf3c3a84f0084058eacd535d077c363e1.zip |
WIP new rlp implementation
Diffstat (limited to 'rlp.go')
-rw-r--r-- | rlp.go | 52 |
1 files changed, 52 insertions, 0 deletions
@@ -0,0 +1,52 @@ +package main + +import ( + _"fmt" + "bytes" + "math" +) + +func EncodeSlice(slice []interface{}) []byte { + var buff bytes.Buffer + + for _, val := range slice { + switch t := val.(type) { + case []interface{}, []string: + buff.Write(Encode(t)) + } + } + + return buff.Bytes() +} + +func Encode(object interface{}) []byte { + var buff bytes.Buffer + + switch t := object.(type) { + case string: + if len(t) < 56 { + buff.WriteString(string(len(t) + 64) + t) + } else { + + } + case uint32, uint64: + var num uint64 + if _num, ok := t.(uint64); ok { + num = _num + } else if _num, ok := t.(uint32); ok { + num = uint64(_num) + } + + if num >= 0 && num < 24 { + buff.WriteString(string(num)) + } else if num <= uint64(math.Pow(2, 256)) { + } + case []byte: + // Cast the byte slice to a string + buff.Write(Encode(string(t))) + case []interface{}: + buff.Write(EncodeSlice(t)) + } + + return buff.Bytes() +} |