Skip to content

Commit

Permalink
txnbuild: Adds helper function to craft assets from canonical strings. (
Browse files Browse the repository at this point in the history
#3105)

Introduces `func ParseAssetString(string) txnbuild.Asset`

Co-authored-by: Leigh McCulloch <[email protected]>
  • Loading branch information
Shaptic and leighmcculloch authored Oct 9, 2020
1 parent b0ec1a1 commit dd35fa3
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions txnbuild/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
All notable changes to this project will be documented in this
file. This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased
* Add helper function `ParseAssetString()`, making it easier to build an `Asset` structure from a string in [canonical form](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0011.md#asset) and check its various properties ([#3105](https://github.com/stellar/go/pull/3105)).

## [v4.0.1](https://github.com/stellar/go/releases/tag/horizonclient-v4.0.1) - 2020-10-02

* Fixed bug in `TransactionFromXDR()` which occurs when parsing transaction XDR envelopes which contain Protocol 14 operations.
Expand Down
21 changes: 21 additions & 0 deletions txnbuild/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,24 @@ func NewValidationError(field, message string) *ValidationError {
Message: message,
}
}

// Parses an asset string in canonical form (SEP-11) into an Asset structure.
// https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0011.md#asset
func ParseAssetString(canonical string) (Asset, error) {
assets, err := xdr.BuildAssets(canonical)
if err != nil {
return nil, errors.Wrap(err, "error parsing asset string")
}

if len(assets) != 1 {
return nil, errors.New("error parsing out a single asset")
}

// The above returned a list, so we'll need to grab the first element.
asset, err := assetFromXDR(assets[0])
if err != nil {
return nil, errors.Wrap(err, "error parsing asset string via XDR types")
}

return asset, nil
}
55 changes: 55 additions & 0 deletions txnbuild/helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package txnbuild

import (
"fmt"
"testing"

"github.com/stellar/go/keypair"
Expand Down Expand Up @@ -352,3 +353,57 @@ func TestValidateOfferManageSellOffer(t *testing.T) {
expectedErrMsg := "Field: OfferID, Error: amount can not be negative"
require.EqualError(t, err, expectedErrMsg, "valid offerID is required")
}

func TestAssetStringParsing(t *testing.T) {
kp0 := newKeypair0()
cred4 := CreditAsset{Code: "ABCD", Issuer: kp0.Address()}
xdr, err := cred4.ToXDR()
assert.NoError(t, err)
cred4String := xdr.StringCanonical()

kp1 := newKeypair1()
cred12 := CreditAsset{Code: "ABCD1234EFGH", Issuer: kp1.Address()}
xdr, err = cred12.ToXDR()
assert.NoError(t, err)
cred12String := xdr.StringCanonical()

native := NativeAsset{}
xdr, err = native.ToXDR()
assert.NoError(t, err)
nativeString := xdr.StringCanonical()

assets := make([]Asset, 3)
for i, input := range []string{nativeString, cred4String, cred12String} {
actual, innerErr := ParseAssetString(input)
assert.NoError(t, innerErr)
assets[i] = actual
}

compareAssets := func(expected Asset, actual Asset) bool {
expXdr, innerErr := expected.ToXDR()
if innerErr != nil {
return false
}

actXdr, innerErr := actual.ToXDR()
if innerErr != nil {
return false
}

return expXdr.Equals(actXdr)
}

assert.True(t, compareAssets(native, assets[0]))
assert.True(t, compareAssets(cred4, assets[1]))
assert.True(t, compareAssets(cred12, assets[2]))

// Now sanity-check some basic error cases

result, err := ParseAssetString("erroneous:maximus")
assert.Error(t, err)
assert.Equal(t, nil, result)

result, err = ParseAssetString(fmt.Sprintf("ABCD:%s,EFGH:%s", kp0.Address(), kp1.Address()))
assert.Error(t, err)
assert.Equal(t, nil, result)
}

0 comments on commit dd35fa3

Please sign in to comment.