@mainnet-cash/postgresql-storage 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 (36) hide show
  1. package/dist/module/SqlProvider.d.ts +2 -2
  2. package/dist/module/SqlProvider.js +1 -1
  3. package/dist/module/SqlProvider.js.map +1 -1
  4. package/dist/module/webhook/Webhook.d.ts +1 -7
  5. package/dist/module/webhook/Webhook.js +8 -41
  6. package/dist/module/webhook/Webhook.js.map +1 -1
  7. package/dist/module/webhook/WebhookBch.d.ts +1 -2
  8. package/dist/module/webhook/WebhookBch.js +2 -2
  9. package/dist/module/webhook/WebhookBch.js.map +1 -1
  10. package/dist/module/webhook/WebhookMock.d.ts +6 -0
  11. package/dist/module/webhook/WebhookMock.js +26 -0
  12. package/dist/module/webhook/WebhookMock.js.map +1 -0
  13. package/dist/module/webhook/WebhookWorker.d.ts +1 -1
  14. package/dist/module/webhook/WebhookWorker.js.map +1 -1
  15. package/dist/module/webhook/index.d.ts +2 -2
  16. package/dist/module/webhook/index.js +2 -2
  17. package/dist/module/webhook/index.js.map +1 -1
  18. package/package.json +17 -12
  19. package/dist/tsconfig.browser.tsbuildinfo +0 -1
  20. package/src/SqlProvider.test.ts +0 -265
  21. package/src/SqlProvider.ts +0 -233
  22. package/src/Wallet.test.ts +0 -537
  23. package/src/createWallet.test.ts +0 -168
  24. package/src/index.test.ts +0 -93
  25. package/src/index.ts +0 -2
  26. package/src/util.ts +0 -32
  27. package/src/webhook/Webhook.test.ts +0 -9
  28. package/src/webhook/Webhook.ts +0 -99
  29. package/src/webhook/WebhookBch.test.ts +0 -315
  30. package/src/webhook/WebhookBch.ts +0 -192
  31. package/src/webhook/WebhookWorker.test.ts +0 -94
  32. package/src/webhook/WebhookWorker.ts +0 -119
  33. package/src/webhook/index.ts +0 -4
  34. package/src/webhook/interface.ts +0 -7
  35. package/tsconfig.browser.json +0 -6
  36. package/tsconfig.json +0 -32
@@ -1,192 +0,0 @@
1
- import SqlProvider from "../SqlProvider.js";
2
- import { CancelFn, TxI } from "mainnet-js";
3
- import { ElectrumRawTransaction } from "mainnet-js";
4
- import { WatchWallet } from "mainnet-js";
5
- import { Webhook, WebhookRecurrence, WebhookType } from "./Webhook.js";
6
- import WebhookWorker from "./WebhookWorker.js";
7
-
8
- export class WebhookBch extends Webhook {
9
- cancel!: CancelFn;
10
- wallet!: WatchWallet;
11
-
12
- db!: SqlProvider;
13
- seenStatuses: string[] = [];
14
-
15
- constructor(hook: Webhook | Object) {
16
- super(hook);
17
- Object.assign(this, hook);
18
- }
19
-
20
- async stop(): Promise<void> {
21
- await this.cancel?.();
22
- }
23
-
24
- async start(): Promise<void> {
25
- const webhookCallback = async (data: string | Array<string>) => {
26
- let status: string = "";
27
- if (typeof data === "string") {
28
- // subscription acknowledgement notification with current status
29
- status = data;
30
-
31
- // we should skip fetching transactions for freshly registered hooks upon acknowledements
32
- if (this.status === "") {
33
- return;
34
- }
35
- } else if (data instanceof Array) {
36
- status = data[1];
37
- if (data[0] !== this.cashaddr || status === null) {
38
- // console.warn("Address missmatch, skipping", data[0], this.cashaddr);
39
- return;
40
- }
41
- } else {
42
- return;
43
- }
44
-
45
- if (status !== this.status && this.seenStatuses.indexOf(status) === -1) {
46
- this.seenStatuses.push(status);
47
- await this.handler(status);
48
- }
49
- };
50
-
51
- this.wallet = await WatchWallet.fromCashaddr(this.cashaddr);
52
- this.cancel = await this.wallet.provider!.subscribeToAddress(
53
- this.cashaddr,
54
- webhookCallback
55
- );
56
- }
57
-
58
- async handler(status: string): Promise<void> {
59
- // console.debug("Dispatching action for a webhook", this);
60
- // get transactions
61
- const history: TxI[] = await this.wallet.provider!.getHistory(
62
- this.cashaddr
63
- );
64
-
65
- // figure out which transactions to send to the hook
66
- let txs: TxI[] = [];
67
-
68
- if (status === "") {
69
- // send the last transaction only if no tracking was done
70
- txs = history.slice(-1);
71
- } else {
72
- // reverse history for faster search
73
- const revHistory = history.reverse();
74
- // update height of transactions, which are now confirmed
75
- this.tx_seen = this.tx_seen.map((seenTx) => {
76
- if (seenTx.height <= 0) {
77
- const histTx = revHistory.find(
78
- (val) => val.tx_hash === seenTx.tx_hash
79
- );
80
- if (histTx) {
81
- seenTx.height = histTx.height;
82
- }
83
- }
84
- return seenTx;
85
- });
86
-
87
- const seenHashes = this.tx_seen.map((val) => val.tx_hash);
88
- txs = history.filter(
89
- (val) =>
90
- (val.height >= this.last_height || val.height <= 0) &&
91
- !seenHashes.includes(val.tx_hash)
92
- );
93
- }
94
-
95
- let shouldUpdateStatus: boolean = true;
96
-
97
- for (const tx of txs) {
98
- // watching transactions
99
- let result: boolean = false;
100
-
101
- if (this.type.indexOf("transaction:") >= 0) {
102
- // console.debug("Getting raw tx", tx.tx_hash);
103
- const rawTx: ElectrumRawTransaction =
104
- await this.wallet.provider!.getRawTransactionObject(tx.tx_hash);
105
- const parentTxs: ElectrumRawTransaction[] = await Promise.all(
106
- rawTx.vin.map((t) =>
107
- this.wallet.provider!.getRawTransactionObject(t.txid)
108
- )
109
- );
110
- // console.debug("Got raw tx", JSON.stringify(rawTx, null, 2));
111
- const haveAddressInOutputs: boolean = rawTx.vout.some((val) =>
112
- val.scriptPubKey.addresses.includes(this.cashaddr)
113
- );
114
- const haveAddressInParentOutputs: boolean = parentTxs.some((parent) =>
115
- parent.vout.some((val) =>
116
- val.scriptPubKey.addresses.includes(this.cashaddr)
117
- )
118
- );
119
-
120
- const wantsIn: boolean = this.type.indexOf("in") >= 0;
121
- const wantsOut: boolean = this.type.indexOf("out") >= 0;
122
-
123
- const txDirection: string =
124
- haveAddressInParentOutputs && haveAddressInOutputs
125
- ? WebhookType.transactionInOut
126
- : haveAddressInParentOutputs
127
- ? WebhookType.transactionOut
128
- : WebhookType.transactionIn;
129
-
130
- if (wantsIn && haveAddressInOutputs) {
131
- result = await this.post({
132
- direction: txDirection,
133
- data: rawTx,
134
- });
135
- } else if (wantsOut && haveAddressInParentOutputs) {
136
- result = await this.post({
137
- direction: txDirection,
138
- data: rawTx,
139
- });
140
- } else {
141
- // not interested in this transaction
142
- continue;
143
- }
144
- } else if (this.type === WebhookType.balance) {
145
- // watching address balance
146
- const balanceSat = await this.wallet.provider!.getBalance(
147
- this.cashaddr
148
- );
149
- result = await this.post({ sat: balanceSat.toString() });
150
- }
151
-
152
- if (result) {
153
- this.tx_seen.push(tx);
154
- await this.db.setWebhookSeenTxLastHeight(
155
- this.id!,
156
- this.tx_seen,
157
- this.last_height
158
- );
159
- } else {
160
- // console.debug("Failed to execute webhook", hook);
161
- shouldUpdateStatus = false;
162
- }
163
- }
164
-
165
- // successful run
166
- if (shouldUpdateStatus) {
167
- if (this.recurrence === WebhookRecurrence.once) {
168
- // we have to notify the worker about end of life
169
- await (await WebhookWorker.instance()).stopHook(this);
170
- await this.destroy();
171
- return;
172
- }
173
-
174
- this.status = status;
175
- await this.db.setWebhookStatus(this.id!, status);
176
-
177
- // update last_height and truncate tx_seen
178
- const maxHeight = Math.max(...this.tx_seen.map((val) => val.height));
179
- if (maxHeight >= this.last_height) {
180
- this.last_height = maxHeight;
181
- this.tx_seen = this.tx_seen.filter(
182
- (val) => val.height === maxHeight || val.height <= 0
183
- );
184
- await this.db.setWebhookSeenTxLastHeight(
185
- this.id!,
186
- this.tx_seen,
187
- this.last_height
188
- );
189
- }
190
- }
191
- }
192
- }
@@ -1,94 +0,0 @@
1
- import WebhookWorker from "../webhook/WebhookWorker";
2
- import { Webhook } from "./Webhook";
3
-
4
- let worker: WebhookWorker;
5
- let alice = "";
6
- let aliceWif = "";
7
-
8
- /**
9
- * @jest-environment jsdom
10
- */
11
-
12
- describe("Webhook worker tests", () => {
13
- beforeAll(async () => {
14
- try {
15
- if (process.env.PRIVATE_WIF) {
16
- alice = process.env.ADDRESS!;
17
- aliceWif = `wif:regtest:${process.env.PRIVATE_WIF!}`;
18
- } else {
19
- console.error("regtest env vars not set");
20
- }
21
-
22
- Webhook.debug.setupAxiosMocks();
23
- worker = await WebhookWorker.instance();
24
- } catch (e: any) {
25
- throw e;
26
- }
27
- });
28
-
29
- beforeEach(async () => {
30
- worker.deleteAllWebhooks();
31
- });
32
-
33
- afterEach(async () => {
34
- Webhook.debug.reset();
35
- });
36
-
37
- afterAll(async () => {
38
- await worker.destroy();
39
- await worker.db.close();
40
- });
41
-
42
- test("Test posting hook", async () => {
43
- const hook1 = new Webhook({ url: "http://example.com/pass" });
44
- let success = await hook1.post({});
45
- expect(success).toBe(true);
46
-
47
- const hook2 = new Webhook({ url: "http://example.com/fail" });
48
- let fail = await hook2.post({});
49
- expect(fail).toBe(false);
50
-
51
- expect(Webhook.debug.responses["http://example.com/pass"].length).toBe(1);
52
-
53
- expect(Webhook.debug.responses["http://example.com/fail"].length).toBe(1);
54
- });
55
-
56
- test("Test empty hook db", async () => {
57
- try {
58
- await new Promise((resolve) =>
59
- setTimeout(async () => {
60
- expect(worker.activeHooks.size).toBe(0);
61
- expect(Webhook.debug.responses).toStrictEqual({});
62
- resolve(true);
63
- }, 0)
64
- );
65
- } catch (e: any) {
66
- console.log(e, e.stack, e.message);
67
- throw e;
68
- }
69
- });
70
-
71
- test("Test starting with expired hook", async () => {
72
- await worker.registerWebhook(
73
- {
74
- cashaddr: alice,
75
- url: "http://example.com/pass",
76
- type: "transaction:in",
77
- recurrence: "once",
78
- duration_sec: -1000,
79
- },
80
- false
81
- );
82
-
83
- await worker.init();
84
-
85
- try {
86
- expect(worker.activeHooks.size).toBe(0);
87
- expect((await worker.db.getWebhooks()).length).toBe(0);
88
- expect(Webhook.debug.responses).toStrictEqual({});
89
- } catch (e: any) {
90
- console.log(e, e.stack, e.message);
91
- throw e;
92
- }
93
- });
94
- });
@@ -1,119 +0,0 @@
1
- import SqlProvider from "../SqlProvider.js";
2
- import { RegisterWebhookParams } from "./interface.js";
3
-
4
- import { Webhook } from "./Webhook";
5
-
6
- export default class WebhookWorker {
7
- activeHooks: Map<number, Webhook> = new Map();
8
- callbacks: Map<number, (data: any | string | Array<string>) => void> =
9
- new Map();
10
- db: SqlProvider;
11
- checkInterval: any = undefined;
12
-
13
- private static _instance: WebhookWorker;
14
-
15
- static async instance() {
16
- if (!WebhookWorker._instance) {
17
- WebhookWorker._instance = new WebhookWorker();
18
- await WebhookWorker._instance.init();
19
- }
20
-
21
- return WebhookWorker._instance;
22
- }
23
-
24
- constructor() {
25
- this.db = new SqlProvider();
26
- }
27
-
28
- async init(): Promise<void> {
29
- await this.db.init();
30
-
31
- await this.evictOldHooks();
32
- await this.pickupHooks(true);
33
- if (!this.checkInterval) {
34
- this.checkInterval = setInterval(async () => {
35
- await this.evictOldHooks();
36
- await this.pickupHooks(true);
37
- }, 5 * 60 * 1000);
38
- }
39
- }
40
-
41
- async destroy(): Promise<void> {
42
- await this.stopAll();
43
- if (this.checkInterval) {
44
- clearInterval(this.checkInterval);
45
- this.checkInterval = undefined;
46
- }
47
- }
48
-
49
- async pickupHooks(start: boolean = false): Promise<void> {
50
- const hooks: Webhook[] = await this.db.getWebhooks();
51
- for (const hook of hooks) {
52
- if (!this.activeHooks.has(hook.id!)) {
53
- this.activeHooks.set(hook.id!, hook);
54
- if (start) {
55
- await hook.start();
56
- }
57
- }
58
- }
59
- }
60
-
61
- async evictOldHooks(): Promise<void> {
62
- const now = new Date();
63
- const dbHooks = await this.db.getWebhooks();
64
- for (const hook of dbHooks) {
65
- if (now >= hook.expires_at) {
66
- // console.debug("Evicting expired hook with id", hook.id, new Date(), hook.expires_at);
67
- if (this.activeHooks.has(hook.id!)) {
68
- await this.stopHook(hook);
69
- }
70
- await this.db.deleteWebhook(hook.id!);
71
- }
72
- }
73
- }
74
-
75
- async registerWebhook(
76
- params: RegisterWebhookParams,
77
- start: boolean = true
78
- ): Promise<number> {
79
- const webhook = await this.db.addWebhook(params);
80
- if (start) {
81
- this.activeHooks.set(webhook.id!, webhook);
82
- await webhook.start();
83
- }
84
- return webhook.id!;
85
- }
86
-
87
- async getWebhook(id: number): Promise<Webhook | undefined> {
88
- if (this.activeHooks.has(id)) {
89
- return this.activeHooks.get(id)!;
90
- }
91
-
92
- return await this.db.getWebhook(id);
93
- }
94
-
95
- async deleteWebhook(id: number): Promise<void> {
96
- if (this.activeHooks.has(id)) {
97
- await this.stopHook(this.activeHooks.get(id)!);
98
- }
99
- await this.db.deleteWebhook(id);
100
- }
101
-
102
- async deleteAllWebhooks(): Promise<void> {
103
- await this.stopAll();
104
- await this.db.clearWebhooks();
105
- }
106
-
107
- async stopAll(): Promise<void> {
108
- for (const [, hook] of this.activeHooks) {
109
- await this.stopHook(hook);
110
- }
111
- }
112
-
113
- async stopHook(hook: Webhook): Promise<void> {
114
- if (this.activeHooks.has(hook.id!)) {
115
- await hook.stop();
116
- this.activeHooks.delete(hook.id!);
117
- }
118
- }
119
- }
@@ -1,4 +0,0 @@
1
- export { default as WebhookWorker } from "./WebhookWorker.js";
2
- export * from "./Webhook.js";
3
- export * from "./WebhookBch.js";
4
- export * from "./interface.js";
@@ -1,7 +0,0 @@
1
- export interface RegisterWebhookParams {
2
- cashaddr: string;
3
- url: string;
4
- type: string;
5
- recurrence: string;
6
- duration_sec?: number;
7
- }
@@ -1,6 +0,0 @@
1
- {
2
- "extends": "./tsconfig",
3
- "compilerOptions": {},
4
- "include": ["src/**/*.ts"],
5
- "exclude": ["node_modules/**", "dist/**", "src/**/*test.ts"]
6
- }
package/tsconfig.json DELETED
@@ -1,32 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "rootDir": "src",
5
- "skipLibCheck": true,
6
- "esModuleInterop": true,
7
- "allowSyntheticDefaultImports": true,
8
- "strict": true,
9
- "forceConsistentCasingInFileNames": true,
10
- "downlevelIteration": true,
11
- "composite": true,
12
- "module": "esnext",
13
- "target": "esnext",
14
- "outDir": "./dist/module",
15
- "moduleResolution": "node",
16
- "resolveJsonModule": true,
17
- "lib": ["es2020", "es2020.bigint", "dom"],
18
- "typeRoots": [
19
- "node_modules/@types",
20
- "../../node_modules/@types",
21
- "./src/types"
22
- ]
23
- },
24
- "include": ["src/**/*.ts"],
25
- "exclude": ["node_modules/**", "src/**/*test.ts"],
26
- "references": [
27
- {
28
- "path": "../mainnet-js/"
29
- }
30
- ],
31
- "compileOnSave": false
32
- }