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,1411 +0,0 @@
1
- import { RegTestWallet, TestNetWallet } from "./Wif";
2
- import { initProviders, disconnectProviders } from "../network/Connection";
3
- import {
4
- SendRequest,
5
- SendResponse,
6
- TokenMintRequest,
7
- TokenSendRequest,
8
- } from "./model";
9
- import { NFTCapability } from "../interface";
10
- import {
11
- binToHex,
12
- binsAreEqual,
13
- decodeTransaction,
14
- hexToBin,
15
- utf8ToBin,
16
- } from "@bitauth/libauth";
17
- import { convert, delay } from "../util";
18
- import { Config } from "../config";
19
- import json from "../test/json.test";
20
-
21
- beforeAll(async () => {
22
- await initProviders();
23
- });
24
- afterAll(async () => {
25
- await disconnectProviders();
26
- });
27
-
28
- describe(`Test cashtokens`, () => {
29
- test("Test chipnet request", async () => {
30
- const wallet = await TestNetWallet.watchOnly(
31
- "bchtest:pzszr88euuuy87uarx9krcuh5psy4zzghsm2033xk4"
32
- );
33
- const utxos = await wallet.getTokenUtxos();
34
- expect(utxos[0].token?.category).toBeDefined();
35
- });
36
-
37
- test("Test token genesis and max amount to send", async () => {
38
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
39
- const bob = await RegTestWallet.newRandom();
40
- await alice.send([
41
- [bob.cashaddr!, BigInt(await convert(0.101, "bch", "sat"))],
42
- ]);
43
- const genesisResponse = await bob.tokenGenesis({
44
- amount: 100n,
45
- });
46
-
47
- const maxAmountToSend = await bob.getMaxAmountToSend();
48
- await bob.send([[alice.cashaddr!, maxAmountToSend]]);
49
- expect(await bob.getBalance()).toBe(0n + 1000n);
50
- });
51
-
52
- test("Test tokens will not be burned when sending bch value", async () => {
53
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
54
- const bob = await RegTestWallet.newRandom();
55
- const genesisResponse = await alice.tokenGenesis({
56
- amount: 100n,
57
- });
58
-
59
- const category = genesisResponse.categories![0];
60
- const tokenBalance = await alice.getTokenBalance(category);
61
- expect(tokenBalance).toBe(100n);
62
- const tokenUtxos = await alice.getTokenUtxos(category);
63
- expect(tokenUtxos.length).toBe(1);
64
- await alice.send([
65
- new SendRequest({
66
- cashaddr: bob.cashaddr!,
67
- value: 5000n,
68
- }),
69
- new TokenSendRequest({
70
- cashaddr: bob.cashaddr!,
71
- amount: 25n,
72
- category: category,
73
- }),
74
- ]);
75
- expect(await bob.getTokenBalance(category)).toBe(25n);
76
- expect(await bob.getBalance()).toBe(5000n + 1000n);
77
-
78
- await bob.send(
79
- new SendRequest({
80
- cashaddr: alice.cashaddr!,
81
- value: 1000n,
82
- })
83
- );
84
- expect(await bob.getTokenBalance(category)).toBe(25n);
85
- expect(await bob.getBalance()).toBe(3780n + 1000n);
86
-
87
- await bob.sendMax(alice.cashaddr!);
88
- expect(await bob.getTokenBalance(category)).toBe(25n);
89
- expect(await bob.getBalance()).toBe(0n + 1000n);
90
- });
91
-
92
- test("Test fungible cashtoken genesis and sending", async () => {
93
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
94
- const bob = await RegTestWallet.newRandom();
95
- const genesisResponse = await alice.tokenGenesis({
96
- amount: 300n,
97
- });
98
-
99
- const category = genesisResponse.categories![0];
100
- const tokenBalance = await alice.getTokenBalance(category);
101
- expect(tokenBalance).toBe(300n);
102
- const tokenUtxos = await alice.getTokenUtxos(category);
103
- expect(tokenUtxos.length).toBe(1);
104
- const response = await alice.send([
105
- new TokenSendRequest({
106
- cashaddr: bob.cashaddr!,
107
- amount: 25n,
108
- category: category,
109
- }),
110
- new TokenSendRequest({
111
- cashaddr: alice.cashaddr!,
112
- amount: 25n,
113
- category: category,
114
- }),
115
- new SendRequest({
116
- cashaddr: bob.cashaddr!,
117
- value: 20000n,
118
- }),
119
- ]);
120
- const newTokenUtxos = await alice.getTokenUtxos(category);
121
- expect(newTokenUtxos.length).toBe(2);
122
- expect(await alice.getTokenBalance(category)).toBe(275n);
123
- expect(await bob.getTokenBalance(category)).toBe(25n);
124
- expect(await bob.getNftTokenBalance(category)).toBe(0);
125
- expect((await bob.getAllNftTokenBalances())[category] || 0).toBe(0);
126
-
127
- await alice.send([
128
- new TokenSendRequest({
129
- cashaddr: bob.cashaddr!,
130
- amount: 75n,
131
- category: category,
132
- }),
133
- new TokenSendRequest({
134
- cashaddr: bob.cashaddr!,
135
- amount: 100n,
136
- category: category,
137
- }),
138
- new TokenSendRequest({
139
- cashaddr: bob.cashaddr!,
140
- amount: 100n,
141
- category: category,
142
- }),
143
- ]);
144
-
145
- expect(await alice.getTokenBalance(category)).toBe(0n);
146
- expect(await bob.getTokenBalance(category)).toBe(300n);
147
-
148
- await bob.tokenBurn({
149
- category: category,
150
- amount: 99n,
151
- });
152
-
153
- expect(await alice.getTokenBalance(category)).toBe(0n);
154
- expect(await bob.getTokenBalance(category)).toBe(201n);
155
-
156
- await bob.tokenBurn({
157
- category: category,
158
- amount: 200n,
159
- });
160
-
161
- expect(await bob.getTokenBalance(category)).toBe(1n);
162
-
163
- await bob.tokenBurn({
164
- category: category,
165
- amount: 1n,
166
- });
167
- expect(await bob.getTokenBalance(category)).toBe(0n);
168
- });
169
-
170
- test("Test NFT cashtoken genesis and sending", async () => {
171
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
172
- const bob = await RegTestWallet.newRandom();
173
- const genesisResponse = await alice.tokenGenesis({
174
- cashaddr: alice.cashaddr!,
175
- nft: {
176
- capability: NFTCapability.mutable,
177
- commitment: "abcd",
178
- },
179
- });
180
-
181
- const category = genesisResponse.categories![0];
182
- const tokenBalance = await alice.getTokenBalance(category);
183
- expect(tokenBalance).toBe(0n);
184
- const nftTokenBalance = await alice.getNftTokenBalance(category);
185
- expect(nftTokenBalance).toBe(1);
186
- const tokenUtxos = await alice.getTokenUtxos(category);
187
- expect(tokenUtxos.length).toBe(1);
188
- const response = await alice.send([
189
- new TokenSendRequest({
190
- cashaddr: bob.cashaddr!,
191
- category: category,
192
- nft: {
193
- capability: NFTCapability.mutable,
194
- commitment: "abcd",
195
- },
196
- }),
197
- ]);
198
- expect(await alice.getTokenBalance(category)).toBe(0n);
199
- expect(await alice.getNftTokenBalance(category)).toBe(0);
200
- const newTokenUtxos = await alice.getTokenUtxos(category);
201
- expect(newTokenUtxos.length).toBe(0);
202
-
203
- expect(await bob.getTokenBalance(category)).toBe(0n);
204
- const bobTokenUtxos = await bob.getTokenUtxos(category);
205
- expect(bobTokenUtxos.length).toBe(1);
206
- expect(category).toEqual(response.categories![0]);
207
- expect(bobTokenUtxos[0].token?.nft?.commitment).toEqual("abcd");
208
- });
209
-
210
- test("Test immutable NFT cashtoken genesis and sending, error on mutation", async () => {
211
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
212
- const genesisResponse = await alice.tokenGenesis({
213
- cashaddr: alice.cashaddr!,
214
- nft: {
215
- capability: NFTCapability.none,
216
- commitment: "abcd",
217
- },
218
- });
219
-
220
- const category = genesisResponse.categories![0];
221
- const tokenBalance = await alice.getTokenBalance(category);
222
- expect(tokenBalance).toBe(0n);
223
- const tokenUtxos = await alice.getTokenUtxos(category);
224
- expect(tokenUtxos.length).toBe(1);
225
- await expect(
226
- alice.send([
227
- new TokenSendRequest({
228
- cashaddr: alice.cashaddr!,
229
- category: category,
230
- nft: {
231
- capability: NFTCapability.none,
232
- commitment: "abcd02",
233
- },
234
- }),
235
- ])
236
- ).rejects.toThrow("No suitable token utxos available to send token");
237
- });
238
-
239
- test("Test mutable NFT cashtoken genesis and mutation", async () => {
240
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
241
- const genesisResponse = await alice.tokenGenesis({
242
- cashaddr: alice.cashaddr!,
243
- nft: {
244
- capability: NFTCapability.mutable,
245
- commitment: "abcd",
246
- },
247
- });
248
-
249
- const category = genesisResponse.categories![0];
250
- const tokenBalance = await alice.getTokenBalance(category);
251
- expect(tokenBalance).toBe(0n);
252
- const tokenUtxos = await alice.getTokenUtxos(category);
253
- expect(tokenUtxos.length).toBe(1);
254
- const response = await alice.send([
255
- new TokenSendRequest({
256
- cashaddr: alice.cashaddr!,
257
- category: category,
258
- nft: {
259
- capability: NFTCapability.mutable,
260
- commitment: "abcd02",
261
- },
262
- }),
263
- ]);
264
- expect(await alice.getTokenBalance(category)).toBe(0n);
265
- const newTokenUtxos = await alice.getTokenUtxos(category);
266
- expect(newTokenUtxos.length).toBe(1);
267
- expect(category).toEqual(response.categories![0]);
268
- expect(newTokenUtxos[0].token?.nft?.commitment).toEqual("abcd02");
269
- });
270
-
271
- test("Test minting NFT cashtoken genesis and minting", async () => {
272
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
273
- const genesisResponse = await alice.tokenGenesis({
274
- cashaddr: alice.cashaddr!,
275
- nft: {
276
- capability: NFTCapability.minting,
277
- commitment: "abcd",
278
- },
279
- });
280
-
281
- const category = genesisResponse.categories![0];
282
- const tokenBalance = await alice.getTokenBalance(category);
283
- expect(tokenBalance).toBe(0n);
284
- const tokenUtxos = await alice.getTokenUtxos(category);
285
- expect(tokenUtxos.length).toBe(1);
286
- const response = await alice.tokenMint(category, [
287
- new TokenMintRequest({
288
- cashaddr: alice.cashaddr!,
289
- nft: {
290
- commitment: "test",
291
- capability: NFTCapability.none,
292
- },
293
- }),
294
- new TokenMintRequest({
295
- cashaddr: alice.cashaddr!,
296
- nft: {
297
- commitment: "test2",
298
- capability: NFTCapability.none,
299
- },
300
- }),
301
- ]);
302
- expect(await alice.getTokenBalance(category)).toBe(0n);
303
- const newTokenUtxos = await alice.getTokenUtxos(category);
304
- expect(newTokenUtxos.length).toBe(3);
305
- expect(category).toEqual(response.categories![0]);
306
- });
307
-
308
- test("Test minting semifungible tokens and sending them", async () => {
309
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
310
- const genesisResponse = await alice.tokenGenesis({
311
- cashaddr: alice.cashaddr!,
312
- nft: {
313
- capability: NFTCapability.minting,
314
- commitment: "abcd",
315
- },
316
- });
317
-
318
- const category = genesisResponse.categories![0];
319
-
320
- // mint 2 NFTs, amount reducing
321
- const response = await alice.tokenMint(category, [
322
- new TokenMintRequest({
323
- cashaddr: alice.cashaddr!,
324
- nft: {
325
- capability: NFTCapability.none,
326
- commitment: "0a",
327
- },
328
- }),
329
- new TokenMintRequest({
330
- cashaddr: alice.cashaddr!,
331
- nft: {
332
- capability: NFTCapability.none,
333
- commitment: "0a",
334
- },
335
- }),
336
- ]);
337
- const newTokenUtxos = await alice.getTokenUtxos(category);
338
- expect(newTokenUtxos.length).toBe(3);
339
- expect(category).toEqual(response.categories![0]);
340
-
341
- const bob = await RegTestWallet.newRandom();
342
- await alice.send([
343
- new TokenSendRequest({
344
- cashaddr: bob.cashaddr!,
345
- category: category,
346
- nft: {
347
- capability: NFTCapability.none,
348
- commitment: "0a",
349
- },
350
- }),
351
- new TokenSendRequest({
352
- cashaddr: bob.cashaddr!,
353
- category: category,
354
- nft: {
355
- capability: NFTCapability.none,
356
- commitment: "0a",
357
- },
358
- }),
359
- ]);
360
-
361
- expect((await alice.getTokenUtxos(category)).length).toBe(1);
362
- const bobTokenUtxos = await bob.getTokenUtxos(category);
363
- expect(bobTokenUtxos.length).toBe(2);
364
- expect(category).toEqual(response.categories![0]);
365
- });
366
-
367
- test("Test minting NFT and optionally burning FT cashtoken", async () => {
368
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
369
- const genesisResponse = await alice.tokenGenesis({
370
- cashaddr: alice.cashaddr!,
371
- amount: 4n,
372
- nft: {
373
- capability: NFTCapability.minting,
374
- commitment: "abcd",
375
- },
376
- });
377
-
378
- const category = genesisResponse.categories![0];
379
- const tokenBalance = await alice.getTokenBalance(category);
380
- expect(tokenBalance).toBe(4n);
381
- const tokenUtxos = await alice.getTokenUtxos(category);
382
- expect(tokenUtxos.length).toBe(1);
383
-
384
- // mint 2 NFTs, amount reducing
385
- const response = await alice.tokenMint(
386
- category,
387
- [
388
- new TokenMintRequest({
389
- cashaddr: alice.cashaddr!,
390
- nft: {
391
- capability: NFTCapability.none,
392
- commitment: "0a",
393
- },
394
- }),
395
- new TokenMintRequest({
396
- cashaddr: alice.cashaddr!,
397
- nft: {
398
- capability: NFTCapability.none,
399
- commitment: "0b",
400
- },
401
- }),
402
- ],
403
- true
404
- );
405
- expect(await alice.getTokenBalance(category)).toBe(2n);
406
- const newTokenUtxos = await alice.getTokenUtxos(category);
407
- expect(newTokenUtxos.length).toBe(3);
408
- expect(category).toEqual(response.categories![0]);
409
-
410
- // mint 2 more NFTs without amount reducing
411
- const ftResponse = await alice.tokenMint(
412
- category,
413
- [
414
- new TokenMintRequest({
415
- cashaddr: alice.cashaddr!,
416
- nft: {
417
- capability: NFTCapability.none,
418
- commitment: "0c",
419
- },
420
- }),
421
- new TokenMintRequest({
422
- cashaddr: alice.cashaddr!,
423
- nft: {
424
- capability: NFTCapability.none,
425
- commitment: "0d",
426
- },
427
- }),
428
- ],
429
- false
430
- );
431
- expect(await alice.getTokenBalance(category)).toBe(2n);
432
- const ftTokenUtxos = await alice.getTokenUtxos(category);
433
- expect(ftTokenUtxos.length).toBe(5);
434
- expect(category).toEqual(ftResponse.categories![0]);
435
-
436
- // we are going to hit amount -1, when minting 3 more NFTs
437
- // check that it will stop at 0
438
- const ft2Response = await alice.tokenMint(
439
- category,
440
- [
441
- new TokenMintRequest({
442
- cashaddr: alice.cashaddr!,
443
- nft: {
444
- capability: NFTCapability.none,
445
- commitment: "0a",
446
- },
447
- }),
448
- new TokenMintRequest({
449
- cashaddr: alice.cashaddr!,
450
- nft: {
451
- capability: NFTCapability.none,
452
- commitment: "0a",
453
- },
454
- }),
455
- new TokenMintRequest({
456
- cashaddr: alice.cashaddr!,
457
- nft: {
458
- capability: NFTCapability.none,
459
- commitment: "0a",
460
- },
461
- }),
462
- ],
463
- true
464
- );
465
- expect(await alice.getTokenBalance(category)).toBe(0n);
466
- const ft2TokenUtxos = await alice.getTokenUtxos(category);
467
- expect(ft2TokenUtxos.length).toBe(8);
468
- expect(category).toEqual(ft2Response.categories![0]);
469
- });
470
-
471
- test("Test explicit burning of FT", async () => {
472
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
473
- const genesisResponse = await alice.tokenGenesis({
474
- cashaddr: alice.cashaddr!,
475
- amount: 4n,
476
- });
477
-
478
- const category = genesisResponse.categories![0];
479
- const tokenBalance = await alice.getTokenBalance(category);
480
- expect(tokenBalance).toBe(4n);
481
- const tokenUtxos = await alice.getTokenUtxos(category);
482
- expect(tokenUtxos.length).toBe(1);
483
-
484
- // burn 5 FT
485
- const response = await alice.tokenBurn(
486
- {
487
- category: category,
488
- amount: 5n,
489
- },
490
- "burn"
491
- );
492
-
493
- const rawTx = await alice.provider!.getRawTransactionObject(response.txId!);
494
- expect(rawTx!.vout.length).toEqual(3);
495
- expect(rawTx!.vout[0].scriptPubKey.type).toEqual("nulldata");
496
- expect(rawTx!.vout[0].scriptPubKey.hex).toContain(
497
- binToHex(utf8ToBin("burn"))
498
- );
499
- expect(await alice.getTokenBalance(category)).toBe(0n);
500
- const newTokenUtxos = await alice.getTokenUtxos(category);
501
- expect(newTokenUtxos.length).toBe(0);
502
- expect(category).toEqual(response.categories![0]);
503
- });
504
-
505
- test("Test explicit burning of FT and NFT", async () => {
506
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
507
- const genesisResponse = await alice.tokenGenesis({
508
- cashaddr: alice.cashaddr!,
509
- amount: 4n,
510
- nft: {
511
- capability: NFTCapability.minting,
512
- commitment: "abcd",
513
- },
514
- });
515
-
516
- const category = genesisResponse.categories![0];
517
- const tokenBalance = await alice.getTokenBalance(category);
518
- expect(tokenBalance).toBe(4n);
519
- const tokenUtxos = await alice.getTokenUtxos(category);
520
- expect(tokenUtxos.length).toBe(1);
521
-
522
- // burn 1 FT
523
- const response = await alice.tokenBurn(
524
- {
525
- category: category,
526
- amount: 1n,
527
- nft: {
528
- capability: NFTCapability.minting,
529
- commitment: "abcd",
530
- },
531
- },
532
- "burn"
533
- );
534
-
535
- const rawTx = await alice.provider!.getRawTransactionObject(response.txId!);
536
- expect(rawTx!.vout.length).toEqual(3);
537
- expect(rawTx!.vout[0].scriptPubKey.type).toEqual("nulldata");
538
- expect(rawTx!.vout[0].scriptPubKey.hex).toContain(
539
- binToHex(utf8ToBin("burn"))
540
- );
541
- expect(await alice.getTokenBalance(category)).toBe(3n);
542
- expect((await alice.getAllTokenBalances())[category]).toBe(3n);
543
- const newTokenUtxos = await alice.getTokenUtxos(category);
544
- expect(newTokenUtxos.length).toBe(1);
545
- expect(await alice.getNftTokenBalance(category)).toBe(1);
546
- expect((await alice.getAllNftTokenBalances())[category || 0]).toBe(1);
547
- expect(category).toEqual(response.categories![0]);
548
-
549
- // burn the rest FTs
550
- const ftResponse = await alice.tokenBurn(
551
- {
552
- category: category,
553
- amount: 5n,
554
- nft: {
555
- capability: NFTCapability.minting,
556
- commitment: "abcd",
557
- },
558
- },
559
- "burn"
560
- );
561
- expect(await alice.getTokenBalance(category)).toBe(0n);
562
- const ftTokenUtxos = await alice.getTokenUtxos(category);
563
- expect(ftTokenUtxos.length).toBe(1);
564
- expect(category).toEqual(ftResponse.categories![0]);
565
-
566
- // burn the NFT too
567
- const nftResponse = await alice.tokenBurn(
568
- {
569
- category: category,
570
- nft: {
571
- capability: NFTCapability.minting,
572
- commitment: "abcd",
573
- },
574
- },
575
- "burn"
576
- );
577
- expect(await alice.getTokenBalance(category)).toBe(0n);
578
- expect((await alice.getAllTokenBalances())[category] || 0n).toBe(0n);
579
- const nftTokenUtxos = await alice.getTokenUtxos(category);
580
- expect(nftTokenUtxos.length).toBe(0);
581
- expect(category).toEqual(nftResponse.categories![0]);
582
- expect(await alice.getNftTokenBalance(category)).toBe(0);
583
- expect((await alice.getAllNftTokenBalances())[category] || 0).toBe(0);
584
- });
585
-
586
- test("Test cashtoken satoshi values and fee calculations", async () => {
587
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
588
- const bob = await RegTestWallet.newRandom();
589
- const genesisResponse = await alice.tokenGenesis({
590
- amount: 100n,
591
- value: 7000n,
592
- cashaddr: bob.cashaddr!,
593
- });
594
-
595
- const category = genesisResponse.categories![0];
596
- const tokenBalance = await bob.getTokenBalance(category);
597
- expect(tokenBalance).toBe(100n);
598
- const tokenUtxos = await bob.getTokenUtxos(category);
599
- expect(tokenUtxos.length).toBe(1);
600
- expect(tokenUtxos[0].satoshis).toBe(7000n);
601
-
602
- // lower the token satoshi value
603
- const response = await bob.send([
604
- new TokenSendRequest({
605
- cashaddr: bob.cashaddr!,
606
- amount: 100n,
607
- category: category,
608
- value: 1500n,
609
- }),
610
- ]);
611
- let newTokenUtxos = await bob.getTokenUtxos(category);
612
- expect(newTokenUtxos.length).toBe(1);
613
- expect(await bob.getTokenBalance(category)).toBe(100n);
614
-
615
- let bobUtxos = await bob.getUtxos();
616
- expect(bobUtxos.length).toBe(2);
617
- expect(bobUtxos[0].satoshis).toBe(1500n);
618
- expect(bobUtxos[1].satoshis).toBe(5245n);
619
-
620
- // raise the token satoshi value
621
- await bob.send([
622
- new TokenSendRequest({
623
- cashaddr: bob.cashaddr!,
624
- amount: 100n,
625
- category: category,
626
- value: 3000n,
627
- }),
628
- ]);
629
- newTokenUtxos = await bob.getTokenUtxos(category);
630
- expect(newTokenUtxos.length).toBe(1);
631
- expect(await bob.getTokenBalance(category)).toBe(100n);
632
-
633
- bobUtxos = await bob.getUtxos();
634
- expect(bobUtxos.length).toBe(2);
635
- expect(bobUtxos[0].satoshis).toBe(3000n);
636
- expect(bobUtxos[1].satoshis).toBe(3349n);
637
- });
638
-
639
- test("Test cashtoken waiting and watching", async () => {
640
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
641
- const bob = await RegTestWallet.newRandom();
642
-
643
- const genesisResponse = await alice.tokenGenesis({
644
- amount: 100n,
645
- value: 5000n,
646
- nft: {
647
- capability: NFTCapability.minting,
648
- commitment: "test",
649
- },
650
- cashaddr: alice.cashaddr!,
651
- });
652
-
653
- const category = genesisResponse.categories![0];
654
- const tokenBalance = await alice.getTokenBalance(category);
655
- expect(tokenBalance).toBe(100n);
656
- const tokenUtxos = await alice.getTokenUtxos(category);
657
- expect(tokenUtxos.length).toBe(1);
658
- expect(tokenUtxos[0].satoshis).toBe(5000n);
659
-
660
- let seenBalance = 0n;
661
- let sendResponse: SendResponse = {};
662
- setTimeout(
663
- async () =>
664
- (sendResponse = await alice.send([
665
- new TokenSendRequest({
666
- cashaddr: bob.cashaddr!,
667
- amount: 100n,
668
- category: category,
669
- value: 1500n,
670
- nft: {
671
- capability: NFTCapability.minting,
672
- commitment: "test",
673
- },
674
- }),
675
- ])),
676
- 0
677
- );
678
-
679
- const cancel = await bob.watchTokenBalance(category, (balance) => {
680
- seenBalance = balance;
681
- });
682
-
683
- let bobTxId = ".";
684
- const txCancel = await bob.watchTokenTransactions((tx) => {
685
- bobTxId = tx.txid;
686
- });
687
-
688
- const balance = await bob.waitForTokenBalance(category, 100n);
689
- await delay(500);
690
- expect(balance).toBe(100n);
691
- expect(seenBalance).toBe(100n);
692
- expect(sendResponse.txId).toBe(bobTxId);
693
- await cancel();
694
- await txCancel();
695
- await delay(500);
696
- });
697
-
698
- test("Test double genesis should not burn tokens", async () => {
699
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
700
- const bob = await RegTestWallet.newRandom();
701
-
702
- // prepare inputs for two token geneses
703
- await alice.send({ cashaddr: bob.cashaddr!, value: 10000n });
704
- await alice.send({ cashaddr: bob.cashaddr!, value: 10000n });
705
-
706
- const genesisResponse = await bob.tokenGenesis({
707
- amount: 100n,
708
- });
709
-
710
- const category = genesisResponse.categories![0];
711
- const tokenBalance = await bob.getTokenBalance(category);
712
-
713
- expect(tokenBalance).toBe(100n);
714
- const tokenUtxos = await bob.getTokenUtxos(category);
715
- expect(tokenUtxos.length).toBe(1);
716
-
717
- const genesis2Response = await bob.tokenGenesis({
718
- amount: 200n,
719
- });
720
-
721
- const category2 = genesis2Response.categories![0];
722
- const tokenBalance2 = await bob.getTokenBalance(category2);
723
- expect(tokenBalance2).toBe(200n);
724
- const tokenUtxos2 = await bob.getTokenUtxos(category2);
725
- expect(tokenUtxos2.length).toBe(1);
726
-
727
- expect((await bob.getTokenUtxos()).length).toBe(2);
728
- });
729
-
730
- test("Test sending tokens should not burn tokens", async () => {
731
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
732
- const bob = await RegTestWallet.newRandom();
733
-
734
- // prepare inputs for two token geneses
735
- await alice.send({ cashaddr: bob.cashaddr!, value: 10000n });
736
- await alice.send({ cashaddr: bob.cashaddr!, value: 10000n });
737
-
738
- const genesisResponse = await bob.tokenGenesis({
739
- amount: 100n,
740
- });
741
-
742
- const category = genesisResponse.categories![0];
743
- const tokenBalance = await bob.getTokenBalance(category);
744
-
745
- expect(tokenBalance).toBe(100n);
746
- const tokenUtxos = await bob.getTokenUtxos(category);
747
- expect(tokenUtxos.length).toBe(1);
748
-
749
- const genesis2Response = await bob.tokenGenesis({
750
- amount: 200n,
751
- });
752
-
753
- const category2 = genesis2Response.categories![0];
754
- const tokenBalance2 = await bob.getTokenBalance(category2);
755
- expect(tokenBalance2).toBe(200n);
756
- const tokenUtxos2 = await bob.getTokenUtxos(category2);
757
- expect(tokenUtxos2.length).toBe(1);
758
-
759
- expect((await bob.getTokenUtxos()).length).toBe(2);
760
-
761
- const charlie = await RegTestWallet.newRandom();
762
- await bob.send({
763
- cashaddr: charlie.cashaddr!,
764
- category: category,
765
- amount: 50n,
766
- });
767
- expect((await bob.getTokenUtxos()).length).toBe(2);
768
- expect((await charlie.getTokenUtxos()).length).toBe(1);
769
- expect(await bob.getTokenBalance(category)).toBe(50n);
770
- expect(await charlie.getTokenBalance(category)).toBe(50n);
771
- });
772
-
773
- test("Test sending bch should not burn tokens", async () => {
774
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
775
- const bob = await RegTestWallet.newRandom();
776
-
777
- // prepare inputs for two token geneses
778
- await alice.send({ cashaddr: bob.cashaddr!, value: 10000n });
779
-
780
- const genesisResponse = await bob.tokenGenesis({
781
- amount: 100n,
782
- });
783
-
784
- const category = genesisResponse.categories![0];
785
- const tokenBalance = await bob.getTokenBalance(category);
786
-
787
- expect(tokenBalance).toBe(100n);
788
- const tokenUtxos = await bob.getTokenUtxos(category);
789
- expect(tokenUtxos.length).toBe(1);
790
-
791
- await bob.send({ cashaddr: alice.cashaddr!, value: 1000n });
792
-
793
- const tokenBalance2 = await bob.getTokenBalance(category);
794
- expect(tokenBalance2).toBe(100n);
795
- const tokenUtxos2 = await bob.getTokenUtxos(category);
796
- expect(tokenUtxos2.length).toBe(1);
797
- });
798
-
799
- test("Test sending tokens should not burn tokens", async () => {
800
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
801
- const bob = await RegTestWallet.newRandom();
802
- const charlie = await RegTestWallet.newRandom();
803
- // prepare inputs for two token geneses
804
- await alice.send([
805
- { cashaddr: bob.cashaddr!, value: 10000n },
806
- { cashaddr: charlie.cashaddr!, value: 10000n },
807
- ]);
808
-
809
- const genesisResponse = await bob.tokenGenesis({
810
- amount: 1001n,
811
- });
812
-
813
- const category = genesisResponse.categories![0];
814
-
815
- const tokenBalance = await bob.getTokenBalance(category);
816
- expect(tokenBalance).toBe(1001n);
817
- const tokenUtxos = await bob.getTokenUtxos(category);
818
- expect(tokenUtxos.length).toBe(1);
819
-
820
- await bob.send({ cashaddr: alice.cashaddr!, value: 1000n });
821
-
822
- await bob.send([
823
- {
824
- cashaddr: charlie.cashaddr!,
825
- category: category,
826
- amount: 5n,
827
- },
828
- {
829
- cashaddr: charlie.cashaddr!,
830
- category: category,
831
- amount: 501n,
832
- },
833
- {
834
- cashaddr: charlie.cashaddr!,
835
- category: category,
836
- amount: 95n,
837
- },
838
- {
839
- cashaddr: charlie.cashaddr!,
840
- category: category,
841
- amount: 100n,
842
- },
843
- {
844
- cashaddr: charlie.cashaddr!,
845
- category: category,
846
- amount: 300n,
847
- },
848
- ]);
849
-
850
- const tokenBalance2 = await bob.getTokenBalance(category);
851
- expect(tokenBalance2).toBe(0n);
852
- const tokenUtxos2 = await bob.getTokenUtxos(category);
853
- expect(tokenUtxos2.length).toBe(0);
854
-
855
- const tokenBalance3 = await charlie.getTokenBalance(category);
856
- expect(tokenBalance3).toBe(1001n);
857
- const tokenUtxos3 = await charlie.getTokenUtxos(category);
858
- expect(tokenUtxos3.length).toBe(5);
859
-
860
- // charlie sends some from one of this utxos
861
- await charlie.send([
862
- {
863
- cashaddr: bob.cashaddr!,
864
- category: category,
865
- amount: 50n,
866
- },
867
- ]);
868
- const tokenBalance4 = await charlie.getTokenBalance(category);
869
- expect(tokenBalance4).toBe(951n);
870
- const tokenUtxos4 = await charlie.getTokenUtxos(category);
871
- expect(tokenUtxos4.length).toBe(1);
872
-
873
- const tokenBalance5 = await bob.getTokenBalance(category);
874
- expect(tokenBalance5).toBe(50n);
875
- const tokenUtxos5 = await bob.getTokenUtxos(category);
876
- expect(tokenUtxos5.length).toBe(1);
877
- });
878
-
879
- test("Test minting NFTs not burn tokens", async () => {
880
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
881
- const bob = await RegTestWallet.newRandom();
882
- const charlie = await RegTestWallet.newRandom();
883
-
884
- // prepare inputs for two token geneses
885
- await alice.send({ cashaddr: bob.cashaddr!, value: 10000n });
886
-
887
- const genesisResponse = await bob.tokenGenesis({
888
- nft: {
889
- capability: "minting",
890
- commitment: "",
891
- },
892
- });
893
-
894
- const category = genesisResponse.categories![0];
895
-
896
- expect(await bob.getTokenBalance(category)).toBe(0n);
897
- expect(await bob.getNftTokenBalance(category)).toBe(1);
898
- expect((await bob.getTokenUtxos(category)).length).toBe(1);
899
-
900
- await bob.tokenMint(category, {
901
- nft: {
902
- capability: "none",
903
- commitment: "0a",
904
- },
905
- });
906
-
907
- expect(await bob.getTokenBalance(category)).toBe(0n);
908
- expect(await bob.getNftTokenBalance(category)).toBe(2);
909
- expect((await bob.getTokenUtxos(category)).length).toBe(2);
910
-
911
- await bob.send(
912
- new TokenSendRequest({
913
- category: category,
914
- nft: {
915
- capability: "none",
916
- commitment: "0a",
917
- },
918
- cashaddr: charlie.cashaddr!,
919
- })
920
- );
921
- expect(await bob.getTokenBalance(category)).toBe(0n);
922
- expect(await bob.getNftTokenBalance(category)).toBe(1);
923
- expect((await bob.getTokenUtxos(category)).length).toBe(1);
924
-
925
- expect(await charlie.getTokenBalance(category)).toBe(0n);
926
- expect(await charlie.getNftTokenBalance(category)).toBe(1);
927
- expect((await charlie.getTokenUtxos(category)).length).toBe(1);
928
- });
929
-
930
- test("Test sending NFTs after burning minting token", async () => {
931
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
932
- const bob = await RegTestWallet.newRandom();
933
- const charlie = await RegTestWallet.newRandom();
934
-
935
- // prepare inputs for two token geneses
936
- await alice.send({ cashaddr: bob.cashaddr!, value: 10000n });
937
-
938
- const genesisResponse = await bob.tokenGenesis({
939
- nft: {
940
- capability: "minting",
941
- commitment: "",
942
- },
943
- });
944
-
945
- const category = genesisResponse.categories![0];
946
-
947
- expect(await bob.getTokenBalance(category)).toBe(0n);
948
- expect(await bob.getNftTokenBalance(category)).toBe(1);
949
- expect((await bob.getTokenUtxos(category)).length).toBe(1);
950
-
951
- await bob.tokenMint(category, {
952
- nft: {
953
- capability: "none",
954
- commitment: "0a",
955
- },
956
- });
957
-
958
- expect(await bob.getTokenBalance(category)).toBe(0n);
959
- expect(await bob.getNftTokenBalance(category)).toBe(2);
960
- expect((await bob.getTokenUtxos(category)).length).toBe(2);
961
-
962
- await bob.tokenMint(category, {
963
- nft: {
964
- capability: "none",
965
- commitment: "0b",
966
- },
967
- });
968
-
969
- expect(await bob.getTokenBalance(category)).toBe(0n);
970
- expect(await bob.getNftTokenBalance(category)).toBe(3);
971
- expect((await bob.getTokenUtxos(category)).length).toBe(3);
972
-
973
- await bob.tokenBurn({
974
- category: category,
975
- nft: {
976
- capability: "minting",
977
- commitment: "",
978
- },
979
- });
980
-
981
- expect(await bob.getTokenBalance(category)).toBe(0n);
982
- expect(await bob.getNftTokenBalance(category)).toBe(2);
983
- expect((await bob.getTokenUtxos(category)).length).toBe(2);
984
-
985
- await bob.send(
986
- new TokenSendRequest({
987
- category: category,
988
- nft: {
989
- capability: "none",
990
- commitment: "0a",
991
- },
992
- cashaddr: charlie.cashaddr!,
993
- })
994
- );
995
- expect(await bob.getTokenBalance(category)).toBe(0n);
996
- expect(await bob.getNftTokenBalance(category)).toBe(1);
997
- expect((await bob.getTokenUtxos(category)).length).toBe(1);
998
-
999
- expect(await charlie.getTokenBalance(category)).toBe(0n);
1000
- expect(await charlie.getNftTokenBalance(category)).toBe(1);
1001
- expect((await charlie.getTokenUtxos(category)).length).toBe(1);
1002
-
1003
- await bob.send(
1004
- new TokenSendRequest({
1005
- category: category,
1006
- nft: {
1007
- capability: "none",
1008
- commitment: "0b",
1009
- },
1010
- cashaddr: charlie.cashaddr!,
1011
- })
1012
- );
1013
- expect(await bob.getTokenBalance(category)).toBe(0n);
1014
- expect(await bob.getNftTokenBalance(category)).toBe(0);
1015
- expect((await bob.getTokenUtxos(category)).length).toBe(0);
1016
-
1017
- expect(await charlie.getTokenBalance(category)).toBe(0n);
1018
- expect(await charlie.getNftTokenBalance(category)).toBe(2);
1019
- expect((await charlie.getTokenUtxos(category)).length).toBe(2);
1020
- });
1021
-
1022
- test("Test sending NFTs with empty commitment", async () => {
1023
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
1024
- const bob = await RegTestWallet.newRandom();
1025
- const charlie = await RegTestWallet.newRandom();
1026
-
1027
- // prepare inputs for two token geneses
1028
- await alice.send({ cashaddr: bob.cashaddr!, value: 10000n });
1029
-
1030
- const genesisResponse = await bob.tokenGenesis({
1031
- nft: {
1032
- capability: "minting",
1033
- commitment: "00",
1034
- },
1035
- });
1036
-
1037
- const category = genesisResponse.categories![0];
1038
-
1039
- expect(await bob.getTokenBalance(category)).toBe(0n);
1040
- expect(await bob.getNftTokenBalance(category)).toBe(1);
1041
- expect((await bob.getTokenUtxos(category)).length).toBe(1);
1042
-
1043
- await bob.tokenMint(category, {
1044
- nft: {
1045
- capability: "none",
1046
- commitment: "0a",
1047
- },
1048
- });
1049
-
1050
- expect(await bob.getTokenBalance(category)).toBe(0n);
1051
- expect(await bob.getNftTokenBalance(category)).toBe(2);
1052
- expect((await bob.getTokenUtxos(category)).length).toBe(2);
1053
-
1054
- await bob.tokenMint(category, {
1055
- nft: {
1056
- capability: "none",
1057
- commitment: "0b",
1058
- },
1059
- });
1060
-
1061
- expect(await bob.getTokenBalance(category)).toBe(0n);
1062
- expect(await bob.getNftTokenBalance(category)).toBe(3);
1063
- expect((await bob.getTokenUtxos(category)).length).toBe(3);
1064
-
1065
- await bob.tokenBurn({
1066
- category: category,
1067
- nft: {
1068
- capability: "minting",
1069
- commitment: "00",
1070
- },
1071
- });
1072
-
1073
- expect(await bob.getTokenBalance(category)).toBe(0n);
1074
- expect(await bob.getNftTokenBalance(category)).toBe(2);
1075
- expect((await bob.getTokenUtxos(category)).length).toBe(2);
1076
-
1077
- await expect(
1078
- bob.send(
1079
- new TokenSendRequest({
1080
- category: category,
1081
- nft: {
1082
- capability: "none",
1083
- commitment: "",
1084
- },
1085
- cashaddr: charlie.cashaddr!,
1086
- })
1087
- )
1088
- ).rejects.toThrow(
1089
- "No suitable token utxos available to send token with id"
1090
- );
1091
- });
1092
-
1093
- test("Test splitting and merging FT and NFTs", async () => {
1094
- // handle the cases from the following examples
1095
- // https://bch.loping.net/tx/86f3c86e7e9c6853e489dc7ea85e2192b1b2cce691fa5ff9597e98f045a0bf72
1096
- // https://bch.loping.net/tx/83ef2ab9687c53bb24c3d99f3cc7f0a2c7f23e180c8dbee5c845d1db6725d3e8
1097
-
1098
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
1099
- const bob = await RegTestWallet.newRandom();
1100
-
1101
- await alice.send({ cashaddr: bob.cashaddr!, value: 10000n });
1102
-
1103
- const genesisResponse = await bob.tokenGenesis({
1104
- nft: {
1105
- capability: "none",
1106
- commitment: "0000000e",
1107
- },
1108
- amount: 10000n,
1109
- });
1110
-
1111
- const category = genesisResponse.categories![0];
1112
-
1113
- expect(await bob.getTokenBalance(category)).toBe(10000n);
1114
- expect(await bob.getNftTokenBalance(category)).toBe(1);
1115
- expect((await bob.getTokenUtxos(category)).length).toBe(1);
1116
-
1117
- // explicitly split FT amounts from NFT
1118
- await bob.send([
1119
- new TokenSendRequest({
1120
- cashaddr: bob.cashaddr!,
1121
- category: category,
1122
- nft: {
1123
- capability: "none",
1124
- commitment: "0000000e",
1125
- },
1126
- }),
1127
- new TokenSendRequest({
1128
- cashaddr: bob.cashaddr!,
1129
- category: category,
1130
- amount: 10000n,
1131
- }),
1132
- ]);
1133
-
1134
- expect(await bob.getTokenBalance(category)).toBe(10000n);
1135
- expect(await bob.getNftTokenBalance(category)).toBe(1);
1136
- expect((await bob.getTokenUtxos(category)).length).toBe(2);
1137
-
1138
- // add FT amount to an NFT
1139
- await bob.send([
1140
- new TokenSendRequest({
1141
- cashaddr: bob.cashaddr!,
1142
- category: category,
1143
- amount: 9000n,
1144
- }),
1145
- new TokenSendRequest({
1146
- cashaddr: bob.cashaddr!,
1147
- category: category,
1148
- nft: {
1149
- capability: "none",
1150
- commitment: "0000000e",
1151
- },
1152
- amount: 1000n,
1153
- }),
1154
- ]);
1155
-
1156
- expect(await bob.getTokenBalance(category)).toBe(10000n);
1157
- expect(await bob.getNftTokenBalance(category)).toBe(1);
1158
- expect((await bob.getTokenUtxos(category)).length).toBe(2);
1159
- const tokenUtxos = await bob.getTokenUtxos(category);
1160
- expect(tokenUtxos[0].token?.amount).toBe(9000n);
1161
- expect(tokenUtxos[1].token?.amount).toBe(1000n);
1162
-
1163
- // merge FT and NFT into single utxo
1164
- await bob.send([
1165
- new TokenSendRequest({
1166
- cashaddr: bob.cashaddr!,
1167
- category: category,
1168
- nft: {
1169
- capability: "none",
1170
- commitment: "0000000e",
1171
- },
1172
- amount: 10000n,
1173
- }),
1174
- ]);
1175
-
1176
- expect(await bob.getTokenBalance(category)).toBe(10000n);
1177
- expect(await bob.getNftTokenBalance(category)).toBe(1);
1178
- expect((await bob.getTokenUtxos(category)).length).toBe(1);
1179
- });
1180
-
1181
- test("Should encode unsigned transactions", async () => {
1182
- const aliceWif = `wif:regtest:${process.env.PRIVATE_WIF!}`;
1183
- const aliceWallet = await RegTestWallet.fromId(aliceWif);
1184
- const aliceWatchWallet = await RegTestWallet.watchOnly(
1185
- aliceWallet.cashaddr!
1186
- );
1187
-
1188
- let category;
1189
- {
1190
- const aliceUtxos = await aliceWallet.getUtxos();
1191
-
1192
- const { unsignedTransaction, sourceOutputs, categories } =
1193
- await aliceWatchWallet.tokenGenesis(
1194
- {
1195
- nft: {
1196
- capability: "minting",
1197
- commitment: "00",
1198
- },
1199
- },
1200
- undefined,
1201
- { buildUnsigned: true }
1202
- );
1203
- const encodedTransaction = hexToBin(unsignedTransaction!);
1204
- expect(encodedTransaction.length).toBeGreaterThan(0);
1205
-
1206
- // check transaction was not submitted
1207
- expect(json(aliceUtxos)).toBe(json(await aliceWallet.getUtxos()));
1208
-
1209
- const decoded = decodeTransaction(encodedTransaction);
1210
- if (typeof decoded === "string") {
1211
- throw decoded;
1212
- }
1213
-
1214
- expect(
1215
- binsAreEqual(decoded.inputs[0].unlockingBytecode, Uint8Array.from([]))
1216
- ).toBe(true);
1217
- expect(sourceOutputs!.length).toBe(decoded.inputs.length);
1218
- expect(binToHex(decoded.outputs[0].token?.nft?.commitment!)).toBe("00");
1219
-
1220
- const signed = await aliceWallet.signUnsignedTransaction(
1221
- unsignedTransaction!,
1222
- sourceOutputs!
1223
- );
1224
- await aliceWallet.submitTransaction(signed);
1225
-
1226
- category = categories![0];
1227
-
1228
- expect(await aliceWallet.getNftTokenBalance(category)).toBe(1);
1229
- const tokenUtxos = await aliceWallet.getTokenUtxos(category);
1230
- expect(tokenUtxos.length).toBe(1);
1231
- expect(tokenUtxos[0].token?.nft?.capability).toBe(NFTCapability.minting);
1232
- }
1233
-
1234
- {
1235
- const aliceUtxos = await aliceWallet.getUtxos();
1236
-
1237
- const { unsignedTransaction, sourceOutputs } =
1238
- await aliceWatchWallet.tokenMint(
1239
- category,
1240
- {
1241
- nft: {
1242
- capability: "none",
1243
- commitment: "0a",
1244
- },
1245
- },
1246
- undefined,
1247
- { buildUnsigned: true }
1248
- );
1249
- const encodedTransaction = hexToBin(unsignedTransaction!);
1250
- expect(encodedTransaction.length).toBeGreaterThan(0);
1251
-
1252
- // check transaction was not submitted
1253
- expect(json(aliceUtxos)).toBe(json(await aliceWallet.getUtxos()));
1254
-
1255
- const decoded = decodeTransaction(encodedTransaction);
1256
- if (typeof decoded === "string") {
1257
- throw decoded;
1258
- }
1259
-
1260
- expect(
1261
- binsAreEqual(decoded.inputs[0].unlockingBytecode, Uint8Array.from([]))
1262
- ).toBe(true);
1263
- expect(sourceOutputs!.length).toBe(decoded.inputs.length);
1264
- expect(binToHex(sourceOutputs![0].token?.nft?.commitment!)).toBe("00");
1265
- expect(binToHex(decoded.outputs[0].token?.nft?.commitment!)).toBe("00");
1266
- expect(binToHex(decoded.outputs[1].token?.nft?.commitment!)).toBe("0a");
1267
-
1268
- const signed = await aliceWallet.signUnsignedTransaction(
1269
- unsignedTransaction!,
1270
- sourceOutputs!
1271
- );
1272
- await aliceWallet.submitTransaction(signed);
1273
-
1274
- expect(await aliceWallet.getNftTokenBalance(category)).toBe(2);
1275
- const tokenUtxos = await aliceWallet.getTokenUtxos(category);
1276
- expect(tokenUtxos.length).toBe(2);
1277
- expect(
1278
- tokenUtxos.filter(
1279
- (val) => val.token?.nft?.capability === NFTCapability.minting
1280
- ).length
1281
- ).toBe(1);
1282
- expect(
1283
- tokenUtxos.filter(
1284
- (val) => val.token?.nft?.capability === NFTCapability.none
1285
- ).length
1286
- ).toBe(1);
1287
- }
1288
-
1289
- {
1290
- const aliceUtxos = await aliceWallet.getUtxos();
1291
-
1292
- const { unsignedTransaction, sourceOutputs } =
1293
- await aliceWatchWallet.send(
1294
- [
1295
- new TokenSendRequest({
1296
- category: category,
1297
- nft: {
1298
- capability: "none",
1299
- commitment: "0a",
1300
- },
1301
- cashaddr: aliceWallet.cashaddr!,
1302
- }),
1303
- ],
1304
- { buildUnsigned: true }
1305
- );
1306
- const encodedTransaction = hexToBin(unsignedTransaction!);
1307
- expect(encodedTransaction.length).toBeGreaterThan(0);
1308
-
1309
- // check transaction was not submitted
1310
- expect(json(aliceUtxos)).toBe(json(await aliceWallet.getUtxos()));
1311
-
1312
- const decoded = decodeTransaction(encodedTransaction);
1313
- if (typeof decoded === "string") {
1314
- throw decoded;
1315
- }
1316
-
1317
- expect(
1318
- binsAreEqual(decoded.inputs[0].unlockingBytecode, Uint8Array.from([]))
1319
- ).toBe(true);
1320
- expect(sourceOutputs!.length).toBe(decoded.inputs.length);
1321
- expect(binToHex(sourceOutputs![0].token?.nft?.commitment!)).toBe("0a");
1322
- expect(binToHex(decoded.outputs[0].token?.nft?.commitment!)).toBe("0a");
1323
-
1324
- const signed = await aliceWallet.signUnsignedTransaction(
1325
- unsignedTransaction!,
1326
- sourceOutputs!
1327
- );
1328
- await aliceWallet.submitTransaction(signed);
1329
- expect(await aliceWallet.getNftTokenBalance(category)).toBe(2);
1330
- const tokenUtxos = await aliceWallet.getTokenUtxos(category);
1331
- expect(tokenUtxos.length).toBe(2);
1332
- expect(
1333
- tokenUtxos.filter(
1334
- (val) => val.token?.nft?.capability === NFTCapability.minting
1335
- ).length
1336
- ).toBe(1);
1337
- expect(
1338
- tokenUtxos.filter(
1339
- (val) => val.token?.nft?.capability === NFTCapability.none
1340
- ).length
1341
- ).toBe(1);
1342
- }
1343
- });
1344
-
1345
- test("Test enforcing token addresses", async () => {
1346
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
1347
- const genesisResponse = await alice.tokenGenesis({
1348
- amount: 100n,
1349
- });
1350
- const category = genesisResponse.categories![0];
1351
-
1352
- const previousValue = Config.EnforceCashTokenReceiptAddresses;
1353
-
1354
- const wrap = (addr) => {
1355
- return new Promise((resolve) => {
1356
- resolve(new TokenSendRequest({ cashaddr: addr, category: "" }));
1357
- });
1358
- };
1359
-
1360
- Config.EnforceCashTokenReceiptAddresses = false;
1361
- await expect(wrap(alice.cashaddr)).resolves.not.toThrow();
1362
- await expect(wrap(alice.tokenaddr)).resolves.not.toThrow();
1363
-
1364
- await expect(
1365
- alice.send(
1366
- new TokenSendRequest({
1367
- cashaddr: alice.cashaddr!,
1368
- category: category,
1369
- amount: 1n,
1370
- })
1371
- )
1372
- ).resolves.not.toThrow();
1373
-
1374
- await expect(
1375
- alice.send(
1376
- new TokenSendRequest({
1377
- cashaddr: alice.tokenaddr!,
1378
- category: category,
1379
- amount: 2n,
1380
- })
1381
- )
1382
- ).resolves.not.toThrow();
1383
-
1384
- Config.EnforceCashTokenReceiptAddresses = true;
1385
- await expect(wrap(alice.cashaddr)).rejects.toThrow();
1386
- await expect(wrap(alice.tokenaddr)).resolves.not.toThrow();
1387
-
1388
- await expect(
1389
- (async () =>
1390
- await alice.send(
1391
- new TokenSendRequest({
1392
- cashaddr: alice.cashaddr!,
1393
- category: category,
1394
- amount: 1n,
1395
- })
1396
- ))()
1397
- ).rejects.toThrow();
1398
-
1399
- await expect(
1400
- alice.send(
1401
- new TokenSendRequest({
1402
- cashaddr: alice.tokenaddr!,
1403
- category: category,
1404
- amount: 2n,
1405
- })
1406
- )
1407
- ).resolves.not.toThrow();
1408
-
1409
- Config.EnforceCashTokenReceiptAddresses = previousValue;
1410
- });
1411
- });