@ocap/tx-util 1.28.9 → 1.29.1
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.
- package/README.md +0 -1
- package/esm/index.d.mts +11 -0
- package/esm/index.mjs +81 -0
- package/esm/package.mjs +5 -0
- package/esm/verifier.d.mts +52 -0
- package/esm/verifier.mjs +91 -0
- package/lib/_virtual/rolldown_runtime.cjs +29 -0
- package/lib/index.cjs +87 -0
- package/lib/index.d.cts +11 -0
- package/lib/package.cjs +11 -0
- package/lib/verifier.cjs +95 -0
- package/lib/verifier.d.cts +52 -0
- package/package.json +34 -11
- package/lib/index.d.ts +0 -16
- package/lib/index.js +0 -94
- package/lib/verifier.js +0 -101
package/README.md
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
-
[](https://github.com/prettier/prettier)
|
|
4
3
|
[](https://docs.arcblock.io)
|
|
5
4
|
[](https://gitter.im/ArcBlock/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
|
6
5
|
|
package/esm/index.d.mts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CreateVerifierOptions, VerifierDoneEvent, VerifierEventEmitter, VerifyResponse } from "./verifier.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare const verifyTx: (params: CreateVerifierOptions) => VerifierEventEmitter;
|
|
5
|
+
declare const verifyTxAsync: (params: CreateVerifierOptions) => Promise<VerifierDoneEvent>;
|
|
6
|
+
declare const verifyAccount: (params: CreateVerifierOptions) => VerifierEventEmitter;
|
|
7
|
+
declare const verifyAccountAsync: (params: CreateVerifierOptions) => Promise<VerifierDoneEvent>;
|
|
8
|
+
declare const verifyAsset: (params: CreateVerifierOptions) => VerifierEventEmitter;
|
|
9
|
+
declare const verifyAssetAsync: (params: CreateVerifierOptions) => Promise<VerifierDoneEvent>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { type CreateVerifierOptions, type VerifierDoneEvent, type VerifierEventEmitter, type VerifyResponse, verifyTx as createVerifier, verifyTx, verifyAccount, verifyAccountAsync, verifyAsset, verifyAssetAsync, verifyTxAsync };
|
package/esm/index.mjs
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { create, createAsync } from "./verifier.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* Generate a transaction verifier by tx hash
|
|
6
|
+
* Emit events on transaction included in a block
|
|
7
|
+
*
|
|
8
|
+
* - `error`: when there is something wrong when verify the transaction
|
|
9
|
+
* - `done`: when the transaction is successfully verified
|
|
10
|
+
*
|
|
11
|
+
* @param params
|
|
12
|
+
* @param params.hash - tx hash to check
|
|
13
|
+
* @param params.chainHost - on which chain to check the tx
|
|
14
|
+
* @param params.checkInterval - tx query interval to see the tx
|
|
15
|
+
* @param params.autoStart - should the verifier start on create
|
|
16
|
+
* @param params.maxRetry - max number of checks before mark the tx as expired
|
|
17
|
+
* @returns verifier object
|
|
18
|
+
*/
|
|
19
|
+
const txVerifierParams = {
|
|
20
|
+
key: "hash",
|
|
21
|
+
label: "tx",
|
|
22
|
+
fn: "getTx",
|
|
23
|
+
validate: (res) => {
|
|
24
|
+
if (res.info) {
|
|
25
|
+
if (res.info.code === "OK") return true;
|
|
26
|
+
throw new Error(res.info.code);
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const verifyTx = create(txVerifierParams);
|
|
32
|
+
const verifyTxAsync = createAsync(txVerifierParams);
|
|
33
|
+
/**
|
|
34
|
+
* Generate a account verifier by address
|
|
35
|
+
* Emit events on account declared in a block
|
|
36
|
+
*
|
|
37
|
+
* - `error`: when there is something wrong when verify the transaction
|
|
38
|
+
* - `done`: when the transaction is successfully verified
|
|
39
|
+
*
|
|
40
|
+
* @param params
|
|
41
|
+
* @param params.address - account address to check
|
|
42
|
+
* @param params.chainHost - on which chain to check the account
|
|
43
|
+
* @param params.checkInterval - query interval to see the account
|
|
44
|
+
* @param params.autoStart - should the verifier start on create
|
|
45
|
+
* @param params.maxRetry - max number of checks before mark the check as expired
|
|
46
|
+
* @returns verifier object
|
|
47
|
+
*/
|
|
48
|
+
const accountVerifierParams = {
|
|
49
|
+
key: "address",
|
|
50
|
+
label: "account",
|
|
51
|
+
fn: "getAccountState",
|
|
52
|
+
validate: (res) => res.state
|
|
53
|
+
};
|
|
54
|
+
const verifyAccount = create(accountVerifierParams);
|
|
55
|
+
const verifyAccountAsync = createAsync(accountVerifierParams);
|
|
56
|
+
/**
|
|
57
|
+
* Generate a asset verifier by address
|
|
58
|
+
* Emit events on asset can be found on chain
|
|
59
|
+
*
|
|
60
|
+
* - `error`: when there is something wrong when verify the asset
|
|
61
|
+
* - `done`: when the asset is found
|
|
62
|
+
*
|
|
63
|
+
* @param params
|
|
64
|
+
* @param params.address - asset address to check
|
|
65
|
+
* @param params.chainHost - on which chain to check the asset
|
|
66
|
+
* @param params.checkInterval - query interval to see the asset
|
|
67
|
+
* @param params.autoStart - should the verifier start on create
|
|
68
|
+
* @param params.maxRetry - max number of checks before mark the check as expired
|
|
69
|
+
* @returns verifier object
|
|
70
|
+
*/
|
|
71
|
+
const assetVerifierParams = {
|
|
72
|
+
key: "address",
|
|
73
|
+
label: "asset",
|
|
74
|
+
fn: "getAssetState",
|
|
75
|
+
validate: (res) => res.state
|
|
76
|
+
};
|
|
77
|
+
const verifyAsset = create(assetVerifierParams);
|
|
78
|
+
const verifyAssetAsync = createAsync(assetVerifierParams);
|
|
79
|
+
|
|
80
|
+
//#endregion
|
|
81
|
+
export { verifyTx as createVerifier, verifyTx, verifyAccount, verifyAccountAsync, verifyAsset, verifyAssetAsync, verifyTxAsync };
|
package/esm/package.mjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
|
|
3
|
+
//#region src/verifier.d.ts
|
|
4
|
+
interface VerifierParams {
|
|
5
|
+
key: string;
|
|
6
|
+
label: string;
|
|
7
|
+
fn: string;
|
|
8
|
+
validate: (res: VerifyResponse) => unknown;
|
|
9
|
+
}
|
|
10
|
+
interface VerifyResponse {
|
|
11
|
+
code?: string;
|
|
12
|
+
info?: {
|
|
13
|
+
code: string;
|
|
14
|
+
hash?: string;
|
|
15
|
+
};
|
|
16
|
+
state?: {
|
|
17
|
+
address: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
interface CreateVerifierOptions {
|
|
21
|
+
chainHost: string;
|
|
22
|
+
checkInterval?: number;
|
|
23
|
+
autoStart?: boolean;
|
|
24
|
+
maxRetry?: number;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
interface VerifierEventEmitter extends EventEmitter {
|
|
28
|
+
start: () => Promise<boolean>;
|
|
29
|
+
}
|
|
30
|
+
interface VerifierDoneEvent {
|
|
31
|
+
data: VerifyResponse;
|
|
32
|
+
[key: string]: unknown;
|
|
33
|
+
}
|
|
34
|
+
interface VerifierErrorEvent {
|
|
35
|
+
type: 'expired' | 'invalid' | 'exception';
|
|
36
|
+
retryCount: number;
|
|
37
|
+
error?: Error;
|
|
38
|
+
data?: VerifyResponse;
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
}
|
|
41
|
+
declare const create: ({
|
|
42
|
+
key,
|
|
43
|
+
label,
|
|
44
|
+
fn,
|
|
45
|
+
validate
|
|
46
|
+
}: VerifierParams) => (params: CreateVerifierOptions) => VerifierEventEmitter;
|
|
47
|
+
/**
|
|
48
|
+
* Create an async verifier
|
|
49
|
+
*/
|
|
50
|
+
declare const createAsync: (verifierParams: VerifierParams) => (params: CreateVerifierOptions) => Promise<VerifierDoneEvent>;
|
|
51
|
+
//#endregion
|
|
52
|
+
export { CreateVerifierOptions, VerifierDoneEvent, VerifierErrorEvent, VerifierEventEmitter, VerifierParams, VerifyResponse, create, createAsync };
|
package/esm/verifier.mjs
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { name } from "./package.mjs";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
import Client from "@ocap/client";
|
|
4
|
+
import Debug from "debug";
|
|
5
|
+
|
|
6
|
+
//#region src/verifier.ts
|
|
7
|
+
const debug = Debug(name);
|
|
8
|
+
const create = ({ key, label, fn, validate }) => (params) => {
|
|
9
|
+
const { chainHost, checkInterval = 1e3, autoStart = true, maxRetry = 20 } = params;
|
|
10
|
+
debug(`${label}.verify.params`, params);
|
|
11
|
+
if (!params[key]) throw new Error(`${key} must be provided to create ${label} verifier`);
|
|
12
|
+
if (!chainHost) throw new Error(`chainHost must be provided to create ${label} verifier`);
|
|
13
|
+
const client = new Client(chainHost);
|
|
14
|
+
if (typeof client[fn] !== "function") throw new Error(`Verify method ${fn} does not exist on client.`);
|
|
15
|
+
const param = params[key];
|
|
16
|
+
const events = new EventEmitter();
|
|
17
|
+
let retryCount = 0;
|
|
18
|
+
const doCheck = async () => {
|
|
19
|
+
if (retryCount > maxRetry) {
|
|
20
|
+
events.emit("error", {
|
|
21
|
+
[key]: params[key],
|
|
22
|
+
type: "expired",
|
|
23
|
+
retryCount,
|
|
24
|
+
error: /* @__PURE__ */ new Error("Transaction verifier exceeded max retry count")
|
|
25
|
+
});
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
retryCount += 1;
|
|
29
|
+
try {
|
|
30
|
+
const res = await client[fn]({ [key]: param });
|
|
31
|
+
if (res && res.code === "OK") try {
|
|
32
|
+
if (validate(res)) {
|
|
33
|
+
debug(`${label}.verify.done`, {
|
|
34
|
+
[key]: param,
|
|
35
|
+
retryCount
|
|
36
|
+
});
|
|
37
|
+
events.emit("done", {
|
|
38
|
+
[key]: param,
|
|
39
|
+
data: res
|
|
40
|
+
});
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
debug(`${label}.verify.pending`, {
|
|
44
|
+
[key]: param,
|
|
45
|
+
code: res.code,
|
|
46
|
+
retryCount
|
|
47
|
+
});
|
|
48
|
+
setTimeout(doCheck, checkInterval);
|
|
49
|
+
} catch (_err) {
|
|
50
|
+
events.emit("error", {
|
|
51
|
+
[key]: param,
|
|
52
|
+
type: "invalid",
|
|
53
|
+
retryCount,
|
|
54
|
+
data: res
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
debug(`${label}.verify.pending`, {
|
|
59
|
+
[key]: param,
|
|
60
|
+
code: res.code,
|
|
61
|
+
retryCount
|
|
62
|
+
});
|
|
63
|
+
setTimeout(doCheck, checkInterval);
|
|
64
|
+
}
|
|
65
|
+
} catch (err) {
|
|
66
|
+
console.error(`${label}.verify.error`, err);
|
|
67
|
+
setTimeout(doCheck, checkInterval);
|
|
68
|
+
events.emit("error", {
|
|
69
|
+
[key]: param,
|
|
70
|
+
type: "exception",
|
|
71
|
+
error: err,
|
|
72
|
+
retryCount
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
};
|
|
77
|
+
events.start = doCheck;
|
|
78
|
+
if (autoStart) doCheck();
|
|
79
|
+
return events;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Create an async verifier
|
|
83
|
+
*/
|
|
84
|
+
const createAsync = (verifierParams) => (params) => new Promise((resolve, reject) => {
|
|
85
|
+
const verifier = create(verifierParams)(params);
|
|
86
|
+
verifier.on("done", resolve);
|
|
87
|
+
verifier.on("error", reject);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
export { create, createAsync };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
exports.__toESM = __toESM;
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const require_verifier = require('./verifier.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* Generate a transaction verifier by tx hash
|
|
6
|
+
* Emit events on transaction included in a block
|
|
7
|
+
*
|
|
8
|
+
* - `error`: when there is something wrong when verify the transaction
|
|
9
|
+
* - `done`: when the transaction is successfully verified
|
|
10
|
+
*
|
|
11
|
+
* @param params
|
|
12
|
+
* @param params.hash - tx hash to check
|
|
13
|
+
* @param params.chainHost - on which chain to check the tx
|
|
14
|
+
* @param params.checkInterval - tx query interval to see the tx
|
|
15
|
+
* @param params.autoStart - should the verifier start on create
|
|
16
|
+
* @param params.maxRetry - max number of checks before mark the tx as expired
|
|
17
|
+
* @returns verifier object
|
|
18
|
+
*/
|
|
19
|
+
const txVerifierParams = {
|
|
20
|
+
key: "hash",
|
|
21
|
+
label: "tx",
|
|
22
|
+
fn: "getTx",
|
|
23
|
+
validate: (res) => {
|
|
24
|
+
if (res.info) {
|
|
25
|
+
if (res.info.code === "OK") return true;
|
|
26
|
+
throw new Error(res.info.code);
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const verifyTx = require_verifier.create(txVerifierParams);
|
|
32
|
+
const verifyTxAsync = require_verifier.createAsync(txVerifierParams);
|
|
33
|
+
/**
|
|
34
|
+
* Generate a account verifier by address
|
|
35
|
+
* Emit events on account declared in a block
|
|
36
|
+
*
|
|
37
|
+
* - `error`: when there is something wrong when verify the transaction
|
|
38
|
+
* - `done`: when the transaction is successfully verified
|
|
39
|
+
*
|
|
40
|
+
* @param params
|
|
41
|
+
* @param params.address - account address to check
|
|
42
|
+
* @param params.chainHost - on which chain to check the account
|
|
43
|
+
* @param params.checkInterval - query interval to see the account
|
|
44
|
+
* @param params.autoStart - should the verifier start on create
|
|
45
|
+
* @param params.maxRetry - max number of checks before mark the check as expired
|
|
46
|
+
* @returns verifier object
|
|
47
|
+
*/
|
|
48
|
+
const accountVerifierParams = {
|
|
49
|
+
key: "address",
|
|
50
|
+
label: "account",
|
|
51
|
+
fn: "getAccountState",
|
|
52
|
+
validate: (res) => res.state
|
|
53
|
+
};
|
|
54
|
+
const verifyAccount = require_verifier.create(accountVerifierParams);
|
|
55
|
+
const verifyAccountAsync = require_verifier.createAsync(accountVerifierParams);
|
|
56
|
+
/**
|
|
57
|
+
* Generate a asset verifier by address
|
|
58
|
+
* Emit events on asset can be found on chain
|
|
59
|
+
*
|
|
60
|
+
* - `error`: when there is something wrong when verify the asset
|
|
61
|
+
* - `done`: when the asset is found
|
|
62
|
+
*
|
|
63
|
+
* @param params
|
|
64
|
+
* @param params.address - asset address to check
|
|
65
|
+
* @param params.chainHost - on which chain to check the asset
|
|
66
|
+
* @param params.checkInterval - query interval to see the asset
|
|
67
|
+
* @param params.autoStart - should the verifier start on create
|
|
68
|
+
* @param params.maxRetry - max number of checks before mark the check as expired
|
|
69
|
+
* @returns verifier object
|
|
70
|
+
*/
|
|
71
|
+
const assetVerifierParams = {
|
|
72
|
+
key: "address",
|
|
73
|
+
label: "asset",
|
|
74
|
+
fn: "getAssetState",
|
|
75
|
+
validate: (res) => res.state
|
|
76
|
+
};
|
|
77
|
+
const verifyAsset = require_verifier.create(assetVerifierParams);
|
|
78
|
+
const verifyAssetAsync = require_verifier.createAsync(assetVerifierParams);
|
|
79
|
+
|
|
80
|
+
//#endregion
|
|
81
|
+
exports.createVerifier = verifyTx;
|
|
82
|
+
exports.verifyTx = verifyTx;
|
|
83
|
+
exports.verifyAccount = verifyAccount;
|
|
84
|
+
exports.verifyAccountAsync = verifyAccountAsync;
|
|
85
|
+
exports.verifyAsset = verifyAsset;
|
|
86
|
+
exports.verifyAssetAsync = verifyAssetAsync;
|
|
87
|
+
exports.verifyTxAsync = verifyTxAsync;
|
package/lib/index.d.cts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CreateVerifierOptions, VerifierDoneEvent, VerifierEventEmitter, VerifyResponse } from "./verifier.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare const verifyTx: (params: CreateVerifierOptions) => VerifierEventEmitter;
|
|
5
|
+
declare const verifyTxAsync: (params: CreateVerifierOptions) => Promise<VerifierDoneEvent>;
|
|
6
|
+
declare const verifyAccount: (params: CreateVerifierOptions) => VerifierEventEmitter;
|
|
7
|
+
declare const verifyAccountAsync: (params: CreateVerifierOptions) => Promise<VerifierDoneEvent>;
|
|
8
|
+
declare const verifyAsset: (params: CreateVerifierOptions) => VerifierEventEmitter;
|
|
9
|
+
declare const verifyAssetAsync: (params: CreateVerifierOptions) => Promise<VerifierDoneEvent>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { type CreateVerifierOptions, type VerifierDoneEvent, type VerifierEventEmitter, type VerifyResponse, verifyTx as createVerifier, verifyTx, verifyAccount, verifyAccountAsync, verifyAsset, verifyAssetAsync, verifyTxAsync };
|
package/lib/package.cjs
ADDED
package/lib/verifier.cjs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_package = require('./package.cjs');
|
|
3
|
+
let node_events = require("node:events");
|
|
4
|
+
let _ocap_client = require("@ocap/client");
|
|
5
|
+
_ocap_client = require_rolldown_runtime.__toESM(_ocap_client);
|
|
6
|
+
let debug = require("debug");
|
|
7
|
+
debug = require_rolldown_runtime.__toESM(debug);
|
|
8
|
+
|
|
9
|
+
//#region src/verifier.ts
|
|
10
|
+
const debug$1 = (0, debug.default)(require_package.name);
|
|
11
|
+
const create = ({ key, label, fn, validate }) => (params) => {
|
|
12
|
+
const { chainHost, checkInterval = 1e3, autoStart = true, maxRetry = 20 } = params;
|
|
13
|
+
debug$1(`${label}.verify.params`, params);
|
|
14
|
+
if (!params[key]) throw new Error(`${key} must be provided to create ${label} verifier`);
|
|
15
|
+
if (!chainHost) throw new Error(`chainHost must be provided to create ${label} verifier`);
|
|
16
|
+
const client = new _ocap_client.default(chainHost);
|
|
17
|
+
if (typeof client[fn] !== "function") throw new Error(`Verify method ${fn} does not exist on client.`);
|
|
18
|
+
const param = params[key];
|
|
19
|
+
const events = new node_events.EventEmitter();
|
|
20
|
+
let retryCount = 0;
|
|
21
|
+
const doCheck = async () => {
|
|
22
|
+
if (retryCount > maxRetry) {
|
|
23
|
+
events.emit("error", {
|
|
24
|
+
[key]: params[key],
|
|
25
|
+
type: "expired",
|
|
26
|
+
retryCount,
|
|
27
|
+
error: /* @__PURE__ */ new Error("Transaction verifier exceeded max retry count")
|
|
28
|
+
});
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
retryCount += 1;
|
|
32
|
+
try {
|
|
33
|
+
const res = await client[fn]({ [key]: param });
|
|
34
|
+
if (res && res.code === "OK") try {
|
|
35
|
+
if (validate(res)) {
|
|
36
|
+
debug$1(`${label}.verify.done`, {
|
|
37
|
+
[key]: param,
|
|
38
|
+
retryCount
|
|
39
|
+
});
|
|
40
|
+
events.emit("done", {
|
|
41
|
+
[key]: param,
|
|
42
|
+
data: res
|
|
43
|
+
});
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
debug$1(`${label}.verify.pending`, {
|
|
47
|
+
[key]: param,
|
|
48
|
+
code: res.code,
|
|
49
|
+
retryCount
|
|
50
|
+
});
|
|
51
|
+
setTimeout(doCheck, checkInterval);
|
|
52
|
+
} catch (_err) {
|
|
53
|
+
events.emit("error", {
|
|
54
|
+
[key]: param,
|
|
55
|
+
type: "invalid",
|
|
56
|
+
retryCount,
|
|
57
|
+
data: res
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
debug$1(`${label}.verify.pending`, {
|
|
62
|
+
[key]: param,
|
|
63
|
+
code: res.code,
|
|
64
|
+
retryCount
|
|
65
|
+
});
|
|
66
|
+
setTimeout(doCheck, checkInterval);
|
|
67
|
+
}
|
|
68
|
+
} catch (err) {
|
|
69
|
+
console.error(`${label}.verify.error`, err);
|
|
70
|
+
setTimeout(doCheck, checkInterval);
|
|
71
|
+
events.emit("error", {
|
|
72
|
+
[key]: param,
|
|
73
|
+
type: "exception",
|
|
74
|
+
error: err,
|
|
75
|
+
retryCount
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
};
|
|
80
|
+
events.start = doCheck;
|
|
81
|
+
if (autoStart) doCheck();
|
|
82
|
+
return events;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Create an async verifier
|
|
86
|
+
*/
|
|
87
|
+
const createAsync = (verifierParams) => (params) => new Promise((resolve, reject) => {
|
|
88
|
+
const verifier = create(verifierParams)(params);
|
|
89
|
+
verifier.on("done", resolve);
|
|
90
|
+
verifier.on("error", reject);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
exports.create = create;
|
|
95
|
+
exports.createAsync = createAsync;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
|
|
3
|
+
//#region src/verifier.d.ts
|
|
4
|
+
interface VerifierParams {
|
|
5
|
+
key: string;
|
|
6
|
+
label: string;
|
|
7
|
+
fn: string;
|
|
8
|
+
validate: (res: VerifyResponse) => unknown;
|
|
9
|
+
}
|
|
10
|
+
interface VerifyResponse {
|
|
11
|
+
code?: string;
|
|
12
|
+
info?: {
|
|
13
|
+
code: string;
|
|
14
|
+
hash?: string;
|
|
15
|
+
};
|
|
16
|
+
state?: {
|
|
17
|
+
address: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
interface CreateVerifierOptions {
|
|
21
|
+
chainHost: string;
|
|
22
|
+
checkInterval?: number;
|
|
23
|
+
autoStart?: boolean;
|
|
24
|
+
maxRetry?: number;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
interface VerifierEventEmitter extends EventEmitter {
|
|
28
|
+
start: () => Promise<boolean>;
|
|
29
|
+
}
|
|
30
|
+
interface VerifierDoneEvent {
|
|
31
|
+
data: VerifyResponse;
|
|
32
|
+
[key: string]: unknown;
|
|
33
|
+
}
|
|
34
|
+
interface VerifierErrorEvent {
|
|
35
|
+
type: 'expired' | 'invalid' | 'exception';
|
|
36
|
+
retryCount: number;
|
|
37
|
+
error?: Error;
|
|
38
|
+
data?: VerifyResponse;
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
}
|
|
41
|
+
declare const create: ({
|
|
42
|
+
key,
|
|
43
|
+
label,
|
|
44
|
+
fn,
|
|
45
|
+
validate
|
|
46
|
+
}: VerifierParams) => (params: CreateVerifierOptions) => VerifierEventEmitter;
|
|
47
|
+
/**
|
|
48
|
+
* Create an async verifier
|
|
49
|
+
*/
|
|
50
|
+
declare const createAsync: (verifierParams: VerifierParams) => (params: CreateVerifierOptions) => Promise<VerifierDoneEvent>;
|
|
51
|
+
//#endregion
|
|
52
|
+
export { CreateVerifierOptions, VerifierDoneEvent, VerifierErrorEvent, VerifierEventEmitter, VerifierParams, VerifyResponse, create, createAsync };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/tx-util",
|
|
3
3
|
"description": "Utility to work with forge transactions",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.29.1",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "wangshijun",
|
|
7
7
|
"email": "shijun@arcblock.io",
|
|
@@ -17,13 +17,38 @@
|
|
|
17
17
|
"publishConfig": {
|
|
18
18
|
"access": "public"
|
|
19
19
|
},
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "./lib/index.cjs",
|
|
22
|
+
"module": "./esm/index.mjs",
|
|
23
|
+
"types": "./esm/index.d.mts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./esm/index.d.mts",
|
|
27
|
+
"import": "./esm/index.mjs",
|
|
28
|
+
"default": "./lib/index.cjs"
|
|
29
|
+
},
|
|
30
|
+
"./lib/*.js": {
|
|
31
|
+
"types": "./esm/*.d.mts",
|
|
32
|
+
"import": "./esm/*.mjs",
|
|
33
|
+
"default": "./lib/*.cjs"
|
|
34
|
+
},
|
|
35
|
+
"./lib/*": {
|
|
36
|
+
"types": "./esm/*.d.mts",
|
|
37
|
+
"import": "./esm/*.mjs",
|
|
38
|
+
"default": "./lib/*.cjs"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"lib",
|
|
43
|
+
"esm"
|
|
44
|
+
],
|
|
20
45
|
"dependencies": {
|
|
21
|
-
"@ocap/client": "1.
|
|
22
|
-
"@ocap/tx-protocols": "1.
|
|
23
|
-
"debug": "^4.3
|
|
46
|
+
"@ocap/client": "1.29.1",
|
|
47
|
+
"@ocap/tx-protocols": "1.29.1",
|
|
48
|
+
"debug": "^4.4.3"
|
|
24
49
|
},
|
|
25
50
|
"devDependencies": {
|
|
26
|
-
"@ocap/e2e-test": "1.
|
|
51
|
+
"@ocap/e2e-test": "1.29.1",
|
|
27
52
|
"remark-cli": "^10.0.1",
|
|
28
53
|
"remark-preset-github": "^4.0.4"
|
|
29
54
|
},
|
|
@@ -45,10 +70,6 @@
|
|
|
45
70
|
"nodejs"
|
|
46
71
|
],
|
|
47
72
|
"license": "Apache-2.0",
|
|
48
|
-
"main": "./lib/index.js",
|
|
49
|
-
"files": [
|
|
50
|
-
"lib"
|
|
51
|
-
],
|
|
52
73
|
"repository": {
|
|
53
74
|
"type": "git",
|
|
54
75
|
"url": "https://github.com/ArcBlock/blockchain/tree/master/core/tx-util"
|
|
@@ -56,10 +77,12 @@
|
|
|
56
77
|
"scripts": {
|
|
57
78
|
"lint": "biome check",
|
|
58
79
|
"lint:fix": "biome check --write",
|
|
80
|
+
"build": "tsdown",
|
|
81
|
+
"prebuild": "rm -rf lib esm",
|
|
59
82
|
"docs": "bun run gen-dts && bun run gen-docs && bun run cleanup-docs && bun run format-docs",
|
|
60
83
|
"cleanup-docs": "node ../../scripts/cleanup-docs.js docs/README.md $npm_package_name",
|
|
61
|
-
"gen-dts": "j2d lib/index.
|
|
62
|
-
"gen-docs": "jsdoc2md lib/index.
|
|
84
|
+
"gen-dts": "j2d lib/index.cjs",
|
|
85
|
+
"gen-docs": "jsdoc2md lib/index.cjs > docs/README.md",
|
|
63
86
|
"format-docs": "remark . -o",
|
|
64
87
|
"test": "bun test",
|
|
65
88
|
"coverage": "npm run test -- --coverage"
|
package/lib/index.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
// Generate by [js2dts@0.3.3](https://github.com/whxaxes/js2dts#readme)
|
|
2
|
-
|
|
3
|
-
import * as events from 'events';
|
|
4
|
-
declare const _Lib: _Lib.T100;
|
|
5
|
-
declare namespace _Lib {
|
|
6
|
-
export interface T100 {
|
|
7
|
-
createVerifier: (params: any) => events;
|
|
8
|
-
verifyTx: (params: any) => events;
|
|
9
|
-
verifyTxAsync: (params: any) => Promise<any>;
|
|
10
|
-
verifyAccount: (params: any) => events;
|
|
11
|
-
verifyAccountAsync: (params: any) => Promise<any>;
|
|
12
|
-
verifyAsset: (params: any) => events;
|
|
13
|
-
verifyAssetAsync: (params: any) => Promise<any>;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
export = _Lib;
|
package/lib/index.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
const { create, createAsync } = require('./verifier');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Generate a transaction verifier by tx hash
|
|
5
|
-
* Emit events on transaction included in a block
|
|
6
|
-
*
|
|
7
|
-
* - `error`: when there is something wrong when verify the transaction
|
|
8
|
-
* - `done`: when the transaction is successfully verified
|
|
9
|
-
*
|
|
10
|
-
* @param {object} params
|
|
11
|
-
* @param {string} params.hash - tx hash to check
|
|
12
|
-
* @param {string} params.chainHost - on which chain to check the tx
|
|
13
|
-
* @param {number} params.checkInterval - tx query interval to see the tx
|
|
14
|
-
* @param {boolean} params.autoStart - should the verifier start on create
|
|
15
|
-
* @param {number} params.maxRetry - max number of checks before mark the tx as expired
|
|
16
|
-
* @returns {EventEmitter} verifier object
|
|
17
|
-
*/
|
|
18
|
-
const txVerifierParams = {
|
|
19
|
-
key: 'hash',
|
|
20
|
-
label: 'tx',
|
|
21
|
-
fn: 'getTx',
|
|
22
|
-
validate: (res) => {
|
|
23
|
-
if (res.info) {
|
|
24
|
-
if (res.info.code === 'OK') {
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// If the tx failed, we throw an error
|
|
29
|
-
throw new Error(res.info.code);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return false;
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
const verifyTx = create(txVerifierParams);
|
|
36
|
-
const verifyTxAsync = createAsync(txVerifierParams);
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Generate a account verifier by address
|
|
40
|
-
* Emit events on account declared in a block
|
|
41
|
-
*
|
|
42
|
-
* - `error`: when there is something wrong when verify the transaction
|
|
43
|
-
* - `done`: when the transaction is successfully verified
|
|
44
|
-
*
|
|
45
|
-
* @param {object} params
|
|
46
|
-
* @param {string} params.address - account address to check
|
|
47
|
-
* @param {string} params.chainHost - on which chain to check the account
|
|
48
|
-
* @param {number} params.checkInterval - query interval to see the account
|
|
49
|
-
* @param {boolean} params.autoStart - should the verifier start on create
|
|
50
|
-
* @param {number} params.maxRetry - max number of checks before mark the check as expired
|
|
51
|
-
* @returns {EventEmitter} verifier object
|
|
52
|
-
*/
|
|
53
|
-
const accountVerifierParams = {
|
|
54
|
-
key: 'address',
|
|
55
|
-
label: 'account',
|
|
56
|
-
fn: 'getAccountState',
|
|
57
|
-
validate: (res) => res.state,
|
|
58
|
-
};
|
|
59
|
-
const verifyAccount = create(accountVerifierParams);
|
|
60
|
-
const verifyAccountAsync = createAsync(accountVerifierParams);
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Generate a asset verifier by address
|
|
64
|
-
* Emit events on asset can be found on chain
|
|
65
|
-
*
|
|
66
|
-
* - `error`: when there is something wrong when verify the asset
|
|
67
|
-
* - `done`: when the asset is found
|
|
68
|
-
*
|
|
69
|
-
* @param {object} params
|
|
70
|
-
* @param {string} params.address - asset address to check
|
|
71
|
-
* @param {string} params.chainHost - on which chain to check the asset
|
|
72
|
-
* @param {number} params.checkInterval - query interval to see the asset
|
|
73
|
-
* @param {boolean} params.autoStart - should the verifier start on create
|
|
74
|
-
* @param {number} params.maxRetry - max number of checks before mark the check as expired
|
|
75
|
-
* @returns {EventEmitter} verifier object
|
|
76
|
-
*/
|
|
77
|
-
const assetVerifierParams = {
|
|
78
|
-
key: 'address',
|
|
79
|
-
label: 'asset',
|
|
80
|
-
fn: 'getAssetState',
|
|
81
|
-
validate: (res) => res.state,
|
|
82
|
-
};
|
|
83
|
-
const verifyAsset = create(assetVerifierParams);
|
|
84
|
-
const verifyAssetAsync = createAsync(assetVerifierParams);
|
|
85
|
-
|
|
86
|
-
module.exports = {
|
|
87
|
-
createVerifier: verifyTx,
|
|
88
|
-
verifyTx,
|
|
89
|
-
verifyTxAsync,
|
|
90
|
-
verifyAccount,
|
|
91
|
-
verifyAccountAsync,
|
|
92
|
-
verifyAsset,
|
|
93
|
-
verifyAssetAsync,
|
|
94
|
-
};
|
package/lib/verifier.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
const Client = require('@ocap/client');
|
|
2
|
-
const { EventEmitter } = require('node:events');
|
|
3
|
-
|
|
4
|
-
const debug = require('debug')(require('../package.json').name);
|
|
5
|
-
|
|
6
|
-
const create =
|
|
7
|
-
({ key, label, fn, validate }) =>
|
|
8
|
-
(params) => {
|
|
9
|
-
const { chainHost, checkInterval = 1000, autoStart = true, maxRetry = 20 } = params;
|
|
10
|
-
debug(`${label}.verify.params`, params);
|
|
11
|
-
|
|
12
|
-
if (!params[key]) {
|
|
13
|
-
throw new Error(`${key} must be provided to create ${label} verifier`);
|
|
14
|
-
}
|
|
15
|
-
if (!chainHost) {
|
|
16
|
-
throw new Error(`chainHost must be provided to create ${label} verifier`);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const client = new Client(chainHost);
|
|
20
|
-
if (typeof client[fn] !== 'function') {
|
|
21
|
-
throw new Error(`Verify method ${fn} does not exist on client.`);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const param = params[key];
|
|
25
|
-
const events = new EventEmitter();
|
|
26
|
-
let retryCount = 0;
|
|
27
|
-
|
|
28
|
-
const doCheck = async () => {
|
|
29
|
-
if (retryCount > maxRetry) {
|
|
30
|
-
events.emit('error', {
|
|
31
|
-
[key]: params[key],
|
|
32
|
-
type: 'expired',
|
|
33
|
-
retryCount,
|
|
34
|
-
error: new Error('Transaction verifier exceeded max retry count'),
|
|
35
|
-
});
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
retryCount += 1;
|
|
40
|
-
|
|
41
|
-
try {
|
|
42
|
-
const res = await client[fn]({ [key]: param });
|
|
43
|
-
if (res && res.code === 'OK') {
|
|
44
|
-
try {
|
|
45
|
-
const result = validate(res);
|
|
46
|
-
if (result) {
|
|
47
|
-
debug(`${label}.verify.done`, { [key]: param, retryCount });
|
|
48
|
-
events.emit('done', { [key]: param, data: res });
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
debug(`${label}.verify.pending`, { [key]: param, code: res.code, retryCount });
|
|
52
|
-
setTimeout(doCheck, checkInterval);
|
|
53
|
-
} catch (_err) {
|
|
54
|
-
events.emit('error', {
|
|
55
|
-
[key]: param,
|
|
56
|
-
type: 'invalid',
|
|
57
|
-
retryCount,
|
|
58
|
-
data: res,
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
} else {
|
|
62
|
-
debug(`${label}.verify.pending`, { [key]: param, code: res.code, retryCount });
|
|
63
|
-
setTimeout(doCheck, checkInterval);
|
|
64
|
-
}
|
|
65
|
-
} catch (err) {
|
|
66
|
-
console.error(`${label}.verify.error`, err);
|
|
67
|
-
setTimeout(doCheck, checkInterval);
|
|
68
|
-
events.emit('error', {
|
|
69
|
-
[key]: param,
|
|
70
|
-
type: 'exception',
|
|
71
|
-
error: err,
|
|
72
|
-
retryCount,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return false;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
events.start = doCheck;
|
|
80
|
-
|
|
81
|
-
if (autoStart) {
|
|
82
|
-
doCheck();
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return events;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Create an async verifier
|
|
90
|
-
*/
|
|
91
|
-
const createAsync = (verifierParams) => (params) =>
|
|
92
|
-
new Promise((resolve, reject) => {
|
|
93
|
-
const verifier = create(verifierParams)(params);
|
|
94
|
-
verifier.on('done', resolve);
|
|
95
|
-
verifier.on('error', reject);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
module.exports = {
|
|
99
|
-
create,
|
|
100
|
-
createAsync,
|
|
101
|
-
};
|