mainnet-js 1.1.34 → 2.0.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 (117) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-1.1.34.js → mainnet-2.0.0.js} +25 -337
  3. package/dist/module/db/SqlProvider.d.ts +2 -2
  4. package/dist/module/db/SqlProvider.d.ts.map +1 -1
  5. package/dist/module/db/SqlProvider.js +6 -24
  6. package/dist/module/db/SqlProvider.js.map +1 -1
  7. package/dist/module/index.d.ts +0 -3
  8. package/dist/module/index.d.ts.map +1 -1
  9. package/dist/module/index.js +3 -6
  10. package/dist/module/index.js.map +1 -1
  11. package/dist/module/transaction/Wif.d.ts +4 -8
  12. package/dist/module/transaction/Wif.d.ts.map +1 -1
  13. package/dist/module/transaction/Wif.js +6 -10
  14. package/dist/module/transaction/Wif.js.map +1 -1
  15. package/dist/module/util/deriveCashaddr.d.ts +1 -0
  16. package/dist/module/util/deriveCashaddr.d.ts.map +1 -1
  17. package/dist/module/util/deriveCashaddr.js +8 -1
  18. package/dist/module/util/deriveCashaddr.js.map +1 -1
  19. package/dist/module/wallet/Base.d.ts +1 -1
  20. package/dist/module/wallet/Base.js +1 -1
  21. package/dist/module/wallet/Util.d.ts +2 -2
  22. package/dist/module/wallet/Util.js +2 -2
  23. package/dist/module/wallet/Wif.d.ts +0 -28
  24. package/dist/module/wallet/Wif.d.ts.map +1 -1
  25. package/dist/module/wallet/Wif.js +2 -109
  26. package/dist/module/wallet/Wif.js.map +1 -1
  27. package/dist/module/wallet/createWallet.d.ts +0 -12
  28. package/dist/module/wallet/createWallet.d.ts.map +1 -1
  29. package/dist/module/wallet/createWallet.js +0 -43
  30. package/dist/module/wallet/createWallet.js.map +1 -1
  31. package/dist/module/wallet/interface.d.ts +0 -2
  32. package/dist/module/wallet/interface.d.ts.map +1 -1
  33. package/dist/module/webhook/Webhook.d.ts +1 -6
  34. package/dist/module/webhook/Webhook.d.ts.map +1 -1
  35. package/dist/module/webhook/Webhook.js +0 -4
  36. package/dist/module/webhook/Webhook.js.map +1 -1
  37. package/dist/module/webhook/index.d.ts +0 -1
  38. package/dist/module/webhook/index.d.ts.map +1 -1
  39. package/dist/module/webhook/index.js +0 -1
  40. package/dist/module/webhook/index.js.map +1 -1
  41. package/dist/module/webhook/interface.d.ts +0 -1
  42. package/dist/module/webhook/interface.d.ts.map +1 -1
  43. package/dist/tsconfig.tsbuildinfo +1 -1
  44. package/package.json +5 -10
  45. package/src/db/SqlProvider.test.ts +2 -18
  46. package/src/db/SqlProvider.ts +5 -26
  47. package/src/index.ts +3 -7
  48. package/src/transaction/Wif.ts +2 -19
  49. package/src/util/deriveCashaddr.test.ts +8 -0
  50. package/src/util/deriveCashaddr.ts +9 -0
  51. package/src/wallet/Base.ts +1 -1
  52. package/src/wallet/Util.ts +2 -2
  53. package/src/wallet/Wif.test.ts +0 -23
  54. package/src/wallet/Wif.ts +2 -148
  55. package/src/wallet/createWallet.ts +0 -53
  56. package/src/wallet/interface.ts +0 -2
  57. package/src/webhook/Webhook.ts +4 -10
  58. package/src/webhook/index.ts +0 -1
  59. package/src/webhook/interface.ts +0 -1
  60. package/webpack.config.cjs +7 -7
  61. package/dist/module/slp/GsppProvider.d.ts +0 -55
  62. package/dist/module/slp/GsppProvider.d.ts.map +0 -1
  63. package/dist/module/slp/GsppProvider.js +0 -204
  64. package/dist/module/slp/GsppProvider.js.map +0 -1
  65. package/dist/module/slp/SlpDbProvider.d.ts +0 -52
  66. package/dist/module/slp/SlpDbProvider.d.ts.map +0 -1
  67. package/dist/module/slp/SlpDbProvider.js +0 -175
  68. package/dist/module/slp/SlpDbProvider.js.map +0 -1
  69. package/dist/module/slp/SlpDbTemplates.d.ts +0 -313
  70. package/dist/module/slp/SlpDbTemplates.d.ts.map +0 -1
  71. package/dist/module/slp/SlpDbTemplates.js +0 -375
  72. package/dist/module/slp/SlpDbTemplates.js.map +0 -1
  73. package/dist/module/slp/SlpLibAuth.d.ts +0 -210
  74. package/dist/module/slp/SlpLibAuth.d.ts.map +0 -1
  75. package/dist/module/slp/SlpLibAuth.js +0 -451
  76. package/dist/module/slp/SlpLibAuth.js.map +0 -1
  77. package/dist/module/slp/SlpProvider.d.ts +0 -24
  78. package/dist/module/slp/SlpProvider.d.ts.map +0 -1
  79. package/dist/module/slp/SlpProvider.js +0 -29
  80. package/dist/module/slp/SlpProvider.js.map +0 -1
  81. package/dist/module/slp/index.d.ts +0 -6
  82. package/dist/module/slp/index.d.ts.map +0 -1
  83. package/dist/module/slp/index.js +0 -5
  84. package/dist/module/slp/index.js.map +0 -1
  85. package/dist/module/slp/interface.d.ts +0 -114
  86. package/dist/module/slp/interface.d.ts.map +0 -1
  87. package/dist/module/slp/interface.js +0 -7
  88. package/dist/module/slp/interface.js.map +0 -1
  89. package/dist/module/util/bchaddr.d.ts +0 -2
  90. package/dist/module/util/bchaddr.d.ts.map +0 -1
  91. package/dist/module/util/bchaddr.js +0 -16
  92. package/dist/module/util/bchaddr.js.map +0 -1
  93. package/dist/module/wallet/Slp.d.ts +0 -431
  94. package/dist/module/wallet/Slp.d.ts.map +0 -1
  95. package/dist/module/wallet/Slp.js +0 -770
  96. package/dist/module/wallet/Slp.js.map +0 -1
  97. package/dist/module/webhook/WebhookSlp.d.ts +0 -15
  98. package/dist/module/webhook/WebhookSlp.d.ts.map +0 -1
  99. package/dist/module/webhook/WebhookSlp.js +0 -100
  100. package/dist/module/webhook/WebhookSlp.js.map +0 -1
  101. package/src/slp/GsppProvider.test.ts +0 -40
  102. package/src/slp/GsppProvider.ts +0 -329
  103. package/src/slp/SlpDbProvider.test.ts +0 -27
  104. package/src/slp/SlpDbProvider.ts +0 -291
  105. package/src/slp/SlpDbTemplates.ts +0 -400
  106. package/src/slp/SlpLibAuth.test.ts +0 -162
  107. package/src/slp/SlpLibAuth.ts +0 -532
  108. package/src/slp/SlpProvider.ts +0 -100
  109. package/src/slp/index.ts +0 -6
  110. package/src/slp/interface.ts +0 -128
  111. package/src/util/bchaddr.test.ts +0 -27
  112. package/src/util/bchaddr.ts +0 -27
  113. package/src/wallet/Slp.test.headless.js +0 -120
  114. package/src/wallet/Slp.test.ts +0 -1000
  115. package/src/wallet/Slp.ts +0 -1014
  116. package/src/webhook/WebhookSlp.test.ts +0 -288
  117. package/src/webhook/WebhookSlp.ts +0 -123
@@ -1,288 +0,0 @@
1
- import WebhookWorker from "./WebhookWorker";
2
- import { RegTestWallet } from "../wallet/Wif";
3
- import { mine } from "../mine/mine";
4
- import { Webhook, WebhookRecurrence, WebhookType } from "./Webhook";
5
- import { GsppProvider, SlpDbProvider, SlpGenesisOptions } from "../slp";
6
-
7
- let worker: WebhookWorker;
8
- let alice;
9
- let aliceWif;
10
- let tokenId;
11
-
12
- const serversSlpDb = { ...{}, ...SlpDbProvider.defaultServers };
13
- const serversGspp = { ...{}, ...GsppProvider.defaultServers };
14
-
15
- /**
16
- * @jest-environment jsdom
17
- */
18
- describe.skip("Webhook worker tests", () => {
19
- beforeAll(async () => {
20
- try {
21
- if (process.env.PRIVATE_WIF) {
22
- alice = process.env.ADDRESS!;
23
- aliceWif = `wif:regtest:${process.env.PRIVATE_WIF!}`;
24
- } else {
25
- console.error("regtest env vars not set");
26
- }
27
-
28
- Webhook.debug.setupAxiosMocks();
29
- worker = await WebhookWorker.instance();
30
-
31
- const genesisOptions: SlpGenesisOptions = {
32
- name: "Webhook Token",
33
- ticker: "WHT",
34
- decimals: 2,
35
- initialAmount: 10000,
36
- documentUrl: "https://mainnet.cash",
37
- documentHash:
38
- "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
39
- };
40
-
41
- const aliceWallet = await RegTestWallet.slp.fromId(aliceWif);
42
- const genesisResult = await aliceWallet.slp.genesis(genesisOptions);
43
- tokenId = genesisResult.tokenId;
44
-
45
- SlpDbProvider.defaultServers.testnet =
46
- SlpDbProvider.defaultServers.regtest;
47
- GsppProvider.defaultServers.testnet = GsppProvider.defaultServers.regtest;
48
- } catch (e: any) {
49
- throw e;
50
- }
51
- });
52
-
53
- beforeEach(async () => {
54
- worker.deleteAllWebhooks();
55
- });
56
-
57
- afterEach(async () => {
58
- Webhook.debug.reset();
59
- });
60
-
61
- afterAll(async () => {
62
- await worker.destroy();
63
- await worker.db.close();
64
-
65
- SlpDbProvider.defaultServers = serversSlpDb;
66
- GsppProvider.defaultServers = serversGspp;
67
- });
68
-
69
- test("Test non-recurrent hook to be deleted after successful call", async () => {
70
- try {
71
- const aliceWallet = await RegTestWallet.slp.fromId(aliceWif);
72
- const bobWallet = await RegTestWallet.slp.newRandom();
73
- await worker.registerWebhook({
74
- cashaddr: bobWallet.slp.slpaddr,
75
- url: "http://example.com/success",
76
- type: WebhookType.slpTransactionIn,
77
- recurrence: WebhookRecurrence.once,
78
- tokenId: tokenId,
79
- });
80
-
81
- await Promise.all([
82
- aliceWallet.slp.send([
83
- {
84
- slpaddr: bobWallet.slp.slpaddr,
85
- value: 1000,
86
- tokenId: tokenId,
87
- },
88
- ]),
89
- bobWallet.slp.waitForTransaction(),
90
- ]);
91
-
92
- // return funds
93
- // let sendResponse2 = await bobWallet.sendMax(aliceWallet.cashaddr!);
94
-
95
- await new Promise((resolve) =>
96
- setTimeout(async () => {
97
- expect(
98
- Webhook.debug.responses["http://example.com/success"].length
99
- ).toBe(1);
100
- expect(worker.activeHooks.size).toBe(0);
101
-
102
- resolve(true);
103
- }, 3000)
104
- );
105
- } catch (e: any) {
106
- console.log(e, e.stack, e.message);
107
- throw e;
108
- }
109
- });
110
-
111
- test("Test non-recurrent hook to be not deleted after failed call", async () => {
112
- try {
113
- const aliceWallet = await RegTestWallet.slp.fromId(aliceWif);
114
- const bobWallet = await RegTestWallet.slp.newRandom();
115
- await worker.registerWebhook({
116
- cashaddr: bobWallet.slp.slpaddr,
117
- url: "http://example.com/fail",
118
- type: WebhookType.slpTransactionIn,
119
- recurrence: WebhookRecurrence.once,
120
- tokenId: tokenId,
121
- });
122
-
123
- await Promise.all([
124
- aliceWallet.slp.send([
125
- {
126
- slpaddr: bobWallet.slp.slpaddr,
127
- value: 1000,
128
- tokenId: tokenId,
129
- },
130
- ]),
131
- bobWallet.slp.waitForTransaction(),
132
- ]);
133
-
134
- await new Promise((resolve) =>
135
- setTimeout(async () => {
136
- expect(
137
- Webhook.debug.responses["http://example.com/fail"].length
138
- ).toBe(1);
139
- expect(worker.activeHooks.size).toBe(1);
140
-
141
- // return funds
142
- // let sendResponse2 = await bobWallet.sendMax(aliceWallet.cashaddr!);
143
- resolve(true);
144
- }, 3000)
145
- );
146
- } catch (e: any) {
147
- console.log(e, e.stack, e.message);
148
- throw e;
149
- }
150
- });
151
-
152
- test("Test recurrent hook for incoming transaction", async () => {
153
- try {
154
- const aliceWallet = await RegTestWallet.slp.fromId(aliceWif);
155
- const bobWallet = await RegTestWallet.slp.newRandom();
156
- await worker.registerWebhook({
157
- cashaddr: bobWallet.slp.slpaddr,
158
- url: "http://example.com/bob",
159
- type: WebhookType.slpTransactionIn,
160
- recurrence: WebhookRecurrence.recurrent,
161
- tokenId: tokenId,
162
- });
163
-
164
- await Promise.all([
165
- aliceWallet.slp.send([
166
- {
167
- slpaddr: bobWallet.slp.slpaddr,
168
- value: 1000,
169
- tokenId: tokenId,
170
- },
171
- ]),
172
- bobWallet.slp.waitForTransaction(),
173
- ]);
174
-
175
- // return funds
176
- // let sendResponse2 = await bobWallet.sendMax(aliceWallet.cashaddr!);
177
-
178
- await new Promise((resolve) =>
179
- setTimeout(async () => {
180
- expect(Webhook.debug.responses["http://example.com/bob"].length).toBe(
181
- 1
182
- );
183
- expect(worker.activeHooks.size).toBe(1);
184
-
185
- resolve(true);
186
- }, 3000)
187
- );
188
- } catch (e: any) {
189
- console.log(e, e.stack, e.message);
190
- throw e;
191
- }
192
- });
193
-
194
- test("Test recurrent hook for outgoing transactions", async () => {
195
- try {
196
- const aliceWallet = await RegTestWallet.slp.fromId(aliceWif);
197
- const bobWallet = await RegTestWallet.slp.newRandom();
198
- await worker.registerWebhook({
199
- cashaddr: bobWallet.slp.slpaddr,
200
- url: "http://example.com/bob",
201
- type: WebhookType.slpTransactionOut,
202
- recurrence: WebhookRecurrence.recurrent,
203
- tokenId: tokenId,
204
- });
205
-
206
- await aliceWallet.send([
207
- {
208
- cashaddr: bobWallet.cashaddr!,
209
- value: 1000,
210
- unit: "satoshi",
211
- },
212
- ]);
213
-
214
- await Promise.all([
215
- aliceWallet.slp.send([
216
- {
217
- slpaddr: bobWallet.slp.slpaddr,
218
- value: 1000,
219
- tokenId: tokenId,
220
- },
221
- ]),
222
- bobWallet.slp.waitForTransaction(),
223
- ]);
224
-
225
- // return funds
226
- await Promise.all([
227
- bobWallet.slp.sendMax(aliceWallet.cashaddr!, tokenId),
228
- aliceWallet.slp.waitForTransaction(),
229
- ]);
230
-
231
- await new Promise((resolve) =>
232
- setTimeout(async () => {
233
- expect(Webhook.debug.responses["http://example.com/bob"].length).toBe(
234
- 1
235
- );
236
- expect(worker.activeHooks.size).toBe(1);
237
-
238
- resolve(true);
239
- }, 3000)
240
- );
241
- } catch (e: any) {
242
- console.log(e, e.stack, e.message);
243
- throw e;
244
- }
245
- });
246
-
247
- test("Test non-recurrent watch balance hook", async () => {
248
- try {
249
- const aliceWallet = await RegTestWallet.slp.fromId(aliceWif);
250
- const bobWallet = await RegTestWallet.slp.newRandom();
251
- await worker.registerWebhook({
252
- cashaddr: bobWallet.slp.slpaddr,
253
- url: "http://example.com/watchBalance",
254
- type: WebhookType.slpBalance,
255
- recurrence: WebhookRecurrence.once,
256
- tokenId: tokenId,
257
- });
258
-
259
- await Promise.all([
260
- aliceWallet.slp.send([
261
- {
262
- slpaddr: bobWallet.slp.slpaddr,
263
- value: 1000,
264
- tokenId: tokenId,
265
- },
266
- ]),
267
- bobWallet.slp.waitForTransaction(),
268
- ]);
269
-
270
- // return funds
271
- // let sendResponse2 = await bobWallet.sendMax(aliceWallet.cashaddr!);
272
-
273
- await new Promise((resolve) =>
274
- setTimeout(async () => {
275
- expect(
276
- Webhook.debug.responses["http://example.com/watchBalance"].length
277
- ).toBe(1);
278
- expect(worker.activeHooks.size).toBe(0);
279
-
280
- resolve(true);
281
- }, 3000)
282
- );
283
- } catch (e: any) {
284
- console.log(e, e.stack, e.message);
285
- throw e;
286
- }
287
- });
288
- });
@@ -1,123 +0,0 @@
1
- import { GsppTx, SlpDbTx, SlpTxI } from "../slp/index.js";
2
- import {
3
- SlpCancelWatchFn,
4
- SlpWatchTransactionCallback,
5
- } from "../slp/SlpProvider.js";
6
- import { toSlpAddress } from "../util/bchaddr.js";
7
- import { Wallet } from "../wallet/Wif.js";
8
- import { Webhook, WebhookRecurrence, WebhookType } from "./Webhook.js";
9
- import WebhookWorker from "./WebhookWorker.js";
10
-
11
- export class WebhookSlp extends Webhook {
12
- callback!: SlpWatchTransactionCallback;
13
- cancelFn!: SlpCancelWatchFn;
14
- wallet!: Wallet;
15
-
16
- constructor(hook: Webhook | Object) {
17
- super(hook);
18
- Object.assign(this, hook);
19
- this.cashaddr = toSlpAddress(this.cashaddr);
20
- }
21
-
22
- async start(): Promise<void> {
23
- const webhookCallback: SlpWatchTransactionCallback = async (
24
- rawTx: SlpTxI
25
- ) => {
26
- let result = false;
27
- if ("_id" in rawTx.details) {
28
- result = await this.slpDbHandler(rawTx);
29
- } else {
30
- result = await this.gsppHandler(rawTx);
31
- }
32
-
33
- if (result && this.recurrence === WebhookRecurrence.once) {
34
- // we have to notify the worker about end of life
35
- await (await WebhookWorker.instance()).stopHook(this);
36
- await this.destroy();
37
- }
38
- };
39
-
40
- this.callback = webhookCallback;
41
- this.wallet = await Wallet.fromSlpaddr(this.cashaddr);
42
- this.cancelFn = this.wallet.slp.watchTransactions(
43
- webhookCallback,
44
- this.tokenId
45
- );
46
- }
47
-
48
- async slpDbHandler(rawTx: SlpTxI) {
49
- let result: boolean = false;
50
- const txDirection = this.type;
51
- const details: SlpDbTx = rawTx.details as SlpDbTx;
52
- if (this.type === WebhookType.slpTransactionInOut) {
53
- result = await this.post({
54
- direction: txDirection,
55
- data: rawTx,
56
- });
57
- } else if (
58
- this.type === WebhookType.slpTransactionIn &&
59
- details.out.findIndex((val) => val.e.a === this.cashaddr) > -1
60
- ) {
61
- result = await this.post({
62
- direction: txDirection,
63
- data: rawTx,
64
- });
65
- } else if (
66
- this.type === WebhookType.slpTransactionOut &&
67
- details.in.findIndex((val) => val.e.a === this.cashaddr) > -1
68
- ) {
69
- result = await this.post({
70
- direction: txDirection,
71
- data: rawTx,
72
- });
73
- } else if (this.type === WebhookType.slpBalance) {
74
- const balance = this.wallet.slp.getBalance(this.tokenId!);
75
- result = await this.post({
76
- direction: txDirection,
77
- data: balance,
78
- });
79
- }
80
-
81
- return result;
82
- }
83
-
84
- async gsppHandler(rawTx: SlpTxI) {
85
- let result: boolean = false;
86
- const txDirection = this.type;
87
- const details: GsppTx = rawTx.details as GsppTx;
88
- if (this.type === WebhookType.slpTransactionInOut) {
89
- result = await this.post({
90
- direction: txDirection,
91
- data: rawTx,
92
- });
93
- } else if (
94
- this.type === WebhookType.slpTransactionIn &&
95
- details.outputs.findIndex((val) => val === this.cashaddr) > -1
96
- ) {
97
- result = await this.post({
98
- direction: txDirection,
99
- data: rawTx,
100
- });
101
- } else if (
102
- this.type === WebhookType.slpTransactionOut &&
103
- details.inputs.findIndex((val) => val === this.cashaddr) > -1
104
- ) {
105
- result = await this.post({
106
- direction: txDirection,
107
- data: rawTx,
108
- });
109
- } else if (this.type === WebhookType.slpBalance) {
110
- const balance = this.wallet.slp.getBalance(this.tokenId!);
111
- result = await this.post({
112
- direction: txDirection,
113
- data: balance,
114
- });
115
- }
116
-
117
- return result;
118
- }
119
-
120
- async stop(): Promise<void> {
121
- await this.cancelFn();
122
- }
123
- }