-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add discriminator to the account and allow encoding/decoding of non-a…
…ccount types
- Loading branch information
Showing
8 changed files
with
302 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package codec | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/codec/encodings" | ||
"github.com/smartcontractkit/chainlink-common/pkg/types" | ||
) | ||
|
||
const discriminatorLength = 8 | ||
|
||
func NewDiscriminator(name string) encodings.TypeCodec { | ||
sum := sha256.Sum256([]byte("account:" + name)) | ||
return &discriminator{hashPrefix: sum[:discriminatorLength]} | ||
} | ||
|
||
type discriminator struct { | ||
hashPrefix []byte | ||
} | ||
|
||
func (d discriminator) Encode(value any, into []byte) ([]byte, error) { | ||
if value == nil { | ||
return append(into, d.hashPrefix...), nil | ||
} | ||
|
||
raw, ok := value.(*[]byte) | ||
if !ok { | ||
return nil, fmt.Errorf("%w: value must be a byte slice got %T", types.ErrInvalidType, value) | ||
} | ||
|
||
// inject if not specified | ||
if raw == nil { | ||
return append(into, d.hashPrefix...), nil | ||
} | ||
|
||
// Not sure if we should really be encoding accounts... | ||
if !bytes.Equal(*raw, d.hashPrefix) { | ||
return nil, fmt.Errorf("%w: invalid discriminator expected %x got %x", types.ErrInvalidType, d.hashPrefix, raw) | ||
} | ||
|
||
return append(into, *raw...), nil | ||
} | ||
|
||
func (d discriminator) Decode(encoded []byte) (any, []byte, error) { | ||
raw, remaining, err := encodings.SafeDecode(encoded, discriminatorLength, func(raw []byte) []byte { return raw }) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
if !bytes.Equal(raw, d.hashPrefix) { | ||
return nil, nil, fmt.Errorf("%w: invalid discriminator expected %x got %x", types.ErrInvalidEncoding, d.hashPrefix, raw) | ||
} | ||
|
||
return &raw, remaining, nil | ||
} | ||
|
||
func (d discriminator) GetType() reflect.Type { | ||
// Pointer type so that nil can inject values and so that the NamedCodec won't wrap with no-nil pointer. | ||
return reflect.TypeOf(&[]byte{}) | ||
} | ||
|
||
func (d discriminator) Size(_ int) (int, error) { | ||
return discriminatorLength, nil | ||
} | ||
|
||
func (d discriminator) FixedSize() (int, error) { | ||
return discriminatorLength, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package codec_test | ||
|
||
import ( | ||
"crypto/sha256" | ||
"errors" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink-solana/pkg/solana/codec" | ||
) | ||
|
||
func TestDiscriminator(t *testing.T) { | ||
t.Run("encode and decode return the discriminator", func(t *testing.T) { | ||
tmp := sha256.Sum256([]byte("account:Foo")) | ||
expected := tmp[:8] | ||
c := codec.NewDiscriminator("Foo") | ||
encoded, err := c.Encode(&expected, nil) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, encoded) | ||
actual, remaining, err := c.Decode(encoded) | ||
require.NoError(t, err) | ||
require.Equal(t, &expected, actual) | ||
require.Len(t, remaining, 0) | ||
}) | ||
|
||
t.Run("encode returns an error if the discriminator is invalid", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
_, err := c.Encode(&[]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, nil) | ||
require.True(t, errors.Is(err, types.ErrInvalidType)) | ||
}) | ||
|
||
t.Run("encode injects the discriminator if it's not provided", func(t *testing.T) { | ||
tmp := sha256.Sum256([]byte("account:Foo")) | ||
expected := tmp[:8] | ||
c := codec.NewDiscriminator("Foo") | ||
encoded, err := c.Encode(nil, nil) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, encoded) | ||
encoded, err = c.Encode((*[]byte)(nil), nil) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, encoded) | ||
}) | ||
|
||
t.Run("decode returns an error if the encoded value is too short", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
_, _, err := c.Decode([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}) | ||
require.True(t, errors.Is(err, types.ErrInvalidEncoding)) | ||
}) | ||
|
||
t.Run("decode returns an error if the discriminator is invalid", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
_, _, err := c.Decode([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}) | ||
require.True(t, errors.Is(err, types.ErrInvalidEncoding)) | ||
}) | ||
|
||
t.Run("encode returns an error if the value is not a byte slice", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
_, err := c.Encode(42, nil) | ||
require.True(t, errors.Is(err, types.ErrInvalidType)) | ||
}) | ||
|
||
t.Run("GetType returns the type of the discriminator", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
require.Equal(t, reflect.TypeOf(&[]byte{}), c.GetType()) | ||
}) | ||
|
||
t.Run("Size returns the length of the discriminator", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
size, err := c.Size(0) | ||
require.NoError(t, err) | ||
require.Equal(t, 8, size) | ||
}) | ||
|
||
t.Run("FixedSize returns the length of the discriminator", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
size, err := c.FixedSize() | ||
require.NoError(t, err) | ||
require.Equal(t, 8, size) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.