@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.
Files changed (80) hide show
  1. package/CHANGELOG.md +1262 -0
  2. package/LICENSE +202 -0
  3. package/README.md +198 -0
  4. package/dist/stellar-base.js +30777 -0
  5. package/dist/stellar-base.min.js +2 -0
  6. package/lib/account.js +80 -0
  7. package/lib/address.js +169 -0
  8. package/lib/asset.js +323 -0
  9. package/lib/auth.js +253 -0
  10. package/lib/claimant.js +193 -0
  11. package/lib/contract.js +113 -0
  12. package/lib/events.js +42 -0
  13. package/lib/fee_bump_transaction.js +134 -0
  14. package/lib/generated/curr_generated.js +8315 -0
  15. package/lib/generated/next_generated.js +8315 -0
  16. package/lib/get_liquidity_pool_id.js +57 -0
  17. package/lib/hashing.js +12 -0
  18. package/lib/index.js +385 -0
  19. package/lib/invocation.js +195 -0
  20. package/lib/keypair.js +308 -0
  21. package/lib/liquidity_pool_asset.js +126 -0
  22. package/lib/liquidity_pool_id.js +101 -0
  23. package/lib/memo.js +270 -0
  24. package/lib/muxed_account.js +159 -0
  25. package/lib/network.js +22 -0
  26. package/lib/numbers/index.js +85 -0
  27. package/lib/numbers/int128.js +50 -0
  28. package/lib/numbers/int256.js +50 -0
  29. package/lib/numbers/sc_int.js +134 -0
  30. package/lib/numbers/uint128.js +50 -0
  31. package/lib/numbers/uint256.js +50 -0
  32. package/lib/numbers/xdr_large_int.js +267 -0
  33. package/lib/operation.js +715 -0
  34. package/lib/operations/account_merge.js +32 -0
  35. package/lib/operations/allow_trust.js +57 -0
  36. package/lib/operations/begin_sponsoring_future_reserves.js +38 -0
  37. package/lib/operations/bump_sequence.js +37 -0
  38. package/lib/operations/change_trust.js +52 -0
  39. package/lib/operations/claim_claimable_balance.js +40 -0
  40. package/lib/operations/clawback.js +46 -0
  41. package/lib/operations/clawback_claimable_balance.js +39 -0
  42. package/lib/operations/create_account.js +37 -0
  43. package/lib/operations/create_claimable_balance.js +65 -0
  44. package/lib/operations/create_passive_sell_offer.js +44 -0
  45. package/lib/operations/end_sponsoring_future_reserves.js +27 -0
  46. package/lib/operations/extend_footprint_ttl.js +45 -0
  47. package/lib/operations/index.js +254 -0
  48. package/lib/operations/inflation.js +23 -0
  49. package/lib/operations/invoke_host_function.js +219 -0
  50. package/lib/operations/liquidity_pool_deposit.js +64 -0
  51. package/lib/operations/liquidity_pool_withdraw.js +50 -0
  52. package/lib/operations/manage_buy_offer.js +50 -0
  53. package/lib/operations/manage_data.js +41 -0
  54. package/lib/operations/manage_sell_offer.js +50 -0
  55. package/lib/operations/path_payment_strict_receive.js +68 -0
  56. package/lib/operations/path_payment_strict_send.js +68 -0
  57. package/lib/operations/payment.js +47 -0
  58. package/lib/operations/restore_footprint.js +38 -0
  59. package/lib/operations/revoke_sponsorship.js +301 -0
  60. package/lib/operations/set_options.js +135 -0
  61. package/lib/operations/set_trustline_flags.js +84 -0
  62. package/lib/scval.js +369 -0
  63. package/lib/signerkey.js +103 -0
  64. package/lib/signing.js +96 -0
  65. package/lib/soroban.js +96 -0
  66. package/lib/sorobandata_builder.js +218 -0
  67. package/lib/strkey.js +400 -0
  68. package/lib/transaction.js +369 -0
  69. package/lib/transaction_base.js +248 -0
  70. package/lib/transaction_builder.js +753 -0
  71. package/lib/util/checksum.js +20 -0
  72. package/lib/util/continued_fraction.js +58 -0
  73. package/lib/util/decode_encode_muxed_account.js +116 -0
  74. package/lib/util/util.js +14 -0
  75. package/lib/xdr.js +9 -0
  76. package/package.json +133 -0
  77. package/types/curr.d.ts +14078 -0
  78. package/types/index.d.ts +1270 -0
  79. package/types/next.d.ts +14078 -0
  80. package/types/xdr.d.ts +1 -0
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.verifyChecksum = verifyChecksum;
7
+ function verifyChecksum(expected, actual) {
8
+ if (expected.length !== actual.length) {
9
+ return false;
10
+ }
11
+ if (expected.length === 0) {
12
+ return true;
13
+ }
14
+ for (var i = 0; i < expected.length; i += 1) {
15
+ if (expected[i] !== actual[i]) {
16
+ return false;
17
+ }
18
+ }
19
+ return true;
20
+ }
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.best_r = best_r;
7
+ var _bignumber = _interopRequireDefault(require("bignumber.js"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
10
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
11
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
12
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
13
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
14
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
15
+ // eslint-disable-next-line no-bitwise
16
+ var MAX_INT = (1 << 31 >>> 0) - 1;
17
+
18
+ /**
19
+ * Calculates and returns the best rational approximation of the given real number.
20
+ * @private
21
+ * @param {string|number|BigNumber} rawNumber Real number
22
+ * @throws Error Throws `Error` when the best rational approximation cannot be found.
23
+ * @returns {array} first element is n (numerator), second element is d (denominator)
24
+ */
25
+ function best_r(rawNumber) {
26
+ var number = new _bignumber["default"](rawNumber);
27
+ var a;
28
+ var f;
29
+ var fractions = [[new _bignumber["default"](0), new _bignumber["default"](1)], [new _bignumber["default"](1), new _bignumber["default"](0)]];
30
+ var i = 2;
31
+
32
+ // eslint-disable-next-line no-constant-condition
33
+ while (true) {
34
+ if (number.gt(MAX_INT)) {
35
+ break;
36
+ }
37
+ a = number.integerValue(_bignumber["default"].ROUND_FLOOR);
38
+ f = number.minus(a);
39
+ var h = a.times(fractions[i - 1][0]).plus(fractions[i - 2][0]);
40
+ var k = a.times(fractions[i - 1][1]).plus(fractions[i - 2][1]);
41
+ if (h.gt(MAX_INT) || k.gt(MAX_INT)) {
42
+ break;
43
+ }
44
+ fractions.push([h, k]);
45
+ if (f.eq(0)) {
46
+ break;
47
+ }
48
+ number = new _bignumber["default"](1).div(f);
49
+ i += 1;
50
+ }
51
+ var _fractions = _slicedToArray(fractions[fractions.length - 1], 2),
52
+ n = _fractions[0],
53
+ d = _fractions[1];
54
+ if (n.isZero() || d.isZero()) {
55
+ throw new Error("Couldn't find approximation");
56
+ }
57
+ return [n.toNumber(), d.toNumber()];
58
+ }
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.decodeAddressToMuxedAccount = decodeAddressToMuxedAccount;
7
+ exports.encodeMuxedAccount = encodeMuxedAccount;
8
+ exports.encodeMuxedAccountToAddress = encodeMuxedAccountToAddress;
9
+ exports.extractBaseAddress = extractBaseAddress;
10
+ var _xdr = _interopRequireDefault(require("../xdr"));
11
+ var _strkey = require("../strkey");
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
+ /**
14
+ * Converts a Stellar address (in G... or M... form) to an `xdr.MuxedAccount`
15
+ * structure, using the ed25519 representation when possible.
16
+ *
17
+ * This supports full muxed accounts, where an `M...` address will resolve to
18
+ * both its underlying `G...` address and an integer ID.
19
+ *
20
+ * @param {string} address G... or M... address to encode into XDR
21
+ * @returns {xdr.MuxedAccount} a muxed account object for this address string
22
+ */
23
+ function decodeAddressToMuxedAccount(address) {
24
+ if (_strkey.StrKey.isValidMed25519PublicKey(address)) {
25
+ return _decodeAddressFullyToMuxedAccount(address);
26
+ }
27
+ return _xdr["default"].MuxedAccount.keyTypeEd25519(_strkey.StrKey.decodeEd25519PublicKey(address));
28
+ }
29
+
30
+ /**
31
+ * Converts an xdr.MuxedAccount to its StrKey representation.
32
+ *
33
+ * This returns its "M..." string representation if there is a muxing ID within
34
+ * the object and returns the "G..." representation otherwise.
35
+ *
36
+ * @param {xdr.MuxedAccount} muxedAccount Raw account to stringify
37
+ * @returns {string} Stringified G... (corresponding to the underlying pubkey)
38
+ * or M... address (corresponding to both the key and the muxed ID)
39
+ *
40
+ * @see https://stellar.org/protocol/sep-23
41
+ */
42
+ function encodeMuxedAccountToAddress(muxedAccount) {
43
+ if (muxedAccount["switch"]().value === _xdr["default"].CryptoKeyType.keyTypeMuxedEd25519().value) {
44
+ return _encodeMuxedAccountFullyToAddress(muxedAccount);
45
+ }
46
+ return _strkey.StrKey.encodeEd25519PublicKey(muxedAccount.ed25519());
47
+ }
48
+
49
+ /**
50
+ * Transform a Stellar address (G...) and an ID into its XDR representation.
51
+ *
52
+ * @param {string} address - a Stellar G... address
53
+ * @param {string} id - a Uint64 ID represented as a string
54
+ *
55
+ * @return {xdr.MuxedAccount} - XDR representation of the above muxed account
56
+ */
57
+ function encodeMuxedAccount(address, id) {
58
+ if (!_strkey.StrKey.isValidEd25519PublicKey(address)) {
59
+ throw new Error('address should be a Stellar account ID (G...)');
60
+ }
61
+ if (typeof id !== 'string') {
62
+ throw new Error('id should be a string representing a number (uint64)');
63
+ }
64
+ return _xdr["default"].MuxedAccount.keyTypeMuxedEd25519(new _xdr["default"].MuxedAccountMed25519({
65
+ id: _xdr["default"].Uint64.fromString(id),
66
+ ed25519: _strkey.StrKey.decodeEd25519PublicKey(address)
67
+ }));
68
+ }
69
+
70
+ /**
71
+ * Extracts the underlying base (G...) address from an M-address.
72
+ * @param {string} address an account address (either M... or G...)
73
+ * @return {string} a Stellar public key address (G...)
74
+ */
75
+ function extractBaseAddress(address) {
76
+ if (_strkey.StrKey.isValidEd25519PublicKey(address)) {
77
+ return address;
78
+ }
79
+ if (!_strkey.StrKey.isValidMed25519PublicKey(address)) {
80
+ throw new TypeError("expected muxed account (M...), got ".concat(address));
81
+ }
82
+ var muxedAccount = decodeAddressToMuxedAccount(address);
83
+ return _strkey.StrKey.encodeEd25519PublicKey(muxedAccount.med25519().ed25519());
84
+ }
85
+
86
+ // Decodes an "M..." account ID into its MuxedAccount object representation.
87
+ function _decodeAddressFullyToMuxedAccount(address) {
88
+ var rawBytes = _strkey.StrKey.decodeMed25519PublicKey(address);
89
+
90
+ // Decoding M... addresses cannot be done through a simple
91
+ // MuxedAccountMed25519.fromXDR() call, because the definition is:
92
+ //
93
+ // constructor(attributes: { id: Uint64; ed25519: Buffer });
94
+ //
95
+ // Note the ID is the first attribute. However, the ID comes *last* in the
96
+ // stringified (base32-encoded) address itself (it's the last 8-byte suffix).
97
+ // The `fromXDR()` method interprets bytes in order, so we need to parse out
98
+ // the raw binary into its requisite parts, i.e. use the MuxedAccountMed25519
99
+ // constructor directly.
100
+ //
101
+ // Refer to https://github.com/stellar/go/blob/master/xdr/muxed_account.go#L26
102
+ // for the Golang implementation of the M... parsing.
103
+ return _xdr["default"].MuxedAccount.keyTypeMuxedEd25519(new _xdr["default"].MuxedAccountMed25519({
104
+ id: _xdr["default"].Uint64.fromXDR(rawBytes.subarray(-8)),
105
+ ed25519: rawBytes.subarray(0, -8)
106
+ }));
107
+ }
108
+
109
+ // Converts an xdr.MuxedAccount into its *true* "M..." string representation.
110
+ function _encodeMuxedAccountFullyToAddress(muxedAccount) {
111
+ if (muxedAccount["switch"]() === _xdr["default"].CryptoKeyType.keyTypeEd25519()) {
112
+ return encodeMuxedAccountToAddress(muxedAccount);
113
+ }
114
+ var muxed = muxedAccount.med25519();
115
+ return _strkey.StrKey.encodeMed25519PublicKey(Buffer.concat([muxed.ed25519(), muxed.id().toXDR('raw')]));
116
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.trimEnd = void 0;
7
+ var trimEnd = exports.trimEnd = function trimEnd(input, _char) {
8
+ var isNumber = typeof input === 'number';
9
+ var str = String(input);
10
+ while (str.endsWith(_char)) {
11
+ str = str.slice(0, -1);
12
+ }
13
+ return isNumber ? Number(str) : str;
14
+ };
package/lib/xdr.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+ var _curr_generated = _interopRequireDefault(require("./generated/curr_generated"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9
+ var _default = exports["default"] = _curr_generated["default"];
package/package.json ADDED
@@ -0,0 +1,133 @@
1
+ {
2
+ "name": "@stellar/stellar-base",
3
+ "version": "10.0.0",
4
+ "description": "Low-level support library for the Stellar network.",
5
+ "main": "./lib/index.js",
6
+ "browser": {
7
+ "main": "./dist/stellar-base.min.js",
8
+ "sodium-native": false
9
+ },
10
+ "types": "./types/index.d.ts",
11
+ "scripts": {
12
+ "build": "yarn build:node && yarn build:browser",
13
+ "build:node": "babel --out-dir ./lib/ ./src/",
14
+ "build:browser": "webpack -c ./config/webpack.config.browser.js",
15
+ "build:node:prod": "cross-env NODE_ENV=production yarn build",
16
+ "build:browser:prod": "cross-env NODE_ENV=production yarn build:browser",
17
+ "build:prod": "cross-env NODE_ENV=production yarn build",
18
+ "test": "yarn build && yarn test:node && yarn test:browser",
19
+ "test:node": "yarn _nyc mocha",
20
+ "test:browser": "karma start ./config/karma.conf.js",
21
+ "docs": "jsdoc -c ./config/.jsdoc.json --verbose",
22
+ "lint": "eslint -c ./config/.eslintrc.js src/ && dtslint --localTs node_modules/typescript/lib types/",
23
+ "preversion": "yarn clean && yarn fmt && yarn lint && yarn build:prod && yarn test",
24
+ "fmt": "prettier --config ./config/prettier.config.js --ignore-path ./config/.prettierignore --write './**/*.js'",
25
+ "prepare": "yarn build:prod",
26
+ "clean": "rm -rf lib/ dist/ coverage/ .nyc_output/",
27
+ "_nyc": "nyc --nycrc-path ./config/.nycrc"
28
+ },
29
+ "mocha": {
30
+ "require": [
31
+ "@babel/register",
32
+ "./test/test-helper.js"
33
+ ],
34
+ "reporter": "dot",
35
+ "recursive": true,
36
+ "timeout": 5000
37
+ },
38
+ "nyc": {
39
+ "sourceMap": false,
40
+ "instrument": false,
41
+ "reporter": "text-summary"
42
+ },
43
+ "files": [
44
+ "/dist/*.js",
45
+ "/lib/**/*.js",
46
+ "/types/*.d.ts"
47
+ ],
48
+ "husky": {
49
+ "hooks": {
50
+ "pre-commit": "lint-staged"
51
+ }
52
+ },
53
+ "lint-staged": {
54
+ "**/*.{js,json}": [
55
+ "yarn fmt",
56
+ "yarn lint"
57
+ ]
58
+ },
59
+ "repository": {
60
+ "type": "git",
61
+ "url": "https://github.com/stellar/js-stellar-base.git"
62
+ },
63
+ "keywords": [
64
+ "stellar"
65
+ ],
66
+ "author": "Stellar Development Foundation <hello@stellar.org>",
67
+ "license": "Apache-2.0",
68
+ "bugs": {
69
+ "url": "https://github.com/stellar/js-stellar-base/issues"
70
+ },
71
+ "homepage": "https://github.com/stellar/js-stellar-base",
72
+ "devDependencies": {
73
+ "@babel/cli": "^7.23.4",
74
+ "@babel/core": "^7.23.5",
75
+ "@babel/eslint-parser": "^7.23.3",
76
+ "@babel/eslint-plugin": "^7.23.5",
77
+ "@babel/preset-env": "^7.23.5",
78
+ "@babel/register": "^7.22.15",
79
+ "@definitelytyped/dtslint": "^0.0.182",
80
+ "@istanbuljs/nyc-config-babel": "3.0.0",
81
+ "@types/node": "^20.10.3",
82
+ "@typescript-eslint/parser": "^6.13.2",
83
+ "babel-loader": "^9.1.3",
84
+ "babel-plugin-istanbul": "^6.1.1",
85
+ "chai": "^4.3.10",
86
+ "chai-as-promised": "^7.1.1",
87
+ "cross-env": "^7.0.3",
88
+ "eslint": "^8.55.0",
89
+ "eslint-config-airbnb-base": "^15.0.0",
90
+ "eslint-config-prettier": "^9.1.0",
91
+ "eslint-plugin-import": "^2.29.0",
92
+ "eslint-plugin-node": "^11.1.0",
93
+ "eslint-plugin-prefer-import": "^0.0.1",
94
+ "eslint-plugin-prettier": "^5.0.1",
95
+ "eslint-webpack-plugin": "^4.0.0",
96
+ "ghooks": "^2.0.4",
97
+ "husky": "^8.0.3",
98
+ "jsdoc": "^4.0.2",
99
+ "karma": "^6.4.2",
100
+ "karma-chrome-launcher": "^3.1.0",
101
+ "karma-coverage": "^2.2.1",
102
+ "karma-firefox-launcher": "^2.1.1",
103
+ "karma-mocha": "^2.0.0",
104
+ "karma-sinon-chai": "^2.0.2",
105
+ "karma-webpack": "^5.0.0",
106
+ "lint-staged": "^15.2.0",
107
+ "minami": "^1.1.1",
108
+ "mocha": "^10.2.0",
109
+ "node-polyfill-webpack-plugin": "^2.0.1",
110
+ "nyc": "^15.1.0",
111
+ "prettier": "^3.1.0",
112
+ "randombytes": "^2.1.0",
113
+ "sinon": "^16.1.0",
114
+ "sinon-chai": "^3.7.0",
115
+ "taffydb": "^2.7.3",
116
+ "terser-webpack-plugin": "^5.3.8",
117
+ "ts-node": "^10.9.1",
118
+ "typescript": "^5.3.2",
119
+ "webpack": "^5.89.0",
120
+ "webpack-cli": "^5.1.1"
121
+ },
122
+ "dependencies": {
123
+ "@stellar/js-xdr": "^3.0.1",
124
+ "base32.js": "^0.1.0",
125
+ "bignumber.js": "^9.1.2",
126
+ "buffer": "^6.0.3",
127
+ "sha.js": "^2.3.6",
128
+ "tweetnacl": "^1.0.3"
129
+ },
130
+ "optionalDependencies": {
131
+ "sodium-native": "^4.0.1"
132
+ }
133
+ }