@stellar/typescript-wallet-sdk 1.0.0-alpha.2 → 1.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 (75) hide show
  1. package/.husky/pre-commit +4 -0
  2. package/docs/WalletGuide.md +323 -0
  3. package/lib/bundle.js +32343 -5920
  4. package/lib/bundle.js.map +1 -1
  5. package/lib/index.d.ts +25 -0
  6. package/lib/walletSdk/Exceptions/index.d.ts +41 -0
  7. package/lib/walletSdk/Types/anchor.d.ts +148 -0
  8. package/lib/walletSdk/Types/auth.d.ts +35 -0
  9. package/lib/walletSdk/Types/horizon.d.ts +12 -0
  10. package/lib/walletSdk/Types/index.d.ts +30 -0
  11. package/lib/walletSdk/Types/sep24.d.ts +30 -0
  12. package/lib/walletSdk/Types/utils.d.ts +77 -0
  13. package/lib/walletSdk/Types/watcher.d.ts +30 -0
  14. package/lib/walletSdk/Utils/camelToSnakeCase.d.ts +2 -0
  15. package/lib/walletSdk/Utils/index.d.ts +3 -0
  16. package/lib/walletSdk/Utils/toml.d.ts +3 -0
  17. package/lib/walletSdk/Utils/url.d.ts +1 -0
  18. package/lib/walletSdk/Watcher/Types.d.ts +65 -0
  19. package/lib/walletSdk/Watcher/index.d.ts +33 -0
  20. package/lib/walletSdk/anchor/Sep24.d.ts +48 -0
  21. package/lib/walletSdk/anchor/index.d.ts +20 -12
  22. package/lib/walletSdk/auth/WalletSigner.d.ts +4 -0
  23. package/lib/walletSdk/auth/index.d.ts +19 -7
  24. package/lib/walletSdk/exception/index.d.ts +9 -0
  25. package/lib/walletSdk/horizon/Account.d.ts +17 -0
  26. package/lib/walletSdk/horizon/AccountService.d.ts +8 -0
  27. package/lib/walletSdk/horizon/Stellar.d.ts +11 -1
  28. package/lib/walletSdk/horizon/index.d.ts +3 -0
  29. package/lib/walletSdk/horizon/transaction/TransactionBuilder.d.ts +12 -0
  30. package/lib/walletSdk/index.d.ts +19 -18
  31. package/lib/walletSdk/interactive/index.d.ts +16 -5
  32. package/lib/walletSdk/recovery/index.d.ts +18 -0
  33. package/lib/walletSdk/util/camelToSnakeCase.d.ts +2 -0
  34. package/package.json +18 -6
  35. package/prettier.config.js +1 -0
  36. package/src/index.ts +39 -1
  37. package/src/walletSdk/Anchor/Sep24.ts +267 -0
  38. package/src/walletSdk/Anchor/index.ts +63 -32
  39. package/src/walletSdk/Auth/WalletSigner.ts +29 -3
  40. package/src/walletSdk/Auth/index.ts +108 -35
  41. package/src/walletSdk/Exceptions/index.ts +103 -0
  42. package/src/walletSdk/Horizon/Account.ts +50 -0
  43. package/src/walletSdk/Horizon/AccountService.ts +19 -0
  44. package/src/walletSdk/Horizon/Stellar.ts +76 -0
  45. package/src/walletSdk/Horizon/index.ts +3 -0
  46. package/src/walletSdk/Horizon/transaction/TransactionBuilder.ts +65 -0
  47. package/src/walletSdk/Recovery/index.ts +32 -0
  48. package/src/walletSdk/Types/anchor.ts +174 -0
  49. package/src/walletSdk/Types/auth.ts +43 -0
  50. package/src/walletSdk/Types/horizon.ts +14 -0
  51. package/src/walletSdk/Types/index.ts +39 -0
  52. package/src/walletSdk/Types/sep24.ts +41 -0
  53. package/src/walletSdk/Types/utils.ts +77 -0
  54. package/src/walletSdk/Types/watcher.ts +34 -0
  55. package/src/walletSdk/Utils/camelToSnakeCase.ts +16 -0
  56. package/src/walletSdk/Utils/index.ts +3 -0
  57. package/src/walletSdk/Utils/toml.ts +103 -0
  58. package/src/walletSdk/{util → Utils}/url.ts +1 -1
  59. package/src/walletSdk/Watcher/index.ts +364 -0
  60. package/src/walletSdk/index.ts +90 -60
  61. package/test/account.test.ts +37 -0
  62. package/test/fixtures/TransactionsResponse.ts +247 -0
  63. package/test/stellar.test.ts +65 -0
  64. package/test/wallet.test.ts +1704 -0
  65. package/tsconfig.json +3 -7
  66. package/webpack.config.js +2 -0
  67. package/src/walletSdk/exception/index.ts +0 -27
  68. package/src/walletSdk/horizon/Account.ts +0 -0
  69. package/src/walletSdk/horizon/AccountService.ts +0 -0
  70. package/src/walletSdk/horizon/Stellar.ts +0 -6
  71. package/src/walletSdk/horizon/constants.ts +0 -4
  72. package/src/walletSdk/interactive/index.ts +0 -103
  73. package/src/walletSdk/recovery/Recovery.ts +0 -6
  74. package/src/walletSdk/toml/index.ts +0 -179
  75. package/test/index.test.ts +0 -73
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env sh
2
+ . "$(dirname -- "$0")/_/husky.sh"
3
+
4
+ yarn pretty-quick --staged
@@ -0,0 +1,323 @@
1
+ # Wallet SDK usage guide
2
+
3
+ <!--- TOC -->
4
+
5
+ - [Getting started](#getting-started)
6
+ - [Configuring client](#configuring-client)
7
+ - [Anchor](#anchor)
8
+ - [Add client domain signing](#add-client-domain-signing)
9
+ - [Example](#example)
10
+ - [Build on Stellar](#build-on-stellar)
11
+ - [Account service](#account-service)
12
+ - [Transaction builder](#transaction-builder)
13
+ - [Recovery](#recovery)
14
+
15
+ <!--- END -->
16
+
17
+ ## Getting Started
18
+
19
+ First, a root Wallet object should be created. This is a core class, that
20
+ provides all functionality available in the current SDK. Later, it will be
21
+ shown, how to use the wallet object to access methods.
22
+
23
+ It's advised to have a singleton wallet object shared across the app.
24
+ Creating a wallet with default configuration connected to testnet is simple:
25
+
26
+ ```typescript
27
+ let wallet = Wallet.TestNet();
28
+ ```
29
+
30
+ The wallet instance can be further configured. For example, to connect to the
31
+ public main network:
32
+
33
+ ```typescript
34
+ let wallet = new Wallet({
35
+ stellarConfiguration: StellarConfiguration.MainNet(),
36
+ });
37
+ ```
38
+
39
+ ### Configuring client
40
+
41
+ The Wallet SDK uses the [axios client](https://axios-http.com/docs/intro) for
42
+ all network requests. A default client is used by default, or a custom client
43
+ can given and configured like so:
44
+
45
+ ```typescript
46
+ const customClient: AxiosInstance = axios.create({
47
+ baseURL: "https://some-url.com/api",
48
+ timeout: 1000,
49
+ headers: { "X-Custom-Header": "foobar" },
50
+ });
51
+ let appConfig = new ApplicationConfiguration(DefaultSigner, customClient);
52
+ let wal = new Wallet({
53
+ stellarConfiguration: StellarConfiguration.TestNet(),
54
+ applicationConfiguration: appConfig,
55
+ });
56
+ ```
57
+
58
+ ## Anchor
59
+
60
+ Build on and off ramps with anchors for deposits and withdrawals.
61
+
62
+ ```typescript
63
+ let anchor = wallet.anchor({ homeDomain: "testanchor.stellar.org" });
64
+ ```
65
+
66
+ Get anchor information from a TOML file.
67
+
68
+ ```typescript
69
+ let resp = await anchor.sep1();
70
+ ```
71
+
72
+ Authenticate an account with the anchor using SEP-10.
73
+
74
+ ```typescript
75
+ // create an account to authenticate
76
+ let account = wal.stellar().account();
77
+ let accountKp = account.createKeypair();
78
+
79
+ // authenticate with Sep-10
80
+ let auth = await anchor.sep10();
81
+ let authToken = await auth.authenticate({ accountKp });
82
+ ```
83
+
84
+ Get available anchor services and information about them. For example, sep24
85
+ deposit/withdrawal limits, currency, fees, payment methods.
86
+
87
+ ```typescript
88
+ let serviceInfo = await anchor.getServicesInfo();
89
+ ```
90
+
91
+ Sep24 deposit and withdrawal.
92
+
93
+ ```typescript
94
+ const assetCode = "XLM";
95
+
96
+ let resp = await anchor.sep24().deposit({
97
+ accountAddress: accountKp.publicKey(),
98
+ assetCode,
99
+ authToken,
100
+ });
101
+
102
+ let resp = await anchor.sep24().withdraw({
103
+ accountAddress: accountKp.publicKey(),
104
+ assetCode,
105
+ authToken,
106
+ });
107
+ ```
108
+
109
+ Get a single transaction's current status and details.
110
+
111
+ ```typescript
112
+ const transaction = await anchor.sep24().getTransactionBy({
113
+ authToken,
114
+ id: transactionId,
115
+ });
116
+ ```
117
+
118
+ Get account transactions for a specified asset.
119
+
120
+ ```typescript
121
+ const transactions = await anchor.sep24().getTransactionsForAsset({
122
+ authToken,
123
+ assetCode,
124
+ });
125
+ ```
126
+
127
+ Watch transaction.
128
+
129
+ ```typescript
130
+ let watcher = anchor.watcher();
131
+
132
+ let { stop } = watcher.watchOneTransaction({
133
+ authToken,
134
+ assetCode,
135
+ id: successfulTransaction.id,
136
+ onMessage,
137
+ onSuccess,
138
+ onError,
139
+ });
140
+
141
+ // stop watching transaction
142
+ stop();
143
+ ```
144
+
145
+ ### Add client domain signing
146
+
147
+ One of the features being supported by
148
+ [SEP-10 Authentication](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md)
149
+ is verifying client domain. This enables anchor to recognize that user request
150
+ was made using a specific client.
151
+
152
+ Functionality of signing with client domain is enabled by the
153
+ [WalletSigner](../wallet-sdk/src/walletsdk/Auth/WalletSigner.ts). You can use
154
+ the `DefaultSigner` class as a baseline for your implementation. The main method
155
+ that needs to be implemented is `signWithDomainAccount`.
156
+
157
+ Implementation should make a request to a backend server containing the client
158
+ domain private key. Server returns signed transaction, finishing the flow.
159
+
160
+ For your wallet signer to be used, it's required to set it in this SDK. It's
161
+ recommended to set it globally via `ApplicationConfiguration` `defaultSigner`
162
+ [option](../wallet-sdk/src/walletsdk/index.ts). Alternatively, it can be passed
163
+ to the `authenticate` call of the `Sep10` class.
164
+
165
+ #### Example
166
+
167
+ In the example below, [Stellar demo wallet](https://demo-wallet.stellar.org)
168
+ will be used as a client domain. Server-side implementation, responsible for
169
+ signing transaction can be found
170
+ [here](https://github.com/stellar/stellar-demo-wallet/tree/master/packages/demo-wallet-server).
171
+
172
+ First, create an object using the `WalletSigner` interface, and define both the
173
+ signWithClientAccount and a signWithDomainAccount method.
174
+
175
+ ```typescript
176
+ const demoWalletSigner: WalletSigner = {
177
+ signWithClientAccount: ({ transaction, accountKp }) => {
178
+ transaction.sign(accountKp);
179
+ return transaction;
180
+ },
181
+ signWithDomainAccount: async ({
182
+ transactionXDR,
183
+ networkPassphrase,
184
+ accountKp,
185
+ }) => {
186
+ return await axios.post("https://demo-wallet-server.stellar.org/sign", {
187
+ transactionXDR,
188
+ networkPassphrase,
189
+ });
190
+ },
191
+ };
192
+ ```
193
+
194
+ Wallet can now use this class:
195
+
196
+ ```typescript
197
+ const wallet = new walletSdk.Wallet({
198
+ stellarConfiguration: walletSdk.StellarConfiguration.TestNet(),
199
+ applicationConfiguration: new walletSdk.ApplicationConfiguration(
200
+ demoWalletSigner,
201
+ ),
202
+ });
203
+ ```
204
+
205
+ And it can now be used for authentication with client domain:
206
+
207
+ ```typescript
208
+ const authToken = await auth.authenticate({
209
+ accountKp,
210
+ walletSigner: demoWalletSigner,
211
+ clientDomain: "demo-wallet-server.stellar.org",
212
+ });
213
+ ```
214
+
215
+ ## Build on Stellar
216
+
217
+ Once the Wallet is configured, you can use the following:
218
+
219
+ - transaction builder to create transactions (for example, fund the account or
220
+ add asset support),
221
+ - account service to generate account keypair or get account information, and
222
+ - submit a signed transaction to Stellar network.
223
+
224
+ ### Account service
225
+
226
+ Generate new account keypair (public and secret keys).
227
+
228
+ ```typescript
229
+ let account = wal.stellar().account();
230
+ let accountKeyPair = account.createKeypair();
231
+ ```
232
+
233
+ Get account information from the Stellar network (assets, liquidity pools, and
234
+ reserved native balance).
235
+
236
+ ```typescript
237
+ ... code to be added
238
+ ```
239
+
240
+ Get account history (all operations) from the Stellar network.
241
+
242
+ ```typescript
243
+ ... code to be added
244
+ ```
245
+
246
+ ### Transaction builder
247
+
248
+ Transaction builder allows you to create various transactions that can be signed
249
+ and submitted to the Stellar network. Some transactions can be sponsored.
250
+
251
+ ```typescript
252
+ let sourceAccountKeyPair = account.createKeypair();
253
+ let destinationAccountKeyPair = account.createKeypair();
254
+ ```
255
+
256
+ Create account transaction activates/creates an account with a starting balance
257
+ (by default, it's 1 XLM). This transaction can be sponsored.
258
+
259
+ ```typescript
260
+ ... code to be added
261
+ ```
262
+
263
+ Lock the master key of the account (set its weight to 0). Use caution when
264
+ locking the account's master key. Make sure you have set the correct signers and
265
+ weights. Otherwise, you will lock the account irreversibly. This transaction can
266
+ be sponsored.
267
+
268
+ ```typescript
269
+ ... code to be added
270
+ ```
271
+
272
+ Add an asset (trustline) to the account. This allows account to receive the
273
+ asset. This transaction can be sponsored.
274
+
275
+ ```typescript
276
+ ... code to be added
277
+ ```
278
+
279
+ Remove an asset from the account (the balance must be 0)
280
+
281
+ ```typescript
282
+ ... code to be added
283
+ ```
284
+
285
+ Add a new signer to the account. Use caution when adding new signers. Make sure
286
+ you set the correct signer weight. Otherwise, you will lock the account
287
+ irreversibly. This transaction can be sponsored.
288
+
289
+ ```typescript
290
+ ... code to be added
291
+ ```
292
+
293
+ Remove a signer from the account.
294
+
295
+ ```typescript
296
+ ... code to be added
297
+ ```
298
+
299
+ Modify account thresholds (usefully with multiple signers assigned to the
300
+ account). Allows to restrict access to certain operations when limit is not
301
+ reached.
302
+
303
+ ```typescript
304
+ ... code to be added
305
+ ```
306
+
307
+ coming soon:
308
+
309
+ ### Submit transaction
310
+
311
+ ### Transaction builder (extra)
312
+
313
+ ### Transaction builder (sponsoring)
314
+
315
+ ### Sponsoring account creation
316
+
317
+ ### Sponsoring account creation and modification
318
+
319
+ ### Fee bump transaction
320
+
321
+ ### Using XDR to exchange transaction data between server and client
322
+
323
+ ## Recovery