stellar-base
Advanced tools
Comparing version 0.4.11 to 0.4.12
268
lib/asset.js
@@ -10,3 +10,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
@@ -36,158 +36,162 @@ | ||
/** | ||
* An asset code describes an asset code and issuer pair. In the case of the native | ||
* asset XLM, the issuer will be null. | ||
* @constructor | ||
* @param {string} code - The asset code. | ||
* @param {string} issuer - The address of the issuer. | ||
*/ | ||
/** | ||
* An asset code describes an asset code and issuer pair. In the case of the native | ||
* asset XLM, the issuer will be null. | ||
* @constructor | ||
* @param {string} code - The asset code. | ||
* @param {string} issuer - The address of the issuer. | ||
*/ | ||
function Asset(code, issuer) { | ||
_classCallCheck(this, Asset); | ||
function Asset(code, issuer) { | ||
_classCallCheck(this, Asset); | ||
if (code.length > 12) { | ||
throw new Error("Asset code must be 12 characters at max"); | ||
} | ||
if (String(code).toLowerCase() !== "xlm" && !issuer) { | ||
throw new Error("Issuer cannot be null"); | ||
} | ||
if (issuer && !Account.isValidAddress(issuer)) { | ||
throw new Error("Issuer is invalid"); | ||
} | ||
this.code = code; | ||
this.issuer = issuer; | ||
if (code.length > 12) { | ||
throw new Error("Asset code must be 12 characters at max"); | ||
} | ||
if (String(code).toLowerCase() !== "xlm" && !issuer) { | ||
throw new Error("Issuer cannot be null"); | ||
} | ||
if (issuer && !Account.isValidAddress(issuer)) { | ||
throw new Error("Issuer is invalid"); | ||
} | ||
_createClass(Asset, { | ||
toXdrObject: { | ||
this.code = code; | ||
this.issuer = issuer; | ||
} | ||
/** | ||
* Returns the xdr object for this asset. | ||
*/ | ||
_createClass(Asset, { | ||
toXdrObject: { | ||
value: function toXdrObject() { | ||
if (this.isNative()) { | ||
return xdr.Asset.assetTypeNative(); | ||
} else { | ||
var xdrType = undefined, | ||
xdrTypeString = undefined; | ||
if (this.code.length <= 4) { | ||
xdrType = xdr.AssetAlphaNum4; | ||
xdrTypeString = "assetTypeCreditAlphanum4"; | ||
} else { | ||
xdrType = xdr.AssetAlphaNum12; | ||
xdrTypeString = "assetTypeCreditAlphanum12"; | ||
} | ||
/** | ||
* Returns the xdr object for this asset. | ||
*/ | ||
// pad code with null bytes if necessary | ||
var padLength = this.code.length <= 4 ? 4 : 12; | ||
var paddedCode = padRight(this.code, padLength, "\u0000"); | ||
value: function toXdrObject() { | ||
if (this.isNative()) { | ||
return xdr.Asset.assetTypeNative(); | ||
} else { | ||
var xdrType = undefined, | ||
xdrTypeString = undefined; | ||
if (this.code.length <= 4) { | ||
xdrType = xdr.AssetAlphaNum4; | ||
xdrTypeString = "assetTypeCreditAlphanum4"; | ||
} else { | ||
xdrType = xdr.AssetAlphaNum12; | ||
xdrTypeString = "assetTypeCreditAlphanum12"; | ||
} | ||
var assetType = new xdrType({ | ||
assetCode: paddedCode, | ||
issuer: Keypair.fromAddress(this.issuer).accountId() | ||
}); | ||
// pad code with null bytes if necessary | ||
var padLength = this.code.length <= 4 ? 4 : 12; | ||
var paddedCode = padRight(this.code, padLength, "\u0000"); | ||
return new xdr.Asset(xdrTypeString, assetType); | ||
} | ||
} | ||
}, | ||
getCode: { | ||
var assetType = new xdrType({ | ||
assetCode: paddedCode, | ||
issuer: Keypair.fromAddress(this.issuer).accountId() | ||
}); | ||
/** | ||
* Return the asset code | ||
*/ | ||
return new xdr.Asset(xdrTypeString, assetType); | ||
} | ||
} | ||
}, | ||
getCode: { | ||
value: function getCode() { | ||
return clone(this.code); | ||
} | ||
}, | ||
getIssuer: { | ||
/** | ||
* Return the asset code | ||
*/ | ||
/** | ||
* Return the asset issuer | ||
**/ | ||
value: function getCode() { | ||
return clone(this.code); | ||
} | ||
}, | ||
getIssuer: { | ||
value: function getIssuer() { | ||
return clone(this.issuer); | ||
} | ||
}, | ||
getAssetType: { | ||
/** | ||
* Return the asset issuer | ||
**/ | ||
/** | ||
* Return the asset type | ||
*/ | ||
value: function getIssuer() { | ||
return clone(this.issuer); | ||
} | ||
}, | ||
getAssetType: { | ||
value: function getAssetType() { | ||
if (this.code.length >= 1 && this.code.length <= 4) { | ||
return "credit_alphanum4"; | ||
} else if (this.code.length >= 5 && this.code.length <= 12) { | ||
return "credit_alphanum12"; | ||
} | ||
} | ||
}, | ||
isNative: { | ||
/** | ||
* Return the asset type | ||
*/ | ||
/** | ||
* Returns true if this asset object is the native asset. | ||
*/ | ||
value: function getAssetType() { | ||
if (this.isNative()) { | ||
return "native"; | ||
} else { | ||
if (this.code.length >= 1 && this.code.length <= 4) { | ||
return "credit_alphanum4"; | ||
} else if (this.code.length >= 5 && this.code.length <= 12) { | ||
return "credit_alphanum12"; | ||
} | ||
} | ||
} | ||
}, | ||
isNative: { | ||
value: function isNative() { | ||
return !this.issuer; | ||
} | ||
}, | ||
equals: { | ||
/** | ||
* Returns true if this asset object is the native asset. | ||
*/ | ||
/** | ||
* Returns true if this asset equals the given asset. | ||
*/ | ||
value: function isNative() { | ||
return !this.issuer; | ||
} | ||
}, | ||
equals: { | ||
value: function equals(asset) { | ||
return this.code == asset.getCode() && this.issuer == asset.getIssuer(); | ||
} | ||
} | ||
}, { | ||
native: { | ||
/** | ||
* Returns true if this asset equals the given asset. | ||
*/ | ||
/** | ||
* Returns an asset object for the native asset. | ||
*/ | ||
value: function equals(asset) { | ||
return this.code == asset.getCode() && this.issuer == asset.getIssuer(); | ||
} | ||
} | ||
}, { | ||
native: { | ||
value: function native() { | ||
return new Asset("XLM"); | ||
} | ||
}, | ||
fromOperation: { | ||
/** | ||
* Returns an asset object for the native asset. | ||
*/ | ||
/** | ||
* Returns an asset object from its XDR object representation. | ||
* @param {xdr.Asset} cx - The asset xdr object. | ||
*/ | ||
value: function native() { | ||
return new Asset("XLM"); | ||
} | ||
}, | ||
fromOperation: { | ||
value: function fromOperation(cx) { | ||
var anum = undefined, | ||
code = undefined, | ||
issuer = undefined; | ||
switch (cx["switch"]()) { | ||
case xdr.AssetType.assetTypeNative(): | ||
return this.native(); | ||
case xdr.AssetType.assetTypeCreditAlphanum4(): | ||
anum = cx.alphaNum4(); | ||
issuer = encodeCheck("accountId", anum.issuer().ed25519()); | ||
code = trimRight(anum.assetCode(), "\u0000"); | ||
return new this(code, issuer); | ||
case xdr.AssetType.assetTypeCreditAlphanum12(): | ||
anum = cx.alphaNum12(); | ||
issuer = encodeCheck("accountId", anum.issuer().ed25519()); | ||
code = trimRight(anum.assetCode(), "\u0000"); | ||
return new this(code, issuer); | ||
default: | ||
throw new Error("Invalid asset type: " + cx["switch"]().name); | ||
} | ||
} | ||
/** | ||
* Returns an asset object from its XDR object representation. | ||
* @param {xdr.Asset} cx - The asset xdr object. | ||
*/ | ||
value: function fromOperation(cx) { | ||
var anum = undefined, | ||
code = undefined, | ||
issuer = undefined; | ||
switch (cx["switch"]()) { | ||
case xdr.AssetType.assetTypeNative(): | ||
return this.native(); | ||
case xdr.AssetType.assetTypeCreditAlphanum4(): | ||
anum = cx.alphaNum4(); | ||
issuer = encodeCheck("accountId", anum.issuer().ed25519()); | ||
code = trimRight(anum.assetCode(), "\u0000"); | ||
return new this(code, issuer); | ||
case xdr.AssetType.assetTypeCreditAlphanum12(): | ||
anum = cx.alphaNum12(); | ||
issuer = encodeCheck("accountId", anum.issuer().ed25519()); | ||
code = trimRight(anum.assetCode(), "\u0000"); | ||
return new this(code, issuer); | ||
default: | ||
throw new Error("Invalid asset type: " + cx["switch"]().name); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
return Asset; | ||
return Asset; | ||
})(); |
{ | ||
"name": "stellar-base", | ||
"version": "0.4.11", | ||
"version": "0.4.12", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "babel-runtime": { |
{ | ||
"name": "stellar-base", | ||
"version": "0.4.11", | ||
"version": "0.4.12", | ||
"description": "Low level stellar support library", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -114,6 +114,10 @@ import {default as xdr} from "./generated/stellar-xdr_generated"; | ||
getAssetType() { | ||
if (this.code.length >= 1 && this.code.length <= 4) { | ||
return "credit_alphanum4"; | ||
} else if (this.code.length >= 5 && this.code.length <= 12) { | ||
return "credit_alphanum12"; | ||
if (this.isNative()) { | ||
return 'native'; | ||
} else { | ||
if (this.code.length >= 1 && this.code.length <= 4) { | ||
return "credit_alphanum4"; | ||
} else if (this.code.length >= 5 && this.code.length <= 12) { | ||
return "credit_alphanum12"; | ||
} | ||
} | ||
@@ -120,0 +124,0 @@ } |
@@ -43,2 +43,7 @@ describe('Asset', function() { | ||
describe("getAssetType()", function () { | ||
it("returns native for native assets", function () { | ||
var asset = StellarBase.Asset.native(); | ||
expect(asset.getAssetType()).to.eq("native"); | ||
}); | ||
it("returns credit_alphanum4 if the asset code length is between 1 and 4", function () { | ||
@@ -45,0 +50,0 @@ var asset = new StellarBase.Asset("ABCD", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ"); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
40747
2498175