aboutsummaryrefslogtreecommitdiffstats
path: root/rlp/encode.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-01-30 23:52:48 +0800
committerFelix Lange <fjl@twurst.com>2015-02-06 07:00:36 +0800
commit8c3095faf03fb6c8138e6b8691df49be4a6fffae (patch)
tree0734942413dcc168f0e59b609df4b28eb4ea34a1 /rlp/encode.go
parent410b35e9135baa86e92bc07e0ef85d04e3ac0561 (diff)
downloaddexon-8c3095faf03fb6c8138e6b8691df49be4a6fffae.tar.gz
dexon-8c3095faf03fb6c8138e6b8691df49be4a6fffae.tar.zst
dexon-8c3095faf03fb6c8138e6b8691df49be4a6fffae.zip
rlp: fix encoding of arrays with byte element type
Diffstat (limited to 'rlp/encode.go')
-rw-r--r--rlp/encode.go22
1 files changed, 21 insertions, 1 deletions
diff --git a/rlp/encode.go b/rlp/encode.go
index d80b66315..b453b5bf4 100644
--- a/rlp/encode.go
+++ b/rlp/encode.go
@@ -301,8 +301,10 @@ func makeWriter(typ reflect.Type) (writer, error) {
return writeUint, nil
case kind == reflect.String:
return writeString, nil
- case kind == reflect.Slice && typ.Elem().Kind() == reflect.Uint8 && !typ.Elem().Implements(encoderInterface):
+ case kind == reflect.Slice && isByte(typ.Elem()):
return writeBytes, nil
+ case kind == reflect.Array && isByte(typ.Elem()):
+ return writeByteArray, nil
case kind == reflect.Slice || kind == reflect.Array:
return makeSliceWriter(typ)
case kind == reflect.Struct:
@@ -314,6 +316,10 @@ func makeWriter(typ reflect.Type) (writer, error) {
}
}
+func isByte(typ reflect.Type) bool {
+ return typ.Kind() == reflect.Uint8 && !typ.Implements(encoderInterface)
+}
+
func writeUint(val reflect.Value, w *encbuf) error {
i := val.Uint()
if i == 0 {
@@ -358,6 +364,20 @@ func writeBytes(val reflect.Value, w *encbuf) error {
return nil
}
+func writeByteArray(val reflect.Value, w *encbuf) error {
+ if !val.CanAddr() {
+ // Slice requires the value to be addressable.
+ // Make it addressable by copying.
+ copy := reflect.New(val.Type()).Elem()
+ copy.Set(val)
+ val = copy
+ }
+ size := val.Len()
+ slice := val.Slice(0, size).Bytes()
+ w.encodeString(slice)
+ return nil
+}
+
func writeString(val reflect.Value, w *encbuf) error {
s := val.String()
w.encodeStringHeader(len(s))