@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
@@ -0,0 +1,119 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import type { BaseParams, JsonObject } from '@xylabs/object'
3
+ import type { Promisable } from '@xylabs/promise'
4
+ import type { WalletInstance } from '@xyo-network/account'
5
+ import type { KernelExternal } from '@xyo-network/kernel-model'
6
+ import type { PackageManifest } from '@xyo-network/manifest'
7
+ import { BaseEmitter } from '@xyo-network/module-abstract'
8
+ import { ModuleFactoryLocator } from '@xyo-network/module-factory-locator'
9
+ import type { NodeInstance } from '@xyo-network/node-model'
10
+ import type { DappId, DappPackageManifestPayload, DappParams, EventBusInterface, StackInitializer, XyOsContext, XyOsDappContext } from '@xyo-network/os-model'
11
+ import { Mutex } from 'async-mutex'
12
+
13
+ export interface XyOsContextParams<TManifest extends PackageManifest = PackageManifest> extends BaseParams {
14
+ eventBus?: EventBusInterface
15
+ locator?: ModuleFactoryLocator
16
+ manifest?: TManifest
17
+ platformLocator?: ModuleFactoryLocator
18
+ }
19
+
20
+ export class XyOsContextBase<TParams extends XyOsContextParams | void = void> extends BaseEmitter<
21
+ TParams extends XyOsContextParams ? TParams : XyOsContextParams
22
+ > {
23
+ protected _bootMutex = new Mutex()
24
+ protected _platformLocator: ModuleFactoryLocator | undefined
25
+ protected _root: NodeInstance | undefined
26
+
27
+ get bios(): KernelExternal['bios'] | undefined {
28
+ return this.kernel?.bios
29
+ }
30
+
31
+ get eventBus(): XyOsContext['eventBus'] {
32
+ return assertEx(this.params.eventBus, () => 'Missing event bus')
33
+ }
34
+
35
+ get exposedNode(): NodeInstance | undefined {
36
+ throw new Error('Method not implemented. [exposedNode]')
37
+ }
38
+
39
+ get exposedNodeOuter(): NodeInstance | undefined {
40
+ throw new Error('Method not implemented. [exposedNodeOuter]')
41
+ }
42
+
43
+ get installedDapps(): DappId[] {
44
+ return []
45
+ }
46
+
47
+ get kernel(): XyOsContext['kernel'] | undefined {
48
+ return undefined
49
+ }
50
+
51
+ get manifest() {
52
+ return assertEx(this.params.manifest, () => 'Missing manifest')
53
+ }
54
+
55
+ get platformLocator(): ModuleFactoryLocator {
56
+ this._platformLocator = this._platformLocator ?? this.params.platformLocator ?? new ModuleFactoryLocator()
57
+ return this._platformLocator
58
+ }
59
+
60
+ get possibleRoot(): NodeInstance | undefined {
61
+ return this._root
62
+ }
63
+
64
+ get root(): NodeInstance {
65
+ return assertEx(this._root, () => 'XyOs root not set (probably not yet booted)')
66
+ }
67
+
68
+ get runningDapps(): DappId[] {
69
+ return []
70
+ }
71
+
72
+ get state() {
73
+ return this._root ? 'running' : 'stopped'
74
+ }
75
+
76
+ boot(_wallet: WalletInstance, _locator: ModuleFactoryLocator) {
77
+ throw new Error('Method not implemented. [boot]')
78
+ }
79
+
80
+ buildDapp(_manifest: DappPackageManifestPayload, _dappId?: DappId): Promisable<XyOsDappContext> {
81
+ throw new Error('Method not implemented. [buildDapp]')
82
+ }
83
+
84
+ dappByName(_dappId: DappId): Promisable<XyOsDappContext | undefined> {
85
+ throw new Error('Method not implemented. [runningDapp]')
86
+ }
87
+
88
+ getDappParams(_dappId: DappId): DappParams | undefined {
89
+ throw new Error('Method not implemented. [getDappParams]')
90
+ }
91
+
92
+ getStack(_stackName: string): StackInitializer {
93
+ throw new Error('Method not implemented. [getStack]')
94
+ }
95
+
96
+ installDapp(_dappId: DappId) {
97
+ throw new Error('Method not implemented. [installDapp]')
98
+ }
99
+
100
+ monitor<T>(_fn: () => Promise<T>, _eventConfig: { additionalProperties?: JsonObject; name: string }): Promise<T> {
101
+ throw new Error('Method not implemented. [monitor]')
102
+ }
103
+
104
+ shutdown() {
105
+ throw new Error('Method not implemented. [shutdown]')
106
+ }
107
+
108
+ startDapp(_dappId: DappId): Promisable<XyOsDappContext> {
109
+ throw new Error('Method not implemented. [startDapp]')
110
+ }
111
+
112
+ stopDapp(_dappId: DappId) {
113
+ throw new Error('Method not implemented. [stopDapp]')
114
+ }
115
+
116
+ uninstallDapp(_dappId: DappId) {
117
+ throw new Error('Method not implemented. [uninstallDapp]')
118
+ }
119
+ }
@@ -0,0 +1,90 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import type { WalletInstance } from '@xyo-network/account'
3
+ import { ManifestWrapper } from '@xyo-network/manifest-wrapper'
4
+ import { ModuleFactoryLocator } from '@xyo-network/module-factory-locator'
5
+ import type { DappPackageManifestPayload, XyOsContext, XyOsDappContext, XyOsDappDetails } from '@xyo-network/os-model'
6
+ import { DappPackageManifestPayloadSchema } from '@xyo-network/os-model'
7
+
8
+ import { DappWindowManifest } from './manifests/index.ts'
9
+ import type { XyOsContextParams } from './XyOsBase.ts'
10
+ import { XyOsContextBase } from './XyOsBase.ts'
11
+
12
+ export interface XyOsDappContextParams extends XyOsContextParams<DappPackageManifestPayload> {
13
+ dapp: XyOsDappDetails
14
+ locator: ModuleFactoryLocator
15
+ parent: XyOsContext
16
+ }
17
+
18
+ export class XyOsDapp extends XyOsContextBase<XyOsDappContextParams> implements XyOsDappContext {
19
+ dapp: XyOsDappDetails
20
+
21
+ constructor(params: XyOsDappContextParams) {
22
+ super(params)
23
+ this.dapp = { ...params.dapp }
24
+ }
25
+
26
+ override get eventBus() {
27
+ return this.parent.eventBus
28
+ }
29
+
30
+ override get exposedNode() {
31
+ return this.parent.exposedNode
32
+ }
33
+
34
+ override get exposedNodeOuter() {
35
+ return this.parent.exposedNodeOuter
36
+ }
37
+
38
+ /**
39
+ * @deprecated Dapps should not have access to the bios. This is done for the
40
+ * settings dapp, but should be refactored out. Bios could possibly be an
41
+ * optional property of the parent context passed in for dapps that require
42
+ * elevated permissions.
43
+ * ^^^ I believe we may want this here in that is a dApp requests access to the bios it will optionally be there
44
+ */
45
+ override get kernel(): XyOsContextBase['kernel'] {
46
+ return assertEx(this.parent.kernel, () => 'Missing kernel')
47
+ }
48
+
49
+ get parent() {
50
+ return this.params.parent
51
+ }
52
+
53
+ override async boot(wallet: WalletInstance, locator?: ModuleFactoryLocator): Promise<void> {
54
+ return await this._bootMutex.runExclusive(async () => {
55
+ console.log('XyOsDapp:boot:start', wallet.address, this.dapp.id)
56
+ assertEx(this._root === undefined, () => 'Dapp already booted')
57
+
58
+ const finalLocator
59
+ = locator ? (this.params.locator ?? new ModuleFactoryLocator()).merge(locator) : (this.params.locator ?? new ModuleFactoryLocator())
60
+
61
+ console.log('XyOsDapp:boot:finalLocator', finalLocator)
62
+
63
+ const dappNodesWrapper = new ManifestWrapper({ ...this.manifest, schema: DappPackageManifestPayloadSchema }, wallet, finalLocator)
64
+ const dappNodes = await dappNodesWrapper.loadNodes()
65
+
66
+ // Derive a specific wallet for the dappManifestWrapper.
67
+ // If the dappNodes and dappManifest wrappers get the same wallet, there will be collisions.
68
+ const dappManifestWallet = await wallet.derivePath('99999')
69
+ DappWindowManifest.nodes[0].config.name = this.dapp.name + 'Root'
70
+ const wrapper = new ManifestWrapper<DappPackageManifestPayload>(
71
+ { ...DappWindowManifest, schema: DappPackageManifestPayloadSchema },
72
+ dappManifestWallet,
73
+ finalLocator,
74
+ )
75
+
76
+ const node = await wrapper.loadNodeFromIndex(0)
77
+ for (const dappNode of dappNodes) {
78
+ await node.register(dappNode)
79
+ await node.attach(dappNode.address, true)
80
+ }
81
+
82
+ this._root = node
83
+ console.log('XyOsDapp:boot:finish', wallet.address, this.dapp.id)
84
+ })
85
+ }
86
+
87
+ override getStack(stackName: string) {
88
+ return this.parent.getStack(stackName)
89
+ }
90
+ }
@@ -0,0 +1,8 @@
1
+ import type { DappAccessInterface } from '@xyo-network/os-model'
2
+ import { RegisteredNames, RegisteredNamesInterface } from '@xyo-network/os-model'
3
+
4
+ export type ValidDappAccessInterfaces = typeof RegisteredNames
5
+
6
+ export const ValidDappAccessInterfaces: Record<string, DappAccessInterface> = {
7
+ [RegisteredNames]: RegisteredNamesInterface,
8
+ }
@@ -0,0 +1,2 @@
1
+ export * from './registered-names/index.ts'
2
+ export * from './ValidDappAccessInterfaces.ts'
@@ -0,0 +1,59 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import { asArchivistInstance } from '@xyo-network/archivist-model'
3
+ import type { DappAccessRequest, RegisteredDappAccess, TemporalIndexRegistrationResults, XyOsContext } from '@xyo-network/os-model'
4
+ import type { WithMeta, WithSources } from '@xyo-network/payload-model'
5
+
6
+ import { RunningDappAccessCache } from '../../../classes/cache/index.ts'
7
+
8
+ type IndexingResults = WithMeta<WithSources<TemporalIndexRegistrationResults>>[]
9
+
10
+ export class AccessNodeQueries {
11
+ constructor(
12
+ protected xyOsContext: XyOsContext,
13
+ private accessRequest: DappAccessRequest,
14
+ private registeredDappAccess: RegisteredDappAccess,
15
+ ) {}
16
+
17
+ get nameServiceNamesAccessNode() {
18
+ return assertEx(RunningDappAccessCache.get(this.registeredDappAccess.registeringDappId), () => 'No access node found')
19
+ }
20
+
21
+ // Refresh the archivist in the dapp
22
+ async refresh() {
23
+ const archivist = await this.getArchivistFromDappNode()
24
+ await archivist.clear()
25
+ await this.run()
26
+ console.log('refreshed')
27
+ }
28
+
29
+ // Run the access node queries
30
+ async run() {
31
+ const payloads = await this.getPayloadsFromAccessNode()
32
+ await this.insertAccessNodePayloadsIntoDappNodeArchivist(payloads)
33
+ }
34
+
35
+ private async getArchivistFromDappNode() {
36
+ const archivistPath = this.accessRequest?.destinationArchivist || 'DappArchivist'
37
+ const mod = assertEx(await this.xyOsContext.root.resolve(archivistPath), () => `Unable to find archivist at ${archivistPath}`)
38
+ return asArchivistInstance(mod, () => `Module at ${archivistPath} is not an archivist`)
39
+ }
40
+
41
+ private async getPayloadsFromAccessNode() {
42
+ const archivistFromAccessNode = await this.resolveArchivistFromAccessNode()
43
+ return (await archivistFromAccessNode.all()) as IndexingResults
44
+ }
45
+
46
+ private async insertAccessNodePayloadsIntoDappNodeArchivist(payloads: IndexingResults) {
47
+ const archivist = await this.getArchivistFromDappNode()
48
+ await archivist.clear()
49
+ await archivist.insert(payloads)
50
+ }
51
+
52
+ private async resolveArchivistFromAccessNode() {
53
+ const archivist = assertEx(
54
+ await this.nameServiceNamesAccessNode.resolve('PublicXnsArchivist'),
55
+ () => 'Unable to find archivist at PublicXnsArchivist',
56
+ )
57
+ return asArchivistInstance(archivist, () => 'PublicXnsArchivist is not an archivist')
58
+ }
59
+ }
@@ -0,0 +1,2 @@
1
+ export * from './AccessNodeQueries.ts'
2
+ export * from './resource/index.ts'
@@ -0,0 +1,38 @@
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 XnsArchivistPayloadDivinerModuleName = 'XnsArchivistPayloadDiviner'
7
+ export const XnsArchivistModuleName = 'XnsArchivist'
8
+ export const XnsRegistrationsArchivistModuleName = 'XnsRegistrationsArchivist'
9
+
10
+ export abstract class AbstractXnsCaller {
11
+ constructor(protected context: XyOsContext) {}
12
+ /**
13
+ * Get the XnsArchivist
14
+ */
15
+ async getXnsArchivist(pathPrefix?: string) {
16
+ const fullyQualifiedPath = pathPrefix ? `${pathPrefix}:${XnsArchivistModuleName}` : XnsArchivistModuleName
17
+ const mod = assertEx(await this.context.root.resolve(fullyQualifiedPath), () => `${fullyQualifiedPath} not found`)
18
+ return asArchivistInstance(mod, () => `${fullyQualifiedPath} is not an archivist`)
19
+ }
20
+
21
+ /**
22
+ * Get the XnsArchivistPayloadDiviner
23
+ */
24
+ async getXnsArchivistPayloadDiviner(pathPrefix?: string) {
25
+ const fullyQualifiedPath = pathPrefix ? `${pathPrefix}:${XnsArchivistPayloadDivinerModuleName}` : XnsArchivistPayloadDivinerModuleName
26
+ const mod = assertEx(await this.context.root.resolve(fullyQualifiedPath), () => `${fullyQualifiedPath} not found`)
27
+ return asDivinerInstance(mod, () => `${fullyQualifiedPath} is not an diviner`)
28
+ }
29
+
30
+ /**
31
+ * Get the XnsRegistrationsArchivist
32
+ */
33
+ async getXnsRegistrationsArchivist(pathPrefix?: string) {
34
+ const fullyQualifiedPath = pathPrefix ? `${pathPrefix}:${XnsRegistrationsArchivistModuleName}` : XnsRegistrationsArchivistModuleName
35
+ const mod = assertEx(await this.context.root.resolve(fullyQualifiedPath), () => `${fullyQualifiedPath} not found`)
36
+ return asArchivistInstance(mod, () => `${fullyQualifiedPath} is not an archivist`)
37
+ }
38
+ }
@@ -0,0 +1,54 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import { isTemporalIndexingDivinerResultIndex } from '@xyo-network/diviner-temporal-indexing-model'
3
+ import type { TemporalIndexRegistrationResults, XyOsContext } from '@xyo-network/os-model'
4
+ import type { WithMeta, WithSources } from '@xyo-network/payload-model'
5
+ import type { Listener } from 'ethers'
6
+
7
+ import type { ResourceStore } from '../../../../lib/index.ts'
8
+ import { PayloadStore } from '../../../../lib/index.ts'
9
+ import { AbstractXnsCaller } from './AbstractXnsCaller.ts'
10
+ import { XnsRegistrationsResourceQueries } from './RegistrationsResourceQueries.ts'
11
+
12
+ export type XnsRegistrationsViews = 'allRegistrations'
13
+
14
+ export class XnsRegistrationsResource extends AbstractXnsCaller implements ResourceStore<XnsRegistrationsViews> {
15
+ protected _allRegistrations: PayloadStore<WithMeta<WithSources<TemporalIndexRegistrationResults>>> | undefined
16
+
17
+ constructor(
18
+ context: XyOsContext,
19
+ private pathPrefix?: string,
20
+ ) {
21
+ super(context)
22
+ }
23
+
24
+ get allRegistrations() {
25
+ return assertEx(this._allRegistrations)
26
+ }
27
+
28
+ get subscriptions() {
29
+ return {
30
+ allRegistrations: (cb: Listener) => this.allRegistrations.subscribe(cb),
31
+ }
32
+ }
33
+
34
+ get views() {
35
+ return {
36
+ allRegistrations: () => this.allRegistrations.getSnapshot(),
37
+ }
38
+ }
39
+
40
+ cleanupListeners() {
41
+ this.allRegistrations.cleanupListeners()
42
+ }
43
+
44
+ async start() {
45
+ const xnsArchivistPayloadDiviner = await this.getXnsArchivistPayloadDiviner(this.pathPrefix)
46
+ const xnsArchivist = await this.getXnsArchivist(this.pathPrefix)
47
+
48
+ this._allRegistrations = await PayloadStore.create<WithMeta<WithSources<TemporalIndexRegistrationResults>>>({
49
+ archivist: xnsArchivist,
50
+ getLatest: async () => await XnsRegistrationsResourceQueries.getAllRegistrations(xnsArchivistPayloadDiviner),
51
+ idFunction: isTemporalIndexingDivinerResultIndex,
52
+ })
53
+ }
54
+ }
@@ -0,0 +1,22 @@
1
+ import type { ArchivistInstance } from '@xyo-network/archivist-model'
2
+ import type { DivinerInstance } from '@xyo-network/diviner-model'
3
+ import { PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'
4
+ import { TemporalIndexingDivinerResultIndexSchema } from '@xyo-network/diviner-temporal-indexing-model'
5
+ import type { TemporalIndexRegistrationResults } from '@xyo-network/os-model'
6
+
7
+ export const XnsRegistrationsResourceQueries = {
8
+ getAllRegistrations: async (diviner: DivinerInstance) => {
9
+ const query = {
10
+ limit: 100,
11
+ order: 'desc',
12
+ schema: PayloadDivinerQuerySchema,
13
+ schemas: [TemporalIndexingDivinerResultIndexSchema],
14
+ }
15
+
16
+ return (await diviner.divine([query])) as TemporalIndexRegistrationResults[]
17
+ },
18
+
19
+ getRecentRegistrations: async (archivist: ArchivistInstance) => {
20
+ return (await archivist.all()).reverse() as TemporalIndexRegistrationResults[]
21
+ },
22
+ }
@@ -0,0 +1,3 @@
1
+ export * from './AbstractXnsCaller.ts'
2
+ export * from './RegistrationsResource.ts'
3
+ export * from './RegistrationsResourceQueries.ts'
@@ -0,0 +1 @@
1
+ export * from './helpers/index.ts'
@@ -0,0 +1,96 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import { forget } from '@xylabs/forget'
3
+ import type { BaseParams } from '@xylabs/object'
4
+ import type { ArchivistModuleEventData } from '@xyo-network/archivist'
5
+ import { asArchivistInstance } from '@xyo-network/archivist'
6
+ import type { KernelExternal } from '@xyo-network/kernel-model'
7
+ import { BaseEmitter } from '@xyo-network/module-abstract'
8
+ import { asAttachableNodeInstance } from '@xyo-network/node-model'
9
+ import type { NodeAdapter, NodeAdapterBaseEvents } from '@xyo-network/os-model'
10
+ import type { Payload, WithMeta } from '@xyo-network/payload-model'
11
+ import { v4 as uuid } from 'uuid'
12
+
13
+ type ModuleEventPayload = { event: string; name: string | undefined; schema: 'network.xyo.kernel.module.event' }
14
+
15
+ export class NodeAdapterBase extends BaseEmitter<BaseParams, NodeAdapterBaseEvents> implements NodeAdapter {
16
+ initialized = false
17
+
18
+ private _id: string = ''
19
+
20
+ constructor(
21
+ private kernel: KernelExternal,
22
+ private driverName: string,
23
+ ) {
24
+ super({})
25
+ this._id = uuid()
26
+ }
27
+
28
+ get id() {
29
+ return assertEx(this._id, () => 'NodeAdapterBase id not initialized')
30
+ }
31
+
32
+ async start() {
33
+ try {
34
+ // listen to events inserted into the kernel events archivist
35
+ const moduleAttachedInsertListener = ({ payloads }: ArchivistModuleEventData['inserted']) => {
36
+ const moduleAttachedEvent = this.findModuleEventPayloads(payloads)
37
+
38
+ // loop through the event payloads
39
+ for (const payload of moduleAttachedEvent) {
40
+ // check if the event is for the driver
41
+ if (payload.name === this.driverName) {
42
+ this.validateAndReturnDriver()
43
+ }
44
+ }
45
+ }
46
+ // get the events archivist
47
+ const eventsArchivist = await this.getEventsArchivist()
48
+
49
+ // prepare the listener
50
+ const weakRefInsertListener = new WeakRef(moduleAttachedInsertListener)
51
+ const insertListenerRef = weakRefInsertListener.deref()
52
+
53
+ // attach the listener
54
+ if (insertListenerRef) eventsArchivist.on('inserted', insertListenerRef)
55
+
56
+ // check if the driver is already attached
57
+ const node = this.validateAndReturnDriver()
58
+ if (node) return node
59
+
60
+ // reset the initialized flag
61
+ this.initialized = false
62
+ } catch (error) {
63
+ // call the onError callbacks
64
+ await this.emit('driverError', { error: error as Error })
65
+ }
66
+ }
67
+
68
+ private findModuleEventPayloads(payloads: WithMeta<Payload>[]) {
69
+ return payloads.filter((payload) => {
70
+ const payloadToTest = payload as WithMeta<ModuleEventPayload>
71
+ return payloadToTest.event === 'driverReady' && payloadToTest.schema === 'network.xyo.kernel.module.event'
72
+ }) as WithMeta<ModuleEventPayload>[]
73
+ }
74
+
75
+ private async getEventsArchivist() {
76
+ const mod = assertEx(await this.kernel.node.resolve('KernelNode:ModuleEvents'), () => 'KernelNode:ModuleEvents not found')
77
+ return asArchivistInstance(mod, () => `${mod.id} is not an archivist`)
78
+ }
79
+
80
+ private validateAndReturnDriver() {
81
+ const mod = this.kernel.initialized[this.driverName]
82
+ if (mod) {
83
+ // confirm expected shape
84
+ const node = asAttachableNodeInstance(mod, () => `${mod.id} is not a node`)
85
+
86
+ // set the initialized flag
87
+ this.initialized = true
88
+
89
+ // call the next callbacks
90
+ const emit = async () => await this.emit('driverReady', { node })
91
+ forget(emit())
92
+
93
+ return node
94
+ }
95
+ }
96
+ }
@@ -0,0 +1,31 @@
1
+ import type { KernelExternal } from '@xyo-network/kernel-model'
2
+ import type { AttachableNodeInstance } from '@xyo-network/node-model'
3
+ import type { NetworkAdapterBase } from '@xyo-network/os-model'
4
+
5
+ import { NodeAdapterBase } from './Base.ts'
6
+
7
+ /**
8
+ * Adapts Network Driver to the Operating System
9
+ */
10
+ export class NetworkAdapter extends NodeAdapterBase implements NetworkAdapterBase {
11
+ _connection: AttachableNodeInstance | undefined
12
+
13
+ constructor(kernel: KernelExternal, driverName: string) {
14
+ super(kernel, driverName)
15
+ this.on('driverReady', ({ node }) => {
16
+ this._connection = node
17
+ })
18
+ }
19
+
20
+ get connection() {
21
+ return this._connection
22
+ }
23
+
24
+ get hasConnection() {
25
+ return !!this.connection
26
+ }
27
+
28
+ removeConnection() {
29
+ this._connection = undefined
30
+ }
31
+ }
@@ -0,0 +1,30 @@
1
+ import type { KernelExternal } from '@xyo-network/kernel-model'
2
+ import type { AttachableNodeInstance } from '@xyo-network/node-model'
3
+
4
+ import { NodeAdapterBase } from './Base.ts'
5
+
6
+ /**
7
+ * Adapts Settings Driver to the Operating System
8
+ */
9
+ export class SettingsAdapter extends NodeAdapterBase {
10
+ private _settings: AttachableNodeInstance | undefined = undefined
11
+
12
+ constructor(kernel: KernelExternal, driverName: string) {
13
+ super(kernel, driverName)
14
+ this.on('driverReady', ({ node }) => {
15
+ this._settings = node
16
+ })
17
+ }
18
+
19
+ get hasSettings() {
20
+ return !!this.settings
21
+ }
22
+
23
+ get settings() {
24
+ return this._settings
25
+ }
26
+
27
+ removeSettings() {
28
+ this._settings = undefined
29
+ }
30
+ }
@@ -0,0 +1,2 @@
1
+ export * from './Network.ts'
2
+ export * from './Settings.ts'
@@ -0,0 +1,10 @@
1
+ import type { KernelExternal } from '@xyo-network/kernel-model'
2
+ import type { AdapterSetCreator } from '@xyo-network/os-model'
3
+ import { ExposedNodeOuterNodeName, PubSubBridgeNodeNodeName } from '@xyo-network/os-model'
4
+
5
+ import { NetworkAdapter } from '../adapter/index.ts'
6
+
7
+ export const OsPubSubBridgeNetworkAdapters: AdapterSetCreator = (kernel: KernelExternal) => [
8
+ new NetworkAdapter(kernel, PubSubBridgeNodeNodeName),
9
+ new NetworkAdapter(kernel, ExposedNodeOuterNodeName),
10
+ ]
@@ -0,0 +1,7 @@
1
+ import type { KernelExternal } from '@xyo-network/kernel-model'
2
+ import type { AdapterSetCreator } from '@xyo-network/os-model'
3
+ import { OsSettingsNodeName } from '@xyo-network/os-model'
4
+
5
+ import { SettingsAdapter } from '../adapter/index.ts'
6
+
7
+ export const OsSettingsAdapters: AdapterSetCreator = (kernel: KernelExternal) => [new SettingsAdapter(kernel, OsSettingsNodeName)]
@@ -0,0 +1,9 @@
1
+ import type { KernelExternal } from '@xyo-network/kernel-model'
2
+ import type { AdapterSetCreator } from '@xyo-network/os-model'
3
+ import { XyoPublicNodeName } from '@xyo-network/os-model'
4
+
5
+ import { NetworkAdapter } from '../adapter/index.ts'
6
+
7
+ export const OsXyoPublicNetworkAdapters: AdapterSetCreator = (kernel: KernelExternal) => [
8
+ new NetworkAdapter(kernel, XyoPublicNodeName),
9
+ ]
@@ -0,0 +1,3 @@
1
+ export * from './OsPubSubBridgeNetwork.ts'
2
+ export * from './OsSettings.ts'
3
+ export * from './OsXyoPublicNetwork.ts'
@@ -0,0 +1,21 @@
1
+ import type { NodeInstance } from '@xyo-network/node-model'
2
+ import type { DappId } from '@xyo-network/os-model'
3
+
4
+ /**
5
+ * A cache for running access node instances
6
+ */
7
+ export class RunningDappAccessCache {
8
+ private static _cache: { [key: DappId]: NodeInstance } = {}
9
+
10
+ static get(key: DappId) {
11
+ return this._cache[key]
12
+ }
13
+
14
+ static has(key?: DappId) {
15
+ return key ? !!this._cache[key] : false
16
+ }
17
+
18
+ static set(key: DappId, value: NodeInstance) {
19
+ this._cache[key] = value
20
+ }
21
+ }