diff --git a/xdr/asset.go b/xdr/asset.go index be3dc64840..4749e49940 100644 --- a/xdr/asset.go +++ b/xdr/asset.go @@ -268,44 +268,6 @@ func (a Asset) StringCanonical() string { return fmt.Sprintf("%s:%s", c, i) } -func trimRightZeros(b []byte) []byte { - if len(b) == 0 { - return b - } - i := len(b) - for ; i > 0; i-- { - if b[i-1] != 0 { - break - } - } - return b[:i] -} - -func (e *EncodingBuffer) assetCompressEncodeTo(a Asset) error { - if err := e.xdrEncoderBuf.WriteByte(byte(a.Type)); err != nil { - return err - } - - switch a.Type { - case AssetTypeAssetTypeNative: - return nil - case AssetTypeAssetTypeCreditAlphanum4: - code := trimRightZeros(a.AlphaNum4.AssetCode[:]) - if _, err := e.xdrEncoderBuf.Write(code); err != nil { - return err - } - return e.accountIdCompressEncodeTo(a.AlphaNum4.Issuer) - case AssetTypeAssetTypeCreditAlphanum12: - code := trimRightZeros(a.AlphaNum12.AssetCode[:]) - if _, err := e.xdrEncoderBuf.Write(code); err != nil { - return err - } - return e.accountIdCompressEncodeTo(a.AlphaNum12.Issuer) - default: - panic(fmt.Errorf("Unknown asset type: %v", a.Type)) - } -} - // Equals returns true if `other` is equivalent to `a` func (a Asset) Equals(other Asset) bool { if a.Type != other.Type { diff --git a/xdr/asset_test.go b/xdr/asset_test.go index 2deacec7ae..ce9902f273 100644 --- a/xdr/asset_test.go +++ b/xdr/asset_test.go @@ -1,8 +1,10 @@ -package xdr +package xdr_test import ( "testing" + . "github.com/stellar/go/xdr" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/stretchr/testify/assert" @@ -111,22 +113,6 @@ func TestStringCanonical(t *testing.T) { require.Equal(t, "USD:GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H", asset.StringCanonical()) } -func TestTrimRightZeros(t *testing.T) { - require.Equal(t, []byte(nil), trimRightZeros(nil)) - require.Equal(t, []byte{}, trimRightZeros([]byte{})) - require.Equal(t, []byte{}, trimRightZeros([]byte{0x0})) - require.Equal(t, []byte{}, trimRightZeros([]byte{0x0, 0x0})) - require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1})) - require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1, 0x0})) - require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1, 0x0, 0x0})) - require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1, 0x0, 0x0, 0x0})) - require.Equal(t, []byte{0x1, 0x2}, trimRightZeros([]byte{0x1, 0x2})) - require.Equal(t, []byte{0x1, 0x2}, trimRightZeros([]byte{0x1, 0x2, 0x0})) - require.Equal(t, []byte{0x1, 0x2}, trimRightZeros([]byte{0x1, 0x2, 0x0, 0x0})) - require.Equal(t, []byte{0x0, 0x2}, trimRightZeros([]byte{0x0, 0x2, 0x0, 0x0})) - require.Equal(t, []byte{0x0, 0x2, 0x0, 0x1}, trimRightZeros([]byte{0x0, 0x2, 0x0, 0x1, 0x0})) -} - var _ = Describe("xdr.Asset#Equals()", func() { var ( issuer1 AccountId diff --git a/xdr/ledger_key_test.go b/xdr/ledger_key_test.go index 16fd9f336d..33f52f90b4 100644 --- a/xdr/ledger_key_test.go +++ b/xdr/ledger_key_test.go @@ -1,18 +1,18 @@ -package xdr_test +package xdr import ( "encoding/base64" "testing" - "github.com/stellar/go/xdr" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestLedgerKeyTrustLineBinaryMaxLength(t *testing.T) { - key := &xdr.LedgerKey{} + key := &LedgerKey{} err := key.SetTrustline( - xdr.MustAddress("GBFLTCDLOE6YQ74B66RH3S2UW5I2MKZ5VLTM75F4YMIWUIXRIFVNRNIF"), - xdr.MustNewCreditAsset("123456789012", "GBFLTCDLOE6YQ74B66RH3S2UW5I2MKZ5VLTM75F4YMIWUIXRIFVNRNIF").ToTrustLineAsset(), + MustAddress("GBFLTCDLOE6YQ74B66RH3S2UW5I2MKZ5VLTM75F4YMIWUIXRIFVNRNIF"), + MustNewCreditAsset("123456789012", "GBFLTCDLOE6YQ74B66RH3S2UW5I2MKZ5VLTM75F4YMIWUIXRIFVNRNIF").ToTrustLineAsset(), ) assert.NoError(t, err) @@ -22,3 +22,19 @@ func TestLedgerKeyTrustLineBinaryMaxLength(t *testing.T) { bcompressed := base64.StdEncoding.EncodeToString(compressed) assert.Equal(t, len(bcompressed), 124) } + +func TestTrimRightZeros(t *testing.T) { + require.Equal(t, []byte(nil), trimRightZeros(nil)) + require.Equal(t, []byte{}, trimRightZeros([]byte{})) + require.Equal(t, []byte{}, trimRightZeros([]byte{0x0})) + require.Equal(t, []byte{}, trimRightZeros([]byte{0x0, 0x0})) + require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1})) + require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1, 0x0})) + require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1, 0x0, 0x0})) + require.Equal(t, []byte{0x1}, trimRightZeros([]byte{0x1, 0x0, 0x0, 0x0})) + require.Equal(t, []byte{0x1, 0x2}, trimRightZeros([]byte{0x1, 0x2})) + require.Equal(t, []byte{0x1, 0x2}, trimRightZeros([]byte{0x1, 0x2, 0x0})) + require.Equal(t, []byte{0x1, 0x2}, trimRightZeros([]byte{0x1, 0x2, 0x0, 0x0})) + require.Equal(t, []byte{0x0, 0x2}, trimRightZeros([]byte{0x0, 0x2, 0x0, 0x0})) + require.Equal(t, []byte{0x0, 0x2, 0x0, 0x1}, trimRightZeros([]byte{0x0, 0x2, 0x0, 0x1, 0x0})) +} diff --git a/xdr/trust_line_asset.go b/xdr/trust_line_asset.go index 016848c0d7..d9eb9d4f6a 100644 --- a/xdr/trust_line_asset.go +++ b/xdr/trust_line_asset.go @@ -42,16 +42,40 @@ func (a TrustLineAsset) MustExtract(typ interface{}, code interface{}, issuer in } } +func trimRightZeros(b []byte) []byte { + if len(b) == 0 { + return b + } + i := len(b) + for ; i > 0; i-- { + if b[i-1] != 0 { + break + } + } + return b[:i] +} + func (e *EncodingBuffer) assetTrustlineCompressEncodeTo(a TrustLineAsset) error { + if err := e.xdrEncoderBuf.WriteByte(byte(a.Type)); err != nil { + return err + } + switch a.Type { - case AssetTypeAssetTypeNative, - AssetTypeAssetTypeCreditAlphanum4, - AssetTypeAssetTypeCreditAlphanum12: - return e.assetCompressEncodeTo(a.ToAsset()) - case AssetTypeAssetTypePoolShare: - if err := e.xdrEncoderBuf.WriteByte(byte(a.Type)); err != nil { + case AssetTypeAssetTypeNative: + return nil + case AssetTypeAssetTypeCreditAlphanum4: + code := trimRightZeros(a.AlphaNum4.AssetCode[:]) + if _, err := e.xdrEncoderBuf.Write(code); err != nil { return err } + return e.accountIdCompressEncodeTo(a.AlphaNum4.Issuer) + case AssetTypeAssetTypeCreditAlphanum12: + code := trimRightZeros(a.AlphaNum12.AssetCode[:]) + if _, err := e.xdrEncoderBuf.Write(code); err != nil { + return err + } + return e.accountIdCompressEncodeTo(a.AlphaNum12.Issuer) + case AssetTypeAssetTypePoolShare: _, err := e.xdrEncoderBuf.Write(a.LiquidityPoolId[:]) return err default: