Skip to content

Commit

Permalink
xdr: Add function to obtain the type of a binary-compressed key (#5026)
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio authored Aug 24, 2023
1 parent cdc7114 commit 8371bee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 13 additions & 0 deletions xdr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

xdr "github.com/stellar/go-xdr/xdr3"

"github.com/stellar/go/support/errors"
)

Expand Down Expand Up @@ -230,6 +231,18 @@ func (e *EncodingBuffer) LedgerKeyUnsafeMarshalBinaryCompress(key LedgerKey) ([]
return e.xdrEncoderBuf.Bytes(), nil
}

// GetBinaryCompressedLedgerKeyType gets the key type from the result of LedgerKeyUnsafeMarshalBinaryCompress
func GetBinaryCompressedLedgerKeyType(compressedKey []byte) (LedgerEntryType, error) {
if len(compressedKey) < 1 {
return 0, errors.New("empty compressed ledger key")
}
result := LedgerEntryType(compressedKey[0])
if int(result) > len(ledgerEntryTypeMap)-1 {
return 0, fmt.Errorf("incorrect key type %d", result)
}
return result, nil
}

func (e *EncodingBuffer) MarshalBase64(encodable EncoderTo) (string, error) {
b, err := e.UnsafeMarshalBase64(encodable)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion xdr/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,11 @@ func TestLedgerKeyBinaryCompressCoverage(t *testing.T) {
)
assert.NoError(t, gxdr.Convert(shape, &ledgerKey))

_, err := e.LedgerKeyUnsafeMarshalBinaryCompress(ledgerKey)
compressed, err := e.LedgerKeyUnsafeMarshalBinaryCompress(ledgerKey)
assert.NoError(t, err)
keyType, err := GetBinaryCompressedLedgerKeyType(compressed)
assert.NoError(t, err)
assert.Equal(t, ledgerKey.Type, keyType)
}
}

Expand Down

0 comments on commit 8371bee

Please sign in to comment.