UNPKG

@okxweb3/coin-stellar

Version:

@ok/coin-stellar is a Stellar SDK for building Web3 wallets and applications. It supports Stellar and PI blockchains, enabling private key management, address generation, transaction signing, trustline creation, and asset transfers

162 lines 5.83 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Asset = void 0; const util_1 = require("./util/util"); const xdr_1 = __importDefault(require("./xdr")); const keypair_1 = require("./keypair"); const strkey_1 = require("./strkey"); const hashing_1 = require("./hashing"); class Asset { constructor(code, issuer) { if (!/^[a-zA-Z0-9]{1,12}$/.test(code)) { throw new Error('Asset code is invalid (maximum alphanumeric, 12 characters at max)'); } if (String(code).toLowerCase() !== 'xlm' && !issuer) { throw new Error('Issuer cannot be null'); } if (issuer && !strkey_1.StrKey.isValidEd25519PublicKey(issuer)) { throw new Error('Issuer is invalid'); } if (String(code).toLowerCase() === 'xlm') { this.code = 'XLM'; } else { this.code = code; } this.issuer = issuer; } static native() { return new Asset('XLM'); } static fromOperation(assetXdr) { let anum; let code; let issuer; switch (assetXdr.switch()) { case xdr_1.default.AssetType.assetTypeNative(): return this.native(); case xdr_1.default.AssetType.assetTypeCreditAlphanum4(): anum = assetXdr.alphaNum4(); case xdr_1.default.AssetType.assetTypeCreditAlphanum12(): anum = anum || assetXdr.alphaNum12(); issuer = strkey_1.StrKey.encodeEd25519PublicKey(anum.issuer().ed25519()); code = (0, util_1.trimEnd)(anum.assetCode(), '\0'); return new this(code, issuer); default: throw new Error(`Invalid asset type: ${assetXdr.switch().name}`); } } toXDRObject() { return this._toXDRObject(xdr_1.default.Asset); } toChangeTrustXDRObject() { return this._toXDRObject(xdr_1.default.ChangeTrustAsset); } toTrustLineXDRObject() { return this._toXDRObject(xdr_1.default.TrustLineAsset); } contractId(networkPassphrase) { const networkId = (0, hashing_1.hash)(Buffer.from(networkPassphrase)); const preimage = xdr_1.default.HashIdPreimage.envelopeTypeContractId(new xdr_1.default.HashIdPreimageContractId({ networkId, contractIdPreimage: xdr_1.default.ContractIdPreimage.contractIdPreimageFromAsset(this.toXDRObject()) })); return strkey_1.StrKey.encodeContract((0, hashing_1.hash)(preimage.toXDR())); } _toXDRObject(xdrAsset = xdr_1.default.Asset) { if (this.isNative()) { return xdrAsset.assetTypeNative(); } let xdrType; let xdrTypeString; if (this.code.length <= 4) { xdrType = xdr_1.default.AlphaNum4; xdrTypeString = 'assetTypeCreditAlphanum4'; } else { xdrType = xdr_1.default.AlphaNum12; xdrTypeString = 'assetTypeCreditAlphanum12'; } const padLength = this.code.length <= 4 ? 4 : 12; const paddedCode = this.code.padEnd(padLength, '\0'); const assetType = new xdrType({ assetCode: paddedCode, issuer: keypair_1.Keypair.fromPublicKey(this.issuer).xdrAccountId() }); return new xdrAsset(xdrTypeString, assetType); } getCode() { if (this.code === undefined) { return undefined; } return String(this.code); } getIssuer() { if (this.issuer === undefined) { return undefined; } return String(this.issuer); } getAssetType() { switch (this.getRawAssetType().value) { case xdr_1.default.AssetType.assetTypeNative().value: return 'native'; case xdr_1.default.AssetType.assetTypeCreditAlphanum4().value: return 'credit_alphanum4'; case xdr_1.default.AssetType.assetTypeCreditAlphanum12().value: return 'credit_alphanum12'; default: return 'unknown'; } } getRawAssetType() { if (this.isNative()) { return xdr_1.default.AssetType.assetTypeNative(); } if (this.code.length <= 4) { return xdr_1.default.AssetType.assetTypeCreditAlphanum4(); } return xdr_1.default.AssetType.assetTypeCreditAlphanum12(); } isNative() { return !this.issuer; } equals(asset) { return this.code === asset.getCode() && this.issuer === asset.getIssuer(); } toString() { if (this.isNative()) { return 'native'; } return `${this.getCode()}:${this.getIssuer()}`; } static compare(assetA, assetB) { if (!assetA || !(assetA instanceof Asset)) { throw new Error('assetA is invalid'); } if (!assetB || !(assetB instanceof Asset)) { throw new Error('assetB is invalid'); } if (assetA.equals(assetB)) { return 0; } const xdrAtype = assetA.getRawAssetType().value; const xdrBtype = assetB.getRawAssetType().value; if (xdrAtype !== xdrBtype) { return xdrAtype < xdrBtype ? -1 : 1; } const result = asciiCompare(assetA.getCode(), assetB.getCode()); if (result !== 0) { return result; } return asciiCompare(assetA.getIssuer(), assetB.getIssuer()); } } exports.Asset = Asset; function asciiCompare(a, b) { return Buffer.compare(Buffer.from(a, 'ascii'), Buffer.from(b, 'ascii')); } //# sourceMappingURL=asset.js.map