@stellar/typescript-wallet-sdk 1.1.0-alpha.2 → 1.1.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 (79) hide show
  1. package/.husky/pre-commit +4 -0
  2. package/docs/WalletGuide.md +5 -0
  3. package/lib/bundle.js +30068 -4748
  4. package/lib/bundle.js.map +1 -1
  5. package/lib/index.d.ts +26 -7
  6. package/lib/walletSdk/Asset/index.d.ts +27 -0
  7. package/lib/walletSdk/Exceptions/index.d.ts +56 -0
  8. package/lib/walletSdk/Types/anchor.d.ts +148 -0
  9. package/lib/walletSdk/Types/auth.d.ts +35 -0
  10. package/lib/walletSdk/Types/horizon.d.ts +34 -0
  11. package/lib/walletSdk/Types/index.d.ts +30 -0
  12. package/lib/walletSdk/Types/sep24.d.ts +30 -0
  13. package/lib/walletSdk/Types/utils.d.ts +77 -0
  14. package/lib/walletSdk/Types/watcher.d.ts +30 -0
  15. package/lib/walletSdk/Utils/camelToSnakeCase.d.ts +2 -0
  16. package/lib/walletSdk/Utils/getResultCode.d.ts +1 -0
  17. package/lib/walletSdk/Utils/index.d.ts +3 -0
  18. package/lib/walletSdk/Utils/toml.d.ts +3 -0
  19. package/lib/walletSdk/Utils/url.d.ts +1 -0
  20. package/lib/walletSdk/Watcher/index.d.ts +21 -42
  21. package/lib/walletSdk/anchor/Sep24.d.ts +48 -0
  22. package/lib/walletSdk/anchor/index.d.ts +12 -75
  23. package/lib/walletSdk/auth/WalletSigner.d.ts +4 -3
  24. package/lib/walletSdk/auth/index.d.ts +14 -5
  25. package/lib/walletSdk/horizon/Account.d.ts +3 -6
  26. package/lib/walletSdk/horizon/AccountService.d.ts +46 -1
  27. package/lib/walletSdk/horizon/Stellar.d.ts +10 -1
  28. package/lib/walletSdk/horizon/index.d.ts +4 -0
  29. package/lib/walletSdk/horizon/transaction/TransactionBuilder.d.ts +20 -0
  30. package/lib/walletSdk/index.d.ts +17 -15
  31. package/lib/walletSdk/interactive/index.d.ts +3 -14
  32. package/lib/walletSdk/recovery/index.d.ts +18 -0
  33. package/package.json +7 -1
  34. package/prettier.config.js +1 -0
  35. package/src/index.ts +39 -17
  36. package/src/walletSdk/Anchor/Sep24.ts +267 -0
  37. package/src/walletSdk/Anchor/index.ts +32 -201
  38. package/src/walletSdk/Asset/index.ts +58 -0
  39. package/src/walletSdk/Auth/WalletSigner.ts +23 -12
  40. package/src/walletSdk/Auth/index.ts +96 -49
  41. package/src/walletSdk/Exceptions/index.ts +148 -0
  42. package/src/walletSdk/{horizon → Horizon}/Account.ts +7 -9
  43. package/src/walletSdk/Horizon/AccountService.ts +96 -0
  44. package/src/walletSdk/Horizon/Stellar.ts +164 -0
  45. package/src/walletSdk/Horizon/Transaction/TransactionBuilder.ts +140 -0
  46. package/src/walletSdk/Horizon/index.ts +4 -0
  47. package/src/walletSdk/Recovery/index.ts +32 -0
  48. package/src/walletSdk/{Watcher/Types.ts → Types/anchor.ts} +114 -21
  49. package/src/walletSdk/Types/auth.ts +43 -0
  50. package/src/walletSdk/Types/horizon.ts +40 -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/getResultCode.ts +5 -0
  57. package/src/walletSdk/Utils/index.ts +3 -0
  58. package/src/walletSdk/Utils/toml.ts +103 -0
  59. package/src/walletSdk/{util → Utils}/url.ts +1 -1
  60. package/src/walletSdk/Watcher/index.ts +137 -128
  61. package/src/walletSdk/index.ts +72 -55
  62. package/test/account.test.ts +19 -15
  63. package/test/accountService.test.ts +109 -0
  64. package/test/fixtures/TransactionsResponse.ts +144 -127
  65. package/test/stellar.test.ts +247 -0
  66. package/test/wallet.test.ts +196 -214
  67. package/tsconfig.json +3 -7
  68. package/webpack.config.js +2 -0
  69. package/lib/walletSdk/anchor/Types.d.ts +0 -75
  70. package/lib/walletSdk/util/sleep.d.ts +0 -1
  71. package/src/walletSdk/Anchor/Types.ts +0 -83
  72. package/src/walletSdk/exception/index.ts +0 -48
  73. package/src/walletSdk/horizon/AccountService.ts +0 -19
  74. package/src/walletSdk/horizon/Stellar.ts +0 -14
  75. package/src/walletSdk/horizon/constants.ts +0 -4
  76. package/src/walletSdk/interactive/index.ts +0 -105
  77. package/src/walletSdk/recovery/Recovery.ts +0 -6
  78. package/src/walletSdk/toml/index.ts +0 -179
  79. package/src/walletSdk/util/camelToSnakeCase.ts +0 -13
@@ -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
  });
@@ -0,0 +1,109 @@
1
+ import axios from "axios";
2
+ import { Horizon } from "stellar-sdk";
3
+
4
+ import { AccountService, SigningKeypair, Wallet } from "../src";
5
+ import { HORIZON_ORDER } from "../src/walletSdk/Types";
6
+ import { IssuedAssetId } from "../src/walletSdk/Asset";
7
+
8
+ let accountService: AccountService;
9
+ let accountAddress: string;
10
+
11
+ describe("Horizon", () => {
12
+ // Creates testing stellar account with USDC trustline
13
+ // in case it doesn't exist just yet
14
+ beforeAll(async () => {
15
+ const wallet = Wallet.TestNet();
16
+ const stellar = wallet.stellar();
17
+ accountService = stellar.account();
18
+
19
+ const fundingAccountKp = SigningKeypair.fromSecret(
20
+ "SDJTZXPFPWRK4GHECLQBDFRDCNEZFA4PIZA475WQEGKTL4Y2QLS77DGD",
21
+ );
22
+
23
+ // make sure funding account exists
24
+ try {
25
+ await stellar.server.loadAccount(fundingAccountKp.publicKey);
26
+ } catch (e) {
27
+ await axios.get(
28
+ "https://friendbot.stellar.org/?addr=" + fundingAccountKp.publicKey,
29
+ );
30
+ }
31
+
32
+ const testingAccountKp = SigningKeypair.fromSecret(
33
+ "SAXW2HC7JH5IJSIRFQ22JTMT6T3VONKGMYSIBLHNEJCV7AXLIGAXNESD",
34
+ );
35
+
36
+ // make sure testing account exists
37
+ accountAddress = testingAccountKp.publicKey;
38
+ try {
39
+ await stellar.server.loadAccount(accountAddress);
40
+ } catch (e) {
41
+ const txBuilder1 = await stellar.transaction({
42
+ sourceAddress: fundingAccountKp,
43
+ baseFee: 100,
44
+ });
45
+ const createAccTx = txBuilder1.createAccount(testingAccountKp, 2).build();
46
+ createAccTx.sign(fundingAccountKp.keypair);
47
+
48
+ let failed = false;
49
+ try {
50
+ await stellar.submitTransaction(createAccTx);
51
+ await stellar.server.loadAccount(accountAddress);
52
+ } catch (e) {
53
+ failed = true;
54
+ }
55
+
56
+ const txBuilder2 = await stellar.transaction({
57
+ sourceAddress: testingAccountKp,
58
+ baseFee: 100,
59
+ });
60
+ const asset = new IssuedAssetId(
61
+ "USDC",
62
+ "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
63
+ );
64
+ const addUsdcTx = txBuilder2.addAssetSupport(asset).build();
65
+ addUsdcTx.sign(testingAccountKp.keypair);
66
+
67
+ // make sure testing account has USDC trustline
68
+ try {
69
+ await stellar.submitTransaction(addUsdcTx);
70
+ } catch (e) {
71
+ failed = true;
72
+ }
73
+
74
+ expect(failed).toBeFalsy();
75
+ }
76
+ }, 30000);
77
+
78
+ it("should return stellar account details", async () => {
79
+ const response = await accountService.getInfo({ accountAddress });
80
+
81
+ expect(response.account_id).toBe(accountAddress);
82
+ expect(response.balances).toBeInstanceOf(Array);
83
+ expect(
84
+ response.balances.some(
85
+ (balance) =>
86
+ (balance as Horizon.BalanceLineAsset).asset_code === "USDC",
87
+ ),
88
+ ).toBeTruthy();
89
+ });
90
+
91
+ it("should return stellar account operations", async () => {
92
+ const response = await accountService.getHistory({
93
+ accountAddress,
94
+ order: HORIZON_ORDER.ASC,
95
+ });
96
+
97
+ expect(response.records).toBeInstanceOf(Array);
98
+ expect(response.records[0]).toBeInstanceOf(Object);
99
+ expect(response.records[0]).toHaveProperty("id");
100
+ expect(response.records[0]).toHaveProperty("type");
101
+ expect(response.records[0]).toHaveProperty("created_at");
102
+ expect(
103
+ response.records.some(({ type }) => type === "create_account"),
104
+ ).toBeTruthy();
105
+ expect(
106
+ response.records.some(({ type }) => type === "change_trust"),
107
+ ).toBeTruthy();
108
+ });
109
+ });