-
Notifications
You must be signed in to change notification settings - Fork 3
Solana bindings #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Solana bindings #102
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d4d7a18
sol-bindings
yashnevatia eb63f51
add fwdReport marshal
yashnevatia 1f2d520
plural
yashnevatia b722136
tidy
yashnevatia 6cfdf64
vulnerability fix
yashnevatia 9d7b9ed
Merge branch 'main' into sol-bindings
yashnevatia 7bfb439
adding tests and improving code
yashnevatia 895d92e
Merge branch 'sol-bindings' of ssh://github.com/smartcontractkit/cre-…
yashnevatia de16870
Merge branch 'main' into sol-bindings
yashnevatia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,57 @@ | ||
| package bindings | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto/sha256" | ||
| "fmt" | ||
|
|
||
| binary "github.com/gagliardetto/binary" | ||
|
|
||
| "github.com/smartcontractkit/cre-sdk-go/capabilities/blockchain/solana" | ||
| ) | ||
|
|
||
| // ForwarderReport represents the Borsh-serialized report format expected by | ||
| // the Solana keystone-forwarder program's on_report instruction. | ||
| type ForwarderReport struct { | ||
| AccountHash [32]byte | ||
| Payload []byte | ||
| } | ||
|
|
||
| // MarshalWithEncoder serializes the ForwarderReport using the provided Borsh encoder. | ||
| func (obj ForwarderReport) MarshalWithEncoder(encoder *binary.Encoder) (err error) { | ||
| // Serialize `AccountHash`: | ||
| err = encoder.Encode(obj.AccountHash) | ||
| if err != nil { | ||
| return fmt.Errorf("field AccountHash: %w", err) | ||
| } | ||
| // Serialize `Payload`: | ||
| err = encoder.Encode(obj.Payload) | ||
| if err != nil { | ||
| return fmt.Errorf("field Payload: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Marshal serializes the ForwarderReport into Borsh-encoded bytes. | ||
| func (obj ForwarderReport) Marshal() ([]byte, error) { | ||
| buf := bytes.NewBuffer(nil) | ||
| encoder := binary.NewBorshEncoder(buf) | ||
| err := obj.MarshalWithEncoder(encoder) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error while encoding ForwarderReport: %w", err) | ||
| } | ||
| return buf.Bytes(), nil | ||
| } | ||
|
|
||
| // CalculateAccountsHash computes the SHA-256 hash of the concatenated public | ||
| // keys of the given accounts, matching the on-chain account hash verification. | ||
| func CalculateAccountsHash(accs []*solana.AccountMeta) [32]byte { | ||
| accounts := make([]byte, 0, len(accs)*32) | ||
| for _, acc := range accs { | ||
| if acc == nil { | ||
| continue | ||
| } | ||
| accounts = append(accounts, acc.PublicKey...) | ||
| } | ||
| return sha256.Sum256(accounts) | ||
| } | ||
This file contains hidden or 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,133 @@ | ||
| package bindings | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/binary" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/smartcontractkit/cre-sdk-go/capabilities/blockchain/solana" | ||
| ) | ||
|
|
||
| func TestForwarderReport_Marshal(t *testing.T) { | ||
| t.Run("encodes to expected Borsh format", func(t *testing.T) { | ||
| hash := [32]byte{ | ||
| 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, | ||
| 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, | ||
| 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, | ||
| 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, | ||
| } | ||
| payload := []byte("hello solana") | ||
|
|
||
| report := ForwarderReport{ | ||
| AccountHash: hash, | ||
| Payload: payload, | ||
| } | ||
|
|
||
| data, err := report.Marshal() | ||
| require.NoError(t, err) | ||
|
|
||
| // Borsh format: account_hash(32) | payload_len(u32 LE) | payload | ||
| expectedLen := 32 + 4 + len(payload) | ||
| require.Len(t, data, expectedLen) | ||
|
|
||
| // First 32 bytes: account hash | ||
| assert.Equal(t, hash[:], data[:32]) | ||
|
|
||
| // Next 4 bytes: little-endian u32 payload length | ||
| payloadLen := binary.LittleEndian.Uint32(data[32:36]) | ||
| assert.Equal(t, uint32(len(payload)), payloadLen) | ||
|
|
||
| // Remaining bytes: payload | ||
| assert.Equal(t, payload, data[36:]) | ||
| }) | ||
| } | ||
|
|
||
| func TestCalculateAccountsHash(t *testing.T) { | ||
| t.Run("single account", func(t *testing.T) { | ||
| key := make([]byte, 32) | ||
| for i := range key { | ||
| key[i] = byte(i) | ||
| } | ||
|
|
||
| accs := []*solana.AccountMeta{ | ||
| {PublicKey: key}, | ||
| } | ||
|
|
||
| got := CalculateAccountsHash(accs) | ||
| expected := sha256.Sum256(key) | ||
| assert.Equal(t, expected, got) | ||
| }) | ||
|
|
||
| t.Run("multiple accounts", func(t *testing.T) { | ||
| key1 := make([]byte, 32) | ||
| key2 := make([]byte, 32) | ||
| for i := range key1 { | ||
| key1[i] = byte(i) | ||
| key2[i] = byte(i + 32) | ||
| } | ||
|
|
||
| accs := []*solana.AccountMeta{ | ||
| {PublicKey: key1}, | ||
| {PublicKey: key2}, | ||
| } | ||
|
|
||
| got := CalculateAccountsHash(accs) | ||
|
|
||
| // Hash should be SHA-256 of concatenated keys | ||
| concat := append(key1, key2...) | ||
| expected := sha256.Sum256(concat) | ||
| assert.Equal(t, expected, got) | ||
| }) | ||
|
|
||
| t.Run("empty slice", func(t *testing.T) { | ||
| got := CalculateAccountsHash([]*solana.AccountMeta{}) | ||
| expected := sha256.Sum256([]byte{}) | ||
| assert.Equal(t, expected, got) | ||
| }) | ||
|
|
||
| t.Run("nil slice", func(t *testing.T) { | ||
| got := CalculateAccountsHash(nil) | ||
| expected := sha256.Sum256([]byte{}) | ||
| assert.Equal(t, expected, got) | ||
| }) | ||
|
|
||
| t.Run("skips nil entries", func(t *testing.T) { | ||
| key := make([]byte, 32) | ||
| for i := range key { | ||
| key[i] = byte(i + 100) | ||
| } | ||
|
|
||
| accs := []*solana.AccountMeta{ | ||
| nil, | ||
| {PublicKey: key}, | ||
| nil, | ||
| } | ||
|
|
||
| got := CalculateAccountsHash(accs) | ||
| expected := sha256.Sum256(key) | ||
| assert.Equal(t, expected, got) | ||
| }) | ||
|
|
||
| t.Run("order matters", func(t *testing.T) { | ||
| key1 := make([]byte, 32) | ||
| key2 := make([]byte, 32) | ||
| for i := range key1 { | ||
| key1[i] = byte(i) | ||
| key2[i] = byte(i + 32) | ||
| } | ||
|
|
||
| hash12 := CalculateAccountsHash([]*solana.AccountMeta{ | ||
| {PublicKey: key1}, | ||
| {PublicKey: key2}, | ||
| }) | ||
| hash21 := CalculateAccountsHash([]*solana.AccountMeta{ | ||
| {PublicKey: key2}, | ||
| {PublicKey: key1}, | ||
| }) | ||
|
|
||
| assert.NotEqual(t, hash12, hash21, "hash should depend on account order") | ||
| }) | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know enough about Solana to review this, but are you planning on having mock helpers like EVM does that the bindings can use to generate mocks for your contract? If so, is it going to be in another PR?
Essentially, the EVM one allows you to bind your contract to an EVM client, so you can simulate the read / write using the types that the contract would receive and respond with instead of requiring you decode and encode. Also, it ensures your'll calling the right contract if you bind to multiple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nolag created a ticket for this work
https://smartcontract-it.atlassian.net/browse/PLEX-2530