mainnet-js 3.1.7 → 4.0.0-next.10

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 (267) hide show
  1. package/dist/module/cache/IndexedDbCache.d.ts +1 -1
  2. package/dist/module/cache/IndexedDbCache.d.ts.map +1 -1
  3. package/dist/module/cache/MemoryCache.d.ts +1 -1
  4. package/dist/module/cache/MemoryCache.d.ts.map +1 -1
  5. package/dist/module/cache/WebStorageCache.d.ts +1 -1
  6. package/dist/module/cache/WebStorageCache.d.ts.map +1 -1
  7. package/dist/module/cache/walletCache.d.ts +16 -6
  8. package/dist/module/cache/walletCache.d.ts.map +1 -1
  9. package/dist/module/cache/walletCache.js +92 -34
  10. package/dist/module/cache/walletCache.js.map +1 -1
  11. package/dist/module/cli.js +1 -2
  12. package/dist/module/cli.js.map +1 -1
  13. package/dist/module/db/index.d.ts +1 -1
  14. package/dist/module/db/index.d.ts.map +1 -1
  15. package/dist/module/db/index.js +1 -1
  16. package/dist/module/db/index.js.map +1 -1
  17. package/dist/module/enum.d.ts +1 -1
  18. package/dist/module/enum.d.ts.map +1 -1
  19. package/dist/module/history/getHistory.d.ts +1 -1
  20. package/dist/module/history/getHistory.d.ts.map +1 -1
  21. package/dist/module/history/getHistory.js +3 -3
  22. package/dist/module/history/getHistory.js.map +1 -1
  23. package/dist/module/index.d.ts +25 -22
  24. package/dist/module/index.d.ts.map +1 -1
  25. package/dist/module/index.js +33 -26
  26. package/dist/module/index.js.map +1 -1
  27. package/dist/module/interface.d.ts +12 -2
  28. package/dist/module/interface.d.ts.map +1 -1
  29. package/dist/module/interface.js.map +1 -1
  30. package/dist/module/libauth.d.ts +1 -1
  31. package/dist/module/libauth.d.ts.map +1 -1
  32. package/dist/module/libauth.js +1 -1
  33. package/dist/module/libauth.js.map +1 -1
  34. package/dist/module/mine/mine.d.ts +2 -7
  35. package/dist/module/mine/mine.d.ts.map +1 -1
  36. package/dist/module/mine/mine.js +6 -27
  37. package/dist/module/mine/mine.js.map +1 -1
  38. package/dist/module/network/Connection.d.ts +1 -12
  39. package/dist/module/network/Connection.d.ts.map +1 -1
  40. package/dist/module/network/Connection.js +24 -33
  41. package/dist/module/network/Connection.js.map +1 -1
  42. package/dist/module/network/ElectrumNetworkProvider.d.ts +16 -17
  43. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  44. package/dist/module/network/ElectrumNetworkProvider.js +91 -93
  45. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  46. package/dist/module/network/MockNetworkProvider.d.ts +28 -0
  47. package/dist/module/network/MockNetworkProvider.d.ts.map +1 -0
  48. package/dist/module/network/MockNetworkProvider.js +74 -0
  49. package/dist/module/network/MockNetworkProvider.js.map +1 -0
  50. package/dist/module/network/NetworkProvider.d.ts +9 -2
  51. package/dist/module/network/NetworkProvider.d.ts.map +1 -1
  52. package/dist/module/network/configuration.d.ts +2 -4
  53. package/dist/module/network/configuration.d.ts.map +1 -1
  54. package/dist/module/network/configuration.js +25 -50
  55. package/dist/module/network/configuration.js.map +1 -1
  56. package/dist/module/network/constant.d.ts +7 -7
  57. package/dist/module/network/constant.d.ts.map +1 -1
  58. package/dist/module/network/constant.js +21 -24
  59. package/dist/module/network/constant.js.map +1 -1
  60. package/dist/module/network/default.d.ts +5 -3
  61. package/dist/module/network/default.d.ts.map +1 -1
  62. package/dist/module/network/default.js +29 -51
  63. package/dist/module/network/default.js.map +1 -1
  64. package/dist/module/network/index.d.ts +4 -4
  65. package/dist/module/network/index.d.ts.map +1 -1
  66. package/dist/module/network/index.js +2 -2
  67. package/dist/module/network/index.js.map +1 -1
  68. package/dist/module/network/interface.d.ts +0 -6
  69. package/dist/module/network/interface.d.ts.map +1 -1
  70. package/dist/module/rate/ExchangeRate.d.ts +1 -1
  71. package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
  72. package/dist/module/rate/ExchangeRate.js +13 -7
  73. package/dist/module/rate/ExchangeRate.js.map +1 -1
  74. package/dist/module/transaction/Wif.d.ts +3 -3
  75. package/dist/module/transaction/Wif.d.ts.map +1 -1
  76. package/dist/module/transaction/Wif.js +4 -4
  77. package/dist/module/transaction/Wif.js.map +1 -1
  78. package/dist/module/transaction/allocateFee.d.ts +1 -1
  79. package/dist/module/transaction/allocateFee.d.ts.map +1 -1
  80. package/dist/module/transaction/allocateFee.js +2 -2
  81. package/dist/module/transaction/allocateFee.js.map +1 -1
  82. package/dist/module/util/deriveCashaddr.js.map +1 -1
  83. package/dist/module/util/deriveNetwork.js +1 -1
  84. package/dist/module/util/deriveNetwork.js.map +1 -1
  85. package/dist/module/util/getAddrsByXpubKey.js +1 -1
  86. package/dist/module/util/getAddrsByXpubKey.js.map +1 -1
  87. package/dist/module/util/getXPubKey.js +1 -1
  88. package/dist/module/util/getXPubKey.js.map +1 -1
  89. package/dist/module/util/hd.d.ts +2 -0
  90. package/dist/module/util/hd.d.ts.map +1 -1
  91. package/dist/module/util/hd.js +41 -0
  92. package/dist/module/util/hd.js.map +1 -1
  93. package/dist/module/util/index.d.ts +7 -9
  94. package/dist/module/util/index.d.ts.map +1 -1
  95. package/dist/module/util/index.js +7 -9
  96. package/dist/module/util/index.js.map +1 -1
  97. package/dist/module/util/transaction.js +1 -1
  98. package/dist/module/util/transaction.js.map +1 -1
  99. package/dist/module/wallet/Base.d.ts +13 -9
  100. package/dist/module/wallet/Base.d.ts.map +1 -1
  101. package/dist/module/wallet/Base.js +54 -58
  102. package/dist/module/wallet/Base.js.map +1 -1
  103. package/dist/module/wallet/HDWallet.d.ts.map +1 -1
  104. package/dist/module/wallet/HDWallet.js +19 -38
  105. package/dist/module/wallet/HDWallet.js.map +1 -1
  106. package/dist/module/wallet/Util.d.ts +4 -22
  107. package/dist/module/wallet/Util.d.ts.map +1 -1
  108. package/dist/module/wallet/Util.js +75 -102
  109. package/dist/module/wallet/Util.js.map +1 -1
  110. package/dist/module/wallet/Watch.d.ts +24 -5
  111. package/dist/module/wallet/Watch.d.ts.map +1 -1
  112. package/dist/module/wallet/Watch.js +141 -17
  113. package/dist/module/wallet/Watch.js.map +1 -1
  114. package/dist/module/wallet/Wif.d.ts +2 -6
  115. package/dist/module/wallet/Wif.d.ts.map +1 -1
  116. package/dist/module/wallet/Wif.js +3 -69
  117. package/dist/module/wallet/Wif.js.map +1 -1
  118. package/dist/module/wallet/createWallet.d.ts +1 -1
  119. package/dist/module/wallet/createWallet.d.ts.map +1 -1
  120. package/dist/module/wallet/createWallet.js +2 -1
  121. package/dist/module/wallet/createWallet.js.map +1 -1
  122. package/dist/module/wallet/interface.d.ts +3 -2
  123. package/dist/module/wallet/interface.d.ts.map +1 -1
  124. package/dist/module/wallet/model.d.ts +2 -1
  125. package/dist/module/wallet/model.d.ts.map +1 -1
  126. package/dist/module/wallet/model.js +1 -1
  127. package/dist/module/wallet/model.js.map +1 -1
  128. package/package.json +17 -29
  129. package/dist/index.html +0 -9
  130. package/dist/mainnet-3.1.7.js +0 -2066
  131. package/dist/module/network/util.d.ts +0 -3
  132. package/dist/module/network/util.d.ts.map +0 -1
  133. package/dist/module/network/util.js +0 -27
  134. package/dist/module/network/util.js.map +0 -1
  135. package/dist/module/test/expect.d.ts +0 -12
  136. package/dist/module/test/expect.d.ts.map +0 -1
  137. package/dist/module/test/expect.js +0 -47
  138. package/dist/module/test/expect.js.map +0 -1
  139. package/dist/module/test/fetch.d.ts +0 -3
  140. package/dist/module/test/fetch.d.ts.map +0 -1
  141. package/dist/module/test/fetch.js +0 -32
  142. package/dist/module/test/fetch.js.map +0 -1
  143. package/dist/module/util/randomBytes.d.ts +0 -2
  144. package/dist/module/util/randomBytes.d.ts.map +0 -1
  145. package/dist/module/util/randomBytes.js +0 -13
  146. package/dist/module/util/randomBytes.js.map +0 -1
  147. package/dist/tsconfig.tsbuildinfo +0 -1
  148. package/src/cache/IndexedDbCache.test.ts +0 -15
  149. package/src/cache/IndexedDbCache.ts +0 -172
  150. package/src/cache/MemoryCache.test.ts +0 -15
  151. package/src/cache/MemoryCache.ts +0 -32
  152. package/src/cache/WebStorageCache.test.ts +0 -15
  153. package/src/cache/WebStorageCache.ts +0 -38
  154. package/src/cache/index.ts +0 -2
  155. package/src/cache/interface.ts +0 -9
  156. package/src/cache/walletCache.ts +0 -254
  157. package/src/chain.ts +0 -3
  158. package/src/cli.ts +0 -32
  159. package/src/config.ts +0 -23
  160. package/src/constant.ts +0 -27
  161. package/src/db/ExchangeRateProvider.ts +0 -28
  162. package/src/db/StorageProvider.ts +0 -64
  163. package/src/db/index.ts +0 -2
  164. package/src/db/interface.ts +0 -11
  165. package/src/enum.ts +0 -34
  166. package/src/history/getHistory.test.ts +0 -290
  167. package/src/history/getHistory.ts +0 -411
  168. package/src/history/interface.ts +0 -24
  169. package/src/index.ts +0 -48
  170. package/src/interface.ts +0 -72
  171. package/src/libauth.ts +0 -11
  172. package/src/message/index.ts +0 -2
  173. package/src/message/interface.ts +0 -40
  174. package/src/message/signed.test.ts +0 -309
  175. package/src/message/signed.ts +0 -201
  176. package/src/mine/index.ts +0 -1
  177. package/src/mine/mine.test.ts +0 -10
  178. package/src/mine/mine.ts +0 -42
  179. package/src/network/Connection.test.ts +0 -51
  180. package/src/network/Connection.ts +0 -73
  181. package/src/network/ElectrumNetworkProvider.ts +0 -657
  182. package/src/network/NetworkProvider.ts +0 -180
  183. package/src/network/Rpc.test.ts +0 -130
  184. package/src/network/configuration.test.ts +0 -59
  185. package/src/network/configuration.ts +0 -72
  186. package/src/network/constant.ts +0 -43
  187. package/src/network/default.ts +0 -120
  188. package/src/network/electrum.test.ts +0 -28
  189. package/src/network/getRelayFeeCache.test.ts +0 -15
  190. package/src/network/getRelayFeeCache.ts +0 -23
  191. package/src/network/index.ts +0 -14
  192. package/src/network/interface.ts +0 -80
  193. package/src/network/util.test.ts +0 -24
  194. package/src/network/util.ts +0 -30
  195. package/src/rate/ExchangeRate.test.headless.js +0 -35
  196. package/src/rate/ExchangeRate.test.ts +0 -51
  197. package/src/rate/ExchangeRate.ts +0 -142
  198. package/src/test/expect.ts +0 -59
  199. package/src/test/fetch.ts +0 -39
  200. package/src/test/json.test.ts +0 -13
  201. package/src/transaction/Wif.ts +0 -680
  202. package/src/transaction/allocateFee.test.ts +0 -298
  203. package/src/transaction/allocateFee.ts +0 -149
  204. package/src/util/amountInSatoshi.test.ts +0 -27
  205. package/src/util/amountInSatoshi.ts +0 -33
  206. package/src/util/asSendRequestObject.ts +0 -81
  207. package/src/util/base64.test.ts +0 -39
  208. package/src/util/base64.ts +0 -12
  209. package/src/util/browserNotSupported.ts +0 -7
  210. package/src/util/checkForEmptySeed.ts +0 -9
  211. package/src/util/checkUtxos.ts +0 -29
  212. package/src/util/checkWifNetwork.ts +0 -24
  213. package/src/util/convert.test.ts +0 -46
  214. package/src/util/convert.ts +0 -50
  215. package/src/util/delay.ts +0 -3
  216. package/src/util/deriveCashaddr.test.ts +0 -164
  217. package/src/util/deriveCashaddr.ts +0 -143
  218. package/src/util/deriveLockscript.ts +0 -16
  219. package/src/util/deriveNetwork.ts +0 -19
  220. package/src/util/derivePublicKeyHash.test.ts +0 -55
  221. package/src/util/derivePublicKeyHash.ts +0 -64
  222. package/src/util/floor.test.ts +0 -21
  223. package/src/util/floor.ts +0 -4
  224. package/src/util/getAddrsByXpubKey.test.ts +0 -115
  225. package/src/util/getAddrsByXpubKey.ts +0 -86
  226. package/src/util/getRuntimePlatform.test.headless.js +0 -40
  227. package/src/util/getRuntimePlatform.test.ts +0 -5
  228. package/src/util/getRuntimePlatform.ts +0 -31
  229. package/src/util/getUsdRate.ts +0 -5
  230. package/src/util/getXPubKey.ts +0 -39
  231. package/src/util/hash160.test.ts +0 -18
  232. package/src/util/hash160.ts +0 -12
  233. package/src/util/hd.ts +0 -16
  234. package/src/util/header.test.ts +0 -34
  235. package/src/util/header.ts +0 -26
  236. package/src/util/index.ts +0 -33
  237. package/src/util/randomBytes.ts +0 -13
  238. package/src/util/randomInt.test.ts +0 -15
  239. package/src/util/randomInt.ts +0 -4
  240. package/src/util/sanitizeAddress.ts +0 -10
  241. package/src/util/sanitizeUnit.ts +0 -11
  242. package/src/util/satoshiToAmount.test.ts +0 -6
  243. package/src/util/satoshiToAmount.ts +0 -33
  244. package/src/util/sumSendRequestAmounts.ts +0 -34
  245. package/src/util/sumUtxoValue.ts +0 -27
  246. package/src/util/transaction.ts +0 -10
  247. package/src/wallet/Base.ts +0 -1563
  248. package/src/wallet/Cashtokens.test.headless.js +0 -730
  249. package/src/wallet/Cashtokens.test.ts +0 -1411
  250. package/src/wallet/HDWallet.test.ts +0 -1086
  251. package/src/wallet/HDWallet.ts +0 -992
  252. package/src/wallet/Util.test.ts +0 -134
  253. package/src/wallet/Util.ts +0 -191
  254. package/src/wallet/WalletCache.test.ts +0 -45
  255. package/src/wallet/Watch.ts +0 -441
  256. package/src/wallet/Wif.bip39.test.ts +0 -48
  257. package/src/wallet/Wif.test.ts +0 -1189
  258. package/src/wallet/Wif.ts +0 -687
  259. package/src/wallet/Wif.watchOnly.test.ts +0 -58
  260. package/src/wallet/createWallet.ts +0 -238
  261. package/src/wallet/enum.ts +0 -18
  262. package/src/wallet/interface.ts +0 -102
  263. package/src/wallet/model.test.ts +0 -24
  264. package/src/wallet/model.ts +0 -352
  265. package/tsconfig.browser.json +0 -11
  266. package/tsconfig.json +0 -33
  267. package/webpack.config.cjs +0 -132
@@ -1,58 +0,0 @@
1
- import { RegTestWallet, TestNetWallet, Wallet } from "./Wif";
2
- import { initProviders, disconnectProviders } from "../network/Connection";
3
-
4
- beforeAll(async () => {
5
- await initProviders();
6
- });
7
- afterAll(async () => {
8
- await disconnectProviders();
9
- });
10
-
11
- describe(`Test creation of wallet from walletId`, () => {
12
- test("Get a regtest wallet from string id", async () => {
13
- const ADDRESS = "bchreg:qpttdv3qg2usm4nm7talhxhl05mlhms3ys43u76rn0";
14
- const ADDRESS_TOKEN = "bchreg:zpttdv3qg2usm4nm7talhxhl05mlhms3ysjm0q59vu";
15
-
16
- let w = await RegTestWallet.fromId(
17
- "wif:regtest:cNfsPtqN2bMRS7vH5qd8tR8GMvgXyL5BjnGAKgZ8DYEiCrCCQcP6"
18
- );
19
-
20
- expect(w.cashaddr!.startsWith("bchreg:")).toBeTruthy();
21
- expect(w.cashaddr!).toBe(ADDRESS);
22
- expect(w.cashaddr!).toBe(ADDRESS);
23
- expect(w.tokenaddr!).toBe(ADDRESS_TOKEN);
24
- let w2 = await RegTestWallet.watchOnly(w.cashaddr!);
25
-
26
- expect(w2.cashaddr!).toBe(ADDRESS);
27
- expect(w2.cashaddr!).toBe(ADDRESS);
28
- expect(w2.tokenaddr!).toBe(ADDRESS_TOKEN);
29
-
30
- let w3 = await RegTestWallet.watchOnly(ADDRESS_TOKEN);
31
- expect(w3.cashaddr!).toBe(ADDRESS);
32
- expect(w3.cashaddr!).toBe(ADDRESS);
33
- expect(w3.tokenaddr!).toBe(ADDRESS_TOKEN);
34
- });
35
-
36
- test("Get create a p2sh32 watch address", async () => {
37
- const ADDRESS =
38
- "bitcoincash:pvykylj7uk385669fjse37r2eknx35ngaalx805q2hul62lmx2fq2v5dx6g8w";
39
-
40
- let w = await Wallet.watchOnly(
41
- "bitcoincash:pvykylj7uk385669fjse37r2eknx35ngaalx805q2hul62lmx2fq2v5dx6g8w"
42
- );
43
-
44
- expect(w.cashaddr!.startsWith("bitcoincash:")).toBeTruthy();
45
- expect(w.cashaddr!).toBe(ADDRESS);
46
- });
47
-
48
- test("Get create a p2sh watch address", async () => {
49
- const ADDRESS = "bitcoincash:pzvts0uztwg32yvrx7xz9lp572dgnt20sy0dj993vx";
50
-
51
- let w = await Wallet.watchOnly(
52
- "bitcoincash:pzvts0uztwg32yvrx7xz9lp572dgnt20sy0dj993vx"
53
- );
54
-
55
- expect(w.cashaddr!.startsWith("bitcoincash:")).toBeTruthy();
56
- expect(w.cashaddr!).toBe(ADDRESS);
57
- });
58
- });
@@ -1,238 +0,0 @@
1
- import { prefixFromNetworkMap } from "../enum.js";
2
- import { getNamedWalletId } from "./Base.js";
3
- import { HDWallet, RegTestHDWallet, TestNetHDWallet } from "./HDWallet.js";
4
- import {
5
- RegTestWatchWallet,
6
- TestNetWatchWallet,
7
- WatchWallet,
8
- } from "./Watch.js";
9
- import {
10
- RegTestWallet,
11
- RegTestWifWallet,
12
- TestNetWallet,
13
- TestNetWifWallet,
14
- Wallet,
15
- WifWallet,
16
- } from "./Wif.js";
17
- import { WalletRequestI, WalletResponseI } from "./interface.js";
18
-
19
- // Convenience map to access classes by types and network
20
- export const walletClassMap = {
21
- wif: {
22
- mainnet: () => {
23
- return WifWallet;
24
- },
25
- testnet: () => {
26
- return TestNetWifWallet;
27
- },
28
- regtest: () => {
29
- return RegTestWifWallet;
30
- },
31
- },
32
- seed: {
33
- mainnet: () => {
34
- return Wallet;
35
- },
36
- testnet: () => {
37
- return TestNetWallet;
38
- },
39
- regtest: () => {
40
- return RegTestWallet;
41
- },
42
- },
43
- watch: {
44
- mainnet: () => {
45
- return WatchWallet;
46
- },
47
- testnet: () => {
48
- return TestNetWatchWallet;
49
- },
50
- regtest: () => {
51
- return RegTestWatchWallet;
52
- },
53
- },
54
- hd: {
55
- mainnet: () => {
56
- return HDWallet;
57
- },
58
- testnet: () => {
59
- return TestNetHDWallet;
60
- },
61
- regtest: () => {
62
- return RegTestHDWallet;
63
- },
64
- },
65
- };
66
-
67
- /**
68
- * Check wallet type and network of a requested wallet for mismatches against retrieved from DB
69
- */
70
- function checkWalletTypeAndNetwork(wallet: Wallet, walletType, networkType) {
71
- if (wallet.network != networkType) {
72
- throw Error(
73
- `A wallet already exists with name ${wallet.name}, but with network ${wallet.network} not ${networkType}, per request`
74
- );
75
- }
76
- if (wallet.walletType != walletType) {
77
- throw Error(
78
- `A wallet already exists with name ${wallet.name}, but with type ${wallet.walletType} not ${walletType}, per request`
79
- );
80
- }
81
- }
82
-
83
- /**
84
- * Check if wallet exists
85
- * @param body A wallet request object
86
- * @returns A promise to the check result
87
- */
88
- export async function namedWalletExists(body): Promise<boolean> {
89
- const walletType = body.type ? body.type : "seed";
90
- const networkType = body.network ? body.network : "mainnet";
91
- const name = body.name;
92
-
93
- return await walletClassMap[walletType][networkType]().namedExists(name);
94
- }
95
-
96
- /**
97
- * Get or create a named wallet
98
- * @param name user friendly wallet alias
99
- * @param walletType wallet type
100
- * @param networkType wallet network type
101
- * @returns A promise to a new wallet object
102
- */
103
- export async function namedWallet(
104
- name,
105
- walletType,
106
- networkType
107
- ): Promise<Wallet> {
108
- // Named wallets are saved in the database
109
- if (!name) {
110
- throw Error(`Wallet name is required for this operation`);
111
- }
112
-
113
- let wallet;
114
- const dbName = prefixFromNetworkMap[networkType];
115
- if (walletClassMap[walletType] !== undefined) {
116
- wallet = await walletClassMap[walletType][networkType]().named(name);
117
- checkWalletTypeAndNetwork(wallet, walletType, networkType);
118
- } else {
119
- const walletId = await getNamedWalletId(name, dbName);
120
- if (walletId !== undefined) {
121
- wallet = await walletFromId(walletId);
122
- wallet.name = name;
123
- } else {
124
- throw Error(
125
- "A named wallet, without wallet type, was passed but there was no corresponding record for the named wallet in the database."
126
- );
127
- }
128
- }
129
-
130
- return wallet;
131
- }
132
-
133
- /**
134
- * Replace or create a named wallet with a walletId
135
- * @param body A wallet request object
136
- * @returns A promise to a new wallet object
137
- */
138
- export async function replaceNamedWallet(body): Promise<Wallet> {
139
- let wallet;
140
- const walletType = body.type ? body.type : "seed";
141
- const networkType = body.network ? body.network : "mainnet";
142
- const name = body.name;
143
- const walletId = body.walletId;
144
-
145
- // Named wallets are saved in the database
146
- if (!name || !walletId) {
147
- throw Error(`Wallet name and walletId are required for this operation`);
148
- }
149
-
150
- wallet = await walletClassMap[walletType][networkType]().replaceNamed(
151
- name,
152
- walletId
153
- );
154
- return wallet;
155
- }
156
-
157
- /**
158
- * Create a new wallet
159
- * @param body A wallet request object
160
- * @returns A promise to a new wallet object
161
- */
162
- export async function createWallet(body: WalletRequestI): Promise<Wallet> {
163
- const walletType = body.type ? body.type : "seed";
164
- const networkType = body.network ? body.network : "mainnet";
165
-
166
- // Named wallets are saved in the database
167
- if (body.name && body.name.length > 0) {
168
- const wallet = await namedWallet(body.name, walletType, networkType);
169
- return wallet;
170
- }
171
- // This handles unsaved/unnamed wallets
172
- else {
173
- const walletClass = walletClassMap[walletType][networkType]();
174
- const wallet = new walletClass();
175
- wallet.walletType = walletType;
176
- return wallet.initialize();
177
- }
178
- }
179
-
180
- /**
181
- * Create a new wallet
182
- * @param walletRequest A wallet request object
183
- * @returns A new wallet object
184
- */
185
- export async function createWalletResponse(
186
- walletRequest: WalletRequestI
187
- ): Promise<WalletResponseI> {
188
- const wallet = await createWallet(walletRequest);
189
- if (wallet) {
190
- return asJsonResponse(wallet);
191
- } else {
192
- throw Error("Error creating wallet");
193
- }
194
- }
195
-
196
- /**
197
- * asJsonResponse return a wallet as json
198
- * @param wallet A wallet object
199
- * @returns A json wallet response
200
- */
201
- function asJsonResponse(wallet: Wallet): WalletResponseI {
202
- if (wallet.mnemonic) {
203
- return {
204
- name: wallet.name,
205
- cashaddr: wallet.cashaddr as string,
206
- walletId: wallet.toString(),
207
- ...wallet.getSeed(),
208
- network: wallet.network as any,
209
- };
210
- } else {
211
- return {
212
- name: wallet.name,
213
- cashaddr: wallet.cashaddr as string,
214
- walletId: wallet.toString(),
215
- network: wallet.network as any,
216
- };
217
- }
218
- }
219
-
220
- /**
221
- * walletFromId - get a wallet from a serialized wallet
222
- * @param {string} walletId A serialized wallet object
223
- * @returns A wallet
224
- */
225
- export async function walletFromId(walletId: string): Promise<any> {
226
- const [walletType, network, name]: string[] = walletId.split(":");
227
-
228
- if (walletType === "named") {
229
- return await namedWallet(name, walletType, network);
230
- }
231
-
232
- const walletClass = walletClassMap[walletType][network]();
233
- const wallet = new walletClass();
234
- wallet.walletType = walletType;
235
-
236
- await (wallet as any).fromId(walletId);
237
- return wallet;
238
- }
@@ -1,18 +0,0 @@
1
- export enum WalletTypeEnum {
2
- Named = "named",
3
- Wif = "wif",
4
- Seed = "seed",
5
- Hd = "hd",
6
- Watch = "watch",
7
- PrivateKey = "privkey",
8
- }
9
-
10
- export enum FeePaidByEnum {
11
- change = "change",
12
- first = "firstOutput",
13
- any = "anyOutputs",
14
- last = "lastOutput",
15
- changeThenFirst = "changeThenFirst",
16
- changeThenAny = "changeThenAny",
17
- changeThenLast = "changeThenLast",
18
- }
@@ -1,102 +0,0 @@
1
- import { WalletTypeEnum, FeePaidByEnum } from "./enum.js";
2
- import { NetworkEnum } from "../enum.js";
3
- import { ElectrumRawTransaction } from "../network/interface.js";
4
- import { Utxo } from "../interface.js";
5
-
6
- export interface WalletRequestI {
7
- name?: string;
8
- network?: string;
9
- type?: WalletTypeEnum;
10
- platform?: string;
11
- walletId?: string;
12
- }
13
-
14
- export interface WalletResponseI {
15
- name: string;
16
- cashaddr?: string;
17
- address?: string;
18
- walletId: string;
19
- network?: NetworkEnum;
20
- wif?: string;
21
- privkey?: string;
22
- seed?: string;
23
- derivationPath?: string;
24
- parentDerivationPath?: string;
25
- }
26
-
27
- export interface WalletInfoI {
28
- cashaddr?: string;
29
- tokenaddr?: string;
30
- isTestnet?: boolean;
31
- name?: string;
32
- network: NetworkEnum;
33
- seed?: string;
34
- derivationPath?: string;
35
- parentDerivationPath?: string;
36
- parentXPubKey?: string;
37
- publicKey?: string;
38
- publicKeyHash?: string;
39
- privateKey?: string;
40
- privateKeyWif?: string;
41
- walletId: string;
42
- walletDbEntry: string;
43
- }
44
-
45
- export interface SendRequestOptionsI {
46
- utxoIds?: string[] | Utxo[];
47
- changeAddress?: string;
48
- slpSemiAware?: boolean; // a flag which requires an utxo to have more than 546 sats to be spendable and counted in the balance
49
- queryBalance?: boolean;
50
- awaitTransactionPropagation?: boolean;
51
- feePaidBy?: FeePaidByEnum;
52
- checkTokenQuantities?: boolean; // true
53
- tokenOperation?: "send" | "genesis" | "mint" | "burn"; // undefined. internal use only
54
- ensureUtxos?: Utxo[]; // ensure these inputs will be consumed in the transaction
55
- buildUnsigned?: boolean; // false
56
- }
57
-
58
- export interface MnemonicI {
59
- seed: string;
60
- derivationPath: string;
61
- parentDerivationPath: string;
62
- }
63
-
64
- export interface WalletI {
65
- // Accessors
66
- getDepositAddress(): string;
67
- getTokenDepositAddress(): string;
68
- // getSeed(): MnemonicI;
69
- // getNetworkProvider(network: NetworkType): any;
70
- // generate(): Promise<this>;
71
-
72
- // Serialization
73
- toString(): string;
74
- toDbString(): string;
75
-
76
- // Static constructors
77
- // fromId(walletId: string): Promise<this>;
78
- // fromSeed(mnemonic: string, derivationPath?: string): Promise<this>;
79
- // newRandom(name: string, dbName?: string): Promise<this>;
80
- // watchOnly(address: string): Promise<this>;
81
- // named(name: string, dbName?: string, forceNew?: boolean): Promise<this>;
82
- // namedExists(name: string, dbName?: string): Promise<boolean>;
83
-
84
- // Funds
85
- getBalance(): Promise<any>;
86
- getMaxAmountToSend(params?: any): Promise<any>;
87
- send(requests: any, options?: any): Promise<any>;
88
- sendMax(address: string, options?: any): Promise<any>;
89
- }
90
-
91
- export interface WaitForTransactionOptions {
92
- getTransactionInfo?: boolean;
93
- getBalance?: boolean;
94
- txHash?: string;
95
- }
96
-
97
- export interface WaitForTransactionResponse {
98
- transactionInfo?: ElectrumRawTransaction;
99
- balance?: bigint;
100
- }
101
-
102
- export type CancelFn = () => Promise<void>;
@@ -1,24 +0,0 @@
1
- import { UtxoId } from "../interface";
2
- import { fromUtxoId, toUtxoId } from "./model";
3
-
4
- test("Should serialize utxo", () => {
5
- const utxo = toUtxoId({
6
- txid: "this",
7
- vout: 42,
8
- }).toString();
9
- expect(utxo).toBe("this:42");
10
- });
11
-
12
- test("Should deserialize utxo", () => {
13
- const utxo = fromUtxoId("this:42:1");
14
- expect(utxo.txid).toBe("this");
15
- expect(utxo.vout).toBe(42);
16
- });
17
-
18
- test("Should deserialize utxo", () => {
19
- const u = {
20
- txid: "this",
21
- vout: 42,
22
- } as UtxoId;
23
- expect(toUtxoId(u)).toBe("this:42");
24
- });