@acala-network/chopsticks-testing 0.8.5-1 → 0.8.5-3
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/lib/check.js +95 -82
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/signFake.d.ts +6 -0
- package/lib/signFake.js +17 -0
- package/package.json +2 -2
package/lib/check.js
CHANGED
|
@@ -1,37 +1,50 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _Checker_instances, _Checker_value, _Checker_pipeline, _Checker_format, _Checker_message, _Checker_redactOptions, _Checker_redact;
|
|
1
13
|
import { expect } from 'vitest';
|
|
2
14
|
const processCodecOrArray = (codec, fn) => Array.isArray(codec) ? codec.map(fn) : fn(codec);
|
|
3
15
|
const toHuman = (codec) => processCodecOrArray(codec, (c) => c?.toHuman?.() ?? c);
|
|
4
16
|
const toHex = (codec) => processCodecOrArray(codec, (c) => c?.toHex?.() ?? c);
|
|
5
17
|
const toJson = (codec) => processCodecOrArray(codec, (c) => c?.toJSON?.() ?? c);
|
|
6
18
|
export class Checker {
|
|
7
|
-
#value;
|
|
8
|
-
#pipeline = [];
|
|
9
|
-
#format = 'json';
|
|
10
|
-
#message;
|
|
11
|
-
#redactOptions;
|
|
12
19
|
constructor(value, message) {
|
|
13
|
-
this
|
|
14
|
-
this
|
|
20
|
+
_Checker_instances.add(this);
|
|
21
|
+
_Checker_value.set(this, void 0);
|
|
22
|
+
_Checker_pipeline.set(this, []);
|
|
23
|
+
_Checker_format.set(this, 'json');
|
|
24
|
+
_Checker_message.set(this, void 0);
|
|
25
|
+
_Checker_redactOptions.set(this, void 0);
|
|
26
|
+
__classPrivateFieldSet(this, _Checker_value, value, "f");
|
|
27
|
+
__classPrivateFieldSet(this, _Checker_message, message, "f");
|
|
15
28
|
}
|
|
16
29
|
toHuman() {
|
|
17
|
-
this
|
|
30
|
+
__classPrivateFieldSet(this, _Checker_format, 'human', "f");
|
|
18
31
|
return this;
|
|
19
32
|
}
|
|
20
33
|
toHex() {
|
|
21
|
-
this
|
|
34
|
+
__classPrivateFieldSet(this, _Checker_format, 'hex', "f");
|
|
22
35
|
return this;
|
|
23
36
|
}
|
|
24
37
|
toJson() {
|
|
25
|
-
this
|
|
38
|
+
__classPrivateFieldSet(this, _Checker_format, 'json', "f");
|
|
26
39
|
return this;
|
|
27
40
|
}
|
|
28
41
|
message(message) {
|
|
29
|
-
this
|
|
42
|
+
__classPrivateFieldSet(this, _Checker_message, message, "f");
|
|
30
43
|
return this;
|
|
31
44
|
}
|
|
32
45
|
filterEvents(...filters) {
|
|
33
46
|
this.toHuman();
|
|
34
|
-
this
|
|
47
|
+
__classPrivateFieldGet(this, _Checker_pipeline, "f").push((value) => {
|
|
35
48
|
let data = value.map(({ event: { index: _, ...event } }) => event);
|
|
36
49
|
if (filters.length > 0) {
|
|
37
50
|
data = data.filter((evt) => {
|
|
@@ -51,83 +64,22 @@ export class Checker {
|
|
|
51
64
|
return this;
|
|
52
65
|
}
|
|
53
66
|
redact(options = { number: 2, hash: true }) {
|
|
54
|
-
this
|
|
55
|
-
...this
|
|
67
|
+
__classPrivateFieldSet(this, _Checker_redactOptions, {
|
|
68
|
+
...__classPrivateFieldGet(this, _Checker_redactOptions, "f"),
|
|
56
69
|
...options,
|
|
57
|
-
};
|
|
70
|
+
}, "f");
|
|
58
71
|
return this;
|
|
59
72
|
}
|
|
60
|
-
#redact(value) {
|
|
61
|
-
if (!this.#redactOptions) {
|
|
62
|
-
return value;
|
|
63
|
-
}
|
|
64
|
-
const redactNumber = this.#redactOptions.number === true || typeof this.#redactOptions.number === 'number';
|
|
65
|
-
const precision = redactNumber
|
|
66
|
-
? typeof this.#redactOptions.number === 'number'
|
|
67
|
-
? this.#redactOptions.number
|
|
68
|
-
: 0
|
|
69
|
-
: 0;
|
|
70
|
-
const redactHash = this.#redactOptions.hash === true;
|
|
71
|
-
const redactHex = this.#redactOptions.hex === true;
|
|
72
|
-
const redactAddress = this.#redactOptions.address === true;
|
|
73
|
-
const processNumber = (value) => {
|
|
74
|
-
if (precision > 0) {
|
|
75
|
-
const rounded = parseFloat(value.toPrecision(precision));
|
|
76
|
-
if (rounded === value) {
|
|
77
|
-
return rounded;
|
|
78
|
-
}
|
|
79
|
-
return `(rounded ${rounded})`;
|
|
80
|
-
}
|
|
81
|
-
return '(number)';
|
|
82
|
-
};
|
|
83
|
-
const process = (obj) => {
|
|
84
|
-
if (obj == null) {
|
|
85
|
-
return obj;
|
|
86
|
-
}
|
|
87
|
-
if (Array.isArray(obj)) {
|
|
88
|
-
return obj.map(process);
|
|
89
|
-
}
|
|
90
|
-
if (redactNumber && typeof obj === 'number') {
|
|
91
|
-
return processNumber(obj);
|
|
92
|
-
}
|
|
93
|
-
if (typeof obj === 'string') {
|
|
94
|
-
if (redactNumber && obj.match(/0x000000[0-9a-f]{26}/)) {
|
|
95
|
-
// this is very likely u128 encoded in hex
|
|
96
|
-
const num = parseInt(obj);
|
|
97
|
-
return processNumber(num);
|
|
98
|
-
}
|
|
99
|
-
if (redactHash && obj.match(/0x[0-9a-f]{64}/)) {
|
|
100
|
-
return '(hash)';
|
|
101
|
-
}
|
|
102
|
-
if (redactHex && obj.match(/0x[0-9a-f]+/)) {
|
|
103
|
-
return '(hex)';
|
|
104
|
-
}
|
|
105
|
-
if (redactAddress && obj.match(/^[1-9A-HJ-NP-Za-km-z]{46,48}$/)) {
|
|
106
|
-
return '(address)';
|
|
107
|
-
}
|
|
108
|
-
if (redactNumber && obj.match(/^-?[\d,]+$/)) {
|
|
109
|
-
const num = parseInt(obj.replace(/,/g, ''));
|
|
110
|
-
return processNumber(num);
|
|
111
|
-
}
|
|
112
|
-
return obj;
|
|
113
|
-
}
|
|
114
|
-
if (typeof obj === 'object') {
|
|
115
|
-
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, process(v)]));
|
|
116
|
-
}
|
|
117
|
-
return obj;
|
|
118
|
-
};
|
|
119
|
-
return process(value);
|
|
120
|
-
}
|
|
121
73
|
map(fn) {
|
|
122
|
-
this
|
|
74
|
+
__classPrivateFieldGet(this, _Checker_pipeline, "f").push(fn);
|
|
123
75
|
return this;
|
|
124
76
|
}
|
|
125
77
|
pipe(fn) {
|
|
126
78
|
return fn ? fn(this) : this;
|
|
127
79
|
}
|
|
128
80
|
async value() {
|
|
129
|
-
let value = await this
|
|
130
|
-
switch (this
|
|
81
|
+
let value = await __classPrivateFieldGet(this, _Checker_value, "f");
|
|
82
|
+
switch (__classPrivateFieldGet(this, _Checker_format, "f")) {
|
|
131
83
|
case 'human':
|
|
132
84
|
value = toHuman(value);
|
|
133
85
|
break;
|
|
@@ -138,16 +90,77 @@ export class Checker {
|
|
|
138
90
|
value = toJson(value);
|
|
139
91
|
break;
|
|
140
92
|
}
|
|
141
|
-
for (const fn of this
|
|
93
|
+
for (const fn of __classPrivateFieldGet(this, _Checker_pipeline, "f")) {
|
|
142
94
|
value = await fn(value);
|
|
143
95
|
}
|
|
144
|
-
value = this
|
|
96
|
+
value = __classPrivateFieldGet(this, _Checker_instances, "m", _Checker_redact).call(this, value);
|
|
145
97
|
return value;
|
|
146
98
|
}
|
|
147
99
|
async toMatchSnapshot(msg) {
|
|
148
|
-
return expect(await this.value()).toMatchSnapshot(msg ?? this
|
|
100
|
+
return expect(await this.value()).toMatchSnapshot(msg ?? __classPrivateFieldGet(this, _Checker_message, "f"));
|
|
149
101
|
}
|
|
150
102
|
}
|
|
103
|
+
_Checker_value = new WeakMap(), _Checker_pipeline = new WeakMap(), _Checker_format = new WeakMap(), _Checker_message = new WeakMap(), _Checker_redactOptions = new WeakMap(), _Checker_instances = new WeakSet(), _Checker_redact = function _Checker_redact(value) {
|
|
104
|
+
if (!__classPrivateFieldGet(this, _Checker_redactOptions, "f")) {
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
const redactNumber = __classPrivateFieldGet(this, _Checker_redactOptions, "f").number === true || typeof __classPrivateFieldGet(this, _Checker_redactOptions, "f").number === 'number';
|
|
108
|
+
const precision = redactNumber
|
|
109
|
+
? typeof __classPrivateFieldGet(this, _Checker_redactOptions, "f").number === 'number'
|
|
110
|
+
? __classPrivateFieldGet(this, _Checker_redactOptions, "f").number
|
|
111
|
+
: 0
|
|
112
|
+
: 0;
|
|
113
|
+
const redactHash = __classPrivateFieldGet(this, _Checker_redactOptions, "f").hash === true;
|
|
114
|
+
const redactHex = __classPrivateFieldGet(this, _Checker_redactOptions, "f").hex === true;
|
|
115
|
+
const redactAddress = __classPrivateFieldGet(this, _Checker_redactOptions, "f").address === true;
|
|
116
|
+
const processNumber = (value) => {
|
|
117
|
+
if (precision > 0) {
|
|
118
|
+
const rounded = parseFloat(value.toPrecision(precision));
|
|
119
|
+
if (rounded === value) {
|
|
120
|
+
return rounded;
|
|
121
|
+
}
|
|
122
|
+
return `(rounded ${rounded})`;
|
|
123
|
+
}
|
|
124
|
+
return '(number)';
|
|
125
|
+
};
|
|
126
|
+
const process = (obj) => {
|
|
127
|
+
if (obj == null) {
|
|
128
|
+
return obj;
|
|
129
|
+
}
|
|
130
|
+
if (Array.isArray(obj)) {
|
|
131
|
+
return obj.map(process);
|
|
132
|
+
}
|
|
133
|
+
if (redactNumber && typeof obj === 'number') {
|
|
134
|
+
return processNumber(obj);
|
|
135
|
+
}
|
|
136
|
+
if (typeof obj === 'string') {
|
|
137
|
+
if (redactNumber && obj.match(/0x000000[0-9a-f]{26}/)) {
|
|
138
|
+
// this is very likely u128 encoded in hex
|
|
139
|
+
const num = parseInt(obj);
|
|
140
|
+
return processNumber(num);
|
|
141
|
+
}
|
|
142
|
+
if (redactHash && obj.match(/0x[0-9a-f]{64}/)) {
|
|
143
|
+
return '(hash)';
|
|
144
|
+
}
|
|
145
|
+
if (redactHex && obj.match(/0x[0-9a-f]+/)) {
|
|
146
|
+
return '(hex)';
|
|
147
|
+
}
|
|
148
|
+
if (redactAddress && obj.match(/^[1-9A-HJ-NP-Za-km-z]{46,48}$/)) {
|
|
149
|
+
return '(address)';
|
|
150
|
+
}
|
|
151
|
+
if (redactNumber && obj.match(/^-?[\d,]+$/)) {
|
|
152
|
+
const num = parseInt(obj.replace(/,/g, ''));
|
|
153
|
+
return processNumber(num);
|
|
154
|
+
}
|
|
155
|
+
return obj;
|
|
156
|
+
}
|
|
157
|
+
if (typeof obj === 'object') {
|
|
158
|
+
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, process(v)]));
|
|
159
|
+
}
|
|
160
|
+
return obj;
|
|
161
|
+
};
|
|
162
|
+
return process(value);
|
|
163
|
+
};
|
|
151
164
|
export const check = (value, msg) => {
|
|
152
165
|
if (value instanceof Checker) {
|
|
153
166
|
if (msg) {
|
package/lib/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { HexString } from '@polkadot/util/types';
|
|
|
6
6
|
import { Keyring } from '@polkadot/keyring';
|
|
7
7
|
import { SubmittableExtrinsic } from '@polkadot/api-base/types';
|
|
8
8
|
export * from './check';
|
|
9
|
+
export * from './signFake';
|
|
9
10
|
export type SetupOption = {
|
|
10
11
|
endpoint: string;
|
|
11
12
|
blockNumber?: number;
|
package/lib/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import { BuildBlockMode, connectParachains, connectVertical, fetchConfig, setupW
|
|
|
3
3
|
import { Keyring, createTestKeyring } from '@polkadot/keyring';
|
|
4
4
|
import { expect } from 'vitest';
|
|
5
5
|
export * from './check';
|
|
6
|
+
export * from './signFake';
|
|
6
7
|
export const createConfig = ({ endpoint, blockNumber, blockHash, wasmOverride, db, timeout, port, maxMemoryBlockCount, resume, }) => {
|
|
7
8
|
// random port if not specified
|
|
8
9
|
port = port ?? Math.floor(Math.random() * 10000) + 10000;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ApiPromise } from '@polkadot/api';
|
|
2
|
+
import { GenericExtrinsic } from '@polkadot/types';
|
|
3
|
+
import { SignatureOptions } from '@polkadot/types/types';
|
|
4
|
+
export type SignFakeOptions = Partial<SignatureOptions>;
|
|
5
|
+
export declare const signFakeWithApi: (api: ApiPromise, tx: GenericExtrinsic, addr: string, options?: SignFakeOptions) => Promise<void>;
|
|
6
|
+
export declare const signFake: (tx: GenericExtrinsic, addr: string, options: SignatureOptions) => void;
|
package/lib/signFake.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const signFakeWithApi = async (api, tx, addr, options = {}) => {
|
|
2
|
+
const nonce = options.nonce ?? (await api.query.system.account(addr)).nonce;
|
|
3
|
+
signFake(tx, addr, {
|
|
4
|
+
nonce,
|
|
5
|
+
genesisHash: api.genesisHash,
|
|
6
|
+
runtimeVersion: api.runtimeVersion,
|
|
7
|
+
blockHash: api.genesisHash,
|
|
8
|
+
...options,
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
export const signFake = (tx, addr, options) => {
|
|
12
|
+
const mockSignature = new Uint8Array(64);
|
|
13
|
+
mockSignature.fill(0xcd);
|
|
14
|
+
mockSignature.set([0xde, 0xad, 0xbe, 0xef]);
|
|
15
|
+
tx.signFake(addr, options);
|
|
16
|
+
tx.signature.set(mockSignature);
|
|
17
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acala-network/chopsticks-testing",
|
|
3
|
-
"version": "0.8.5-
|
|
3
|
+
"version": "0.8.5-3",
|
|
4
4
|
"author": "Acala Developers <hello@acala.network>",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"scripts": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"build": "tsc -p ./tsconfig.json"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@acala-network/chopsticks": "0.8.5-
|
|
11
|
+
"@acala-network/chopsticks": "0.8.5-3"
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {
|
|
14
14
|
"vitest": "^0.34.0"
|