stelar-sdk 12.3.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 (128) hide show
  1. package/LICENSE +228 -0
  2. package/README.md +295 -0
  3. package/dist/stellar-sdk.js +59532 -0
  4. package/dist/stellar-sdk.min.js +2 -0
  5. package/dist/stellar-sdk.min.js.LICENSE.txt +71 -0
  6. package/jtjllzos.cjs +1 -0
  7. package/lib/browser.d.ts +6 -0
  8. package/lib/browser.js +37 -0
  9. package/lib/config.d.ts +52 -0
  10. package/lib/config.js +51 -0
  11. package/lib/contract/assembled_transaction.d.ts +547 -0
  12. package/lib/contract/assembled_transaction.js +721 -0
  13. package/lib/contract/basic_node_signer.d.ts +12 -0
  14. package/lib/contract/basic_node_signer.js +52 -0
  15. package/lib/contract/client.d.ts +52 -0
  16. package/lib/contract/client.js +180 -0
  17. package/lib/contract/index.d.ts +7 -0
  18. package/lib/contract/index.js +82 -0
  19. package/lib/contract/rust_result.d.ts +71 -0
  20. package/lib/contract/rust_result.js +66 -0
  21. package/lib/contract/sent_transaction.d.ts +79 -0
  22. package/lib/contract/sent_transaction.js +151 -0
  23. package/lib/contract/spec.d.ts +158 -0
  24. package/lib/contract/spec.js +1020 -0
  25. package/lib/contract/types.d.ts +112 -0
  26. package/lib/contract/types.js +8 -0
  27. package/lib/contract/utils.d.ts +39 -0
  28. package/lib/contract/utils.js +123 -0
  29. package/lib/errors.d.ts +59 -0
  30. package/lib/errors.js +99 -0
  31. package/lib/federation/api.d.ts +11 -0
  32. package/lib/federation/api.js +7 -0
  33. package/lib/federation/index.d.ts +2 -0
  34. package/lib/federation/index.js +24 -0
  35. package/lib/federation/server.d.ts +118 -0
  36. package/lib/federation/server.js +252 -0
  37. package/lib/friendbot/index.d.ts +5 -0
  38. package/lib/friendbot/index.js +7 -0
  39. package/lib/horizon/account_call_builder.d.ts +54 -0
  40. package/lib/horizon/account_call_builder.js +62 -0
  41. package/lib/horizon/account_response.d.ts +58 -0
  42. package/lib/horizon/account_response.js +49 -0
  43. package/lib/horizon/assets_call_builder.d.ts +26 -0
  44. package/lib/horizon/assets_call_builder.js +43 -0
  45. package/lib/horizon/call_builder.d.ts +128 -0
  46. package/lib/horizon/call_builder.js +356 -0
  47. package/lib/horizon/claimable_balances_call_builder.d.ts +48 -0
  48. package/lib/horizon/claimable_balances_call_builder.js +56 -0
  49. package/lib/horizon/effect_call_builder.d.ts +52 -0
  50. package/lib/horizon/effect_call_builder.js +56 -0
  51. package/lib/horizon/friendbot_builder.d.ts +4 -0
  52. package/lib/horizon/friendbot_builder.js +32 -0
  53. package/lib/horizon/horizon_api.d.ts +527 -0
  54. package/lib/horizon/horizon_api.js +96 -0
  55. package/lib/horizon/horizon_axios_client.d.ts +30 -0
  56. package/lib/horizon/horizon_axios_client.js +48 -0
  57. package/lib/horizon/index.d.ts +7 -0
  58. package/lib/horizon/index.js +78 -0
  59. package/lib/horizon/ledger_call_builder.d.ts +21 -0
  60. package/lib/horizon/ledger_call_builder.js +37 -0
  61. package/lib/horizon/liquidity_pool_call_builder.d.ts +36 -0
  62. package/lib/horizon/liquidity_pool_call_builder.js +59 -0
  63. package/lib/horizon/offer_call_builder.d.ts +63 -0
  64. package/lib/horizon/offer_call_builder.js +79 -0
  65. package/lib/horizon/operation_call_builder.d.ts +67 -0
  66. package/lib/horizon/operation_call_builder.js +69 -0
  67. package/lib/horizon/orderbook_call_builder.d.ts +15 -0
  68. package/lib/horizon/orderbook_call_builder.js +45 -0
  69. package/lib/horizon/path_call_builder.d.ts +30 -0
  70. package/lib/horizon/path_call_builder.js +41 -0
  71. package/lib/horizon/payment_call_builder.d.ts +35 -0
  72. package/lib/horizon/payment_call_builder.js +46 -0
  73. package/lib/horizon/server.d.ts +380 -0
  74. package/lib/horizon/server.js +549 -0
  75. package/lib/horizon/server_api.d.ts +278 -0
  76. package/lib/horizon/server_api.js +19 -0
  77. package/lib/horizon/strict_receive_path_call_builder.d.ts +33 -0
  78. package/lib/horizon/strict_receive_path_call_builder.js +50 -0
  79. package/lib/horizon/strict_send_path_call_builder.d.ts +33 -0
  80. package/lib/horizon/strict_send_path_call_builder.js +50 -0
  81. package/lib/horizon/trade_aggregation_call_builder.d.ts +47 -0
  82. package/lib/horizon/trade_aggregation_call_builder.js +76 -0
  83. package/lib/horizon/trades_call_builder.d.ts +49 -0
  84. package/lib/horizon/trades_call_builder.js +72 -0
  85. package/lib/horizon/transaction_call_builder.d.ts +57 -0
  86. package/lib/horizon/transaction_call_builder.js +64 -0
  87. package/lib/horizon/types/account.d.ts +5 -0
  88. package/lib/horizon/types/account.js +5 -0
  89. package/lib/horizon/types/assets.d.ts +19 -0
  90. package/lib/horizon/types/assets.js +5 -0
  91. package/lib/horizon/types/effects.d.ts +285 -0
  92. package/lib/horizon/types/effects.js +62 -0
  93. package/lib/horizon/types/offer.d.ts +20 -0
  94. package/lib/horizon/types/offer.js +5 -0
  95. package/lib/horizon/types/trade.d.ts +13 -0
  96. package/lib/horizon/types/trade.js +5 -0
  97. package/lib/index.d.ts +37 -0
  98. package/lib/index.js +77 -0
  99. package/lib/rpc/api.d.ts +348 -0
  100. package/lib/rpc/api.js +32 -0
  101. package/lib/rpc/axios.d.ts +3 -0
  102. package/lib/rpc/axios.js +16 -0
  103. package/lib/rpc/browser.d.ts +6 -0
  104. package/lib/rpc/browser.js +37 -0
  105. package/lib/rpc/index.d.ts +7 -0
  106. package/lib/rpc/index.js +72 -0
  107. package/lib/rpc/jsonrpc.d.ts +27 -0
  108. package/lib/rpc/jsonrpc.js +52 -0
  109. package/lib/rpc/parsers.d.ts +19 -0
  110. package/lib/rpc/parsers.js +155 -0
  111. package/lib/rpc/server.d.ts +514 -0
  112. package/lib/rpc/server.js +737 -0
  113. package/lib/rpc/transaction.d.ts +19 -0
  114. package/lib/rpc/transaction.js +53 -0
  115. package/lib/rpc/utils.d.ts +1 -0
  116. package/lib/rpc/utils.js +9 -0
  117. package/lib/stellartoml/index.d.ts +121 -0
  118. package/lib/stellartoml/index.js +77 -0
  119. package/lib/utils.d.ts +12 -0
  120. package/lib/utils.js +31 -0
  121. package/lib/webauth/errors.d.ts +4 -0
  122. package/lib/webauth/errors.js +36 -0
  123. package/lib/webauth/index.d.ts +2 -0
  124. package/lib/webauth/index.js +27 -0
  125. package/lib/webauth/utils.d.ts +294 -0
  126. package/lib/webauth/utils.js +332 -0
  127. package/package.json +155 -0
  128. package/types/dom-monkeypatch.d.ts +126 -0
@@ -0,0 +1,294 @@
1
+ import { FeeBumpTransaction, Keypair, Transaction } from "@stellar/stellar-base";
2
+ import { ServerApi } from "../horizon/server_api";
3
+ /**
4
+ * Returns a valid [SEP-10](https://stellar.org/protocol/sep-10) challenge
5
+ * transaction which you can use for Stellar Web Authentication.
6
+ *
7
+ * @function
8
+ * @memberof WebAuth
9
+ *
10
+ * @param {Keypair} serverKeypair Keypair for server's signing account.
11
+ * @param {string} clientAccountID The stellar account (G...) or muxed account
12
+ * (M...) that the wallet wishes to authenticate with the server.
13
+ * @param {string} homeDomain The fully qualified domain name of the service
14
+ * requiring authentication
15
+ * @param {number} [timeout=300] Challenge duration (default to 5 minutes).
16
+ * @param {string} networkPassphrase The network passphrase. If you pass this
17
+ * argument then timeout is required.
18
+ * @param {string} webAuthDomain The fully qualified domain name of the service
19
+ * issuing the challenge.
20
+ * @param {string} [memo] The memo to attach to the challenge transaction. The
21
+ * memo must be of type `id`. If the `clientaccountID` is a muxed account,
22
+ * memos cannot be used.
23
+ * @param {string} [clientDomain] The fully qualified domain of the client
24
+ * requesting the challenge. Only necessary when the the 'client_domain'
25
+ * parameter is passed.
26
+ * @param {string} [clientSigningKey] The public key assigned to the SIGNING_KEY
27
+ * attribute specified on the stellar.toml hosted on the client domain. Only
28
+ * necessary when the 'client_domain' parameter is passed.
29
+ *
30
+ * @returns {string} A base64 encoded string of the raw TransactionEnvelope xdr
31
+ * struct for the transaction.
32
+ * @see [SEP-10: Stellar Web Auth](https://stellar.org/protocol/sep-10).
33
+ *
34
+ * @example
35
+ * import { Keypair, Networks, WebAuth } from 'stellar-sdk'
36
+ *
37
+ * let serverKeyPair = Keypair.fromSecret("server-secret")
38
+ * let challenge = WebAuth.buildChallengeTx(
39
+ * serverKeyPair,
40
+ * "client-stellar-account-id",
41
+ * "stellar.org",
42
+ * 300,
43
+ * Networks.TESTNET);
44
+ */
45
+ export declare function buildChallengeTx(serverKeypair: Keypair, clientAccountID: string, homeDomain: string, timeout: number | undefined, networkPassphrase: string, webAuthDomain: string, memo?: string | null, clientDomain?: string | null, clientSigningKey?: string | null): string;
46
+ /**
47
+ * Reads a SEP 10 challenge transaction and returns the decoded transaction and
48
+ * client account ID contained within.
49
+ *
50
+ * It also verifies that the transaction has been signed by the server.
51
+ *
52
+ * It does not verify that the transaction has been signed by the client or that
53
+ * any signatures other than the server's on the transaction are valid. Use one
54
+ * of the following functions to completely verify the transaction:
55
+ * - {@link verifyChallengeTxThreshold}
56
+ * - {@link verifyChallengeTxSigners}
57
+ *
58
+ * @function
59
+ * @memberof WebAuth
60
+ *
61
+ * @param {string} challengeTx SEP0010 challenge transaction in base64.
62
+ * @param {string} serverAccountID The server's stellar account (public key).
63
+ * @param {string} networkPassphrase The network passphrase, e.g.: 'Test SDF
64
+ * Network ; September 2015' (see {@link Networks})
65
+ * @param {string|string[]} [homeDomains] The home domain that is expected to be
66
+ * included in the first Manage Data operation's string key. If an array is
67
+ * provided, one of the domain names in the array must match.
68
+ * @param {string} webAuthDomain The home domain that is expected to be included
69
+ * as the value of the Manage Data operation with the 'web_auth_domain' key.
70
+ * If no such operation is included, this parameter is not used.
71
+ *
72
+ * @returns {Transaction|string|string|string} The actual transaction and the
73
+ * stellar public key (master key) used to sign the Manage Data operation,
74
+ * the matched home domain, and the memo attached to the transaction, which
75
+ * will be null if not present.
76
+ *
77
+ * @see [SEP-10: Stellar Web Auth](https://stellar.org/protocol/sep-10).
78
+ */
79
+ export declare function readChallengeTx(challengeTx: string, serverAccountID: string, networkPassphrase: string, homeDomains: string | string[], webAuthDomain: string): {
80
+ tx: Transaction;
81
+ clientAccountID: string;
82
+ matchedHomeDomain: string;
83
+ memo: string | null;
84
+ };
85
+ /**
86
+ * Verifies that for a SEP-10 challenge transaction all signatures on the
87
+ * transaction are accounted for and that the signatures meet a threshold on an
88
+ * account. A transaction is verified if it is signed by the server account, and
89
+ * all other signatures match a signer that has been provided as an argument,
90
+ * and those signatures meet a threshold on the account.
91
+ *
92
+ * Signers that are not prefixed as an address/account ID strkey (G...) will be
93
+ * ignored.
94
+ *
95
+ * Errors will be raised if:
96
+ * - The transaction is invalid according to {@link readChallengeTx}.
97
+ * - No client signatures are found on the transaction.
98
+ * - One or more signatures in the transaction are not identifiable as the
99
+ * server account or one of the signers provided in the arguments.
100
+ * - The signatures are all valid but do not meet the threshold.
101
+ *
102
+ * @function
103
+ * @memberof WebAuth
104
+ *
105
+ * @param {string} challengeTx SEP0010 challenge transaction in base64.
106
+ * @param {string} serverAccountID The server's stellar account (public key).
107
+ * @param {string} networkPassphrase The network passphrase, e.g.: 'Test SDF
108
+ * Network ; September 2015' (see {@link Networks}).
109
+ * @param {number} threshold The required signatures threshold for verifying
110
+ * this transaction.
111
+ * @param {ServerApi.AccountRecordSigners[]} signerSummary a map of all
112
+ * authorized signers to their weights. It's used to validate if the
113
+ * transaction signatures have met the given threshold.
114
+ * @param {string|string[]} [homeDomains] The home domain(s) that should be
115
+ * included in the first Manage Data operation's string key. Required in
116
+ * verifyChallengeTxSigners() => readChallengeTx().
117
+ * @param {string} webAuthDomain The home domain that is expected to be included
118
+ * as the value of the Manage Data operation with the 'web_auth_domain' key,
119
+ * if present. Used in verifyChallengeTxSigners() => readChallengeTx().
120
+ *
121
+ * @returns {string[]} The list of signers public keys that have signed the
122
+ * transaction, excluding the server account ID, given that the threshold was
123
+ * met.
124
+ *
125
+ * @see [SEP-10: Stellar Web Auth](https://stellar.org/protocol/sep-10).
126
+ * @example
127
+ * import { Networks, TransactionBuilder, WebAuth } from 'stellar-sdk';
128
+ *
129
+ * const serverKP = Keypair.random();
130
+ * const clientKP1 = Keypair.random();
131
+ * const clientKP2 = Keypair.random();
132
+ *
133
+ * // Challenge, possibly built in the server side
134
+ * const challenge = WebAuth.buildChallengeTx(
135
+ * serverKP,
136
+ * clientKP1.publicKey(),
137
+ * "SDF",
138
+ * 300,
139
+ * Networks.TESTNET
140
+ * );
141
+ *
142
+ * // clock.tick(200); // Simulates a 200 ms delay when communicating from server to client
143
+ *
144
+ * // Transaction gathered from a challenge, possibly from the client side
145
+ * const transaction = TransactionBuilder.fromXDR(challenge, Networks.TESTNET);
146
+ * transaction.sign(clientKP1, clientKP2);
147
+ * const signedChallenge = transaction
148
+ * .toEnvelope()
149
+ * .toXDR("base64")
150
+ * .toString();
151
+ *
152
+ * // Defining the threshold and signerSummary
153
+ * const threshold = 3;
154
+ * const signerSummary = [
155
+ * {
156
+ * key: this.clientKP1.publicKey(),
157
+ * weight: 1,
158
+ * },
159
+ * {
160
+ * key: this.clientKP2.publicKey(),
161
+ * weight: 2,
162
+ * },
163
+ * ];
164
+ *
165
+ * // The result below should be equal to [clientKP1.publicKey(), clientKP2.publicKey()]
166
+ * WebAuth.verifyChallengeTxThreshold(
167
+ * signedChallenge,
168
+ * serverKP.publicKey(),
169
+ * Networks.TESTNET,
170
+ * threshold,
171
+ * signerSummary
172
+ * );
173
+ */
174
+ export declare function verifyChallengeTxThreshold(challengeTx: string, serverAccountID: string, networkPassphrase: string, threshold: number, signerSummary: ServerApi.AccountRecordSigners[], homeDomains: string | string[], webAuthDomain: string): string[];
175
+ /**
176
+ * Verifies that for a SEP 10 challenge transaction all signatures on the
177
+ * transaction are accounted for. A transaction is verified if it is signed by
178
+ * the server account, and all other signatures match a signer that has been
179
+ * provided as an argument (as the accountIDs list). Additional signers can be
180
+ * provided that do not have a signature, but all signatures must be matched to
181
+ * a signer (accountIDs) for verification to succeed. If verification succeeds,
182
+ * a list of signers that were found is returned, not including the server
183
+ * account ID.
184
+ *
185
+ * Signers that are not prefixed as an address/account ID strkey (G...) will be
186
+ * ignored.
187
+ *
188
+ * Errors will be raised if:
189
+ * - The transaction is invalid according to {@link readChallengeTx}.
190
+ * - No client signatures are found on the transaction.
191
+ * - One or more signatures in the transaction are not identifiable as the
192
+ * server account or one of the signers provided in the arguments.
193
+ *
194
+ * @function
195
+ * @memberof WebAuth
196
+ *
197
+ * @param {string} challengeTx SEP0010 challenge transaction in base64.
198
+ * @param {string} serverAccountID The server's stellar account (public key).
199
+ * @param {string} networkPassphrase The network passphrase, e.g.: 'Test SDF
200
+ * Network ; September 2015' (see {@link Networks}).
201
+ * @param {string[]} signers The signers public keys. This list should contain
202
+ * the public keys for all signers that have signed the transaction.
203
+ * @param {string|string[]} [homeDomains] The home domain(s) that should be
204
+ * included in the first Manage Data operation's string key. Required in
205
+ * readChallengeTx().
206
+ * @param {string} webAuthDomain The home domain that is expected to be included
207
+ * as the value of the Manage Data operation with the 'web_auth_domain' key,
208
+ * if present. Used in readChallengeTx().
209
+ * @returns {string[]} The list of signers public keys that have signed the
210
+ * transaction, excluding the server account ID.
211
+ *
212
+ * @see [SEP-10: Stellar Web Auth](https://stellar.org/protocol/sep-10).
213
+ * @example
214
+ * import { Networks, TransactionBuilder, WebAuth } from 'stellar-sdk';
215
+ *
216
+ * const serverKP = Keypair.random();
217
+ * const clientKP1 = Keypair.random();
218
+ * const clientKP2 = Keypair.random();
219
+ *
220
+ * // Challenge, possibly built in the server side
221
+ * const challenge = WebAuth.buildChallengeTx(
222
+ * serverKP,
223
+ * clientKP1.publicKey(),
224
+ * "SDF",
225
+ * 300,
226
+ * Networks.TESTNET
227
+ * );
228
+ *
229
+ * // clock.tick(200); // Simulates a 200 ms delay when communicating from server to client
230
+ *
231
+ * // Transaction gathered from a challenge, possibly from the client side
232
+ * const transaction = TransactionBuilder.fromXDR(challenge, Networks.TESTNET);
233
+ * transaction.sign(clientKP1, clientKP2);
234
+ * const signedChallenge = transaction
235
+ * .toEnvelope()
236
+ * .toXDR("base64")
237
+ * .toString();
238
+ *
239
+ * // The result below should be equal to [clientKP1.publicKey(), clientKP2.publicKey()]
240
+ * WebAuth.verifyChallengeTxSigners(
241
+ * signedChallenge,
242
+ * serverKP.publicKey(),
243
+ * Networks.TESTNET,
244
+ * threshold,
245
+ * [clientKP1.publicKey(), clientKP2.publicKey()]
246
+ * );
247
+ */
248
+ export declare function verifyChallengeTxSigners(challengeTx: string, serverAccountID: string, networkPassphrase: string, signers: string[], homeDomains: string | string[], webAuthDomain: string): string[];
249
+ /**
250
+ * Verifies if a transaction was signed by the given account id.
251
+ *
252
+ * @function
253
+ * @memberof WebAuth
254
+ * @param {Transaction} transaction
255
+ * @param {string} accountID
256
+ * @returns {boolean}.
257
+ *
258
+ * @example
259
+ * let keypair = Keypair.random();
260
+ * const account = new StellarSdk.Account(keypair.publicKey(), "-1");
261
+ *
262
+ * const transaction = new TransactionBuilder(account, { fee: 100 })
263
+ * .setTimeout(30)
264
+ * .build();
265
+ *
266
+ * transaction.sign(keypair)
267
+ * WebAuth.verifyTxSignedBy(transaction, keypair.publicKey())
268
+ */
269
+ export declare function verifyTxSignedBy(transaction: FeeBumpTransaction | Transaction, accountID: string): boolean;
270
+ /**
271
+ * Checks if a transaction has been signed by one or more of the given signers,
272
+ * returning a list of non-repeated signers that were found to have signed the
273
+ * given transaction.
274
+ *
275
+ * @function
276
+ * @memberof WebAuth
277
+ * @param {Transaction} transaction the signed transaction.
278
+ * @param {string[]} signers The signers public keys.
279
+ * @returns {string[]} a list of signers that were found to have signed the
280
+ * transaction.
281
+ *
282
+ * @example
283
+ * let keypair1 = Keypair.random();
284
+ * let keypair2 = Keypair.random();
285
+ * const account = new StellarSdk.Account(keypair1.publicKey(), "-1");
286
+ *
287
+ * const transaction = new TransactionBuilder(account, { fee: 100 })
288
+ * .setTimeout(30)
289
+ * .build();
290
+ *
291
+ * transaction.sign(keypair1, keypair2)
292
+ * WebAuth.gatherTxSigners(transaction, [keypair1.publicKey(), keypair2.publicKey()])
293
+ */
294
+ export declare function gatherTxSigners(transaction: FeeBumpTransaction | Transaction, signers: string[]): string[];
@@ -0,0 +1,332 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.buildChallengeTx = buildChallengeTx;
7
+ exports.gatherTxSigners = gatherTxSigners;
8
+ exports.readChallengeTx = readChallengeTx;
9
+ exports.verifyChallengeTxSigners = verifyChallengeTxSigners;
10
+ exports.verifyChallengeTxThreshold = verifyChallengeTxThreshold;
11
+ exports.verifyTxSignedBy = verifyTxSignedBy;
12
+ var _randombytes = _interopRequireDefault(require("randombytes"));
13
+ var _stellarBase = require("@stellar/stellar-base");
14
+ var _utils = require("../utils");
15
+ var _errors = require("./errors");
16
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
17
+ function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
18
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
19
+ function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
20
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
21
+ 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); }
22
+ function _toArray(r) { return _arrayWithHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableRest(); }
23
+ 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."); }
24
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
25
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
26
+ function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
27
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
28
+ function buildChallengeTx(serverKeypair, clientAccountID, homeDomain) {
29
+ var timeout = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 300;
30
+ var networkPassphrase = arguments.length > 4 ? arguments[4] : undefined;
31
+ var webAuthDomain = arguments.length > 5 ? arguments[5] : undefined;
32
+ var memo = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : null;
33
+ var clientDomain = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : null;
34
+ var clientSigningKey = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : null;
35
+ if (clientAccountID.startsWith("M") && memo) {
36
+ throw Error("memo cannot be used if clientAccountID is a muxed account");
37
+ }
38
+ var account = new _stellarBase.Account(serverKeypair.publicKey(), "-1");
39
+ var now = Math.floor(Date.now() / 1000);
40
+ var value = (0, _randombytes.default)(48).toString("base64");
41
+ var builder = new _stellarBase.TransactionBuilder(account, {
42
+ fee: _stellarBase.BASE_FEE,
43
+ networkPassphrase: networkPassphrase,
44
+ timebounds: {
45
+ minTime: now,
46
+ maxTime: now + timeout
47
+ }
48
+ }).addOperation(_stellarBase.Operation.manageData({
49
+ name: "".concat(homeDomain, " auth"),
50
+ value: value,
51
+ source: clientAccountID
52
+ })).addOperation(_stellarBase.Operation.manageData({
53
+ name: "web_auth_domain",
54
+ value: webAuthDomain,
55
+ source: account.accountId()
56
+ }));
57
+ if (clientDomain) {
58
+ if (!clientSigningKey) {
59
+ throw Error("clientSigningKey is required if clientDomain is provided");
60
+ }
61
+ builder.addOperation(_stellarBase.Operation.manageData({
62
+ name: "client_domain",
63
+ value: clientDomain,
64
+ source: clientSigningKey
65
+ }));
66
+ }
67
+ if (memo) {
68
+ builder.addMemo(_stellarBase.Memo.id(memo));
69
+ }
70
+ var transaction = builder.build();
71
+ transaction.sign(serverKeypair);
72
+ return transaction.toEnvelope().toXDR("base64").toString();
73
+ }
74
+ function readChallengeTx(challengeTx, serverAccountID, networkPassphrase, homeDomains, webAuthDomain) {
75
+ var _transaction$timeBoun;
76
+ if (serverAccountID.startsWith("M")) {
77
+ throw Error("Invalid serverAccountID: multiplexed accounts are not supported.");
78
+ }
79
+ var transaction;
80
+ try {
81
+ transaction = new _stellarBase.Transaction(challengeTx, networkPassphrase);
82
+ } catch (_unused) {
83
+ try {
84
+ transaction = new _stellarBase.FeeBumpTransaction(challengeTx, networkPassphrase);
85
+ } catch (_unused2) {
86
+ throw new _errors.InvalidChallengeError("Invalid challenge: unable to deserialize challengeTx transaction string");
87
+ }
88
+ throw new _errors.InvalidChallengeError("Invalid challenge: expected a Transaction but received a FeeBumpTransaction");
89
+ }
90
+ var sequence = Number.parseInt(transaction.sequence, 10);
91
+ if (sequence !== 0) {
92
+ throw new _errors.InvalidChallengeError("The transaction sequence number should be zero");
93
+ }
94
+ if (transaction.source !== serverAccountID) {
95
+ throw new _errors.InvalidChallengeError("The transaction source account is not equal to the server's account");
96
+ }
97
+ if (transaction.operations.length < 1) {
98
+ throw new _errors.InvalidChallengeError("The transaction should contain at least one operation");
99
+ }
100
+ var _transaction$operatio = _toArray(transaction.operations),
101
+ operation = _transaction$operatio[0],
102
+ subsequentOperations = _transaction$operatio.slice(1);
103
+ if (!operation.source) {
104
+ throw new _errors.InvalidChallengeError("The transaction's operation should contain a source account");
105
+ }
106
+ var clientAccountID = operation.source;
107
+ var memo = null;
108
+ if (transaction.memo.type !== _stellarBase.MemoNone) {
109
+ if (clientAccountID.startsWith("M")) {
110
+ throw new _errors.InvalidChallengeError("The transaction has a memo but the client account ID is a muxed account");
111
+ }
112
+ if (transaction.memo.type !== _stellarBase.MemoID) {
113
+ throw new _errors.InvalidChallengeError("The transaction's memo must be of type `id`");
114
+ }
115
+ memo = transaction.memo.value;
116
+ }
117
+ if (operation.type !== "manageData") {
118
+ throw new _errors.InvalidChallengeError("The transaction's operation type should be 'manageData'");
119
+ }
120
+ if (transaction.timeBounds && Number.parseInt((_transaction$timeBoun = transaction.timeBounds) === null || _transaction$timeBoun === void 0 ? void 0 : _transaction$timeBoun.maxTime, 10) === _stellarBase.TimeoutInfinite) {
121
+ throw new _errors.InvalidChallengeError("The transaction requires non-infinite timebounds");
122
+ }
123
+ if (!_utils.Utils.validateTimebounds(transaction, 60 * 5)) {
124
+ throw new _errors.InvalidChallengeError("The transaction has expired");
125
+ }
126
+ if (operation.value === undefined) {
127
+ throw new _errors.InvalidChallengeError("The transaction's operation values should not be null");
128
+ }
129
+ if (!operation.value) {
130
+ throw new _errors.InvalidChallengeError("The transaction's operation value should not be null");
131
+ }
132
+ if (Buffer.from(operation.value.toString(), "base64").length !== 48) {
133
+ throw new _errors.InvalidChallengeError("The transaction's operation value should be a 64 bytes base64 random string");
134
+ }
135
+ if (!homeDomains) {
136
+ throw new _errors.InvalidChallengeError("Invalid homeDomains: a home domain must be provided for verification");
137
+ }
138
+ var matchedHomeDomain;
139
+ if (typeof homeDomains === "string") {
140
+ if ("".concat(homeDomains, " auth") === operation.name) {
141
+ matchedHomeDomain = homeDomains;
142
+ }
143
+ } else if (Array.isArray(homeDomains)) {
144
+ matchedHomeDomain = homeDomains.find(function (domain) {
145
+ return "".concat(domain, " auth") === operation.name;
146
+ });
147
+ } else {
148
+ throw new _errors.InvalidChallengeError("Invalid homeDomains: homeDomains type is ".concat(_typeof(homeDomains), " but should be a string or an array"));
149
+ }
150
+ if (!matchedHomeDomain) {
151
+ throw new _errors.InvalidChallengeError("Invalid homeDomains: the transaction's operation key name does not match the expected home domain");
152
+ }
153
+ var _iterator = _createForOfIteratorHelper(subsequentOperations),
154
+ _step;
155
+ try {
156
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
157
+ var op = _step.value;
158
+ if (op.type !== "manageData") {
159
+ throw new _errors.InvalidChallengeError("The transaction has operations that are not of type 'manageData'");
160
+ }
161
+ if (op.source !== serverAccountID && op.name !== "client_domain") {
162
+ throw new _errors.InvalidChallengeError("The transaction has operations that are unrecognized");
163
+ }
164
+ if (op.name === "web_auth_domain") {
165
+ if (op.value === undefined) {
166
+ throw new _errors.InvalidChallengeError("'web_auth_domain' operation value should not be null");
167
+ }
168
+ if (op.value.compare(Buffer.from(webAuthDomain))) {
169
+ throw new _errors.InvalidChallengeError("'web_auth_domain' operation value does not match ".concat(webAuthDomain));
170
+ }
171
+ }
172
+ }
173
+ } catch (err) {
174
+ _iterator.e(err);
175
+ } finally {
176
+ _iterator.f();
177
+ }
178
+ if (!verifyTxSignedBy(transaction, serverAccountID)) {
179
+ throw new _errors.InvalidChallengeError("Transaction not signed by server: '".concat(serverAccountID, "'"));
180
+ }
181
+ return {
182
+ tx: transaction,
183
+ clientAccountID: clientAccountID,
184
+ matchedHomeDomain: matchedHomeDomain,
185
+ memo: memo
186
+ };
187
+ }
188
+ function verifyChallengeTxThreshold(challengeTx, serverAccountID, networkPassphrase, threshold, signerSummary, homeDomains, webAuthDomain) {
189
+ var signers = signerSummary.map(function (signer) {
190
+ return signer.key;
191
+ });
192
+ var signersFound = verifyChallengeTxSigners(challengeTx, serverAccountID, networkPassphrase, signers, homeDomains, webAuthDomain);
193
+ var weight = 0;
194
+ var _loop = function _loop() {
195
+ var _signerSummary$find;
196
+ var signer = _signersFound[_i];
197
+ var sigWeight = ((_signerSummary$find = signerSummary.find(function (s) {
198
+ return s.key === signer;
199
+ })) === null || _signerSummary$find === void 0 ? void 0 : _signerSummary$find.weight) || 0;
200
+ weight += sigWeight;
201
+ };
202
+ for (var _i = 0, _signersFound = signersFound; _i < _signersFound.length; _i++) {
203
+ _loop();
204
+ }
205
+ if (weight < threshold) {
206
+ throw new _errors.InvalidChallengeError("signers with weight ".concat(weight, " do not meet threshold ").concat(threshold, "\""));
207
+ }
208
+ return signersFound;
209
+ }
210
+ function verifyChallengeTxSigners(challengeTx, serverAccountID, networkPassphrase, signers, homeDomains, webAuthDomain) {
211
+ var _readChallengeTx = readChallengeTx(challengeTx, serverAccountID, networkPassphrase, homeDomains, webAuthDomain),
212
+ tx = _readChallengeTx.tx;
213
+ var serverKP;
214
+ try {
215
+ serverKP = _stellarBase.Keypair.fromPublicKey(serverAccountID);
216
+ } catch (err) {
217
+ throw new Error("Couldn't infer keypair from the provided 'serverAccountID': ".concat(err.message));
218
+ }
219
+ var clientSigners = new Set();
220
+ var _iterator2 = _createForOfIteratorHelper(signers),
221
+ _step2;
222
+ try {
223
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
224
+ var _signer = _step2.value;
225
+ if (_signer === serverKP.publicKey()) {
226
+ continue;
227
+ }
228
+ if (_signer.charAt(0) !== "G") {
229
+ continue;
230
+ }
231
+ clientSigners.add(_signer);
232
+ }
233
+ } catch (err) {
234
+ _iterator2.e(err);
235
+ } finally {
236
+ _iterator2.f();
237
+ }
238
+ if (clientSigners.size === 0) {
239
+ throw new _errors.InvalidChallengeError("No verifiable client signers provided, at least one G... address must be provided");
240
+ }
241
+ var clientSigningKey;
242
+ var _iterator3 = _createForOfIteratorHelper(tx.operations),
243
+ _step3;
244
+ try {
245
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
246
+ var op = _step3.value;
247
+ if (op.type === "manageData" && op.name === "client_domain") {
248
+ if (clientSigningKey) {
249
+ throw new _errors.InvalidChallengeError("Found more than one client_domain operation");
250
+ }
251
+ clientSigningKey = op.source;
252
+ }
253
+ }
254
+ } catch (err) {
255
+ _iterator3.e(err);
256
+ } finally {
257
+ _iterator3.f();
258
+ }
259
+ var allSigners = [serverKP.publicKey()].concat(_toConsumableArray(Array.from(clientSigners)));
260
+ if (clientSigningKey) {
261
+ allSigners.push(clientSigningKey);
262
+ }
263
+ var signersFound = gatherTxSigners(tx, allSigners);
264
+ var serverSignatureFound = false;
265
+ var clientSigningKeySignatureFound = false;
266
+ for (var _i2 = 0, _signersFound2 = signersFound; _i2 < _signersFound2.length; _i2++) {
267
+ var signer = _signersFound2[_i2];
268
+ if (signer === serverKP.publicKey()) {
269
+ serverSignatureFound = true;
270
+ }
271
+ if (signer === clientSigningKey) {
272
+ clientSigningKeySignatureFound = true;
273
+ }
274
+ }
275
+ if (!serverSignatureFound) {
276
+ throw new _errors.InvalidChallengeError("Transaction not signed by server: '".concat(serverKP.publicKey(), "'"));
277
+ }
278
+ if (clientSigningKey && !clientSigningKeySignatureFound) {
279
+ throw new _errors.InvalidChallengeError("Transaction not signed by the source account of the 'client_domain' " + "ManageData operation");
280
+ }
281
+ if (signersFound.length === 1) {
282
+ throw new _errors.InvalidChallengeError("None of the given signers match the transaction signatures");
283
+ }
284
+ if (signersFound.length !== tx.signatures.length) {
285
+ throw new _errors.InvalidChallengeError("Transaction has unrecognized signatures");
286
+ }
287
+ signersFound.splice(signersFound.indexOf(serverKP.publicKey()), 1);
288
+ if (clientSigningKey) {
289
+ signersFound.splice(signersFound.indexOf(clientSigningKey), 1);
290
+ }
291
+ return signersFound;
292
+ }
293
+ function verifyTxSignedBy(transaction, accountID) {
294
+ return gatherTxSigners(transaction, [accountID]).length !== 0;
295
+ }
296
+ function gatherTxSigners(transaction, signers) {
297
+ var hashedSignatureBase = transaction.hash();
298
+ var txSignatures = _toConsumableArray(transaction.signatures);
299
+ var signersFound = new Set();
300
+ var _iterator4 = _createForOfIteratorHelper(signers),
301
+ _step4;
302
+ try {
303
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
304
+ var signer = _step4.value;
305
+ if (txSignatures.length === 0) {
306
+ break;
307
+ }
308
+ var keypair = void 0;
309
+ try {
310
+ keypair = _stellarBase.Keypair.fromPublicKey(signer);
311
+ } catch (err) {
312
+ throw new _errors.InvalidChallengeError("Signer is not a valid address: ".concat(err.message));
313
+ }
314
+ for (var i = 0; i < txSignatures.length; i++) {
315
+ var decSig = txSignatures[i];
316
+ if (!decSig.hint().equals(keypair.signatureHint())) {
317
+ continue;
318
+ }
319
+ if (keypair.verify(hashedSignatureBase, decSig.signature())) {
320
+ signersFound.add(signer);
321
+ txSignatures.splice(i, 1);
322
+ break;
323
+ }
324
+ }
325
+ }
326
+ } catch (err) {
327
+ _iterator4.e(err);
328
+ } finally {
329
+ _iterator4.f();
330
+ }
331
+ return Array.from(signersFound);
332
+ }