@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
@@ -0,0 +1,247 @@
1
+ import {
2
+ Keypair,
3
+ Memo,
4
+ MemoText,
5
+ Operation,
6
+ Asset,
7
+ Horizon,
8
+ } from "stellar-sdk";
9
+ import axios from "axios";
10
+
11
+ import { Stellar, Wallet } from "../src";
12
+ import {
13
+ AccountKeypair,
14
+ SigningKeypair,
15
+ } from "../src/walletSdk/Horizon/Account";
16
+ import {
17
+ IssuedAssetId,
18
+ FiatAssetId,
19
+ NativeAssetId,
20
+ } from "../src/walletSdk/Asset";
21
+ import { TransactionStatus, WithdrawTransaction } from "../src/walletSdk/Types";
22
+
23
+ let wal: Wallet;
24
+ let stellar: Stellar;
25
+ const kp = SigningKeypair.fromSecret(
26
+ "SAS372GXRG6U7FW6W2PRVELKPOJG2FZZUADCIELWU2U3A45TNWXEQUV5",
27
+ );
28
+ describe("Stellar", () => {
29
+ beforeAll(async () => {
30
+ wal = Wallet.TestNet();
31
+ stellar = wal.stellar();
32
+
33
+ // make sure signing key exists
34
+ try {
35
+ await stellar.server.loadAccount(kp.publicKey);
36
+ } catch (e) {
37
+ await axios.get("https://friendbot.stellar.org/?addr=" + kp.publicKey);
38
+ }
39
+ });
40
+ it("should create and submit a transaction", async () => {
41
+ const now = Math.floor(Date.now() / 1000) - 5;
42
+
43
+ const txBuilderParams = [
44
+ {
45
+ sourceAddress: kp,
46
+ baseFee: 100,
47
+ startingBalance: 2,
48
+ },
49
+ {
50
+ sourceAddress: kp,
51
+ baseFee: 100,
52
+ timebounds: 180,
53
+ memo: new Memo(MemoText, "test-memo"),
54
+ startingBalance: 2.1,
55
+ },
56
+ {
57
+ sourceAddress: kp,
58
+ baseFee: 100,
59
+ timebounds: { minTime: now, maxTime: now + 180 },
60
+ startingBalance: 2,
61
+ },
62
+ ];
63
+
64
+ for (const param of txBuilderParams) {
65
+ const txBuilder = await stellar.transaction(param);
66
+ const newKp = new AccountKeypair(Keypair.random());
67
+ const tx = txBuilder.createAccount(newKp, param.startingBalance).build();
68
+ tx.sign(kp.keypair);
69
+
70
+ let failed;
71
+ try {
72
+ await stellar.submitTransaction(tx);
73
+ await stellar.server.loadAccount(newKp.publicKey);
74
+ } catch (e) {
75
+ failed = true;
76
+ }
77
+ expect(failed).toBeFalsy();
78
+ }
79
+ }, 30000);
80
+
81
+ it("should resubmit with fee increase if txn fails", async () => {
82
+ // mock 1 failed response and then 1 successful to test retry
83
+ jest
84
+ .spyOn(stellar, "submitTransaction")
85
+ .mockRejectedValueOnce({
86
+ response: {
87
+ status: 400,
88
+ statusText: "Bad Request",
89
+ data: {
90
+ extras: {
91
+ result_codes: { transaction: "tx_too_late" },
92
+ },
93
+ },
94
+ },
95
+ })
96
+ .mockReturnValueOnce(Promise.resolve(true));
97
+
98
+ const buildingFunction = (builder) =>
99
+ builder.transfer(kp.publicKey, new NativeAssetId(), "2");
100
+
101
+ const txn = await stellar.submitWithFeeIncrease({
102
+ sourceAddress: kp,
103
+ timeout: 180,
104
+ baseFeeIncrease: 100,
105
+ buildingFunction,
106
+ });
107
+
108
+ expect(txn).toBeTruthy();
109
+ expect(txn.fee).toBe("200");
110
+ });
111
+
112
+ it("should create and submit fee-bump transaction", async () => {
113
+ const txBuilder = await stellar.transaction({
114
+ sourceAddress: kp,
115
+ baseFee: 100,
116
+ });
117
+ const newKp = new AccountKeypair(Keypair.random());
118
+ const transaction = txBuilder.createAccount(newKp, 2).build();
119
+ kp.sign(transaction);
120
+
121
+ const feeBumpTx = stellar.makeFeeBump({ feeAddress: kp, transaction });
122
+ kp.sign(feeBumpTx);
123
+ await stellar.submitTransaction(feeBumpTx);
124
+ }, 10000);
125
+
126
+ it("should be able to give a signing keypair", async () => {
127
+ // mock 1 failed response and then 1 successful to test retry
128
+ jest
129
+ .spyOn(stellar, "submitTransaction")
130
+ .mockRejectedValueOnce({
131
+ response: {
132
+ status: 400,
133
+ statusText: "Bad Request",
134
+ data: {
135
+ extras: {
136
+ result_codes: { transaction: "tx_too_late" },
137
+ },
138
+ },
139
+ },
140
+ })
141
+ .mockReturnValueOnce(Promise.resolve(true));
142
+
143
+ const signerFunction = (txn) => {
144
+ txn.sign(kp.keypair);
145
+ return txn;
146
+ };
147
+
148
+ const buildingFunction = (builder) =>
149
+ builder.transfer(kp.publicKey, new NativeAssetId(), "2");
150
+
151
+ const txn = await stellar.submitWithFeeIncrease({
152
+ sourceAddress: kp,
153
+ timeout: 180,
154
+ baseFeeIncrease: 100,
155
+ signerFunction,
156
+ buildingFunction,
157
+ });
158
+ expect(txn).toBeTruthy();
159
+ expect(txn.fee).toBe("200");
160
+ });
161
+
162
+ it("should add and remove asset support", async () => {
163
+ const asset = new IssuedAssetId(
164
+ "USDC",
165
+ "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
166
+ );
167
+
168
+ const txBuilder = await stellar.transaction({
169
+ sourceAddress: kp,
170
+ baseFee: 1000,
171
+ });
172
+ const tx = txBuilder.addAssetSupport(asset).build();
173
+ tx.sign(kp.keypair);
174
+ await stellar.submitTransaction(tx);
175
+
176
+ let acc = await stellar.server.loadAccount(kp.publicKey);
177
+ let balance = acc.balances.find(
178
+ (b) => (b as Horizon.BalanceLineAsset).asset_code === "USDC",
179
+ );
180
+ expect(balance).toBeTruthy();
181
+
182
+ const tx2 = txBuilder.removeAssetSupport(asset).build();
183
+ tx2.sign(kp.keypair);
184
+ await stellar.submitTransaction(tx2);
185
+
186
+ acc = await stellar.server.loadAccount(kp.publicKey);
187
+ balance = acc.balances.find(
188
+ (b) => (b as Horizon.BalanceLineAsset).asset_code === "USDC",
189
+ );
190
+ expect(balance).toBeFalsy();
191
+ }, 20000);
192
+
193
+ it("should import and sign a transaction from xdr", async () => {
194
+ const txnXdr =
195
+ "AAAAAgAAAACHw+LvUYx5O3Ot8A1SUChfTVk4qxFFJZ5QZ/ktaEUKPwAAAGQACEjuAAABDAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAQAAAACHw+LvUYx5O3Ot8A1SUChfTVk4qxFFJZ5QZ/ktaEUKPwAAAAAAAAAAATEtAAAAAAAAAAAA";
196
+
197
+ const tx = stellar.decodeTransaction(txnXdr);
198
+ kp.sign(tx);
199
+ });
200
+ it("should transfer withdrawal transaction", async () => {
201
+ const walletTransaction = {
202
+ id: "db15d166-5a5e-4d5c-ba5d-271c32cd8cf0",
203
+ kind: "withdrawal",
204
+ status: TransactionStatus.pending_user_transfer_start,
205
+ amount_in: "50.55",
206
+ withdraw_memo_type: "text",
207
+ withdraw_memo: "the withdraw memo",
208
+ withdraw_anchor_account:
209
+ "GCSGSR6KQQ5BP2FXVPWRL6SWPUSFWLVONLIBJZUKTVQB5FYJFVL6XOXE",
210
+ } as WithdrawTransaction;
211
+
212
+ const asset = new IssuedAssetId(
213
+ "USDC",
214
+ "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
215
+ );
216
+
217
+ const txBuilder = await stellar.transaction({
218
+ sourceAddress: kp,
219
+ baseFee: 100,
220
+ });
221
+
222
+ const txn = txBuilder
223
+ .transferWithdrawalTransaction(walletTransaction, asset)
224
+ .build();
225
+ expect(txn).toBeTruthy();
226
+ });
227
+ });
228
+
229
+ describe("Asset", () => {
230
+ it("should create an asset", () => {
231
+ const issued = new IssuedAssetId(
232
+ "USDC",
233
+ "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
234
+ );
235
+ expect(issued.sep38).toBe(
236
+ "stellar:USDC:GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
237
+ );
238
+ expect(issued.toAsset().code).toBe("USDC");
239
+
240
+ const native = new NativeAssetId();
241
+ expect(native.sep38).toBe("stellar:native");
242
+ expect(native.toAsset().code).toBe("XLM");
243
+
244
+ const fiat = new FiatAssetId("USD");
245
+ expect(fiat.sep38).toBe("iso4217:USD");
246
+ });
247
+ });