@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.
Files changed (132) hide show
  1. package/LICENSE +165 -0
  2. package/README.md +13 -0
  3. package/dist/neutral/index.d.ts +1656 -0
  4. package/dist/neutral/index.mjs +3432 -0
  5. package/dist/neutral/index.mjs.map +1 -0
  6. package/package.json +78 -0
  7. package/src/Caller.ts +221 -0
  8. package/src/DappCallerBase.ts +52 -0
  9. package/src/DefaultsQueries.ts +27 -0
  10. package/src/OsCallerBase.ts +55 -0
  11. package/src/PubSubBridgeCaller.ts +30 -0
  12. package/src/XyOs.ts +125 -0
  13. package/src/XyOsBase.ts +119 -0
  14. package/src/XyOsDapp.ts +90 -0
  15. package/src/access-interfaces/ValidDappAccessInterfaces.ts +8 -0
  16. package/src/access-interfaces/index.ts +2 -0
  17. package/src/access-interfaces/registered-names/helpers/AccessNodeQueries.ts +59 -0
  18. package/src/access-interfaces/registered-names/helpers/index.ts +2 -0
  19. package/src/access-interfaces/registered-names/helpers/resource/AbstractXnsCaller.ts +38 -0
  20. package/src/access-interfaces/registered-names/helpers/resource/RegistrationsResource.ts +54 -0
  21. package/src/access-interfaces/registered-names/helpers/resource/RegistrationsResourceQueries.ts +22 -0
  22. package/src/access-interfaces/registered-names/helpers/resource/index.ts +3 -0
  23. package/src/access-interfaces/registered-names/index.ts +1 -0
  24. package/src/adapter/Base.ts +96 -0
  25. package/src/adapter/Network.ts +31 -0
  26. package/src/adapter/Settings.ts +30 -0
  27. package/src/adapter/index.ts +2 -0
  28. package/src/adapters/OsPubSubBridgeNetwork.ts +10 -0
  29. package/src/adapters/OsSettings.ts +7 -0
  30. package/src/adapters/OsXyoPublicNetwork.ts +9 -0
  31. package/src/adapters/index.ts +3 -0
  32. package/src/classes/cache/RunningAccessDappCache.ts +21 -0
  33. package/src/classes/cache/RunningDappCache.ts +50 -0
  34. package/src/classes/cache/index.ts +2 -0
  35. package/src/classes/dapp/DefaultsResource.ts +65 -0
  36. package/src/classes/dapp/access/Caller.ts +73 -0
  37. package/src/classes/dapp/access/Queries.ts +38 -0
  38. package/src/classes/dapp/access/Resource.ts +63 -0
  39. package/src/classes/dapp/access/index.ts +3 -0
  40. package/src/classes/dapp/index.ts +2 -0
  41. package/src/classes/index.ts +8 -0
  42. package/src/classes/lib/DappCreatorParams.ts +16 -0
  43. package/src/classes/lib/Insertable.ts +14 -0
  44. package/src/classes/lib/index.ts +2 -0
  45. package/src/classes/menu/Caller.ts +76 -0
  46. package/src/classes/menu/Queries.ts +59 -0
  47. package/src/classes/menu/Resource.ts +103 -0
  48. package/src/classes/menu/index.ts +2 -0
  49. package/src/classes/node/Creator.ts +96 -0
  50. package/src/classes/node/DefaultPayloads/DappAccessPayloads.ts +17 -0
  51. package/src/classes/node/DefaultPayloads/DefaultPayloads.ts +33 -0
  52. package/src/classes/node/DefaultPayloads/NodeInfoPayload.ts +27 -0
  53. package/src/classes/node/DefaultPayloads/SigningKeyPayloads.ts +85 -0
  54. package/src/classes/node/DefaultPayloads/index.ts +1 -0
  55. package/src/classes/node/ExternalModulePermissions/ExternalModulePermissions.ts +47 -0
  56. package/src/classes/node/ExternalModulePermissions/index.ts +1 -0
  57. package/src/classes/node/createDappContext.ts +52 -0
  58. package/src/classes/node/index.ts +3 -0
  59. package/src/classes/registration/DappRegistrationService.ts +146 -0
  60. package/src/classes/registration/DappRegistry.ts +121 -0
  61. package/src/classes/registration/ValidateDappAccessDiviner/Config.ts +19 -0
  62. package/src/classes/registration/ValidateDappAccessDiviner/Diviner.ts +132 -0
  63. package/src/classes/registration/ValidateDappAccessDiviner/index.ts +2 -0
  64. package/src/classes/registration/index.ts +2 -0
  65. package/src/classes/settings/Caller.ts +76 -0
  66. package/src/classes/settings/CallerBase.ts +24 -0
  67. package/src/classes/settings/Resource.ts +79 -0
  68. package/src/classes/settings/SettingsQueries.ts +38 -0
  69. package/src/classes/settings/badge/Caller.ts +30 -0
  70. package/src/classes/settings/badge/Queries.ts +18 -0
  71. package/src/classes/settings/badge/Resource.ts +54 -0
  72. package/src/classes/settings/badge/index.ts +2 -0
  73. package/src/classes/settings/index.ts +5 -0
  74. package/src/classes/system/ManageSystemDapps.ts +131 -0
  75. package/src/classes/system/Queries.ts +69 -0
  76. package/src/classes/system/index.ts +1 -0
  77. package/src/event/bus/Connection.ts +31 -0
  78. package/src/event/bus/EventBus.ts +145 -0
  79. package/src/event/bus/PubSubConnection.ts +28 -0
  80. package/src/event/bus/index.ts +3 -0
  81. package/src/event/connections/DappAccessRequest.ts +11 -0
  82. package/src/event/connections/DappsReady.ts +9 -0
  83. package/src/event/connections/ExposeDappRequest.ts +11 -0
  84. package/src/event/connections/OsPubSubNetworkReady.ts +12 -0
  85. package/src/event/connections/OsSettingsReady.ts +12 -0
  86. package/src/event/connections/OsXyoPublicReady.ts +12 -0
  87. package/src/event/connections/index.ts +6 -0
  88. package/src/event/index.ts +2 -0
  89. package/src/helpers/index.ts +1 -0
  90. package/src/helpers/monitor/XyOsMonitor.ts +52 -0
  91. package/src/helpers/monitor/index.ts +2 -0
  92. package/src/helpers/monitor/types.ts +5 -0
  93. package/src/index.ts +22 -0
  94. package/src/intent/Caller.ts +72 -0
  95. package/src/intent/Resource.ts +66 -0
  96. package/src/intent/index.ts +2 -0
  97. package/src/lib/ExternalStore.ts +7 -0
  98. package/src/lib/Listener.ts +1 -0
  99. package/src/lib/ModuleAccountPaths.ts +29 -0
  100. package/src/lib/ModuleNames.ts +3 -0
  101. package/src/lib/NameTransforms.ts +31 -0
  102. package/src/lib/PayloadStore.ts +98 -0
  103. package/src/lib/ResourceStores.ts +7 -0
  104. package/src/lib/index.ts +10 -0
  105. package/src/lib/initializeXns.ts +16 -0
  106. package/src/lib/isPayload.ts +24 -0
  107. package/src/lib/tokenPlacesSplit.ts +17 -0
  108. package/src/loadOsNode.ts +43 -0
  109. package/src/manifest/ManifestReplaceableTokens.ts +17 -0
  110. package/src/manifest/index.ts +2 -0
  111. package/src/manifest/os-node.manifest.json +132 -0
  112. package/src/manifests/dapp-window.manifest.json +36 -0
  113. package/src/manifests/index.ts +1 -0
  114. package/src/profileModuleEvents.ts +43 -0
  115. package/src/stack/Base.ts +153 -0
  116. package/src/stack/Manager.ts +48 -0
  117. package/src/stack/Map.ts +20 -0
  118. package/src/stack/OsPubSubNetworkStack.ts +70 -0
  119. package/src/stack/OsSettingsStack.ts +24 -0
  120. package/src/stack/XyoPublicNetworkStack.ts +32 -0
  121. package/src/stack/index.ts +6 -0
  122. package/src/types/global.d.ts +9 -0
  123. package/src/types/images.d.ts +5 -0
  124. package/src/utils/buildWalletSeedPhrasePayload.ts +41 -0
  125. package/src/utils/getApiDomain.ts +14 -0
  126. package/src/utils/index.ts +3 -0
  127. package/src/utils/renameObjKey.ts +22 -0
  128. package/src/wallet/DappSeedPhraseRepository.ts +95 -0
  129. package/src/wallet/SeedPhraseRepository.ts +32 -0
  130. package/src/wallet/index.ts +2 -0
  131. package/typedoc.json +5 -0
  132. package/xy.config.ts +10 -0
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@xyo-network/os-runtime",
3
+ "author": {
4
+ "email": "support@xyo.network",
5
+ "name": "XYO Development Team",
6
+ "url": "https://xyo.network"
7
+ },
8
+ "bugs": {
9
+ "email": "support@xyo.network",
10
+ "url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues"
11
+ },
12
+ "dependencies": {
13
+ "@xylabs/assert": "^4.0.2",
14
+ "@xylabs/forget": "^4.0.2",
15
+ "@xylabs/hex": "^4.0.2",
16
+ "@xylabs/lodash": "^4.0.2",
17
+ "@xylabs/object": "^4.0.2",
18
+ "@xylabs/promise": "^4.0.2",
19
+ "@xyo-network/account": "^3.0.3",
20
+ "@xyo-network/archivist": "^3.0.3",
21
+ "@xyo-network/archivist-indexeddb": "^3.0.3",
22
+ "@xyo-network/archivist-model": "^3.0.3",
23
+ "@xyo-network/bridge-model": "^3.0.3",
24
+ "@xyo-network/diviner-abstract": "^3.0.3",
25
+ "@xyo-network/diviner-model": "^3.0.3",
26
+ "@xyo-network/diviner-payload-generic": "^3.0.3",
27
+ "@xyo-network/diviner-payload-indexeddb": "^3.0.3",
28
+ "@xyo-network/diviner-payload-model": "^3.0.3",
29
+ "@xyo-network/diviner-temporal-indexing-model": "^3.0.3",
30
+ "@xyo-network/kernel": "^3.0.0",
31
+ "@xyo-network/kernel-model": "^3.0.0",
32
+ "@xyo-network/manifest": "^3.0.3",
33
+ "@xyo-network/manifest-wrapper": "^3.0.3",
34
+ "@xyo-network/module-abstract": "^3.0.3",
35
+ "@xyo-network/module-events": "^3.0.3",
36
+ "@xyo-network/module-factory-locator": "^3.0.3",
37
+ "@xyo-network/module-model": "^3.0.3",
38
+ "@xyo-network/module-resolver": "^3.0.3",
39
+ "@xyo-network/node-model": "^3.0.3",
40
+ "@xyo-network/os-model": "^3.0.0",
41
+ "@xyo-network/payload-builder": "^3.0.3",
42
+ "@xyo-network/payload-model": "^3.0.3",
43
+ "@xyo-network/wallet-model": "^3.0.3",
44
+ "async-mutex": "^0.5.0",
45
+ "ethers": "^6.13.2",
46
+ "semver": "^7.6.3",
47
+ "uuid": "^10.0.0"
48
+ },
49
+ "devDependencies": {
50
+ "@types/semver": "^7.5.8",
51
+ "@types/uuid": "^10.0.0",
52
+ "@xylabs/ts-scripts-yarn3": "^4.0.0-rc.20",
53
+ "@xylabs/tsconfig": "^4.0.0-rc.20",
54
+ "typescript": "^5.5.4"
55
+ },
56
+ "description": "Primary SDK for using XYO Protocol 2.0",
57
+ "exports": {
58
+ ".": {
59
+ "types": "./dist/neutral/index.d.ts",
60
+ "default": "./dist/neutral/index.mjs"
61
+ },
62
+ "./package.json": "./package.json"
63
+ },
64
+ "module": "dist/neutral/index.mjs",
65
+ "types": "dist/neutral/index.d.ts",
66
+ "homepage": "https://xyo.network",
67
+ "license": "LGPL-3.0-only",
68
+ "publishConfig": {
69
+ "access": "public"
70
+ },
71
+ "repository": {
72
+ "type": "git",
73
+ "url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
74
+ },
75
+ "sideEffects": false,
76
+ "version": "3.0.0",
77
+ "type": "module"
78
+ }
package/src/Caller.ts ADDED
@@ -0,0 +1,221 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import type { Hash } from '@xylabs/hex'
3
+ import type { AccountInstance } from '@xyo-network/account'
4
+ import { Account } from '@xyo-network/account'
5
+ import type { PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'
6
+ import { PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'
7
+ import type {
8
+ DappAccessRequest,
9
+ DappId,
10
+ DappInjectableParams,
11
+ DappIntent,
12
+ RegisteredDappAccess,
13
+ SigningKey,
14
+ SigningKeyFields,
15
+ XyOsContext } from '@xyo-network/os-model'
16
+ import {
17
+ DappAccessRequestSchema,
18
+ DappInjectableParamsSchema,
19
+ DappIntentTypes,
20
+ DappMode,
21
+ RegisteredDappAccessSchema,
22
+ SigningKeySchema,
23
+ } from '@xyo-network/os-model'
24
+ import { PayloadBuilder } from '@xyo-network/payload-builder'
25
+ import type { Payload, WithMeta } from '@xyo-network/payload-model'
26
+
27
+ import { DappCallerBase } from './DappCallerBase.ts'
28
+ import { DappDefaultsResourceQueries } from './DefaultsQueries.ts'
29
+ import { DappIntentCaller } from './intent/index.ts'
30
+
31
+ export class DappCaller extends DappCallerBase {
32
+ constructor(context: XyOsContext) {
33
+ super(context)
34
+ }
35
+
36
+ /**
37
+ * Add payloads to the dapp archivist
38
+ * @param {Payload[]} payloads
39
+ * @param {Boolean} force
40
+ */
41
+ async addPayloadsToDappArchivist(payloads: Payload[] = [], force: boolean = false) {
42
+ const dappArchivist = await this.getDappArchivist()
43
+ if (force) {
44
+ await dappArchivist.insert(payloads)
45
+ } else {
46
+ for (const payload of payloads) {
47
+ const hash = await PayloadBuilder.dataHash(payload)
48
+ const [existing] = await dappArchivist.get([hash])
49
+ if (existing) continue
50
+ await dappArchivist.insert([payload])
51
+ }
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Build Access Request
57
+ * @param {DappAccessRequest['mode']} mode
58
+ * @param {Hash} registeredDappAccessHash
59
+ * @param {string} requestingDappId
60
+ * @param {string | undefined} destinationArchivist
61
+ */
62
+ buildAccessRequest(mode: DappAccessRequest['mode'], registeredDappAccessHash: Hash, requestingDappId: string, destinationArchivist?: string) {
63
+ const accessRequest: DappAccessRequest = {
64
+ destinationArchivist,
65
+ mode,
66
+ registeredDappAccessHash,
67
+ requestingDappId,
68
+ schema: DappAccessRequestSchema,
69
+ timestamp: Date.now(),
70
+ }
71
+ return accessRequest
72
+ }
73
+
74
+ /**
75
+ * Get all available dapp access requests
76
+ */
77
+ async getDappAccessPayloads() {
78
+ const diviner = await this.getDappArchivistPayloadDiviner()
79
+ const query: PayloadDivinerQueryPayload = {
80
+ order: 'desc',
81
+ schema: PayloadDivinerQuerySchema,
82
+ schemas: [RegisteredDappAccessSchema],
83
+ }
84
+
85
+ return diviner.divine([query])
86
+ }
87
+
88
+ /**
89
+ * Get the most recent dapp injectable params
90
+ * @returns Promise
91
+ */
92
+ async getDappInjectableParams(): Promise<DappInjectableParams[] | undefined> {
93
+ const diviner = await this.getDappArchivistPayloadDiviner()
94
+
95
+ return await DappDefaultsResourceQueries.getDappInjectableParamsFromRoute(diviner)
96
+ }
97
+
98
+ /**
99
+ * Get the most recent dapp intent
100
+ * @param {string} name
101
+ */
102
+ async getRegisteredDappAccessByName(name: string) {
103
+ const diviner = await this.getDappArchivistPayloadDiviner()
104
+ const query = {
105
+ limit: 1,
106
+ name,
107
+ schema: PayloadDivinerQuerySchema,
108
+ schemas: [RegisteredDappAccessSchema],
109
+ }
110
+
111
+ const [payload] = assertEx(await diviner.divine([query]), () => 'Unable to find registered dapp access') as WithMeta<RegisteredDappAccess>[]
112
+ return payload
113
+ }
114
+
115
+ /**
116
+ * Get the user wallet
117
+ */
118
+ async getUserAccount(dappName: string): Promise<AccountInstance> {
119
+ const diviner = await this.getDappArchivistPayloadDiviner()
120
+ const query: PayloadDivinerQueryPayload<SigningKeyFields> = {
121
+ dappName,
122
+ limit: 1,
123
+ order: 'desc',
124
+ schema: PayloadDivinerQuerySchema,
125
+ schemas: [SigningKeySchema],
126
+ }
127
+ const [payload] = assertEx(await diviner.divine([query]), () => 'Unable to find user wallet') as WithMeta<SigningKey>[]
128
+ if (payload) {
129
+ return await Account.fromPrivateKey(payload.privateKey)
130
+ } else {
131
+ throw new Error('Unable to find user wallet')
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Get all wallets for the user
137
+ */
138
+ async getUserAccountsAll(): Promise<SigningKey[]> {
139
+ const diviner = await this.getDappArchivistPayloadDiviner()
140
+ const query: PayloadDivinerQueryPayload<SigningKeyFields> = {
141
+ order: 'desc',
142
+ schema: PayloadDivinerQuerySchema,
143
+ schemas: [SigningKeySchema],
144
+ }
145
+ const payloads = assertEx(await diviner.divine([query]), () => 'Unable to find user wallet') as WithMeta<SigningKey>[]
146
+ if (payloads?.length) {
147
+ return payloads
148
+ } else {
149
+ throw new Error('Unable to find user wallet')
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Make Dapp Access Request
155
+ * @param {string} requestingDappId
156
+ * @param {string} registeredDappAccessHash
157
+ * @param {string} mode
158
+ * @param {string} destinationArchivist
159
+ */
160
+ async makeDappAccessRequest(
161
+ requestingDappId: DappAccessRequest['requestingDappId'],
162
+ registeredDappAccessHash: DappAccessRequest['registeredDappAccessHash'],
163
+ mode?: DappAccessRequest['mode'],
164
+ destinationArchivist?: DappAccessRequest['destinationArchivist'],
165
+ ) {
166
+ const archivist = await this.getDappArchivist()
167
+ const registeredDappAccessPayload = await archivist.get([registeredDappAccessHash])
168
+ if (!registeredDappAccessPayload) {
169
+ throw new Error('No registered dapp access found')
170
+ }
171
+ const accessRequest = this.buildAccessRequest(mode, registeredDappAccessHash, requestingDappId, destinationArchivist)
172
+ await archivist.insert([accessRequest])
173
+ }
174
+
175
+ /**
176
+ * Make Dapp Intent Request
177
+ * @param {DappId} sourceDappId
178
+ * @param {DappId} targetDappId
179
+ * @param {DappIntentTypes=DappIntentTypes.Launch} intent
180
+ * @param {DappIntent['params']} mode
181
+ */
182
+ async makeDappIntentRequest(
183
+ sourceDappId: DappId,
184
+ targetDappId: DappId,
185
+ intent: DappIntentTypes = DappIntentTypes.Launch,
186
+ mode: DappMode = DappMode.Window,
187
+ params?: DappIntent['params'],
188
+ ) {
189
+ if (!sourceDappId) {
190
+ throw new Error('No source dapp id provided, dApps must declare a sourceDappId')
191
+ }
192
+ const archivist = await this.getDappArchivist()
193
+ const dappIntent: DappIntent = DappIntentCaller.buildIntent(sourceDappId, intent, targetDappId, mode, params)
194
+ return await archivist.insert([dappIntent])
195
+ }
196
+
197
+ /**
198
+ * Set the dapp injectable params
199
+ *
200
+ * @param {URLSearchParams} searchParams
201
+ * @param {string} pathname
202
+ */
203
+ async setDappInjectableRouteParams(searchParams: URLSearchParams = new URLSearchParams(), pathname: string): Promise<void> {
204
+ const dappArchivist = await this.getDappArchivist()
205
+
206
+ const params: DappInjectableParams['params'] = {}
207
+ for (const [key, value] of searchParams.entries()) {
208
+ params[key] = value
209
+ }
210
+
211
+ const payload: DappInjectableParams = {
212
+ params,
213
+ path: pathname,
214
+ schema: DappInjectableParamsSchema,
215
+ source: 'route',
216
+ timestamp: Date.now(),
217
+ }
218
+
219
+ await dappArchivist.insert([payload])
220
+ }
221
+ }
@@ -0,0 +1,52 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import { toJsonString } from '@xylabs/object'
3
+ import { asArchivistInstance } from '@xyo-network/archivist-model'
4
+ import { asDivinerInstance } from '@xyo-network/diviner-model'
5
+ import type { XyOsContext } from '@xyo-network/os-model'
6
+
7
+ export const DappArchivistModuleName = 'DappArchivist'
8
+ export const IntentArchivistModuleName = 'IntentArchivist'
9
+ export const IntentArchivistPayloadDivinerModuleName = 'IntentArchivistPayloadDiviner'
10
+ export const DappArchivistPayloadDivinerModuleName = 'DappArchivistPayloadDiviner'
11
+
12
+ export class DappCallerBase {
13
+ constructor(protected context: XyOsContext) {}
14
+ /**
15
+ * Get the DappArchivist
16
+ */
17
+ async getDappArchivist() {
18
+ const mod = assertEx(
19
+ await this.context.root.resolve(DappArchivistModuleName),
20
+ () => `${DappArchivistModuleName} not found [${toJsonString(this.context.root.publicChildren())}]`,
21
+ )
22
+ return asArchivistInstance(mod, () => `${DappArchivistModuleName} is not an archivist`)
23
+ }
24
+
25
+ /**
26
+ * Get the DappArchivistPayloadDiviner
27
+ */
28
+ async getDappArchivistPayloadDiviner() {
29
+ const mod = assertEx(
30
+ await this.context.root.resolve(DappArchivistPayloadDivinerModuleName),
31
+ () => `${DappArchivistPayloadDivinerModuleName} not found`,
32
+ )
33
+ return asDivinerInstance(mod, () => `${DappArchivistPayloadDivinerModuleName} is not a diviner`)
34
+ }
35
+
36
+ /**
37
+ * Get the IntentArchivist
38
+ */
39
+ async getDappIntentArchivist() {
40
+ const mod = assertEx(await this.context.root.resolve(IntentArchivistModuleName), () => `${IntentArchivistModuleName} not found`)
41
+ return asArchivistInstance(mod, () => `${IntentArchivistModuleName} is not an archivist`)
42
+ }
43
+
44
+ // Get the IntentArchivistPayloadDiviner
45
+ async getDappIntentArchivistPayloadDiviner() {
46
+ const mod = assertEx(
47
+ await this.context.root.resolve(IntentArchivistPayloadDivinerModuleName),
48
+ () => `${IntentArchivistPayloadDivinerModuleName} not found`,
49
+ )
50
+ return asDivinerInstance(mod, () => `${IntentArchivistPayloadDivinerModuleName} is not a diviner`)
51
+ }
52
+ }
@@ -0,0 +1,27 @@
1
+ import type { DivinerInstance } from '@xyo-network/diviner-model'
2
+ import { PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'
3
+ import type { DappInjectableParams, NodeOsInfo } from '@xyo-network/os-model'
4
+ import { DappInjectableParamsSchema, NodeOsInfoSchema } from '@xyo-network/os-model'
5
+ import type { WithMeta } from '@xyo-network/payload-model'
6
+
7
+ export const DappDefaultsResourceQueries = {
8
+ async getDappInjectableParamsFromRoute(diviner: DivinerInstance) {
9
+ const query = {
10
+ limit: 1,
11
+ order: 'desc',
12
+ schema: PayloadDivinerQuerySchema,
13
+ schemas: [DappInjectableParamsSchema],
14
+ source: 'route',
15
+ }
16
+ return (await diviner.divine([query])) as WithMeta<DappInjectableParams>[]
17
+ },
18
+ async getNodeOsInfo(diviner: DivinerInstance) {
19
+ const query = {
20
+ limit: 1,
21
+ order: 'desc',
22
+ schema: PayloadDivinerQuerySchema,
23
+ schemas: [NodeOsInfoSchema],
24
+ }
25
+ return (await diviner.divine([query])) as WithMeta<NodeOsInfo>[]
26
+ },
27
+ }
@@ -0,0 +1,55 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import { asArchivistInstance } from '@xyo-network/archivist-model'
3
+ import { asDivinerInstance } from '@xyo-network/diviner-model'
4
+ import type { XyOsContext } from '@xyo-network/os-model'
5
+
6
+ export const DappsArchivistModuleName = 'DappsArchivist'
7
+ export const DappsArchivistDevelopmentModuleName = `${DappsArchivistModuleName}Development`
8
+ export const DappsArchivistPayloadDivinerModuleName = 'DappsArchivistPayloadDiviner'
9
+ export const DappsArchivistPayloadDevelopmentDivinerModuleName = `${DappsArchivistPayloadDivinerModuleName}Development`
10
+ export const RegisteredDappInterfacesArchivistModuleName = 'RegisteredDappInterfacesArchivist'
11
+
12
+ export class OsCallerBase {
13
+ constructor(protected context: XyOsContext) {}
14
+ // Get the dapps archivist
15
+ async getDappsArchivist() {
16
+ const mod = assertEx(await this.context.root.resolve(DappsArchivistModuleName), () => `${DappsArchivistModuleName} not found`)
17
+ return asArchivistInstance(mod, () => `${DappsArchivistModuleName} is not an archivist`)
18
+ }
19
+
20
+ // Get the dapps archivist development
21
+ async getDappsArchivistDevelopment() {
22
+ const mod = assertEx(
23
+ await this.context.root.resolve(DappsArchivistDevelopmentModuleName),
24
+ () => `${DappsArchivistDevelopmentModuleName} not found`,
25
+ )
26
+ return asArchivistInstance(mod, () => `${DappsArchivistDevelopmentModuleName} is not an archivist`)
27
+ }
28
+
29
+ // Get the dapps archivist payload diviner
30
+ async getDappsArchivistPayloadDiviner() {
31
+ const mod = assertEx(
32
+ await this.context.root.resolve(DappsArchivistPayloadDivinerModuleName),
33
+ () => `${DappsArchivistPayloadDivinerModuleName} not found`,
34
+ )
35
+ return asDivinerInstance(mod, () => `${DappsArchivistPayloadDivinerModuleName} is not a diviner`)
36
+ }
37
+
38
+ // Get the dapps archivist payload diviner development
39
+ async getDappsArchivistPayloadDivinerDevelopment() {
40
+ const mod = assertEx(
41
+ await this.context.root.resolve(DappsArchivistPayloadDevelopmentDivinerModuleName),
42
+ () => `${DappsArchivistPayloadDevelopmentDivinerModuleName} not found`,
43
+ )
44
+ return asDivinerInstance(mod, () => `${DappsArchivistPayloadDevelopmentDivinerModuleName} is not a diviner`)
45
+ }
46
+
47
+ // Get the registered dapp interfaces archivist
48
+ async getRegisteredDappInterfacesArchivist() {
49
+ const mod = assertEx(
50
+ await this.context.root.resolve(RegisteredDappInterfacesArchivistModuleName),
51
+ () => `${RegisteredDappInterfacesArchivistModuleName} not found`,
52
+ )
53
+ return asArchivistInstance(mod, () => `${RegisteredDappInterfacesArchivistModuleName} is not an archivist`)
54
+ }
55
+ }
@@ -0,0 +1,30 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import type { Address } from '@xylabs/hex'
3
+ import type { BridgeExposeOptions } from '@xyo-network/bridge-model'
4
+ import { asBridgeInstance } from '@xyo-network/bridge-model'
5
+ import type { NodeInstance } from '@xyo-network/node-model'
6
+ import { BridgedPubSubModuleName } from '@xyo-network/os-model'
7
+
8
+ export class PubSubBridgeCaller {
9
+ constructor(private node: NodeInstance) {}
10
+
11
+ async exposeAddresses(addresses: Address[], options?: BridgeExposeOptions) {
12
+ const pubSubBridge = await this.getPubSubBridge()
13
+ for (const address of addresses) {
14
+ console.log(`Exposing: ${address}`)
15
+ await pubSubBridge.expose(address, options)
16
+ }
17
+ }
18
+
19
+ async unExposeAddresses(addresses: Address[]) {
20
+ const pubSubBridge = await this.getPubSubBridge()
21
+ for (const address of addresses) {
22
+ await pubSubBridge.unexpose?.(address)
23
+ }
24
+ }
25
+
26
+ private async getPubSubBridge() {
27
+ const mod = assertEx(await this.node.resolve(BridgedPubSubModuleName), () => `${BridgedPubSubModuleName} not found`)
28
+ return asBridgeInstance(mod, () => `${BridgedPubSubModuleName} is not a bridge`)
29
+ }
30
+ }
package/src/XyOs.ts ADDED
@@ -0,0 +1,125 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import type { Promisable } from '@xylabs/promise'
3
+ import type { KernelExternal } from '@xyo-network/kernel-model'
4
+ import type { PackageManifest } from '@xyo-network/manifest'
5
+ import { ModuleFactoryLocator } from '@xyo-network/module-factory-locator'
6
+ import type { NodeInstance } from '@xyo-network/node-model'
7
+ import type {
8
+ DappId,
9
+ DappPackageManifestPayload,
10
+ DappParams,
11
+ StackInitializer,
12
+ XyOsContext,
13
+ XyOsDappContext,
14
+ XyOsGlobal,
15
+ } from '@xyo-network/os-model'
16
+ import type { WalletInstance } from '@xyo-network/wallet-model'
17
+
18
+ import type { DappRegistrationServiceParams } from './classes/registration/index.ts'
19
+ import { DappRegistrationService } from './classes/registration/index.ts'
20
+ import type { MonitorEventConfig, XyOsMonitor } from './helpers/index.ts'
21
+ import { NameTransforms } from './lib/index.ts'
22
+ import { loadOsNode } from './loadOsNode.ts'
23
+ import type { OsPubSubNetworkStack } from './stack/index.ts'
24
+ import { OsStackMap, StackManager } from './stack/index.ts'
25
+ import type { XyOsContextParams } from './XyOsBase.ts'
26
+ import { XyOsContextBase } from './XyOsBase.ts'
27
+
28
+ export interface XyOsParams<TManifest extends PackageManifest = PackageManifest> extends XyOsContextParams<TManifest> {
29
+ kernel: XyOsContext['kernel']
30
+ }
31
+
32
+ export class XyOs extends XyOsContextBase<XyOsParams> implements XyOsContext {
33
+ protected _exposedNode: NodeInstance | undefined
34
+ protected _exposedNodeOuter: NodeInstance | undefined
35
+ protected _locator: ModuleFactoryLocator
36
+ protected dappRegistrationService: DappRegistrationService
37
+ private stackManager: StackManager
38
+
39
+ constructor(params: XyOsParams, dappsConfiguration?: DappRegistrationServiceParams, developmentMode?: boolean) {
40
+ super(params)
41
+ this._locator = params.locator ?? new ModuleFactoryLocator()
42
+ this.stackManager = new StackManager(OsStackMap)
43
+ this.dappRegistrationService = new DappRegistrationService(this, dappsConfiguration, this.locator, developmentMode)
44
+ }
45
+
46
+ static get monitoring(): XyOsMonitor | undefined {
47
+ return
48
+ }
49
+
50
+ static get xyOsGlobal(): XyOsGlobal {
51
+ return getXyOsGlobal()
52
+ }
53
+
54
+ override get bios(): KernelExternal['bios'] | undefined {
55
+ return assertEx(this.params.kernel?.bios, () => 'Missing bios')
56
+ }
57
+
58
+ override get exposedNode() {
59
+ return this.stackManager.getStack<OsPubSubNetworkStack>('osPubSubNetworkStack')?.exposedNode
60
+ }
61
+
62
+ override get exposedNodeOuter() {
63
+ return this.stackManager.getStack<OsPubSubNetworkStack>('osPubSubNetworkStack')?.exposedNodeOuter
64
+ }
65
+
66
+ override get kernel(): XyOsContext['kernel'] | undefined {
67
+ return assertEx(this.params.kernel, () => 'Missing kernel')
68
+ }
69
+
70
+ get locator() {
71
+ return this._locator
72
+ }
73
+
74
+ static async monitor<T>(fn: () => Promise<T>, eventConfig: MonitorEventConfig): Promise<T> {
75
+ const { name, additionalProperties } = eventConfig
76
+ const monitor = this.monitoring
77
+ monitor?.startTimer(name)
78
+ const result = await fn()
79
+ monitor?.stopTimer({ additionalProperties, name })
80
+
81
+ return result
82
+ }
83
+
84
+ override async boot(wallet: WalletInstance, locator = new ModuleFactoryLocator()): Promise<void> {
85
+ console.warn('XyOs:boot', wallet.address)
86
+ const fullLocator = this._locator.merge(locator)
87
+ return await this._bootMutex.runExclusive(async () => {
88
+ assertEx(this._root === undefined, () => 'XyOs already booted')
89
+ const [root] = await XyOs.monitor(async () => await loadOsNode(wallet, fullLocator), { name: 'Load XyOs' })
90
+ this._root = root
91
+
92
+ this.stackManager.initialize(this)
93
+ })
94
+ }
95
+
96
+ override buildDapp(
97
+ manifest: DappPackageManifestPayload,
98
+ dappId = NameTransforms.slug(manifest.nodes[0]?.config.name),
99
+ name = NameTransforms.moduleName(manifest.nodes[0]?.config.name),
100
+ ): Promisable<XyOsDappContext> {
101
+ return this.dappRegistrationService.buildDapp(manifest, dappId, name)
102
+ }
103
+
104
+ override dappByName(dappId: DappId): Promisable<XyOsDappContext | undefined> {
105
+ return this.dappRegistrationService.builtDapps[dappId]
106
+ }
107
+
108
+ override getDappParams(dappId: DappId): DappParams {
109
+ return this.dappRegistrationService.getParamsForRegisteredDapp(dappId)
110
+ }
111
+
112
+ override getStack(stackName: string): StackInitializer {
113
+ return assertEx(this.stackManager.getStack(stackName), () => `Stack not found: ${stackName}`)
114
+ }
115
+
116
+ override async monitor<T>(fn: () => Promise<T>, eventConfig: MonitorEventConfig): Promise<T> {
117
+ return await XyOs.monitor<T>(fn, eventConfig)
118
+ }
119
+
120
+ override shutdown() {
121
+ assertEx(this._root !== undefined, () => 'XyOs not booted')
122
+ this._root = undefined
123
+ this.stackManager.stop()
124
+ }
125
+ }