Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use canonical asset format. #277

Merged
merged 1 commit into from
Oct 15, 2019
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ A breaking change will get clearly notified in this log.

## Unreleased

## Update ❗️

- Update `asset.toString()` to return canonical representation for asset. ([277](https://github.com/stellar/js-stellar-base/pull/277)).

Calling `asset.toString()` will return `native` for `XLM` or `AssetCode:AssetIssuer` for issued assets. See [this PR](https://github.com/stellar/stellar-protocol/pull/313/files) for more information.

## [v2.1.0](https://github.com/stellar/js-stellar-base/compare/v2.0.2...v2.1.0)

This release adds support for [stellar-core protocol 12 release](https://github.com/stellar/stellar-core/projects/11) and [CAP 24](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0024.md) ("Make PathPayment Symmetrical").
Expand Down
2 changes: 1 addition & 1 deletion src/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,6 @@ export class Asset {
return 'native';
}

return `${this.getCode()}-${this.getIssuer()}`;
return `${this.getCode()}:${this.getIssuer()}`;
}
}
314 changes: 184 additions & 130 deletions test/unit/asset_test.js
Original file line number Diff line number Diff line change
@@ -1,171 +1,225 @@
describe('Asset', function() {
describe('constructor', function() {
it("throws an error when there's no issuer for non XLM type asset", function() {
expect(() => new StellarBase.Asset('USD')).to.throw(
/Issuer cannot be null/
);
});

describe("constructor", function () {

it("throws an error when there's no issuer for non XLM type asset", function () {
expect(() => new StellarBase.Asset("USD")).to.throw(/Issuer cannot be null/)
});

it("throws an error when code is invalid", function () {
expect(() => new StellarBase.Asset("", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ")).to.throw(/Asset code is invalid/)
expect(() => new StellarBase.Asset("1234567890123", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ")).to.throw(/Asset code is invalid/)
expect(() => new StellarBase.Asset("ab_", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ")).to.throw(/Asset code is invalid/)
});

it("throws an error when issuer is invalid", function () {
expect(() => new StellarBase.Asset("USD", "GCEZWKCA5")).to.throw(/Issuer is invalid/)
});
it('throws an error when code is invalid', function() {
expect(
() =>
new StellarBase.Asset(
'',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
)
).to.throw(/Asset code is invalid/);
expect(
() =>
new StellarBase.Asset(
'1234567890123',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
)
).to.throw(/Asset code is invalid/);
expect(
() =>
new StellarBase.Asset(
'ab_',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
)
).to.throw(/Asset code is invalid/);
});

describe("getCode()", function () {
it("returns a code for a native asset object", function () {
var asset = new StellarBase.Asset.native();
expect(asset.getCode()).to.be.equal('XLM');
});
it('throws an error when issuer is invalid', function() {
expect(() => new StellarBase.Asset('USD', 'GCEZWKCA5')).to.throw(
/Issuer is invalid/
);
});
});

it("returns a code for a non-native asset", function () {
var asset = new StellarBase.Asset("USD", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ");
expect(asset.getCode()).to.be.equal('USD');
});
describe('getCode()', function() {
it('returns a code for a native asset object', function() {
var asset = new StellarBase.Asset.native();
expect(asset.getCode()).to.be.equal('XLM');
});

describe("getIssuer()", function () {
it("returns a code for a native asset object", function () {
var asset = new StellarBase.Asset.native();
expect(asset.getIssuer()).to.be.undefined;
});
it('returns a code for a non-native asset', function() {
var asset = new StellarBase.Asset(
'USD',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
expect(asset.getCode()).to.be.equal('USD');
});
});

it("returns a code for a non-native asset", function () {
var asset = new StellarBase.Asset("USD", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ");
expect(asset.getIssuer()).to.be.equal('GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ');
});
describe('getIssuer()', function() {
it('returns a code for a native asset object', function() {
var asset = new StellarBase.Asset.native();
expect(asset.getIssuer()).to.be.undefined;
});

describe("getAssetType()", function () {
it("returns native for native assets", function () {
var asset = StellarBase.Asset.native();
expect(asset.getAssetType()).to.eq("native");
});
it('returns a code for a non-native asset', function() {
var asset = new StellarBase.Asset(
'USD',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
expect(asset.getIssuer()).to.be.equal(
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
});
});

it("returns credit_alphanum4 if the asset code length is between 1 and 4", function () {
var asset = new StellarBase.Asset("ABCD", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ");
expect(asset.getAssetType()).to.eq("credit_alphanum4");
});
describe('getAssetType()', function() {
it('returns native for native assets', function() {
var asset = StellarBase.Asset.native();
expect(asset.getAssetType()).to.eq('native');
});

it("returns credit_alphanum12 if the asset code length is between 5 and 12", function () {
var asset = new StellarBase.Asset("ABCDEF", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ");
expect(asset.getAssetType()).to.eq("credit_alphanum12");
});
it('returns credit_alphanum4 if the asset code length is between 1 and 4', function() {
var asset = new StellarBase.Asset(
'ABCD',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
expect(asset.getAssetType()).to.eq('credit_alphanum4');
});

describe("toXDRObject()", function () {
it("parses a native asset object", function () {
var asset = new StellarBase.Asset.native();
var xdr = asset.toXDRObject();
expect(xdr.toXDR().toString()).to.be.equal(Buffer.from([0,0,0,0]).toString());
});
it('returns credit_alphanum12 if the asset code length is between 5 and 12', function() {
var asset = new StellarBase.Asset(
'ABCDEF',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
expect(asset.getAssetType()).to.eq('credit_alphanum12');
});
});

describe('toXDRObject()', function() {
it('parses a native asset object', function() {
var asset = new StellarBase.Asset.native();
var xdr = asset.toXDRObject();
expect(xdr.toXDR().toString()).to.be.equal(
Buffer.from([0, 0, 0, 0]).toString()
);
});

it("parses a 3-alphanum asset object", function () {
var asset = new StellarBase.Asset("USD", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ");
var xdr = asset.toXDRObject();
it('parses a 3-alphanum asset object', function() {
var asset = new StellarBase.Asset(
'USD',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
var xdr = asset.toXDRObject();

expect(xdr).to.be.instanceof(StellarBase.xdr.Asset);
expect(() => xdr.toXDR('hex')).to.not.throw();
expect(xdr).to.be.instanceof(StellarBase.xdr.Asset);
expect(() => xdr.toXDR('hex')).to.not.throw();

expect(xdr.arm()).to.equal('alphaNum4');
expect(xdr.value().assetCode()).to.equal('USD\0');
});
expect(xdr.arm()).to.equal('alphaNum4');
expect(xdr.value().assetCode()).to.equal('USD\0');
});

it("parses a 4-alphanum asset object", function () {
var asset = new StellarBase.Asset("BART", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ");
var xdr = asset.toXDRObject();
it('parses a 4-alphanum asset object', function() {
var asset = new StellarBase.Asset(
'BART',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
var xdr = asset.toXDRObject();

expect(xdr).to.be.instanceof(StellarBase.xdr.Asset);
expect(() => xdr.toXDR('hex')).to.not.throw();
expect(xdr).to.be.instanceof(StellarBase.xdr.Asset);
expect(() => xdr.toXDR('hex')).to.not.throw();

expect(xdr.arm()).to.equal('alphaNum4');
expect(xdr.value().assetCode()).to.equal('BART');
});
expect(xdr.arm()).to.equal('alphaNum4');
expect(xdr.value().assetCode()).to.equal('BART');
});

it("parses a 5-alphanum asset object", function () {
var asset = new StellarBase.Asset("12345", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ");
var xdr = asset.toXDRObject();
it('parses a 5-alphanum asset object', function() {
var asset = new StellarBase.Asset(
'12345',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
var xdr = asset.toXDRObject();

expect(xdr).to.be.instanceof(StellarBase.xdr.Asset);
expect(() => xdr.toXDR('hex')).to.not.throw();
expect(xdr).to.be.instanceof(StellarBase.xdr.Asset);
expect(() => xdr.toXDR('hex')).to.not.throw();

expect(xdr.arm()).to.equal('alphaNum12');
expect(xdr.value().assetCode()).to.equal('12345\0\0\0\0\0\0\0');
});
expect(xdr.arm()).to.equal('alphaNum12');
expect(xdr.value().assetCode()).to.equal('12345\0\0\0\0\0\0\0');
});

it("parses a 12-alphanum asset object", function () {
var asset = new StellarBase.Asset("123456789012", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ");
var xdr = asset.toXDRObject();
it('parses a 12-alphanum asset object', function() {
var asset = new StellarBase.Asset(
'123456789012',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
var xdr = asset.toXDRObject();

expect(xdr).to.be.instanceof(StellarBase.xdr.Asset);
expect(() => xdr.toXDR('hex')).to.not.throw();
expect(xdr).to.be.instanceof(StellarBase.xdr.Asset);
expect(() => xdr.toXDR('hex')).to.not.throw();

expect(xdr.arm()).to.equal('alphaNum12');
expect(xdr.value().assetCode()).to.equal('123456789012');
});
expect(xdr.arm()).to.equal('alphaNum12');
expect(xdr.value().assetCode()).to.equal('123456789012');
});
});

describe("fromOperation()", function () {
it("parses a native asset XDR", function () {
var xdr = new StellarBase.xdr.Asset.assetTypeNative();
var asset = StellarBase.Asset.fromOperation(xdr);
describe('fromOperation()', function() {
it('parses a native asset XDR', function() {
var xdr = new StellarBase.xdr.Asset.assetTypeNative();
var asset = StellarBase.Asset.fromOperation(xdr);

expect(asset).to.be.instanceof(StellarBase.Asset);
expect(asset.isNative()).to.equal(true);
expect(asset).to.be.instanceof(StellarBase.Asset);
expect(asset.isNative()).to.equal(true);
});

it('parses a 4-alphanum asset XDR', function() {
var issuer = 'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ';
var assetCode = 'KHL';
var assetType = new StellarBase.xdr.AssetAlphaNum4({
assetCode: assetCode + '\0',
issuer: StellarBase.Keypair.fromPublicKey(issuer).xdrAccountId()
});
var xdr = new StellarBase.xdr.Asset(
'assetTypeCreditAlphanum4',
assetType
);

it("parses a 4-alphanum asset XDR", function () {
var issuer = 'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
var assetCode = 'KHL';
var assetType = new StellarBase.xdr.AssetAlphaNum4({
assetCode: assetCode + '\0',
issuer: StellarBase.Keypair.fromPublicKey(issuer).xdrAccountId()
});
var xdr = new StellarBase.xdr.Asset('assetTypeCreditAlphanum4', assetType);
var asset = StellarBase.Asset.fromOperation(xdr);

var asset = StellarBase.Asset.fromOperation(xdr);
expect(asset).to.be.instanceof(StellarBase.Asset);
expect(asset.getCode()).to.equal(assetCode);
expect(asset.getIssuer()).to.equal(issuer);
});

expect(asset).to.be.instanceof(StellarBase.Asset);
expect(asset.getCode()).to.equal(assetCode);
expect(asset.getIssuer()).to.equal(issuer);
it('parses a 12-alphanum asset XDR', function() {
var issuer = 'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ';
var assetCode = 'KHLTOKEN';
var assetType = new StellarBase.xdr.AssetAlphaNum4({
assetCode: assetCode + '\0\0\0\0',
issuer: StellarBase.Keypair.fromPublicKey(issuer).xdrAccountId()
});
var xdr = new StellarBase.xdr.Asset(
'assetTypeCreditAlphanum12',
assetType
);

it("parses a 12-alphanum asset XDR", function () {
var issuer = 'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
var assetCode = 'KHLTOKEN';
var assetType = new StellarBase.xdr.AssetAlphaNum4({
assetCode: assetCode + '\0\0\0\0',
issuer: StellarBase.Keypair.fromPublicKey(issuer).xdrAccountId()
});
var xdr = new StellarBase.xdr.Asset('assetTypeCreditAlphanum12', assetType);
var asset = StellarBase.Asset.fromOperation(xdr);

var asset = StellarBase.Asset.fromOperation(xdr);

expect(asset).to.be.instanceof(StellarBase.Asset);
expect(asset.getCode()).to.equal(assetCode);
expect(asset.getIssuer()).to.equal(issuer);
});
expect(asset).to.be.instanceof(StellarBase.Asset);
expect(asset.getCode()).to.equal(assetCode);
expect(asset.getIssuer()).to.equal(issuer);
});
});

describe('toString()', function() {
it("returns 'native' for native asset", function() {
var asset = StellarBase.Asset.native();
expect(asset.toString()).to.be.equal('native');
});
describe('toString()', function() {
it("returns 'native' for native asset", function() {
var asset = StellarBase.Asset.native();
expect(asset.toString()).to.be.equal('native');
});

it("returns 'code-issuer' for non-native asset", function() {
var asset = new StellarBase.Asset(
'USD',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
expect(asset.toString()).to.be.equal(
'USD-GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
});
it("returns 'code-issuer' for non-native asset", function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo - need code:issuer

var asset = new StellarBase.Asset(
'USD',
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
expect(asset.toString()).to.be.equal(
'USD:GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
);
});
});
});