@xyo-network/module-abstract 2.75.0 → 2.75.2
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/dist/browser/AbstractModule.d.cts +84 -0
- package/dist/browser/AbstractModule.d.cts.map +1 -0
- package/dist/browser/AbstractModule.js +16 -182
- package/dist/browser/AbstractModule.js.map +1 -1
- package/dist/browser/AbstractModuleInstance.d.cts +14 -0
- package/dist/browser/AbstractModuleInstance.d.cts.map +1 -0
- package/dist/browser/AbstractModuleInstance.js +4 -738
- package/dist/browser/AbstractModuleInstance.js.map +1 -1
- package/dist/browser/BaseEmitter.d.cts +17 -0
- package/dist/browser/BaseEmitter.d.cts.map +1 -0
- package/dist/browser/BaseEmitter.js +2 -4
- package/dist/browser/BaseEmitter.js.map +1 -1
- package/dist/browser/Error.d.cts +15 -0
- package/dist/browser/Error.d.cts.map +1 -0
- package/dist/browser/Error.js +2 -4
- package/dist/browser/Error.js.map +1 -1
- package/dist/browser/QueryValidator/ModuleConfigQueryValidator.d.cts +15 -0
- package/dist/browser/QueryValidator/ModuleConfigQueryValidator.d.cts.map +1 -0
- package/dist/browser/QueryValidator/ModuleConfigQueryValidator.js +4 -6
- package/dist/browser/QueryValidator/ModuleConfigQueryValidator.js.map +1 -1
- package/dist/browser/QueryValidator/QueryValidator.d.cts +8 -0
- package/dist/browser/QueryValidator/QueryValidator.d.cts.map +1 -0
- package/dist/browser/QueryValidator/SupportedQueryValidator.d.cts +22 -0
- package/dist/browser/QueryValidator/SupportedQueryValidator.d.cts.map +1 -0
- package/dist/browser/QueryValidator/SupportedQueryValidator.js +3 -5
- package/dist/browser/QueryValidator/SupportedQueryValidator.js.map +1 -1
- package/dist/browser/QueryValidator/index.d.cts +4 -0
- package/dist/browser/QueryValidator/index.d.cts.map +1 -0
- package/dist/browser/QueryValidator/index.js +3 -86
- package/dist/browser/QueryValidator/index.js.map +1 -1
- package/dist/browser/index.d.cts +6 -0
- package/dist/browser/index.d.cts.map +1 -0
- package/dist/browser/index.js +5 -782
- package/dist/browser/index.js.map +1 -1
- package/dist/docs.json +42701 -0
- package/dist/node/AbstractModule.d.cts +84 -0
- package/dist/node/AbstractModule.d.cts.map +1 -0
- package/dist/node/AbstractModule.js +240 -56
- package/dist/node/AbstractModule.js.map +1 -1
- package/dist/node/AbstractModule.mjs +232 -48
- package/dist/node/AbstractModule.mjs.map +1 -1
- package/dist/node/AbstractModuleInstance.d.cts +14 -0
- package/dist/node/AbstractModuleInstance.d.cts.map +1 -0
- package/dist/node/AbstractModuleInstance.js +757 -6
- package/dist/node/AbstractModuleInstance.js.map +1 -1
- package/dist/node/AbstractModuleInstance.mjs +757 -6
- package/dist/node/AbstractModuleInstance.mjs.map +1 -1
- package/dist/node/BaseEmitter.d.cts +17 -0
- package/dist/node/BaseEmitter.d.cts.map +1 -0
- package/dist/node/BaseEmitter.js +6 -5
- package/dist/node/BaseEmitter.js.map +1 -1
- package/dist/node/BaseEmitter.mjs +6 -5
- package/dist/node/BaseEmitter.mjs.map +1 -1
- package/dist/node/Error.d.cts +15 -0
- package/dist/node/Error.d.cts.map +1 -0
- package/dist/node/Error.js +6 -5
- package/dist/node/Error.js.map +1 -1
- package/dist/node/Error.mjs +6 -5
- package/dist/node/Error.mjs.map +1 -1
- package/dist/node/QueryValidator/ModuleConfigQueryValidator.d.cts +15 -0
- package/dist/node/QueryValidator/ModuleConfigQueryValidator.d.cts.map +1 -0
- package/dist/node/QueryValidator/ModuleConfigQueryValidator.js +23 -14
- package/dist/node/QueryValidator/ModuleConfigQueryValidator.js.map +1 -1
- package/dist/node/QueryValidator/ModuleConfigQueryValidator.mjs +23 -14
- package/dist/node/QueryValidator/ModuleConfigQueryValidator.mjs.map +1 -1
- package/dist/node/QueryValidator/QueryValidator.d.cts +8 -0
- package/dist/node/QueryValidator/QueryValidator.d.cts.map +1 -0
- package/dist/node/QueryValidator/QueryValidator.js +2 -0
- package/dist/node/QueryValidator/QueryValidator.js.map +1 -1
- package/dist/node/QueryValidator/SupportedQueryValidator.d.cts +22 -0
- package/dist/node/QueryValidator/SupportedQueryValidator.d.cts.map +1 -0
- package/dist/node/QueryValidator/SupportedQueryValidator.js +7 -6
- package/dist/node/QueryValidator/SupportedQueryValidator.js.map +1 -1
- package/dist/node/QueryValidator/SupportedQueryValidator.mjs +7 -6
- package/dist/node/QueryValidator/SupportedQueryValidator.mjs.map +1 -1
- package/dist/node/QueryValidator/index.d.cts +4 -0
- package/dist/node/QueryValidator/index.d.cts.map +1 -0
- package/dist/node/QueryValidator/index.js +100 -7
- package/dist/node/QueryValidator/index.js.map +1 -1
- package/dist/node/QueryValidator/index.mjs +92 -3
- package/dist/node/QueryValidator/index.mjs.map +1 -1
- package/dist/node/index.d.cts +6 -0
- package/dist/node/index.d.cts.map +1 -0
- package/dist/node/index.js +811 -11
- package/dist/node/index.js.map +1 -1
- package/dist/node/index.mjs +799 -5
- package/dist/node/index.mjs.map +1 -1
- package/package.json +26 -26
package/dist/browser/index.js
CHANGED
|
@@ -1,783 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { exists } from "@xylabs/exists";
|
|
7
|
-
import { compact } from "@xylabs/lodash";
|
|
8
|
-
import { HDWallet } from "@xyo-network/account";
|
|
9
|
-
import { AddressSchema } from "@xyo-network/address-payload-plugin";
|
|
10
|
-
import { asArchivistInstance } from "@xyo-network/archivist-model";
|
|
11
|
-
import { BoundWitnessBuilder, QueryBoundWitnessBuilder, QueryBoundWitnessWrapper as QueryBoundWitnessWrapper3 } from "@xyo-network/boundwitness-builder";
|
|
12
|
-
import { ConfigSchema } from "@xyo-network/config-payload-plugin";
|
|
13
|
-
import { PayloadHasher } from "@xyo-network/core";
|
|
14
|
-
import { handleError, handleErrorAsync } from "@xyo-network/error";
|
|
15
|
-
import { ModuleManifestPayloadSchema } from "@xyo-network/manifest-model";
|
|
16
|
-
import { AddressPreviousHashSchema, duplicateModules, ModuleAddressQuerySchema, ModuleDescribeQuerySchema, ModuleDescriptionSchema, ModuleDiscoverQuerySchema, ModuleFactory, ModuleManifestQuerySchema, ModuleSubscribeQuerySchema, serializableField } from "@xyo-network/module-model";
|
|
17
|
-
import { CompositeModuleResolver } from "@xyo-network/module-resolver";
|
|
18
|
-
import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
|
|
19
|
-
import { PromiseEx } from "@xyo-network/promise";
|
|
20
|
-
import { QuerySchema } from "@xyo-network/query-payload-plugin";
|
|
21
|
-
import { IdLogger } from "@xyo-network/shared";
|
|
22
|
-
|
|
23
|
-
// src/BaseEmitter.ts
|
|
24
|
-
import { Base } from "@xyo-network/core";
|
|
25
|
-
import { Events } from "@xyo-network/module-events";
|
|
26
|
-
var BaseEmitter = class extends Base {
|
|
27
|
-
static {
|
|
28
|
-
__name(this, "BaseEmitter");
|
|
29
|
-
}
|
|
30
|
-
//just here to query types
|
|
31
|
-
eventData = {};
|
|
32
|
-
events;
|
|
33
|
-
constructor(params) {
|
|
34
|
-
super(params);
|
|
35
|
-
this.events = new Events();
|
|
36
|
-
}
|
|
37
|
-
clearListeners(eventNames) {
|
|
38
|
-
return this.events.clearListeners(eventNames);
|
|
39
|
-
}
|
|
40
|
-
emit(eventName, eventArgs) {
|
|
41
|
-
return this.events.emit(eventName, eventArgs);
|
|
42
|
-
}
|
|
43
|
-
emitSerial(eventName, eventArgs) {
|
|
44
|
-
return this.events.emitSerial(eventName, eventArgs);
|
|
45
|
-
}
|
|
46
|
-
listenerCount(eventNames) {
|
|
47
|
-
return this.events.listenerCount(eventNames);
|
|
48
|
-
}
|
|
49
|
-
off(eventNames, listener) {
|
|
50
|
-
return this.events.off(eventNames, listener);
|
|
51
|
-
}
|
|
52
|
-
offAny(listener) {
|
|
53
|
-
return this.events.offAny(listener);
|
|
54
|
-
}
|
|
55
|
-
on(eventNames, listener) {
|
|
56
|
-
return this.events.on(eventNames, listener);
|
|
57
|
-
}
|
|
58
|
-
onAny(listener) {
|
|
59
|
-
return this.events.onAny(listener);
|
|
60
|
-
}
|
|
61
|
-
once(eventName, listener) {
|
|
62
|
-
return this.events.once(eventName, listener);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
// src/Error.ts
|
|
67
|
-
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
68
|
-
import { ModuleErrorSchema } from "@xyo-network/payload-model";
|
|
69
|
-
var ModuleErrorBuilder = class extends PayloadBuilder {
|
|
70
|
-
static {
|
|
71
|
-
__name(this, "ModuleErrorBuilder");
|
|
72
|
-
}
|
|
73
|
-
_message;
|
|
74
|
-
_name;
|
|
75
|
-
_query;
|
|
76
|
-
_sources;
|
|
77
|
-
constructor() {
|
|
78
|
-
super({
|
|
79
|
-
schema: ModuleErrorSchema
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
build() {
|
|
83
|
-
return {
|
|
84
|
-
message: this._message,
|
|
85
|
-
name: this._name,
|
|
86
|
-
query: this._query,
|
|
87
|
-
schema: ModuleErrorSchema,
|
|
88
|
-
sources: this._sources
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
message(message) {
|
|
92
|
-
this._message = message;
|
|
93
|
-
return this;
|
|
94
|
-
}
|
|
95
|
-
name(name) {
|
|
96
|
-
this._name = name;
|
|
97
|
-
return this;
|
|
98
|
-
}
|
|
99
|
-
query(query) {
|
|
100
|
-
this._query = query;
|
|
101
|
-
return this;
|
|
102
|
-
}
|
|
103
|
-
sources(sources) {
|
|
104
|
-
this._sources = sources;
|
|
105
|
-
return this;
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
// src/QueryValidator/ModuleConfigQueryValidator.ts
|
|
110
|
-
import { QueryBoundWitnessWrapper } from "@xyo-network/boundwitness-builder";
|
|
111
|
-
var delimiter = "";
|
|
112
|
-
var ModuleConfigQueryValidator = class {
|
|
113
|
-
static {
|
|
114
|
-
__name(this, "ModuleConfigQueryValidator");
|
|
115
|
-
}
|
|
116
|
-
allowed = {};
|
|
117
|
-
disallowed = {};
|
|
118
|
-
hasAllowedRules;
|
|
119
|
-
hasDisallowedRules;
|
|
120
|
-
hasRules;
|
|
121
|
-
constructor(config) {
|
|
122
|
-
if (config?.security?.allowed) {
|
|
123
|
-
Object.entries(config.security?.allowed).forEach(([schema, addresses]) => {
|
|
124
|
-
this.allowed[schema] = addresses.map(toAddressesString);
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
if (config?.security?.disallowed) {
|
|
128
|
-
Object.entries(config.security?.disallowed).forEach(([schema, addresses]) => {
|
|
129
|
-
this.disallowed[schema] = addresses.map(toAddressesString);
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
this.hasAllowedRules = Object.keys(this.allowed).length > 0;
|
|
133
|
-
this.hasDisallowedRules = Object.keys(this.disallowed).length > 0;
|
|
134
|
-
this.hasRules = this.hasAllowedRules || this.hasDisallowedRules;
|
|
135
|
-
}
|
|
136
|
-
queryable = async (query, payloads) => {
|
|
137
|
-
if (!this.hasRules)
|
|
138
|
-
return true;
|
|
139
|
-
const addresses = query.addresses;
|
|
140
|
-
if (!addresses.length)
|
|
141
|
-
return false;
|
|
142
|
-
const wrapper = QueryBoundWitnessWrapper.parseQuery(query, payloads);
|
|
143
|
-
const schema = (await wrapper.getQuery()).schema;
|
|
144
|
-
return this.queryAllowed(schema, addresses) && !this.queryDisallowed(schema, addresses);
|
|
145
|
-
};
|
|
146
|
-
queryAllowed = (schema, addresses) => {
|
|
147
|
-
if (!this.hasAllowedRules)
|
|
148
|
-
return true;
|
|
149
|
-
if (addresses.length > 1) {
|
|
150
|
-
const signatories = toAddressesString(addresses);
|
|
151
|
-
const validCosigners = this.allowed?.[schema]?.includes(signatories);
|
|
152
|
-
if (validCosigners)
|
|
153
|
-
return true;
|
|
154
|
-
}
|
|
155
|
-
return addresses.every((address) => this.allowed?.[schema]?.includes(address) || false);
|
|
156
|
-
};
|
|
157
|
-
queryDisallowed = (schema, addresses) => {
|
|
158
|
-
if (!this.hasDisallowedRules)
|
|
159
|
-
return false;
|
|
160
|
-
return addresses.some((address) => this.disallowed?.[schema]?.includes(address));
|
|
161
|
-
};
|
|
162
|
-
};
|
|
163
|
-
var toAddressesString = /* @__PURE__ */ __name((addresses) => {
|
|
164
|
-
return Array.isArray(addresses) ? addresses.sort().map((address) => address.toLowerCase()).join(delimiter) : addresses.toLowerCase();
|
|
165
|
-
}, "toAddressesString");
|
|
166
|
-
|
|
167
|
-
// src/QueryValidator/SupportedQueryValidator.ts
|
|
168
|
-
import { QueryBoundWitnessWrapper as QueryBoundWitnessWrapper2 } from "@xyo-network/boundwitness-builder";
|
|
169
|
-
var isQuerySupportedByModule = /* @__PURE__ */ __name(async (mod, query, payloads) => {
|
|
170
|
-
const wrapper = QueryBoundWitnessWrapper2.parseQuery(query, payloads);
|
|
171
|
-
const schema = (await wrapper.getQuery()).schema;
|
|
172
|
-
return mod.queries.includes(schema);
|
|
173
|
-
}, "isQuerySupportedByModule");
|
|
174
|
-
var SupportedQueryValidator = class {
|
|
175
|
-
static {
|
|
176
|
-
__name(this, "SupportedQueryValidator");
|
|
177
|
-
}
|
|
178
|
-
mod;
|
|
179
|
-
constructor(mod) {
|
|
180
|
-
this.mod = mod;
|
|
181
|
-
this.queryable = (query, payloads) => {
|
|
182
|
-
return isQuerySupportedByModule(this.mod, query, payloads);
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
queryable;
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
// src/AbstractModule.ts
|
|
189
|
-
var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
190
|
-
static {
|
|
191
|
-
__name(this, "AbstractModule");
|
|
192
|
-
}
|
|
193
|
-
static configSchemas;
|
|
194
|
-
static enableLazyLoad = false;
|
|
195
|
-
static privateConstructorKey = Date.now().toString();
|
|
196
|
-
downResolver = new CompositeModuleResolver();
|
|
197
|
-
upResolver = new CompositeModuleResolver();
|
|
198
|
-
_account = void 0;
|
|
199
|
-
_baseModuleQueryAccountPaths = {
|
|
200
|
-
[ModuleAddressQuerySchema]: "1",
|
|
201
|
-
[ModuleDescribeQuerySchema]: "4",
|
|
202
|
-
[ModuleDiscoverQuerySchema]: "2",
|
|
203
|
-
[ModuleManifestQuerySchema]: "5",
|
|
204
|
-
[ModuleSubscribeQuerySchema]: "3"
|
|
205
|
-
};
|
|
206
|
-
_queryAccounts = {
|
|
207
|
-
[ModuleAddressQuerySchema]: void 0,
|
|
208
|
-
[ModuleDescribeQuerySchema]: void 0,
|
|
209
|
-
[ModuleDiscoverQuerySchema]: void 0,
|
|
210
|
-
[ModuleManifestQuerySchema]: void 0,
|
|
211
|
-
[ModuleSubscribeQuerySchema]: void 0
|
|
212
|
-
};
|
|
213
|
-
_startPromise = void 0;
|
|
214
|
-
_started = void 0;
|
|
215
|
-
moduleConfigQueryValidator;
|
|
216
|
-
supportedQueryValidator;
|
|
217
|
-
_busyCount = 0;
|
|
218
|
-
constructor(privateConstructorKey, params, account) {
|
|
219
|
-
assertEx(_AbstractModule.privateConstructorKey === privateConstructorKey, "Use create function instead of constructor");
|
|
220
|
-
const mutatedParams = {
|
|
221
|
-
...params
|
|
222
|
-
};
|
|
223
|
-
super(mutatedParams);
|
|
224
|
-
this._account = account;
|
|
225
|
-
this.supportedQueryValidator = new SupportedQueryValidator(this).queryable;
|
|
226
|
-
this.moduleConfigQueryValidator = new ModuleConfigQueryValidator(mutatedParams?.config).queryable;
|
|
227
|
-
}
|
|
228
|
-
static get configSchema() {
|
|
229
|
-
return this.configSchemas[0];
|
|
230
|
-
}
|
|
231
|
-
get account() {
|
|
232
|
-
return assertEx(this._account, "Missing account");
|
|
233
|
-
}
|
|
234
|
-
get address() {
|
|
235
|
-
return this.account.address;
|
|
236
|
-
}
|
|
237
|
-
get allowAnonymous() {
|
|
238
|
-
return !!this.config.security?.allowAnonymous;
|
|
239
|
-
}
|
|
240
|
-
get config() {
|
|
241
|
-
return this.params.config;
|
|
242
|
-
}
|
|
243
|
-
get ephemeralQueryAccountEnabled() {
|
|
244
|
-
return !!this.params.ephemeralQueryAccountEnabled;
|
|
245
|
-
}
|
|
246
|
-
get queries() {
|
|
247
|
-
return [
|
|
248
|
-
ModuleDiscoverQuerySchema,
|
|
249
|
-
ModuleAddressQuerySchema,
|
|
250
|
-
ModuleSubscribeQuerySchema,
|
|
251
|
-
ModuleDescribeQuerySchema,
|
|
252
|
-
ModuleManifestQuerySchema
|
|
253
|
-
];
|
|
254
|
-
}
|
|
255
|
-
get queryAccountPaths() {
|
|
256
|
-
return {
|
|
257
|
-
...this._baseModuleQueryAccountPaths,
|
|
258
|
-
...this._queryAccountPaths
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
get queryAccounts() {
|
|
262
|
-
return this._queryAccounts;
|
|
263
|
-
}
|
|
264
|
-
get timestamp() {
|
|
265
|
-
return this.config.timestamp ?? false;
|
|
266
|
-
}
|
|
267
|
-
static _getRootFunction(funcName) {
|
|
268
|
-
let anyThis = this;
|
|
269
|
-
while (anyThis.__proto__[funcName]) {
|
|
270
|
-
anyThis = anyThis.__proto__;
|
|
271
|
-
}
|
|
272
|
-
return anyThis[funcName];
|
|
273
|
-
}
|
|
274
|
-
static _noOverride(functionName) {
|
|
275
|
-
const thisFunc = this[functionName];
|
|
276
|
-
const rootFunc = this._getRootFunction(functionName);
|
|
277
|
-
assertEx(thisFunc === rootFunc, `Override not allowed for [${functionName}] - override ${functionName}Handler instead`);
|
|
278
|
-
}
|
|
279
|
-
static async create(params) {
|
|
280
|
-
this._noOverride("create");
|
|
281
|
-
if (!this.configSchemas || this.configSchemas.length === 0) {
|
|
282
|
-
throw Error(`Missing configSchema [${params?.config?.schema}][${this.name}]`);
|
|
283
|
-
}
|
|
284
|
-
const { account, config, wallet } = params ?? {};
|
|
285
|
-
const { accountDerivationPath } = config ?? {};
|
|
286
|
-
assertEx(!(!!account && !!wallet), `Specifying both {account} and {wallet} are not allowed [${config?.schema}] [${account === "random" ? "random" : account?.address}, ${wallet?.address}]`);
|
|
287
|
-
const schema = params?.config?.schema ?? this.configSchema;
|
|
288
|
-
const allowedSchemas = this.configSchemas;
|
|
289
|
-
assertEx(allowedSchemas.filter((allowedSchema) => allowedSchema === schema).length > 0, `Bad Config Schema [Received ${schema}] [Expected ${JSON.stringify(allowedSchemas)}]`);
|
|
290
|
-
const mutatedConfig = {
|
|
291
|
-
...params?.config,
|
|
292
|
-
schema
|
|
293
|
-
};
|
|
294
|
-
params?.logger?.debug(`config: ${JSON.stringify(mutatedConfig, null, 2)}`);
|
|
295
|
-
const mutatedParams = {
|
|
296
|
-
...params,
|
|
297
|
-
config: mutatedConfig
|
|
298
|
-
};
|
|
299
|
-
const activeLogger = params?.logger ?? _AbstractModule.defaultLogger;
|
|
300
|
-
const generatedAccount = await _AbstractModule.determineAccount({
|
|
301
|
-
account,
|
|
302
|
-
accountDerivationPath,
|
|
303
|
-
wallet
|
|
304
|
-
});
|
|
305
|
-
const address = generatedAccount.address;
|
|
306
|
-
mutatedParams.logger = activeLogger ? new IdLogger(activeLogger, () => `0x${address}`) : void 0;
|
|
307
|
-
const newModule = new this(_AbstractModule.privateConstructorKey, mutatedParams, generatedAccount);
|
|
308
|
-
if (!_AbstractModule.enableLazyLoad) {
|
|
309
|
-
await newModule.start?.();
|
|
310
|
-
}
|
|
311
|
-
return newModule;
|
|
312
|
-
}
|
|
313
|
-
static async determineAccount({ account, accountDerivationPath, wallet }) {
|
|
314
|
-
if (wallet) {
|
|
315
|
-
return assertEx(accountDerivationPath ? await wallet.derivePath(accountDerivationPath) : wallet, "Failed to derive account from path");
|
|
316
|
-
} else if (account === "random") {
|
|
317
|
-
return await HDWallet.random();
|
|
318
|
-
} else if (account) {
|
|
319
|
-
return account;
|
|
320
|
-
} else {
|
|
321
|
-
console.warn("AbstractModule.determineAccount: No account provided - Creating Random account");
|
|
322
|
-
return await HDWallet.random();
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
static factory(params) {
|
|
326
|
-
return ModuleFactory.withParams(this, params);
|
|
327
|
-
}
|
|
328
|
-
_getRootFunction(funcName) {
|
|
329
|
-
let anyThis = this;
|
|
330
|
-
while (anyThis.__proto__[funcName]) {
|
|
331
|
-
anyThis = anyThis.__proto__;
|
|
332
|
-
}
|
|
333
|
-
return anyThis[funcName];
|
|
334
|
-
}
|
|
335
|
-
_noOverride(functionName) {
|
|
336
|
-
const thisFunc = this[functionName];
|
|
337
|
-
const rootFunc = this._getRootFunction(functionName);
|
|
338
|
-
assertEx(thisFunc === rootFunc, `Override not allowed for [${functionName}] - override ${functionName}Handler instead`);
|
|
339
|
-
}
|
|
340
|
-
async busy(closure) {
|
|
341
|
-
if (this._busyCount <= 0) {
|
|
342
|
-
this._busyCount = 0;
|
|
343
|
-
const args = {
|
|
344
|
-
busy: true,
|
|
345
|
-
module: this
|
|
346
|
-
};
|
|
347
|
-
await this.emit("moduleBusy", args);
|
|
348
|
-
}
|
|
349
|
-
this._busyCount++;
|
|
350
|
-
try {
|
|
351
|
-
return await closure();
|
|
352
|
-
} finally {
|
|
353
|
-
this._busyCount--;
|
|
354
|
-
if (this._busyCount <= 0) {
|
|
355
|
-
this._busyCount = 0;
|
|
356
|
-
const args = {
|
|
357
|
-
busy: false,
|
|
358
|
-
module: this
|
|
359
|
-
};
|
|
360
|
-
await this.emit("moduleBusy", args);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
emit(eventName, eventArgs) {
|
|
365
|
-
return super.emit(eventName, eventArgs);
|
|
366
|
-
}
|
|
367
|
-
previousHash() {
|
|
368
|
-
return this.account.previousHash;
|
|
369
|
-
}
|
|
370
|
-
async query(query, payloads, queryConfig) {
|
|
371
|
-
this._noOverride("query");
|
|
372
|
-
return await this.busy(async () => {
|
|
373
|
-
const resultPayloads = [];
|
|
374
|
-
const errorPayloads = [];
|
|
375
|
-
const queryAccount = this.ephemeralQueryAccountEnabled ? await HDWallet.random() : void 0;
|
|
376
|
-
try {
|
|
377
|
-
await this.started("throw");
|
|
378
|
-
if (!this.allowAnonymous) {
|
|
379
|
-
if (query.addresses.length === 0) {
|
|
380
|
-
throw Error(`Anonymous Queries not allowed, but running anyway [${this.config.name}], [${this.address}]`);
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
resultPayloads.push(...await this.queryHandler(assertEx(QueryBoundWitnessWrapper3.unwrap(query)), payloads, queryConfig));
|
|
384
|
-
} catch (ex) {
|
|
385
|
-
await handleErrorAsync(ex, async (error) => {
|
|
386
|
-
errorPayloads.push(new ModuleErrorBuilder().sources([
|
|
387
|
-
await PayloadHasher.hashAsync(query)
|
|
388
|
-
]).name(this.config.name ?? "<Unknown>").query(query.schema).message(error.message).build());
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
if (this.timestamp) {
|
|
392
|
-
const timestamp = {
|
|
393
|
-
schema: "network.xyo.timestamp",
|
|
394
|
-
timestamp: Date.now()
|
|
395
|
-
};
|
|
396
|
-
resultPayloads.push(timestamp);
|
|
397
|
-
}
|
|
398
|
-
const result = await this.bindQueryResult(query, resultPayloads, queryAccount ? [
|
|
399
|
-
queryAccount
|
|
400
|
-
] : [], errorPayloads);
|
|
401
|
-
const args = {
|
|
402
|
-
module: this,
|
|
403
|
-
payloads,
|
|
404
|
-
query,
|
|
405
|
-
result
|
|
406
|
-
};
|
|
407
|
-
await this.emit("moduleQueried", args);
|
|
408
|
-
return result;
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
queryable(query, payloads, queryConfig) {
|
|
412
|
-
if (!this.started("warn"))
|
|
413
|
-
return false;
|
|
414
|
-
const configValidator = queryConfig ? new ModuleConfigQueryValidator(Object.assign({}, this.config, queryConfig)).queryable : this.moduleConfigQueryValidator;
|
|
415
|
-
const validators = [
|
|
416
|
-
this.supportedQueryValidator,
|
|
417
|
-
configValidator
|
|
418
|
-
];
|
|
419
|
-
return validators.every((validator) => validator(query, payloads));
|
|
420
|
-
}
|
|
421
|
-
async resolve(nameOrAddressOrFilter, options) {
|
|
422
|
-
const direction = options?.direction ?? "all";
|
|
423
|
-
const up = direction === "up" || direction === "all";
|
|
424
|
-
const down = direction === "down" || direction === "all";
|
|
425
|
-
switch (typeof nameOrAddressOrFilter) {
|
|
426
|
-
case "string": {
|
|
427
|
-
return (down ? await this.downResolver.resolve(nameOrAddressOrFilter, options) : void 0) ?? (up ? await this.upResolver.resolve(nameOrAddressOrFilter, options) : void 0);
|
|
428
|
-
}
|
|
429
|
-
default: {
|
|
430
|
-
const filter = nameOrAddressOrFilter;
|
|
431
|
-
return [
|
|
432
|
-
...down ? await this.downResolver.resolve(filter, options) : [],
|
|
433
|
-
...up ? await this.upResolver.resolve(filter, options) : []
|
|
434
|
-
].filter(duplicateModules);
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
start(_timeout) {
|
|
439
|
-
this._startPromise = this._startPromise ?? this.startHandler();
|
|
440
|
-
return this._startPromise;
|
|
441
|
-
}
|
|
442
|
-
async started(notStartedAction = "log", tryStart = true) {
|
|
443
|
-
const started = await this._started;
|
|
444
|
-
if (started === true) {
|
|
445
|
-
return true;
|
|
446
|
-
}
|
|
447
|
-
if (!started) {
|
|
448
|
-
this._started = (async () => {
|
|
449
|
-
if (tryStart) {
|
|
450
|
-
try {
|
|
451
|
-
await this.start();
|
|
452
|
-
return true;
|
|
453
|
-
} catch (ex) {
|
|
454
|
-
handleError(ex, (error) => {
|
|
455
|
-
this.logger?.warn(`Autostart of Module Failed: ${error.message})`);
|
|
456
|
-
this._started = void 0;
|
|
457
|
-
});
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
switch (notStartedAction) {
|
|
461
|
-
case "throw":
|
|
462
|
-
throw Error(`Module not Started [${this.address}]`);
|
|
463
|
-
case "warn":
|
|
464
|
-
this.logger?.warn("Module not started");
|
|
465
|
-
break;
|
|
466
|
-
case "error":
|
|
467
|
-
this.logger?.error("Module not started");
|
|
468
|
-
break;
|
|
469
|
-
case "none":
|
|
470
|
-
break;
|
|
471
|
-
case "log":
|
|
472
|
-
default: {
|
|
473
|
-
this.logger?.log("Module not started");
|
|
474
|
-
break;
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
return false;
|
|
478
|
-
})();
|
|
479
|
-
}
|
|
480
|
-
if (!this._started) {
|
|
481
|
-
throw "Failed to create start promise";
|
|
482
|
-
}
|
|
483
|
-
return await this._started;
|
|
484
|
-
}
|
|
485
|
-
async stop(_timeout) {
|
|
486
|
-
return await this.busy(async () => {
|
|
487
|
-
const result = await this.stopHandler();
|
|
488
|
-
this._started = void 0;
|
|
489
|
-
this._startPromise = void 0;
|
|
490
|
-
return result;
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
bindHashes(hashes, schema, account) {
|
|
494
|
-
const promise = new PromiseEx((resolve) => {
|
|
495
|
-
const result = this.bindHashesInternal(hashes, schema, account);
|
|
496
|
-
resolve?.(result);
|
|
497
|
-
return result;
|
|
498
|
-
}, account);
|
|
499
|
-
return promise;
|
|
500
|
-
}
|
|
501
|
-
async bindHashesInternal(hashes, schema, account) {
|
|
502
|
-
const builder = new BoundWitnessBuilder().hashes(hashes, schema).witness(this.account);
|
|
503
|
-
const result = (await (account ? builder.witness(account) : builder).build())[0];
|
|
504
|
-
this.logger?.debug(`result: ${JSON.stringify(result, null, 2)}`);
|
|
505
|
-
return result;
|
|
506
|
-
}
|
|
507
|
-
bindQuery(query, payloads, account) {
|
|
508
|
-
const promise = new PromiseEx(async (resolve) => {
|
|
509
|
-
const result = await this.bindQueryInternal(query, payloads, account);
|
|
510
|
-
resolve?.(result);
|
|
511
|
-
return result;
|
|
512
|
-
}, account);
|
|
513
|
-
return promise;
|
|
514
|
-
}
|
|
515
|
-
async bindQueryInternal(query, payloads, account) {
|
|
516
|
-
const builder = new QueryBoundWitnessBuilder().payloads(payloads).witness(this.account).query(query);
|
|
517
|
-
const result = await (account ? builder.witness(account) : builder).build();
|
|
518
|
-
return result;
|
|
519
|
-
}
|
|
520
|
-
async bindQueryResult(query, payloads, additionalWitnesses = [], errors) {
|
|
521
|
-
const builder = new BoundWitnessBuilder().payloads(payloads).errors(errors);
|
|
522
|
-
const queryWitnessAccount = this.queryAccounts[query.schema];
|
|
523
|
-
const witnesses = [
|
|
524
|
-
this.account,
|
|
525
|
-
queryWitnessAccount,
|
|
526
|
-
...additionalWitnesses
|
|
527
|
-
].filter(exists);
|
|
528
|
-
builder.witnesses(witnesses);
|
|
529
|
-
const result = [
|
|
530
|
-
(await builder.build())[0],
|
|
531
|
-
payloads,
|
|
532
|
-
errors ?? []
|
|
533
|
-
];
|
|
534
|
-
return result;
|
|
535
|
-
}
|
|
536
|
-
commitArchivist = () => this.getArchivist("commit");
|
|
537
|
-
async describeHandler() {
|
|
538
|
-
const description = {
|
|
539
|
-
address: this.address,
|
|
540
|
-
queries: this.queries,
|
|
541
|
-
schema: ModuleDescriptionSchema
|
|
542
|
-
};
|
|
543
|
-
if (this.config?.name) {
|
|
544
|
-
description.name = this.config.name;
|
|
545
|
-
}
|
|
546
|
-
const discover = await this.discoverHandler();
|
|
547
|
-
description.children = compact(discover?.map((payload) => {
|
|
548
|
-
const address = payload.schema === AddressSchema ? payload.address : void 0;
|
|
549
|
-
return address != this.address ? address : void 0;
|
|
550
|
-
}) ?? []);
|
|
551
|
-
return description;
|
|
552
|
-
}
|
|
553
|
-
discoverHandler() {
|
|
554
|
-
const config = this.config;
|
|
555
|
-
const address = new PayloadBuilder2({
|
|
556
|
-
schema: AddressSchema
|
|
557
|
-
}).fields({
|
|
558
|
-
address: this.address,
|
|
559
|
-
name: this.config?.name
|
|
560
|
-
}).build();
|
|
561
|
-
const queries = this.queries.map((query) => {
|
|
562
|
-
return new PayloadBuilder2({
|
|
563
|
-
schema: QuerySchema
|
|
564
|
-
}).fields({
|
|
565
|
-
query
|
|
566
|
-
}).build();
|
|
567
|
-
});
|
|
568
|
-
const configSchema = {
|
|
569
|
-
config: config.schema,
|
|
570
|
-
schema: ConfigSchema
|
|
571
|
-
};
|
|
572
|
-
return compact([
|
|
573
|
-
config,
|
|
574
|
-
configSchema,
|
|
575
|
-
address,
|
|
576
|
-
...queries
|
|
577
|
-
]);
|
|
578
|
-
}
|
|
579
|
-
async getArchivist(kind) {
|
|
580
|
-
if (!this.config.archivist)
|
|
581
|
-
return void 0;
|
|
582
|
-
const filter = typeof this.config.archivist === "string" || this.config.archivist instanceof String ? this.config.archivist : this.config?.archivist?.[kind];
|
|
583
|
-
const resolved = await this.upResolver.resolve(filter);
|
|
584
|
-
return asArchivistInstance(resolved);
|
|
585
|
-
}
|
|
586
|
-
async initializeQueryAccounts() {
|
|
587
|
-
const paths = Object.values(this.queryAccountPaths).filter(exists);
|
|
588
|
-
const distinctPaths = new Set(paths);
|
|
589
|
-
assertEx(distinctPaths.size === paths.length, `${this.config?.name ? this.config.name + ": " : ""}Duplicate query account paths`);
|
|
590
|
-
const wallet = this.account;
|
|
591
|
-
if (wallet?.derivePath) {
|
|
592
|
-
for (const key in this.queryAccountPaths) {
|
|
593
|
-
if (Object.prototype.hasOwnProperty.call(this.queryAccountPaths, key)) {
|
|
594
|
-
const query = key;
|
|
595
|
-
const queryAccountPath = this.queryAccountPaths[query];
|
|
596
|
-
if (queryAccountPath) {
|
|
597
|
-
this._queryAccounts[query] = await wallet.derivePath?.(queryAccountPath);
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
manifestHandler(_ignoreAddresses) {
|
|
604
|
-
const name = this.config.name ?? "Anonymous";
|
|
605
|
-
return {
|
|
606
|
-
config: {
|
|
607
|
-
name,
|
|
608
|
-
...this.config
|
|
609
|
-
},
|
|
610
|
-
schema: ModuleManifestPayloadSchema
|
|
611
|
-
};
|
|
612
|
-
}
|
|
613
|
-
moduleAddressHandler() {
|
|
614
|
-
const queryAccounts = Object.entries(this.queryAccounts).filter((value) => {
|
|
615
|
-
return exists(value[1]);
|
|
616
|
-
}).map(([name2, account]) => {
|
|
617
|
-
const address2 = account.address;
|
|
618
|
-
const previousHash2 = account.previousHash;
|
|
619
|
-
return [
|
|
620
|
-
{
|
|
621
|
-
address: address2,
|
|
622
|
-
name: name2,
|
|
623
|
-
schema: AddressSchema
|
|
624
|
-
},
|
|
625
|
-
{
|
|
626
|
-
address: address2,
|
|
627
|
-
previousHash: previousHash2,
|
|
628
|
-
schema: AddressPreviousHashSchema
|
|
629
|
-
}
|
|
630
|
-
];
|
|
631
|
-
});
|
|
632
|
-
const address = this.address;
|
|
633
|
-
const name = this.config.name;
|
|
634
|
-
const previousHash = this.address;
|
|
635
|
-
const moduleAccount = name ? {
|
|
636
|
-
address,
|
|
637
|
-
name,
|
|
638
|
-
schema: AddressSchema
|
|
639
|
-
} : {
|
|
640
|
-
address,
|
|
641
|
-
schema: AddressSchema
|
|
642
|
-
};
|
|
643
|
-
const moduleAccountPreviousHash = previousHash ? {
|
|
644
|
-
address,
|
|
645
|
-
previousHash,
|
|
646
|
-
schema: AddressPreviousHashSchema
|
|
647
|
-
} : {
|
|
648
|
-
address,
|
|
649
|
-
schema: AddressPreviousHashSchema
|
|
650
|
-
};
|
|
651
|
-
return [
|
|
652
|
-
moduleAccount,
|
|
653
|
-
moduleAccountPreviousHash,
|
|
654
|
-
...queryAccounts
|
|
655
|
-
].flat();
|
|
656
|
-
}
|
|
657
|
-
async queryHandler(query, payloads, queryConfig) {
|
|
658
|
-
await this.started("throw");
|
|
659
|
-
const wrapper = QueryBoundWitnessWrapper3.parseQuery(query, payloads);
|
|
660
|
-
const queryPayload = await wrapper.getQuery();
|
|
661
|
-
assertEx(this.queryable(query, payloads, queryConfig));
|
|
662
|
-
const resultPayloads = [];
|
|
663
|
-
switch (queryPayload.schema) {
|
|
664
|
-
case ModuleManifestQuerySchema: {
|
|
665
|
-
resultPayloads.push(await this.manifestHandler());
|
|
666
|
-
break;
|
|
667
|
-
}
|
|
668
|
-
case ModuleDiscoverQuerySchema: {
|
|
669
|
-
resultPayloads.push(...await this.discoverHandler());
|
|
670
|
-
break;
|
|
671
|
-
}
|
|
672
|
-
case ModuleDescribeQuerySchema: {
|
|
673
|
-
resultPayloads.push(await this.describeHandler());
|
|
674
|
-
break;
|
|
675
|
-
}
|
|
676
|
-
case ModuleAddressQuerySchema: {
|
|
677
|
-
resultPayloads.push(...await this.moduleAddressHandler());
|
|
678
|
-
break;
|
|
679
|
-
}
|
|
680
|
-
case ModuleSubscribeQuerySchema: {
|
|
681
|
-
this.subscribeHandler();
|
|
682
|
-
break;
|
|
683
|
-
}
|
|
684
|
-
default:
|
|
685
|
-
throw Error(`Unsupported Query [${queryPayload.schema}]`);
|
|
686
|
-
}
|
|
687
|
-
return resultPayloads;
|
|
688
|
-
}
|
|
689
|
-
readArchivist = () => this.getArchivist("read");
|
|
690
|
-
async startHandler() {
|
|
691
|
-
this.validateConfig();
|
|
692
|
-
await this.initializeQueryAccounts();
|
|
693
|
-
this._started = true;
|
|
694
|
-
return true;
|
|
695
|
-
}
|
|
696
|
-
stopHandler(_timeout) {
|
|
697
|
-
this._started = void 0;
|
|
698
|
-
return true;
|
|
699
|
-
}
|
|
700
|
-
subscribeHandler() {
|
|
701
|
-
return;
|
|
702
|
-
}
|
|
703
|
-
validateConfig(config, parents = []) {
|
|
704
|
-
return Object.entries(config ?? this.config ?? {}).reduce((valid, [key, value]) => {
|
|
705
|
-
switch (typeof value) {
|
|
706
|
-
case "function":
|
|
707
|
-
this.logger?.warn(`Fields of type function not allowed in config [${parents?.join(".")}.${key}]`);
|
|
708
|
-
return false;
|
|
709
|
-
case "object": {
|
|
710
|
-
if (Array.isArray(value)) {
|
|
711
|
-
return value.reduce((valid2, value2) => {
|
|
712
|
-
return this.validateConfig(value2, [
|
|
713
|
-
...parents,
|
|
714
|
-
key
|
|
715
|
-
]) && valid2;
|
|
716
|
-
}, true) && valid;
|
|
717
|
-
}
|
|
718
|
-
if (!serializableField(value)) {
|
|
719
|
-
this.logger?.warn(`Fields that are not serializable to JSON are not allowed in config [${parents?.join(".")}.${key}]`);
|
|
720
|
-
return false;
|
|
721
|
-
}
|
|
722
|
-
return value ? this.validateConfig(value, [
|
|
723
|
-
...parents,
|
|
724
|
-
key
|
|
725
|
-
]) && valid : true;
|
|
726
|
-
}
|
|
727
|
-
default:
|
|
728
|
-
return valid;
|
|
729
|
-
}
|
|
730
|
-
}, true);
|
|
731
|
-
}
|
|
732
|
-
writeArchivist = () => this.getArchivist("write");
|
|
733
|
-
};
|
|
734
|
-
|
|
735
|
-
// src/AbstractModuleInstance.ts
|
|
736
|
-
import { assertEx as assertEx2 } from "@xylabs/assert";
|
|
737
|
-
var AbstractModuleInstance = class extends AbstractModule {
|
|
738
|
-
static {
|
|
739
|
-
__name(this, "AbstractModuleInstance");
|
|
740
|
-
}
|
|
741
|
-
constructor(privateConstructorKey, params, account) {
|
|
742
|
-
assertEx2(AbstractModule.privateConstructorKey === privateConstructorKey, "Use create function instead of constructor");
|
|
743
|
-
const mutatedParams = {
|
|
744
|
-
...params
|
|
745
|
-
};
|
|
746
|
-
super(privateConstructorKey, mutatedParams, account);
|
|
747
|
-
this.upResolver.add(this);
|
|
748
|
-
this.downResolver.add(this);
|
|
749
|
-
}
|
|
750
|
-
describe() {
|
|
751
|
-
return this.busy(async () => {
|
|
752
|
-
return await this.describeHandler();
|
|
753
|
-
});
|
|
754
|
-
}
|
|
755
|
-
discover() {
|
|
756
|
-
return this.busy(async () => {
|
|
757
|
-
return await this.discoverHandler();
|
|
758
|
-
});
|
|
759
|
-
}
|
|
760
|
-
manifest(ignoreAddresses) {
|
|
761
|
-
return this.busy(async () => {
|
|
762
|
-
return await this.manifestHandler(ignoreAddresses);
|
|
763
|
-
});
|
|
764
|
-
}
|
|
765
|
-
moduleAddress() {
|
|
766
|
-
return this.busy(async () => {
|
|
767
|
-
return await this.moduleAddressHandler();
|
|
768
|
-
});
|
|
769
|
-
}
|
|
770
|
-
subscribe(_queryAccount) {
|
|
771
|
-
return this.subscribeHandler();
|
|
772
|
-
}
|
|
773
|
-
};
|
|
774
|
-
export {
|
|
775
|
-
AbstractModule,
|
|
776
|
-
AbstractModuleInstance,
|
|
777
|
-
BaseEmitter,
|
|
778
|
-
ModuleConfigQueryValidator,
|
|
779
|
-
ModuleErrorBuilder,
|
|
780
|
-
SupportedQueryValidator,
|
|
781
|
-
isQuerySupportedByModule
|
|
782
|
-
};
|
|
1
|
+
export * from "./AbstractModule";
|
|
2
|
+
export * from "./AbstractModuleInstance";
|
|
3
|
+
export * from "./BaseEmitter";
|
|
4
|
+
export * from "./Error";
|
|
5
|
+
export * from "./QueryValidator";
|
|
783
6
|
//# sourceMappingURL=index.js.map
|