@stellar/typescript-wallet-sdk 1.1.2 → 1.2.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.
- package/examples/sep24/README.md +22 -0
- package/examples/sep24/sep24.ts +199 -0
- package/lib/bundle.js +42285 -45231
- package/lib/bundle.js.map +1 -1
- package/lib/bundle_browser.js +55838 -51560
- package/lib/bundle_browser.js.map +1 -1
- package/lib/walletSdk/Exceptions/index.d.ts +3 -0
- package/package.json +6 -4
- package/src/index.ts +1 -0
- package/src/walletSdk/Anchor/Sep24.ts +2 -0
- package/src/walletSdk/Auth/WalletSigner.ts +64 -1
- package/src/walletSdk/Auth/index.ts +1 -1
- package/src/walletSdk/Exceptions/index.ts +13 -0
- package/src/walletSdk/Horizon/Transaction/CommonTransactionBuilder.ts +75 -0
- package/src/walletSdk/Horizon/Transaction/SponsoringBuilder.ts +58 -0
- package/src/walletSdk/Horizon/Transaction/TransactionBuilder.ts +132 -37
- package/src/walletSdk/Horizon/index.ts +1 -0
- package/src/walletSdk/Types/auth.ts +4 -0
- package/src/walletSdk/Types/horizon.ts +12 -1
- package/src/walletSdk/index.ts +7 -1
- package/test/stellar.test.ts +237 -19
- package/test/transaction.test.ts +325 -0
- package/test/wallet.test.ts +24 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Sep-24 examples for stellar typescript-wallet-sdk
|
|
2
|
+
|
|
3
|
+
## Example code
|
|
4
|
+
|
|
5
|
+
To view how the wallet-sdk can be used to create sep-24 deposits and withdrawals
|
|
6
|
+
look at `sep24.ts`.
|
|
7
|
+
|
|
8
|
+
## Running deposit and withdrawals
|
|
9
|
+
|
|
10
|
+
To see them in action you can run below from the project root:
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
$ yarn example:sep24
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
This will run the deposit flow and watch for it to finish. At the end it will
|
|
17
|
+
ask if you'd like to run the withdraw flow. Use USDC as the asset for the
|
|
18
|
+
example.
|
|
19
|
+
|
|
20
|
+
Progress will be logged in the terminal.
|
|
21
|
+
|
|
22
|
+
_note: the identity values used in the sep24 interactive portal can all be fake_
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import readline from "readline";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
walletSdk,
|
|
6
|
+
Anchor,
|
|
7
|
+
SigningKeypair,
|
|
8
|
+
Types,
|
|
9
|
+
IssuedAssetId,
|
|
10
|
+
} from "../../src";
|
|
11
|
+
import { Memo, MemoText } from "stellar-sdk";
|
|
12
|
+
|
|
13
|
+
const wallet = walletSdk.Wallet.TestNet();
|
|
14
|
+
const stellar = wallet.stellar();
|
|
15
|
+
const anchor = wallet.anchor({ homeDomain: "testanchor.stellar.org" });
|
|
16
|
+
const account = stellar.account();
|
|
17
|
+
|
|
18
|
+
let kp: SigningKeypair;
|
|
19
|
+
|
|
20
|
+
const asset = new IssuedAssetId(
|
|
21
|
+
"USDC",
|
|
22
|
+
"GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const runSep24 = async () => {
|
|
26
|
+
await createAccount();
|
|
27
|
+
await runDeposit(anchor, kp);
|
|
28
|
+
await runDepositWatcher(anchor);
|
|
29
|
+
|
|
30
|
+
while (!depositDone) {
|
|
31
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const ans = await askQuestion("Do you want to start withdrawal? (y/n)");
|
|
35
|
+
if (ans !== "y") {
|
|
36
|
+
process.exit(0);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
await runWithdraw(anchor, kp);
|
|
40
|
+
await runWithdrawWatcher(anchor, kp);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Create Account
|
|
44
|
+
const createAccount = async () => {
|
|
45
|
+
console.log("creating account ...");
|
|
46
|
+
kp = account.createKeypair();
|
|
47
|
+
console.log(`kp: \n${kp.publicKey}\n${kp.secretKey}`);
|
|
48
|
+
|
|
49
|
+
// funding new account
|
|
50
|
+
await axios.get("https://friendbot.stellar.org/?addr=" + kp.publicKey);
|
|
51
|
+
|
|
52
|
+
const txBuilder = await stellar.transaction({
|
|
53
|
+
sourceAddress: kp,
|
|
54
|
+
baseFee: 1000,
|
|
55
|
+
});
|
|
56
|
+
const tx = txBuilder.addAssetSupport(asset).build();
|
|
57
|
+
kp.sign(tx);
|
|
58
|
+
await stellar.submitTransaction(tx);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Create Deposit
|
|
62
|
+
let authToken: string;
|
|
63
|
+
export const runDeposit = async (anchor: Anchor, kp: SigningKeypair) => {
|
|
64
|
+
console.log("\ncreating deposit ...");
|
|
65
|
+
const auth = await anchor.sep10();
|
|
66
|
+
authToken = await auth.authenticate({ accountKp: kp });
|
|
67
|
+
|
|
68
|
+
const resp = await anchor.sep24().deposit({
|
|
69
|
+
assetCode: asset.code,
|
|
70
|
+
authToken: authToken,
|
|
71
|
+
lang: "en-US",
|
|
72
|
+
destinationMemo: new Memo(MemoText, "test-memo"),
|
|
73
|
+
// Optional field. Same result would be achieved with omitting this field.
|
|
74
|
+
// Replace with a different account if you want to change destination
|
|
75
|
+
destinationAccount: kp.publicKey,
|
|
76
|
+
// If not specified, amount will be collected in the interactive flow. You can also pass extra SEP-9 fields.
|
|
77
|
+
extraFields: { amount: "10" },
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
console.log("Open url:\n", resp.url);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// Watch Deposit
|
|
84
|
+
export let depositDone = false;
|
|
85
|
+
export const runDepositWatcher = (anchor: Anchor) => {
|
|
86
|
+
console.log("\nstarting watcher ...");
|
|
87
|
+
|
|
88
|
+
let stop: Types.WatcherStopFunction;
|
|
89
|
+
const onMessage = (m: Types.AnchorTransaction) => {
|
|
90
|
+
console.log({ m });
|
|
91
|
+
if (m.status === Types.TransactionStatus.completed) {
|
|
92
|
+
console.log("status completed, stopping watcher");
|
|
93
|
+
stop();
|
|
94
|
+
depositDone = true;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const onError = (error: Types.AnchorTransaction | Error) => {
|
|
99
|
+
console.error({ error });
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const watcher = anchor.sep24().watcher();
|
|
103
|
+
const resp = watcher.watchAllTransactions({
|
|
104
|
+
authToken: authToken,
|
|
105
|
+
assetCode: asset.code,
|
|
106
|
+
onMessage,
|
|
107
|
+
onError,
|
|
108
|
+
timeout: 5000,
|
|
109
|
+
lang: "en-US",
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
stop = resp.stop;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// Create Withdrawal
|
|
116
|
+
export const runWithdraw = async (anchor, kp) => {
|
|
117
|
+
console.log("\ncreating withdrawal ...");
|
|
118
|
+
|
|
119
|
+
const resp = await anchor.sep24().withdraw({
|
|
120
|
+
assetCode: asset.code,
|
|
121
|
+
authToken: authToken,
|
|
122
|
+
lang: "en-US",
|
|
123
|
+
// Optional field. Same result would be achieved with omitting this field.
|
|
124
|
+
// Replace with a different account if you want to change destination
|
|
125
|
+
withdrawalAccount: kp.publicKey,
|
|
126
|
+
// If not specified, amount will be collected in the interactive flow. You can also pass extra SEP-9 fields.
|
|
127
|
+
extraFields: { amount: "10" },
|
|
128
|
+
});
|
|
129
|
+
console.log("Open url:\n", resp.url);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const sendWithdrawalTransaction = async (withdrawalTxn, kp) => {
|
|
133
|
+
const asset = new IssuedAssetId(
|
|
134
|
+
"USDC",
|
|
135
|
+
"GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
const txBuilder = await stellar.transaction({
|
|
139
|
+
sourceAddress: kp,
|
|
140
|
+
baseFee: 1000,
|
|
141
|
+
});
|
|
142
|
+
const tx = txBuilder
|
|
143
|
+
.transferWithdrawalTransaction(withdrawalTxn, asset)
|
|
144
|
+
.build();
|
|
145
|
+
kp.sign(tx);
|
|
146
|
+
await stellar.submitTransaction(tx);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// Watch Withdrawal
|
|
150
|
+
export const runWithdrawWatcher = (anchor, kp) => {
|
|
151
|
+
console.log("\nstarting watcher ...");
|
|
152
|
+
|
|
153
|
+
let stop;
|
|
154
|
+
const onMessage = (m) => {
|
|
155
|
+
console.log({ m });
|
|
156
|
+
|
|
157
|
+
if (m.status === Types.TransactionStatus.pending_user_transfer_start) {
|
|
158
|
+
sendWithdrawalTransaction(m, kp);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (m.status === Types.TransactionStatus.completed) {
|
|
162
|
+
console.log("status completed, stopping watcher");
|
|
163
|
+
|
|
164
|
+
stop();
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const onError = (e) => {
|
|
169
|
+
console.error({ e });
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const watcher = anchor.sep24().watcher();
|
|
173
|
+
const resp = watcher.watchAllTransactions({
|
|
174
|
+
authToken: authToken,
|
|
175
|
+
assetCode: asset.code,
|
|
176
|
+
onMessage,
|
|
177
|
+
onError,
|
|
178
|
+
timeout: 5000,
|
|
179
|
+
lang: "en-US",
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
stop = resp.stop;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export const askQuestion = (query) => {
|
|
186
|
+
const rl = readline.createInterface({
|
|
187
|
+
input: process.stdin,
|
|
188
|
+
output: process.stdout,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
return new Promise((resolve) =>
|
|
192
|
+
rl.question(query, (ans) => {
|
|
193
|
+
rl.close();
|
|
194
|
+
resolve(ans);
|
|
195
|
+
}),
|
|
196
|
+
);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
runSep24();
|