@xyo-network/os-runtime 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +165 -0
- package/README.md +13 -0
- package/dist/neutral/index.d.ts +1656 -0
- package/dist/neutral/index.mjs +3432 -0
- package/dist/neutral/index.mjs.map +1 -0
- package/package.json +78 -0
- package/src/Caller.ts +221 -0
- package/src/DappCallerBase.ts +52 -0
- package/src/DefaultsQueries.ts +27 -0
- package/src/OsCallerBase.ts +55 -0
- package/src/PubSubBridgeCaller.ts +30 -0
- package/src/XyOs.ts +125 -0
- package/src/XyOsBase.ts +119 -0
- package/src/XyOsDapp.ts +90 -0
- package/src/access-interfaces/ValidDappAccessInterfaces.ts +8 -0
- package/src/access-interfaces/index.ts +2 -0
- package/src/access-interfaces/registered-names/helpers/AccessNodeQueries.ts +59 -0
- package/src/access-interfaces/registered-names/helpers/index.ts +2 -0
- package/src/access-interfaces/registered-names/helpers/resource/AbstractXnsCaller.ts +38 -0
- package/src/access-interfaces/registered-names/helpers/resource/RegistrationsResource.ts +54 -0
- package/src/access-interfaces/registered-names/helpers/resource/RegistrationsResourceQueries.ts +22 -0
- package/src/access-interfaces/registered-names/helpers/resource/index.ts +3 -0
- package/src/access-interfaces/registered-names/index.ts +1 -0
- package/src/adapter/Base.ts +96 -0
- package/src/adapter/Network.ts +31 -0
- package/src/adapter/Settings.ts +30 -0
- package/src/adapter/index.ts +2 -0
- package/src/adapters/OsPubSubBridgeNetwork.ts +10 -0
- package/src/adapters/OsSettings.ts +7 -0
- package/src/adapters/OsXyoPublicNetwork.ts +9 -0
- package/src/adapters/index.ts +3 -0
- package/src/classes/cache/RunningAccessDappCache.ts +21 -0
- package/src/classes/cache/RunningDappCache.ts +50 -0
- package/src/classes/cache/index.ts +2 -0
- package/src/classes/dapp/DefaultsResource.ts +65 -0
- package/src/classes/dapp/access/Caller.ts +73 -0
- package/src/classes/dapp/access/Queries.ts +38 -0
- package/src/classes/dapp/access/Resource.ts +63 -0
- package/src/classes/dapp/access/index.ts +3 -0
- package/src/classes/dapp/index.ts +2 -0
- package/src/classes/index.ts +8 -0
- package/src/classes/lib/DappCreatorParams.ts +16 -0
- package/src/classes/lib/Insertable.ts +14 -0
- package/src/classes/lib/index.ts +2 -0
- package/src/classes/menu/Caller.ts +76 -0
- package/src/classes/menu/Queries.ts +59 -0
- package/src/classes/menu/Resource.ts +103 -0
- package/src/classes/menu/index.ts +2 -0
- package/src/classes/node/Creator.ts +96 -0
- package/src/classes/node/DefaultPayloads/DappAccessPayloads.ts +17 -0
- package/src/classes/node/DefaultPayloads/DefaultPayloads.ts +33 -0
- package/src/classes/node/DefaultPayloads/NodeInfoPayload.ts +27 -0
- package/src/classes/node/DefaultPayloads/SigningKeyPayloads.ts +85 -0
- package/src/classes/node/DefaultPayloads/index.ts +1 -0
- package/src/classes/node/ExternalModulePermissions/ExternalModulePermissions.ts +47 -0
- package/src/classes/node/ExternalModulePermissions/index.ts +1 -0
- package/src/classes/node/createDappContext.ts +52 -0
- package/src/classes/node/index.ts +3 -0
- package/src/classes/registration/DappRegistrationService.ts +146 -0
- package/src/classes/registration/DappRegistry.ts +121 -0
- package/src/classes/registration/ValidateDappAccessDiviner/Config.ts +19 -0
- package/src/classes/registration/ValidateDappAccessDiviner/Diviner.ts +132 -0
- package/src/classes/registration/ValidateDappAccessDiviner/index.ts +2 -0
- package/src/classes/registration/index.ts +2 -0
- package/src/classes/settings/Caller.ts +76 -0
- package/src/classes/settings/CallerBase.ts +24 -0
- package/src/classes/settings/Resource.ts +79 -0
- package/src/classes/settings/SettingsQueries.ts +38 -0
- package/src/classes/settings/badge/Caller.ts +30 -0
- package/src/classes/settings/badge/Queries.ts +18 -0
- package/src/classes/settings/badge/Resource.ts +54 -0
- package/src/classes/settings/badge/index.ts +2 -0
- package/src/classes/settings/index.ts +5 -0
- package/src/classes/system/ManageSystemDapps.ts +131 -0
- package/src/classes/system/Queries.ts +69 -0
- package/src/classes/system/index.ts +1 -0
- package/src/event/bus/Connection.ts +31 -0
- package/src/event/bus/EventBus.ts +145 -0
- package/src/event/bus/PubSubConnection.ts +28 -0
- package/src/event/bus/index.ts +3 -0
- package/src/event/connections/DappAccessRequest.ts +11 -0
- package/src/event/connections/DappsReady.ts +9 -0
- package/src/event/connections/ExposeDappRequest.ts +11 -0
- package/src/event/connections/OsPubSubNetworkReady.ts +12 -0
- package/src/event/connections/OsSettingsReady.ts +12 -0
- package/src/event/connections/OsXyoPublicReady.ts +12 -0
- package/src/event/connections/index.ts +6 -0
- package/src/event/index.ts +2 -0
- package/src/helpers/index.ts +1 -0
- package/src/helpers/monitor/XyOsMonitor.ts +52 -0
- package/src/helpers/monitor/index.ts +2 -0
- package/src/helpers/monitor/types.ts +5 -0
- package/src/index.ts +22 -0
- package/src/intent/Caller.ts +72 -0
- package/src/intent/Resource.ts +66 -0
- package/src/intent/index.ts +2 -0
- package/src/lib/ExternalStore.ts +7 -0
- package/src/lib/Listener.ts +1 -0
- package/src/lib/ModuleAccountPaths.ts +29 -0
- package/src/lib/ModuleNames.ts +3 -0
- package/src/lib/NameTransforms.ts +31 -0
- package/src/lib/PayloadStore.ts +98 -0
- package/src/lib/ResourceStores.ts +7 -0
- package/src/lib/index.ts +10 -0
- package/src/lib/initializeXns.ts +16 -0
- package/src/lib/isPayload.ts +24 -0
- package/src/lib/tokenPlacesSplit.ts +17 -0
- package/src/loadOsNode.ts +43 -0
- package/src/manifest/ManifestReplaceableTokens.ts +17 -0
- package/src/manifest/index.ts +2 -0
- package/src/manifest/os-node.manifest.json +132 -0
- package/src/manifests/dapp-window.manifest.json +36 -0
- package/src/manifests/index.ts +1 -0
- package/src/profileModuleEvents.ts +43 -0
- package/src/stack/Base.ts +153 -0
- package/src/stack/Manager.ts +48 -0
- package/src/stack/Map.ts +20 -0
- package/src/stack/OsPubSubNetworkStack.ts +70 -0
- package/src/stack/OsSettingsStack.ts +24 -0
- package/src/stack/XyoPublicNetworkStack.ts +32 -0
- package/src/stack/index.ts +6 -0
- package/src/types/global.d.ts +9 -0
- package/src/types/images.d.ts +5 -0
- package/src/utils/buildWalletSeedPhrasePayload.ts +41 -0
- package/src/utils/getApiDomain.ts +14 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/renameObjKey.ts +22 -0
- package/src/wallet/DappSeedPhraseRepository.ts +95 -0
- package/src/wallet/SeedPhraseRepository.ts +32 -0
- package/src/wallet/index.ts +2 -0
- package/typedoc.json +5 -0
- package/xy.config.ts +10 -0
|
@@ -0,0 +1,3432 @@
|
|
|
1
|
+
// src/access-interfaces/registered-names/helpers/AccessNodeQueries.ts
|
|
2
|
+
import { assertEx as assertEx20 } from "@xylabs/assert";
|
|
3
|
+
import { asArchivistInstance as asArchivistInstance7 } from "@xyo-network/archivist-model";
|
|
4
|
+
|
|
5
|
+
// src/classes/cache/RunningAccessDappCache.ts
|
|
6
|
+
var RunningDappAccessCache = class {
|
|
7
|
+
static _cache = {};
|
|
8
|
+
static get(key) {
|
|
9
|
+
return this._cache[key];
|
|
10
|
+
}
|
|
11
|
+
static has(key) {
|
|
12
|
+
return key ? !!this._cache[key] : false;
|
|
13
|
+
}
|
|
14
|
+
static set(key, value) {
|
|
15
|
+
this._cache[key] = value;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// src/classes/cache/RunningDappCache.ts
|
|
20
|
+
import { assertEx as assertEx19 } from "@xylabs/assert";
|
|
21
|
+
import { Mutex as Mutex2 } from "async-mutex";
|
|
22
|
+
|
|
23
|
+
// src/XyOs.ts
|
|
24
|
+
import { assertEx as assertEx12 } from "@xylabs/assert";
|
|
25
|
+
import { ModuleFactoryLocator as ModuleFactoryLocator5 } from "@xyo-network/module-factory-locator";
|
|
26
|
+
|
|
27
|
+
// src/classes/registration/DappRegistrationService.ts
|
|
28
|
+
import { forget as forget2 } from "@xylabs/forget";
|
|
29
|
+
import { fulfilled, rejected } from "@xylabs/promise";
|
|
30
|
+
import { ModuleFactoryLocator as ModuleFactoryLocator3 } from "@xyo-network/module-factory-locator";
|
|
31
|
+
import {
|
|
32
|
+
DappIntentTypes,
|
|
33
|
+
DappMode as DappMode2,
|
|
34
|
+
isRegisteredDappAccessDappSet,
|
|
35
|
+
isRegisteredDappExposedDappSet
|
|
36
|
+
} from "@xyo-network/os-model";
|
|
37
|
+
|
|
38
|
+
// src/event/bus/Connection.ts
|
|
39
|
+
import { assertEx } from "@xylabs/assert";
|
|
40
|
+
import { BaseEmitter } from "@xyo-network/module-abstract";
|
|
41
|
+
var EventBusConnection = class extends BaseEmitter {
|
|
42
|
+
_id;
|
|
43
|
+
description;
|
|
44
|
+
type;
|
|
45
|
+
constructor(request) {
|
|
46
|
+
super({});
|
|
47
|
+
this.description = request.description;
|
|
48
|
+
this.type = request.type;
|
|
49
|
+
}
|
|
50
|
+
get id() {
|
|
51
|
+
return assertEx(this._id, () => "Connection ID not set. Did you forget to call buildConnection()?");
|
|
52
|
+
}
|
|
53
|
+
buildConnection(id) {
|
|
54
|
+
this._id = id;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/event/bus/EventBus.ts
|
|
59
|
+
import { assertEx as assertEx2 } from "@xylabs/assert";
|
|
60
|
+
import { forget } from "@xylabs/forget";
|
|
61
|
+
import { asArchivistInstance, MemoryArchivist, MemoryArchivistConfigSchema } from "@xyo-network/archivist";
|
|
62
|
+
import {
|
|
63
|
+
isEventBusEvent,
|
|
64
|
+
isPubSubConnections
|
|
65
|
+
} from "@xyo-network/os-model";
|
|
66
|
+
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
67
|
+
import { v4 as uuid } from "uuid";
|
|
68
|
+
var EventBus = class {
|
|
69
|
+
// Internal Archivist to store event data
|
|
70
|
+
_archivist;
|
|
71
|
+
// Store all connections
|
|
72
|
+
connections = {};
|
|
73
|
+
// Store all publisher callbacks for later removal
|
|
74
|
+
publisherCallbacks = {};
|
|
75
|
+
get archivist() {
|
|
76
|
+
return asArchivistInstance(
|
|
77
|
+
assertEx2(this._archivist, () => "Archivist not found. Did you forget to call start()?"),
|
|
78
|
+
"not a valid archivist instance"
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
addConnection(connection) {
|
|
82
|
+
const id = uuid();
|
|
83
|
+
this.connections[id] = connection;
|
|
84
|
+
connection.buildConnection(id);
|
|
85
|
+
if (isPubSubConnections(connection)) {
|
|
86
|
+
this.handlePublisherAdd(connection);
|
|
87
|
+
} else {
|
|
88
|
+
throw new Error("Unsupported connection type. Must be PubSub");
|
|
89
|
+
}
|
|
90
|
+
return connection;
|
|
91
|
+
}
|
|
92
|
+
removeConnection(connectionId) {
|
|
93
|
+
const connection = this.connections[connectionId];
|
|
94
|
+
if (!connection) {
|
|
95
|
+
throw new Error("Connection not found " + connectionId);
|
|
96
|
+
}
|
|
97
|
+
if (connection && isPubSubConnections(connection)) {
|
|
98
|
+
this.handlePublisherRemove(connection);
|
|
99
|
+
}
|
|
100
|
+
delete this.connections[connectionId];
|
|
101
|
+
}
|
|
102
|
+
async start(storeName = "EventBusArchivist") {
|
|
103
|
+
this._archivist = await MemoryArchivist.create({ config: { name: storeName, schema: MemoryArchivistConfigSchema } });
|
|
104
|
+
this.archivist.on("inserted", ({ payloads }) => {
|
|
105
|
+
const eventBusEvent = payloads.find(isEventBusEvent);
|
|
106
|
+
if (!eventBusEvent) return;
|
|
107
|
+
for (const connectionId in this.connections) {
|
|
108
|
+
const connection = this.connections[connectionId];
|
|
109
|
+
if (isPubSubConnections(connection) && connection.subscribableEvents) {
|
|
110
|
+
for (const [eventName, callback] of connection.subscribableEvents.entries()) {
|
|
111
|
+
if (eventName !== eventBusEvent.name) continue;
|
|
112
|
+
if (callback) {
|
|
113
|
+
const forgettable = async () => await callback({ payloads });
|
|
114
|
+
forget(forgettable());
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
handlePublisherAdd(connection) {
|
|
122
|
+
for (const eventName of connection.publishableEvents ?? []) {
|
|
123
|
+
const connectionPublisherCallback = async ({ payloads }) => {
|
|
124
|
+
const event = { name: eventName, schema: "network.xyo.event.bus.event" };
|
|
125
|
+
const eventPayloads = payloads ?? [];
|
|
126
|
+
event.sources = await PayloadBuilder.dataHashes(eventPayloads);
|
|
127
|
+
try {
|
|
128
|
+
await this.archivist.insert([event, ...payloads ?? []]);
|
|
129
|
+
} catch (e) {
|
|
130
|
+
console.error("Error inserting event into archivist", e);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
connection.on(eventName, connectionPublisherCallback);
|
|
134
|
+
const callbackId = connection.id + eventName;
|
|
135
|
+
this.publisherCallbacks[callbackId] = connectionPublisherCallback;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
handlePublisherRemove(connection) {
|
|
139
|
+
for (const [, event] of connection.publishableEvents?.entries() ?? []) {
|
|
140
|
+
const id = connection.id + event;
|
|
141
|
+
const callback = this.publisherCallbacks[id];
|
|
142
|
+
if (callback) connection.off(event, callback);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
// src/event/bus/PubSubConnection.ts
|
|
148
|
+
var EventBusPubSubConnection = class extends EventBusConnection {
|
|
149
|
+
publishableEvents;
|
|
150
|
+
subscribableEvents;
|
|
151
|
+
constructor(request) {
|
|
152
|
+
super(request);
|
|
153
|
+
if (request.type !== "PubSub") {
|
|
154
|
+
throw new Error("Invalid connection type. Must be PubSub");
|
|
155
|
+
}
|
|
156
|
+
this.publishableEvents = request.publishableEvents;
|
|
157
|
+
this.subscribableEvents = request.subscribableEvents;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// src/event/connections/DappAccessRequest.ts
|
|
162
|
+
var DappAccessRequestEvent = "dappAccessRequest";
|
|
163
|
+
var dappAccessRequestConnectionRequest = {
|
|
164
|
+
publishableEvents: [DappAccessRequestEvent],
|
|
165
|
+
type: "PubSub"
|
|
166
|
+
};
|
|
167
|
+
var dappAccessRequestConnection = () => new EventBusPubSubConnection(dappAccessRequestConnectionRequest);
|
|
168
|
+
|
|
169
|
+
// src/event/connections/DappsReady.ts
|
|
170
|
+
var dappsReadyConnectionRequest = {
|
|
171
|
+
publishableEvents: ["dappsReady"],
|
|
172
|
+
type: "PubSub"
|
|
173
|
+
};
|
|
174
|
+
var dappsReadyConnection = () => new EventBusPubSubConnection(dappsReadyConnectionRequest);
|
|
175
|
+
|
|
176
|
+
// src/event/connections/ExposeDappRequest.ts
|
|
177
|
+
var ExposeDappRequestEvent = "exposeDappRequest";
|
|
178
|
+
var exposeDappConnectionRequest = {
|
|
179
|
+
publishableEvents: [ExposeDappRequestEvent],
|
|
180
|
+
type: "PubSub"
|
|
181
|
+
};
|
|
182
|
+
var exposeDappRequestConnection = () => new EventBusPubSubConnection(exposeDappConnectionRequest);
|
|
183
|
+
|
|
184
|
+
// src/event/connections/OsPubSubNetworkReady.ts
|
|
185
|
+
var OsPubSubNetworkReadyEvent = "osPubSubNetworkReady";
|
|
186
|
+
var osPubSubNetworkConnectionRequest = {
|
|
187
|
+
publishableEvents: [OsPubSubNetworkReadyEvent],
|
|
188
|
+
type: "PubSub"
|
|
189
|
+
};
|
|
190
|
+
var osPubSubNetworkReadyConnection = () => new EventBusPubSubConnection(osPubSubNetworkConnectionRequest);
|
|
191
|
+
|
|
192
|
+
// src/event/connections/OsSettingsReady.ts
|
|
193
|
+
var OsSettingsReadyEvent = "osSettingsReady";
|
|
194
|
+
var osSettingsConnectionRequest = {
|
|
195
|
+
publishableEvents: [OsSettingsReadyEvent],
|
|
196
|
+
type: "PubSub"
|
|
197
|
+
};
|
|
198
|
+
var osSettingsReadyConnection = () => new EventBusPubSubConnection(osSettingsConnectionRequest);
|
|
199
|
+
|
|
200
|
+
// src/event/connections/OsXyoPublicReady.ts
|
|
201
|
+
var OsXyoPublicNetworkReadyEvent = "osXyoPublicNetworkReady";
|
|
202
|
+
var osXyoPublicNetworkConnectionRequest = {
|
|
203
|
+
publishableEvents: [OsXyoPublicNetworkReadyEvent],
|
|
204
|
+
type: "PubSub"
|
|
205
|
+
};
|
|
206
|
+
var osXyoPublicNetworkReadyConnection = () => new EventBusPubSubConnection(osXyoPublicNetworkConnectionRequest);
|
|
207
|
+
|
|
208
|
+
// src/intent/Caller.ts
|
|
209
|
+
import { PayloadDivinerQuerySchema } from "@xyo-network/diviner-payload-model";
|
|
210
|
+
import { DappIntentSchema, DappMode } from "@xyo-network/os-model";
|
|
211
|
+
|
|
212
|
+
// src/DappCallerBase.ts
|
|
213
|
+
import { assertEx as assertEx3 } from "@xylabs/assert";
|
|
214
|
+
import { toJsonString } from "@xylabs/object";
|
|
215
|
+
import { asArchivistInstance as asArchivistInstance2 } from "@xyo-network/archivist-model";
|
|
216
|
+
import { asDivinerInstance } from "@xyo-network/diviner-model";
|
|
217
|
+
var DappArchivistModuleName = "DappArchivist";
|
|
218
|
+
var IntentArchivistModuleName = "IntentArchivist";
|
|
219
|
+
var IntentArchivistPayloadDivinerModuleName = "IntentArchivistPayloadDiviner";
|
|
220
|
+
var DappArchivistPayloadDivinerModuleName = "DappArchivistPayloadDiviner";
|
|
221
|
+
var DappCallerBase = class {
|
|
222
|
+
constructor(context) {
|
|
223
|
+
this.context = context;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Get the DappArchivist
|
|
227
|
+
*/
|
|
228
|
+
async getDappArchivist() {
|
|
229
|
+
const mod = assertEx3(
|
|
230
|
+
await this.context.root.resolve(DappArchivistModuleName),
|
|
231
|
+
() => `${DappArchivistModuleName} not found [${toJsonString(this.context.root.publicChildren())}]`
|
|
232
|
+
);
|
|
233
|
+
return asArchivistInstance2(mod, () => `${DappArchivistModuleName} is not an archivist`);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Get the DappArchivistPayloadDiviner
|
|
237
|
+
*/
|
|
238
|
+
async getDappArchivistPayloadDiviner() {
|
|
239
|
+
const mod = assertEx3(
|
|
240
|
+
await this.context.root.resolve(DappArchivistPayloadDivinerModuleName),
|
|
241
|
+
() => `${DappArchivistPayloadDivinerModuleName} not found`
|
|
242
|
+
);
|
|
243
|
+
return asDivinerInstance(mod, () => `${DappArchivistPayloadDivinerModuleName} is not a diviner`);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Get the IntentArchivist
|
|
247
|
+
*/
|
|
248
|
+
async getDappIntentArchivist() {
|
|
249
|
+
const mod = assertEx3(await this.context.root.resolve(IntentArchivistModuleName), () => `${IntentArchivistModuleName} not found`);
|
|
250
|
+
return asArchivistInstance2(mod, () => `${IntentArchivistModuleName} is not an archivist`);
|
|
251
|
+
}
|
|
252
|
+
// Get the IntentArchivistPayloadDiviner
|
|
253
|
+
async getDappIntentArchivistPayloadDiviner() {
|
|
254
|
+
const mod = assertEx3(
|
|
255
|
+
await this.context.root.resolve(IntentArchivistPayloadDivinerModuleName),
|
|
256
|
+
() => `${IntentArchivistPayloadDivinerModuleName} not found`
|
|
257
|
+
);
|
|
258
|
+
return asDivinerInstance(mod, () => `${IntentArchivistPayloadDivinerModuleName} is not a diviner`);
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
// src/intent/Caller.ts
|
|
263
|
+
var DappIntentCaller = class _DappIntentCaller extends DappCallerBase {
|
|
264
|
+
static OsDappName = "XyOs";
|
|
265
|
+
constructor(context) {
|
|
266
|
+
super(context);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Build a new intent
|
|
270
|
+
* @param {DappId} sourceDappId
|
|
271
|
+
* @param {DappIntentTypes} intent
|
|
272
|
+
* @param {DappId} targetDappId
|
|
273
|
+
* @param {DappMode} targetMode
|
|
274
|
+
* @param {DappIntent['params']} params?
|
|
275
|
+
* @returns DappIntent
|
|
276
|
+
*/
|
|
277
|
+
static buildIntent(sourceDappId, intent, targetDappId, targetMode = DappMode.Window, params) {
|
|
278
|
+
return {
|
|
279
|
+
intent,
|
|
280
|
+
params: params || "",
|
|
281
|
+
schema: DappIntentSchema,
|
|
282
|
+
sourceDappId,
|
|
283
|
+
targetDappId,
|
|
284
|
+
targetMode: targetMode || DappMode.Window,
|
|
285
|
+
timestamp: Date.now()
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
// Add a new intent
|
|
289
|
+
async addIntent(id, intent, mode) {
|
|
290
|
+
const intentArchivist = await this.getDappIntentArchivist();
|
|
291
|
+
const newIntent = _DappIntentCaller.buildIntent(id, intent, mode);
|
|
292
|
+
await intentArchivist.insert([newIntent]);
|
|
293
|
+
}
|
|
294
|
+
// Get all intents
|
|
295
|
+
async allIntents() {
|
|
296
|
+
const archivist = await this.getDappIntentArchivist();
|
|
297
|
+
return await archivist.all();
|
|
298
|
+
}
|
|
299
|
+
// Get the latest intent saved to the archivist
|
|
300
|
+
async latestIntent() {
|
|
301
|
+
const archivist = await this.getDappIntentArchivist();
|
|
302
|
+
return (await archivist.all()).at(-1);
|
|
303
|
+
}
|
|
304
|
+
// Get the latest intent for a dapp
|
|
305
|
+
async latestIntentForDappName(dappName) {
|
|
306
|
+
const diviner = await this.getDappIntentArchivistPayloadDiviner();
|
|
307
|
+
const query = {
|
|
308
|
+
limit: 1,
|
|
309
|
+
order: "desc",
|
|
310
|
+
schema: PayloadDivinerQuerySchema,
|
|
311
|
+
sourceDappId: dappName
|
|
312
|
+
};
|
|
313
|
+
return await diviner.divine([query]);
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
// src/intent/Resource.ts
|
|
318
|
+
import { assertEx as assertEx5 } from "@xylabs/assert";
|
|
319
|
+
import { isDappIntent } from "@xyo-network/os-model";
|
|
320
|
+
|
|
321
|
+
// src/lib/initializeXns.ts
|
|
322
|
+
import { asDivinerInstance as asDivinerInstance2 } from "@xyo-network/diviner-model";
|
|
323
|
+
import { ResolveHelper } from "@xyo-network/module-model";
|
|
324
|
+
import { NameRegistrarTransformer } from "@xyo-network/module-resolver";
|
|
325
|
+
var initializeXns = async (xnsNode) => {
|
|
326
|
+
if (ResolveHelper.transformers.length === 0) {
|
|
327
|
+
const registrarDiviner = asDivinerInstance2(await xnsNode?.resolve("AddressRecords:AddressRecordIndexDiviner"));
|
|
328
|
+
if (registrarDiviner) {
|
|
329
|
+
const transformer = new NameRegistrarTransformer(registrarDiviner, "xyo");
|
|
330
|
+
ResolveHelper.transformers = [transformer];
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return false;
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
// src/lib/isPayload.ts
|
|
338
|
+
import { AsObjectFactory } from "@xylabs/object";
|
|
339
|
+
var isObject = (x) => {
|
|
340
|
+
return typeof x === "object" && !Array.isArray(x);
|
|
341
|
+
};
|
|
342
|
+
var isAnyPayload = (obj) => {
|
|
343
|
+
if (isObject(obj)) {
|
|
344
|
+
return typeof obj.schema === "string";
|
|
345
|
+
}
|
|
346
|
+
return false;
|
|
347
|
+
};
|
|
348
|
+
var isPayload = (schema) => (obj) => {
|
|
349
|
+
if (isAnyPayload(obj)) {
|
|
350
|
+
return schema.includes(obj.schema);
|
|
351
|
+
}
|
|
352
|
+
return false;
|
|
353
|
+
};
|
|
354
|
+
var asPayload = (schema) => AsObjectFactory.create(isPayload(schema));
|
|
355
|
+
|
|
356
|
+
// src/lib/ModuleNames.ts
|
|
357
|
+
var DevelopArchivist = "DevelopArchivist";
|
|
358
|
+
var NodeSentinel = "NodeSentinel";
|
|
359
|
+
|
|
360
|
+
// src/lib/ModuleAccountPaths.ts
|
|
361
|
+
var RemoteNodeArchivistOffsetPaths = {
|
|
362
|
+
Kerplunk: {
|
|
363
|
+
[DevelopArchivist]: "118"
|
|
364
|
+
},
|
|
365
|
+
Local: {
|
|
366
|
+
[DevelopArchivist]: "115"
|
|
367
|
+
},
|
|
368
|
+
Main: {
|
|
369
|
+
[DevelopArchivist]: "121"
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
var ModuleOffsetPaths = {
|
|
373
|
+
IdWitness: "1",
|
|
374
|
+
DomainWitness: "2",
|
|
375
|
+
LocationWitness: "3",
|
|
376
|
+
SchemaWitness: "4",
|
|
377
|
+
CryptoCardsGameWitness: "5",
|
|
378
|
+
CryptoCardsMoveWitness: "6",
|
|
379
|
+
EthereumGasEtherchainV2Witness: "7",
|
|
380
|
+
CoingeckoCryptoMarketWitness: "8",
|
|
381
|
+
UniswapCryptoMarketWitness: "9",
|
|
382
|
+
EthereumGasEtherscanWitness: "10",
|
|
383
|
+
[NodeSentinel]: "11",
|
|
384
|
+
[DevelopArchivist]: "12",
|
|
385
|
+
BowserSystemInfoPluginWitness: "13"
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
// src/lib/NameTransforms.ts
|
|
389
|
+
var NameTransforms = class _NameTransforms {
|
|
390
|
+
constructor(value) {
|
|
391
|
+
this.value = value;
|
|
392
|
+
}
|
|
393
|
+
static deSlug(value) {
|
|
394
|
+
return value.replaceAll("-", " ").split(" ").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
|
|
395
|
+
}
|
|
396
|
+
static moduleName(value) {
|
|
397
|
+
return value.replaceAll(" ", "");
|
|
398
|
+
}
|
|
399
|
+
static slug(value) {
|
|
400
|
+
return value.replaceAll(" ", "-").toLocaleLowerCase();
|
|
401
|
+
}
|
|
402
|
+
deSlug() {
|
|
403
|
+
return _NameTransforms.deSlug(this.value);
|
|
404
|
+
}
|
|
405
|
+
moduleName() {
|
|
406
|
+
return _NameTransforms.moduleName(this.value);
|
|
407
|
+
}
|
|
408
|
+
slug() {
|
|
409
|
+
return _NameTransforms.slug(this.value);
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
// src/lib/PayloadStore.ts
|
|
414
|
+
var PayloadStore = class {
|
|
415
|
+
constructor(archivist) {
|
|
416
|
+
this.archivist = archivist;
|
|
417
|
+
}
|
|
418
|
+
latest = [];
|
|
419
|
+
deleteListeners = [];
|
|
420
|
+
insertListeners = [];
|
|
421
|
+
listeners = [];
|
|
422
|
+
/**
|
|
423
|
+
* Initialize listeners on the archivist that update class member variables when new payloads are inserted
|
|
424
|
+
*/
|
|
425
|
+
static async create({ archivist, getLatest, idFunction }) {
|
|
426
|
+
const instance = new this(archivist);
|
|
427
|
+
const insertListener = async ({ payloads }) => {
|
|
428
|
+
if (payloads.some(idFunction)) {
|
|
429
|
+
instance.latest = await getLatest();
|
|
430
|
+
instance.emitChange();
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
const deleteListener = async () => {
|
|
434
|
+
instance.latest = await getLatest();
|
|
435
|
+
instance.emitChange();
|
|
436
|
+
};
|
|
437
|
+
const weakRefInsertListener = new WeakRef(insertListener);
|
|
438
|
+
const weakRefDeleteListener = new WeakRef(deleteListener);
|
|
439
|
+
const insertListenerRef = weakRefInsertListener.deref();
|
|
440
|
+
const deleteListenerRef = weakRefDeleteListener.deref();
|
|
441
|
+
if (insertListenerRef) instance.archivist.on("inserted", insertListenerRef);
|
|
442
|
+
if (deleteListenerRef) instance.archivist.on("deleted", deleteListenerRef);
|
|
443
|
+
instance.insertListeners.push(weakRefInsertListener);
|
|
444
|
+
instance.deleteListeners.push(weakRefDeleteListener);
|
|
445
|
+
instance.latest = await getLatest();
|
|
446
|
+
return instance;
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Remove all listeners and reset their class members
|
|
450
|
+
*/
|
|
451
|
+
cleanupListeners() {
|
|
452
|
+
this.removeListeners(this.insertListeners, "inserted");
|
|
453
|
+
this.removeListeners(this.deleteListeners, "deleted");
|
|
454
|
+
this.insertListeners = [];
|
|
455
|
+
this.deleteListeners = [];
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Get the current value of the store
|
|
459
|
+
*/
|
|
460
|
+
getSnapshot() {
|
|
461
|
+
return this.latest;
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Add a call back function that is invoked when new Payloads are inserted
|
|
465
|
+
*
|
|
466
|
+
* @param {PayloadListener} onStoreChange
|
|
467
|
+
*/
|
|
468
|
+
subscribe(onStoreChange) {
|
|
469
|
+
this.listeners.push(onStoreChange);
|
|
470
|
+
return () => {
|
|
471
|
+
this.listeners = this.listeners.filter((l) => l !== onStoreChange);
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
emitChange() {
|
|
475
|
+
for (const listener of this.listeners) {
|
|
476
|
+
listener();
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
480
|
+
removeListeners(listeners, eventName) {
|
|
481
|
+
for (const listener of listeners) {
|
|
482
|
+
const listenerRef = listener.deref();
|
|
483
|
+
if (listenerRef) this.archivist.off(eventName, listenerRef);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
// src/lib/tokenPlacesSplit.ts
|
|
489
|
+
import { assertEx as assertEx4 } from "@xylabs/assert";
|
|
490
|
+
var tokenPlacesSplit = (value, places) => {
|
|
491
|
+
assertEx4(places >= 0, () => "Places has to be >= 0");
|
|
492
|
+
const factor = BigInt(10 ** Math.abs(places));
|
|
493
|
+
const remainder = value % factor;
|
|
494
|
+
const wholeValue = value / factor;
|
|
495
|
+
return [wholeValue, remainder];
|
|
496
|
+
};
|
|
497
|
+
var tokenPlacesSplitString = (value, places) => {
|
|
498
|
+
if (value === void 0) {
|
|
499
|
+
return "-";
|
|
500
|
+
}
|
|
501
|
+
const [wholeValue, remainder] = tokenPlacesSplit(value, places);
|
|
502
|
+
return `${wholeValue}.${remainder.toString().padStart(Math.abs(places), "0")}`;
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
// src/intent/Resource.ts
|
|
506
|
+
var NO_RESULTS = [];
|
|
507
|
+
var DappIntentResource = class extends DappCallerBase {
|
|
508
|
+
_allDappIntents;
|
|
509
|
+
_latestDappIntent;
|
|
510
|
+
constructor(context) {
|
|
511
|
+
super(context);
|
|
512
|
+
}
|
|
513
|
+
get allDappIntents() {
|
|
514
|
+
return assertEx5(this._allDappIntents, () => "allDappIntents not set");
|
|
515
|
+
}
|
|
516
|
+
get latestDappIntent() {
|
|
517
|
+
return assertEx5(this._latestDappIntent, () => "latestDappIntent not set");
|
|
518
|
+
}
|
|
519
|
+
get subscriptions() {
|
|
520
|
+
return {
|
|
521
|
+
allDappIntents: (cb) => this.allDappIntents.subscribe(cb),
|
|
522
|
+
latestDappIntent: (cb) => this.latestDappIntent.subscribe(cb)
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
get views() {
|
|
526
|
+
return {
|
|
527
|
+
allDappIntents: () => this.allDappIntents.getSnapshot(),
|
|
528
|
+
latestDappIntent: () => this.latestDappIntent.getSnapshot()
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
cleanupListeners() {
|
|
532
|
+
this.latestDappIntent.cleanupListeners();
|
|
533
|
+
this.allDappIntents.cleanupListeners();
|
|
534
|
+
}
|
|
535
|
+
async start() {
|
|
536
|
+
const archivist = await this.getDappIntentArchivist();
|
|
537
|
+
this._allDappIntents = await PayloadStore.create({
|
|
538
|
+
archivist,
|
|
539
|
+
getLatest: async () => await archivist.all(),
|
|
540
|
+
idFunction: isDappIntent
|
|
541
|
+
});
|
|
542
|
+
this._latestDappIntent = await PayloadStore.create({
|
|
543
|
+
archivist,
|
|
544
|
+
getLatest: async () => {
|
|
545
|
+
const all = await archivist.all();
|
|
546
|
+
const result = all.length > 0 ? [all.at(-1)] : NO_RESULTS;
|
|
547
|
+
return result;
|
|
548
|
+
},
|
|
549
|
+
idFunction: isDappIntent
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
// src/wallet/DappSeedPhraseRepository.ts
|
|
555
|
+
import { HDWallet } from "@xyo-network/account";
|
|
556
|
+
import { PayloadDivinerQuerySchema as PayloadDivinerQuerySchema2 } from "@xyo-network/diviner-payload-model";
|
|
557
|
+
import { DappWalletSeedPhraseSchema } from "@xyo-network/os-model";
|
|
558
|
+
import { v4 as uuid2 } from "uuid";
|
|
559
|
+
|
|
560
|
+
// src/OsCallerBase.ts
|
|
561
|
+
import { assertEx as assertEx6 } from "@xylabs/assert";
|
|
562
|
+
import { asArchivistInstance as asArchivistInstance3 } from "@xyo-network/archivist-model";
|
|
563
|
+
import { asDivinerInstance as asDivinerInstance3 } from "@xyo-network/diviner-model";
|
|
564
|
+
var DappsArchivistModuleName = "DappsArchivist";
|
|
565
|
+
var DappsArchivistDevelopmentModuleName = `${DappsArchivistModuleName}Development`;
|
|
566
|
+
var DappsArchivistPayloadDivinerModuleName = "DappsArchivistPayloadDiviner";
|
|
567
|
+
var DappsArchivistPayloadDevelopmentDivinerModuleName = `${DappsArchivistPayloadDivinerModuleName}Development`;
|
|
568
|
+
var RegisteredDappInterfacesArchivistModuleName = "RegisteredDappInterfacesArchivist";
|
|
569
|
+
var OsCallerBase = class {
|
|
570
|
+
constructor(context) {
|
|
571
|
+
this.context = context;
|
|
572
|
+
}
|
|
573
|
+
// Get the dapps archivist
|
|
574
|
+
async getDappsArchivist() {
|
|
575
|
+
const mod = assertEx6(await this.context.root.resolve(DappsArchivistModuleName), () => `${DappsArchivistModuleName} not found`);
|
|
576
|
+
return asArchivistInstance3(mod, () => `${DappsArchivistModuleName} is not an archivist`);
|
|
577
|
+
}
|
|
578
|
+
// Get the dapps archivist development
|
|
579
|
+
async getDappsArchivistDevelopment() {
|
|
580
|
+
const mod = assertEx6(
|
|
581
|
+
await this.context.root.resolve(DappsArchivistDevelopmentModuleName),
|
|
582
|
+
() => `${DappsArchivistDevelopmentModuleName} not found`
|
|
583
|
+
);
|
|
584
|
+
return asArchivistInstance3(mod, () => `${DappsArchivistDevelopmentModuleName} is not an archivist`);
|
|
585
|
+
}
|
|
586
|
+
// Get the dapps archivist payload diviner
|
|
587
|
+
async getDappsArchivistPayloadDiviner() {
|
|
588
|
+
const mod = assertEx6(
|
|
589
|
+
await this.context.root.resolve(DappsArchivistPayloadDivinerModuleName),
|
|
590
|
+
() => `${DappsArchivistPayloadDivinerModuleName} not found`
|
|
591
|
+
);
|
|
592
|
+
return asDivinerInstance3(mod, () => `${DappsArchivistPayloadDivinerModuleName} is not a diviner`);
|
|
593
|
+
}
|
|
594
|
+
// Get the dapps archivist payload diviner development
|
|
595
|
+
async getDappsArchivistPayloadDivinerDevelopment() {
|
|
596
|
+
const mod = assertEx6(
|
|
597
|
+
await this.context.root.resolve(DappsArchivistPayloadDevelopmentDivinerModuleName),
|
|
598
|
+
() => `${DappsArchivistPayloadDevelopmentDivinerModuleName} not found`
|
|
599
|
+
);
|
|
600
|
+
return asDivinerInstance3(mod, () => `${DappsArchivistPayloadDevelopmentDivinerModuleName} is not a diviner`);
|
|
601
|
+
}
|
|
602
|
+
// Get the registered dapp interfaces archivist
|
|
603
|
+
async getRegisteredDappInterfacesArchivist() {
|
|
604
|
+
const mod = assertEx6(
|
|
605
|
+
await this.context.root.resolve(RegisteredDappInterfacesArchivistModuleName),
|
|
606
|
+
() => `${RegisteredDappInterfacesArchivistModuleName} not found`
|
|
607
|
+
);
|
|
608
|
+
return asArchivistInstance3(mod, () => `${RegisteredDappInterfacesArchivistModuleName} is not an archivist`);
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
// src/wallet/DappSeedPhraseRepository.ts
|
|
613
|
+
var DappSeedPhraseRepository = class extends OsCallerBase {
|
|
614
|
+
constructor(xyOs, allowedNames) {
|
|
615
|
+
super(xyOs);
|
|
616
|
+
this.allowedNames = allowedNames;
|
|
617
|
+
}
|
|
618
|
+
// record of all dapps that have requested a seed phrase with the OS
|
|
619
|
+
dappIdRepository = /* @__PURE__ */ new Map();
|
|
620
|
+
// record of all walletIds that have been issued to registered dapps
|
|
621
|
+
walletIdRepository = /* @__PURE__ */ new Map();
|
|
622
|
+
async add(dappId) {
|
|
623
|
+
const archivist = await this.getDappsArchivist();
|
|
624
|
+
const seedPhrase = this.newPhrase();
|
|
625
|
+
const walletId = uuid2();
|
|
626
|
+
const payload = {
|
|
627
|
+
dappId,
|
|
628
|
+
schema: DappWalletSeedPhraseSchema,
|
|
629
|
+
seedPhrase,
|
|
630
|
+
walletId
|
|
631
|
+
};
|
|
632
|
+
await archivist.insert([payload]);
|
|
633
|
+
this.dappIdRepository.set(dappId, payload);
|
|
634
|
+
this.walletIdRepository.set(walletId, payload);
|
|
635
|
+
return walletId;
|
|
636
|
+
}
|
|
637
|
+
async findOrCreate(dappName) {
|
|
638
|
+
if (this.allowedNames.includes(dappName)) {
|
|
639
|
+
const existingPayload = await this.checkDappId(dappName);
|
|
640
|
+
if (existingPayload) return existingPayload.walletId;
|
|
641
|
+
try {
|
|
642
|
+
await this.add(dappName);
|
|
643
|
+
const existingPayload2 = await this.checkDappId(dappName);
|
|
644
|
+
if (existingPayload2) return existingPayload2.walletId;
|
|
645
|
+
throw new Error(`failed to add seed phrase: ${dappName}`);
|
|
646
|
+
} catch (e) {
|
|
647
|
+
console.error(e);
|
|
648
|
+
throw new Error(`failed to add seed phrase: ${dappName}`);
|
|
649
|
+
}
|
|
650
|
+
} else {
|
|
651
|
+
throw new Error(`${dappName} is not allowed to register a seed phase. NOTE: this is a safeguard till 3rd party dApps can be validated`);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
async request(walletId) {
|
|
655
|
+
return (await this.checkWalletId(walletId))?.seedPhrase;
|
|
656
|
+
}
|
|
657
|
+
async checkDappId(dappId) {
|
|
658
|
+
const diviner = await this.getDappsArchivistPayloadDiviner();
|
|
659
|
+
const query = {
|
|
660
|
+
dappId,
|
|
661
|
+
limit: 1,
|
|
662
|
+
order: "desc",
|
|
663
|
+
schema: PayloadDivinerQuerySchema2,
|
|
664
|
+
schemas: [DappWalletSeedPhraseSchema]
|
|
665
|
+
};
|
|
666
|
+
const results = await diviner.divine([query]);
|
|
667
|
+
return results.length > 0 ? results[0] : void 0;
|
|
668
|
+
}
|
|
669
|
+
async checkWalletId(walletId) {
|
|
670
|
+
const diviner = await this.getDappsArchivistPayloadDiviner();
|
|
671
|
+
const query = {
|
|
672
|
+
limit: 1,
|
|
673
|
+
order: "desc",
|
|
674
|
+
schema: PayloadDivinerQuerySchema2,
|
|
675
|
+
schemas: [DappWalletSeedPhraseSchema],
|
|
676
|
+
walletId
|
|
677
|
+
};
|
|
678
|
+
const results = await diviner.divine([query]);
|
|
679
|
+
return results.length > 0 ? results[0] : void 0;
|
|
680
|
+
}
|
|
681
|
+
newPhrase() {
|
|
682
|
+
return HDWallet.generateMnemonic();
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
|
|
686
|
+
// src/XyOsDapp.ts
|
|
687
|
+
import { assertEx as assertEx8 } from "@xylabs/assert";
|
|
688
|
+
import { ManifestWrapper } from "@xyo-network/manifest-wrapper";
|
|
689
|
+
import { ModuleFactoryLocator as ModuleFactoryLocator2 } from "@xyo-network/module-factory-locator";
|
|
690
|
+
import { DappPackageManifestPayloadSchema } from "@xyo-network/os-model";
|
|
691
|
+
|
|
692
|
+
// src/manifests/dapp-window.manifest.json
|
|
693
|
+
var dapp_window_manifest_default = {
|
|
694
|
+
$schema: "https://raw.githubusercontent.com/XYOracleNetwork/sdk-xyo-client-js/main/packages/manifest/src/compilations/dapp-package-manifest-schema.json",
|
|
695
|
+
nodes: [
|
|
696
|
+
{
|
|
697
|
+
config: {
|
|
698
|
+
accountPath: "0'",
|
|
699
|
+
name: "Dapp",
|
|
700
|
+
schema: "network.xyo.node.config"
|
|
701
|
+
},
|
|
702
|
+
modules: {
|
|
703
|
+
private: [],
|
|
704
|
+
public: [
|
|
705
|
+
{
|
|
706
|
+
config: {
|
|
707
|
+
accountPath: "0'/0'",
|
|
708
|
+
name: "DappArchivist",
|
|
709
|
+
schema: "network.xyo.archivist.memory.config"
|
|
710
|
+
}
|
|
711
|
+
},
|
|
712
|
+
{
|
|
713
|
+
config: {
|
|
714
|
+
accountPath: "0'/1'",
|
|
715
|
+
archivist: "DappArchivist",
|
|
716
|
+
labels: {
|
|
717
|
+
"network.xyo.generic.payload.diviner": "GenericPayloadDiviner"
|
|
718
|
+
},
|
|
719
|
+
name: "DappArchivistPayloadDiviner",
|
|
720
|
+
schema: "network.xyo.diviner.payload.config"
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
]
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
],
|
|
727
|
+
schema: "network.xyo.manifest.package.dapp"
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
// src/XyOsBase.ts
|
|
731
|
+
import { assertEx as assertEx7 } from "@xylabs/assert";
|
|
732
|
+
import { BaseEmitter as BaseEmitter2 } from "@xyo-network/module-abstract";
|
|
733
|
+
import { ModuleFactoryLocator } from "@xyo-network/module-factory-locator";
|
|
734
|
+
import { Mutex } from "async-mutex";
|
|
735
|
+
var XyOsContextBase = class extends BaseEmitter2 {
|
|
736
|
+
_bootMutex = new Mutex();
|
|
737
|
+
_platformLocator;
|
|
738
|
+
_root;
|
|
739
|
+
get bios() {
|
|
740
|
+
return this.kernel?.bios;
|
|
741
|
+
}
|
|
742
|
+
get eventBus() {
|
|
743
|
+
return assertEx7(this.params.eventBus, () => "Missing event bus");
|
|
744
|
+
}
|
|
745
|
+
get exposedNode() {
|
|
746
|
+
throw new Error("Method not implemented. [exposedNode]");
|
|
747
|
+
}
|
|
748
|
+
get exposedNodeOuter() {
|
|
749
|
+
throw new Error("Method not implemented. [exposedNodeOuter]");
|
|
750
|
+
}
|
|
751
|
+
get installedDapps() {
|
|
752
|
+
return [];
|
|
753
|
+
}
|
|
754
|
+
get kernel() {
|
|
755
|
+
return void 0;
|
|
756
|
+
}
|
|
757
|
+
get manifest() {
|
|
758
|
+
return assertEx7(this.params.manifest, () => "Missing manifest");
|
|
759
|
+
}
|
|
760
|
+
get platformLocator() {
|
|
761
|
+
this._platformLocator = this._platformLocator ?? this.params.platformLocator ?? new ModuleFactoryLocator();
|
|
762
|
+
return this._platformLocator;
|
|
763
|
+
}
|
|
764
|
+
get possibleRoot() {
|
|
765
|
+
return this._root;
|
|
766
|
+
}
|
|
767
|
+
get root() {
|
|
768
|
+
return assertEx7(this._root, () => "XyOs root not set (probably not yet booted)");
|
|
769
|
+
}
|
|
770
|
+
get runningDapps() {
|
|
771
|
+
return [];
|
|
772
|
+
}
|
|
773
|
+
get state() {
|
|
774
|
+
return this._root ? "running" : "stopped";
|
|
775
|
+
}
|
|
776
|
+
boot(_wallet, _locator) {
|
|
777
|
+
throw new Error("Method not implemented. [boot]");
|
|
778
|
+
}
|
|
779
|
+
buildDapp(_manifest, _dappId) {
|
|
780
|
+
throw new Error("Method not implemented. [buildDapp]");
|
|
781
|
+
}
|
|
782
|
+
dappByName(_dappId) {
|
|
783
|
+
throw new Error("Method not implemented. [runningDapp]");
|
|
784
|
+
}
|
|
785
|
+
getDappParams(_dappId) {
|
|
786
|
+
throw new Error("Method not implemented. [getDappParams]");
|
|
787
|
+
}
|
|
788
|
+
getStack(_stackName) {
|
|
789
|
+
throw new Error("Method not implemented. [getStack]");
|
|
790
|
+
}
|
|
791
|
+
installDapp(_dappId) {
|
|
792
|
+
throw new Error("Method not implemented. [installDapp]");
|
|
793
|
+
}
|
|
794
|
+
monitor(_fn, _eventConfig) {
|
|
795
|
+
throw new Error("Method not implemented. [monitor]");
|
|
796
|
+
}
|
|
797
|
+
shutdown() {
|
|
798
|
+
throw new Error("Method not implemented. [shutdown]");
|
|
799
|
+
}
|
|
800
|
+
startDapp(_dappId) {
|
|
801
|
+
throw new Error("Method not implemented. [startDapp]");
|
|
802
|
+
}
|
|
803
|
+
stopDapp(_dappId) {
|
|
804
|
+
throw new Error("Method not implemented. [stopDapp]");
|
|
805
|
+
}
|
|
806
|
+
uninstallDapp(_dappId) {
|
|
807
|
+
throw new Error("Method not implemented. [uninstallDapp]");
|
|
808
|
+
}
|
|
809
|
+
};
|
|
810
|
+
|
|
811
|
+
// src/XyOsDapp.ts
|
|
812
|
+
var XyOsDapp = class extends XyOsContextBase {
|
|
813
|
+
dapp;
|
|
814
|
+
constructor(params) {
|
|
815
|
+
super(params);
|
|
816
|
+
this.dapp = { ...params.dapp };
|
|
817
|
+
}
|
|
818
|
+
get eventBus() {
|
|
819
|
+
return this.parent.eventBus;
|
|
820
|
+
}
|
|
821
|
+
get exposedNode() {
|
|
822
|
+
return this.parent.exposedNode;
|
|
823
|
+
}
|
|
824
|
+
get exposedNodeOuter() {
|
|
825
|
+
return this.parent.exposedNodeOuter;
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
* @deprecated Dapps should not have access to the bios. This is done for the
|
|
829
|
+
* settings dapp, but should be refactored out. Bios could possibly be an
|
|
830
|
+
* optional property of the parent context passed in for dapps that require
|
|
831
|
+
* elevated permissions.
|
|
832
|
+
* ^^^ I believe we may want this here in that is a dApp requests access to the bios it will optionally be there
|
|
833
|
+
*/
|
|
834
|
+
get kernel() {
|
|
835
|
+
return assertEx8(this.parent.kernel, () => "Missing kernel");
|
|
836
|
+
}
|
|
837
|
+
get parent() {
|
|
838
|
+
return this.params.parent;
|
|
839
|
+
}
|
|
840
|
+
async boot(wallet, locator) {
|
|
841
|
+
return await this._bootMutex.runExclusive(async () => {
|
|
842
|
+
console.log("XyOsDapp:boot:start", wallet.address, this.dapp.id);
|
|
843
|
+
assertEx8(this._root === void 0, () => "Dapp already booted");
|
|
844
|
+
const finalLocator = locator ? (this.params.locator ?? new ModuleFactoryLocator2()).merge(locator) : this.params.locator ?? new ModuleFactoryLocator2();
|
|
845
|
+
console.log("XyOsDapp:boot:finalLocator", finalLocator);
|
|
846
|
+
const dappNodesWrapper = new ManifestWrapper({ ...this.manifest, schema: DappPackageManifestPayloadSchema }, wallet, finalLocator);
|
|
847
|
+
const dappNodes = await dappNodesWrapper.loadNodes();
|
|
848
|
+
const dappManifestWallet = await wallet.derivePath("99999");
|
|
849
|
+
dapp_window_manifest_default.nodes[0].config.name = this.dapp.name + "Root";
|
|
850
|
+
const wrapper = new ManifestWrapper(
|
|
851
|
+
{ ...dapp_window_manifest_default, schema: DappPackageManifestPayloadSchema },
|
|
852
|
+
dappManifestWallet,
|
|
853
|
+
finalLocator
|
|
854
|
+
);
|
|
855
|
+
const node = await wrapper.loadNodeFromIndex(0);
|
|
856
|
+
for (const dappNode of dappNodes) {
|
|
857
|
+
await node.register(dappNode);
|
|
858
|
+
await node.attach(dappNode.address, true);
|
|
859
|
+
}
|
|
860
|
+
this._root = node;
|
|
861
|
+
console.log("XyOsDapp:boot:finish", wallet.address, this.dapp.id);
|
|
862
|
+
});
|
|
863
|
+
}
|
|
864
|
+
getStack(stackName) {
|
|
865
|
+
return this.parent.getStack(stackName);
|
|
866
|
+
}
|
|
867
|
+
};
|
|
868
|
+
|
|
869
|
+
// src/classes/system/ManageSystemDapps.ts
|
|
870
|
+
import { PayloadBuilder as PayloadBuilder3 } from "@xyo-network/payload-builder";
|
|
871
|
+
import semver from "semver";
|
|
872
|
+
|
|
873
|
+
// src/classes/system/Queries.ts
|
|
874
|
+
import { PayloadDivinerQuerySchema as PayloadDivinerQuerySchema3 } from "@xyo-network/diviner-payload-model";
|
|
875
|
+
import {
|
|
876
|
+
DappConfigSchema,
|
|
877
|
+
DappIconSchema,
|
|
878
|
+
DappWidgetConfigSchema,
|
|
879
|
+
UnregisteredDappAccessSchema
|
|
880
|
+
} from "@xyo-network/os-model";
|
|
881
|
+
import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
|
|
882
|
+
var SystemDappQueries = {
|
|
883
|
+
async getDappAccess(diviner, name, version) {
|
|
884
|
+
const accessQuery = {
|
|
885
|
+
order: "desc",
|
|
886
|
+
registeringDappId: name,
|
|
887
|
+
schema: PayloadDivinerQuerySchema3,
|
|
888
|
+
schemas: [UnregisteredDappAccessSchema],
|
|
889
|
+
version
|
|
890
|
+
};
|
|
891
|
+
return await diviner.divine([accessQuery]);
|
|
892
|
+
},
|
|
893
|
+
async getLatestConfig(diviner, name) {
|
|
894
|
+
const configQuery = {
|
|
895
|
+
limit: 1,
|
|
896
|
+
name,
|
|
897
|
+
order: "desc",
|
|
898
|
+
schema: PayloadDivinerQuerySchema3,
|
|
899
|
+
schemas: [DappConfigSchema]
|
|
900
|
+
};
|
|
901
|
+
const [dappConfig] = await diviner.divine([configQuery]);
|
|
902
|
+
return dappConfig;
|
|
903
|
+
},
|
|
904
|
+
async getLatestIcon(diviner, name, version) {
|
|
905
|
+
const iconQuery = {
|
|
906
|
+
name,
|
|
907
|
+
order: "desc",
|
|
908
|
+
schema: PayloadDivinerQuerySchema3,
|
|
909
|
+
schemas: [DappIconSchema],
|
|
910
|
+
version
|
|
911
|
+
};
|
|
912
|
+
const [dappIcon] = await diviner.divine([iconQuery]);
|
|
913
|
+
return dappIcon;
|
|
914
|
+
},
|
|
915
|
+
async getWidgetConfigs(diviner, name, version) {
|
|
916
|
+
const widgetConfigQuery = {
|
|
917
|
+
dappId: name,
|
|
918
|
+
order: "desc",
|
|
919
|
+
schema: PayloadDivinerQuerySchema3,
|
|
920
|
+
schemas: [DappWidgetConfigSchema],
|
|
921
|
+
version
|
|
922
|
+
};
|
|
923
|
+
const widgetConfigs = await diviner.divine([widgetConfigQuery]);
|
|
924
|
+
const uniqueConfigs = {};
|
|
925
|
+
for (const widgetConfig of widgetConfigs) {
|
|
926
|
+
const hash = await PayloadBuilder2.dataHash(widgetConfig);
|
|
927
|
+
uniqueConfigs[widgetConfig.mode ?? hash] = widgetConfig;
|
|
928
|
+
}
|
|
929
|
+
return Object.values(uniqueConfigs);
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
|
|
933
|
+
// src/classes/system/ManageSystemDapps.ts
|
|
934
|
+
var ManageSystemDapps = class extends OsCallerBase {
|
|
935
|
+
constructor(context, defaultSystemNames, defaultSystemDapps, defaultSystemDappParams, locator, developmentMode) {
|
|
936
|
+
super(context);
|
|
937
|
+
this.defaultSystemNames = defaultSystemNames;
|
|
938
|
+
this.defaultSystemDapps = defaultSystemDapps;
|
|
939
|
+
this.defaultSystemDappParams = defaultSystemDappParams;
|
|
940
|
+
this.locator = locator;
|
|
941
|
+
this.developmentMode = developmentMode;
|
|
942
|
+
}
|
|
943
|
+
onErrorCallbacks = [];
|
|
944
|
+
/**
|
|
945
|
+
* Add a callback to listen for errors throwing during system dapp registration
|
|
946
|
+
* @param {ErrorListener} errorListener
|
|
947
|
+
*/
|
|
948
|
+
addErrorListener(errorListener) {
|
|
949
|
+
this.onErrorCallbacks.push(errorListener);
|
|
950
|
+
}
|
|
951
|
+
/**
|
|
952
|
+
* Installs the dapps into the OS and builds the appropriate DappSet
|
|
953
|
+
*/
|
|
954
|
+
async install() {
|
|
955
|
+
await this.insertPayloads();
|
|
956
|
+
return await this.latestSets();
|
|
957
|
+
}
|
|
958
|
+
async freshInstall(archivist) {
|
|
959
|
+
await archivist.insert(this.defaultSystemDapps);
|
|
960
|
+
}
|
|
961
|
+
async insertPayloads() {
|
|
962
|
+
const archivist = this.developmentMode ? await this.getDappsArchivistDevelopment() : await this.getDappsArchivist();
|
|
963
|
+
try {
|
|
964
|
+
const allPayloads = await archivist.all();
|
|
965
|
+
if (allPayloads.length === 0) {
|
|
966
|
+
await this.freshInstall(archivist);
|
|
967
|
+
return true;
|
|
968
|
+
}
|
|
969
|
+
for (const systemDappPayload of this.defaultSystemDapps) {
|
|
970
|
+
try {
|
|
971
|
+
const validSemver = semver.valid(systemDappPayload.version);
|
|
972
|
+
if (!validSemver) throw new Error("semver is a valid type (string) but was not able to be parsed");
|
|
973
|
+
} catch (e) {
|
|
974
|
+
console.error(`${systemDappPayload.version} does not appear to be a valid semver value`, e);
|
|
975
|
+
continue;
|
|
976
|
+
}
|
|
977
|
+
const [existing] = await archivist.get([await PayloadBuilder3.dataHash(systemDappPayload)]);
|
|
978
|
+
if (existing) continue;
|
|
979
|
+
await archivist.insert([systemDappPayload]);
|
|
980
|
+
}
|
|
981
|
+
return true;
|
|
982
|
+
} catch (e) {
|
|
983
|
+
console.error("error installing system dapps");
|
|
984
|
+
throw e;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
async latestSets() {
|
|
988
|
+
const diviner = this.developmentMode ? await this.getDappsArchivistPayloadDivinerDevelopment() : await this.getDappsArchivistPayloadDiviner();
|
|
989
|
+
const dappSets = [];
|
|
990
|
+
const failedPayloads = [];
|
|
991
|
+
for (const name of this.defaultSystemNames) {
|
|
992
|
+
try {
|
|
993
|
+
const dappConfig = await SystemDappQueries.getLatestConfig(diviner, name);
|
|
994
|
+
if (!dappConfig) {
|
|
995
|
+
console.error(`No config found for dapp ${name}`);
|
|
996
|
+
continue;
|
|
997
|
+
}
|
|
998
|
+
const dappIcon = await SystemDappQueries.getLatestIcon(diviner, name, dappConfig.version);
|
|
999
|
+
if (!dappIcon) {
|
|
1000
|
+
console.error(`No icon found for dapp ${dappConfig.name}`);
|
|
1001
|
+
failedPayloads.push(dappConfig);
|
|
1002
|
+
continue;
|
|
1003
|
+
}
|
|
1004
|
+
const dappAccess = await SystemDappQueries.getDappAccess(diviner, name, dappConfig.version);
|
|
1005
|
+
const dappParams = this.defaultSystemDappParams[dappConfig.name];
|
|
1006
|
+
if (!dappParams) {
|
|
1007
|
+
console.error(`No params found for dapp ${dappConfig.name}. Did you forget to add it to OsDappParams?`);
|
|
1008
|
+
failedPayloads.push(dappConfig);
|
|
1009
|
+
continue;
|
|
1010
|
+
}
|
|
1011
|
+
const dappWidgetConfigs = await SystemDappQueries.getWidgetConfigs(diviner, name, dappConfig.version);
|
|
1012
|
+
dappSets.push({
|
|
1013
|
+
dapp: {
|
|
1014
|
+
accessors: dappAccess,
|
|
1015
|
+
config: dappConfig,
|
|
1016
|
+
icon: dappIcon,
|
|
1017
|
+
params: dappParams,
|
|
1018
|
+
widgetConfigs: dappWidgetConfigs
|
|
1019
|
+
}
|
|
1020
|
+
});
|
|
1021
|
+
} catch {
|
|
1022
|
+
console.error(`Failed to build dapp ${name}`);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
if (failedPayloads.length > 0) {
|
|
1026
|
+
for (const cb of this.onErrorCallbacks) cb(failedPayloads);
|
|
1027
|
+
}
|
|
1028
|
+
return dappSets;
|
|
1029
|
+
}
|
|
1030
|
+
};
|
|
1031
|
+
|
|
1032
|
+
// src/classes/registration/DappRegistry.ts
|
|
1033
|
+
import { HDWallet as HDWallet3 } from "@xyo-network/account";
|
|
1034
|
+
import {
|
|
1035
|
+
DappRegisteredSchema,
|
|
1036
|
+
DappRegisteredState,
|
|
1037
|
+
isRegisteredDappAccess
|
|
1038
|
+
} from "@xyo-network/os-model";
|
|
1039
|
+
|
|
1040
|
+
// src/classes/registration/ValidateDappAccessDiviner/Config.ts
|
|
1041
|
+
import { isPayloadOfSchemaTypeWithMeta } from "@xyo-network/payload-model";
|
|
1042
|
+
var FailedAccessorSchema = "network.xyo.os.failed.accessor.registration";
|
|
1043
|
+
var isFailedAccessor = isPayloadOfSchemaTypeWithMeta(FailedAccessorSchema);
|
|
1044
|
+
var ValidateDappAccessDivinerConfigSchema = "network.xyo.os.dapp.access.registration.validate";
|
|
1045
|
+
|
|
1046
|
+
// src/classes/registration/ValidateDappAccessDiviner/Diviner.ts
|
|
1047
|
+
import { HDWallet as HDWallet2 } from "@xyo-network/account";
|
|
1048
|
+
import { AbstractDiviner } from "@xyo-network/diviner-abstract";
|
|
1049
|
+
import { ManifestWrapper as ManifestWrapper2, PackageManifestPayloadSchema } from "@xyo-network/manifest";
|
|
1050
|
+
import {
|
|
1051
|
+
isDappPackageManifestPayload,
|
|
1052
|
+
isUnregisteredDappAccess,
|
|
1053
|
+
RegisteredDappAccessSchema
|
|
1054
|
+
} from "@xyo-network/os-model";
|
|
1055
|
+
import { PayloadBuilder as PayloadBuilder4 } from "@xyo-network/payload-builder";
|
|
1056
|
+
var ValidateDappAccessDiviner = class extends AbstractDiviner {
|
|
1057
|
+
static configSchemas = [ValidateDappAccessDivinerConfigSchema];
|
|
1058
|
+
async divineHandler(payloads) {
|
|
1059
|
+
const dappManifest = payloads?.filter(isDappPackageManifestPayload);
|
|
1060
|
+
const accessors = payloads?.filter(isUnregisteredDappAccess);
|
|
1061
|
+
const dappParams = this.params.dappParams;
|
|
1062
|
+
if (dappManifest.length > 1) {
|
|
1063
|
+
throw new Error("Only one dapp manifest payload is allowed");
|
|
1064
|
+
}
|
|
1065
|
+
if (!dappManifest || dappManifest.length === 0 || accessors?.length === 0) {
|
|
1066
|
+
return [];
|
|
1067
|
+
}
|
|
1068
|
+
const registeredAccessors = [];
|
|
1069
|
+
const failedAccessors = [];
|
|
1070
|
+
for (const access of accessors) {
|
|
1071
|
+
try {
|
|
1072
|
+
if (access) {
|
|
1073
|
+
const dappAccessInterface = ValidDappAccessInterfaces[access.name];
|
|
1074
|
+
if (!dappAccessInterface) {
|
|
1075
|
+
console.warn("Invalid dapp access interface", access.name);
|
|
1076
|
+
continue;
|
|
1077
|
+
}
|
|
1078
|
+
const dappManifestToTest = dappPackageManifestToPackageManifest(dappManifest[0]);
|
|
1079
|
+
const dappChildren = await getNodeChildren(dappManifestToTest, dappParams);
|
|
1080
|
+
const interfaceChildren = await getNodeChildren(dappAccessInterface.manifest, dappParams);
|
|
1081
|
+
const valid = compareChildren(interfaceChildren, dappChildren);
|
|
1082
|
+
if (valid) {
|
|
1083
|
+
const payload = await PayloadBuilder4.build({
|
|
1084
|
+
...access,
|
|
1085
|
+
schema: RegisteredDappAccessSchema,
|
|
1086
|
+
timestamp: Date.now()
|
|
1087
|
+
});
|
|
1088
|
+
registeredAccessors.push(payload);
|
|
1089
|
+
} else {
|
|
1090
|
+
const failedRegistration = await PayloadBuilder4.build({
|
|
1091
|
+
accessor: access,
|
|
1092
|
+
errorMessage: "Invalid dapp access interface",
|
|
1093
|
+
schema: FailedAccessorSchema
|
|
1094
|
+
});
|
|
1095
|
+
failedAccessors.push(failedRegistration);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
} catch (e) {
|
|
1099
|
+
failedAccessors.push(
|
|
1100
|
+
await PayloadBuilder4.build({ accessor: access, errorMessage: e.message, schema: FailedAccessorSchema })
|
|
1101
|
+
);
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
return [...registeredAccessors, ...failedAccessors];
|
|
1105
|
+
}
|
|
1106
|
+
};
|
|
1107
|
+
var compareChildren = (interfaceChildren, dappChildren) => {
|
|
1108
|
+
return interfaceChildren.every(
|
|
1109
|
+
(interfaceChild) => Object.values(interfaceChild).every(
|
|
1110
|
+
(interfaceChildName) => dappChildren.some((dappChild) => Object.values(dappChild).includes(interfaceChildName))
|
|
1111
|
+
)
|
|
1112
|
+
);
|
|
1113
|
+
};
|
|
1114
|
+
var dappPackageManifestToPackageManifest = (dappPackageManifest) => {
|
|
1115
|
+
return {
|
|
1116
|
+
...dappPackageManifest,
|
|
1117
|
+
schema: PackageManifestPayloadSchema
|
|
1118
|
+
};
|
|
1119
|
+
};
|
|
1120
|
+
var getChildrenFromNode = async (node) => {
|
|
1121
|
+
const nodeManifest = (await node.state())?.[0];
|
|
1122
|
+
return nodeManifest.status?.children;
|
|
1123
|
+
};
|
|
1124
|
+
var getNodeChildren = async (manifestToTest, dappParams) => {
|
|
1125
|
+
const testNodes = await getNodeToTest(manifestToTest, dappParams);
|
|
1126
|
+
const children = [];
|
|
1127
|
+
for (const node of testNodes) {
|
|
1128
|
+
const nodeChildren = await getChildrenFromNode(node);
|
|
1129
|
+
if (nodeChildren) {
|
|
1130
|
+
children.push(nodeChildren);
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
return children;
|
|
1134
|
+
};
|
|
1135
|
+
var getNodeToTest = async (manifestToTest, dappParams) => {
|
|
1136
|
+
const dappManifestWrapper = new ManifestWrapper2(manifestToTest, await HDWallet2.random(), dappParams.locator);
|
|
1137
|
+
return await dappManifestWrapper.loadNodes();
|
|
1138
|
+
};
|
|
1139
|
+
|
|
1140
|
+
// src/classes/registration/DappRegistry.ts
|
|
1141
|
+
var DappRegistry = class {
|
|
1142
|
+
constructor(dappSeedPhraseRepository) {
|
|
1143
|
+
this.dappSeedPhraseRepository = dappSeedPhraseRepository;
|
|
1144
|
+
}
|
|
1145
|
+
dappRegistry = /* @__PURE__ */ new Map();
|
|
1146
|
+
/**
|
|
1147
|
+
* Register a Dapp with window manager and if successful, return its id
|
|
1148
|
+
*
|
|
1149
|
+
* @param {UnregisteredDapp} dapp Manifest and UI of the Dapp to register
|
|
1150
|
+
* @returns {RegisteredDapp} dapp with registration fields
|
|
1151
|
+
**/
|
|
1152
|
+
async registerDapp(dapp) {
|
|
1153
|
+
if (dapp) {
|
|
1154
|
+
const { exposedModuleIds, manifest, modes, name, version, params, widgetConfigs } = this.extractDappProperties(dapp);
|
|
1155
|
+
try {
|
|
1156
|
+
const walletId = await this.dappSeedPhraseRepository.findOrCreate(name);
|
|
1157
|
+
const registeredAccessors = await this.validateDappAccessPayloads(manifest, dapp.accessors, params);
|
|
1158
|
+
const registeredDapp = {
|
|
1159
|
+
accessors: registeredAccessors,
|
|
1160
|
+
config: {
|
|
1161
|
+
exposedModuleIds,
|
|
1162
|
+
manifest,
|
|
1163
|
+
modes,
|
|
1164
|
+
name,
|
|
1165
|
+
schema: DappRegisteredSchema,
|
|
1166
|
+
state: DappRegisteredState.Registered,
|
|
1167
|
+
version,
|
|
1168
|
+
// attach the walletId to a registered dapp so it can request it back in the future
|
|
1169
|
+
walletId
|
|
1170
|
+
},
|
|
1171
|
+
params,
|
|
1172
|
+
widgetConfigs
|
|
1173
|
+
};
|
|
1174
|
+
if (this.dappRegistry.get(name)) {
|
|
1175
|
+
console.warn("tried to register two dapps with the same name", name);
|
|
1176
|
+
return null;
|
|
1177
|
+
}
|
|
1178
|
+
this.dappRegistry.set(name, registeredDapp);
|
|
1179
|
+
return registeredDapp;
|
|
1180
|
+
} catch (e) {
|
|
1181
|
+
console.error("Error registeringDapp", e);
|
|
1182
|
+
return null;
|
|
1183
|
+
}
|
|
1184
|
+
} else {
|
|
1185
|
+
return null;
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
/**
|
|
1189
|
+
* Unregister a dapp so it can no longer be launched
|
|
1190
|
+
*
|
|
1191
|
+
* @param dappId
|
|
1192
|
+
*/
|
|
1193
|
+
unregisterDapp(dappId) {
|
|
1194
|
+
this.dappRegistry.delete(dappId);
|
|
1195
|
+
}
|
|
1196
|
+
extractDappProperties(dapp) {
|
|
1197
|
+
const { params, config, widgetConfigs } = dapp;
|
|
1198
|
+
const { exposedModuleIds, manifest, modes, name, sources, version } = config;
|
|
1199
|
+
return { exposedModuleIds, manifest, modes, name, params, sources, version, widgetConfigs };
|
|
1200
|
+
}
|
|
1201
|
+
async validateDappAccessPayloads(manifest, accessors = [], params) {
|
|
1202
|
+
const validateDappAccess = await ValidateDappAccessDiviner.create({
|
|
1203
|
+
account: await HDWallet3.random(),
|
|
1204
|
+
config: { schema: ValidateDappAccessDivinerConfigSchema },
|
|
1205
|
+
dappParams: params
|
|
1206
|
+
});
|
|
1207
|
+
const payloads = await validateDappAccess.divine([manifest, ...accessors]);
|
|
1208
|
+
const failedAccessors = payloads.filter(isFailedAccessor);
|
|
1209
|
+
const registeredAccessors = payloads.filter(isRegisteredDappAccess);
|
|
1210
|
+
if (failedAccessors.length > 0) {
|
|
1211
|
+
console.error("Failed to register all dapp accessors", failedAccessors);
|
|
1212
|
+
}
|
|
1213
|
+
return registeredAccessors;
|
|
1214
|
+
}
|
|
1215
|
+
};
|
|
1216
|
+
|
|
1217
|
+
// src/classes/registration/DappRegistrationService.ts
|
|
1218
|
+
var DappRegistrationService = class {
|
|
1219
|
+
constructor(context, params = { dappNames: [], dappParams: {}, dappPayloads: [] }, locator = new ModuleFactoryLocator3(), developmentMode) {
|
|
1220
|
+
this.context = context;
|
|
1221
|
+
this.params = params;
|
|
1222
|
+
this.locator = locator;
|
|
1223
|
+
this.developmentMode = developmentMode;
|
|
1224
|
+
this.manageSystemDapps = new ManageSystemDapps(
|
|
1225
|
+
context,
|
|
1226
|
+
this.params?.dappNames ?? [],
|
|
1227
|
+
this.params?.dappPayloads ?? [],
|
|
1228
|
+
this.params?.dappParams ?? {},
|
|
1229
|
+
this.locator,
|
|
1230
|
+
this.developmentMode
|
|
1231
|
+
);
|
|
1232
|
+
this.dappSeedPhraseRepository = new DappSeedPhraseRepository(context, this.params?.dappNames ?? []);
|
|
1233
|
+
this.dappRegistry = new DappRegistry(this.dappSeedPhraseRepository);
|
|
1234
|
+
this.addConnectionRequests();
|
|
1235
|
+
}
|
|
1236
|
+
// Dapps that have been built with their own context
|
|
1237
|
+
builtDapps = {};
|
|
1238
|
+
dappRegistry;
|
|
1239
|
+
dappSeedPhraseRepository;
|
|
1240
|
+
manageSystemDapps;
|
|
1241
|
+
// Dapps that have been registered with dappAccessRequests
|
|
1242
|
+
registeredAccessDappSets = /* @__PURE__ */ new Set();
|
|
1243
|
+
// Dapps that have been registered with the dapp registry
|
|
1244
|
+
registeredDappSets = /* @__PURE__ */ new Set();
|
|
1245
|
+
// Dapps that have been registered with exposeDappRequests
|
|
1246
|
+
registeredExposedDappSets = /* @__PURE__ */ new Set();
|
|
1247
|
+
dappAccessRequestConnection = dappAccessRequestConnection();
|
|
1248
|
+
exposeDappRequestConnection = exposeDappRequestConnection();
|
|
1249
|
+
buildDapp(manifest, dappId = NameTransforms.slug(manifest.nodes[0]?.config.name), name = NameTransforms.moduleName(manifest.nodes[0]?.config.name)) {
|
|
1250
|
+
const dapp = new XyOsDapp({ dapp: { id: dappId, name }, locator: this.locator, manifest, parent: this.context });
|
|
1251
|
+
this.builtDapps[dappId] = dapp;
|
|
1252
|
+
return dapp;
|
|
1253
|
+
}
|
|
1254
|
+
getParamsForRegisteredDapp(dappId) {
|
|
1255
|
+
return this.params?.dappParams[dappId];
|
|
1256
|
+
}
|
|
1257
|
+
async start() {
|
|
1258
|
+
const systemDapps = await this.context.monitor(async () => await this.manageSystemDapps.install(), { name: "Install System dApps" });
|
|
1259
|
+
const activeDapps = systemDapps.filter(({ dapp }) => dapp.icon.active === true);
|
|
1260
|
+
const results = await Promise.allSettled(
|
|
1261
|
+
activeDapps.map(async (dappSet) => {
|
|
1262
|
+
const { dapp } = dappSet;
|
|
1263
|
+
const registeredDapp = await this.dappRegistry.registerDapp(dapp);
|
|
1264
|
+
const result = { dapp: registeredDapp, dappIcon: dapp.icon };
|
|
1265
|
+
const forgetHandlers = async () => {
|
|
1266
|
+
try {
|
|
1267
|
+
await this.postRegistrationHandlers(result);
|
|
1268
|
+
} catch (e) {
|
|
1269
|
+
console.error("Error in post registration handlers", e);
|
|
1270
|
+
}
|
|
1271
|
+
};
|
|
1272
|
+
forget2(forgetHandlers());
|
|
1273
|
+
return result;
|
|
1274
|
+
})
|
|
1275
|
+
);
|
|
1276
|
+
const failed = results.filter(rejected).map((result) => result.reason);
|
|
1277
|
+
const succeeded = results.filter(fulfilled).map((result) => result.value);
|
|
1278
|
+
for (const registeredDapp of succeeded) this.registeredDappSets.add(registeredDapp);
|
|
1279
|
+
return { failed, succeeded };
|
|
1280
|
+
}
|
|
1281
|
+
addConnectionRequests() {
|
|
1282
|
+
this.context.eventBus.addConnection(this.dappAccessRequestConnection);
|
|
1283
|
+
this.context.eventBus.addConnection(this.exposeDappRequestConnection);
|
|
1284
|
+
}
|
|
1285
|
+
async postRegistrationHandlers(registeredDapp) {
|
|
1286
|
+
if (registeredDapp.dapp) {
|
|
1287
|
+
if (isRegisteredDappExposedDappSet(registeredDapp)) {
|
|
1288
|
+
this.registeredExposedDappSets.add(registeredDapp);
|
|
1289
|
+
const exposeIntent = DappIntentCaller.buildIntent(
|
|
1290
|
+
DappIntentCaller.OsDappName,
|
|
1291
|
+
DappIntentTypes.Launch,
|
|
1292
|
+
registeredDapp.dapp.config.name,
|
|
1293
|
+
DappMode2.Exposed
|
|
1294
|
+
);
|
|
1295
|
+
await this.exposeDappRequestConnection.emit(ExposeDappRequestEvent, { payloads: [registeredDapp.dapp.config, exposeIntent] });
|
|
1296
|
+
}
|
|
1297
|
+
if (isRegisteredDappAccessDappSet(registeredDapp)) {
|
|
1298
|
+
this.registeredAccessDappSets.add(registeredDapp);
|
|
1299
|
+
await this.dappAccessRequestConnection.emit(DappAccessRequestEvent, { payloads: registeredDapp.dapp.accessors });
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
};
|
|
1304
|
+
|
|
1305
|
+
// src/loadOsNode.ts
|
|
1306
|
+
import { IndexedDbArchivist } from "@xyo-network/archivist-indexeddb";
|
|
1307
|
+
import { IndexedDbPayloadDiviner } from "@xyo-network/diviner-payload-indexeddb";
|
|
1308
|
+
import { ManifestWrapper as ManifestWrapper3 } from "@xyo-network/manifest-wrapper";
|
|
1309
|
+
import { ModuleFactoryLocator as ModuleFactoryLocator4 } from "@xyo-network/module-factory-locator";
|
|
1310
|
+
|
|
1311
|
+
// src/manifest/ManifestReplaceableTokens.ts
|
|
1312
|
+
import { getXnsDomain } from "@xyo-network/kernel";
|
|
1313
|
+
var ManifestReplaceableTokens = (xnsNodeUrl, xnsNetwork) => ({
|
|
1314
|
+
"[REPLACE_WITH_NS_NODE_URL]": getXnsDomain(xnsNodeUrl, xnsNetwork)
|
|
1315
|
+
});
|
|
1316
|
+
var ReplaceManifestTokens = (manifest, xnsNodeUrl, xnsNetwork) => {
|
|
1317
|
+
let stringifiedManifestReplaced = JSON.stringify(manifest);
|
|
1318
|
+
for (const [token, value] of Object.entries(ManifestReplaceableTokens(xnsNodeUrl, xnsNetwork))) {
|
|
1319
|
+
stringifiedManifestReplaced = stringifiedManifestReplaced.replaceAll(token, value);
|
|
1320
|
+
}
|
|
1321
|
+
return JSON.parse(stringifiedManifestReplaced);
|
|
1322
|
+
};
|
|
1323
|
+
|
|
1324
|
+
// src/manifest/os-node.manifest.json
|
|
1325
|
+
var os_node_manifest_default = {
|
|
1326
|
+
$schema: "https://raw.githubusercontent.com/XYOracleNetwork/sdk-xyo-client-js/main/packages/manifest/src/compilations/dapp-package-manifest-schema.json",
|
|
1327
|
+
nodes: [
|
|
1328
|
+
{
|
|
1329
|
+
config: {
|
|
1330
|
+
accountPath: "0'",
|
|
1331
|
+
name: "OsNode",
|
|
1332
|
+
schema: "network.xyo.node.config"
|
|
1333
|
+
},
|
|
1334
|
+
modules: {
|
|
1335
|
+
private: [],
|
|
1336
|
+
public: [
|
|
1337
|
+
{
|
|
1338
|
+
config: {
|
|
1339
|
+
accountPath: "1'",
|
|
1340
|
+
name: "OSArchivist",
|
|
1341
|
+
dbName: "OSArchivist",
|
|
1342
|
+
labels: {
|
|
1343
|
+
"network.xyo.archivist.persistence.scope": "device"
|
|
1344
|
+
},
|
|
1345
|
+
storeName: "payloads",
|
|
1346
|
+
parents: {
|
|
1347
|
+
read: [
|
|
1348
|
+
"Archivist"
|
|
1349
|
+
]
|
|
1350
|
+
},
|
|
1351
|
+
schema: "network.xyo.archivist.config"
|
|
1352
|
+
}
|
|
1353
|
+
},
|
|
1354
|
+
{
|
|
1355
|
+
config: {
|
|
1356
|
+
accountPath: "3'",
|
|
1357
|
+
name: "DappsArchivist",
|
|
1358
|
+
dbName: "DappsArchivist",
|
|
1359
|
+
labels: {
|
|
1360
|
+
"network.xyo.archivist.persistence.scope": "device"
|
|
1361
|
+
},
|
|
1362
|
+
storeName: "payloads",
|
|
1363
|
+
schema: "network.xyo.archivist.config"
|
|
1364
|
+
}
|
|
1365
|
+
},
|
|
1366
|
+
{
|
|
1367
|
+
config: {
|
|
1368
|
+
archivist: "DappsArchivist",
|
|
1369
|
+
dbVersion: 1,
|
|
1370
|
+
name: "DappsArchivistPayloadDiviner",
|
|
1371
|
+
labels: {
|
|
1372
|
+
"network.xyo.archivist.persistence.scope": "device"
|
|
1373
|
+
},
|
|
1374
|
+
schema: "network.xyo.diviner.payload.config"
|
|
1375
|
+
}
|
|
1376
|
+
},
|
|
1377
|
+
{
|
|
1378
|
+
config: {
|
|
1379
|
+
accountPath: "4'",
|
|
1380
|
+
dbName: "IntentArchivist",
|
|
1381
|
+
name: "IntentArchivist",
|
|
1382
|
+
storeName: "payloads",
|
|
1383
|
+
labels: {
|
|
1384
|
+
"network.xyo.archivist.persistence.scope": "device"
|
|
1385
|
+
},
|
|
1386
|
+
schema: "network.xyo.archivist.config"
|
|
1387
|
+
}
|
|
1388
|
+
},
|
|
1389
|
+
{
|
|
1390
|
+
config: {
|
|
1391
|
+
accountPath: "7",
|
|
1392
|
+
archivist: "IntentArchivist",
|
|
1393
|
+
dbVersion: 1,
|
|
1394
|
+
name: "IntentArchivistPayloadDiviner",
|
|
1395
|
+
labels: {
|
|
1396
|
+
"network.xyo.archivist.persistence.scope": "device"
|
|
1397
|
+
},
|
|
1398
|
+
schema: "network.xyo.diviner.payload.config",
|
|
1399
|
+
storeName: "payloads"
|
|
1400
|
+
}
|
|
1401
|
+
},
|
|
1402
|
+
{
|
|
1403
|
+
config: {
|
|
1404
|
+
accountPath: "8",
|
|
1405
|
+
name: "RegisteredDappInterfacesArchivist",
|
|
1406
|
+
schema: "network.xyo.archivist.memory.config"
|
|
1407
|
+
}
|
|
1408
|
+
},
|
|
1409
|
+
{
|
|
1410
|
+
config: {
|
|
1411
|
+
accountPath: "9'",
|
|
1412
|
+
name: "DappsArchivistDevelopment",
|
|
1413
|
+
parents: {
|
|
1414
|
+
write: [
|
|
1415
|
+
"DappsArchivist"
|
|
1416
|
+
]
|
|
1417
|
+
},
|
|
1418
|
+
schema: "network.xyo.archivist.memory.config"
|
|
1419
|
+
}
|
|
1420
|
+
},
|
|
1421
|
+
{
|
|
1422
|
+
config: {
|
|
1423
|
+
accountPath: "10",
|
|
1424
|
+
archivist: "DappsArchivistDevelopment",
|
|
1425
|
+
dbVersion: 1,
|
|
1426
|
+
name: "DappsArchivistPayloadDivinerDevelopment",
|
|
1427
|
+
labels: {
|
|
1428
|
+
"network.xyo.generic.payload.diviner": "GenericPayloadDiviner"
|
|
1429
|
+
},
|
|
1430
|
+
schema: "network.xyo.diviner.payload.generic.config"
|
|
1431
|
+
}
|
|
1432
|
+
},
|
|
1433
|
+
{
|
|
1434
|
+
config: {
|
|
1435
|
+
name: "OsPubSubNetworkStackNode",
|
|
1436
|
+
schema: "network.xyo.node.config"
|
|
1437
|
+
}
|
|
1438
|
+
},
|
|
1439
|
+
{
|
|
1440
|
+
config: {
|
|
1441
|
+
name: "OsSettingsStackNode",
|
|
1442
|
+
schema: "network.xyo.node.config"
|
|
1443
|
+
}
|
|
1444
|
+
},
|
|
1445
|
+
{
|
|
1446
|
+
config: {
|
|
1447
|
+
name: "OsXyoPublicNetworkStackNode",
|
|
1448
|
+
schema: "network.xyo.node.config"
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
]
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
],
|
|
1455
|
+
schema: "network.xyo.manifest.package.dapp"
|
|
1456
|
+
};
|
|
1457
|
+
|
|
1458
|
+
// src/loadOsNode.ts
|
|
1459
|
+
var OS_NODE_PATH = "1'";
|
|
1460
|
+
var getDefaultOsNodeLocator = () => {
|
|
1461
|
+
const locator = new ModuleFactoryLocator4();
|
|
1462
|
+
locator.register(IndexedDbArchivist, { "network.xyo.archivist.persistence.scope": "device" });
|
|
1463
|
+
locator.register(IndexedDbPayloadDiviner, { "network.xyo.archivist.persistence.scope": "device" });
|
|
1464
|
+
return locator;
|
|
1465
|
+
};
|
|
1466
|
+
var loadOsNode = async (osWallet, locator) => {
|
|
1467
|
+
try {
|
|
1468
|
+
const osNodeWallet = await osWallet.derivePath(OS_NODE_PATH);
|
|
1469
|
+
const osNodeLocator = locator ?? getDefaultOsNodeLocator();
|
|
1470
|
+
const manifestWrapper = new ManifestWrapper3(os_node_manifest_default, osNodeWallet, osNodeLocator);
|
|
1471
|
+
const osNode = (await manifestWrapper.loadNodes())[0];
|
|
1472
|
+
return [osNode];
|
|
1473
|
+
} catch (e) {
|
|
1474
|
+
const error = e;
|
|
1475
|
+
console.error(`Error creating os node: ${error.message}`);
|
|
1476
|
+
console.error(`Error creating os node: ${error.stack}`);
|
|
1477
|
+
throw new Error(`Error creating os node: ${error.message}`);
|
|
1478
|
+
}
|
|
1479
|
+
};
|
|
1480
|
+
|
|
1481
|
+
// src/stack/Base.ts
|
|
1482
|
+
import { assertEx as assertEx9 } from "@xylabs/assert";
|
|
1483
|
+
import { forget as forget3 } from "@xylabs/forget";
|
|
1484
|
+
import { BaseEmitter as BaseEmitter3 } from "@xyo-network/module-abstract";
|
|
1485
|
+
import { isModuleInstance } from "@xyo-network/module-model";
|
|
1486
|
+
import { asAttachableNodeInstance } from "@xyo-network/node-model";
|
|
1487
|
+
var StackBase = class extends BaseEmitter3 {
|
|
1488
|
+
// flag to check that all adapters are initialized
|
|
1489
|
+
initialized = false;
|
|
1490
|
+
// adapters to initialize into the stack
|
|
1491
|
+
_adapterSet;
|
|
1492
|
+
_context;
|
|
1493
|
+
// stack node
|
|
1494
|
+
_stack;
|
|
1495
|
+
// stack node module identifier for the stack
|
|
1496
|
+
_stackNodeModuleId;
|
|
1497
|
+
// list of initialized nodes
|
|
1498
|
+
initializedNodes = [];
|
|
1499
|
+
// list of listeners - lister type not important since its only recalled to stop listening
|
|
1500
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1501
|
+
listeners = [];
|
|
1502
|
+
constructor(context, adapterSet, stackNodeModuleId) {
|
|
1503
|
+
super({});
|
|
1504
|
+
this._context = context;
|
|
1505
|
+
this._adapterSet = adapterSet(assertEx9(this.context.kernel, "Kernel access required"));
|
|
1506
|
+
this._stackNodeModuleId = stackNodeModuleId;
|
|
1507
|
+
}
|
|
1508
|
+
get adapterSet() {
|
|
1509
|
+
return assertEx9(this._adapterSet, () => `${this.stackNodeModuleId} adapters not initialized`);
|
|
1510
|
+
}
|
|
1511
|
+
get context() {
|
|
1512
|
+
return assertEx9(this._context, () => `${this.stackNodeModuleId} stack context not initialized`);
|
|
1513
|
+
}
|
|
1514
|
+
get stack() {
|
|
1515
|
+
return assertEx9(this._stack, () => `${this.stackNodeModuleId} stack node not initialized`);
|
|
1516
|
+
}
|
|
1517
|
+
get stackNodeModuleId() {
|
|
1518
|
+
return assertEx9(this._stackNodeModuleId, () => "Stack node module identifier not initialized");
|
|
1519
|
+
}
|
|
1520
|
+
initialize() {
|
|
1521
|
+
this.assignStack();
|
|
1522
|
+
for (const adapter of this.adapterSet) {
|
|
1523
|
+
const driverReadyListener = async ({ node }) => await this.handleDriverReady(node);
|
|
1524
|
+
adapter.on("driverReady", driverReadyListener);
|
|
1525
|
+
this.listeners.push({ adapter, eventName: "driverReady", listener: driverReadyListener });
|
|
1526
|
+
const driverErrorListener = async ({ error }) => {
|
|
1527
|
+
console.error(`Error updating ${this.stackNodeModuleId} stack node`, error);
|
|
1528
|
+
await this.emit("driverError", { error });
|
|
1529
|
+
};
|
|
1530
|
+
adapter.on("driverError", driverErrorListener);
|
|
1531
|
+
this.listeners.push({ adapter, eventName: "driverError", listener: driverErrorListener });
|
|
1532
|
+
const startAdapter = async () => await adapter.start();
|
|
1533
|
+
forget3(startAdapter());
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
/**
|
|
1537
|
+
* Stops the stack
|
|
1538
|
+
*/
|
|
1539
|
+
stop() {
|
|
1540
|
+
for (const { eventName, listener, adapter } of this.listeners) {
|
|
1541
|
+
adapter.off(eventName, listener);
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1544
|
+
/**
|
|
1545
|
+
* Adds a node to the stack. Idempotent to avoid adding the
|
|
1546
|
+
* same node multiple times
|
|
1547
|
+
* @param node The node to add to the stack
|
|
1548
|
+
* @returns
|
|
1549
|
+
*/
|
|
1550
|
+
async addNodeToStackNode(node) {
|
|
1551
|
+
const stackNode = await this.getStackNode();
|
|
1552
|
+
const existingModule = await stackNode.resolve(node.address, { direction: "down" });
|
|
1553
|
+
if (isModuleInstance(existingModule)) return;
|
|
1554
|
+
await stackNode.register?.(node);
|
|
1555
|
+
await stackNode.attach?.(node.address, true);
|
|
1556
|
+
}
|
|
1557
|
+
assignStack() {
|
|
1558
|
+
const assign = async () => {
|
|
1559
|
+
const stackNode = await this.getStackNode();
|
|
1560
|
+
this._stack = stackNode;
|
|
1561
|
+
};
|
|
1562
|
+
forget3(assign());
|
|
1563
|
+
}
|
|
1564
|
+
async getStackNode() {
|
|
1565
|
+
const mod = assertEx9(await this.context.root.resolve(this.stackNodeModuleId), () => `${this.stackNodeModuleId} not found`);
|
|
1566
|
+
return asAttachableNodeInstance(mod, () => `${this.stackNodeModuleId} not a NodeInstance`);
|
|
1567
|
+
}
|
|
1568
|
+
async handleDriverReady(node) {
|
|
1569
|
+
try {
|
|
1570
|
+
await this.emit("driverReady", { node });
|
|
1571
|
+
await this.addNodeToStackNode(node);
|
|
1572
|
+
await this.updateInitializationState(node);
|
|
1573
|
+
} catch (error) {
|
|
1574
|
+
console.error(`Error updating ${this.stackNodeModuleId} stack node`, node.id, error);
|
|
1575
|
+
await this.emit("stackError", { error });
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
async updateInitializationState(node) {
|
|
1579
|
+
this.initializedNodes.push(node);
|
|
1580
|
+
this.initialized = this.adapterSet.every((n) => n.initialized);
|
|
1581
|
+
if (this.initialized) {
|
|
1582
|
+
await this.emit("initialized", { stack: await this.getStackNode() });
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
};
|
|
1586
|
+
|
|
1587
|
+
// src/stack/Manager.ts
|
|
1588
|
+
var StackManager = class {
|
|
1589
|
+
initializedStacksMap = /* @__PURE__ */ new Map();
|
|
1590
|
+
stackMap;
|
|
1591
|
+
constructor(stackMap) {
|
|
1592
|
+
this.stackMap = stackMap;
|
|
1593
|
+
}
|
|
1594
|
+
get stacks() {
|
|
1595
|
+
return this.initializedStacksMap;
|
|
1596
|
+
}
|
|
1597
|
+
getStack(key) {
|
|
1598
|
+
return this.initializedStacksMap.get(key);
|
|
1599
|
+
}
|
|
1600
|
+
initialize(context) {
|
|
1601
|
+
for (const [key, stackSet] of Object.entries(this.stackMap)) {
|
|
1602
|
+
const { stack: Stack, adapters: stackNodeModuleId } = stackSet;
|
|
1603
|
+
const initializedStack = new Stack(context, stackNodeModuleId);
|
|
1604
|
+
initializedStack.initialize();
|
|
1605
|
+
this.stacks.set(key, initializedStack);
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
stop() {
|
|
1609
|
+
for (const stack of this.stacks.values()) {
|
|
1610
|
+
stack.stop();
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
};
|
|
1614
|
+
|
|
1615
|
+
// src/adapters/OsPubSubBridgeNetwork.ts
|
|
1616
|
+
import { ExposedNodeOuterNodeName, PubSubBridgeNodeNodeName } from "@xyo-network/os-model";
|
|
1617
|
+
|
|
1618
|
+
// src/adapter/Base.ts
|
|
1619
|
+
import { assertEx as assertEx10 } from "@xylabs/assert";
|
|
1620
|
+
import { forget as forget4 } from "@xylabs/forget";
|
|
1621
|
+
import { asArchivistInstance as asArchivistInstance4 } from "@xyo-network/archivist";
|
|
1622
|
+
import { BaseEmitter as BaseEmitter4 } from "@xyo-network/module-abstract";
|
|
1623
|
+
import { asAttachableNodeInstance as asAttachableNodeInstance2 } from "@xyo-network/node-model";
|
|
1624
|
+
import { v4 as uuid3 } from "uuid";
|
|
1625
|
+
var NodeAdapterBase = class extends BaseEmitter4 {
|
|
1626
|
+
constructor(kernel, driverName) {
|
|
1627
|
+
super({});
|
|
1628
|
+
this.kernel = kernel;
|
|
1629
|
+
this.driverName = driverName;
|
|
1630
|
+
this._id = uuid3();
|
|
1631
|
+
}
|
|
1632
|
+
initialized = false;
|
|
1633
|
+
_id = "";
|
|
1634
|
+
get id() {
|
|
1635
|
+
return assertEx10(this._id, () => "NodeAdapterBase id not initialized");
|
|
1636
|
+
}
|
|
1637
|
+
async start() {
|
|
1638
|
+
try {
|
|
1639
|
+
const moduleAttachedInsertListener = ({ payloads }) => {
|
|
1640
|
+
const moduleAttachedEvent = this.findModuleEventPayloads(payloads);
|
|
1641
|
+
for (const payload of moduleAttachedEvent) {
|
|
1642
|
+
if (payload.name === this.driverName) {
|
|
1643
|
+
this.validateAndReturnDriver();
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
};
|
|
1647
|
+
const eventsArchivist = await this.getEventsArchivist();
|
|
1648
|
+
const weakRefInsertListener = new WeakRef(moduleAttachedInsertListener);
|
|
1649
|
+
const insertListenerRef = weakRefInsertListener.deref();
|
|
1650
|
+
if (insertListenerRef) eventsArchivist.on("inserted", insertListenerRef);
|
|
1651
|
+
const node = this.validateAndReturnDriver();
|
|
1652
|
+
if (node) return node;
|
|
1653
|
+
this.initialized = false;
|
|
1654
|
+
} catch (error) {
|
|
1655
|
+
await this.emit("driverError", { error });
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
findModuleEventPayloads(payloads) {
|
|
1659
|
+
return payloads.filter((payload) => {
|
|
1660
|
+
const payloadToTest = payload;
|
|
1661
|
+
return payloadToTest.event === "driverReady" && payloadToTest.schema === "network.xyo.kernel.module.event";
|
|
1662
|
+
});
|
|
1663
|
+
}
|
|
1664
|
+
async getEventsArchivist() {
|
|
1665
|
+
const mod = assertEx10(await this.kernel.node.resolve("KernelNode:ModuleEvents"), () => "KernelNode:ModuleEvents not found");
|
|
1666
|
+
return asArchivistInstance4(mod, () => `${mod.id} is not an archivist`);
|
|
1667
|
+
}
|
|
1668
|
+
validateAndReturnDriver() {
|
|
1669
|
+
const mod = this.kernel.initialized[this.driverName];
|
|
1670
|
+
if (mod) {
|
|
1671
|
+
const node = asAttachableNodeInstance2(mod, () => `${mod.id} is not a node`);
|
|
1672
|
+
this.initialized = true;
|
|
1673
|
+
const emit = async () => await this.emit("driverReady", { node });
|
|
1674
|
+
forget4(emit());
|
|
1675
|
+
return node;
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
};
|
|
1679
|
+
|
|
1680
|
+
// src/adapter/Network.ts
|
|
1681
|
+
var NetworkAdapter = class extends NodeAdapterBase {
|
|
1682
|
+
_connection;
|
|
1683
|
+
constructor(kernel, driverName) {
|
|
1684
|
+
super(kernel, driverName);
|
|
1685
|
+
this.on("driverReady", ({ node }) => {
|
|
1686
|
+
this._connection = node;
|
|
1687
|
+
});
|
|
1688
|
+
}
|
|
1689
|
+
get connection() {
|
|
1690
|
+
return this._connection;
|
|
1691
|
+
}
|
|
1692
|
+
get hasConnection() {
|
|
1693
|
+
return !!this.connection;
|
|
1694
|
+
}
|
|
1695
|
+
removeConnection() {
|
|
1696
|
+
this._connection = void 0;
|
|
1697
|
+
}
|
|
1698
|
+
};
|
|
1699
|
+
|
|
1700
|
+
// src/adapter/Settings.ts
|
|
1701
|
+
var SettingsAdapter = class extends NodeAdapterBase {
|
|
1702
|
+
_settings = void 0;
|
|
1703
|
+
constructor(kernel, driverName) {
|
|
1704
|
+
super(kernel, driverName);
|
|
1705
|
+
this.on("driverReady", ({ node }) => {
|
|
1706
|
+
this._settings = node;
|
|
1707
|
+
});
|
|
1708
|
+
}
|
|
1709
|
+
get hasSettings() {
|
|
1710
|
+
return !!this.settings;
|
|
1711
|
+
}
|
|
1712
|
+
get settings() {
|
|
1713
|
+
return this._settings;
|
|
1714
|
+
}
|
|
1715
|
+
removeSettings() {
|
|
1716
|
+
this._settings = void 0;
|
|
1717
|
+
}
|
|
1718
|
+
};
|
|
1719
|
+
|
|
1720
|
+
// src/adapters/OsPubSubBridgeNetwork.ts
|
|
1721
|
+
var OsPubSubBridgeNetworkAdapters = (kernel) => [
|
|
1722
|
+
new NetworkAdapter(kernel, PubSubBridgeNodeNodeName),
|
|
1723
|
+
new NetworkAdapter(kernel, ExposedNodeOuterNodeName)
|
|
1724
|
+
];
|
|
1725
|
+
|
|
1726
|
+
// src/adapters/OsSettings.ts
|
|
1727
|
+
import { OsSettingsNodeName } from "@xyo-network/os-model";
|
|
1728
|
+
var OsSettingsAdapters = (kernel) => [new SettingsAdapter(kernel, OsSettingsNodeName)];
|
|
1729
|
+
|
|
1730
|
+
// src/adapters/OsXyoPublicNetwork.ts
|
|
1731
|
+
import { XyoPublicNodeName } from "@xyo-network/os-model";
|
|
1732
|
+
var OsXyoPublicNetworkAdapters = (kernel) => [
|
|
1733
|
+
new NetworkAdapter(kernel, XyoPublicNodeName)
|
|
1734
|
+
];
|
|
1735
|
+
|
|
1736
|
+
// src/stack/OsPubSubNetworkStack.ts
|
|
1737
|
+
import { assertEx as assertEx11 } from "@xylabs/assert";
|
|
1738
|
+
import { asAttachableNodeInstance as asAttachableNodeInstance3, asNodeInstance } from "@xyo-network/node-model";
|
|
1739
|
+
import { ExposedNodeOuterNodeName as ExposedNodeOuterNodeName2, PubSubBridgeNodeNodeName as PubSubBridgeNodeNodeName2 } from "@xyo-network/os-model";
|
|
1740
|
+
var OsPubSubNetworkStack = class extends StackBase {
|
|
1741
|
+
_exposedNode;
|
|
1742
|
+
_exposedNodeOuter;
|
|
1743
|
+
busConnection = osPubSubNetworkReadyConnection();
|
|
1744
|
+
constructor(context, adapters) {
|
|
1745
|
+
super(context, adapters, "OsPubSubNetworkStackNode");
|
|
1746
|
+
}
|
|
1747
|
+
get exposedNode() {
|
|
1748
|
+
return this._exposedNode;
|
|
1749
|
+
}
|
|
1750
|
+
get exposedNodeOuter() {
|
|
1751
|
+
return this._exposedNodeOuter;
|
|
1752
|
+
}
|
|
1753
|
+
initialize() {
|
|
1754
|
+
super.initialize();
|
|
1755
|
+
this.context.eventBus.addConnection(this.busConnection);
|
|
1756
|
+
this.on("initialized", () => this.busConnection.emit(OsPubSubNetworkReadyEvent, {}));
|
|
1757
|
+
super.on("driverReady", async ({ node }) => {
|
|
1758
|
+
if (node.id === PubSubBridgeNodeNodeName2) {
|
|
1759
|
+
const httpBridge = assertEx11(await node.resolve("NsHttpBridge"), () => "HttpBridge not found");
|
|
1760
|
+
const xnsNode = asNodeInstance(await httpBridge.resolve("XNS"));
|
|
1761
|
+
if (xnsNode) {
|
|
1762
|
+
const initializedXns = await initializeXns(xnsNode);
|
|
1763
|
+
console.log(`Xns: ${initializedXns}`);
|
|
1764
|
+
} else {
|
|
1765
|
+
console.log("Xns: Failed to contact Xns Node");
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
});
|
|
1769
|
+
super.on("driverReady", async ({ node }) => {
|
|
1770
|
+
if (node.id === ExposedNodeOuterNodeName2) {
|
|
1771
|
+
this._exposedNodeOuter = node;
|
|
1772
|
+
const mod = assertEx11(await node.resolve("ExposedNode"), () => "ExposedNode not found");
|
|
1773
|
+
const exposedNode = asAttachableNodeInstance3(mod, () => "ExposedNode is not a node");
|
|
1774
|
+
this._exposedNode = exposedNode;
|
|
1775
|
+
}
|
|
1776
|
+
});
|
|
1777
|
+
super.on("driverError", (error) => console.error("Unable to assign exposed node(s) to context", error));
|
|
1778
|
+
}
|
|
1779
|
+
stop() {
|
|
1780
|
+
super.stop();
|
|
1781
|
+
this._exposedNode = void 0;
|
|
1782
|
+
this._exposedNodeOuter = void 0;
|
|
1783
|
+
if (this.busConnection._id)
|
|
1784
|
+
this.context.eventBus.removeConnection(this.busConnection.id);
|
|
1785
|
+
}
|
|
1786
|
+
};
|
|
1787
|
+
|
|
1788
|
+
// src/stack/OsSettingsStack.ts
|
|
1789
|
+
var OsSettingsStack = class extends StackBase {
|
|
1790
|
+
busConnection = osSettingsReadyConnection();
|
|
1791
|
+
constructor(context, adapters) {
|
|
1792
|
+
super(context, adapters, "OsSettingsStackNode");
|
|
1793
|
+
}
|
|
1794
|
+
initialize() {
|
|
1795
|
+
super.initialize();
|
|
1796
|
+
this.context.eventBus.addConnection(this.busConnection);
|
|
1797
|
+
this.on("initialized", () => this.busConnection.emit(OsSettingsReadyEvent, {}));
|
|
1798
|
+
}
|
|
1799
|
+
stop() {
|
|
1800
|
+
super.stop();
|
|
1801
|
+
}
|
|
1802
|
+
};
|
|
1803
|
+
|
|
1804
|
+
// src/stack/XyoPublicNetworkStack.ts
|
|
1805
|
+
import { XyoPublicNodeName as XyoPublicNodeName2 } from "@xyo-network/os-model";
|
|
1806
|
+
var XyoPublicNetworkStack = class extends StackBase {
|
|
1807
|
+
busConnection = osXyoPublicNetworkReadyConnection();
|
|
1808
|
+
constructor(context, adapters) {
|
|
1809
|
+
super(context, adapters, "OsXyoPublicNetworkStackNode");
|
|
1810
|
+
}
|
|
1811
|
+
initialize() {
|
|
1812
|
+
super.initialize();
|
|
1813
|
+
this.context.eventBus.addConnection(this.busConnection);
|
|
1814
|
+
this.on("initialized", () => this.busConnection.emit(OsXyoPublicNetworkReadyEvent, {}));
|
|
1815
|
+
this.on("driverReady", ({ node }) => {
|
|
1816
|
+
if (node.id === XyoPublicNodeName2) {
|
|
1817
|
+
console.log("XyoPublicNode ready");
|
|
1818
|
+
}
|
|
1819
|
+
});
|
|
1820
|
+
this.on("driverError", (error) => console.error("Error loading driver", error));
|
|
1821
|
+
}
|
|
1822
|
+
stop() {
|
|
1823
|
+
super.stop();
|
|
1824
|
+
}
|
|
1825
|
+
};
|
|
1826
|
+
|
|
1827
|
+
// src/stack/Map.ts
|
|
1828
|
+
var OsStackMap = {
|
|
1829
|
+
osPubSubNetworkStack: {
|
|
1830
|
+
stack: OsPubSubNetworkStack,
|
|
1831
|
+
adapters: OsPubSubBridgeNetworkAdapters
|
|
1832
|
+
},
|
|
1833
|
+
xyoPublicNetworkStack: {
|
|
1834
|
+
stack: XyoPublicNetworkStack,
|
|
1835
|
+
adapters: OsXyoPublicNetworkAdapters
|
|
1836
|
+
},
|
|
1837
|
+
osSettingsStack: {
|
|
1838
|
+
stack: OsSettingsStack,
|
|
1839
|
+
adapters: OsSettingsAdapters
|
|
1840
|
+
}
|
|
1841
|
+
};
|
|
1842
|
+
|
|
1843
|
+
// src/XyOs.ts
|
|
1844
|
+
var XyOs = class _XyOs extends XyOsContextBase {
|
|
1845
|
+
_exposedNode;
|
|
1846
|
+
_exposedNodeOuter;
|
|
1847
|
+
_locator;
|
|
1848
|
+
dappRegistrationService;
|
|
1849
|
+
stackManager;
|
|
1850
|
+
constructor(params, dappsConfiguration, developmentMode) {
|
|
1851
|
+
super(params);
|
|
1852
|
+
this._locator = params.locator ?? new ModuleFactoryLocator5();
|
|
1853
|
+
this.stackManager = new StackManager(OsStackMap);
|
|
1854
|
+
this.dappRegistrationService = new DappRegistrationService(this, dappsConfiguration, this.locator, developmentMode);
|
|
1855
|
+
}
|
|
1856
|
+
static get monitoring() {
|
|
1857
|
+
return;
|
|
1858
|
+
}
|
|
1859
|
+
static get xyOsGlobal() {
|
|
1860
|
+
return getXyOsGlobal();
|
|
1861
|
+
}
|
|
1862
|
+
get bios() {
|
|
1863
|
+
return assertEx12(this.params.kernel?.bios, () => "Missing bios");
|
|
1864
|
+
}
|
|
1865
|
+
get exposedNode() {
|
|
1866
|
+
return this.stackManager.getStack("osPubSubNetworkStack")?.exposedNode;
|
|
1867
|
+
}
|
|
1868
|
+
get exposedNodeOuter() {
|
|
1869
|
+
return this.stackManager.getStack("osPubSubNetworkStack")?.exposedNodeOuter;
|
|
1870
|
+
}
|
|
1871
|
+
get kernel() {
|
|
1872
|
+
return assertEx12(this.params.kernel, () => "Missing kernel");
|
|
1873
|
+
}
|
|
1874
|
+
get locator() {
|
|
1875
|
+
return this._locator;
|
|
1876
|
+
}
|
|
1877
|
+
static async monitor(fn, eventConfig) {
|
|
1878
|
+
const { name, additionalProperties } = eventConfig;
|
|
1879
|
+
const monitor = this.monitoring;
|
|
1880
|
+
monitor?.startTimer(name);
|
|
1881
|
+
const result = await fn();
|
|
1882
|
+
monitor?.stopTimer({ additionalProperties, name });
|
|
1883
|
+
return result;
|
|
1884
|
+
}
|
|
1885
|
+
async boot(wallet, locator = new ModuleFactoryLocator5()) {
|
|
1886
|
+
console.warn("XyOs:boot", wallet.address);
|
|
1887
|
+
const fullLocator = this._locator.merge(locator);
|
|
1888
|
+
return await this._bootMutex.runExclusive(async () => {
|
|
1889
|
+
assertEx12(this._root === void 0, () => "XyOs already booted");
|
|
1890
|
+
const [root] = await _XyOs.monitor(async () => await loadOsNode(wallet, fullLocator), { name: "Load XyOs" });
|
|
1891
|
+
this._root = root;
|
|
1892
|
+
this.stackManager.initialize(this);
|
|
1893
|
+
});
|
|
1894
|
+
}
|
|
1895
|
+
buildDapp(manifest, dappId = NameTransforms.slug(manifest.nodes[0]?.config.name), name = NameTransforms.moduleName(manifest.nodes[0]?.config.name)) {
|
|
1896
|
+
return this.dappRegistrationService.buildDapp(manifest, dappId, name);
|
|
1897
|
+
}
|
|
1898
|
+
dappByName(dappId) {
|
|
1899
|
+
return this.dappRegistrationService.builtDapps[dappId];
|
|
1900
|
+
}
|
|
1901
|
+
getDappParams(dappId) {
|
|
1902
|
+
return this.dappRegistrationService.getParamsForRegisteredDapp(dappId);
|
|
1903
|
+
}
|
|
1904
|
+
getStack(stackName) {
|
|
1905
|
+
return assertEx12(this.stackManager.getStack(stackName), () => `Stack not found: ${stackName}`);
|
|
1906
|
+
}
|
|
1907
|
+
async monitor(fn, eventConfig) {
|
|
1908
|
+
return await _XyOs.monitor(fn, eventConfig);
|
|
1909
|
+
}
|
|
1910
|
+
shutdown() {
|
|
1911
|
+
assertEx12(this._root !== void 0, () => "XyOs not booted");
|
|
1912
|
+
this._root = void 0;
|
|
1913
|
+
this.stackManager.stop();
|
|
1914
|
+
}
|
|
1915
|
+
};
|
|
1916
|
+
|
|
1917
|
+
// src/classes/node/createDappContext.ts
|
|
1918
|
+
import { HDWallet as HDWallet5 } from "@xyo-network/account";
|
|
1919
|
+
import { GenericPayloadDiviner } from "@xyo-network/diviner-payload-generic";
|
|
1920
|
+
import { ModuleFactoryLocator as ModuleFactoryLocator6 } from "@xyo-network/module-factory-locator";
|
|
1921
|
+
|
|
1922
|
+
// src/classes/node/Creator.ts
|
|
1923
|
+
import { assertEx as assertEx18 } from "@xylabs/assert";
|
|
1924
|
+
import { asArchivistInstance as asArchivistInstance6 } from "@xyo-network/archivist-model";
|
|
1925
|
+
import { PackageManifestPayloadSchema as PackageManifestPayloadSchema2 } from "@xyo-network/manifest";
|
|
1926
|
+
|
|
1927
|
+
// src/classes/node/DefaultPayloads/DefaultPayloads.ts
|
|
1928
|
+
import { PayloadBuilder as PayloadBuilder6 } from "@xyo-network/payload-builder";
|
|
1929
|
+
|
|
1930
|
+
// src/classes/lib/Insertable.ts
|
|
1931
|
+
var createInsertable = (insertable, ...params) => new insertable(...params);
|
|
1932
|
+
|
|
1933
|
+
// src/classes/node/DefaultPayloads/DappAccessPayloads.ts
|
|
1934
|
+
var DappAccessPayloads = class {
|
|
1935
|
+
constructor(context, _dappName) {
|
|
1936
|
+
this.context = context;
|
|
1937
|
+
}
|
|
1938
|
+
async payloads() {
|
|
1939
|
+
const osCaller = new OsCallerBase(this.context);
|
|
1940
|
+
const archivist = await osCaller.getRegisteredDappInterfacesArchivist();
|
|
1941
|
+
return await archivist.all();
|
|
1942
|
+
}
|
|
1943
|
+
};
|
|
1944
|
+
|
|
1945
|
+
// src/classes/node/DefaultPayloads/NodeInfoPayload.ts
|
|
1946
|
+
import { NodeOsInfoSchema } from "@xyo-network/os-model";
|
|
1947
|
+
var NodeInfoPayload = class {
|
|
1948
|
+
constructor(_context, _dappName) {
|
|
1949
|
+
this._context = _context;
|
|
1950
|
+
}
|
|
1951
|
+
async payloads() {
|
|
1952
|
+
const exposedNode = this._context.exposedNode;
|
|
1953
|
+
if (exposedNode) {
|
|
1954
|
+
console.debug("[DEBUG]", "Found Exposed Node Address", exposedNode?.address);
|
|
1955
|
+
} else {
|
|
1956
|
+
console.error("exposedNode not found");
|
|
1957
|
+
}
|
|
1958
|
+
await Promise.resolve();
|
|
1959
|
+
const nodeOsInfo = {
|
|
1960
|
+
publicAddress: exposedNode?.address ?? "",
|
|
1961
|
+
schema: NodeOsInfoSchema
|
|
1962
|
+
};
|
|
1963
|
+
return [nodeOsInfo];
|
|
1964
|
+
}
|
|
1965
|
+
};
|
|
1966
|
+
|
|
1967
|
+
// src/classes/node/DefaultPayloads/SigningKeyPayloads.ts
|
|
1968
|
+
import { assertEx as assertEx17 } from "@xylabs/assert";
|
|
1969
|
+
import { hexFrom } from "@xylabs/hex";
|
|
1970
|
+
import { HDWallet as HDWallet4 } from "@xyo-network/account";
|
|
1971
|
+
import { mnemonicArrayToString, SigningKeySchema } from "@xyo-network/os-model";
|
|
1972
|
+
import { isAnyPayload as isAnyPayload2 } from "@xyo-network/payload-model";
|
|
1973
|
+
|
|
1974
|
+
// src/classes/settings/badge/Caller.ts
|
|
1975
|
+
import { OsBadgeSchema as OsBadgeSchema2 } from "@xyo-network/os-model";
|
|
1976
|
+
|
|
1977
|
+
// src/classes/settings/CallerBase.ts
|
|
1978
|
+
import { assertEx as assertEx13 } from "@xylabs/assert";
|
|
1979
|
+
import { asArchivistInstance as asArchivistInstance5 } from "@xyo-network/archivist-model";
|
|
1980
|
+
import { asDivinerInstance as asDivinerInstance4 } from "@xyo-network/diviner-model";
|
|
1981
|
+
var OsSettingsArchivistModuleName = "OsSettingsNode:OsSettingsArchivist";
|
|
1982
|
+
var OsSettingsArchivistPayloadDivinerModuleName = "OsSettingsNode:OsSettingsArchivistPayloadDiviner";
|
|
1983
|
+
var OsSettingsCallerBase = class {
|
|
1984
|
+
constructor(context) {
|
|
1985
|
+
this.context = context;
|
|
1986
|
+
}
|
|
1987
|
+
async getOsSettingsArchivist() {
|
|
1988
|
+
const mod = assertEx13(await this.context.root.resolve(OsSettingsArchivistModuleName), () => `${OsSettingsArchivistModuleName} not found`);
|
|
1989
|
+
return asArchivistInstance5(mod, () => `${OsSettingsArchivistModuleName} is not an archivist`);
|
|
1990
|
+
}
|
|
1991
|
+
async getOsSettingsPayloadDiviner() {
|
|
1992
|
+
const mod = assertEx13(
|
|
1993
|
+
await this.context.root.resolve(OsSettingsArchivistPayloadDivinerModuleName),
|
|
1994
|
+
() => `${OsSettingsArchivistPayloadDivinerModuleName} not found`
|
|
1995
|
+
);
|
|
1996
|
+
return asDivinerInstance4(mod, () => `${OsSettingsArchivistPayloadDivinerModuleName} is not a diviner`);
|
|
1997
|
+
}
|
|
1998
|
+
};
|
|
1999
|
+
|
|
2000
|
+
// src/classes/settings/badge/Queries.ts
|
|
2001
|
+
import { PayloadDivinerQuerySchema as PayloadDivinerQuerySchema4 } from "@xyo-network/diviner-payload-model";
|
|
2002
|
+
import { OsBadgeSchema } from "@xyo-network/os-model";
|
|
2003
|
+
var OsBadgeQueries = {
|
|
2004
|
+
getBadge: async (achievement, diviner) => {
|
|
2005
|
+
const query = {
|
|
2006
|
+
achievement,
|
|
2007
|
+
limit: 1,
|
|
2008
|
+
order: "desc",
|
|
2009
|
+
schema: PayloadDivinerQuerySchema4,
|
|
2010
|
+
schemas: [OsBadgeSchema]
|
|
2011
|
+
};
|
|
2012
|
+
return await diviner.divine([query]);
|
|
2013
|
+
}
|
|
2014
|
+
};
|
|
2015
|
+
|
|
2016
|
+
// src/classes/settings/badge/Caller.ts
|
|
2017
|
+
var OsBadgeCaller = class extends OsSettingsCallerBase {
|
|
2018
|
+
/**
|
|
2019
|
+
* @param {ValidOsAchievements} achievement
|
|
2020
|
+
*/
|
|
2021
|
+
async getBadge(achievement) {
|
|
2022
|
+
const diviner = await this.getOsSettingsPayloadDiviner();
|
|
2023
|
+
const results = await OsBadgeQueries.getBadge(achievement, diviner);
|
|
2024
|
+
return results[0] ?? null;
|
|
2025
|
+
}
|
|
2026
|
+
/**
|
|
2027
|
+
* @param {ValidOsAchievements} achievement
|
|
2028
|
+
*/
|
|
2029
|
+
async saveBadge(achievement) {
|
|
2030
|
+
const badge = {
|
|
2031
|
+
achievement,
|
|
2032
|
+
schema: OsBadgeSchema2,
|
|
2033
|
+
timestamp: Date.now()
|
|
2034
|
+
};
|
|
2035
|
+
const archivist = await this.getOsSettingsArchivist();
|
|
2036
|
+
return await archivist.insert([badge]);
|
|
2037
|
+
}
|
|
2038
|
+
};
|
|
2039
|
+
|
|
2040
|
+
// src/classes/settings/badge/Resource.ts
|
|
2041
|
+
import { assertEx as assertEx14 } from "@xylabs/assert";
|
|
2042
|
+
import { isBadgeWithMeta } from "@xyo-network/os-model";
|
|
2043
|
+
var OsBadgeResource = class extends OsSettingsCallerBase {
|
|
2044
|
+
static views = ["savedSeedPhrase"];
|
|
2045
|
+
_savedSeedPhrase;
|
|
2046
|
+
constructor(xyOsContext) {
|
|
2047
|
+
super(xyOsContext);
|
|
2048
|
+
}
|
|
2049
|
+
get savedSeedPhrase() {
|
|
2050
|
+
return assertEx14(this._savedSeedPhrase, () => "Saved seed phrase listener not set");
|
|
2051
|
+
}
|
|
2052
|
+
get subscriptions() {
|
|
2053
|
+
return {
|
|
2054
|
+
savedSeedPhrase: (cb) => this.savedSeedPhrase.subscribe(cb)
|
|
2055
|
+
};
|
|
2056
|
+
}
|
|
2057
|
+
get views() {
|
|
2058
|
+
return {
|
|
2059
|
+
savedSeedPhrase: () => this.savedSeedPhrase.latest
|
|
2060
|
+
};
|
|
2061
|
+
}
|
|
2062
|
+
cleanupListeners() {
|
|
2063
|
+
this.savedSeedPhrase.cleanupListeners();
|
|
2064
|
+
}
|
|
2065
|
+
/**
|
|
2066
|
+
* start listeners
|
|
2067
|
+
**/
|
|
2068
|
+
async start() {
|
|
2069
|
+
const diviner = await this.getOsSettingsPayloadDiviner();
|
|
2070
|
+
const archivist = await this.getOsSettingsArchivist();
|
|
2071
|
+
this._savedSeedPhrase = await PayloadStore.create({
|
|
2072
|
+
archivist,
|
|
2073
|
+
getLatest: async () => await OsBadgeQueries.getBadge("savedSeedPhrase", diviner),
|
|
2074
|
+
idFunction: isBadgeWithMeta
|
|
2075
|
+
});
|
|
2076
|
+
}
|
|
2077
|
+
};
|
|
2078
|
+
|
|
2079
|
+
// src/classes/settings/Caller.ts
|
|
2080
|
+
import { assertEx as assertEx15 } from "@xylabs/assert";
|
|
2081
|
+
import { isEqual } from "@xylabs/lodash";
|
|
2082
|
+
|
|
2083
|
+
// src/utils/buildWalletSeedPhrasePayload.ts
|
|
2084
|
+
import { WalletSeedPhraseSchema } from "@xyo-network/os-model";
|
|
2085
|
+
import { PayloadBuilder as PayloadBuilder5 } from "@xyo-network/payload-builder";
|
|
2086
|
+
var language = "english";
|
|
2087
|
+
var standard = "bip-39";
|
|
2088
|
+
var userWalletLabel = "userWallet";
|
|
2089
|
+
var buildWalletSeedPhrasePayload = async (mnemonic, label) => {
|
|
2090
|
+
const fields = { mnemonic: { language, mnemonic: mnemonic.split(" "), standard } };
|
|
2091
|
+
const meta = { label, timestamp: Date.now() };
|
|
2092
|
+
const builder = new PayloadBuilder5({ fields, meta, schema: WalletSeedPhraseSchema });
|
|
2093
|
+
const payload = await builder.build();
|
|
2094
|
+
const rootHash = await PayloadBuilder5.hash(payload);
|
|
2095
|
+
return { payload, rootHash };
|
|
2096
|
+
};
|
|
2097
|
+
var buildUserWalletSeedPhrasePayload = (mnemonic) => {
|
|
2098
|
+
return buildWalletSeedPhrasePayload(mnemonic, userWalletLabel);
|
|
2099
|
+
};
|
|
2100
|
+
|
|
2101
|
+
// src/utils/getApiDomain.ts
|
|
2102
|
+
var getApiDomain = (scope, apiDomain) => {
|
|
2103
|
+
if (apiDomain !== void 0) {
|
|
2104
|
+
return apiDomain;
|
|
2105
|
+
}
|
|
2106
|
+
if (scope === "localhost") {
|
|
2107
|
+
return "http://localhost:8080";
|
|
2108
|
+
}
|
|
2109
|
+
if (scope === "beta") {
|
|
2110
|
+
return "https://beta.archivist.xyo.network";
|
|
2111
|
+
}
|
|
2112
|
+
return "https://archivist.xyo.network";
|
|
2113
|
+
};
|
|
2114
|
+
|
|
2115
|
+
// src/utils/renameObjKey.ts
|
|
2116
|
+
var renameObjKey = (oldObj, oldKey, newKey) => {
|
|
2117
|
+
const oldObjKeys = Object.keys(oldObj);
|
|
2118
|
+
const newObj = oldObjKeys.reduce(
|
|
2119
|
+
(acc, val) => {
|
|
2120
|
+
if (val === oldKey) {
|
|
2121
|
+
acc[newKey] = oldObj[oldKey];
|
|
2122
|
+
} else {
|
|
2123
|
+
acc[val] = oldObj[val];
|
|
2124
|
+
}
|
|
2125
|
+
return acc;
|
|
2126
|
+
},
|
|
2127
|
+
{}
|
|
2128
|
+
);
|
|
2129
|
+
return newObj;
|
|
2130
|
+
};
|
|
2131
|
+
|
|
2132
|
+
// src/classes/settings/SettingsQueries.ts
|
|
2133
|
+
import { PayloadDivinerQuerySchema as PayloadDivinerQuerySchema5 } from "@xyo-network/diviner-payload-model";
|
|
2134
|
+
import { NodeBackgroundSchema, WalletSeedPhraseSchema as WalletSeedPhraseSchema2 } from "@xyo-network/os-model";
|
|
2135
|
+
var NO_RESULTS2 = [];
|
|
2136
|
+
var OsSettingsQueries = {
|
|
2137
|
+
async getLatestUserWallet(diviner) {
|
|
2138
|
+
const query = {
|
|
2139
|
+
limit: 1,
|
|
2140
|
+
order: "desc",
|
|
2141
|
+
schema: PayloadDivinerQuerySchema5,
|
|
2142
|
+
schemas: [WalletSeedPhraseSchema2]
|
|
2143
|
+
};
|
|
2144
|
+
const results = await diviner.divine([query]);
|
|
2145
|
+
const wallet = results.find((payload) => payload.$meta.label === "userWallet");
|
|
2146
|
+
return wallet ? [wallet] : NO_RESULTS2;
|
|
2147
|
+
},
|
|
2148
|
+
async getNodeBackground(diviner) {
|
|
2149
|
+
const query = {
|
|
2150
|
+
limit: 1,
|
|
2151
|
+
order: "desc",
|
|
2152
|
+
schema: PayloadDivinerQuerySchema5,
|
|
2153
|
+
schemas: [NodeBackgroundSchema]
|
|
2154
|
+
};
|
|
2155
|
+
return await diviner.divine([query]);
|
|
2156
|
+
},
|
|
2157
|
+
async getWallets(diviner) {
|
|
2158
|
+
const query = {
|
|
2159
|
+
order: "desc",
|
|
2160
|
+
schema: PayloadDivinerQuerySchema5,
|
|
2161
|
+
schemas: [WalletSeedPhraseSchema2]
|
|
2162
|
+
};
|
|
2163
|
+
return await diviner.divine([query]);
|
|
2164
|
+
}
|
|
2165
|
+
};
|
|
2166
|
+
|
|
2167
|
+
// src/classes/settings/Caller.ts
|
|
2168
|
+
var OsSettingsCaller = class extends OsSettingsCallerBase {
|
|
2169
|
+
constructor(context) {
|
|
2170
|
+
super(context);
|
|
2171
|
+
}
|
|
2172
|
+
/**
|
|
2173
|
+
* Add a node background to the settings
|
|
2174
|
+
* @param {WithMeta<NodeBackground>} payload
|
|
2175
|
+
*/
|
|
2176
|
+
async addNodeBackground(payload) {
|
|
2177
|
+
const archivist = await this.getOsSettingsArchivist();
|
|
2178
|
+
await archivist.insert([payload]);
|
|
2179
|
+
}
|
|
2180
|
+
/**
|
|
2181
|
+
* Add a wallet seed phrase to the settings
|
|
2182
|
+
* @param {WalletSeedPhrase} payload
|
|
2183
|
+
*/
|
|
2184
|
+
async addWalletSeedPhrase(payload) {
|
|
2185
|
+
const archivist = await this.getOsSettingsArchivist();
|
|
2186
|
+
await archivist.insert([payload]);
|
|
2187
|
+
const phrase = payload.mnemonic.mnemonic.join(" ");
|
|
2188
|
+
await this.context.kernel?.bios?.seedPhraseStore.set("user", phrase);
|
|
2189
|
+
}
|
|
2190
|
+
/**
|
|
2191
|
+
* Get the latest node background
|
|
2192
|
+
*/
|
|
2193
|
+
async getLatestNodeBackground() {
|
|
2194
|
+
const diviner = await this.getOsSettingsPayloadDiviner();
|
|
2195
|
+
const [result] = await OsSettingsQueries.getNodeBackground(diviner);
|
|
2196
|
+
return result ?? null;
|
|
2197
|
+
}
|
|
2198
|
+
async getLatestUserWallet() {
|
|
2199
|
+
const diviner = await this.getOsSettingsPayloadDiviner();
|
|
2200
|
+
const storedSeedPhrasePayload = (await OsSettingsQueries.getLatestUserWallet(diviner)).pop();
|
|
2201
|
+
const biosWalletSeedPhrase = assertEx15(await this.context.kernel?.bios?.seedPhraseStore.get("user"), () => "No user seed phrase found from BIOS");
|
|
2202
|
+
const biosWalletSeedPhraseParts = biosWalletSeedPhrase.split(" ");
|
|
2203
|
+
if (!storedSeedPhrasePayload?.mnemonic?.mnemonic) {
|
|
2204
|
+
const { payload: biosSeedPhrasePayload } = await buildUserWalletSeedPhrasePayload(biosWalletSeedPhrase);
|
|
2205
|
+
await this.addWalletSeedPhrase(biosSeedPhrasePayload);
|
|
2206
|
+
return biosSeedPhrasePayload;
|
|
2207
|
+
} else if (!isEqual(storedSeedPhrasePayload.mnemonic.mnemonic, biosWalletSeedPhraseParts)) {
|
|
2208
|
+
const updatedBiosSeedPhrase = storedSeedPhrasePayload.mnemonic.mnemonic.join(" ");
|
|
2209
|
+
await this.context.kernel?.bios?.seedPhraseStore.set("user", updatedBiosSeedPhrase);
|
|
2210
|
+
}
|
|
2211
|
+
return storedSeedPhrasePayload;
|
|
2212
|
+
}
|
|
2213
|
+
};
|
|
2214
|
+
|
|
2215
|
+
// src/classes/settings/Resource.ts
|
|
2216
|
+
import { assertEx as assertEx16 } from "@xylabs/assert";
|
|
2217
|
+
import { isNodeBackground, isWalletSeedPhrase } from "@xyo-network/os-model";
|
|
2218
|
+
var OsSettingsResource = class extends OsSettingsCallerBase {
|
|
2219
|
+
_latestUserWalletListener;
|
|
2220
|
+
_nodeBackgroundListener;
|
|
2221
|
+
_walletsListener;
|
|
2222
|
+
constructor(context) {
|
|
2223
|
+
super(context);
|
|
2224
|
+
}
|
|
2225
|
+
get latestUserWalletListener() {
|
|
2226
|
+
return assertEx16(this._latestUserWalletListener, () => "Latest user wallet listener not set");
|
|
2227
|
+
}
|
|
2228
|
+
get nodeBackgroundListener() {
|
|
2229
|
+
return assertEx16(this._nodeBackgroundListener, () => "Node background listener not set");
|
|
2230
|
+
}
|
|
2231
|
+
get subscriptions() {
|
|
2232
|
+
return {
|
|
2233
|
+
latestUserWallet: (cb) => this.walletsListener.subscribe(cb),
|
|
2234
|
+
nodeBackground: (cb) => this.nodeBackgroundListener.subscribe(cb),
|
|
2235
|
+
wallets: (cb) => this.walletsListener.subscribe(cb)
|
|
2236
|
+
};
|
|
2237
|
+
}
|
|
2238
|
+
get views() {
|
|
2239
|
+
return {
|
|
2240
|
+
latestUserWallet: () => this.latestUserWalletListener.latest,
|
|
2241
|
+
nodeBackground: () => this.nodeBackgroundListener.latest,
|
|
2242
|
+
wallets: () => this.walletsListener.latest
|
|
2243
|
+
};
|
|
2244
|
+
}
|
|
2245
|
+
get walletsListener() {
|
|
2246
|
+
return assertEx16(this._walletsListener, () => "Wallets listener not set");
|
|
2247
|
+
}
|
|
2248
|
+
cleanupListeners() {
|
|
2249
|
+
this.walletsListener.cleanupListeners();
|
|
2250
|
+
this.latestUserWalletListener.cleanupListeners();
|
|
2251
|
+
}
|
|
2252
|
+
/**
|
|
2253
|
+
* start listeners
|
|
2254
|
+
*/
|
|
2255
|
+
async start() {
|
|
2256
|
+
const archivist = await this.getOsSettingsArchivist();
|
|
2257
|
+
const diviner = await this.getOsSettingsPayloadDiviner();
|
|
2258
|
+
this._walletsListener = await PayloadStore.create({
|
|
2259
|
+
archivist,
|
|
2260
|
+
getLatest: async () => await OsSettingsQueries.getWallets(diviner),
|
|
2261
|
+
idFunction: isWalletSeedPhrase
|
|
2262
|
+
});
|
|
2263
|
+
this._latestUserWalletListener = await PayloadStore.create({
|
|
2264
|
+
archivist,
|
|
2265
|
+
getLatest: async () => await OsSettingsQueries.getLatestUserWallet(diviner),
|
|
2266
|
+
idFunction: isWalletSeedPhrase
|
|
2267
|
+
});
|
|
2268
|
+
this._nodeBackgroundListener = await PayloadStore.create({
|
|
2269
|
+
archivist,
|
|
2270
|
+
getLatest: async () => await OsSettingsQueries.getNodeBackground(diviner),
|
|
2271
|
+
idFunction: isNodeBackground
|
|
2272
|
+
});
|
|
2273
|
+
}
|
|
2274
|
+
};
|
|
2275
|
+
|
|
2276
|
+
// src/classes/node/DefaultPayloads/SigningKeyPayloads.ts
|
|
2277
|
+
var DappsWithAdditionalPayloads = /* @__PURE__ */ new Set(["Profile"]);
|
|
2278
|
+
var DefaultName = "Name Service";
|
|
2279
|
+
var DEFAULT_DAPP_ACCOUNT_PATHS = {
|
|
2280
|
+
[DefaultName]: "1'/1"
|
|
2281
|
+
};
|
|
2282
|
+
var ADDITIONAL_DAPP_ACCOUNT_PATHS = {
|
|
2283
|
+
["Profile"]: {
|
|
2284
|
+
[DefaultName]: DEFAULT_DAPP_ACCOUNT_PATHS[DefaultName]
|
|
2285
|
+
}
|
|
2286
|
+
};
|
|
2287
|
+
var SigningKeyPayloads = class {
|
|
2288
|
+
constructor(context, dappName) {
|
|
2289
|
+
this.context = context;
|
|
2290
|
+
this.dappName = dappName;
|
|
2291
|
+
}
|
|
2292
|
+
async payloads() {
|
|
2293
|
+
const wallet = await this.resolveXyoWallet();
|
|
2294
|
+
const defaultDappAccountSigningKeyPayloads = await this.signingKeyPayload(wallet, this.dappName, DEFAULT_DAPP_ACCOUNT_PATHS[this.dappName]);
|
|
2295
|
+
const additionalDappAccountSigningKeyPayloads = [];
|
|
2296
|
+
for (const [dappName, accountPaths] of Object.entries(ADDITIONAL_DAPP_ACCOUNT_PATHS)) {
|
|
2297
|
+
if (dappName !== this.dappName || !DappsWithAdditionalPayloads.has(dappName)) continue;
|
|
2298
|
+
for (const additionalDappAccountName of Object.keys(accountPaths)) {
|
|
2299
|
+
const signingKeyPayload = await this.signingKeyPayload(wallet, additionalDappAccountName, accountPaths[additionalDappAccountName]);
|
|
2300
|
+
additionalDappAccountSigningKeyPayloads.push(signingKeyPayload);
|
|
2301
|
+
}
|
|
2302
|
+
}
|
|
2303
|
+
return [...additionalDappAccountSigningKeyPayloads, defaultDappAccountSigningKeyPayloads].filter(isAnyPayload2);
|
|
2304
|
+
}
|
|
2305
|
+
async resolveXyoWallet() {
|
|
2306
|
+
const settingsCaller = new OsSettingsCaller(this.context);
|
|
2307
|
+
const latestWallet = await settingsCaller.getLatestUserWallet();
|
|
2308
|
+
const phrase = mnemonicArrayToString(latestWallet.mnemonic.mnemonic);
|
|
2309
|
+
const assertedPhrase = assertEx17(phrase, () => "No phrase found in latest wallet");
|
|
2310
|
+
return await HDWallet4.fromPhrase(assertedPhrase);
|
|
2311
|
+
}
|
|
2312
|
+
async signingKeyPayload(wallet, name, path) {
|
|
2313
|
+
if (path) {
|
|
2314
|
+
const subAccount = await wallet.derivePath(path);
|
|
2315
|
+
const privateKey = hexFrom(subAccount.privateKey, { prefix: false });
|
|
2316
|
+
return {
|
|
2317
|
+
dappName: name,
|
|
2318
|
+
privateKey,
|
|
2319
|
+
schema: SigningKeySchema
|
|
2320
|
+
};
|
|
2321
|
+
}
|
|
2322
|
+
}
|
|
2323
|
+
};
|
|
2324
|
+
|
|
2325
|
+
// src/classes/node/DefaultPayloads/DefaultPayloads.ts
|
|
2326
|
+
var DefaultPayloads = class {
|
|
2327
|
+
constructor(dappArchivist, xyOs, dappName) {
|
|
2328
|
+
this.dappArchivist = dappArchivist;
|
|
2329
|
+
this.xyOs = xyOs;
|
|
2330
|
+
this.dappName = dappName;
|
|
2331
|
+
}
|
|
2332
|
+
async insert() {
|
|
2333
|
+
const insertables = [NodeInfoPayload, SigningKeyPayloads, DappAccessPayloads];
|
|
2334
|
+
const insertPayloads = [];
|
|
2335
|
+
for (const insertable of insertables) {
|
|
2336
|
+
const classInstance = createInsertable(insertable, this.xyOs, this.dappName);
|
|
2337
|
+
const payloads = await classInstance.payloads();
|
|
2338
|
+
insertPayloads.push(...payloads);
|
|
2339
|
+
}
|
|
2340
|
+
for (const payload of insertPayloads) {
|
|
2341
|
+
const [existing] = await this.dappArchivist.get([await PayloadBuilder6.dataHash(payload)]);
|
|
2342
|
+
if (existing) continue;
|
|
2343
|
+
await this.dappArchivist.insert([payload]);
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
};
|
|
2347
|
+
|
|
2348
|
+
// src/classes/node/ExternalModulePermissions/ExternalModulePermissions.ts
|
|
2349
|
+
var ALLOWED_MODULES_FROM_PARENT = ["IntentArchivist", "OsSettingsNode", "OsPubSubNetworkStackNode", "OsXyoPublicNetworkStackNode"];
|
|
2350
|
+
var ExternalModulePermissions = class {
|
|
2351
|
+
constructor(context, dappWindowNode, dappName, externalPermissions) {
|
|
2352
|
+
this.context = context;
|
|
2353
|
+
this.dappWindowNode = dappWindowNode;
|
|
2354
|
+
this.dappName = dappName;
|
|
2355
|
+
this.externalPermissions = externalPermissions;
|
|
2356
|
+
}
|
|
2357
|
+
async permit() {
|
|
2358
|
+
for (const permission of this.externalPermissions ?? []) {
|
|
2359
|
+
await this.permitExternalModule(permission);
|
|
2360
|
+
}
|
|
2361
|
+
}
|
|
2362
|
+
getExternalModuleName(permission) {
|
|
2363
|
+
return permission.name;
|
|
2364
|
+
}
|
|
2365
|
+
async permitExternalModule(externalPermission) {
|
|
2366
|
+
const externalModuleName = this.getExternalModuleName(externalPermission);
|
|
2367
|
+
if (ALLOWED_MODULES_FROM_PARENT.includes(externalModuleName)) {
|
|
2368
|
+
const mod = await this.resolveExternalModule(externalModuleName);
|
|
2369
|
+
await this.registerAndAttachModule(mod);
|
|
2370
|
+
} else {
|
|
2371
|
+
console.warn(`${this.dappName} requested ${externalModuleName} but it was not fulfilled`);
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
async registerAndAttachModule(mod) {
|
|
2375
|
+
if (mod) {
|
|
2376
|
+
await this.dappWindowNode.register?.(mod);
|
|
2377
|
+
await this.dappWindowNode.attach(mod.address, true);
|
|
2378
|
+
}
|
|
2379
|
+
}
|
|
2380
|
+
async resolveExternalModule(id) {
|
|
2381
|
+
return await this.context.root.resolve(id);
|
|
2382
|
+
}
|
|
2383
|
+
};
|
|
2384
|
+
|
|
2385
|
+
// src/classes/node/Creator.ts
|
|
2386
|
+
var DappContextCreator = class _DappContextCreator {
|
|
2387
|
+
constructor(targetDappManifestParams, dappId, context, rootWallet, externalPermissions, sharedLocator) {
|
|
2388
|
+
this.targetDappManifestParams = targetDappManifestParams;
|
|
2389
|
+
this.dappId = dappId;
|
|
2390
|
+
this.context = context;
|
|
2391
|
+
this.rootWallet = rootWallet;
|
|
2392
|
+
this.externalPermissions = externalPermissions;
|
|
2393
|
+
this.sharedLocator = sharedLocator;
|
|
2394
|
+
}
|
|
2395
|
+
// Designated offset path for the wallet used by windowed dapps. In the future, other offsets could be used for other dapp modes
|
|
2396
|
+
static DAPP_WINDOW_WALLET_PATH = "1";
|
|
2397
|
+
static async create(params, xnsNodeUrl, xnsNetwork) {
|
|
2398
|
+
const { config, locator, context, wallet } = params;
|
|
2399
|
+
const { payload, dappName, publicChildren, privateChildren } = config;
|
|
2400
|
+
const { external, manifestPayload } = this.parseDappPackageManifestPayload(payload, xnsNodeUrl, xnsNetwork);
|
|
2401
|
+
console.debug("[DEBUG] dApp manifest", manifestPayload);
|
|
2402
|
+
const dappWallet = await wallet.derivePath(_DappContextCreator.DAPP_WINDOW_WALLET_PATH);
|
|
2403
|
+
const targetDappManifestParams = [manifestPayload, dappWallet, locator, publicChildren, privateChildren];
|
|
2404
|
+
const instance = new this(targetDappManifestParams, dappName, context, wallet, external?.modules, locator);
|
|
2405
|
+
return instance;
|
|
2406
|
+
}
|
|
2407
|
+
// Add default payloads to the archivist shared between the os and the dapp
|
|
2408
|
+
static async primeDappArchivist(dappContext, osContext, dappName) {
|
|
2409
|
+
const dappCaller = new DappCallerBase(dappContext);
|
|
2410
|
+
const dappArchivist = await dappCaller.getDappArchivist();
|
|
2411
|
+
const defaultPayloads = new DefaultPayloads(dappArchivist, osContext, dappName);
|
|
2412
|
+
await defaultPayloads.insert();
|
|
2413
|
+
}
|
|
2414
|
+
// Reset the dapp archivist to remove all payloads
|
|
2415
|
+
static async resetDappArchivist(context) {
|
|
2416
|
+
const dappModule = await context.root.resolve("DappArchivist");
|
|
2417
|
+
const dappArchivist = assertEx18(asArchivistInstance6(dappModule), () => "DappArchivist not found");
|
|
2418
|
+
await dappArchivist.clear();
|
|
2419
|
+
}
|
|
2420
|
+
static parseDappPackageManifestPayload(payload, xnsNodeUrl, xnsNetwork) {
|
|
2421
|
+
const { external, ...manifestWithoutExternals } = payload;
|
|
2422
|
+
const manifestPayloadRaw = {
|
|
2423
|
+
...manifestWithoutExternals,
|
|
2424
|
+
schema: PackageManifestPayloadSchema2
|
|
2425
|
+
};
|
|
2426
|
+
const manifestPayload = ReplaceManifestTokens(manifestPayloadRaw, xnsNodeUrl, xnsNetwork);
|
|
2427
|
+
return { external, manifestPayload };
|
|
2428
|
+
}
|
|
2429
|
+
async loadDappContext(context) {
|
|
2430
|
+
console.log("DappNodesCreator:loadDappContext", this.dappId);
|
|
2431
|
+
const dapp = await context.dappByName(this.dappId) ?? await (async () => {
|
|
2432
|
+
const dapp2 = await context.buildDapp(this.targetDappManifestParams[0], this.dappId);
|
|
2433
|
+
await dapp2.boot(this.rootWallet, this.sharedLocator);
|
|
2434
|
+
return dapp2;
|
|
2435
|
+
})();
|
|
2436
|
+
await this.handleExternalModulePermissions(dapp.root);
|
|
2437
|
+
await _DappContextCreator.primeDappArchivist(dapp, this.context, this.dappId);
|
|
2438
|
+
return dapp;
|
|
2439
|
+
}
|
|
2440
|
+
// try to satisfy the external permissions requested by a dapp
|
|
2441
|
+
async handleExternalModulePermissions(dappWindowNode) {
|
|
2442
|
+
const externalModulePermissions = new ExternalModulePermissions(this.context, dappWindowNode, this.dappId, this.externalPermissions);
|
|
2443
|
+
await externalModulePermissions.permit();
|
|
2444
|
+
}
|
|
2445
|
+
};
|
|
2446
|
+
|
|
2447
|
+
// src/classes/node/createDappContext.ts
|
|
2448
|
+
var GenericPayloadDivinerTags = { "network.xyo.generic.payload.diviner": "GenericPayloadDiviner" };
|
|
2449
|
+
var resolveLocator = (existingLocator) => {
|
|
2450
|
+
const locator = existingLocator ?? new ModuleFactoryLocator6();
|
|
2451
|
+
locator.register(GenericPayloadDiviner, GenericPayloadDivinerTags);
|
|
2452
|
+
return locator;
|
|
2453
|
+
};
|
|
2454
|
+
var createDappContext = async (dapp, context, allowedNames, xnsNodeUrl, xnsNetwork) => {
|
|
2455
|
+
console.log("createNodes");
|
|
2456
|
+
try {
|
|
2457
|
+
const dappSeedPhraseRepository = new DappSeedPhraseRepository(context, allowedNames);
|
|
2458
|
+
const seedPhrase = await dappSeedPhraseRepository.request(dapp.config.walletId);
|
|
2459
|
+
if (!seedPhrase) {
|
|
2460
|
+
throw new Error(`unable to find seed phrase for walletId: ${JSON.stringify(dapp.config, null, 2)}`);
|
|
2461
|
+
}
|
|
2462
|
+
const wallet = await HDWallet5.fromPhrase(seedPhrase);
|
|
2463
|
+
const dappName = dapp.config.name;
|
|
2464
|
+
const { config: dappConfig, params: dappParams } = dapp;
|
|
2465
|
+
const locator = resolveLocator((dappParams.locator ?? new ModuleFactoryLocator6()).merge(context.platformLocator));
|
|
2466
|
+
const params = {
|
|
2467
|
+
config: { dappName, payload: dappConfig.manifest },
|
|
2468
|
+
context,
|
|
2469
|
+
locator,
|
|
2470
|
+
wallet
|
|
2471
|
+
};
|
|
2472
|
+
const dappContextCreator = await DappContextCreator.create(params, xnsNodeUrl, xnsNetwork);
|
|
2473
|
+
const dappContext = await dappContextCreator.loadDappContext(context);
|
|
2474
|
+
return {
|
|
2475
|
+
context: dappContext,
|
|
2476
|
+
dappWallet: wallet
|
|
2477
|
+
};
|
|
2478
|
+
} catch (e) {
|
|
2479
|
+
const error = e;
|
|
2480
|
+
console.error(`Error creating dappWindow node: ${error.message}`);
|
|
2481
|
+
console.error(`Error creating dappWindow node: ${error.stack}`);
|
|
2482
|
+
throw new Error(`Error creating dappWindow node: ${error.message}`);
|
|
2483
|
+
}
|
|
2484
|
+
};
|
|
2485
|
+
|
|
2486
|
+
// src/classes/cache/RunningDappCache.ts
|
|
2487
|
+
var findOrCreateMutex = new Mutex2();
|
|
2488
|
+
var RunningDappCache = class _RunningDappCache {
|
|
2489
|
+
static _cache = {};
|
|
2490
|
+
static async findOrCreate(dapp, xyOs, allowedNames, xnsNodeUrl, xnsNetwork) {
|
|
2491
|
+
const dappId = dapp.config.name;
|
|
2492
|
+
return await XyOs.monitor(
|
|
2493
|
+
async () => await findOrCreateMutex.runExclusive(async () => {
|
|
2494
|
+
const existingWindowDappSet = _RunningDappCache.get(dappId);
|
|
2495
|
+
if (existingWindowDappSet) {
|
|
2496
|
+
console.debug("[DEBUG]", `RunningDappCache, using existing node for ${dappId}`);
|
|
2497
|
+
const existingDappContext = assertEx19(existingWindowDappSet.context, () => "No context found in existing dapp node");
|
|
2498
|
+
await DappContextCreator.primeDappArchivist(existingDappContext, xyOs, dappId);
|
|
2499
|
+
return existingWindowDappSet;
|
|
2500
|
+
}
|
|
2501
|
+
console.debug("[DEBUG]", "RunningDappCache:creating", dappId);
|
|
2502
|
+
const windowDappSet = await createDappContext(dapp, xyOs, allowedNames, xnsNodeUrl, xnsNetwork);
|
|
2503
|
+
_RunningDappCache.set(dappId, windowDappSet);
|
|
2504
|
+
return windowDappSet;
|
|
2505
|
+
}),
|
|
2506
|
+
{ additionalProperties: { dappId }, name: "Loading dApp" }
|
|
2507
|
+
);
|
|
2508
|
+
}
|
|
2509
|
+
static get(key) {
|
|
2510
|
+
return this._cache[key];
|
|
2511
|
+
}
|
|
2512
|
+
static has(key) {
|
|
2513
|
+
return key ? !!this._cache[key] : false;
|
|
2514
|
+
}
|
|
2515
|
+
static set(key, value) {
|
|
2516
|
+
this._cache[key] = value;
|
|
2517
|
+
}
|
|
2518
|
+
};
|
|
2519
|
+
|
|
2520
|
+
// src/access-interfaces/registered-names/helpers/AccessNodeQueries.ts
|
|
2521
|
+
var AccessNodeQueries = class {
|
|
2522
|
+
constructor(xyOsContext, accessRequest, registeredDappAccess) {
|
|
2523
|
+
this.xyOsContext = xyOsContext;
|
|
2524
|
+
this.accessRequest = accessRequest;
|
|
2525
|
+
this.registeredDappAccess = registeredDappAccess;
|
|
2526
|
+
}
|
|
2527
|
+
get nameServiceNamesAccessNode() {
|
|
2528
|
+
return assertEx20(RunningDappAccessCache.get(this.registeredDappAccess.registeringDappId), () => "No access node found");
|
|
2529
|
+
}
|
|
2530
|
+
// Refresh the archivist in the dapp
|
|
2531
|
+
async refresh() {
|
|
2532
|
+
const archivist = await this.getArchivistFromDappNode();
|
|
2533
|
+
await archivist.clear();
|
|
2534
|
+
await this.run();
|
|
2535
|
+
console.log("refreshed");
|
|
2536
|
+
}
|
|
2537
|
+
// Run the access node queries
|
|
2538
|
+
async run() {
|
|
2539
|
+
const payloads = await this.getPayloadsFromAccessNode();
|
|
2540
|
+
await this.insertAccessNodePayloadsIntoDappNodeArchivist(payloads);
|
|
2541
|
+
}
|
|
2542
|
+
async getArchivistFromDappNode() {
|
|
2543
|
+
const archivistPath = this.accessRequest?.destinationArchivist || "DappArchivist";
|
|
2544
|
+
const mod = assertEx20(await this.xyOsContext.root.resolve(archivistPath), () => `Unable to find archivist at ${archivistPath}`);
|
|
2545
|
+
return asArchivistInstance7(mod, () => `Module at ${archivistPath} is not an archivist`);
|
|
2546
|
+
}
|
|
2547
|
+
async getPayloadsFromAccessNode() {
|
|
2548
|
+
const archivistFromAccessNode = await this.resolveArchivistFromAccessNode();
|
|
2549
|
+
return await archivistFromAccessNode.all();
|
|
2550
|
+
}
|
|
2551
|
+
async insertAccessNodePayloadsIntoDappNodeArchivist(payloads) {
|
|
2552
|
+
const archivist = await this.getArchivistFromDappNode();
|
|
2553
|
+
await archivist.clear();
|
|
2554
|
+
await archivist.insert(payloads);
|
|
2555
|
+
}
|
|
2556
|
+
async resolveArchivistFromAccessNode() {
|
|
2557
|
+
const archivist = assertEx20(
|
|
2558
|
+
await this.nameServiceNamesAccessNode.resolve("PublicXnsArchivist"),
|
|
2559
|
+
() => "Unable to find archivist at PublicXnsArchivist"
|
|
2560
|
+
);
|
|
2561
|
+
return asArchivistInstance7(archivist, () => "PublicXnsArchivist is not an archivist");
|
|
2562
|
+
}
|
|
2563
|
+
};
|
|
2564
|
+
|
|
2565
|
+
// src/access-interfaces/registered-names/helpers/resource/AbstractXnsCaller.ts
|
|
2566
|
+
import { assertEx as assertEx21 } from "@xylabs/assert";
|
|
2567
|
+
import { asArchivistInstance as asArchivistInstance8 } from "@xyo-network/archivist-model";
|
|
2568
|
+
import { asDivinerInstance as asDivinerInstance5 } from "@xyo-network/diviner-model";
|
|
2569
|
+
var XnsArchivistPayloadDivinerModuleName = "XnsArchivistPayloadDiviner";
|
|
2570
|
+
var XnsArchivistModuleName = "XnsArchivist";
|
|
2571
|
+
var XnsRegistrationsArchivistModuleName = "XnsRegistrationsArchivist";
|
|
2572
|
+
var AbstractXnsCaller = class {
|
|
2573
|
+
constructor(context) {
|
|
2574
|
+
this.context = context;
|
|
2575
|
+
}
|
|
2576
|
+
/**
|
|
2577
|
+
* Get the XnsArchivist
|
|
2578
|
+
*/
|
|
2579
|
+
async getXnsArchivist(pathPrefix) {
|
|
2580
|
+
const fullyQualifiedPath = pathPrefix ? `${pathPrefix}:${XnsArchivistModuleName}` : XnsArchivistModuleName;
|
|
2581
|
+
const mod = assertEx21(await this.context.root.resolve(fullyQualifiedPath), () => `${fullyQualifiedPath} not found`);
|
|
2582
|
+
return asArchivistInstance8(mod, () => `${fullyQualifiedPath} is not an archivist`);
|
|
2583
|
+
}
|
|
2584
|
+
/**
|
|
2585
|
+
* Get the XnsArchivistPayloadDiviner
|
|
2586
|
+
*/
|
|
2587
|
+
async getXnsArchivistPayloadDiviner(pathPrefix) {
|
|
2588
|
+
const fullyQualifiedPath = pathPrefix ? `${pathPrefix}:${XnsArchivistPayloadDivinerModuleName}` : XnsArchivistPayloadDivinerModuleName;
|
|
2589
|
+
const mod = assertEx21(await this.context.root.resolve(fullyQualifiedPath), () => `${fullyQualifiedPath} not found`);
|
|
2590
|
+
return asDivinerInstance5(mod, () => `${fullyQualifiedPath} is not an diviner`);
|
|
2591
|
+
}
|
|
2592
|
+
/**
|
|
2593
|
+
* Get the XnsRegistrationsArchivist
|
|
2594
|
+
*/
|
|
2595
|
+
async getXnsRegistrationsArchivist(pathPrefix) {
|
|
2596
|
+
const fullyQualifiedPath = pathPrefix ? `${pathPrefix}:${XnsRegistrationsArchivistModuleName}` : XnsRegistrationsArchivistModuleName;
|
|
2597
|
+
const mod = assertEx21(await this.context.root.resolve(fullyQualifiedPath), () => `${fullyQualifiedPath} not found`);
|
|
2598
|
+
return asArchivistInstance8(mod, () => `${fullyQualifiedPath} is not an archivist`);
|
|
2599
|
+
}
|
|
2600
|
+
};
|
|
2601
|
+
|
|
2602
|
+
// src/access-interfaces/registered-names/helpers/resource/RegistrationsResource.ts
|
|
2603
|
+
import { assertEx as assertEx22 } from "@xylabs/assert";
|
|
2604
|
+
import { isTemporalIndexingDivinerResultIndex } from "@xyo-network/diviner-temporal-indexing-model";
|
|
2605
|
+
|
|
2606
|
+
// src/access-interfaces/registered-names/helpers/resource/RegistrationsResourceQueries.ts
|
|
2607
|
+
import { PayloadDivinerQuerySchema as PayloadDivinerQuerySchema6 } from "@xyo-network/diviner-payload-model";
|
|
2608
|
+
import { TemporalIndexingDivinerResultIndexSchema } from "@xyo-network/diviner-temporal-indexing-model";
|
|
2609
|
+
var XnsRegistrationsResourceQueries = {
|
|
2610
|
+
getAllRegistrations: async (diviner) => {
|
|
2611
|
+
const query = {
|
|
2612
|
+
limit: 100,
|
|
2613
|
+
order: "desc",
|
|
2614
|
+
schema: PayloadDivinerQuerySchema6,
|
|
2615
|
+
schemas: [TemporalIndexingDivinerResultIndexSchema]
|
|
2616
|
+
};
|
|
2617
|
+
return await diviner.divine([query]);
|
|
2618
|
+
},
|
|
2619
|
+
getRecentRegistrations: async (archivist) => {
|
|
2620
|
+
return (await archivist.all()).reverse();
|
|
2621
|
+
}
|
|
2622
|
+
};
|
|
2623
|
+
|
|
2624
|
+
// src/access-interfaces/registered-names/helpers/resource/RegistrationsResource.ts
|
|
2625
|
+
var XnsRegistrationsResource = class extends AbstractXnsCaller {
|
|
2626
|
+
constructor(context, pathPrefix) {
|
|
2627
|
+
super(context);
|
|
2628
|
+
this.pathPrefix = pathPrefix;
|
|
2629
|
+
}
|
|
2630
|
+
_allRegistrations;
|
|
2631
|
+
get allRegistrations() {
|
|
2632
|
+
return assertEx22(this._allRegistrations);
|
|
2633
|
+
}
|
|
2634
|
+
get subscriptions() {
|
|
2635
|
+
return {
|
|
2636
|
+
allRegistrations: (cb) => this.allRegistrations.subscribe(cb)
|
|
2637
|
+
};
|
|
2638
|
+
}
|
|
2639
|
+
get views() {
|
|
2640
|
+
return {
|
|
2641
|
+
allRegistrations: () => this.allRegistrations.getSnapshot()
|
|
2642
|
+
};
|
|
2643
|
+
}
|
|
2644
|
+
cleanupListeners() {
|
|
2645
|
+
this.allRegistrations.cleanupListeners();
|
|
2646
|
+
}
|
|
2647
|
+
async start() {
|
|
2648
|
+
const xnsArchivistPayloadDiviner = await this.getXnsArchivistPayloadDiviner(this.pathPrefix);
|
|
2649
|
+
const xnsArchivist = await this.getXnsArchivist(this.pathPrefix);
|
|
2650
|
+
this._allRegistrations = await PayloadStore.create({
|
|
2651
|
+
archivist: xnsArchivist,
|
|
2652
|
+
getLatest: async () => await XnsRegistrationsResourceQueries.getAllRegistrations(xnsArchivistPayloadDiviner),
|
|
2653
|
+
idFunction: isTemporalIndexingDivinerResultIndex
|
|
2654
|
+
});
|
|
2655
|
+
}
|
|
2656
|
+
};
|
|
2657
|
+
|
|
2658
|
+
// src/access-interfaces/ValidDappAccessInterfaces.ts
|
|
2659
|
+
import { RegisteredNames, RegisteredNamesInterface } from "@xyo-network/os-model";
|
|
2660
|
+
var ValidDappAccessInterfaces = {
|
|
2661
|
+
[RegisteredNames]: RegisteredNamesInterface
|
|
2662
|
+
};
|
|
2663
|
+
|
|
2664
|
+
// src/Caller.ts
|
|
2665
|
+
import { assertEx as assertEx23 } from "@xylabs/assert";
|
|
2666
|
+
import { Account } from "@xyo-network/account";
|
|
2667
|
+
import { PayloadDivinerQuerySchema as PayloadDivinerQuerySchema8 } from "@xyo-network/diviner-payload-model";
|
|
2668
|
+
import {
|
|
2669
|
+
DappAccessRequestSchema,
|
|
2670
|
+
DappInjectableParamsSchema as DappInjectableParamsSchema2,
|
|
2671
|
+
DappIntentTypes as DappIntentTypes2,
|
|
2672
|
+
DappMode as DappMode3,
|
|
2673
|
+
RegisteredDappAccessSchema as RegisteredDappAccessSchema2,
|
|
2674
|
+
SigningKeySchema as SigningKeySchema2
|
|
2675
|
+
} from "@xyo-network/os-model";
|
|
2676
|
+
import { PayloadBuilder as PayloadBuilder7 } from "@xyo-network/payload-builder";
|
|
2677
|
+
|
|
2678
|
+
// src/DefaultsQueries.ts
|
|
2679
|
+
import { PayloadDivinerQuerySchema as PayloadDivinerQuerySchema7 } from "@xyo-network/diviner-payload-model";
|
|
2680
|
+
import { DappInjectableParamsSchema, NodeOsInfoSchema as NodeOsInfoSchema2 } from "@xyo-network/os-model";
|
|
2681
|
+
var DappDefaultsResourceQueries = {
|
|
2682
|
+
async getDappInjectableParamsFromRoute(diviner) {
|
|
2683
|
+
const query = {
|
|
2684
|
+
limit: 1,
|
|
2685
|
+
order: "desc",
|
|
2686
|
+
schema: PayloadDivinerQuerySchema7,
|
|
2687
|
+
schemas: [DappInjectableParamsSchema],
|
|
2688
|
+
source: "route"
|
|
2689
|
+
};
|
|
2690
|
+
return await diviner.divine([query]);
|
|
2691
|
+
},
|
|
2692
|
+
async getNodeOsInfo(diviner) {
|
|
2693
|
+
const query = {
|
|
2694
|
+
limit: 1,
|
|
2695
|
+
order: "desc",
|
|
2696
|
+
schema: PayloadDivinerQuerySchema7,
|
|
2697
|
+
schemas: [NodeOsInfoSchema2]
|
|
2698
|
+
};
|
|
2699
|
+
return await diviner.divine([query]);
|
|
2700
|
+
}
|
|
2701
|
+
};
|
|
2702
|
+
|
|
2703
|
+
// src/Caller.ts
|
|
2704
|
+
var DappCaller = class extends DappCallerBase {
|
|
2705
|
+
constructor(context) {
|
|
2706
|
+
super(context);
|
|
2707
|
+
}
|
|
2708
|
+
/**
|
|
2709
|
+
* Add payloads to the dapp archivist
|
|
2710
|
+
* @param {Payload[]} payloads
|
|
2711
|
+
* @param {Boolean} force
|
|
2712
|
+
*/
|
|
2713
|
+
async addPayloadsToDappArchivist(payloads = [], force = false) {
|
|
2714
|
+
const dappArchivist = await this.getDappArchivist();
|
|
2715
|
+
if (force) {
|
|
2716
|
+
await dappArchivist.insert(payloads);
|
|
2717
|
+
} else {
|
|
2718
|
+
for (const payload of payloads) {
|
|
2719
|
+
const hash = await PayloadBuilder7.dataHash(payload);
|
|
2720
|
+
const [existing] = await dappArchivist.get([hash]);
|
|
2721
|
+
if (existing) continue;
|
|
2722
|
+
await dappArchivist.insert([payload]);
|
|
2723
|
+
}
|
|
2724
|
+
}
|
|
2725
|
+
}
|
|
2726
|
+
/**
|
|
2727
|
+
* Build Access Request
|
|
2728
|
+
* @param {DappAccessRequest['mode']} mode
|
|
2729
|
+
* @param {Hash} registeredDappAccessHash
|
|
2730
|
+
* @param {string} requestingDappId
|
|
2731
|
+
* @param {string | undefined} destinationArchivist
|
|
2732
|
+
*/
|
|
2733
|
+
buildAccessRequest(mode, registeredDappAccessHash, requestingDappId, destinationArchivist) {
|
|
2734
|
+
const accessRequest = {
|
|
2735
|
+
destinationArchivist,
|
|
2736
|
+
mode,
|
|
2737
|
+
registeredDappAccessHash,
|
|
2738
|
+
requestingDappId,
|
|
2739
|
+
schema: DappAccessRequestSchema,
|
|
2740
|
+
timestamp: Date.now()
|
|
2741
|
+
};
|
|
2742
|
+
return accessRequest;
|
|
2743
|
+
}
|
|
2744
|
+
/**
|
|
2745
|
+
* Get all available dapp access requests
|
|
2746
|
+
*/
|
|
2747
|
+
async getDappAccessPayloads() {
|
|
2748
|
+
const diviner = await this.getDappArchivistPayloadDiviner();
|
|
2749
|
+
const query = {
|
|
2750
|
+
order: "desc",
|
|
2751
|
+
schema: PayloadDivinerQuerySchema8,
|
|
2752
|
+
schemas: [RegisteredDappAccessSchema2]
|
|
2753
|
+
};
|
|
2754
|
+
return diviner.divine([query]);
|
|
2755
|
+
}
|
|
2756
|
+
/**
|
|
2757
|
+
* Get the most recent dapp injectable params
|
|
2758
|
+
* @returns Promise
|
|
2759
|
+
*/
|
|
2760
|
+
async getDappInjectableParams() {
|
|
2761
|
+
const diviner = await this.getDappArchivistPayloadDiviner();
|
|
2762
|
+
return await DappDefaultsResourceQueries.getDappInjectableParamsFromRoute(diviner);
|
|
2763
|
+
}
|
|
2764
|
+
/**
|
|
2765
|
+
* Get the most recent dapp intent
|
|
2766
|
+
* @param {string} name
|
|
2767
|
+
*/
|
|
2768
|
+
async getRegisteredDappAccessByName(name) {
|
|
2769
|
+
const diviner = await this.getDappArchivistPayloadDiviner();
|
|
2770
|
+
const query = {
|
|
2771
|
+
limit: 1,
|
|
2772
|
+
name,
|
|
2773
|
+
schema: PayloadDivinerQuerySchema8,
|
|
2774
|
+
schemas: [RegisteredDappAccessSchema2]
|
|
2775
|
+
};
|
|
2776
|
+
const [payload] = assertEx23(await diviner.divine([query]), () => "Unable to find registered dapp access");
|
|
2777
|
+
return payload;
|
|
2778
|
+
}
|
|
2779
|
+
/**
|
|
2780
|
+
* Get the user wallet
|
|
2781
|
+
*/
|
|
2782
|
+
async getUserAccount(dappName) {
|
|
2783
|
+
const diviner = await this.getDappArchivistPayloadDiviner();
|
|
2784
|
+
const query = {
|
|
2785
|
+
dappName,
|
|
2786
|
+
limit: 1,
|
|
2787
|
+
order: "desc",
|
|
2788
|
+
schema: PayloadDivinerQuerySchema8,
|
|
2789
|
+
schemas: [SigningKeySchema2]
|
|
2790
|
+
};
|
|
2791
|
+
const [payload] = assertEx23(await diviner.divine([query]), () => "Unable to find user wallet");
|
|
2792
|
+
if (payload) {
|
|
2793
|
+
return await Account.fromPrivateKey(payload.privateKey);
|
|
2794
|
+
} else {
|
|
2795
|
+
throw new Error("Unable to find user wallet");
|
|
2796
|
+
}
|
|
2797
|
+
}
|
|
2798
|
+
/**
|
|
2799
|
+
* Get all wallets for the user
|
|
2800
|
+
*/
|
|
2801
|
+
async getUserAccountsAll() {
|
|
2802
|
+
const diviner = await this.getDappArchivistPayloadDiviner();
|
|
2803
|
+
const query = {
|
|
2804
|
+
order: "desc",
|
|
2805
|
+
schema: PayloadDivinerQuerySchema8,
|
|
2806
|
+
schemas: [SigningKeySchema2]
|
|
2807
|
+
};
|
|
2808
|
+
const payloads = assertEx23(await diviner.divine([query]), () => "Unable to find user wallet");
|
|
2809
|
+
if (payloads?.length) {
|
|
2810
|
+
return payloads;
|
|
2811
|
+
} else {
|
|
2812
|
+
throw new Error("Unable to find user wallet");
|
|
2813
|
+
}
|
|
2814
|
+
}
|
|
2815
|
+
/**
|
|
2816
|
+
* Make Dapp Access Request
|
|
2817
|
+
* @param {string} requestingDappId
|
|
2818
|
+
* @param {string} registeredDappAccessHash
|
|
2819
|
+
* @param {string} mode
|
|
2820
|
+
* @param {string} destinationArchivist
|
|
2821
|
+
*/
|
|
2822
|
+
async makeDappAccessRequest(requestingDappId, registeredDappAccessHash, mode, destinationArchivist) {
|
|
2823
|
+
const archivist = await this.getDappArchivist();
|
|
2824
|
+
const registeredDappAccessPayload = await archivist.get([registeredDappAccessHash]);
|
|
2825
|
+
if (!registeredDappAccessPayload) {
|
|
2826
|
+
throw new Error("No registered dapp access found");
|
|
2827
|
+
}
|
|
2828
|
+
const accessRequest = this.buildAccessRequest(mode, registeredDappAccessHash, requestingDappId, destinationArchivist);
|
|
2829
|
+
await archivist.insert([accessRequest]);
|
|
2830
|
+
}
|
|
2831
|
+
/**
|
|
2832
|
+
* Make Dapp Intent Request
|
|
2833
|
+
* @param {DappId} sourceDappId
|
|
2834
|
+
* @param {DappId} targetDappId
|
|
2835
|
+
* @param {DappIntentTypes=DappIntentTypes.Launch} intent
|
|
2836
|
+
* @param {DappIntent['params']} mode
|
|
2837
|
+
*/
|
|
2838
|
+
async makeDappIntentRequest(sourceDappId, targetDappId, intent = DappIntentTypes2.Launch, mode = DappMode3.Window, params) {
|
|
2839
|
+
if (!sourceDappId) {
|
|
2840
|
+
throw new Error("No source dapp id provided, dApps must declare a sourceDappId");
|
|
2841
|
+
}
|
|
2842
|
+
const archivist = await this.getDappArchivist();
|
|
2843
|
+
const dappIntent = DappIntentCaller.buildIntent(sourceDappId, intent, targetDappId, mode, params);
|
|
2844
|
+
return await archivist.insert([dappIntent]);
|
|
2845
|
+
}
|
|
2846
|
+
/**
|
|
2847
|
+
* Set the dapp injectable params
|
|
2848
|
+
*
|
|
2849
|
+
* @param {URLSearchParams} searchParams
|
|
2850
|
+
* @param {string} pathname
|
|
2851
|
+
*/
|
|
2852
|
+
async setDappInjectableRouteParams(searchParams = new URLSearchParams(), pathname) {
|
|
2853
|
+
const dappArchivist = await this.getDappArchivist();
|
|
2854
|
+
const params = {};
|
|
2855
|
+
for (const [key, value] of searchParams.entries()) {
|
|
2856
|
+
params[key] = value;
|
|
2857
|
+
}
|
|
2858
|
+
const payload = {
|
|
2859
|
+
params,
|
|
2860
|
+
path: pathname,
|
|
2861
|
+
schema: DappInjectableParamsSchema2,
|
|
2862
|
+
source: "route",
|
|
2863
|
+
timestamp: Date.now()
|
|
2864
|
+
};
|
|
2865
|
+
await dappArchivist.insert([payload]);
|
|
2866
|
+
}
|
|
2867
|
+
};
|
|
2868
|
+
|
|
2869
|
+
// src/classes/dapp/access/Caller.ts
|
|
2870
|
+
import { assertEx as assertEx24 } from "@xylabs/assert";
|
|
2871
|
+
import { ModuleFactoryLocator as ModuleFactoryLocator7 } from "@xyo-network/module-factory-locator";
|
|
2872
|
+
import { asNodeInstance as asNodeInstance2 } from "@xyo-network/node-model";
|
|
2873
|
+
import {
|
|
2874
|
+
isRegisteredDappAccessDappSet as isRegisteredDappAccessDappSet2
|
|
2875
|
+
} from "@xyo-network/os-model";
|
|
2876
|
+
import { PayloadBuilder as PayloadBuilder8 } from "@xyo-network/payload-builder";
|
|
2877
|
+
var DappAccessCaller = class extends OsCallerBase {
|
|
2878
|
+
constructor(context, registeredDapps, allowedNames, xnsNodeUrl, xnsNetwork, platformLocator = new ModuleFactoryLocator7()) {
|
|
2879
|
+
super(context);
|
|
2880
|
+
this.registeredDapps = registeredDapps;
|
|
2881
|
+
this.allowedNames = allowedNames;
|
|
2882
|
+
this.xnsNodeUrl = xnsNodeUrl;
|
|
2883
|
+
this.xnsNetwork = xnsNetwork;
|
|
2884
|
+
this.platformLocator = platformLocator;
|
|
2885
|
+
}
|
|
2886
|
+
async start() {
|
|
2887
|
+
const registeredDappAccessDappSets = this.registeredDapps.filter(isRegisteredDappAccessDappSet2);
|
|
2888
|
+
for (const registeredDappSet of registeredDappAccessDappSets) {
|
|
2889
|
+
const context = await this.buildRunningDappCache(registeredDappSet, this.allowedNames);
|
|
2890
|
+
for (const dappAccessor of registeredDappSet.dapp.accessors) {
|
|
2891
|
+
await this.cacheAccessNode(dappAccessor, context);
|
|
2892
|
+
await this.saveRegisteredDappInterfaces(dappAccessor);
|
|
2893
|
+
}
|
|
2894
|
+
}
|
|
2895
|
+
}
|
|
2896
|
+
async buildRunningDappCache(registeredDappSet, allowedNames) {
|
|
2897
|
+
const { context } = await RunningDappCache.findOrCreate(registeredDappSet.dapp, this.context, allowedNames, this.xnsNodeUrl, this.xnsNetwork);
|
|
2898
|
+
if (!context) throw new Error("Dapp context not found");
|
|
2899
|
+
return context;
|
|
2900
|
+
}
|
|
2901
|
+
async cacheAccessNode(dappAccessor, context) {
|
|
2902
|
+
const accessNodeId = dappAccessor.rootModuleId;
|
|
2903
|
+
const accessNodeModule = assertEx24(await context.root.resolve(accessNodeId), () => "Access Node Module not found");
|
|
2904
|
+
const accessNode = asNodeInstance2(accessNodeModule, () => "Access Node Module is not a NodeInstance");
|
|
2905
|
+
RunningDappAccessCache.set(dappAccessor.registeringDappId, accessNode);
|
|
2906
|
+
}
|
|
2907
|
+
async saveRegisteredDappInterfaces(registeredDappAccess) {
|
|
2908
|
+
const archivist = await this.getRegisteredDappInterfacesArchivist();
|
|
2909
|
+
const hash = await PayloadBuilder8.dataHash(registeredDappAccess);
|
|
2910
|
+
const [existing] = await archivist.get([hash]);
|
|
2911
|
+
if (existing) return;
|
|
2912
|
+
await archivist.insert([registeredDappAccess]);
|
|
2913
|
+
console.debug("[DEBUG] -", `Registered ${registeredDappAccess.registeringDappId} with ${registeredDappAccess.name} interface`);
|
|
2914
|
+
}
|
|
2915
|
+
};
|
|
2916
|
+
|
|
2917
|
+
// src/classes/dapp/access/Queries.ts
|
|
2918
|
+
import { PayloadDivinerQuerySchema as PayloadDivinerQuerySchema9 } from "@xyo-network/diviner-payload-model";
|
|
2919
|
+
import { DappAccessRequestSchema as DappAccessRequestSchema2, RegisteredDappAccessSchema as RegisteredDappAccessSchema3 } from "@xyo-network/os-model";
|
|
2920
|
+
var noResults = [];
|
|
2921
|
+
var DappAccessRequestQueries = {
|
|
2922
|
+
async getAccessRequests(diviner) {
|
|
2923
|
+
const query = {
|
|
2924
|
+
limit: 1,
|
|
2925
|
+
schema: PayloadDivinerQuerySchema9,
|
|
2926
|
+
schemas: [DappAccessRequestSchema2]
|
|
2927
|
+
};
|
|
2928
|
+
const results = await diviner.divine([query]);
|
|
2929
|
+
if (results.length === 0) {
|
|
2930
|
+
return noResults;
|
|
2931
|
+
}
|
|
2932
|
+
return results;
|
|
2933
|
+
},
|
|
2934
|
+
async getRegisteredInterfaces(diviner) {
|
|
2935
|
+
const query = {
|
|
2936
|
+
limit: 1,
|
|
2937
|
+
schema: PayloadDivinerQuerySchema9,
|
|
2938
|
+
schemas: [RegisteredDappAccessSchema3]
|
|
2939
|
+
};
|
|
2940
|
+
const results = await diviner.divine([query]);
|
|
2941
|
+
if (results.length === 0) {
|
|
2942
|
+
return noResults;
|
|
2943
|
+
}
|
|
2944
|
+
return results;
|
|
2945
|
+
}
|
|
2946
|
+
};
|
|
2947
|
+
|
|
2948
|
+
// src/classes/dapp/access/Resource.ts
|
|
2949
|
+
import { assertEx as assertEx25 } from "@xylabs/assert";
|
|
2950
|
+
import { isDappAccessRequestWithMeta, isRegisteredDappAccess as isRegisteredDappAccess2 } from "@xyo-network/os-model";
|
|
2951
|
+
var DappAccessRequestResource = class extends DappCallerBase {
|
|
2952
|
+
_accessRequestListener;
|
|
2953
|
+
_registeredInterfacesListener;
|
|
2954
|
+
constructor(context) {
|
|
2955
|
+
super(context);
|
|
2956
|
+
}
|
|
2957
|
+
get accessRequestListener() {
|
|
2958
|
+
return assertEx25(this._accessRequestListener, () => "accessRequests note found");
|
|
2959
|
+
}
|
|
2960
|
+
get registeredInterfacesListener() {
|
|
2961
|
+
return assertEx25(this._registeredInterfacesListener, () => "registeredInterfaces note found");
|
|
2962
|
+
}
|
|
2963
|
+
get subscriptions() {
|
|
2964
|
+
return {
|
|
2965
|
+
accessRequests: (cb) => this.accessRequestListener.subscribe(cb),
|
|
2966
|
+
registeredInterfaces: (cb) => this.registeredInterfacesListener.subscribe(cb)
|
|
2967
|
+
};
|
|
2968
|
+
}
|
|
2969
|
+
get views() {
|
|
2970
|
+
return {
|
|
2971
|
+
accessRequests: () => this.accessRequestListener.getSnapshot(),
|
|
2972
|
+
registeredInterfaces: () => this.registeredInterfacesListener.getSnapshot()
|
|
2973
|
+
};
|
|
2974
|
+
}
|
|
2975
|
+
cleanupListeners() {
|
|
2976
|
+
this.accessRequestListener.cleanupListeners();
|
|
2977
|
+
}
|
|
2978
|
+
// Start the resource
|
|
2979
|
+
async start() {
|
|
2980
|
+
const archivist = await this.getDappArchivist();
|
|
2981
|
+
const diviner = await this.getDappArchivistPayloadDiviner();
|
|
2982
|
+
this._accessRequestListener = await PayloadStore.create({
|
|
2983
|
+
archivist,
|
|
2984
|
+
getLatest: async () => await DappAccessRequestQueries.getAccessRequests(diviner),
|
|
2985
|
+
idFunction: isDappAccessRequestWithMeta
|
|
2986
|
+
});
|
|
2987
|
+
this._registeredInterfacesListener = await PayloadStore.create({
|
|
2988
|
+
archivist,
|
|
2989
|
+
getLatest: async () => await DappAccessRequestQueries.getRegisteredInterfaces(diviner),
|
|
2990
|
+
idFunction: isRegisteredDappAccess2
|
|
2991
|
+
});
|
|
2992
|
+
}
|
|
2993
|
+
};
|
|
2994
|
+
|
|
2995
|
+
// src/classes/dapp/DefaultsResource.ts
|
|
2996
|
+
import { assertEx as assertEx26 } from "@xylabs/assert";
|
|
2997
|
+
import { isDappInjectableParams, isNodeOsInfo } from "@xyo-network/os-model";
|
|
2998
|
+
var DappDefaultsResource = class extends DappCallerBase {
|
|
2999
|
+
_dappInjectableParamsFromRouteListener;
|
|
3000
|
+
_nodeOsInfoListener;
|
|
3001
|
+
constructor(context) {
|
|
3002
|
+
super(context);
|
|
3003
|
+
}
|
|
3004
|
+
get dappInjectableParamsFromRouteListener() {
|
|
3005
|
+
return assertEx26(this._dappInjectableParamsFromRouteListener, () => "DappInjectableParams not found");
|
|
3006
|
+
}
|
|
3007
|
+
get nodeOsInfoListener() {
|
|
3008
|
+
return assertEx26(this._nodeOsInfoListener, () => "NodeOsInfo not found");
|
|
3009
|
+
}
|
|
3010
|
+
get subscriptions() {
|
|
3011
|
+
return {
|
|
3012
|
+
dappInjectableParamsFromRoute: (cb) => this.dappInjectableParamsFromRouteListener.subscribe(cb),
|
|
3013
|
+
nodeOsInfo: (cb) => this.nodeOsInfoListener.subscribe(cb)
|
|
3014
|
+
};
|
|
3015
|
+
}
|
|
3016
|
+
get views() {
|
|
3017
|
+
return {
|
|
3018
|
+
dappInjectableParamsFromRoute: () => this.dappInjectableParamsFromRouteListener.getSnapshot(),
|
|
3019
|
+
nodeOsInfo: () => this.nodeOsInfoListener.getSnapshot()
|
|
3020
|
+
};
|
|
3021
|
+
}
|
|
3022
|
+
cleanupListeners() {
|
|
3023
|
+
this.nodeOsInfoListener.cleanupListeners();
|
|
3024
|
+
}
|
|
3025
|
+
async start() {
|
|
3026
|
+
const archivist = await this.getDappArchivist();
|
|
3027
|
+
const diviner = await this.getDappArchivistPayloadDiviner();
|
|
3028
|
+
this._nodeOsInfoListener = await PayloadStore.create({
|
|
3029
|
+
archivist,
|
|
3030
|
+
getLatest: async () => await DappDefaultsResourceQueries.getNodeOsInfo(diviner),
|
|
3031
|
+
idFunction: isNodeOsInfo
|
|
3032
|
+
});
|
|
3033
|
+
this._dappInjectableParamsFromRouteListener = await PayloadStore.create({
|
|
3034
|
+
archivist,
|
|
3035
|
+
getLatest: async () => await DappDefaultsResourceQueries.getDappInjectableParamsFromRoute(diviner),
|
|
3036
|
+
idFunction: isDappInjectableParams
|
|
3037
|
+
});
|
|
3038
|
+
}
|
|
3039
|
+
};
|
|
3040
|
+
|
|
3041
|
+
// src/classes/menu/Caller.ts
|
|
3042
|
+
import { PayloadDivinerQuerySchema as PayloadDivinerQuerySchema10 } from "@xyo-network/diviner-payload-model";
|
|
3043
|
+
import {
|
|
3044
|
+
DappInjectableParamsSchema as DappInjectableParamsSchema3,
|
|
3045
|
+
DappNavItemSelectionSchema
|
|
3046
|
+
} from "@xyo-network/os-model";
|
|
3047
|
+
import { PayloadBuilder as PayloadBuilder9 } from "@xyo-network/payload-builder";
|
|
3048
|
+
var DappMenuCaller = class extends DappCaller {
|
|
3049
|
+
constructor(context) {
|
|
3050
|
+
super(context);
|
|
3051
|
+
}
|
|
3052
|
+
/**
|
|
3053
|
+
* Add new Payloads to the dappArchivist if they haven't already
|
|
3054
|
+
*
|
|
3055
|
+
* @param {Payload[]} payloads
|
|
3056
|
+
*/
|
|
3057
|
+
async add(payloads, force = false) {
|
|
3058
|
+
const dappArchivist = await this.getDappArchivist();
|
|
3059
|
+
for (const payload of payloads) {
|
|
3060
|
+
const hash = await PayloadBuilder9.dataHash(payload);
|
|
3061
|
+
const [existing] = await dappArchivist.get([hash]);
|
|
3062
|
+
if (!existing || force) {
|
|
3063
|
+
await dappArchivist.insert(payloads);
|
|
3064
|
+
}
|
|
3065
|
+
}
|
|
3066
|
+
}
|
|
3067
|
+
async currentNavSelection() {
|
|
3068
|
+
const diviner = await this.getDappArchivistPayloadDiviner();
|
|
3069
|
+
const query = {
|
|
3070
|
+
limit: 1,
|
|
3071
|
+
order: "desc",
|
|
3072
|
+
schema: PayloadDivinerQuerySchema10,
|
|
3073
|
+
schemas: [DappNavItemSelectionSchema]
|
|
3074
|
+
};
|
|
3075
|
+
return await diviner.divine([query]);
|
|
3076
|
+
}
|
|
3077
|
+
/*
|
|
3078
|
+
* Indicate to the system that a nav item was selected
|
|
3079
|
+
*
|
|
3080
|
+
* @param {string} path
|
|
3081
|
+
* @params {string} injectableParamsPath
|
|
3082
|
+
* @returns Promise
|
|
3083
|
+
*/
|
|
3084
|
+
async makeNavItemSelection(path, injectableParamsPath) {
|
|
3085
|
+
const dappArchivist = await this.getDappArchivist();
|
|
3086
|
+
const payload = {
|
|
3087
|
+
path,
|
|
3088
|
+
schema: DappNavItemSelectionSchema,
|
|
3089
|
+
timestamp: Date.now()
|
|
3090
|
+
};
|
|
3091
|
+
await dappArchivist.insert([payload]);
|
|
3092
|
+
if (injectableParamsPath) {
|
|
3093
|
+
const injectableParams = {
|
|
3094
|
+
params: {},
|
|
3095
|
+
path: injectableParamsPath,
|
|
3096
|
+
schema: DappInjectableParamsSchema3,
|
|
3097
|
+
source: "route",
|
|
3098
|
+
timestamp: Date.now()
|
|
3099
|
+
};
|
|
3100
|
+
await dappArchivist.insert([injectableParams]);
|
|
3101
|
+
}
|
|
3102
|
+
}
|
|
3103
|
+
};
|
|
3104
|
+
|
|
3105
|
+
// src/classes/menu/Resource.ts
|
|
3106
|
+
import { assertEx as assertEx27 } from "@xylabs/assert";
|
|
3107
|
+
import { isDappNavItemSelection, isDappNavMenuConfig } from "@xyo-network/os-model";
|
|
3108
|
+
|
|
3109
|
+
// src/classes/menu/Queries.ts
|
|
3110
|
+
import { PayloadDivinerQuerySchema as PayloadDivinerQuerySchema11 } from "@xyo-network/diviner-payload-model";
|
|
3111
|
+
import {
|
|
3112
|
+
DappNavItemSchema,
|
|
3113
|
+
DappNavItemSelectionSchema as DappNavItemSelectionSchema2,
|
|
3114
|
+
DappNavMenuConfigSchema
|
|
3115
|
+
} from "@xyo-network/os-model";
|
|
3116
|
+
var NO_ITEMS = [];
|
|
3117
|
+
var DappMenuQueries = {
|
|
3118
|
+
async getMenuConfigs(diviner) {
|
|
3119
|
+
const query = {
|
|
3120
|
+
limit: 1,
|
|
3121
|
+
order: "desc",
|
|
3122
|
+
schema: PayloadDivinerQuerySchema11,
|
|
3123
|
+
schemas: [DappNavMenuConfigSchema]
|
|
3124
|
+
};
|
|
3125
|
+
return await diviner.divine([query]);
|
|
3126
|
+
},
|
|
3127
|
+
async getMenuItems(diviner) {
|
|
3128
|
+
const query = {
|
|
3129
|
+
order: "desc",
|
|
3130
|
+
schema: PayloadDivinerQuerySchema11,
|
|
3131
|
+
schemas: [DappNavItemSchema]
|
|
3132
|
+
};
|
|
3133
|
+
const results = await diviner.divine([query]);
|
|
3134
|
+
return results.length > 0 ? results : NO_ITEMS;
|
|
3135
|
+
},
|
|
3136
|
+
async getNavItemSelections(diviner) {
|
|
3137
|
+
const query = {
|
|
3138
|
+
limit: 1,
|
|
3139
|
+
order: "desc",
|
|
3140
|
+
schema: PayloadDivinerQuerySchema11,
|
|
3141
|
+
schemas: [DappNavItemSelectionSchema2]
|
|
3142
|
+
};
|
|
3143
|
+
const results = await diviner.divine([query]);
|
|
3144
|
+
return results.length > 0 ? results : NO_ITEMS;
|
|
3145
|
+
},
|
|
3146
|
+
async getVisibleMenuItems(diviner) {
|
|
3147
|
+
const query = {
|
|
3148
|
+
order: "desc",
|
|
3149
|
+
schema: PayloadDivinerQuerySchema11,
|
|
3150
|
+
schemas: [DappNavItemSchema]
|
|
3151
|
+
};
|
|
3152
|
+
const results = await diviner.divine([query]);
|
|
3153
|
+
return results.filter((menuItem) => menuItem.hidden !== true).sort((a, b) => (a.weight ?? 0) > (b.weight ?? 0) ? 1 : -1);
|
|
3154
|
+
}
|
|
3155
|
+
};
|
|
3156
|
+
|
|
3157
|
+
// src/classes/menu/Resource.ts
|
|
3158
|
+
var DappMenuResource = class extends DappCaller {
|
|
3159
|
+
_menuConfigListener;
|
|
3160
|
+
_menuItemListener;
|
|
3161
|
+
_menuItemSelectionListener;
|
|
3162
|
+
_menuItemVisibleListener;
|
|
3163
|
+
constructor(xyOsContext) {
|
|
3164
|
+
super(xyOsContext);
|
|
3165
|
+
}
|
|
3166
|
+
get menuConfigListener() {
|
|
3167
|
+
return assertEx27(this._menuConfigListener, () => "menuConfigListener note found");
|
|
3168
|
+
}
|
|
3169
|
+
get menuItemListener() {
|
|
3170
|
+
return assertEx27(this._menuItemListener, () => "menuItemListener note found");
|
|
3171
|
+
}
|
|
3172
|
+
get menuItemSelectionListener() {
|
|
3173
|
+
return assertEx27(this._menuItemSelectionListener, () => "menuItemSelectionListener note found");
|
|
3174
|
+
}
|
|
3175
|
+
get menuItemVisibleListener() {
|
|
3176
|
+
return assertEx27(this._menuItemVisibleListener, () => "menuItemVisibleListener note found");
|
|
3177
|
+
}
|
|
3178
|
+
/** subscribe to latest set of payloads */
|
|
3179
|
+
get subscriptions() {
|
|
3180
|
+
return {
|
|
3181
|
+
menuConfigs: (cb) => this.menuConfigListener.subscribe(cb),
|
|
3182
|
+
menuItemSelections: (cb) => this.menuItemSelectionListener.subscribe(cb),
|
|
3183
|
+
menuItems: (cb) => this.menuItemListener.subscribe(cb),
|
|
3184
|
+
menuItemsVisible: (cb) => this.menuItemVisibleListener.subscribe(cb)
|
|
3185
|
+
};
|
|
3186
|
+
}
|
|
3187
|
+
/**
|
|
3188
|
+
* Return the latest menu payloads from the repository
|
|
3189
|
+
*/
|
|
3190
|
+
get views() {
|
|
3191
|
+
return {
|
|
3192
|
+
menuConfigs: () => this.menuConfigListener.getSnapshot(),
|
|
3193
|
+
menuItemSelections: () => this.menuItemSelectionListener.getSnapshot(),
|
|
3194
|
+
menuItems: () => this.menuItemListener.getSnapshot(),
|
|
3195
|
+
menuItemsVisible: () => this.menuItemVisibleListener.getSnapshot()
|
|
3196
|
+
};
|
|
3197
|
+
}
|
|
3198
|
+
/**
|
|
3199
|
+
* Remove all listeners and reset their class members
|
|
3200
|
+
*/
|
|
3201
|
+
cleanupListeners() {
|
|
3202
|
+
this.menuConfigListener.cleanupListeners();
|
|
3203
|
+
this.menuItemListener.cleanupListeners();
|
|
3204
|
+
this.menuItemSelectionListener.cleanupListeners();
|
|
3205
|
+
}
|
|
3206
|
+
/**
|
|
3207
|
+
* Start the resource
|
|
3208
|
+
*/
|
|
3209
|
+
async start() {
|
|
3210
|
+
const dappArchivist = await this.getDappArchivist();
|
|
3211
|
+
const dappArchivistPayloadDiviner = await this.getDappArchivistPayloadDiviner();
|
|
3212
|
+
this._menuConfigListener = await PayloadStore.create({
|
|
3213
|
+
archivist: dappArchivist,
|
|
3214
|
+
getLatest: async () => await DappMenuQueries.getMenuConfigs(dappArchivistPayloadDiviner),
|
|
3215
|
+
idFunction: isDappNavMenuConfig
|
|
3216
|
+
});
|
|
3217
|
+
this._menuItemListener = await PayloadStore.create({
|
|
3218
|
+
archivist: dappArchivist,
|
|
3219
|
+
getLatest: async () => await DappMenuQueries.getMenuItems(dappArchivistPayloadDiviner),
|
|
3220
|
+
idFunction: isDappNavMenuConfig
|
|
3221
|
+
});
|
|
3222
|
+
this._menuItemVisibleListener = await PayloadStore.create({
|
|
3223
|
+
archivist: dappArchivist,
|
|
3224
|
+
getLatest: async () => await DappMenuQueries.getVisibleMenuItems(dappArchivistPayloadDiviner),
|
|
3225
|
+
idFunction: isDappNavMenuConfig
|
|
3226
|
+
});
|
|
3227
|
+
this._menuItemSelectionListener = await PayloadStore.create({
|
|
3228
|
+
archivist: dappArchivist,
|
|
3229
|
+
getLatest: async () => await DappMenuQueries.getNavItemSelections(dappArchivistPayloadDiviner),
|
|
3230
|
+
idFunction: isDappNavItemSelection
|
|
3231
|
+
});
|
|
3232
|
+
}
|
|
3233
|
+
};
|
|
3234
|
+
|
|
3235
|
+
// src/helpers/monitor/XyOsMonitor.ts
|
|
3236
|
+
var DEFAULT_MONITORING_EVENT_NAME = "System Task Duration";
|
|
3237
|
+
globalThis.getXyOsGlobal = globalThis.getXyOsGlobal ?? (() => {
|
|
3238
|
+
return {
|
|
3239
|
+
tracking: {
|
|
3240
|
+
trackEvent: (_event, _properties) => void 0
|
|
3241
|
+
}
|
|
3242
|
+
};
|
|
3243
|
+
});
|
|
3244
|
+
var xyOsGlobal = globalThis.getXyOsGlobal();
|
|
3245
|
+
var XyOsMonitor = class _XyOsMonitor {
|
|
3246
|
+
static trackEvent = xyOsGlobal.tracking.trackEvent;
|
|
3247
|
+
performanceTimers = /* @__PURE__ */ new Map();
|
|
3248
|
+
startTimer(name) {
|
|
3249
|
+
this.performanceTimers.set(name, performance.now());
|
|
3250
|
+
}
|
|
3251
|
+
stopTimer(eventConfig) {
|
|
3252
|
+
const { additionalProperties, name } = eventConfig;
|
|
3253
|
+
const start = this.performanceTimers.get(name);
|
|
3254
|
+
if (start) {
|
|
3255
|
+
const end = performance.now();
|
|
3256
|
+
const duration = end - start;
|
|
3257
|
+
this.performanceTimers.delete(name);
|
|
3258
|
+
const connection = this.connection();
|
|
3259
|
+
_XyOsMonitor.trackEvent(DEFAULT_MONITORING_EVENT_NAME, {
|
|
3260
|
+
downloadSpeedMbps: connection?.downlink,
|
|
3261
|
+
duration: duration.toString(),
|
|
3262
|
+
effectiveConnectionSpeed: connection?.effectiveType,
|
|
3263
|
+
name,
|
|
3264
|
+
roundTripTime: connection?.rtt,
|
|
3265
|
+
...additionalProperties
|
|
3266
|
+
});
|
|
3267
|
+
}
|
|
3268
|
+
}
|
|
3269
|
+
};
|
|
3270
|
+
|
|
3271
|
+
// src/profileModuleEvents.ts
|
|
3272
|
+
import { PayloadBuilder as PayloadBuilder10 } from "@xyo-network/payload-builder";
|
|
3273
|
+
var DISPLAY_EVENT_COUNT_FREQUENCY = 100;
|
|
3274
|
+
var profileData = {};
|
|
3275
|
+
var profileModuleEvents = (mod) => {
|
|
3276
|
+
const modRef = new WeakRef(mod);
|
|
3277
|
+
mod.onAny(async (eventName, args) => {
|
|
3278
|
+
const mod2 = modRef.deref();
|
|
3279
|
+
if (mod2) {
|
|
3280
|
+
const data = profileData[mod2.address] ?? { allEventCount: 0, eventCounts: {} };
|
|
3281
|
+
profileData[mod2.address] = data;
|
|
3282
|
+
data.allEventCount += 1;
|
|
3283
|
+
data.eventCounts[eventName] = (data.eventCounts[eventName] ?? 0) + 1;
|
|
3284
|
+
if (eventName === "querySendStarted") {
|
|
3285
|
+
const { query, payloads = [] } = args;
|
|
3286
|
+
const pairs = await PayloadBuilder10.dataHashPairs(payloads);
|
|
3287
|
+
const foundPair = pairs.find(([, hash]) => hash === query.query);
|
|
3288
|
+
if (foundPair) {
|
|
3289
|
+
const [payload] = foundPair;
|
|
3290
|
+
console.log(`querySendStarted: ${payload.schema}`);
|
|
3291
|
+
}
|
|
3292
|
+
}
|
|
3293
|
+
if (eventName === "querySendFinished") {
|
|
3294
|
+
const { query } = args;
|
|
3295
|
+
console.log(`querySendFinished: ${query.query}`);
|
|
3296
|
+
}
|
|
3297
|
+
if (data.allEventCount % DISPLAY_EVENT_COUNT_FREQUENCY === 0) {
|
|
3298
|
+
console.log(`[PROFILE] - ${mod2.id} Event Counts:`);
|
|
3299
|
+
console.log(data.eventCounts);
|
|
3300
|
+
}
|
|
3301
|
+
}
|
|
3302
|
+
});
|
|
3303
|
+
};
|
|
3304
|
+
|
|
3305
|
+
// src/PubSubBridgeCaller.ts
|
|
3306
|
+
import { assertEx as assertEx28 } from "@xylabs/assert";
|
|
3307
|
+
import { asBridgeInstance } from "@xyo-network/bridge-model";
|
|
3308
|
+
import { BridgedPubSubModuleName } from "@xyo-network/os-model";
|
|
3309
|
+
var PubSubBridgeCaller = class {
|
|
3310
|
+
constructor(node) {
|
|
3311
|
+
this.node = node;
|
|
3312
|
+
}
|
|
3313
|
+
async exposeAddresses(addresses, options) {
|
|
3314
|
+
const pubSubBridge = await this.getPubSubBridge();
|
|
3315
|
+
for (const address of addresses) {
|
|
3316
|
+
console.log(`Exposing: ${address}`);
|
|
3317
|
+
await pubSubBridge.expose(address, options);
|
|
3318
|
+
}
|
|
3319
|
+
}
|
|
3320
|
+
async unExposeAddresses(addresses) {
|
|
3321
|
+
const pubSubBridge = await this.getPubSubBridge();
|
|
3322
|
+
for (const address of addresses) {
|
|
3323
|
+
await pubSubBridge.unexpose?.(address);
|
|
3324
|
+
}
|
|
3325
|
+
}
|
|
3326
|
+
async getPubSubBridge() {
|
|
3327
|
+
const mod = assertEx28(await this.node.resolve(BridgedPubSubModuleName), () => `${BridgedPubSubModuleName} not found`);
|
|
3328
|
+
return asBridgeInstance(mod, () => `${BridgedPubSubModuleName} is not a bridge`);
|
|
3329
|
+
}
|
|
3330
|
+
};
|
|
3331
|
+
export {
|
|
3332
|
+
AbstractXnsCaller,
|
|
3333
|
+
AccessNodeQueries,
|
|
3334
|
+
DappAccessCaller,
|
|
3335
|
+
DappAccessRequestEvent,
|
|
3336
|
+
DappAccessRequestQueries,
|
|
3337
|
+
DappAccessRequestResource,
|
|
3338
|
+
DappArchivistModuleName,
|
|
3339
|
+
DappArchivistPayloadDivinerModuleName,
|
|
3340
|
+
DappCaller,
|
|
3341
|
+
DappCallerBase,
|
|
3342
|
+
DappContextCreator,
|
|
3343
|
+
DappDefaultsResource,
|
|
3344
|
+
DappDefaultsResourceQueries,
|
|
3345
|
+
DappIntentCaller,
|
|
3346
|
+
DappIntentResource,
|
|
3347
|
+
DappMenuCaller,
|
|
3348
|
+
DappMenuResource,
|
|
3349
|
+
DappRegistrationService,
|
|
3350
|
+
DappRegistry,
|
|
3351
|
+
DappSeedPhraseRepository,
|
|
3352
|
+
DappsArchivistDevelopmentModuleName,
|
|
3353
|
+
DappsArchivistModuleName,
|
|
3354
|
+
DappsArchivistPayloadDevelopmentDivinerModuleName,
|
|
3355
|
+
DappsArchivistPayloadDivinerModuleName,
|
|
3356
|
+
DevelopArchivist,
|
|
3357
|
+
EventBus,
|
|
3358
|
+
EventBusConnection,
|
|
3359
|
+
EventBusPubSubConnection,
|
|
3360
|
+
ExposeDappRequestEvent,
|
|
3361
|
+
ExternalModulePermissions,
|
|
3362
|
+
IntentArchivistModuleName,
|
|
3363
|
+
IntentArchivistPayloadDivinerModuleName,
|
|
3364
|
+
ManageSystemDapps,
|
|
3365
|
+
ManifestReplaceableTokens,
|
|
3366
|
+
ModuleOffsetPaths,
|
|
3367
|
+
NameTransforms,
|
|
3368
|
+
NetworkAdapter,
|
|
3369
|
+
NodeSentinel,
|
|
3370
|
+
OsBadgeCaller,
|
|
3371
|
+
OsBadgeResource,
|
|
3372
|
+
OsCallerBase,
|
|
3373
|
+
OsPubSubBridgeNetworkAdapters,
|
|
3374
|
+
OsPubSubNetworkReadyEvent,
|
|
3375
|
+
OsPubSubNetworkStack,
|
|
3376
|
+
OsSettingsAdapters,
|
|
3377
|
+
OsSettingsCaller,
|
|
3378
|
+
OsSettingsCallerBase,
|
|
3379
|
+
OsSettingsQueries,
|
|
3380
|
+
OsSettingsReadyEvent,
|
|
3381
|
+
OsSettingsResource,
|
|
3382
|
+
OsSettingsStack,
|
|
3383
|
+
OsStackMap,
|
|
3384
|
+
OsXyoPublicNetworkAdapters,
|
|
3385
|
+
OsXyoPublicNetworkReadyEvent,
|
|
3386
|
+
PayloadStore,
|
|
3387
|
+
PubSubBridgeCaller,
|
|
3388
|
+
RegisteredDappInterfacesArchivistModuleName,
|
|
3389
|
+
RemoteNodeArchivistOffsetPaths,
|
|
3390
|
+
ReplaceManifestTokens,
|
|
3391
|
+
RunningDappAccessCache,
|
|
3392
|
+
RunningDappCache,
|
|
3393
|
+
SettingsAdapter,
|
|
3394
|
+
StackBase,
|
|
3395
|
+
StackManager,
|
|
3396
|
+
ValidDappAccessInterfaces,
|
|
3397
|
+
XnsArchivistModuleName,
|
|
3398
|
+
XnsArchivistPayloadDivinerModuleName,
|
|
3399
|
+
XnsRegistrationsArchivistModuleName,
|
|
3400
|
+
XnsRegistrationsResource,
|
|
3401
|
+
XnsRegistrationsResourceQueries,
|
|
3402
|
+
XyOs,
|
|
3403
|
+
XyOsContextBase,
|
|
3404
|
+
XyOsDapp,
|
|
3405
|
+
XyOsMonitor,
|
|
3406
|
+
XyoPublicNetworkStack,
|
|
3407
|
+
asPayload,
|
|
3408
|
+
buildUserWalletSeedPhrasePayload,
|
|
3409
|
+
buildWalletSeedPhrasePayload,
|
|
3410
|
+
createDappContext,
|
|
3411
|
+
createInsertable,
|
|
3412
|
+
dappAccessRequestConnection,
|
|
3413
|
+
dappsReadyConnection,
|
|
3414
|
+
exposeDappRequestConnection,
|
|
3415
|
+
getApiDomain,
|
|
3416
|
+
initializeXns,
|
|
3417
|
+
isAnyPayload,
|
|
3418
|
+
isObject,
|
|
3419
|
+
isPayload,
|
|
3420
|
+
loadOsNode,
|
|
3421
|
+
os_node_manifest_default as osNodeManifest,
|
|
3422
|
+
osPubSubNetworkReadyConnection,
|
|
3423
|
+
osSettingsReadyConnection,
|
|
3424
|
+
osXyoPublicNetworkReadyConnection,
|
|
3425
|
+
profileData,
|
|
3426
|
+
profileModuleEvents,
|
|
3427
|
+
renameObjKey,
|
|
3428
|
+
tokenPlacesSplit,
|
|
3429
|
+
tokenPlacesSplitString,
|
|
3430
|
+
userWalletLabel
|
|
3431
|
+
};
|
|
3432
|
+
//# sourceMappingURL=index.mjs.map
|