Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/solana/solana_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,15 +1024,18 @@ func convertDataBytesOrJSON(obj *rpc.DataBytesOrJSON, pref commonsol.EncodingTyp
}

switch pref {
case commonsol.EncodingBase64:
case commonsol.EncodingBase58, commonsol.EncodingBase64, commonsol.EncodingBase64Zstd:
if len(txBytes) != 0 {
return &commonsol.DataBytesOrJSON{
RawDataEncoding: commonsol.EncodingBase64,
RawDataEncoding: pref,
AsDecodedBinary: txBytes,
AsJSON: txJSON,
}, nil
}

if pref != commonsol.EncodingBase64 {
return nil, fmt.Errorf("expected binary account data for encoding %q but got empty bytes: %s", pref, truncateDiag(string(txJSON)))
}

// Fallback: decode ["<base64>", "base64"] manually
var arr []string
if err := json.Unmarshal(txJSON, &arr); err != nil {
Expand All @@ -1056,14 +1059,12 @@ func convertDataBytesOrJSON(obj *rpc.DataBytesOrJSON, pref commonsol.EncodingTyp
return &commonsol.DataBytesOrJSON{
RawDataEncoding: commonsol.EncodingBase64,
AsDecodedBinary: b,
AsJSON: txJSON,
}, nil

case commonsol.EncodingJSON, commonsol.EncodingJSONParsed:
// Caller explicitly wants JSON. Return it even if bytes exist.
// Caller explicitly wants JSON.
return &commonsol.DataBytesOrJSON{
RawDataEncoding: pref,
AsDecodedBinary: txBytes,
AsJSON: txJSON,
}, nil

Expand All @@ -1075,7 +1076,6 @@ func convertDataBytesOrJSON(obj *rpc.DataBytesOrJSON, pref commonsol.EncodingTyp
return &commonsol.DataBytesOrJSON{
RawDataEncoding: commonsol.EncodingBase64,
AsDecodedBinary: txBytes,
AsJSON: txJSON,
}, nil
}
}
Expand Down
21 changes: 18 additions & 3 deletions pkg/solana/solana_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ func TestConvertDataBytesOrJSON(t *testing.T) {
require.NotNil(t, got)
assert.Equal(t, commonsol.EncodingBase64, got.RawDataEncoding)
assert.Equal(t, raw, got.AsDecodedBinary)
assert.NotNil(t, got.AsJSON)
assert.Nil(t, got.AsJSON)
})

t.Run("base64 explicit pref with binary data", func(t *testing.T) {
Expand All @@ -599,6 +599,19 @@ func TestConvertDataBytesOrJSON(t *testing.T) {
require.NotNil(t, got)
assert.Equal(t, commonsol.EncodingBase64, got.RawDataEncoding)
assert.Equal(t, raw, got.AsDecodedBinary)
assert.Nil(t, got.AsJSON)
})

t.Run("base58 explicit pref sets decoded bytes only", func(t *testing.T) {
raw := []byte{0x05, 0x06, 0x07}
obj := rpc.DataBytesOrJSONFromBytes(raw)

got, err := convertDataBytesOrJSON(obj, commonsol.EncodingBase58)
require.NoError(t, err)
require.NotNil(t, got)
assert.Equal(t, commonsol.EncodingBase58, got.RawDataEncoding)
assert.Equal(t, raw, got.AsDecodedBinary)
assert.Nil(t, got.AsJSON)
})

t.Run("JSON fallback with EncodingJSON", func(t *testing.T) {
Expand All @@ -610,7 +623,7 @@ func TestConvertDataBytesOrJSON(t *testing.T) {
require.NotNil(t, got)
assert.Equal(t, commonsol.EncodingJSON, got.RawDataEncoding)
assert.NotNil(t, got.AsJSON)
assert.Equal(t, raw, got.AsDecodedBinary)
assert.Nil(t, got.AsDecodedBinary)
})

t.Run("JSON fallback with EncodingJSONParsed", func(t *testing.T) {
Expand All @@ -622,7 +635,7 @@ func TestConvertDataBytesOrJSON(t *testing.T) {
require.NotNil(t, got)
assert.Equal(t, commonsol.EncodingJSONParsed, got.RawDataEncoding)
assert.NotNil(t, got.AsJSON)
assert.Equal(t, raw, got.AsDecodedBinary)
assert.Nil(t, got.AsDecodedBinary)
})

t.Run("base64 fallback parses json array when GetBinary is empty", func(t *testing.T) {
Expand All @@ -637,6 +650,7 @@ func TestConvertDataBytesOrJSON(t *testing.T) {
require.NotNil(t, got)
assert.Equal(t, commonsol.EncodingBase64, got.RawDataEncoding)
assert.Equal(t, []byte("hello world"), got.AsDecodedBinary)
assert.Nil(t, got.AsJSON)
})

t.Run("unknown encoding with binary data falls back to base64", func(t *testing.T) {
Expand All @@ -648,6 +662,7 @@ func TestConvertDataBytesOrJSON(t *testing.T) {
require.NotNil(t, got)
assert.Equal(t, commonsol.EncodingBase64, got.RawDataEncoding)
assert.Equal(t, raw, got.AsDecodedBinary)
assert.Nil(t, got.AsJSON)
})
}

Expand Down
Loading