-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BCFR-147][solana] - Add codec chain agnostic modifier for converting…
… byte array address to string (#875) * working tests * bump common * bump common * chain agnostic modifer * bump common * fix lint * addresing comments * bump common * manually retype test * bump common * inject modifier during runtime * remove unnecessary skip * fix integration tests deps * general name * refactor helper func * bump common * update mercury dep * update common ref * integration tests fix
- Loading branch information
Showing
8 changed files
with
182 additions
and
22 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
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
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,40 @@ | ||
package codec | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gagliardetto/solana-go" | ||
|
||
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types" | ||
) | ||
|
||
// SolanaAddressModifier implements the AddressModifier interface for Solana addresses. | ||
// It handles encoding and decoding Solana addresses using Base58 encoding. | ||
type SolanaAddressModifier struct{} | ||
|
||
// EncodeAddress encodes a Solana address (32-byte array) into a Base58 string. | ||
func (s SolanaAddressModifier) EncodeAddress(bytes []byte) (string, error) { | ||
if len(bytes) != s.Length() { | ||
return "", fmt.Errorf("%w: got length %d, expected 32 for bytes %x", commontypes.ErrInvalidType, len(bytes), bytes) | ||
} | ||
return solana.PublicKeyFromBytes(bytes).String(), nil | ||
} | ||
|
||
// DecodeAddress decodes a Base58-encoded Solana address into a 32-byte array. | ||
func (s SolanaAddressModifier) DecodeAddress(str string) ([]byte, error) { | ||
if len(str) != 44 { | ||
return nil, fmt.Errorf("%w: got length %d, expected 44 for address %s", commontypes.ErrInvalidType, len(str), str) | ||
} | ||
|
||
pubkey, err := solana.PublicKeyFromBase58(str) | ||
if err != nil { | ||
return nil, fmt.Errorf("%w: failed to decode Base58 address: %s", commontypes.ErrInvalidType, err) | ||
} | ||
|
||
return pubkey.Bytes(), nil | ||
} | ||
|
||
// Length returns the expected length of a Solana address in bytes (32 bytes). | ||
func (s SolanaAddressModifier) Length() int { | ||
return solana.PublicKeyLength | ||
} |
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,57 @@ | ||
package codec_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gagliardetto/solana-go" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types" | ||
|
||
"github.com/smartcontractkit/chainlink-solana/pkg/solana/codec" | ||
) | ||
|
||
func TestSolanaAddressModifier(t *testing.T) { | ||
modifier := codec.SolanaAddressModifier{} | ||
|
||
// Valid Solana address (32 bytes, Base58 encoded) | ||
validAddressStr := "9nQhQ7iCyY5SgAX2Zm4DtxNh9Ubc4vbiLkiYbX43SDXY" | ||
validAddressBytes := solana.MustPublicKeyFromBase58(validAddressStr).Bytes() | ||
invalidLengthAddressStr := "abc123" | ||
|
||
t.Run("EncodeAddress encodes valid Solana address bytes", func(t *testing.T) { | ||
encoded, err := modifier.EncodeAddress(validAddressBytes) | ||
require.NoError(t, err) | ||
assert.Equal(t, validAddressStr, encoded) | ||
}) | ||
|
||
t.Run("EncodeAddress returns error for invalid byte length", func(t *testing.T) { | ||
invalidBytes := []byte(invalidLengthAddressStr) | ||
_, err := modifier.EncodeAddress(invalidBytes) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), commontypes.ErrInvalidType.Error()) | ||
}) | ||
|
||
t.Run("DecodeAddress decodes valid Solana address", func(t *testing.T) { | ||
decodedBytes, err := modifier.DecodeAddress(validAddressStr) | ||
require.NoError(t, err) | ||
assert.Equal(t, validAddressBytes, decodedBytes) | ||
}) | ||
|
||
t.Run("DecodeAddress returns error for invalid address length", func(t *testing.T) { | ||
_, err := modifier.DecodeAddress(invalidLengthAddressStr) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), commontypes.ErrInvalidType.Error()) | ||
}) | ||
|
||
t.Run("DecodeAddress returns error for zero-value address", func(t *testing.T) { | ||
_, err := modifier.DecodeAddress(solana.PublicKey{}.String()) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), commontypes.ErrInvalidType.Error()) | ||
}) | ||
|
||
t.Run("Length returns 32 for Solana addresses", func(t *testing.T) { | ||
assert.Equal(t, solana.PublicKeyLength, modifier.Length()) | ||
}) | ||
} |