diff --git a/pkg/owner/id.go b/pkg/owner/id.go index 08971e8..49acb2c 100644 --- a/pkg/owner/id.go +++ b/pkg/owner/id.go @@ -71,3 +71,35 @@ func (id *ID) Parse(s string) error { return nil } + +// Marshal marshals ID into a protobuf binary form. +// +// Buffer is allocated when the argument is empty. +// Otherwise, the first buffer is used. +func (id *ID) Marshal(b ...[]byte) ([]byte, error) { + var buf []byte + if len(b) > 0 { + buf = b[0] + } + + return (*refs.OwnerID)(id). + StableMarshal(buf) +} + +// Unmarshal unmarshals protobuf binary representation of ID. +func (id *ID) Unmarshal(data []byte) error { + return (*refs.OwnerID)(id). + Unmarshal(data) +} + +// MarshalJSON encodes ID to protobuf JSON format. +func (id *ID) MarshalJSON() ([]byte, error) { + return (*refs.OwnerID)(id). + MarshalJSON() +} + +// UnmarshalJSON decodes ID from protobuf JSON format. +func (id *ID) UnmarshalJSON(data []byte) error { + return (*refs.OwnerID)(id). + UnmarshalJSON(data) +} diff --git a/pkg/owner/id_test.go b/pkg/owner/id_test.go index a36324f..8d867df 100644 --- a/pkg/owner/id_test.go +++ b/pkg/owner/id_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestIDV2(t *testing.T) { +func randID(t *testing.T) *ID { id := NewID() wallet := new(NEO3Wallet) @@ -20,9 +20,15 @@ func TestIDV2(t *testing.T) { id.SetNeo3Wallet(wallet) + return id +} + +func TestIDV2(t *testing.T) { + id := randID(t) + idV2 := id.ToV2() - require.Equal(t, wallet.Bytes(), idV2.GetValue()) + require.Equal(t, id, NewIDFromV2(idV2)) } func TestNewIDFromNeo3Wallet(t *testing.T) { @@ -63,3 +69,27 @@ func TestID_Parse(t *testing.T) { } }) } + +func TestIDEncoding(t *testing.T) { + id := randID(t) + + t.Run("binary", func(t *testing.T) { + data, err := id.Marshal() + require.NoError(t, err) + + id2 := NewID() + require.NoError(t, id2.Unmarshal(data)) + + require.Equal(t, id, id2) + }) + + t.Run("json", func(t *testing.T) { + data, err := id.MarshalJSON() + require.NoError(t, err) + + a2 := NewID() + require.NoError(t, a2.UnmarshalJSON(data)) + + require.Equal(t, id, a2) + }) +} diff --git a/v2/refs/json.go b/v2/refs/json.go index d2fd4ed..e552ae1 100644 --- a/v2/refs/json.go +++ b/v2/refs/json.go @@ -64,3 +64,23 @@ func (c *ContainerID) UnmarshalJSON(data []byte) error { return nil } + +func (o *OwnerID) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + OwnerIDToGRPCMessage(o), + ) +} + +func (o *OwnerID) UnmarshalJSON(data []byte) error { + msg := new(refs.OwnerID) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *o = *OwnerIDFromGRPCMessage(msg) + + return nil +} diff --git a/v2/refs/json_test.go b/v2/refs/json_test.go index b56b1d0..c5684e9 100644 --- a/v2/refs/json_test.go +++ b/v2/refs/json_test.go @@ -44,3 +44,16 @@ func TestContainerIDJSON(t *testing.T) { require.Equal(t, cid, cid2) } + +func TestOwnerIDJSON(t *testing.T) { + o := new(refs.OwnerID) + o.SetValue([]byte{1}) + + data, err := o.MarshalJSON() + require.NoError(t, err) + + o2 := new(refs.OwnerID) + require.NoError(t, o2.UnmarshalJSON(data)) + + require.Equal(t, o, o2) +} diff --git a/v2/refs/marshal.go b/v2/refs/marshal.go index 37b950d..0a2484a 100644 --- a/v2/refs/marshal.go +++ b/v2/refs/marshal.go @@ -51,6 +51,17 @@ func (o *OwnerID) StableSize() int { return proto.BytesSize(ownerIDValField, o.val) } +func (o *OwnerID) Unmarshal(data []byte) error { + m := new(refs.OwnerID) + if err := goproto.Unmarshal(data, m); err != nil { + return err + } + + *o = *OwnerIDFromGRPCMessage(m) + + return nil +} + func (c *ContainerID) StableMarshal(buf []byte) ([]byte, error) { if c == nil { return []byte{}, nil diff --git a/v2/refs/marshal_test.go b/v2/refs/marshal_test.go index d0eb75f..4c12ea2 100644 --- a/v2/refs/marshal_test.go +++ b/v2/refs/marshal_test.go @@ -11,7 +11,6 @@ import ( func TestOwnerID_StableMarshal(t *testing.T) { ownerFrom := new(refs.OwnerID) - ownerTransport := new(grpc.OwnerID) t.Run("non empty", func(t *testing.T) { ownerFrom.SetValue([]byte("Owner ID")) @@ -19,10 +18,9 @@ func TestOwnerID_StableMarshal(t *testing.T) { wire, err := ownerFrom.StableMarshal(nil) require.NoError(t, err) - err = goproto.Unmarshal(wire, ownerTransport) - require.NoError(t, err) + ownerTo := new(refs.OwnerID) + require.NoError(t, ownerTo.Unmarshal(wire)) - ownerTo := refs.OwnerIDFromGRPCMessage(ownerTransport) require.Equal(t, ownerFrom, ownerTo) }) }