@stellar/stellar-base 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1262 -0
- package/LICENSE +202 -0
- package/README.md +198 -0
- package/dist/stellar-base.js +30777 -0
- package/dist/stellar-base.min.js +2 -0
- package/lib/account.js +80 -0
- package/lib/address.js +169 -0
- package/lib/asset.js +323 -0
- package/lib/auth.js +253 -0
- package/lib/claimant.js +193 -0
- package/lib/contract.js +113 -0
- package/lib/events.js +42 -0
- package/lib/fee_bump_transaction.js +134 -0
- package/lib/generated/curr_generated.js +8315 -0
- package/lib/generated/next_generated.js +8315 -0
- package/lib/get_liquidity_pool_id.js +57 -0
- package/lib/hashing.js +12 -0
- package/lib/index.js +385 -0
- package/lib/invocation.js +195 -0
- package/lib/keypair.js +308 -0
- package/lib/liquidity_pool_asset.js +126 -0
- package/lib/liquidity_pool_id.js +101 -0
- package/lib/memo.js +270 -0
- package/lib/muxed_account.js +159 -0
- package/lib/network.js +22 -0
- package/lib/numbers/index.js +85 -0
- package/lib/numbers/int128.js +50 -0
- package/lib/numbers/int256.js +50 -0
- package/lib/numbers/sc_int.js +134 -0
- package/lib/numbers/uint128.js +50 -0
- package/lib/numbers/uint256.js +50 -0
- package/lib/numbers/xdr_large_int.js +267 -0
- package/lib/operation.js +715 -0
- package/lib/operations/account_merge.js +32 -0
- package/lib/operations/allow_trust.js +57 -0
- package/lib/operations/begin_sponsoring_future_reserves.js +38 -0
- package/lib/operations/bump_sequence.js +37 -0
- package/lib/operations/change_trust.js +52 -0
- package/lib/operations/claim_claimable_balance.js +40 -0
- package/lib/operations/clawback.js +46 -0
- package/lib/operations/clawback_claimable_balance.js +39 -0
- package/lib/operations/create_account.js +37 -0
- package/lib/operations/create_claimable_balance.js +65 -0
- package/lib/operations/create_passive_sell_offer.js +44 -0
- package/lib/operations/end_sponsoring_future_reserves.js +27 -0
- package/lib/operations/extend_footprint_ttl.js +45 -0
- package/lib/operations/index.js +254 -0
- package/lib/operations/inflation.js +23 -0
- package/lib/operations/invoke_host_function.js +219 -0
- package/lib/operations/liquidity_pool_deposit.js +64 -0
- package/lib/operations/liquidity_pool_withdraw.js +50 -0
- package/lib/operations/manage_buy_offer.js +50 -0
- package/lib/operations/manage_data.js +41 -0
- package/lib/operations/manage_sell_offer.js +50 -0
- package/lib/operations/path_payment_strict_receive.js +68 -0
- package/lib/operations/path_payment_strict_send.js +68 -0
- package/lib/operations/payment.js +47 -0
- package/lib/operations/restore_footprint.js +38 -0
- package/lib/operations/revoke_sponsorship.js +301 -0
- package/lib/operations/set_options.js +135 -0
- package/lib/operations/set_trustline_flags.js +84 -0
- package/lib/scval.js +369 -0
- package/lib/signerkey.js +103 -0
- package/lib/signing.js +96 -0
- package/lib/soroban.js +96 -0
- package/lib/sorobandata_builder.js +218 -0
- package/lib/strkey.js +400 -0
- package/lib/transaction.js +369 -0
- package/lib/transaction_base.js +248 -0
- package/lib/transaction_builder.js +753 -0
- package/lib/util/checksum.js +20 -0
- package/lib/util/continued_fraction.js +58 -0
- package/lib/util/decode_encode_muxed_account.js +116 -0
- package/lib/util/util.js +14 -0
- package/lib/xdr.js +9 -0
- package/package.json +133 -0
- package/types/curr.d.ts +14078 -0
- package/types/index.d.ts +1270 -0
- package/types/next.d.ts +14078 -0
- package/types/xdr.d.ts +1 -0
package/lib/account.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Account = void 0;
|
|
7
|
+
var _bignumber = _interopRequireDefault(require("bignumber.js"));
|
|
8
|
+
var _strkey = require("./strkey");
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
10
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
11
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
12
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
13
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
14
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
15
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
16
|
+
/**
|
|
17
|
+
* Create a new Account object.
|
|
18
|
+
*
|
|
19
|
+
* `Account` represents a single account in the Stellar network and its sequence
|
|
20
|
+
* number. Account tracks the sequence number as it is used by {@link
|
|
21
|
+
* TransactionBuilder}. See
|
|
22
|
+
* [Accounts](https://developers.stellar.org/docs/glossary/accounts/) for
|
|
23
|
+
* more information about how accounts work in Stellar.
|
|
24
|
+
*
|
|
25
|
+
* @constructor
|
|
26
|
+
*
|
|
27
|
+
* @param {string} accountId - ID of the account (ex.
|
|
28
|
+
* `GB3KJPLFUYN5VL6R3GU3EGCGVCKFDSD7BEDX42HWG5BWFKB3KQGJJRMA`). If you
|
|
29
|
+
* provide a muxed account address, this will throw; use {@link
|
|
30
|
+
* MuxedAccount} instead.
|
|
31
|
+
* @param {string} sequence - current sequence number of the account
|
|
32
|
+
*/
|
|
33
|
+
var Account = exports.Account = /*#__PURE__*/function () {
|
|
34
|
+
function Account(accountId, sequence) {
|
|
35
|
+
_classCallCheck(this, Account);
|
|
36
|
+
if (_strkey.StrKey.isValidMed25519PublicKey(accountId)) {
|
|
37
|
+
throw new Error('accountId is an M-address; use MuxedAccount instead');
|
|
38
|
+
}
|
|
39
|
+
if (!_strkey.StrKey.isValidEd25519PublicKey(accountId)) {
|
|
40
|
+
throw new Error('accountId is invalid');
|
|
41
|
+
}
|
|
42
|
+
if (!(typeof sequence === 'string')) {
|
|
43
|
+
throw new Error('sequence must be of type string');
|
|
44
|
+
}
|
|
45
|
+
this._accountId = accountId;
|
|
46
|
+
this.sequence = new _bignumber["default"](sequence);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Returns Stellar account ID, ex.
|
|
51
|
+
* `GB3KJPLFUYN5VL6R3GU3EGCGVCKFDSD7BEDX42HWG5BWFKB3KQGJJRMA`.
|
|
52
|
+
* @returns {string}
|
|
53
|
+
*/
|
|
54
|
+
_createClass(Account, [{
|
|
55
|
+
key: "accountId",
|
|
56
|
+
value: function accountId() {
|
|
57
|
+
return this._accountId;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @returns {string} sequence number for the account as a string
|
|
62
|
+
*/
|
|
63
|
+
}, {
|
|
64
|
+
key: "sequenceNumber",
|
|
65
|
+
value: function sequenceNumber() {
|
|
66
|
+
return this.sequence.toString();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Increments sequence number in this object by one.
|
|
71
|
+
* @returns {void}
|
|
72
|
+
*/
|
|
73
|
+
}, {
|
|
74
|
+
key: "incrementSequenceNumber",
|
|
75
|
+
value: function incrementSequenceNumber() {
|
|
76
|
+
this.sequence = this.sequence.plus(1);
|
|
77
|
+
}
|
|
78
|
+
}]);
|
|
79
|
+
return Account;
|
|
80
|
+
}();
|
package/lib/address.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Address = void 0;
|
|
7
|
+
var _strkey = require("./strkey");
|
|
8
|
+
var _xdr = _interopRequireDefault(require("./xdr"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
10
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
11
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
12
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
13
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
14
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
15
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
16
|
+
/**
|
|
17
|
+
* Create a new Address object.
|
|
18
|
+
*
|
|
19
|
+
* `Address` represents a single address in the Stellar network. An address can
|
|
20
|
+
* represent an account or a contract.
|
|
21
|
+
*
|
|
22
|
+
* @constructor
|
|
23
|
+
*
|
|
24
|
+
* @param {string} address - ID of the account (ex.
|
|
25
|
+
* `GB3KJPLFUYN5VL6R3GU3EGCGVCKFDSD7BEDX42HWG5BWFKB3KQGJJRMA`). If you
|
|
26
|
+
* provide a muxed account address, this will throw; use {@link
|
|
27
|
+
* MuxedAccount} instead.
|
|
28
|
+
*/
|
|
29
|
+
var Address = exports.Address = /*#__PURE__*/function () {
|
|
30
|
+
function Address(address) {
|
|
31
|
+
_classCallCheck(this, Address);
|
|
32
|
+
if (_strkey.StrKey.isValidEd25519PublicKey(address)) {
|
|
33
|
+
this._type = 'account';
|
|
34
|
+
this._key = _strkey.StrKey.decodeEd25519PublicKey(address);
|
|
35
|
+
} else if (_strkey.StrKey.isValidContract(address)) {
|
|
36
|
+
this._type = 'contract';
|
|
37
|
+
this._key = _strkey.StrKey.decodeContract(address);
|
|
38
|
+
} else {
|
|
39
|
+
throw new Error('Unsupported address type');
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Parses a string and returns an Address object.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} address - The address to parse. ex. `GB3KJPLFUYN5VL6R3GU3EGCGVCKFDSD7BEDX42HWG5BWFKB3KQGJJRMA`
|
|
47
|
+
* @returns {Address}
|
|
48
|
+
*/
|
|
49
|
+
_createClass(Address, [{
|
|
50
|
+
key: "toString",
|
|
51
|
+
value:
|
|
52
|
+
/**
|
|
53
|
+
* Serialize an address to string.
|
|
54
|
+
*
|
|
55
|
+
* @returns {string}
|
|
56
|
+
*/
|
|
57
|
+
function toString() {
|
|
58
|
+
switch (this._type) {
|
|
59
|
+
case 'account':
|
|
60
|
+
return _strkey.StrKey.encodeEd25519PublicKey(this._key);
|
|
61
|
+
case 'contract':
|
|
62
|
+
return _strkey.StrKey.encodeContract(this._key);
|
|
63
|
+
default:
|
|
64
|
+
throw new Error('Unsupported address type');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Convert this Address to an xdr.ScVal type.
|
|
70
|
+
*
|
|
71
|
+
* @returns {xdr.ScVal}
|
|
72
|
+
*/
|
|
73
|
+
}, {
|
|
74
|
+
key: "toScVal",
|
|
75
|
+
value: function toScVal() {
|
|
76
|
+
return _xdr["default"].ScVal.scvAddress(this.toScAddress());
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Convert this Address to an xdr.ScAddress type.
|
|
81
|
+
*
|
|
82
|
+
* @returns {xdr.ScAddress}
|
|
83
|
+
*/
|
|
84
|
+
}, {
|
|
85
|
+
key: "toScAddress",
|
|
86
|
+
value: function toScAddress() {
|
|
87
|
+
switch (this._type) {
|
|
88
|
+
case 'account':
|
|
89
|
+
return _xdr["default"].ScAddress.scAddressTypeAccount(_xdr["default"].PublicKey.publicKeyTypeEd25519(this._key));
|
|
90
|
+
case 'contract':
|
|
91
|
+
return _xdr["default"].ScAddress.scAddressTypeContract(this._key);
|
|
92
|
+
default:
|
|
93
|
+
throw new Error('Unsupported address type');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Return the raw public key bytes for this address.
|
|
99
|
+
*
|
|
100
|
+
* @returns {Buffer}
|
|
101
|
+
*/
|
|
102
|
+
}, {
|
|
103
|
+
key: "toBuffer",
|
|
104
|
+
value: function toBuffer() {
|
|
105
|
+
return this._key;
|
|
106
|
+
}
|
|
107
|
+
}], [{
|
|
108
|
+
key: "fromString",
|
|
109
|
+
value: function fromString(address) {
|
|
110
|
+
return new Address(address);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Creates a new account Address object from a buffer of raw bytes.
|
|
115
|
+
*
|
|
116
|
+
* @param {Buffer} buffer - The bytes of an address to parse.
|
|
117
|
+
* @returns {Address}
|
|
118
|
+
*/
|
|
119
|
+
}, {
|
|
120
|
+
key: "account",
|
|
121
|
+
value: function account(buffer) {
|
|
122
|
+
return new Address(_strkey.StrKey.encodeEd25519PublicKey(buffer));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Creates a new contract Address object from a buffer of raw bytes.
|
|
127
|
+
*
|
|
128
|
+
* @param {Buffer} buffer - The bytes of an address to parse.
|
|
129
|
+
* @returns {Address}
|
|
130
|
+
*/
|
|
131
|
+
}, {
|
|
132
|
+
key: "contract",
|
|
133
|
+
value: function contract(buffer) {
|
|
134
|
+
return new Address(_strkey.StrKey.encodeContract(buffer));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Convert this from an xdr.ScVal type
|
|
139
|
+
*
|
|
140
|
+
* @param {xdr.ScVal} scVal - The xdr.ScVal type to parse
|
|
141
|
+
* @returns {Address}
|
|
142
|
+
*/
|
|
143
|
+
}, {
|
|
144
|
+
key: "fromScVal",
|
|
145
|
+
value: function fromScVal(scVal) {
|
|
146
|
+
return Address.fromScAddress(scVal.address());
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Convert this from an xdr.ScAddress type
|
|
151
|
+
*
|
|
152
|
+
* @param {xdr.ScAddress} scAddress - The xdr.ScAddress type to parse
|
|
153
|
+
* @returns {Address}
|
|
154
|
+
*/
|
|
155
|
+
}, {
|
|
156
|
+
key: "fromScAddress",
|
|
157
|
+
value: function fromScAddress(scAddress) {
|
|
158
|
+
switch (scAddress["switch"]()) {
|
|
159
|
+
case _xdr["default"].ScAddressType.scAddressTypeAccount():
|
|
160
|
+
return Address.account(scAddress.accountId().ed25519());
|
|
161
|
+
case _xdr["default"].ScAddressType.scAddressTypeContract():
|
|
162
|
+
return Address.contract(scAddress.contractId());
|
|
163
|
+
default:
|
|
164
|
+
throw new Error('Unsupported address type');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}]);
|
|
168
|
+
return Address;
|
|
169
|
+
}();
|
package/lib/asset.js
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Asset = void 0;
|
|
7
|
+
var _util = require("./util/util");
|
|
8
|
+
var _xdr = _interopRequireDefault(require("./xdr"));
|
|
9
|
+
var _keypair = require("./keypair");
|
|
10
|
+
var _strkey = require("./strkey");
|
|
11
|
+
var _hashing = require("./hashing");
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
14
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
15
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
16
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
17
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
18
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
19
|
+
/**
|
|
20
|
+
* Asset class represents an asset, either the native asset (`XLM`)
|
|
21
|
+
* or an asset code / issuer account ID pair.
|
|
22
|
+
*
|
|
23
|
+
* An asset code describes an asset code and issuer pair. In the case of the native
|
|
24
|
+
* asset XLM, the issuer will be null.
|
|
25
|
+
*
|
|
26
|
+
* @constructor
|
|
27
|
+
* @param {string} code - The asset code.
|
|
28
|
+
* @param {string} issuer - The account ID of the issuer.
|
|
29
|
+
*/
|
|
30
|
+
var Asset = exports.Asset = /*#__PURE__*/function () {
|
|
31
|
+
function Asset(code, issuer) {
|
|
32
|
+
_classCallCheck(this, Asset);
|
|
33
|
+
if (!/^[a-zA-Z0-9]{1,12}$/.test(code)) {
|
|
34
|
+
throw new Error('Asset code is invalid (maximum alphanumeric, 12 characters at max)');
|
|
35
|
+
}
|
|
36
|
+
if (String(code).toLowerCase() !== 'xlm' && !issuer) {
|
|
37
|
+
throw new Error('Issuer cannot be null');
|
|
38
|
+
}
|
|
39
|
+
if (issuer && !_strkey.StrKey.isValidEd25519PublicKey(issuer)) {
|
|
40
|
+
throw new Error('Issuer is invalid');
|
|
41
|
+
}
|
|
42
|
+
if (String(code).toLowerCase() === 'xlm') {
|
|
43
|
+
// transform all xLM, Xlm, etc. variants -> XLM
|
|
44
|
+
this.code = 'XLM';
|
|
45
|
+
} else {
|
|
46
|
+
this.code = code;
|
|
47
|
+
}
|
|
48
|
+
this.issuer = issuer;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Returns an asset object for the native asset.
|
|
53
|
+
* @Return {Asset}
|
|
54
|
+
*/
|
|
55
|
+
_createClass(Asset, [{
|
|
56
|
+
key: "toXDRObject",
|
|
57
|
+
value:
|
|
58
|
+
/**
|
|
59
|
+
* Returns the xdr.Asset object for this asset.
|
|
60
|
+
* @returns {xdr.Asset} XDR asset object
|
|
61
|
+
*/
|
|
62
|
+
function toXDRObject() {
|
|
63
|
+
return this._toXDRObject(_xdr["default"].Asset);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Returns the xdr.ChangeTrustAsset object for this asset.
|
|
68
|
+
* @returns {xdr.ChangeTrustAsset} XDR asset object
|
|
69
|
+
*/
|
|
70
|
+
}, {
|
|
71
|
+
key: "toChangeTrustXDRObject",
|
|
72
|
+
value: function toChangeTrustXDRObject() {
|
|
73
|
+
return this._toXDRObject(_xdr["default"].ChangeTrustAsset);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns the xdr.TrustLineAsset object for this asset.
|
|
78
|
+
* @returns {xdr.TrustLineAsset} XDR asset object
|
|
79
|
+
*/
|
|
80
|
+
}, {
|
|
81
|
+
key: "toTrustLineXDRObject",
|
|
82
|
+
value: function toTrustLineXDRObject() {
|
|
83
|
+
return this._toXDRObject(_xdr["default"].TrustLineAsset);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Returns the would-be contract ID (`C...` format) for this asset on a given
|
|
88
|
+
* network.
|
|
89
|
+
*
|
|
90
|
+
* @param {string} networkPassphrase indicates which network the contract
|
|
91
|
+
* ID should refer to, since every network will have a unique ID for the
|
|
92
|
+
* same contract (see {@link Networks} for options)
|
|
93
|
+
*
|
|
94
|
+
* @returns {string} the strkey-encoded (`C...`) contract ID for this asset
|
|
95
|
+
*
|
|
96
|
+
* @warning This makes no guarantee that this contract actually *exists*.
|
|
97
|
+
*/
|
|
98
|
+
}, {
|
|
99
|
+
key: "contractId",
|
|
100
|
+
value: function contractId(networkPassphrase) {
|
|
101
|
+
var networkId = (0, _hashing.hash)(Buffer.from(networkPassphrase));
|
|
102
|
+
var preimage = _xdr["default"].HashIdPreimage.envelopeTypeContractId(new _xdr["default"].HashIdPreimageContractId({
|
|
103
|
+
networkId: networkId,
|
|
104
|
+
contractIdPreimage: _xdr["default"].ContractIdPreimage.contractIdPreimageFromAsset(this.toXDRObject())
|
|
105
|
+
}));
|
|
106
|
+
return _strkey.StrKey.encodeContract((0, _hashing.hash)(preimage.toXDR()));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Returns the xdr object for this asset.
|
|
111
|
+
* @param {xdr.Asset | xdr.ChangeTrustAsset} xdrAsset - The asset xdr object.
|
|
112
|
+
* @returns {xdr.Asset | xdr.ChangeTrustAsset | xdr.TrustLineAsset} XDR Asset object
|
|
113
|
+
*/
|
|
114
|
+
}, {
|
|
115
|
+
key: "_toXDRObject",
|
|
116
|
+
value: function _toXDRObject() {
|
|
117
|
+
var xdrAsset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _xdr["default"].Asset;
|
|
118
|
+
if (this.isNative()) {
|
|
119
|
+
return xdrAsset.assetTypeNative();
|
|
120
|
+
}
|
|
121
|
+
var xdrType;
|
|
122
|
+
var xdrTypeString;
|
|
123
|
+
if (this.code.length <= 4) {
|
|
124
|
+
xdrType = _xdr["default"].AlphaNum4;
|
|
125
|
+
xdrTypeString = 'assetTypeCreditAlphanum4';
|
|
126
|
+
} else {
|
|
127
|
+
xdrType = _xdr["default"].AlphaNum12;
|
|
128
|
+
xdrTypeString = 'assetTypeCreditAlphanum12';
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// pad code with null bytes if necessary
|
|
132
|
+
var padLength = this.code.length <= 4 ? 4 : 12;
|
|
133
|
+
var paddedCode = this.code.padEnd(padLength, '\0');
|
|
134
|
+
|
|
135
|
+
// eslint-disable-next-line new-cap
|
|
136
|
+
var assetType = new xdrType({
|
|
137
|
+
assetCode: paddedCode,
|
|
138
|
+
issuer: _keypair.Keypair.fromPublicKey(this.issuer).xdrAccountId()
|
|
139
|
+
});
|
|
140
|
+
return new xdrAsset(xdrTypeString, assetType);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @returns {string} Asset code
|
|
145
|
+
*/
|
|
146
|
+
}, {
|
|
147
|
+
key: "getCode",
|
|
148
|
+
value: function getCode() {
|
|
149
|
+
if (this.code === undefined) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
return String(this.code);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @returns {string} Asset issuer
|
|
157
|
+
*/
|
|
158
|
+
}, {
|
|
159
|
+
key: "getIssuer",
|
|
160
|
+
value: function getIssuer() {
|
|
161
|
+
if (this.issuer === undefined) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
return String(this.issuer);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* @see [Assets concept](https://developers.stellar.org/docs/glossary/assets/)
|
|
169
|
+
* @returns {string} Asset type. Can be one of following types:
|
|
170
|
+
*
|
|
171
|
+
* - `native`,
|
|
172
|
+
* - `credit_alphanum4`,
|
|
173
|
+
* - `credit_alphanum12`, or
|
|
174
|
+
* - `unknown` as the error case (which should never occur)
|
|
175
|
+
*/
|
|
176
|
+
}, {
|
|
177
|
+
key: "getAssetType",
|
|
178
|
+
value: function getAssetType() {
|
|
179
|
+
switch (this.getRawAssetType()) {
|
|
180
|
+
case _xdr["default"].AssetType.assetTypeNative():
|
|
181
|
+
return 'native';
|
|
182
|
+
case _xdr["default"].AssetType.assetTypeCreditAlphanum4():
|
|
183
|
+
return 'credit_alphanum4';
|
|
184
|
+
case _xdr["default"].AssetType.assetTypeCreditAlphanum12():
|
|
185
|
+
return 'credit_alphanum12';
|
|
186
|
+
default:
|
|
187
|
+
return 'unknown';
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* @returns {xdr.AssetType} the raw XDR representation of the asset type
|
|
193
|
+
*/
|
|
194
|
+
}, {
|
|
195
|
+
key: "getRawAssetType",
|
|
196
|
+
value: function getRawAssetType() {
|
|
197
|
+
if (this.isNative()) {
|
|
198
|
+
return _xdr["default"].AssetType.assetTypeNative();
|
|
199
|
+
}
|
|
200
|
+
if (this.code.length <= 4) {
|
|
201
|
+
return _xdr["default"].AssetType.assetTypeCreditAlphanum4();
|
|
202
|
+
}
|
|
203
|
+
return _xdr["default"].AssetType.assetTypeCreditAlphanum12();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @returns {boolean} true if this asset object is the native asset.
|
|
208
|
+
*/
|
|
209
|
+
}, {
|
|
210
|
+
key: "isNative",
|
|
211
|
+
value: function isNative() {
|
|
212
|
+
return !this.issuer;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @param {Asset} asset Asset to compare
|
|
217
|
+
* @returns {boolean} true if this asset equals the given asset.
|
|
218
|
+
*/
|
|
219
|
+
}, {
|
|
220
|
+
key: "equals",
|
|
221
|
+
value: function equals(asset) {
|
|
222
|
+
return this.code === asset.getCode() && this.issuer === asset.getIssuer();
|
|
223
|
+
}
|
|
224
|
+
}, {
|
|
225
|
+
key: "toString",
|
|
226
|
+
value: function toString() {
|
|
227
|
+
if (this.isNative()) {
|
|
228
|
+
return 'native';
|
|
229
|
+
}
|
|
230
|
+
return "".concat(this.getCode(), ":").concat(this.getIssuer());
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Compares two assets according to the criteria:
|
|
235
|
+
*
|
|
236
|
+
* 1. First compare the type (native < alphanum4 < alphanum12).
|
|
237
|
+
* 2. If the types are equal, compare the assets codes.
|
|
238
|
+
* 3. If the asset codes are equal, compare the issuers.
|
|
239
|
+
*
|
|
240
|
+
* @param {Asset} assetA - the first asset
|
|
241
|
+
* @param {Asset} assetB - the second asset
|
|
242
|
+
* @returns {number} `-1` if assetA < assetB, `0` if assetA == assetB, `1` if assetA > assetB.
|
|
243
|
+
*
|
|
244
|
+
* @static
|
|
245
|
+
* @memberof Asset
|
|
246
|
+
*/
|
|
247
|
+
}], [{
|
|
248
|
+
key: "native",
|
|
249
|
+
value: function native() {
|
|
250
|
+
return new Asset('XLM');
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Returns an asset object from its XDR object representation.
|
|
255
|
+
* @param {xdr.Asset} assetXdr - The asset xdr object.
|
|
256
|
+
* @returns {Asset}
|
|
257
|
+
*/
|
|
258
|
+
}, {
|
|
259
|
+
key: "fromOperation",
|
|
260
|
+
value: function fromOperation(assetXdr) {
|
|
261
|
+
var anum;
|
|
262
|
+
var code;
|
|
263
|
+
var issuer;
|
|
264
|
+
switch (assetXdr["switch"]()) {
|
|
265
|
+
case _xdr["default"].AssetType.assetTypeNative():
|
|
266
|
+
return this["native"]();
|
|
267
|
+
case _xdr["default"].AssetType.assetTypeCreditAlphanum4():
|
|
268
|
+
anum = assetXdr.alphaNum4();
|
|
269
|
+
/* falls through */
|
|
270
|
+
case _xdr["default"].AssetType.assetTypeCreditAlphanum12():
|
|
271
|
+
anum = anum || assetXdr.alphaNum12();
|
|
272
|
+
issuer = _strkey.StrKey.encodeEd25519PublicKey(anum.issuer().ed25519());
|
|
273
|
+
code = (0, _util.trimEnd)(anum.assetCode(), '\0');
|
|
274
|
+
return new this(code, issuer);
|
|
275
|
+
default:
|
|
276
|
+
throw new Error("Invalid asset type: ".concat(assetXdr["switch"]().name));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}, {
|
|
280
|
+
key: "compare",
|
|
281
|
+
value: function compare(assetA, assetB) {
|
|
282
|
+
if (!assetA || !(assetA instanceof Asset)) {
|
|
283
|
+
throw new Error('assetA is invalid');
|
|
284
|
+
}
|
|
285
|
+
if (!assetB || !(assetB instanceof Asset)) {
|
|
286
|
+
throw new Error('assetB is invalid');
|
|
287
|
+
}
|
|
288
|
+
if (assetA.equals(assetB)) {
|
|
289
|
+
return 0;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Compare asset types.
|
|
293
|
+
var xdrAtype = assetA.getRawAssetType().value;
|
|
294
|
+
var xdrBtype = assetB.getRawAssetType().value;
|
|
295
|
+
if (xdrAtype !== xdrBtype) {
|
|
296
|
+
return xdrAtype < xdrBtype ? -1 : 1;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Compare asset codes.
|
|
300
|
+
var result = asciiCompare(assetA.getCode(), assetB.getCode());
|
|
301
|
+
if (result !== 0) {
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Compare asset issuers.
|
|
306
|
+
return asciiCompare(assetA.getIssuer(), assetB.getIssuer());
|
|
307
|
+
}
|
|
308
|
+
}]);
|
|
309
|
+
return Asset;
|
|
310
|
+
}();
|
|
311
|
+
/**
|
|
312
|
+
* Compares two ASCII strings in lexographic order with uppercase precedence.
|
|
313
|
+
*
|
|
314
|
+
* @param {string} a - the first string to compare
|
|
315
|
+
* @param {string} b - the second
|
|
316
|
+
* @returns {number} like all `compare()`s:
|
|
317
|
+
* -1 if `a < b`, 0 if `a == b`, and 1 if `a > b`
|
|
318
|
+
*
|
|
319
|
+
* @warning No type-checks are done on the parameters
|
|
320
|
+
*/
|
|
321
|
+
function asciiCompare(a, b) {
|
|
322
|
+
return Buffer.compare(Buffer.from(a, 'ascii'), Buffer.from(b, 'ascii'));
|
|
323
|
+
}
|