@stellar/typescript-wallet-sdk 1.1.0-alpha.2 → 1.1.1

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 (90) hide show
  1. package/.husky/pre-commit +4 -0
  2. package/docs/WalletGuide.md +5 -0
  3. package/lib/bundle.js +15519 -18948
  4. package/lib/bundle.js.map +1 -1
  5. package/lib/bundle_browser.js +89069 -0
  6. package/lib/bundle_browser.js.map +1 -0
  7. package/lib/index.d.ts +26 -7
  8. package/lib/walletSdk/Anchor/Sep24.d.ts +48 -0
  9. package/lib/walletSdk/Anchor/index.d.ts +24 -0
  10. package/lib/walletSdk/Asset/index.d.ts +27 -0
  11. package/lib/walletSdk/Auth/WalletSigner.d.ts +7 -0
  12. package/lib/walletSdk/Auth/index.d.ts +21 -0
  13. package/lib/walletSdk/Exceptions/index.d.ts +56 -0
  14. package/lib/walletSdk/{horizon → Horizon}/Account.d.ts +3 -6
  15. package/lib/walletSdk/Horizon/AccountService.d.ts +53 -0
  16. package/lib/walletSdk/Horizon/Stellar.d.ts +16 -0
  17. package/lib/walletSdk/Horizon/Transaction/TransactionBuilder.d.ts +20 -0
  18. package/lib/walletSdk/Horizon/index.d.ts +4 -0
  19. package/lib/walletSdk/Recovery/index.d.ts +18 -0
  20. package/lib/walletSdk/{Watcher/Types.d.ts → Types/anchor.d.ts} +89 -6
  21. package/lib/walletSdk/Types/auth.d.ts +35 -0
  22. package/lib/walletSdk/Types/horizon.d.ts +34 -0
  23. package/lib/walletSdk/Types/index.d.ts +30 -0
  24. package/lib/walletSdk/Types/sep24.d.ts +30 -0
  25. package/lib/walletSdk/{toml/index.d.ts → Types/utils.d.ts} +4 -5
  26. package/lib/walletSdk/Types/watcher.d.ts +30 -0
  27. package/lib/walletSdk/{util → Utils}/camelToSnakeCase.d.ts +1 -1
  28. package/lib/walletSdk/Utils/getResultCode.d.ts +1 -0
  29. package/lib/walletSdk/Utils/index.d.ts +3 -0
  30. package/lib/walletSdk/Utils/toml.d.ts +3 -0
  31. package/lib/walletSdk/Utils/url.d.ts +1 -0
  32. package/lib/walletSdk/Watcher/index.d.ts +21 -42
  33. package/lib/walletSdk/index.d.ts +17 -15
  34. package/package.json +12 -2
  35. package/prettier.config.js +1 -0
  36. package/src/index.ts +39 -17
  37. package/src/walletSdk/Anchor/Sep24.ts +267 -0
  38. package/src/walletSdk/Anchor/index.ts +32 -201
  39. package/src/walletSdk/Asset/index.ts +58 -0
  40. package/src/walletSdk/Auth/WalletSigner.ts +23 -12
  41. package/src/walletSdk/Auth/index.ts +96 -49
  42. package/src/walletSdk/Exceptions/index.ts +148 -0
  43. package/src/walletSdk/{horizon → Horizon}/Account.ts +7 -9
  44. package/src/walletSdk/Horizon/AccountService.ts +96 -0
  45. package/src/walletSdk/Horizon/Stellar.ts +164 -0
  46. package/src/walletSdk/Horizon/Transaction/TransactionBuilder.ts +140 -0
  47. package/src/walletSdk/Horizon/index.ts +4 -0
  48. package/src/walletSdk/Recovery/index.ts +32 -0
  49. package/src/walletSdk/{Watcher/Types.ts → Types/anchor.ts} +114 -21
  50. package/src/walletSdk/Types/auth.ts +43 -0
  51. package/src/walletSdk/Types/horizon.ts +40 -0
  52. package/src/walletSdk/Types/index.ts +39 -0
  53. package/src/walletSdk/Types/sep24.ts +41 -0
  54. package/src/walletSdk/Types/utils.ts +77 -0
  55. package/src/walletSdk/Types/watcher.ts +34 -0
  56. package/src/walletSdk/Utils/camelToSnakeCase.ts +16 -0
  57. package/src/walletSdk/Utils/getResultCode.ts +5 -0
  58. package/src/walletSdk/Utils/index.ts +3 -0
  59. package/src/walletSdk/Utils/toml.ts +103 -0
  60. package/src/walletSdk/{util → Utils}/url.ts +1 -1
  61. package/src/walletSdk/Watcher/index.ts +137 -128
  62. package/src/walletSdk/index.ts +72 -55
  63. package/test/account.test.ts +19 -15
  64. package/test/accountService.test.ts +109 -0
  65. package/test/fixtures/TransactionsResponse.ts +144 -127
  66. package/test/stellar.test.ts +247 -0
  67. package/test/wallet.test.ts +196 -214
  68. package/tsconfig.json +3 -7
  69. package/webpack.config.js +7 -4
  70. package/lib/walletSdk/anchor/Types.d.ts +0 -75
  71. package/lib/walletSdk/anchor/index.d.ts +0 -87
  72. package/lib/walletSdk/auth/WalletSigner.d.ts +0 -6
  73. package/lib/walletSdk/auth/index.d.ts +0 -12
  74. package/lib/walletSdk/exception/index.d.ts +0 -21
  75. package/lib/walletSdk/horizon/AccountService.d.ts +0 -8
  76. package/lib/walletSdk/horizon/Stellar.d.ts +0 -7
  77. package/lib/walletSdk/horizon/constants.d.ts +0 -4
  78. package/lib/walletSdk/interactive/index.d.ts +0 -38
  79. package/lib/walletSdk/recovery/Recovery.d.ts +0 -3
  80. package/lib/walletSdk/util/sleep.d.ts +0 -1
  81. package/lib/walletSdk/util/url.d.ts +0 -1
  82. package/src/walletSdk/Anchor/Types.ts +0 -83
  83. package/src/walletSdk/exception/index.ts +0 -48
  84. package/src/walletSdk/horizon/AccountService.ts +0 -19
  85. package/src/walletSdk/horizon/Stellar.ts +0 -14
  86. package/src/walletSdk/horizon/constants.ts +0 -4
  87. package/src/walletSdk/interactive/index.ts +0 -105
  88. package/src/walletSdk/recovery/Recovery.ts +0 -6
  89. package/src/walletSdk/toml/index.ts +0 -179
  90. package/src/walletSdk/util/camelToSnakeCase.ts +0 -13
@@ -1,7 +1,13 @@
1
1
  import isEqual from "lodash/isEqual";
2
2
 
3
- import { Anchor } from "walletSdk/Anchor";
4
- import { TransactionStatus, WatcherResponse } from "./Types";
3
+ import { Anchor } from "../Anchor";
4
+ import {
5
+ AnchorTransaction,
6
+ TransactionStatus,
7
+ WatchTransactionParams,
8
+ WatchTransactionsParams,
9
+ WatcherResponse,
10
+ } from "../Types";
5
11
 
6
12
  interface WatchRegistryAsset {
7
13
  [id: string]: boolean;
@@ -30,7 +36,7 @@ export class Watcher {
30
36
  private _oneTransactionWatcher: {
31
37
  [assetCode: string]: {
32
38
  [id: string]: ReturnType<typeof setTimeout>;
33
- }
39
+ };
34
40
  };
35
41
  private _allTransactionsWatcher?: ReturnType<typeof setTimeout>;
36
42
  private _watchOneTransactionRegistry: WatchOneTransactionRegistry;
@@ -50,41 +56,42 @@ export class Watcher {
50
56
  }
51
57
 
52
58
  /**
53
- * Watch all transactions returned from a transfer server. When new or
54
- * updated transactions come in, run an `onMessage` callback.
55
- *
56
- * On initial load, it'll return ALL pending transactions via onMessage.
57
- * Subsequent messages will be any one of these events:
58
- * - Any new transaction appears
59
- * - Any of the initial pending transactions change any state
60
- *
61
- * You may also provide an array of transaction ids, `watchlist`, and this
62
- * watcher will always react to transactions whose ids are in the watchlist.
63
- */
64
- watchAllTransactions(params: {
65
- authToken: string;
66
- assetCode: string;
67
- onMessage: (transaction) => void;
68
- onError: (error) => void; // TOOD - add :Transaction | Error type
69
- watchlist?: string[];
70
- timeout?: number;
71
- isRetry?: boolean;
72
- noOlderThan?: string;
73
- kind?: string;
74
- lang?: string;
75
- }): WatcherResponse {
76
- const {
59
+ * Watch all transactions returned from a transfer server. When new or
60
+ * updated transactions come in, run an `onMessage` callback.
61
+ *
62
+ * On initial load, it'll return ALL pending transactions via onMessage.
63
+ * Subsequent messages will be any one of these events:
64
+ * - Any new transaction appears
65
+ * - Any of the initial pending transactions change any state
66
+ *
67
+ * You may also provide an array of transaction ids, `watchlist`, and this
68
+ * watcher will always react to transactions whose ids are in the watchlist.
69
+ */
70
+ watchAllTransactions({
71
+ authToken,
72
+ assetCode,
73
+ onMessage,
74
+ onError,
75
+ watchlist = [],
76
+ timeout = 5000,
77
+ isRetry = false,
78
+ lang = this.anchor.language,
79
+ kind,
80
+ noOlderThan,
81
+ }: WatchTransactionsParams): WatcherResponse {
82
+ const allParams = {
77
83
  authToken,
78
84
  assetCode,
79
85
  onMessage,
80
86
  onError,
81
- watchlist = [],
82
- timeout = 5000,
83
- isRetry = false,
84
- lang = this.anchor.language,
85
- ...otherParams
86
- } = params;
87
-
87
+ watchlist,
88
+ timeout,
89
+ isRetry,
90
+ lang,
91
+ kind,
92
+ noOlderThan,
93
+ };
94
+
88
95
  // make an object map out of watchlist
89
96
  const watchlistMap = watchlist.reduce(
90
97
  (memo, id: string) => ({ ...memo, [id]: true }),
@@ -93,10 +100,10 @@ export class Watcher {
93
100
 
94
101
  // make sure to initiate registries for the given asset code
95
102
  // to prevent 'Cannot read properties of undefined' errors
96
- if(!this._transactionsRegistry[assetCode]) {
103
+ if (!this._transactionsRegistry[assetCode]) {
97
104
  this._transactionsRegistry[assetCode] = {};
98
105
  }
99
- if(!this._transactionsIgnoredRegistry[assetCode]) {
106
+ if (!this._transactionsIgnoredRegistry[assetCode]) {
100
107
  this._transactionsIgnoredRegistry[assetCode] = {};
101
108
  }
102
109
 
@@ -105,74 +112,76 @@ export class Watcher {
105
112
  this._watchAllTransactionsRegistry[assetCode] = true;
106
113
  }
107
114
 
108
- this.anchor.getTransactionsForAsset({ authToken, assetCode, lang, ...otherParams })
109
- .then((transactions: any[]) => { // TOOD - replace with Transaction[] type
115
+ this.anchor
116
+ .sep24()
117
+ .getTransactionsForAsset({
118
+ authToken,
119
+ assetCode,
120
+ lang,
121
+ kind,
122
+ noOlderThan,
123
+ })
124
+ .then((transactions: AnchorTransaction[]) => {
110
125
  // make sure we're still watching
111
126
  if (!this._watchAllTransactionsRegistry[assetCode]) {
112
127
  return;
113
128
  }
114
129
 
115
130
  try {
116
- const newTransactions = transactions.filter(
117
- (transaction) => {
118
- const isInProgress =
119
- transaction.status.indexOf("pending") === 0 ||
120
- transaction.status === TransactionStatus.incomplete;
121
- const registeredTransaction = this._transactionsRegistry[
122
- assetCode
123
- ][transaction.id];
124
-
125
- // if this is the first watch, only keep the pending ones
126
- if (!isRetry) {
127
- // always show transactions on the watchlist
128
- if (watchlistMap[transaction.id]) {
129
- return true;
130
- }
131
-
132
- // if we're not in progress, then save this in an ignore reg
133
- if (!isInProgress) {
134
- this._transactionsIgnoredRegistry[assetCode][
135
- transaction.id
136
- ] = transaction;
137
- }
138
-
139
- return isInProgress;
131
+ const newTransactions = transactions.filter((transaction) => {
132
+ const isInProgress =
133
+ transaction.status.indexOf("pending") === 0 ||
134
+ transaction.status === TransactionStatus.incomplete;
135
+ const registeredTransaction =
136
+ this._transactionsRegistry[assetCode][transaction.id];
137
+
138
+ // if this is the first watch, only keep the pending ones
139
+ if (!isRetry) {
140
+ // always show transactions on the watchlist
141
+ if (watchlistMap[transaction.id]) {
142
+ return true;
140
143
  }
141
144
 
142
- // if we've had the transaction before, only report updates
143
- if (registeredTransaction) {
144
- return !isEqual(registeredTransaction, transaction);
145
+ // if we're not in progress, then save this in an ignore reg
146
+ if (!isInProgress) {
147
+ this._transactionsIgnoredRegistry[assetCode][transaction.id] =
148
+ transaction;
145
149
  }
146
150
 
147
- // if it's NOT a registered transaction, and it's not the first
148
- // roll, maybe it's a new transaction that achieved a final
149
- // status immediately so register that!
150
- if (
151
- [
152
- TransactionStatus.completed,
153
- TransactionStatus.refunded,
154
- TransactionStatus.expired,
155
- TransactionStatus.error,
156
- ].includes(transaction.status) &&
157
- isRetry &&
158
- !this._transactionsIgnoredRegistry[assetCode][transaction.id]
159
- ) {
160
- return true;
161
- }
151
+ return isInProgress;
152
+ }
162
153
 
163
- // always use in progress transactions
164
- if (isInProgress) {
165
- return true;
166
- }
154
+ // if we've had the transaction before, only report updates
155
+ if (registeredTransaction) {
156
+ return !isEqual(registeredTransaction, transaction);
157
+ }
158
+
159
+ // if it's NOT a registered transaction, and it's not the first
160
+ // roll, maybe it's a new transaction that achieved a final
161
+ // status immediately so register that!
162
+ if (
163
+ [
164
+ TransactionStatus.completed,
165
+ TransactionStatus.refunded,
166
+ TransactionStatus.expired,
167
+ TransactionStatus.error,
168
+ ].includes(transaction.status) &&
169
+ isRetry &&
170
+ !this._transactionsIgnoredRegistry[assetCode][transaction.id]
171
+ ) {
172
+ return true;
173
+ }
174
+
175
+ // always use in progress transactions
176
+ if (isInProgress) {
177
+ return true;
178
+ }
167
179
 
168
- return false;
169
- },
170
- );
180
+ return false;
181
+ });
171
182
 
172
183
  newTransactions.forEach((transaction) => {
173
- this._transactionsRegistry[assetCode][
174
- transaction.id
175
- ] = transaction;
184
+ this._transactionsRegistry[assetCode][transaction.id] = transaction;
176
185
 
177
186
  if (transaction.status === TransactionStatus.error) {
178
187
  onError(transaction);
@@ -191,9 +200,9 @@ export class Watcher {
191
200
  }
192
201
  this._allTransactionsWatcher = setTimeout(() => {
193
202
  this.watchAllTransactions({
194
- ...params,
203
+ ...allParams,
195
204
  isRetry: true,
196
- });
205
+ });
197
206
  }, timeout);
198
207
  })
199
208
  .catch((e) => {
@@ -212,9 +221,9 @@ export class Watcher {
212
221
  }
213
222
 
214
223
  this.watchAllTransactions({
215
- ...params,
224
+ ...allParams,
216
225
  isRetry: true,
217
- });
226
+ });
218
227
  },
219
228
  stop: () => {
220
229
  if (this._allTransactionsWatcher) {
@@ -228,41 +237,41 @@ export class Watcher {
228
237
  }
229
238
 
230
239
  /**
231
- * Watch a transaction until it stops pending. Takes three callbacks:
232
- * * onMessage - When the transaction comes back as pending_ or incomplete.
233
- * * onSuccess - When the transaction comes back as completed / refunded / expired.
234
- * * onError - When there's a runtime error, or the transaction comes back as
235
- * no_market / too_small / too_large / error.
236
- */
237
- watchOneTransaction(params: {
238
- authToken: string;
239
- assetCode: string;
240
- id: string;
241
- onMessage: (transaction) => void;
242
- onSuccess: (transaction) => void;
243
- onError: (error) => void; // TOOD - add :Transaction | Error type
244
- timeout?: number;
245
- isRetry?: boolean;
246
- lang?: string;
247
- }): WatcherResponse {
248
- const {
240
+ * Watch a transaction until it stops pending. Takes three callbacks:
241
+ * * onMessage - When the transaction comes back as pending_ or incomplete.
242
+ * * onSuccess - When the transaction comes back as completed / refunded / expired.
243
+ * * onError - When there's a runtime error, or the transaction comes back as
244
+ * no_market / too_small / too_large / error.
245
+ */
246
+ watchOneTransaction({
247
+ authToken,
248
+ assetCode,
249
+ id,
250
+ onMessage,
251
+ onSuccess,
252
+ onError,
253
+ timeout = 5000,
254
+ isRetry = false,
255
+ lang = this.anchor.language,
256
+ }: WatchTransactionParams): WatcherResponse {
257
+ const allParams = {
249
258
  authToken,
250
259
  assetCode,
251
260
  id,
252
261
  onMessage,
253
262
  onSuccess,
254
263
  onError,
255
- timeout = 5000,
256
- isRetry = false,
257
- lang = this.anchor.language,
258
- } = params;
264
+ timeout,
265
+ isRetry,
266
+ lang,
267
+ };
259
268
 
260
269
  // make sure to initiate registries for the given asset code
261
270
  // to prevent 'Cannot read properties of undefined' errors
262
- if(!this._transactionsRegistry[assetCode]) {
271
+ if (!this._transactionsRegistry[assetCode]) {
263
272
  this._transactionsRegistry[assetCode] = {};
264
273
  }
265
- if(!this._oneTransactionWatcher[assetCode]) {
274
+ if (!this._oneTransactionWatcher[assetCode]) {
266
275
  this._oneTransactionWatcher[assetCode] = {};
267
276
  }
268
277
 
@@ -275,16 +284,17 @@ export class Watcher {
275
284
  }
276
285
 
277
286
  // do this all asynchronously (since this func needs to return a cancel fun)
278
- this.anchor.getTransactionBy({ authToken, id, lang })
279
- .then((transaction) => {
287
+ this.anchor
288
+ .sep24()
289
+ .getTransactionBy({ authToken, id, lang })
290
+ .then((transaction: AnchorTransaction) => {
280
291
  // make sure we're still watching
281
292
  if (!this._watchOneTransactionRegistry[assetCode]?.[id]) {
282
293
  return;
283
294
  }
284
295
 
285
- const registeredTransaction = this._transactionsRegistry[assetCode][
286
- transaction.id
287
- ];
296
+ const registeredTransaction =
297
+ this._transactionsRegistry[assetCode][transaction.id];
288
298
 
289
299
  // if we've had the transaction before, only report if there is a change
290
300
  if (
@@ -306,16 +316,16 @@ export class Watcher {
306
316
 
307
317
  this._oneTransactionWatcher[assetCode][id] = setTimeout(() => {
308
318
  this.watchOneTransaction({
309
- ...params,
319
+ ...allParams,
310
320
  isRetry: true,
311
321
  });
312
322
  }, timeout);
313
323
  onMessage(transaction);
314
324
  } else if (
315
325
  [
316
- TransactionStatus.completed,
326
+ TransactionStatus.completed,
317
327
  TransactionStatus.refunded,
318
- TransactionStatus.expired
328
+ TransactionStatus.expired,
319
329
  ].includes(transaction.status)
320
330
  ) {
321
331
  onSuccess(transaction);
@@ -339,7 +349,7 @@ export class Watcher {
339
349
  }
340
350
 
341
351
  this.watchOneTransaction({
342
- ...params,
352
+ ...allParams,
343
353
  isRetry: true,
344
354
  });
345
355
  },
@@ -351,5 +361,4 @@ export class Watcher {
351
361
  },
352
362
  };
353
363
  }
354
-
355
364
  }
@@ -1,12 +1,19 @@
1
- import StellarSdk, { Networks, Server } from "stellar-sdk";
2
1
  import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
2
+ import { Networks, Server } from "stellar-sdk";
3
3
 
4
4
  import { Anchor } from "./Anchor";
5
- import { WalletSigner, DefaultSigner } from "./Auth/WalletSigner";
6
- import { Stellar } from "./horizon/Stellar";
7
- import { NETWORK_URLS } from "./horizon/constants";
8
- import { Recovery } from "./recovery/Recovery";
9
- import { getUrlDomain } from "./util/url";
5
+ import { DefaultSigner, WalletSigner } from "./Auth";
6
+ import { Stellar } from "./Horizon";
7
+ import { Recovery } from "./Recovery";
8
+ import {
9
+ ConfigParams,
10
+ StellarConfigurationParams,
11
+ WalletAnchor,
12
+ WalletParams,
13
+ WalletRecovery,
14
+ NETWORK_URLS,
15
+ } from "./Types";
16
+ import { getUrlDomain } from "./Utils";
10
17
 
11
18
  /* tslint:disable-next-line:no-var-requires */
12
19
  const version = require("../../package.json").version;
@@ -15,49 +22,40 @@ const walletHeaders = {
15
22
  "X-Client-Version": version,
16
23
  };
17
24
 
18
- export class Config {
19
- app: ApplicationConfiguration;
20
- stellar: StellarConfiguration;
21
- constructor(stellarCfg, appCfg) {
22
- this.stellar = stellarCfg;
23
- this.app = appCfg;
24
- }
25
- }
26
-
27
25
  export class Wallet {
28
26
  private cfg: Config;
29
27
  private language: string;
30
28
 
31
29
  static TestNet = (): Wallet => {
32
- return new Wallet(StellarConfiguration.TestNet());
30
+ return new Wallet({
31
+ stellarConfiguration: StellarConfiguration.TestNet(),
32
+ });
33
33
  };
34
34
 
35
35
  static MainNet = (): Wallet => {
36
- return new Wallet(StellarConfiguration.MainNet());
36
+ return new Wallet({
37
+ stellarConfiguration: StellarConfiguration.MainNet(),
38
+ });
37
39
  };
38
40
 
39
- constructor(
40
- stellarConfiguration: StellarConfiguration,
41
- applicationConfiguration: ApplicationConfiguration = new ApplicationConfiguration(),
41
+ constructor({
42
+ stellarConfiguration,
43
+ applicationConfiguration = new ApplicationConfiguration(),
42
44
  // Defaults wallet language to "en", this will reflect in all Anchor API calls
43
- language: string = "en"
44
- ) {
45
- this.cfg = new Config(stellarConfiguration, applicationConfiguration);
45
+ language = "en",
46
+ }: WalletParams) {
47
+ this.cfg = new Config({ stellarConfiguration, applicationConfiguration });
46
48
  this.language = language;
47
49
  }
48
50
 
49
- anchor(
50
- homeDomain: string,
51
- httpClientConfig: AxiosRequestConfig = {},
52
- language: string = this.language
53
- ) {
51
+ anchor({ homeDomain, language = this.language }: WalletAnchor): Anchor {
54
52
  const url =
55
53
  homeDomain.indexOf("://") !== -1 ? homeDomain : `https://${homeDomain}`;
56
54
 
57
55
  return new Anchor({
58
56
  cfg: this.cfg,
59
57
  homeDomain: getUrlDomain(url),
60
- httpClient: this.getClient(httpClientConfig),
58
+ httpClient: this.cfg.app.defaultClient,
61
59
  language,
62
60
  });
63
61
  }
@@ -66,23 +64,26 @@ export class Wallet {
66
64
  return new Stellar(this.cfg);
67
65
  }
68
66
 
69
- recover(servers, httpClientConfig: AxiosRequestConfig = {}) {
70
- return new Recovery(
71
- this.cfg,
72
- this.stellar(),
73
- this.getClient(httpClientConfig),
74
- servers
75
- );
67
+ recovery({ servers }: WalletRecovery): Recovery {
68
+ return new Recovery({
69
+ cfg: this.cfg,
70
+ stellar: this.stellar(),
71
+ httpClient: this.cfg.app.defaultClient,
72
+ servers,
73
+ });
76
74
  }
75
+ }
77
76
 
78
- getClient(httpClientConfig: AxiosRequestConfig = {}) {
79
- return axios.create({
80
- headers: {
81
- ...walletHeaders,
82
- ...httpClientConfig.headers,
83
- },
84
- ...httpClientConfig,
85
- });
77
+ export class Config {
78
+ stellar: StellarConfiguration;
79
+ app: ApplicationConfiguration;
80
+
81
+ constructor({
82
+ stellarConfiguration,
83
+ applicationConfiguration,
84
+ }: ConfigParams) {
85
+ this.stellar = stellarConfiguration;
86
+ this.app = applicationConfiguration;
86
87
  }
87
88
  }
88
89
 
@@ -91,32 +92,48 @@ export class StellarConfiguration {
91
92
  network: Networks;
92
93
  horizonUrl: string;
93
94
  baseFee: number;
95
+ defaultTimeout: number;
94
96
 
95
97
  static TestNet = (): StellarConfiguration => {
96
- return new StellarConfiguration(Networks.TESTNET, NETWORK_URLS.TESTNET);
98
+ return new StellarConfiguration({
99
+ network: Networks.TESTNET,
100
+ horizonUrl: NETWORK_URLS.TESTNET,
101
+ });
97
102
  };
98
103
 
99
104
  static MainNet = (): StellarConfiguration => {
100
- return new StellarConfiguration(Networks.PUBLIC, NETWORK_URLS.PUBLIC);
105
+ return new StellarConfiguration({
106
+ network: Networks.PUBLIC,
107
+ horizonUrl: NETWORK_URLS.PUBLIC,
108
+ });
101
109
  };
102
110
 
103
- constructor(network, horizonUrl, baseFee = 100) {
111
+ constructor({
112
+ network,
113
+ horizonUrl,
114
+ baseFee = 100,
115
+ defaultTimeout = 180,
116
+ }: StellarConfigurationParams) {
104
117
  this.network = network;
105
- this.baseFee = baseFee;
106
118
  this.horizonUrl = horizonUrl;
119
+ this.baseFee = baseFee;
120
+ this.defaultTimeout = defaultTimeout;
107
121
  this.server = new Server(horizonUrl);
108
122
  }
109
123
  }
110
124
 
125
+ export const DefaultClient = axios.create({
126
+ headers: {
127
+ ...walletHeaders,
128
+ },
129
+ });
130
+
111
131
  export class ApplicationConfiguration {
112
132
  defaultSigner: WalletSigner;
113
133
  defaultClient: AxiosInstance;
114
- constructor(defaultSigner: WalletSigner = DefaultSigner) {
115
- this.defaultSigner = defaultSigner;
116
- this.defaultClient = axios.create({
117
- headers: {
118
- ...walletHeaders,
119
- },
120
- });
134
+
135
+ constructor(defaultSigner?: WalletSigner, defaultClient?: AxiosInstance) {
136
+ this.defaultSigner = defaultSigner || DefaultSigner;
137
+ this.defaultClient = defaultClient || DefaultClient;
121
138
  }
122
139
  }
@@ -1,35 +1,39 @@
1
- import StellarSdk, {
2
- Keypair,
3
- TransactionBuilder,
4
- Transaction,
5
- FeeBumpTransaction,
6
- Networks,
7
- } from "stellar-sdk";
8
- import { PublicKeypair } from "../src/walletSdk/horizon/Account";
1
+ import crypto from "crypto";
2
+ import { TransactionBuilder, Networks } from "stellar-sdk";
9
3
 
10
- import sdk from "../src";
11
- const { walletSdk } = sdk;
4
+ import { Wallet } from "../src";
5
+ import { PublicKeypair } from "../src/walletSdk/Horizon/Account";
12
6
 
7
+ let wal;
8
+ let account;
13
9
  describe("Account", () => {
14
- it("should init keypair and sign", () => {
15
- const wal = walletSdk.Wallet.TestNet();
16
- const account = wal.stellar().account();
10
+ beforeEach(() => {
11
+ wal = Wallet.TestNet();
12
+ account = wal.stellar().account();
13
+ });
14
+ it("should create keypair and sign", () => {
17
15
  const kp = account.createKeypair();
18
16
  expect(kp.publicKey).toBeTruthy();
19
17
  expect(kp.secretKey).toBeTruthy();
20
18
 
21
19
  const tx = TransactionBuilder.fromXDR(
22
20
  "AAAAAgAAAADk/TqnRl6sFK79yasH46qlX/dFxQ8R023aHRxAkUmE8wAAAGQAAAAAAAAAAQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAQAAAADk/TqnRl6sFK79yasH46qlX/dFxQ8R023aHRxAkUmE8wAAAAAAAAAABfXhAAAAAAAAAAAA",
23
- Networks.TESTNET
21
+ Networks.TESTNET,
24
22
  );
25
23
  kp.sign(tx);
26
24
  expect(tx.signatures.length).toBe(1);
27
25
  tx.sign(kp.keypair);
28
26
  expect(tx.signatures.length).toBe(2);
29
27
  });
28
+ it("should create keypair from random", () => {
29
+ const rand = crypto.randomBytes(32);
30
+ const kp = account.createKeypairFromRandom(rand);
31
+ expect(kp.publicKey).toBeTruthy();
32
+ expect(kp.secretKey).toBeTruthy();
33
+ });
30
34
  it("can init from string", () => {
31
35
  const kp = PublicKeypair.fromPublicKey(
32
- "GCPECGTX5RZWBJNH7Q3FNN4742R7OKMSP6G4ECCUX7Q5IGDCYYG2I447"
36
+ "GCPECGTX5RZWBJNH7Q3FNN4742R7OKMSP6G4ECCUX7Q5IGDCYYG2I447",
33
37
  );
34
38
  expect(kp.publicKey).toBeTruthy();
35
39
  });