@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
package/src/stack/Map.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { OsPubSubBridgeNetworkAdapters, OsSettingsAdapters, OsXyoPublicNetworkAdapters } from '../adapters/index.ts'
|
|
2
|
+
import type { UninitializedStackMap } from './Manager.ts'
|
|
3
|
+
import { OsPubSubNetworkStack } from './OsPubSubNetworkStack.ts'
|
|
4
|
+
import { OsSettingsStack } from './OsSettingsStack.ts'
|
|
5
|
+
import { XyoPublicNetworkStack } from './XyoPublicNetworkStack.ts'
|
|
6
|
+
|
|
7
|
+
export const OsStackMap: UninitializedStackMap = {
|
|
8
|
+
osPubSubNetworkStack: {
|
|
9
|
+
stack: OsPubSubNetworkStack,
|
|
10
|
+
adapters: OsPubSubBridgeNetworkAdapters,
|
|
11
|
+
},
|
|
12
|
+
xyoPublicNetworkStack: {
|
|
13
|
+
stack: XyoPublicNetworkStack,
|
|
14
|
+
adapters: OsXyoPublicNetworkAdapters,
|
|
15
|
+
},
|
|
16
|
+
osSettingsStack: {
|
|
17
|
+
stack: OsSettingsStack,
|
|
18
|
+
adapters: OsSettingsAdapters,
|
|
19
|
+
},
|
|
20
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/assert'
|
|
2
|
+
import type { NodeInstance } from '@xyo-network/node-model'
|
|
3
|
+
import { asAttachableNodeInstance, asNodeInstance } from '@xyo-network/node-model'
|
|
4
|
+
import type { AdapterSetCreator, XyOsContext } from '@xyo-network/os-model'
|
|
5
|
+
import { ExposedNodeOuterNodeName, PubSubBridgeNodeNodeName } from '@xyo-network/os-model'
|
|
6
|
+
|
|
7
|
+
import type { EventBusPubSubConnection } from '../event/index.ts'
|
|
8
|
+
import { osPubSubNetworkReadyConnection, OsPubSubNetworkReadyEvent } from '../event/index.ts'
|
|
9
|
+
import { initializeXns } from '../lib/index.ts'
|
|
10
|
+
import { StackBase } from './Base.ts'
|
|
11
|
+
|
|
12
|
+
export class OsPubSubNetworkStack extends StackBase {
|
|
13
|
+
protected _exposedNode: NodeInstance | undefined
|
|
14
|
+
protected _exposedNodeOuter: NodeInstance | undefined
|
|
15
|
+
|
|
16
|
+
private busConnection: EventBusPubSubConnection = osPubSubNetworkReadyConnection()
|
|
17
|
+
|
|
18
|
+
constructor(context: XyOsContext, adapters: AdapterSetCreator) {
|
|
19
|
+
super(context, adapters, 'OsPubSubNetworkStackNode')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get exposedNode() {
|
|
23
|
+
return this._exposedNode
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get exposedNodeOuter() {
|
|
27
|
+
return this._exposedNodeOuter
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
override initialize() {
|
|
31
|
+
super.initialize()
|
|
32
|
+
// connect to the bus
|
|
33
|
+
this.context.eventBus.addConnection(this.busConnection)
|
|
34
|
+
this.on('initialized', () => this.busConnection.emit(OsPubSubNetworkReadyEvent, {}))
|
|
35
|
+
|
|
36
|
+
// listen for drivers
|
|
37
|
+
super.on('driverReady', async ({ node }) => {
|
|
38
|
+
if (node.id === PubSubBridgeNodeNodeName) {
|
|
39
|
+
const httpBridge = assertEx(await node.resolve('NsHttpBridge'), () => 'HttpBridge not found')
|
|
40
|
+
const xnsNode = asNodeInstance(await httpBridge.resolve('XNS'))
|
|
41
|
+
|
|
42
|
+
if (xnsNode) {
|
|
43
|
+
const initializedXns = await initializeXns(xnsNode)
|
|
44
|
+
console.log(`Xns: ${initializedXns}`)
|
|
45
|
+
} else {
|
|
46
|
+
console.log('Xns: Failed to contact Xns Node')
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
super.on('driverReady', async ({ node }) => {
|
|
52
|
+
if (node.id === ExposedNodeOuterNodeName) {
|
|
53
|
+
this._exposedNodeOuter = node
|
|
54
|
+
const mod = assertEx(await node.resolve('ExposedNode'), () => 'ExposedNode not found')
|
|
55
|
+
const exposedNode = asAttachableNodeInstance(mod, () => 'ExposedNode is not a node')
|
|
56
|
+
this._exposedNode = exposedNode
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
super.on('driverError', error => console.error('Unable to assign exposed node(s) to context', error))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
override stop() {
|
|
63
|
+
super.stop()
|
|
64
|
+
this._exposedNode = undefined
|
|
65
|
+
this._exposedNodeOuter = undefined
|
|
66
|
+
// prevent throwing an error if the connection is not initialized
|
|
67
|
+
if (this.busConnection._id)
|
|
68
|
+
this.context.eventBus.removeConnection(this.busConnection.id)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AdapterSetCreator, XyOsContext } from '@xyo-network/os-model'
|
|
2
|
+
|
|
3
|
+
import type { EventBusPubSubConnection } from '../event/index.ts'
|
|
4
|
+
import { osSettingsReadyConnection, OsSettingsReadyEvent } from '../event/index.ts'
|
|
5
|
+
import { StackBase } from './Base.ts'
|
|
6
|
+
|
|
7
|
+
export class OsSettingsStack extends StackBase {
|
|
8
|
+
private busConnection: EventBusPubSubConnection = osSettingsReadyConnection()
|
|
9
|
+
|
|
10
|
+
constructor(context: XyOsContext, adapters: AdapterSetCreator) {
|
|
11
|
+
super(context, adapters, 'OsSettingsStackNode')
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
override initialize() {
|
|
15
|
+
super.initialize()
|
|
16
|
+
// connect to the bus
|
|
17
|
+
this.context.eventBus.addConnection(this.busConnection)
|
|
18
|
+
this.on('initialized', () => this.busConnection.emit(OsSettingsReadyEvent, {}))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
override stop() {
|
|
22
|
+
super.stop()
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { AdapterSetCreator, XyOsContext } from '@xyo-network/os-model'
|
|
2
|
+
import { XyoPublicNodeName } from '@xyo-network/os-model'
|
|
3
|
+
|
|
4
|
+
import type { EventBusPubSubConnection } from '../event/index.ts'
|
|
5
|
+
import { osXyoPublicNetworkReadyConnection, OsXyoPublicNetworkReadyEvent } from '../event/index.ts'
|
|
6
|
+
import { StackBase } from './Base.ts'
|
|
7
|
+
|
|
8
|
+
export class XyoPublicNetworkStack extends StackBase {
|
|
9
|
+
private busConnection: EventBusPubSubConnection = osXyoPublicNetworkReadyConnection()
|
|
10
|
+
|
|
11
|
+
constructor(context: XyOsContext, adapters: AdapterSetCreator) {
|
|
12
|
+
super(context, adapters, 'OsXyoPublicNetworkStackNode')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
override initialize() {
|
|
16
|
+
super.initialize()
|
|
17
|
+
// connect to the bus
|
|
18
|
+
this.context.eventBus.addConnection(this.busConnection)
|
|
19
|
+
this.on('initialized', () => this.busConnection.emit(OsXyoPublicNetworkReadyEvent, {}))
|
|
20
|
+
|
|
21
|
+
this.on('driverReady', ({ node }) => {
|
|
22
|
+
if (node.id === XyoPublicNodeName) {
|
|
23
|
+
console.log('XyoPublicNode ready')
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
this.on('driverError', error => console.error('Error loading driver', error))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
override stop() {
|
|
30
|
+
super.stop()
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Hash } from '@xylabs/hex'
|
|
2
|
+
import type { WalletSeedPhrase } from '@xyo-network/os-model'
|
|
3
|
+
import { WalletSeedPhraseSchema } from '@xyo-network/os-model'
|
|
4
|
+
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
5
|
+
|
|
6
|
+
const language = 'english'
|
|
7
|
+
const standard = 'bip-39'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The label for the user wallet
|
|
11
|
+
*/
|
|
12
|
+
export const userWalletLabel = 'userWallet'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The valid wallet labels
|
|
16
|
+
*/
|
|
17
|
+
export type WalletLabel = typeof userWalletLabel // TODO: More wallet types 'osWallet' | 'deviceWalletBackup' | ...
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Builds a WalletSeedPhrase payload from the supplied mnemonic seed phrase and label
|
|
21
|
+
* @param mnemonic The mnemonic seed phrase
|
|
22
|
+
* @param label The wallet label
|
|
23
|
+
* @returns A WalletSeedPhrase payload and its root hash
|
|
24
|
+
*/
|
|
25
|
+
export const buildWalletSeedPhrasePayload = async (mnemonic: string, label: WalletLabel): Promise<{ payload: WalletSeedPhrase; rootHash: Hash }> => {
|
|
26
|
+
const fields = { mnemonic: { language, mnemonic: mnemonic.split(' '), standard } }
|
|
27
|
+
const meta = { label, timestamp: Date.now() }
|
|
28
|
+
const builder = new PayloadBuilder<WalletSeedPhrase>({ fields, meta, schema: WalletSeedPhraseSchema })
|
|
29
|
+
const payload = await builder.build()
|
|
30
|
+
const rootHash = await PayloadBuilder.hash(payload)
|
|
31
|
+
return { payload, rootHash }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Builds a user WalletSeedPhrase payload from the supplied mnemonic seed phrase and label
|
|
36
|
+
* @param mnemonic The mnemonic seed phrase
|
|
37
|
+
* @returns A user WalletSeedPhrase payload and its root hash
|
|
38
|
+
*/
|
|
39
|
+
export const buildUserWalletSeedPhrasePayload = (mnemonic: string): Promise<{ payload: WalletSeedPhrase; rootHash: Hash }> => {
|
|
40
|
+
return buildWalletSeedPhrasePayload(mnemonic, userWalletLabel)
|
|
41
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const getApiDomain = (scope: 'localhost' | 'beta' | 'prod' | undefined, apiDomain: string | undefined) => {
|
|
2
|
+
if (apiDomain !== undefined) {
|
|
3
|
+
return apiDomain as string
|
|
4
|
+
}
|
|
5
|
+
if (scope === 'localhost') {
|
|
6
|
+
return 'http://localhost:8080'
|
|
7
|
+
}
|
|
8
|
+
if (scope === 'beta') {
|
|
9
|
+
return 'https://beta.archivist.xyo.network'
|
|
10
|
+
}
|
|
11
|
+
return 'https://archivist.xyo.network'
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { getApiDomain }
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renames a key on an existing object by returning a new one with the same insertion order
|
|
3
|
+
*
|
|
4
|
+
* inspired by - https://stackoverflow.com/a/48110891/2803259
|
|
5
|
+
*/
|
|
6
|
+
export const renameObjKey = <T extends Record<string, unknown> = Record<string, unknown>>(oldObj: T, oldKey: keyof T, newKey: string) => {
|
|
7
|
+
const oldObjKeys = Object.keys(oldObj) as Array<keyof T>
|
|
8
|
+
// eslint-disable-next-line unicorn/no-array-reduce
|
|
9
|
+
const newObj = oldObjKeys.reduce(
|
|
10
|
+
(acc, val) => {
|
|
11
|
+
if (val === oldKey) {
|
|
12
|
+
acc[newKey] = oldObj[oldKey]
|
|
13
|
+
} else {
|
|
14
|
+
acc[val as string] = oldObj[val]
|
|
15
|
+
}
|
|
16
|
+
return acc
|
|
17
|
+
},
|
|
18
|
+
{} as Record<string, unknown>,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
return newObj as T
|
|
22
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { HDWallet } from '@xyo-network/account'
|
|
2
|
+
import { PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'
|
|
3
|
+
import type { DappId, DappWalletSeedPhrase, WalletId, XyOsContext } from '@xyo-network/os-model'
|
|
4
|
+
import { DappWalletSeedPhraseSchema } from '@xyo-network/os-model'
|
|
5
|
+
import type { WithMeta } from '@xyo-network/payload-model'
|
|
6
|
+
import { v4 as uuid } from 'uuid'
|
|
7
|
+
|
|
8
|
+
import { OsCallerBase } from '../OsCallerBase.ts'
|
|
9
|
+
import type { SeedPhraseId, SeedPhraseRepository } from './SeedPhraseRepository.ts'
|
|
10
|
+
|
|
11
|
+
export class DappSeedPhraseRepository extends OsCallerBase implements SeedPhraseRepository {
|
|
12
|
+
// record of all dapps that have requested a seed phrase with the OS
|
|
13
|
+
private dappIdRepository = new Map<SeedPhraseId, DappWalletSeedPhrase>()
|
|
14
|
+
// record of all walletIds that have been issued to registered dapps
|
|
15
|
+
private walletIdRepository = new Map<WalletId, DappWalletSeedPhrase>()
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
xyOs: XyOsContext,
|
|
19
|
+
protected readonly allowedNames: string[],
|
|
20
|
+
) {
|
|
21
|
+
super(xyOs)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async add(dappId: DappId): Promise<string> {
|
|
25
|
+
const archivist = await this.getDappsArchivist()
|
|
26
|
+
const seedPhrase = this.newPhrase()
|
|
27
|
+
const walletId = uuid()
|
|
28
|
+
const payload: DappWalletSeedPhrase = {
|
|
29
|
+
dappId,
|
|
30
|
+
schema: DappWalletSeedPhraseSchema,
|
|
31
|
+
seedPhrase,
|
|
32
|
+
walletId,
|
|
33
|
+
}
|
|
34
|
+
await archivist.insert([payload])
|
|
35
|
+
this.dappIdRepository.set(dappId, payload)
|
|
36
|
+
this.walletIdRepository.set(walletId, payload)
|
|
37
|
+
return walletId
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async findOrCreate(dappName: string): Promise<WalletId> {
|
|
41
|
+
if (this.allowedNames.includes(dappName)) {
|
|
42
|
+
const existingPayload = await this.checkDappId(dappName)
|
|
43
|
+
if (existingPayload) return existingPayload.walletId
|
|
44
|
+
|
|
45
|
+
// new dapp request so generate a new wallet Id and return it
|
|
46
|
+
try {
|
|
47
|
+
await this.add(dappName)
|
|
48
|
+
const existingPayload = await this.checkDappId(dappName)
|
|
49
|
+
if (existingPayload) return existingPayload.walletId
|
|
50
|
+
throw new Error(`failed to add seed phrase: ${dappName}`)
|
|
51
|
+
} catch (e) {
|
|
52
|
+
console.error(e)
|
|
53
|
+
throw new Error(`failed to add seed phrase: ${dappName}`)
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
throw new Error(`${dappName} is not allowed to register a seed phase. NOTE: this is a safeguard till 3rd party dApps can be validated`)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async request(walletId: WalletId) {
|
|
61
|
+
return (await this.checkWalletId(walletId))?.seedPhrase
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private async checkDappId(dappId: DappId) {
|
|
65
|
+
const diviner = await this.getDappsArchivistPayloadDiviner()
|
|
66
|
+
const query = {
|
|
67
|
+
dappId,
|
|
68
|
+
limit: 1,
|
|
69
|
+
order: 'desc',
|
|
70
|
+
schema: PayloadDivinerQuerySchema,
|
|
71
|
+
schemas: [DappWalletSeedPhraseSchema],
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const results = (await diviner.divine([query])) as WithMeta<DappWalletSeedPhrase>[]
|
|
75
|
+
return results.length > 0 ? results[0] : undefined
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private async checkWalletId(walletId: WalletId) {
|
|
79
|
+
const diviner = await this.getDappsArchivistPayloadDiviner()
|
|
80
|
+
const query = {
|
|
81
|
+
limit: 1,
|
|
82
|
+
order: 'desc',
|
|
83
|
+
schema: PayloadDivinerQuerySchema,
|
|
84
|
+
schemas: [DappWalletSeedPhraseSchema],
|
|
85
|
+
walletId,
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const results = (await diviner.divine([query])) as WithMeta<DappWalletSeedPhrase>[]
|
|
89
|
+
return results.length > 0 ? results[0] : undefined
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private newPhrase(): string {
|
|
93
|
+
return HDWallet.generateMnemonic()
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { WalletId } from '@xyo-network/os-model'
|
|
2
|
+
|
|
3
|
+
/** Convenience Types for Entities in the Repository */
|
|
4
|
+
export type SeedPhraseId = string
|
|
5
|
+
export type SeedPhrase = string
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A repository for storing wallet seed phrases and requesting them by walletIds (i.e. UUIDs)
|
|
9
|
+
*/
|
|
10
|
+
export interface SeedPhraseRepository {
|
|
11
|
+
/**
|
|
12
|
+
* Add a seed phrase for a given SeedPhraseId
|
|
13
|
+
* @param {SeedPhraseId} id
|
|
14
|
+
* @returns Promise
|
|
15
|
+
*/
|
|
16
|
+
add(id: SeedPhraseId): Promise<string>
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* For a given DappId, find its wallet or create a new wallet and return its walletId
|
|
20
|
+
* @param {SeedPhraseId} dappName
|
|
21
|
+
* @returns Promise
|
|
22
|
+
*/
|
|
23
|
+
findOrCreate(dappName: SeedPhraseId): Promise<WalletId>
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Request a seed phrase by supplying a walletId. Note: In the future, the OS might require verification along side the walletId
|
|
27
|
+
*
|
|
28
|
+
* @param {WalletId} walletId
|
|
29
|
+
* @returns DappWalletSeedPhrase
|
|
30
|
+
*/
|
|
31
|
+
request(walletId: WalletId): Promise<SeedPhrase | undefined>
|
|
32
|
+
}
|
package/typedoc.json
ADDED