sdk/go: Add unmarshal for vaa.Address (#2076)
* sdk/go: Add unmarshal for vaa.Address Change-Id: I1beb99f82673d1fc3225a8c6628a0019648d7e01 * sdk/go: review rework Change-Id: I7c9179e674c019f46eebff13a071f997f20572f3
This commit is contained in:
parent
759550715a
commit
b38dfc015f
|
@ -123,6 +123,17 @@ func (a Address) MarshalJSON() ([]byte, error) {
|
|||
return []byte(fmt.Sprintf(`"%s"`, a)), nil
|
||||
}
|
||||
|
||||
// Standard marshal stores the Address like this: "[0,0,0,0,0,0,0,0,0,0,0,0,2,144,251,22,114,8,175,69,91,177,55,120,1,99,183,183,169,161,12,22]"
|
||||
// The above MarshalJSON stores it like this (66 bytes): ""0000000000000000000000000290fb167208af455bb137780163b7b7a9a10c16""
|
||||
func (a *Address) UnmarshalJSON(data []byte) error {
|
||||
addr, err := StringToAddress(strings.Trim(string(data), `"`))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*a = addr
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a Address) String() string {
|
||||
return hex.EncodeToString(a[:])
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
@ -108,6 +109,59 @@ func TestAddress_MarshalJSON(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestAddress_UnmarshalJSON(t *testing.T) {
|
||||
addr, _ := StringToAddress("0x0290fb167208af455bb137780163b7b7a9a10c16")
|
||||
|
||||
b, err := addr.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
|
||||
var unmarshalAddr Address
|
||||
err = unmarshalAddr.UnmarshalJSON(b)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, addr, unmarshalAddr)
|
||||
}
|
||||
|
||||
func TestAddress_Unmarshal(t *testing.T) {
|
||||
addr, _ := StringToAddress("0x0290fb167208af455bb137780163b7b7a9a10c16")
|
||||
|
||||
b, err := json.Marshal(addr)
|
||||
require.NoError(t, err)
|
||||
|
||||
var unmarshalAddr Address
|
||||
err = json.Unmarshal(b, &unmarshalAddr)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, addr, unmarshalAddr)
|
||||
}
|
||||
|
||||
func TestAddress_UnmarshalEmptyBuffer(t *testing.T) {
|
||||
b := []byte{}
|
||||
|
||||
var unmarshalAddr Address
|
||||
err := json.Unmarshal(b, &unmarshalAddr)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestAddress_UnmarshalBufferTooShort(t *testing.T) {
|
||||
addr, _ := StringToAddress("0x0290fb167208af455bb137780163b7b7a9a10c16")
|
||||
|
||||
b, err := json.Marshal(addr)
|
||||
require.NoError(t, err)
|
||||
|
||||
var unmarshalAddr Address
|
||||
|
||||
// Lop off the first byte, and it should fail.
|
||||
b1 := b[1:]
|
||||
err = json.Unmarshal(b1, &unmarshalAddr)
|
||||
assert.Error(t, err)
|
||||
|
||||
// Lop off the last byte, and it should fail.
|
||||
b2 := b[0 : len(b)-1]
|
||||
err = json.Unmarshal(b2, &unmarshalAddr)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestAddress_String(t *testing.T) {
|
||||
addr := Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
|
||||
expected := "0000000000000000000000000000000000000000000000000000000000000004"
|
||||
|
|
Loading…
Reference in New Issue