@credebl/ssi-mobile-core 2.0.2-alpha-20251027052801
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/README.md +194 -0
- package/build/index.d.mts +104 -0
- package/build/index.d.ts +104 -0
- package/build/index.js +315 -0
- package/build/index.js.map +1 -0
- package/build/index.mjs +269 -0
- package/build/index.mjs.map +1 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# Mobile SDK Core - React Context Integration
|
|
2
|
+
|
|
3
|
+
This package provides React context integration for the MobileSDK, allowing you to access the SDK instance globally throughout your React application.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Global SDK Access**: Access the MobileSDK instance from any component
|
|
8
|
+
- **Type Safety**: Full TypeScript support with proper typing
|
|
9
|
+
- **State Management**: Built-in state management for SDK initialization
|
|
10
|
+
- **React Integration**: Follows React best practices and patterns
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @credebl/mobile-sdk-core
|
|
16
|
+
# or
|
|
17
|
+
yarn add @credebl/mobile-sdk-core
|
|
18
|
+
# or
|
|
19
|
+
pnpm add @credebl/mobile-sdk-core
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
### 1. Wrap your app with the SDKProvider
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import { SDKProvider } from '@credebl/mobile-sdk-core';
|
|
28
|
+
|
|
29
|
+
function App() {
|
|
30
|
+
return (
|
|
31
|
+
<SDKProvider>
|
|
32
|
+
{/* Your app components */}
|
|
33
|
+
</SDKProvider>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 2. Initialize the SDK in a component
|
|
39
|
+
|
|
40
|
+
```tsx
|
|
41
|
+
import { useSDKInitializer } from '@credebl/mobile-sdk-core';
|
|
42
|
+
|
|
43
|
+
function AppInitializer() {
|
|
44
|
+
const { initializeSDK, isInitialized } = useSDKInitializer();
|
|
45
|
+
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
if (!isInitialized) {
|
|
48
|
+
initializeSDK();
|
|
49
|
+
}
|
|
50
|
+
}, [initializeSDK, isInitialized]);
|
|
51
|
+
|
|
52
|
+
return null; // This component doesn't render anything
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 3. Use the SDK anywhere in your app
|
|
57
|
+
|
|
58
|
+
```tsx
|
|
59
|
+
import { useSDK } from '@credebl/mobile-sdk-core';
|
|
60
|
+
|
|
61
|
+
function SomeComponent() {
|
|
62
|
+
const { sdk, isInitialized } = useSDK();
|
|
63
|
+
|
|
64
|
+
if (!isInitialized || !sdk) {
|
|
65
|
+
return <div>SDK not initialized</div>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const handleAction = () => {
|
|
69
|
+
// Use sdk methods here
|
|
70
|
+
console.log('SDK is ready:', sdk);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return <button onClick={handleAction}>Perform Action</button>;
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## API Reference
|
|
78
|
+
|
|
79
|
+
### SDKProvider
|
|
80
|
+
|
|
81
|
+
The main context provider that wraps your app and provides the SDK context.
|
|
82
|
+
|
|
83
|
+
**Props:**
|
|
84
|
+
- `children`: React nodes to be wrapped by the provider
|
|
85
|
+
|
|
86
|
+
### useSDK
|
|
87
|
+
|
|
88
|
+
Hook to access the SDK context. Must be used within an SDKProvider.
|
|
89
|
+
|
|
90
|
+
**Returns:**
|
|
91
|
+
- `sdk`: The MobileSDK instance or null if not initialized
|
|
92
|
+
- `setSDK`: Function to set the SDK instance
|
|
93
|
+
- `isInitialized`: Boolean indicating if the SDK is initialized
|
|
94
|
+
|
|
95
|
+
### useSDKInitializer
|
|
96
|
+
|
|
97
|
+
Hook to handle SDK initialization logic.
|
|
98
|
+
|
|
99
|
+
**Returns:**
|
|
100
|
+
- `initializeSDK`: Function to initialize the SDK
|
|
101
|
+
- `isInitialized`: Boolean indicating if the SDK is initialized
|
|
102
|
+
|
|
103
|
+
## Example Implementation
|
|
104
|
+
|
|
105
|
+
Here's a complete example of how to set up and use the SDK context:
|
|
106
|
+
|
|
107
|
+
```tsx
|
|
108
|
+
import React, { useEffect } from 'react';
|
|
109
|
+
import { SDKProvider, useSDK, useSDKInitializer } from '@credebl/mobile-sdk-core';
|
|
110
|
+
|
|
111
|
+
// Component to initialize the SDK
|
|
112
|
+
function SDKInitializer() {
|
|
113
|
+
const { initializeSDK, isInitialized } = useSDKInitializer();
|
|
114
|
+
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
if (!isInitialized) {
|
|
117
|
+
initializeSDK();
|
|
118
|
+
}
|
|
119
|
+
}, [initializeSDK, isInitialized]);
|
|
120
|
+
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Component that uses the SDK
|
|
125
|
+
function SDKUser() {
|
|
126
|
+
const { sdk, isInitialized } = useSDK();
|
|
127
|
+
|
|
128
|
+
if (!isInitialized || !sdk) {
|
|
129
|
+
return <div>Loading SDK...</div>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<div>
|
|
134
|
+
<h2>SDK Ready!</h2>
|
|
135
|
+
<p>SDK instance: {sdk.constructor.name}</p>
|
|
136
|
+
</div>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Main app component
|
|
141
|
+
function App() {
|
|
142
|
+
return (
|
|
143
|
+
<SDKProvider>
|
|
144
|
+
<SDKInitializer />
|
|
145
|
+
<SDKUser />
|
|
146
|
+
</SDKProvider>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export default App;
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Error Handling
|
|
154
|
+
|
|
155
|
+
The `useSDK` hook will throw an error if used outside of an SDKProvider:
|
|
156
|
+
|
|
157
|
+
```tsx
|
|
158
|
+
function ComponentOutsideProvider() {
|
|
159
|
+
try {
|
|
160
|
+
const { sdk } = useSDK(); // This will throw an error
|
|
161
|
+
} catch (error) {
|
|
162
|
+
console.error('SDK context not available:', error.message);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Best Practices
|
|
168
|
+
|
|
169
|
+
1. **Initialize Early**: Initialize the SDK as early as possible in your app lifecycle
|
|
170
|
+
2. **Error Boundaries**: Wrap your app with error boundaries to handle SDK initialization errors
|
|
171
|
+
3. **Loading States**: Always check `isInitialized` before using the SDK
|
|
172
|
+
4. **Type Safety**: Use TypeScript for better development experience and error catching
|
|
173
|
+
|
|
174
|
+
## Troubleshooting
|
|
175
|
+
|
|
176
|
+
### Common Issues
|
|
177
|
+
|
|
178
|
+
1. **"useSDK must be used within an SDKProvider"**
|
|
179
|
+
- Ensure your component is wrapped with SDKProvider
|
|
180
|
+
- Check that the provider is imported correctly
|
|
181
|
+
|
|
182
|
+
2. **SDK not initializing**
|
|
183
|
+
- Verify that `initializeSDK()` is being called
|
|
184
|
+
- Check browser console for any errors
|
|
185
|
+
- Ensure all required dependencies are installed
|
|
186
|
+
|
|
187
|
+
3. **Type errors**
|
|
188
|
+
- Make sure you're using TypeScript
|
|
189
|
+
- Check that all imports are correct
|
|
190
|
+
- Verify that the MobileSDK class is properly exported
|
|
191
|
+
|
|
192
|
+
## Contributing
|
|
193
|
+
|
|
194
|
+
This package is part of the CredeBL Mobile SDK ecosystem. For contributions, please refer to the main project documentation.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import * as _credo_ts_core0 from "@credo-ts/core";
|
|
2
|
+
import { Agent, BaseRecord, ConsoleLogger, DidCreateOptions, InitConfig, KeyDidCreateOptions, KeyType, LogLevel, TagValue } from "@credo-ts/core";
|
|
3
|
+
import React$1, { PropsWithChildren, ReactNode } from "react";
|
|
4
|
+
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
5
|
+
import * as rxjs0 from "rxjs";
|
|
6
|
+
import { AgentModulesInput } from "@credo-ts/core/build/agent/AgentModules";
|
|
7
|
+
import { Constructor } from "@credo-ts/core/build/utils/mixins";
|
|
8
|
+
|
|
9
|
+
//#region src/MobileSDK.d.ts
|
|
10
|
+
interface MobileSDKModule {
|
|
11
|
+
initialize(agent: Agent): void;
|
|
12
|
+
getAgentModules(): AgentModulesInput;
|
|
13
|
+
}
|
|
14
|
+
type MobileSDKOptions<T extends Record<string, MobileSDKModule> = Record<string, MobileSDKModule>> = {
|
|
15
|
+
agentConfig: InitConfig;
|
|
16
|
+
modules: T;
|
|
17
|
+
defaultModules?: AgentModulesInput;
|
|
18
|
+
};
|
|
19
|
+
declare class MobileSDK<T extends Record<string, MobileSDKModule> = Record<string, MobileSDKModule>> {
|
|
20
|
+
private localAgent;
|
|
21
|
+
readonly configuration: MobileSDKOptions<T>;
|
|
22
|
+
readonly modules: T;
|
|
23
|
+
constructor(options: MobileSDKOptions<T>);
|
|
24
|
+
initialize(): Promise<Agent<_credo_ts_core0.ModulesMap>>;
|
|
25
|
+
get agent(): Agent<any> | null;
|
|
26
|
+
assertAndGetAgent(): Agent<any>;
|
|
27
|
+
static AppProvider({
|
|
28
|
+
children
|
|
29
|
+
}: PropsWithChildren): react_jsx_runtime0.JSX.Element;
|
|
30
|
+
createDid<T extends DidCreateOptions>(options: T): Promise<_credo_ts_core0.DidCreateResult<_credo_ts_core0.DidOperationStateActionBase>>;
|
|
31
|
+
getDids({
|
|
32
|
+
method,
|
|
33
|
+
did,
|
|
34
|
+
tag,
|
|
35
|
+
tagValue
|
|
36
|
+
}: {
|
|
37
|
+
method?: string;
|
|
38
|
+
did?: string;
|
|
39
|
+
tag?: string;
|
|
40
|
+
tagValue?: TagValue;
|
|
41
|
+
}): Promise<_credo_ts_core0.DidRecord[]>;
|
|
42
|
+
addTagToDid({
|
|
43
|
+
did,
|
|
44
|
+
tag,
|
|
45
|
+
tagValue
|
|
46
|
+
}: {
|
|
47
|
+
did: string;
|
|
48
|
+
tag: string;
|
|
49
|
+
tagValue: TagValue;
|
|
50
|
+
}): Promise<_credo_ts_core0.DidRecord>;
|
|
51
|
+
resolveDid({
|
|
52
|
+
did
|
|
53
|
+
}: {
|
|
54
|
+
did: string;
|
|
55
|
+
}): Promise<_credo_ts_core0.DidResolutionResult>;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/providers/recordUtils.d.ts
|
|
59
|
+
type BaseRecordAny = BaseRecord<any, any, any>;
|
|
60
|
+
type RecordClass<R extends BaseRecordAny> = Constructor<R> & {
|
|
61
|
+
type: string;
|
|
62
|
+
};
|
|
63
|
+
interface RecordsState<R extends BaseRecordAny> {
|
|
64
|
+
loading: boolean;
|
|
65
|
+
records: R[];
|
|
66
|
+
}
|
|
67
|
+
declare const addRecord: <R extends BaseRecordAny>(record: R, state: RecordsState<R>) => RecordsState<R>;
|
|
68
|
+
declare const updateRecord: <R extends BaseRecordAny>(record: R, state: RecordsState<R>) => RecordsState<R>;
|
|
69
|
+
declare const removeRecord: <R extends BaseRecordAny>(record: R | {
|
|
70
|
+
id: string;
|
|
71
|
+
type: R["type"];
|
|
72
|
+
}, state: RecordsState<R>) => RecordsState<R>;
|
|
73
|
+
declare const recordsAddedByType: <R extends BaseRecordAny>(agent: Agent | undefined, recordClass: RecordClass<R>) => rxjs0.Observable<R>;
|
|
74
|
+
declare const recordsUpdatedByType: <R extends BaseRecordAny>(agent: Agent | undefined, recordClass: RecordClass<R>) => rxjs0.Observable<R>;
|
|
75
|
+
declare const recordsRemovedByType: <R extends BaseRecordAny>(agent: Agent | undefined, recordClass: RecordClass<R>) => rxjs0.Observable<R>;
|
|
76
|
+
declare const isModuleRegistered: (agent: Agent, ModuleClass: Constructor) => boolean;
|
|
77
|
+
declare const useIsModuleRegistered: (agent: Agent, ModuleClass: Constructor) => boolean;
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/providers/AgentProvider.d.ts
|
|
80
|
+
declare const useAgent: <AppAgent extends Agent = Agent>() => {
|
|
81
|
+
agent: Agent<any>;
|
|
82
|
+
};
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/contexts/MobileSDKContext.d.ts
|
|
85
|
+
declare const useMobileSDK: <T extends Record<string, MobileSDKModule> = Record<string, MobileSDKModule>>() => {
|
|
86
|
+
sdk: MobileSDK<T>;
|
|
87
|
+
isInitialized: boolean;
|
|
88
|
+
initialize: (sdk: MobileSDK) => void;
|
|
89
|
+
shutdown: () => void;
|
|
90
|
+
};
|
|
91
|
+
interface SDKProviderProps {
|
|
92
|
+
children: ReactNode;
|
|
93
|
+
}
|
|
94
|
+
declare const MobileSDKProvider: React$1.FC<SDKProviderProps>;
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/hooks/useSDKInitializer.d.ts
|
|
97
|
+
declare const useMobileSDKInitializer: () => {
|
|
98
|
+
initializeSDK: (options: MobileSDKOptions) => Promise<MobileSDK<Record<string, MobileSDKModule>>>;
|
|
99
|
+
isInitialized: boolean;
|
|
100
|
+
sdk: MobileSDK<Record<string, MobileSDKModule>>;
|
|
101
|
+
};
|
|
102
|
+
//#endregion
|
|
103
|
+
export { BaseRecordAny, ConsoleLogger, KeyDidCreateOptions, KeyType, LogLevel, MobileSDK, MobileSDKModule, MobileSDKOptions, MobileSDKProvider, RecordsState, addRecord, isModuleRegistered, recordsAddedByType, recordsRemovedByType, recordsUpdatedByType, removeRecord, updateRecord, useAgent, useIsModuleRegistered, useMobileSDK, useMobileSDKInitializer };
|
|
104
|
+
//# sourceMappingURL=index.d.mts.map
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import * as _credo_ts_core0 from "@credo-ts/core";
|
|
2
|
+
import { Agent, BaseRecord, ConsoleLogger, DidCreateOptions, InitConfig, KeyDidCreateOptions, KeyType, LogLevel, TagValue } from "@credo-ts/core";
|
|
3
|
+
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
4
|
+
import { AgentModulesInput } from "@credo-ts/core/build/agent/AgentModules";
|
|
5
|
+
import React$1, { PropsWithChildren, ReactNode } from "react";
|
|
6
|
+
import * as rxjs0 from "rxjs";
|
|
7
|
+
import { Constructor } from "@credo-ts/core/build/utils/mixins";
|
|
8
|
+
|
|
9
|
+
//#region src/MobileSDK.d.ts
|
|
10
|
+
interface MobileSDKModule {
|
|
11
|
+
initialize(agent: Agent): void;
|
|
12
|
+
getAgentModules(): AgentModulesInput;
|
|
13
|
+
}
|
|
14
|
+
type MobileSDKOptions<T extends Record<string, MobileSDKModule> = Record<string, MobileSDKModule>> = {
|
|
15
|
+
agentConfig: InitConfig;
|
|
16
|
+
modules: T;
|
|
17
|
+
defaultModules?: AgentModulesInput;
|
|
18
|
+
};
|
|
19
|
+
declare class MobileSDK<T extends Record<string, MobileSDKModule> = Record<string, MobileSDKModule>> {
|
|
20
|
+
private localAgent;
|
|
21
|
+
readonly configuration: MobileSDKOptions<T>;
|
|
22
|
+
readonly modules: T;
|
|
23
|
+
constructor(options: MobileSDKOptions<T>);
|
|
24
|
+
initialize(): Promise<Agent<_credo_ts_core0.ModulesMap>>;
|
|
25
|
+
get agent(): Agent<any> | null;
|
|
26
|
+
assertAndGetAgent(): Agent<any>;
|
|
27
|
+
static AppProvider({
|
|
28
|
+
children
|
|
29
|
+
}: PropsWithChildren): react_jsx_runtime0.JSX.Element;
|
|
30
|
+
createDid<T extends DidCreateOptions>(options: T): Promise<_credo_ts_core0.DidCreateResult<_credo_ts_core0.DidOperationStateActionBase>>;
|
|
31
|
+
getDids({
|
|
32
|
+
method,
|
|
33
|
+
did,
|
|
34
|
+
tag,
|
|
35
|
+
tagValue
|
|
36
|
+
}: {
|
|
37
|
+
method?: string;
|
|
38
|
+
did?: string;
|
|
39
|
+
tag?: string;
|
|
40
|
+
tagValue?: TagValue;
|
|
41
|
+
}): Promise<_credo_ts_core0.DidRecord[]>;
|
|
42
|
+
addTagToDid({
|
|
43
|
+
did,
|
|
44
|
+
tag,
|
|
45
|
+
tagValue
|
|
46
|
+
}: {
|
|
47
|
+
did: string;
|
|
48
|
+
tag: string;
|
|
49
|
+
tagValue: TagValue;
|
|
50
|
+
}): Promise<_credo_ts_core0.DidRecord>;
|
|
51
|
+
resolveDid({
|
|
52
|
+
did
|
|
53
|
+
}: {
|
|
54
|
+
did: string;
|
|
55
|
+
}): Promise<_credo_ts_core0.DidResolutionResult>;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/providers/recordUtils.d.ts
|
|
59
|
+
type BaseRecordAny = BaseRecord<any, any, any>;
|
|
60
|
+
type RecordClass<R extends BaseRecordAny> = Constructor<R> & {
|
|
61
|
+
type: string;
|
|
62
|
+
};
|
|
63
|
+
interface RecordsState<R extends BaseRecordAny> {
|
|
64
|
+
loading: boolean;
|
|
65
|
+
records: R[];
|
|
66
|
+
}
|
|
67
|
+
declare const addRecord: <R extends BaseRecordAny>(record: R, state: RecordsState<R>) => RecordsState<R>;
|
|
68
|
+
declare const updateRecord: <R extends BaseRecordAny>(record: R, state: RecordsState<R>) => RecordsState<R>;
|
|
69
|
+
declare const removeRecord: <R extends BaseRecordAny>(record: R | {
|
|
70
|
+
id: string;
|
|
71
|
+
type: R["type"];
|
|
72
|
+
}, state: RecordsState<R>) => RecordsState<R>;
|
|
73
|
+
declare const recordsAddedByType: <R extends BaseRecordAny>(agent: Agent | undefined, recordClass: RecordClass<R>) => rxjs0.Observable<R>;
|
|
74
|
+
declare const recordsUpdatedByType: <R extends BaseRecordAny>(agent: Agent | undefined, recordClass: RecordClass<R>) => rxjs0.Observable<R>;
|
|
75
|
+
declare const recordsRemovedByType: <R extends BaseRecordAny>(agent: Agent | undefined, recordClass: RecordClass<R>) => rxjs0.Observable<R>;
|
|
76
|
+
declare const isModuleRegistered: (agent: Agent, ModuleClass: Constructor) => boolean;
|
|
77
|
+
declare const useIsModuleRegistered: (agent: Agent, ModuleClass: Constructor) => boolean;
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/providers/AgentProvider.d.ts
|
|
80
|
+
declare const useAgent: <AppAgent extends Agent = Agent>() => {
|
|
81
|
+
agent: Agent<any>;
|
|
82
|
+
};
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/contexts/MobileSDKContext.d.ts
|
|
85
|
+
declare const useMobileSDK: <T extends Record<string, MobileSDKModule> = Record<string, MobileSDKModule>>() => {
|
|
86
|
+
sdk: MobileSDK<T>;
|
|
87
|
+
isInitialized: boolean;
|
|
88
|
+
initialize: (sdk: MobileSDK) => void;
|
|
89
|
+
shutdown: () => void;
|
|
90
|
+
};
|
|
91
|
+
interface SDKProviderProps {
|
|
92
|
+
children: ReactNode;
|
|
93
|
+
}
|
|
94
|
+
declare const MobileSDKProvider: React$1.FC<SDKProviderProps>;
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/hooks/useSDKInitializer.d.ts
|
|
97
|
+
declare const useMobileSDKInitializer: () => {
|
|
98
|
+
initializeSDK: (options: MobileSDKOptions) => Promise<MobileSDK<Record<string, MobileSDKModule>>>;
|
|
99
|
+
isInitialized: boolean;
|
|
100
|
+
sdk: MobileSDK<Record<string, MobileSDKModule>>;
|
|
101
|
+
};
|
|
102
|
+
//#endregion
|
|
103
|
+
export { BaseRecordAny, ConsoleLogger, KeyDidCreateOptions, KeyType, LogLevel, MobileSDK, MobileSDKModule, MobileSDKOptions, MobileSDKProvider, RecordsState, addRecord, isModuleRegistered, recordsAddedByType, recordsRemovedByType, recordsUpdatedByType, removeRecord, updateRecord, useAgent, useIsModuleRegistered, useMobileSDK, useMobileSDKInitializer };
|
|
104
|
+
//# sourceMappingURL=index.d.ts.map
|
package/build/index.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
let __credo_ts_core = require("@credo-ts/core");
|
|
25
|
+
__credo_ts_core = __toESM(__credo_ts_core);
|
|
26
|
+
let __credo_ts_askar = require("@credo-ts/askar");
|
|
27
|
+
__credo_ts_askar = __toESM(__credo_ts_askar);
|
|
28
|
+
let __credo_ts_react_native = require("@credo-ts/react-native");
|
|
29
|
+
__credo_ts_react_native = __toESM(__credo_ts_react_native);
|
|
30
|
+
let __openwallet_foundation_askar_react_native = require("@openwallet-foundation/askar-react-native");
|
|
31
|
+
__openwallet_foundation_askar_react_native = __toESM(__openwallet_foundation_askar_react_native);
|
|
32
|
+
let react = require("react");
|
|
33
|
+
react = __toESM(react);
|
|
34
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
35
|
+
react_jsx_runtime = __toESM(react_jsx_runtime);
|
|
36
|
+
let rxjs = require("rxjs");
|
|
37
|
+
rxjs = __toESM(rxjs);
|
|
38
|
+
|
|
39
|
+
//#region src/contexts/MobileSDKContext.tsx
|
|
40
|
+
const MobileSDKContext = (0, react.createContext)(void 0);
|
|
41
|
+
const useMobileSDK = () => {
|
|
42
|
+
const context = (0, react.useContext)(MobileSDKContext);
|
|
43
|
+
if (context === void 0) throw new Error("useMobileSDK must be used within an MobileSDKProvider");
|
|
44
|
+
return {
|
|
45
|
+
sdk: context.sdk,
|
|
46
|
+
isInitialized: context.isInitialized,
|
|
47
|
+
initialize: context.initialize,
|
|
48
|
+
shutdown: context.shutdown
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const MobileSDKProvider = ({ children }) => {
|
|
52
|
+
const [sdk, setSDK] = (0, react.useState)(null);
|
|
53
|
+
const [isInitialized, setIsInitialized] = (0, react.useState)(false);
|
|
54
|
+
const initialize = (newSDK) => {
|
|
55
|
+
setSDK(newSDK);
|
|
56
|
+
setIsInitialized(true);
|
|
57
|
+
};
|
|
58
|
+
const shutdown = () => {
|
|
59
|
+
setSDK(null);
|
|
60
|
+
setIsInitialized(false);
|
|
61
|
+
};
|
|
62
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MobileSDKContext.Provider, {
|
|
63
|
+
value: {
|
|
64
|
+
sdk,
|
|
65
|
+
isInitialized,
|
|
66
|
+
initialize,
|
|
67
|
+
shutdown
|
|
68
|
+
},
|
|
69
|
+
children
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/providers/recordUtils.ts
|
|
75
|
+
const addRecord = (record, state) => {
|
|
76
|
+
const newRecordsState = [...state.records];
|
|
77
|
+
newRecordsState.unshift(record);
|
|
78
|
+
return {
|
|
79
|
+
loading: state.loading,
|
|
80
|
+
records: newRecordsState
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
const updateRecord = (record, state) => {
|
|
84
|
+
const newRecordsState = [...state.records];
|
|
85
|
+
const index = newRecordsState.findIndex((r) => r.id === record.id);
|
|
86
|
+
if (index > -1) newRecordsState[index] = record;
|
|
87
|
+
return {
|
|
88
|
+
loading: state.loading,
|
|
89
|
+
records: newRecordsState
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
const removeRecord = (record, state) => {
|
|
93
|
+
const newRecordsState = state.records.filter((r) => r.id !== record.id);
|
|
94
|
+
return {
|
|
95
|
+
loading: state.loading,
|
|
96
|
+
records: newRecordsState
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
const filterByType = (recordClass) => {
|
|
100
|
+
return (0, rxjs.pipe)((0, rxjs.map)((event) => event.payload.record), (0, rxjs.filter)((record) => record.type === recordClass.type));
|
|
101
|
+
};
|
|
102
|
+
const recordsAddedByType = (agent, recordClass) => {
|
|
103
|
+
if (!agent) throw new Error("Agent is required to check record type");
|
|
104
|
+
if (!recordClass) throw new Error("The recordClass can't be undefined");
|
|
105
|
+
return agent?.events.observable(__credo_ts_core.RepositoryEventTypes.RecordSaved).pipe(filterByType(recordClass));
|
|
106
|
+
};
|
|
107
|
+
const recordsUpdatedByType = (agent, recordClass) => {
|
|
108
|
+
if (!agent) throw new Error("Agent is required to update record type");
|
|
109
|
+
if (!recordClass) throw new Error("The recordClass can't be undefined");
|
|
110
|
+
return agent?.events.observable(__credo_ts_core.RepositoryEventTypes.RecordUpdated).pipe(filterByType(recordClass));
|
|
111
|
+
};
|
|
112
|
+
const recordsRemovedByType = (agent, recordClass) => {
|
|
113
|
+
if (!agent) throw new Error("Agent is required to remove records by type");
|
|
114
|
+
if (!recordClass) throw new Error("The recordClass can't be undefined");
|
|
115
|
+
return agent?.events.observable(__credo_ts_core.RepositoryEventTypes.RecordDeleted).pipe(filterByType(recordClass));
|
|
116
|
+
};
|
|
117
|
+
const isModuleRegistered = (agent, ModuleClass) => {
|
|
118
|
+
if (!agent) throw new Error("Agent is required to check if a module is enabled");
|
|
119
|
+
return Object.values(agent.dependencyManager.registeredModules).find((module$1) => module$1 instanceof ModuleClass) !== void 0;
|
|
120
|
+
};
|
|
121
|
+
const useIsModuleRegistered = (agent, ModuleClass) => {
|
|
122
|
+
return (0, react.useMemo)(() => isModuleRegistered(agent, ModuleClass), [agent, ModuleClass]);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/providers/W3cCredentialsProvider.tsx
|
|
127
|
+
const addRecord$1 = (record, state) => {
|
|
128
|
+
const newRecordsState = [...state.w3cCredentialRecords];
|
|
129
|
+
newRecordsState.unshift(record);
|
|
130
|
+
return {
|
|
131
|
+
isLoading: state.isLoading,
|
|
132
|
+
w3cCredentialRecords: newRecordsState
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
const updateRecord$1 = (record, state) => {
|
|
136
|
+
const newRecordsState = [...state.w3cCredentialRecords];
|
|
137
|
+
const index = newRecordsState.findIndex((r) => r.id === record.id);
|
|
138
|
+
if (index > -1) newRecordsState[index] = record;
|
|
139
|
+
return {
|
|
140
|
+
isLoading: state.isLoading,
|
|
141
|
+
w3cCredentialRecords: newRecordsState
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
const removeRecord$1 = (record, state) => {
|
|
145
|
+
const newRecordsState = state.w3cCredentialRecords.filter((r) => r.id !== record.id);
|
|
146
|
+
return {
|
|
147
|
+
isLoading: state.isLoading,
|
|
148
|
+
w3cCredentialRecords: newRecordsState
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
const W3cCredentialRecordContext = (0, react.createContext)(void 0);
|
|
152
|
+
const W3cCredentialRecordProvider = ({ agent, children }) => {
|
|
153
|
+
const [state, setState] = (0, react.useState)({
|
|
154
|
+
w3cCredentialRecords: [],
|
|
155
|
+
isLoading: true
|
|
156
|
+
});
|
|
157
|
+
(0, react.useEffect)(() => {
|
|
158
|
+
agent.w3cCredentials.getAllCredentialRecords().then((w3cCredentialRecords) => setState({
|
|
159
|
+
w3cCredentialRecords,
|
|
160
|
+
isLoading: false
|
|
161
|
+
}));
|
|
162
|
+
}, [agent]);
|
|
163
|
+
(0, react.useEffect)(() => {
|
|
164
|
+
if (!state.isLoading && agent) {
|
|
165
|
+
const credentialAdded$ = recordsAddedByType(agent, __credo_ts_core.W3cCredentialRecord).subscribe((record) => setState(addRecord$1(record, state)));
|
|
166
|
+
const credentialUpdate$ = recordsUpdatedByType(agent, __credo_ts_core.W3cCredentialRecord).subscribe((record) => setState(updateRecord$1(record, state)));
|
|
167
|
+
const credentialRemove$ = recordsRemovedByType(agent, __credo_ts_core.W3cCredentialRecord).subscribe((record) => setState(removeRecord$1(record, state)));
|
|
168
|
+
return () => {
|
|
169
|
+
credentialAdded$.unsubscribe();
|
|
170
|
+
credentialUpdate$.unsubscribe();
|
|
171
|
+
credentialRemove$.unsubscribe();
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}, [state, agent]);
|
|
175
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(W3cCredentialRecordContext.Provider, {
|
|
176
|
+
value: state,
|
|
177
|
+
children
|
|
178
|
+
});
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
//#endregion
|
|
182
|
+
//#region src/providers/AgentProvider.tsx
|
|
183
|
+
const AgentContext = (0, react.createContext)(void 0);
|
|
184
|
+
const useAgent = () => {
|
|
185
|
+
const agentContext = (0, react.useContext)(AgentContext);
|
|
186
|
+
if (!agentContext) throw new Error("useAgent must be used within a AgentContextProvider");
|
|
187
|
+
return { agent: agentContext };
|
|
188
|
+
};
|
|
189
|
+
const AgentProvider = ({ agent, children }) => {
|
|
190
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(AgentContext.Provider, {
|
|
191
|
+
value: agent,
|
|
192
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(W3cCredentialRecordProvider, {
|
|
193
|
+
agent,
|
|
194
|
+
children
|
|
195
|
+
})
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
var AgentProvider_default = AgentProvider;
|
|
199
|
+
|
|
200
|
+
//#endregion
|
|
201
|
+
//#region src/MobileSDK.tsx
|
|
202
|
+
const getCoreModules = () => {
|
|
203
|
+
return {
|
|
204
|
+
askar: new __credo_ts_askar.AskarModule({ askar: __openwallet_foundation_askar_react_native.askar }),
|
|
205
|
+
dids: new __credo_ts_core.DidsModule({
|
|
206
|
+
registrars: [new __credo_ts_core.JwkDidRegistrar(), new __credo_ts_core.KeyDidRegistrar()],
|
|
207
|
+
resolvers: [new __credo_ts_core.JwkDidResolver(), new __credo_ts_core.KeyDidResolver()]
|
|
208
|
+
}),
|
|
209
|
+
cache: new __credo_ts_core.CacheModule({ cache: new __credo_ts_core.SingleContextStorageLruCache({ limit: 50 }) })
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
var MobileSDK = class {
|
|
213
|
+
constructor(options) {
|
|
214
|
+
this.localAgent = null;
|
|
215
|
+
this.configuration = options;
|
|
216
|
+
this.modules = options.modules;
|
|
217
|
+
}
|
|
218
|
+
async initialize() {
|
|
219
|
+
const defaultModules = this.configuration.defaultModules ?? {};
|
|
220
|
+
const coreModules = getCoreModules();
|
|
221
|
+
Object.assign(defaultModules, coreModules);
|
|
222
|
+
const modules = Object.entries(this.configuration.modules).reduce((acc, [, module$1]) => {
|
|
223
|
+
const moduleModules = module$1.getAgentModules();
|
|
224
|
+
Object.assign(acc, moduleModules);
|
|
225
|
+
return acc;
|
|
226
|
+
}, defaultModules);
|
|
227
|
+
const agent = new __credo_ts_core.Agent({
|
|
228
|
+
config: this.configuration.agentConfig,
|
|
229
|
+
dependencies: __credo_ts_react_native.agentDependencies,
|
|
230
|
+
modules
|
|
231
|
+
});
|
|
232
|
+
await agent.initialize();
|
|
233
|
+
for await (const [, module$1] of Object.entries(this.configuration.modules)) module$1.initialize(agent);
|
|
234
|
+
this.localAgent = agent;
|
|
235
|
+
return agent;
|
|
236
|
+
}
|
|
237
|
+
get agent() {
|
|
238
|
+
return this.localAgent;
|
|
239
|
+
}
|
|
240
|
+
assertAndGetAgent() {
|
|
241
|
+
if (!this.agent) throw new Error("Agent not initialized");
|
|
242
|
+
return this.agent;
|
|
243
|
+
}
|
|
244
|
+
static AppProvider({ children }) {
|
|
245
|
+
const { sdk } = useMobileSDK();
|
|
246
|
+
if (!sdk?.agent) throw new Error("Mobile SDK not initialized");
|
|
247
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(AgentProvider_default, {
|
|
248
|
+
agent: sdk.agent,
|
|
249
|
+
children
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
async createDid(options) {
|
|
253
|
+
return await this.assertAndGetAgent().dids.create(options);
|
|
254
|
+
}
|
|
255
|
+
async getDids({ method, did, tag, tagValue }) {
|
|
256
|
+
const agent = this.assertAndGetAgent();
|
|
257
|
+
if (tag) {
|
|
258
|
+
const didRecord = await (await agent.dependencyManager.resolve(__credo_ts_core.DidRepository)).findSingleByQuery(agent.context, tagValue ? { [tag]: tagValue } : { tag });
|
|
259
|
+
return didRecord ? [didRecord] : [];
|
|
260
|
+
}
|
|
261
|
+
return await agent.dids.getCreatedDids({
|
|
262
|
+
method,
|
|
263
|
+
did
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
async addTagToDid({ did, tag, tagValue }) {
|
|
267
|
+
const agent = this.assertAndGetAgent();
|
|
268
|
+
const didRecords = await this.getDids({ did });
|
|
269
|
+
if (didRecords.length === 0) throw new Error("Did not found");
|
|
270
|
+
const didRecord = didRecords[0];
|
|
271
|
+
await didRecord.setTag(tag, tagValue);
|
|
272
|
+
await (await agent.dependencyManager.resolve(__credo_ts_core.DidRepository)).update(agent.context, didRecord);
|
|
273
|
+
return didRecord;
|
|
274
|
+
}
|
|
275
|
+
async resolveDid({ did }) {
|
|
276
|
+
return await this.assertAndGetAgent().dids.resolve(did);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
//#endregion
|
|
281
|
+
//#region src/hooks/useSDKInitializer.ts
|
|
282
|
+
const useMobileSDKInitializer = () => {
|
|
283
|
+
const { initialize, isInitialized, sdk } = useMobileSDK();
|
|
284
|
+
return {
|
|
285
|
+
initializeSDK: (0, react.useCallback)(async (options) => {
|
|
286
|
+
if (isInitialized) throw new Error("Mobile SDK is already initialized");
|
|
287
|
+
const sdk$1 = new MobileSDK(options);
|
|
288
|
+
await sdk$1.initialize();
|
|
289
|
+
initialize(sdk$1);
|
|
290
|
+
return sdk$1;
|
|
291
|
+
}, [isInitialized, initialize]),
|
|
292
|
+
isInitialized,
|
|
293
|
+
sdk
|
|
294
|
+
};
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
//#endregion
|
|
298
|
+
exports.ConsoleLogger = __credo_ts_core.ConsoleLogger;
|
|
299
|
+
exports.KeyDidCreateOptions = __credo_ts_core.KeyDidCreateOptions;
|
|
300
|
+
exports.KeyType = __credo_ts_core.KeyType;
|
|
301
|
+
exports.LogLevel = __credo_ts_core.LogLevel;
|
|
302
|
+
exports.MobileSDK = MobileSDK;
|
|
303
|
+
exports.MobileSDKProvider = MobileSDKProvider;
|
|
304
|
+
exports.addRecord = addRecord;
|
|
305
|
+
exports.isModuleRegistered = isModuleRegistered;
|
|
306
|
+
exports.recordsAddedByType = recordsAddedByType;
|
|
307
|
+
exports.recordsRemovedByType = recordsRemovedByType;
|
|
308
|
+
exports.recordsUpdatedByType = recordsUpdatedByType;
|
|
309
|
+
exports.removeRecord = removeRecord;
|
|
310
|
+
exports.updateRecord = updateRecord;
|
|
311
|
+
exports.useAgent = useAgent;
|
|
312
|
+
exports.useIsModuleRegistered = useIsModuleRegistered;
|
|
313
|
+
exports.useMobileSDK = useMobileSDK;
|
|
314
|
+
exports.useMobileSDKInitializer = useMobileSDKInitializer;
|
|
315
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["MobileSDKProvider: React.FC<SDKProviderProps>","RepositoryEventTypes","module","addRecord","updateRecord","removeRecord","W3cCredentialRecordProvider: React.FC<PropsWithChildren<Props>>","W3cCredentialRecord","AgentProvider: React.FC<PropsWithChildren<Props>>","AskarModule","DidsModule","JwkDidRegistrar","KeyDidRegistrar","JwkDidResolver","KeyDidResolver","CacheModule","SingleContextStorageLruCache","module","Agent","agentDependencies","AgentProvider","DidRepository","sdk"],"sources":["../src/contexts/MobileSDKContext.tsx","../src/providers/recordUtils.ts","../src/providers/W3cCredentialsProvider.tsx","../src/providers/AgentProvider.tsx","../src/MobileSDK.tsx","../src/hooks/useSDKInitializer.ts"],"sourcesContent":["import type React from 'react'\nimport { type ReactNode, createContext, useContext, useState } from 'react'\nimport type { MobileSDK, MobileSDKModule } from '../MobileSDK'\n\ninterface MobileSDKContextType {\n sdk: MobileSDK | null\n isInitialized: boolean\n initialize: (sdk: MobileSDK) => void\n shutdown: () => void\n}\n\nconst MobileSDKContext = createContext<MobileSDKContextType | undefined>(undefined)\n\nexport const useMobileSDK = <T extends Record<string, MobileSDKModule> = Record<string, MobileSDKModule>>() => {\n const context = useContext(MobileSDKContext)\n if (context === undefined) {\n throw new Error('useMobileSDK must be used within an MobileSDKProvider')\n }\n return {\n sdk: context.sdk as MobileSDK<T>,\n isInitialized: context.isInitialized,\n initialize: context.initialize,\n shutdown: context.shutdown,\n }\n}\n\ninterface SDKProviderProps {\n children: ReactNode\n}\n\nexport const MobileSDKProvider: React.FC<SDKProviderProps> = ({ children }) => {\n const [sdk, setSDK] = useState<MobileSDK | null>(null)\n const [isInitialized, setIsInitialized] = useState(false)\n\n const initialize = (newSDK: MobileSDK) => {\n setSDK(newSDK)\n setIsInitialized(true)\n }\n\n const shutdown = () => {\n setSDK(null)\n setIsInitialized(false)\n }\n\n return (\n <MobileSDKContext.Provider value={{ sdk, isInitialized, initialize, shutdown }}>\n {children}\n </MobileSDKContext.Provider>\n )\n}\n","import type {\n Agent,\n BaseEvent,\n BaseRecord,\n RecordDeletedEvent,\n RecordSavedEvent,\n RecordUpdatedEvent,\n} from '@credo-ts/core'\nimport type { Constructor } from '@credo-ts/core/build/utils/mixins'\n\nimport { RepositoryEventTypes } from '@credo-ts/core'\nimport { useMemo } from 'react'\nimport { filter, map, pipe } from 'rxjs'\n\n// BaseRecordAny makes sure we allow any type to be used for the generic\n// properties of the BaseRecord. The default BaseRecord type uses Empty objects\n// which means if you have a ConnectionRecord and BaseRecord with default properties\n// their types are incompatible.\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\nexport type BaseRecordAny = BaseRecord<any, any, any>\ntype RecordClass<R extends BaseRecordAny> = Constructor<R> & { type: string }\nexport interface RecordsState<R extends BaseRecordAny> {\n loading: boolean\n records: R[]\n}\n\nexport const addRecord = <R extends BaseRecordAny>(record: R, state: RecordsState<R>): RecordsState<R> => {\n const newRecordsState = [...state.records]\n newRecordsState.unshift(record)\n return {\n loading: state.loading,\n records: newRecordsState,\n }\n}\n\nexport const updateRecord = <R extends BaseRecordAny>(record: R, state: RecordsState<R>): RecordsState<R> => {\n const newRecordsState = [...state.records]\n const index = newRecordsState.findIndex((r) => r.id === record.id)\n if (index > -1) {\n newRecordsState[index] = record\n }\n return {\n loading: state.loading,\n records: newRecordsState,\n }\n}\n\nexport const removeRecord = <R extends BaseRecordAny>(\n record: R | { id: string; type: R['type'] },\n state: RecordsState<R>\n): RecordsState<R> => {\n const newRecordsState = state.records.filter((r) => r.id !== record.id)\n return {\n loading: state.loading,\n records: newRecordsState,\n }\n}\n\nconst filterByType = <R extends BaseRecordAny>(recordClass: RecordClass<R>) => {\n return pipe(\n map((event: BaseEvent) => (event.payload as Record<string, R>).record),\n filter((record: R) => record.type === recordClass.type)\n )\n}\n\nexport const recordsAddedByType = <R extends BaseRecordAny>(agent: Agent | undefined, recordClass: RecordClass<R>) => {\n if (!agent) {\n throw new Error('Agent is required to check record type')\n }\n\n if (!recordClass) {\n throw new Error(\"The recordClass can't be undefined\")\n }\n\n return agent?.events.observable<RecordSavedEvent<R>>(RepositoryEventTypes.RecordSaved).pipe(filterByType(recordClass))\n}\n\nexport const recordsUpdatedByType = <R extends BaseRecordAny>(\n agent: Agent | undefined,\n recordClass: RecordClass<R>\n) => {\n if (!agent) {\n throw new Error('Agent is required to update record type')\n }\n\n if (!recordClass) {\n throw new Error(\"The recordClass can't be undefined\")\n }\n\n return agent?.events\n .observable<RecordUpdatedEvent<R>>(RepositoryEventTypes.RecordUpdated)\n .pipe(filterByType(recordClass))\n}\n\nexport const recordsRemovedByType = <R extends BaseRecordAny>(\n agent: Agent | undefined,\n recordClass: RecordClass<R>\n) => {\n if (!agent) {\n throw new Error('Agent is required to remove records by type')\n }\n\n if (!recordClass) {\n throw new Error(\"The recordClass can't be undefined\")\n }\n\n return agent?.events\n .observable<RecordDeletedEvent<R>>(RepositoryEventTypes.RecordDeleted)\n .pipe(filterByType(recordClass))\n}\n\nexport const isModuleRegistered = (agent: Agent, ModuleClass: Constructor) => {\n if (!agent) {\n throw new Error('Agent is required to check if a module is enabled')\n }\n\n const foundModule = Object.values(agent.dependencyManager.registeredModules).find(\n (module: unknown) => module instanceof ModuleClass\n )\n\n return foundModule !== undefined\n}\n\nexport const useIsModuleRegistered = (agent: Agent, ModuleClass: Constructor) => {\n return useMemo(() => isModuleRegistered(agent, ModuleClass), [agent, ModuleClass])\n}\n","import { type Agent, W3cCredentialRecord } from '@credo-ts/core'\nimport type { PropsWithChildren } from 'react'\nimport { createContext, useContext, useEffect, useState } from 'react'\nimport { recordsAddedByType, recordsRemovedByType, recordsUpdatedByType } from './recordUtils'\n\nexport { W3cCredentialRecord, W3cVerifiableCredential } from '@credo-ts/core'\n\ntype W3cCredentialRecordState = {\n w3cCredentialRecords: Array<W3cCredentialRecord>\n isLoading: boolean\n}\n\nconst addRecord = (record: W3cCredentialRecord, state: W3cCredentialRecordState): W3cCredentialRecordState => {\n const newRecordsState = [...state.w3cCredentialRecords]\n newRecordsState.unshift(record)\n return {\n isLoading: state.isLoading,\n w3cCredentialRecords: newRecordsState,\n }\n}\n\nconst updateRecord = (record: W3cCredentialRecord, state: W3cCredentialRecordState): W3cCredentialRecordState => {\n const newRecordsState = [...state.w3cCredentialRecords]\n const index = newRecordsState.findIndex((r) => r.id === record.id)\n if (index > -1) {\n newRecordsState[index] = record\n }\n return {\n isLoading: state.isLoading,\n w3cCredentialRecords: newRecordsState,\n }\n}\n\nconst removeRecord = (record: W3cCredentialRecord, state: W3cCredentialRecordState): W3cCredentialRecordState => {\n const newRecordsState = state.w3cCredentialRecords.filter((r) => r.id !== record.id)\n return {\n isLoading: state.isLoading,\n w3cCredentialRecords: newRecordsState,\n }\n}\n\nconst W3cCredentialRecordContext = createContext<W3cCredentialRecordState | undefined>(undefined)\n\nexport const useW3cCredentialRecords = (): W3cCredentialRecordState => {\n const w3cCredentialRecordContext = useContext(W3cCredentialRecordContext)\n if (!w3cCredentialRecordContext) {\n throw new Error('useW3cCredentialRecord must be used within a W3cCredentialRecordContextProvider')\n }\n\n return w3cCredentialRecordContext\n}\n\nexport const useW3cCredentialRecordById = (id: string): W3cCredentialRecord | undefined => {\n const { w3cCredentialRecords } = useW3cCredentialRecords()\n return w3cCredentialRecords.find((c) => c.id === id)\n}\n\ninterface Props {\n agent: Agent\n}\n\nexport const W3cCredentialRecordProvider: React.FC<PropsWithChildren<Props>> = ({ agent, children }) => {\n const [state, setState] = useState<W3cCredentialRecordState>({\n w3cCredentialRecords: [],\n isLoading: true,\n })\n\n useEffect(() => {\n void agent.w3cCredentials\n .getAllCredentialRecords()\n .then((w3cCredentialRecords) => setState({ w3cCredentialRecords, isLoading: false }))\n }, [agent])\n\n useEffect(() => {\n if (!state.isLoading && agent) {\n const credentialAdded$ = recordsAddedByType(agent, W3cCredentialRecord).subscribe((record) =>\n setState(addRecord(record, state))\n )\n\n const credentialUpdate$ = recordsUpdatedByType(agent, W3cCredentialRecord).subscribe((record) =>\n setState(updateRecord(record, state))\n )\n\n const credentialRemove$ = recordsRemovedByType(agent, W3cCredentialRecord).subscribe((record) =>\n setState(removeRecord(record, state))\n )\n\n return () => {\n credentialAdded$.unsubscribe()\n credentialUpdate$.unsubscribe()\n credentialRemove$.unsubscribe()\n }\n }\n }, [state, agent])\n\n return <W3cCredentialRecordContext.Provider value={state}>{children}</W3cCredentialRecordContext.Provider>\n}\n","import type { Agent } from '@credo-ts/core'\nimport type { PropsWithChildren } from 'react'\n\nimport { createContext, useContext } from 'react'\nimport { W3cCredentialRecordProvider } from './W3cCredentialsProvider'\n\nconst AgentContext = createContext<Agent | undefined>(undefined)\n\nexport const useAgent = <AppAgent extends Agent = Agent>() => {\n const agentContext = useContext(AgentContext)\n if (!agentContext) {\n throw new Error('useAgent must be used within a AgentContextProvider')\n }\n return {\n agent: agentContext,\n }\n}\n\ninterface Props {\n agent: Agent\n}\n\nconst AgentProvider: React.FC<PropsWithChildren<Props>> = ({ agent, children }) => {\n return (\n <AgentContext.Provider value={agent}>\n <W3cCredentialRecordProvider agent={agent}>{children}</W3cCredentialRecordProvider>\n </AgentContext.Provider>\n )\n}\n\nexport default AgentProvider\n","import { AskarModule } from '@credo-ts/askar'\nimport {\n Agent,\n CacheModule,\n type DidCreateOptions,\n DidRepository,\n DidsModule,\n type InitConfig,\n JwkDidRegistrar,\n JwkDidResolver,\n KeyDidRegistrar,\n KeyDidResolver,\n SingleContextStorageLruCache,\n type TagValue,\n} from '@credo-ts/core'\nimport type { AgentModulesInput } from '@credo-ts/core/build/agent/AgentModules'\nimport { agentDependencies } from '@credo-ts/react-native'\nimport { askar } from '@openwallet-foundation/askar-react-native'\nimport type { PropsWithChildren } from 'react'\nimport { useMobileSDK } from './contexts'\nimport AgentProvider from './providers/AgentProvider'\n\nconst getCoreModules = () => {\n return {\n askar: new AskarModule({\n askar,\n }),\n dids: new DidsModule({\n registrars: [new JwkDidRegistrar(), new KeyDidRegistrar()],\n resolvers: [new JwkDidResolver(), new KeyDidResolver()],\n }),\n cache: new CacheModule({\n cache: new SingleContextStorageLruCache({\n limit: 50,\n }),\n }),\n }\n}\n\nexport interface MobileSDKModule {\n initialize(agent: Agent): void\n getAgentModules(): AgentModulesInput\n}\n\nexport type MobileSDKOptions<T extends Record<string, MobileSDKModule> = Record<string, MobileSDKModule>> = {\n agentConfig: InitConfig\n modules: T\n defaultModules?: AgentModulesInput\n}\n\nexport class MobileSDK<T extends Record<string, MobileSDKModule> = Record<string, MobileSDKModule>> {\n private localAgent: Agent | null = null\n public readonly configuration: MobileSDKOptions<T>\n public readonly modules: T\n\n public constructor(options: MobileSDKOptions<T>) {\n this.configuration = options\n this.modules = options.modules\n }\n\n public async initialize() {\n const defaultModules = this.configuration.defaultModules ?? {}\n const coreModules = getCoreModules()\n Object.assign(defaultModules, coreModules)\n\n const modules = Object.entries(this.configuration.modules).reduce((acc, [, module]) => {\n const moduleModules = module.getAgentModules()\n Object.assign(acc, moduleModules)\n return acc\n }, defaultModules)\n\n const agent = new Agent({\n config: this.configuration.agentConfig,\n dependencies: agentDependencies,\n modules,\n })\n await agent.initialize()\n\n // Initialize modules after agent is initialized to ensure all dependencies are available\n for await (const [, module] of Object.entries(this.configuration.modules)) {\n module.initialize(agent)\n }\n\n this.localAgent = agent\n return agent\n }\n\n public get agent() {\n return this.localAgent\n }\n\n public assertAndGetAgent() {\n if (!this.agent) {\n throw new Error('Agent not initialized')\n }\n\n return this.agent\n }\n\n public static AppProvider({ children }: PropsWithChildren) {\n const { sdk } = useMobileSDK()\n\n if (!sdk?.agent) {\n throw new Error('Mobile SDK not initialized')\n }\n\n return <AgentProvider agent={sdk.agent}>{children}</AgentProvider>\n }\n\n public async createDid<T extends DidCreateOptions>(options: T) {\n const agent = this.assertAndGetAgent()\n\n const did = await agent.dids.create<T>(options)\n\n return did\n }\n\n public async getDids({\n method,\n did,\n tag,\n tagValue,\n }: { method?: string; did?: string; tag?: string; tagValue?: TagValue }) {\n const agent = this.assertAndGetAgent()\n\n if (tag) {\n const didRepository = await agent.dependencyManager.resolve(DidRepository)\n\n const didRecord = await didRepository.findSingleByQuery(agent.context, tagValue ? { [tag]: tagValue } : { tag })\n return didRecord ? [didRecord] : []\n }\n\n const dids = await agent.dids.getCreatedDids({\n method,\n did,\n })\n return dids\n }\n\n public async addTagToDid({ did, tag, tagValue }: { did: string; tag: string; tagValue: TagValue }) {\n const agent = this.assertAndGetAgent()\n const didRecords = await this.getDids({ did })\n\n if (didRecords.length === 0) {\n throw new Error('Did not found')\n }\n\n const didRecord = didRecords[0]\n await didRecord.setTag(tag, tagValue)\n\n const didRepository = await agent.dependencyManager.resolve(DidRepository)\n\n await didRepository.update(agent.context, didRecord)\n\n return didRecord\n }\n\n public async resolveDid({ did }: { did: string }) {\n const agent = this.assertAndGetAgent()\n const didResolutionResult = await agent.dids.resolve(did)\n return didResolutionResult\n }\n}\n","import { useCallback } from 'react'\nimport { MobileSDK, type MobileSDKOptions } from '../MobileSDK'\nimport { useMobileSDK } from '../contexts'\n\nexport const useMobileSDKInitializer = () => {\n const { initialize, isInitialized, sdk } = useMobileSDK()\n\n const initializeSDK = useCallback(\n async (options: MobileSDKOptions) => {\n if (isInitialized) {\n throw new Error('Mobile SDK is already initialized')\n }\n\n const sdk = new MobileSDK(options)\n\n await sdk.initialize()\n initialize(sdk)\n\n return sdk\n },\n [isInitialized, initialize]\n )\n\n return { initializeSDK, isInitialized, sdk }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,MAAM,4CAAmE,OAAU;AAEnF,MAAa,qBAAkG;CAC7G,MAAM,gCAAqB,iBAAiB;AAC5C,KAAI,YAAY,OACd,OAAM,IAAI,MAAM,wDAAwD;AAE1E,QAAO;EACL,KAAK,QAAQ;EACb,eAAe,QAAQ;EACvB,YAAY,QAAQ;EACpB,UAAU,QAAQ;EACnB;;AAOH,MAAaA,qBAAiD,EAAE,eAAe;CAC7E,MAAM,CAAC,KAAK,8BAAqC,KAAK;CACtD,MAAM,CAAC,eAAe,wCAA6B,MAAM;CAEzD,MAAM,cAAc,WAAsB;AACxC,SAAO,OAAO;AACd,mBAAiB,KAAK;;CA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MAAM,UAAU,GAAG,EAAE,KAAK,CAAC;AAChH,UAAO,YAAY,CAAC,UAAU,GAAG,EAAE;;AAOrC,SAJa,MAAM,MAAM,KAAK,eAAe;GAC3C;GACA;GACD,CAAC;;CAIJ,MAAa,YAAY,EAAE,KAAK,KAAK,YAA8D;EACjG,MAAM,QAAQ,KAAK,mBAAmB;EACtC,MAAM,aAAa,MAAM,KAAK,QAAQ,EAAE,KAAK,CAAC;AAE9C,MAAI,WAAW,WAAW,EACxB,OAAM,IAAI,MAAM,gBAAgB;EAGlC,MAAM,YAAY,WAAW;AAC7B,QAAM,UAAU,OAAO,KAAK,SAAS;AAIrC,SAFsB,MAAM,MAAM,kBAAkB,QAAQA,8BAAc,EAEtD,OAAO,MAAM,SAAS,UAAU;AAEpD,SAAO;;CAGT,MAAa,WAAW,EAAE,OAAwB;AAGhD,SAD4B,MADd,KAAK,mBAAmB,CACE,KAAK,QAAQ,IAAI;;;;;;AC3J7D,MAAa,gCAAgC;CAC3C,MAAM,EAAE,YAAY,eAAe,QAAQ,cAAc;AAkBzD,QAAO;EAAE,sCAfP,OAAO,YAA8B;AACnC,OAAI,cACF,OAAM,IAAI,MAAM,oCAAoC;GAGtD,MAAMC,QAAM,IAAI,UAAU,QAAQ;AAElC,SAAMA,MAAI,YAAY;AACtB,cAAWA,MAAI;AAEf,UAAOA;KAET,CAAC,eAAe,WAAW,CAC5B;EAEuB;EAAe;EAAK"}
|
package/build/index.mjs
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { Agent, CacheModule, ConsoleLogger, DidRepository, DidsModule, JwkDidRegistrar, JwkDidResolver, KeyDidCreateOptions, KeyDidRegistrar, KeyDidResolver, KeyType, LogLevel, RepositoryEventTypes, SingleContextStorageLruCache, W3cCredentialRecord } from "@credo-ts/core";
|
|
2
|
+
import { AskarModule } from "@credo-ts/askar";
|
|
3
|
+
import { agentDependencies } from "@credo-ts/react-native";
|
|
4
|
+
import { askar } from "@openwallet-foundation/askar-react-native";
|
|
5
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
import { filter, map, pipe } from "rxjs";
|
|
8
|
+
|
|
9
|
+
//#region src/contexts/MobileSDKContext.tsx
|
|
10
|
+
const MobileSDKContext = createContext(void 0);
|
|
11
|
+
const useMobileSDK = () => {
|
|
12
|
+
const context = useContext(MobileSDKContext);
|
|
13
|
+
if (context === void 0) throw new Error("useMobileSDK must be used within an MobileSDKProvider");
|
|
14
|
+
return {
|
|
15
|
+
sdk: context.sdk,
|
|
16
|
+
isInitialized: context.isInitialized,
|
|
17
|
+
initialize: context.initialize,
|
|
18
|
+
shutdown: context.shutdown
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
const MobileSDKProvider = ({ children }) => {
|
|
22
|
+
const [sdk, setSDK] = useState(null);
|
|
23
|
+
const [isInitialized, setIsInitialized] = useState(false);
|
|
24
|
+
const initialize = (newSDK) => {
|
|
25
|
+
setSDK(newSDK);
|
|
26
|
+
setIsInitialized(true);
|
|
27
|
+
};
|
|
28
|
+
const shutdown = () => {
|
|
29
|
+
setSDK(null);
|
|
30
|
+
setIsInitialized(false);
|
|
31
|
+
};
|
|
32
|
+
return /* @__PURE__ */ jsx(MobileSDKContext.Provider, {
|
|
33
|
+
value: {
|
|
34
|
+
sdk,
|
|
35
|
+
isInitialized,
|
|
36
|
+
initialize,
|
|
37
|
+
shutdown
|
|
38
|
+
},
|
|
39
|
+
children
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/providers/recordUtils.ts
|
|
45
|
+
const addRecord = (record, state) => {
|
|
46
|
+
const newRecordsState = [...state.records];
|
|
47
|
+
newRecordsState.unshift(record);
|
|
48
|
+
return {
|
|
49
|
+
loading: state.loading,
|
|
50
|
+
records: newRecordsState
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
const updateRecord = (record, state) => {
|
|
54
|
+
const newRecordsState = [...state.records];
|
|
55
|
+
const index = newRecordsState.findIndex((r) => r.id === record.id);
|
|
56
|
+
if (index > -1) newRecordsState[index] = record;
|
|
57
|
+
return {
|
|
58
|
+
loading: state.loading,
|
|
59
|
+
records: newRecordsState
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
const removeRecord = (record, state) => {
|
|
63
|
+
const newRecordsState = state.records.filter((r) => r.id !== record.id);
|
|
64
|
+
return {
|
|
65
|
+
loading: state.loading,
|
|
66
|
+
records: newRecordsState
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
const filterByType = (recordClass) => {
|
|
70
|
+
return pipe(map((event) => event.payload.record), filter((record) => record.type === recordClass.type));
|
|
71
|
+
};
|
|
72
|
+
const recordsAddedByType = (agent, recordClass) => {
|
|
73
|
+
if (!agent) throw new Error("Agent is required to check record type");
|
|
74
|
+
if (!recordClass) throw new Error("The recordClass can't be undefined");
|
|
75
|
+
return agent?.events.observable(RepositoryEventTypes.RecordSaved).pipe(filterByType(recordClass));
|
|
76
|
+
};
|
|
77
|
+
const recordsUpdatedByType = (agent, recordClass) => {
|
|
78
|
+
if (!agent) throw new Error("Agent is required to update record type");
|
|
79
|
+
if (!recordClass) throw new Error("The recordClass can't be undefined");
|
|
80
|
+
return agent?.events.observable(RepositoryEventTypes.RecordUpdated).pipe(filterByType(recordClass));
|
|
81
|
+
};
|
|
82
|
+
const recordsRemovedByType = (agent, recordClass) => {
|
|
83
|
+
if (!agent) throw new Error("Agent is required to remove records by type");
|
|
84
|
+
if (!recordClass) throw new Error("The recordClass can't be undefined");
|
|
85
|
+
return agent?.events.observable(RepositoryEventTypes.RecordDeleted).pipe(filterByType(recordClass));
|
|
86
|
+
};
|
|
87
|
+
const isModuleRegistered = (agent, ModuleClass) => {
|
|
88
|
+
if (!agent) throw new Error("Agent is required to check if a module is enabled");
|
|
89
|
+
return Object.values(agent.dependencyManager.registeredModules).find((module) => module instanceof ModuleClass) !== void 0;
|
|
90
|
+
};
|
|
91
|
+
const useIsModuleRegistered = (agent, ModuleClass) => {
|
|
92
|
+
return useMemo(() => isModuleRegistered(agent, ModuleClass), [agent, ModuleClass]);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/providers/W3cCredentialsProvider.tsx
|
|
97
|
+
const addRecord$1 = (record, state) => {
|
|
98
|
+
const newRecordsState = [...state.w3cCredentialRecords];
|
|
99
|
+
newRecordsState.unshift(record);
|
|
100
|
+
return {
|
|
101
|
+
isLoading: state.isLoading,
|
|
102
|
+
w3cCredentialRecords: newRecordsState
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
const updateRecord$1 = (record, state) => {
|
|
106
|
+
const newRecordsState = [...state.w3cCredentialRecords];
|
|
107
|
+
const index = newRecordsState.findIndex((r) => r.id === record.id);
|
|
108
|
+
if (index > -1) newRecordsState[index] = record;
|
|
109
|
+
return {
|
|
110
|
+
isLoading: state.isLoading,
|
|
111
|
+
w3cCredentialRecords: newRecordsState
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
const removeRecord$1 = (record, state) => {
|
|
115
|
+
const newRecordsState = state.w3cCredentialRecords.filter((r) => r.id !== record.id);
|
|
116
|
+
return {
|
|
117
|
+
isLoading: state.isLoading,
|
|
118
|
+
w3cCredentialRecords: newRecordsState
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
const W3cCredentialRecordContext = createContext(void 0);
|
|
122
|
+
const W3cCredentialRecordProvider = ({ agent, children }) => {
|
|
123
|
+
const [state, setState] = useState({
|
|
124
|
+
w3cCredentialRecords: [],
|
|
125
|
+
isLoading: true
|
|
126
|
+
});
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
agent.w3cCredentials.getAllCredentialRecords().then((w3cCredentialRecords) => setState({
|
|
129
|
+
w3cCredentialRecords,
|
|
130
|
+
isLoading: false
|
|
131
|
+
}));
|
|
132
|
+
}, [agent]);
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
if (!state.isLoading && agent) {
|
|
135
|
+
const credentialAdded$ = recordsAddedByType(agent, W3cCredentialRecord).subscribe((record) => setState(addRecord$1(record, state)));
|
|
136
|
+
const credentialUpdate$ = recordsUpdatedByType(agent, W3cCredentialRecord).subscribe((record) => setState(updateRecord$1(record, state)));
|
|
137
|
+
const credentialRemove$ = recordsRemovedByType(agent, W3cCredentialRecord).subscribe((record) => setState(removeRecord$1(record, state)));
|
|
138
|
+
return () => {
|
|
139
|
+
credentialAdded$.unsubscribe();
|
|
140
|
+
credentialUpdate$.unsubscribe();
|
|
141
|
+
credentialRemove$.unsubscribe();
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}, [state, agent]);
|
|
145
|
+
return /* @__PURE__ */ jsx(W3cCredentialRecordContext.Provider, {
|
|
146
|
+
value: state,
|
|
147
|
+
children
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/providers/AgentProvider.tsx
|
|
153
|
+
const AgentContext = createContext(void 0);
|
|
154
|
+
const useAgent = () => {
|
|
155
|
+
const agentContext = useContext(AgentContext);
|
|
156
|
+
if (!agentContext) throw new Error("useAgent must be used within a AgentContextProvider");
|
|
157
|
+
return { agent: agentContext };
|
|
158
|
+
};
|
|
159
|
+
const AgentProvider = ({ agent, children }) => {
|
|
160
|
+
return /* @__PURE__ */ jsx(AgentContext.Provider, {
|
|
161
|
+
value: agent,
|
|
162
|
+
children: /* @__PURE__ */ jsx(W3cCredentialRecordProvider, {
|
|
163
|
+
agent,
|
|
164
|
+
children
|
|
165
|
+
})
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
var AgentProvider_default = AgentProvider;
|
|
169
|
+
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/MobileSDK.tsx
|
|
172
|
+
const getCoreModules = () => {
|
|
173
|
+
return {
|
|
174
|
+
askar: new AskarModule({ askar }),
|
|
175
|
+
dids: new DidsModule({
|
|
176
|
+
registrars: [new JwkDidRegistrar(), new KeyDidRegistrar()],
|
|
177
|
+
resolvers: [new JwkDidResolver(), new KeyDidResolver()]
|
|
178
|
+
}),
|
|
179
|
+
cache: new CacheModule({ cache: new SingleContextStorageLruCache({ limit: 50 }) })
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
var MobileSDK = class {
|
|
183
|
+
constructor(options) {
|
|
184
|
+
this.localAgent = null;
|
|
185
|
+
this.configuration = options;
|
|
186
|
+
this.modules = options.modules;
|
|
187
|
+
}
|
|
188
|
+
async initialize() {
|
|
189
|
+
const defaultModules = this.configuration.defaultModules ?? {};
|
|
190
|
+
const coreModules = getCoreModules();
|
|
191
|
+
Object.assign(defaultModules, coreModules);
|
|
192
|
+
const modules = Object.entries(this.configuration.modules).reduce((acc, [, module]) => {
|
|
193
|
+
const moduleModules = module.getAgentModules();
|
|
194
|
+
Object.assign(acc, moduleModules);
|
|
195
|
+
return acc;
|
|
196
|
+
}, defaultModules);
|
|
197
|
+
const agent = new Agent({
|
|
198
|
+
config: this.configuration.agentConfig,
|
|
199
|
+
dependencies: agentDependencies,
|
|
200
|
+
modules
|
|
201
|
+
});
|
|
202
|
+
await agent.initialize();
|
|
203
|
+
for await (const [, module] of Object.entries(this.configuration.modules)) module.initialize(agent);
|
|
204
|
+
this.localAgent = agent;
|
|
205
|
+
return agent;
|
|
206
|
+
}
|
|
207
|
+
get agent() {
|
|
208
|
+
return this.localAgent;
|
|
209
|
+
}
|
|
210
|
+
assertAndGetAgent() {
|
|
211
|
+
if (!this.agent) throw new Error("Agent not initialized");
|
|
212
|
+
return this.agent;
|
|
213
|
+
}
|
|
214
|
+
static AppProvider({ children }) {
|
|
215
|
+
const { sdk } = useMobileSDK();
|
|
216
|
+
if (!sdk?.agent) throw new Error("Mobile SDK not initialized");
|
|
217
|
+
return /* @__PURE__ */ jsx(AgentProvider_default, {
|
|
218
|
+
agent: sdk.agent,
|
|
219
|
+
children
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
async createDid(options) {
|
|
223
|
+
return await this.assertAndGetAgent().dids.create(options);
|
|
224
|
+
}
|
|
225
|
+
async getDids({ method, did, tag, tagValue }) {
|
|
226
|
+
const agent = this.assertAndGetAgent();
|
|
227
|
+
if (tag) {
|
|
228
|
+
const didRecord = await (await agent.dependencyManager.resolve(DidRepository)).findSingleByQuery(agent.context, tagValue ? { [tag]: tagValue } : { tag });
|
|
229
|
+
return didRecord ? [didRecord] : [];
|
|
230
|
+
}
|
|
231
|
+
return await agent.dids.getCreatedDids({
|
|
232
|
+
method,
|
|
233
|
+
did
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
async addTagToDid({ did, tag, tagValue }) {
|
|
237
|
+
const agent = this.assertAndGetAgent();
|
|
238
|
+
const didRecords = await this.getDids({ did });
|
|
239
|
+
if (didRecords.length === 0) throw new Error("Did not found");
|
|
240
|
+
const didRecord = didRecords[0];
|
|
241
|
+
await didRecord.setTag(tag, tagValue);
|
|
242
|
+
await (await agent.dependencyManager.resolve(DidRepository)).update(agent.context, didRecord);
|
|
243
|
+
return didRecord;
|
|
244
|
+
}
|
|
245
|
+
async resolveDid({ did }) {
|
|
246
|
+
return await this.assertAndGetAgent().dids.resolve(did);
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
//#endregion
|
|
251
|
+
//#region src/hooks/useSDKInitializer.ts
|
|
252
|
+
const useMobileSDKInitializer = () => {
|
|
253
|
+
const { initialize, isInitialized, sdk } = useMobileSDK();
|
|
254
|
+
return {
|
|
255
|
+
initializeSDK: useCallback(async (options) => {
|
|
256
|
+
if (isInitialized) throw new Error("Mobile SDK is already initialized");
|
|
257
|
+
const sdk$1 = new MobileSDK(options);
|
|
258
|
+
await sdk$1.initialize();
|
|
259
|
+
initialize(sdk$1);
|
|
260
|
+
return sdk$1;
|
|
261
|
+
}, [isInitialized, initialize]),
|
|
262
|
+
isInitialized,
|
|
263
|
+
sdk
|
|
264
|
+
};
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
//#endregion
|
|
268
|
+
export { ConsoleLogger, KeyDidCreateOptions, KeyType, LogLevel, MobileSDK, MobileSDKProvider, addRecord, isModuleRegistered, recordsAddedByType, recordsRemovedByType, recordsUpdatedByType, removeRecord, updateRecord, useAgent, useIsModuleRegistered, useMobileSDK, useMobileSDKInitializer };
|
|
269
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["MobileSDKProvider: React.FC<SDKProviderProps>","addRecord","updateRecord","removeRecord","W3cCredentialRecordProvider: React.FC<PropsWithChildren<Props>>","AgentProvider: React.FC<PropsWithChildren<Props>>","AgentProvider","sdk"],"sources":["../src/contexts/MobileSDKContext.tsx","../src/providers/recordUtils.ts","../src/providers/W3cCredentialsProvider.tsx","../src/providers/AgentProvider.tsx","../src/MobileSDK.tsx","../src/hooks/useSDKInitializer.ts"],"sourcesContent":["import type React from 'react'\nimport { type ReactNode, createContext, useContext, useState } from 'react'\nimport type { MobileSDK, MobileSDKModule } from '../MobileSDK'\n\ninterface MobileSDKContextType {\n sdk: MobileSDK | null\n isInitialized: boolean\n initialize: (sdk: MobileSDK) => void\n shutdown: () => void\n}\n\nconst MobileSDKContext = createContext<MobileSDKContextType | undefined>(undefined)\n\nexport const useMobileSDK = <T extends Record<string, MobileSDKModule> = Record<string, MobileSDKModule>>() => {\n const context = useContext(MobileSDKContext)\n if (context === undefined) {\n throw new Error('useMobileSDK must be used within an MobileSDKProvider')\n }\n return {\n sdk: context.sdk as MobileSDK<T>,\n isInitialized: context.isInitialized,\n initialize: context.initialize,\n shutdown: context.shutdown,\n }\n}\n\ninterface SDKProviderProps {\n children: ReactNode\n}\n\nexport const MobileSDKProvider: React.FC<SDKProviderProps> = ({ children }) => {\n const [sdk, setSDK] = useState<MobileSDK | null>(null)\n const [isInitialized, setIsInitialized] = useState(false)\n\n const initialize = (newSDK: MobileSDK) => {\n setSDK(newSDK)\n setIsInitialized(true)\n }\n\n const shutdown = () => {\n setSDK(null)\n setIsInitialized(false)\n }\n\n return (\n <MobileSDKContext.Provider value={{ sdk, isInitialized, initialize, shutdown }}>\n {children}\n </MobileSDKContext.Provider>\n )\n}\n","import type {\n Agent,\n BaseEvent,\n BaseRecord,\n RecordDeletedEvent,\n RecordSavedEvent,\n RecordUpdatedEvent,\n} from '@credo-ts/core'\nimport type { Constructor } from '@credo-ts/core/build/utils/mixins'\n\nimport { RepositoryEventTypes } from '@credo-ts/core'\nimport { useMemo } from 'react'\nimport { filter, map, pipe } from 'rxjs'\n\n// BaseRecordAny makes sure we allow any type to be used for the generic\n// properties of the BaseRecord. The default BaseRecord type uses Empty objects\n// which means if you have a ConnectionRecord and BaseRecord with default properties\n// their types are incompatible.\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\nexport type BaseRecordAny = BaseRecord<any, any, any>\ntype RecordClass<R extends BaseRecordAny> = Constructor<R> & { type: string }\nexport interface RecordsState<R extends BaseRecordAny> {\n loading: boolean\n records: R[]\n}\n\nexport const addRecord = <R extends BaseRecordAny>(record: R, state: RecordsState<R>): RecordsState<R> => {\n const newRecordsState = [...state.records]\n newRecordsState.unshift(record)\n return {\n loading: state.loading,\n records: newRecordsState,\n }\n}\n\nexport const updateRecord = <R extends BaseRecordAny>(record: R, state: RecordsState<R>): RecordsState<R> => {\n const newRecordsState = [...state.records]\n const index = newRecordsState.findIndex((r) => r.id === record.id)\n if (index > -1) {\n newRecordsState[index] = record\n }\n return {\n loading: state.loading,\n records: newRecordsState,\n }\n}\n\nexport const removeRecord = <R extends BaseRecordAny>(\n record: R | { id: string; type: R['type'] },\n state: RecordsState<R>\n): RecordsState<R> => {\n const newRecordsState = state.records.filter((r) => r.id !== record.id)\n return {\n loading: state.loading,\n records: newRecordsState,\n }\n}\n\nconst filterByType = <R extends BaseRecordAny>(recordClass: RecordClass<R>) => {\n return pipe(\n map((event: BaseEvent) => (event.payload as Record<string, R>).record),\n filter((record: R) => record.type === recordClass.type)\n )\n}\n\nexport const recordsAddedByType = <R extends BaseRecordAny>(agent: Agent | undefined, recordClass: RecordClass<R>) => {\n if (!agent) {\n throw new Error('Agent is required to check record type')\n }\n\n if (!recordClass) {\n throw new Error(\"The recordClass can't be undefined\")\n }\n\n return agent?.events.observable<RecordSavedEvent<R>>(RepositoryEventTypes.RecordSaved).pipe(filterByType(recordClass))\n}\n\nexport const recordsUpdatedByType = <R extends BaseRecordAny>(\n agent: Agent | undefined,\n recordClass: RecordClass<R>\n) => {\n if (!agent) {\n throw new Error('Agent is required to update record type')\n }\n\n if (!recordClass) {\n throw new Error(\"The recordClass can't be undefined\")\n }\n\n return agent?.events\n .observable<RecordUpdatedEvent<R>>(RepositoryEventTypes.RecordUpdated)\n .pipe(filterByType(recordClass))\n}\n\nexport const recordsRemovedByType = <R extends BaseRecordAny>(\n agent: Agent | undefined,\n recordClass: RecordClass<R>\n) => {\n if (!agent) {\n throw new Error('Agent is required to remove records by type')\n }\n\n if (!recordClass) {\n throw new Error(\"The recordClass can't be undefined\")\n }\n\n return agent?.events\n .observable<RecordDeletedEvent<R>>(RepositoryEventTypes.RecordDeleted)\n .pipe(filterByType(recordClass))\n}\n\nexport const isModuleRegistered = (agent: Agent, ModuleClass: Constructor) => {\n if (!agent) {\n throw new Error('Agent is required to check if a module is enabled')\n }\n\n const foundModule = Object.values(agent.dependencyManager.registeredModules).find(\n (module: unknown) => module instanceof ModuleClass\n )\n\n return foundModule !== undefined\n}\n\nexport const useIsModuleRegistered = (agent: Agent, ModuleClass: Constructor) => {\n return useMemo(() => isModuleRegistered(agent, ModuleClass), [agent, ModuleClass])\n}\n","import { type Agent, W3cCredentialRecord } from '@credo-ts/core'\nimport type { PropsWithChildren } from 'react'\nimport { createContext, useContext, useEffect, useState } from 'react'\nimport { recordsAddedByType, recordsRemovedByType, recordsUpdatedByType } from './recordUtils'\n\nexport { W3cCredentialRecord, W3cVerifiableCredential } from '@credo-ts/core'\n\ntype W3cCredentialRecordState = {\n w3cCredentialRecords: Array<W3cCredentialRecord>\n isLoading: boolean\n}\n\nconst addRecord = (record: W3cCredentialRecord, state: W3cCredentialRecordState): W3cCredentialRecordState => {\n const newRecordsState = [...state.w3cCredentialRecords]\n newRecordsState.unshift(record)\n return {\n isLoading: state.isLoading,\n w3cCredentialRecords: newRecordsState,\n }\n}\n\nconst updateRecord = (record: W3cCredentialRecord, state: W3cCredentialRecordState): W3cCredentialRecordState => {\n const newRecordsState = [...state.w3cCredentialRecords]\n const index = newRecordsState.findIndex((r) => r.id === record.id)\n if (index > -1) {\n newRecordsState[index] = record\n }\n return {\n isLoading: state.isLoading,\n w3cCredentialRecords: newRecordsState,\n }\n}\n\nconst removeRecord = (record: W3cCredentialRecord, state: W3cCredentialRecordState): W3cCredentialRecordState => {\n const newRecordsState = state.w3cCredentialRecords.filter((r) => r.id !== record.id)\n return {\n isLoading: state.isLoading,\n w3cCredentialRecords: newRecordsState,\n }\n}\n\nconst W3cCredentialRecordContext = createContext<W3cCredentialRecordState | undefined>(undefined)\n\nexport const useW3cCredentialRecords = (): W3cCredentialRecordState => {\n const w3cCredentialRecordContext = useContext(W3cCredentialRecordContext)\n if (!w3cCredentialRecordContext) {\n throw new Error('useW3cCredentialRecord must be used within a W3cCredentialRecordContextProvider')\n }\n\n return w3cCredentialRecordContext\n}\n\nexport const useW3cCredentialRecordById = (id: string): W3cCredentialRecord | undefined => {\n const { w3cCredentialRecords } = useW3cCredentialRecords()\n return w3cCredentialRecords.find((c) => c.id === id)\n}\n\ninterface Props {\n agent: Agent\n}\n\nexport const W3cCredentialRecordProvider: React.FC<PropsWithChildren<Props>> = ({ agent, children }) => {\n const [state, setState] = useState<W3cCredentialRecordState>({\n w3cCredentialRecords: [],\n isLoading: true,\n })\n\n useEffect(() => {\n void agent.w3cCredentials\n .getAllCredentialRecords()\n .then((w3cCredentialRecords) => setState({ w3cCredentialRecords, isLoading: false }))\n }, [agent])\n\n useEffect(() => {\n if (!state.isLoading && agent) {\n const credentialAdded$ = recordsAddedByType(agent, W3cCredentialRecord).subscribe((record) =>\n setState(addRecord(record, state))\n )\n\n const credentialUpdate$ = recordsUpdatedByType(agent, W3cCredentialRecord).subscribe((record) =>\n setState(updateRecord(record, state))\n )\n\n const credentialRemove$ = recordsRemovedByType(agent, W3cCredentialRecord).subscribe((record) =>\n setState(removeRecord(record, state))\n )\n\n return () => {\n credentialAdded$.unsubscribe()\n credentialUpdate$.unsubscribe()\n credentialRemove$.unsubscribe()\n }\n }\n }, [state, agent])\n\n return <W3cCredentialRecordContext.Provider value={state}>{children}</W3cCredentialRecordContext.Provider>\n}\n","import type { Agent } from '@credo-ts/core'\nimport type { PropsWithChildren } from 'react'\n\nimport { createContext, useContext } from 'react'\nimport { W3cCredentialRecordProvider } from './W3cCredentialsProvider'\n\nconst AgentContext = createContext<Agent | undefined>(undefined)\n\nexport const useAgent = <AppAgent extends Agent = Agent>() => {\n const agentContext = useContext(AgentContext)\n if (!agentContext) {\n throw new Error('useAgent must be used within a AgentContextProvider')\n }\n return {\n agent: agentContext,\n }\n}\n\ninterface Props {\n agent: Agent\n}\n\nconst AgentProvider: React.FC<PropsWithChildren<Props>> = ({ agent, children }) => {\n return (\n <AgentContext.Provider value={agent}>\n <W3cCredentialRecordProvider agent={agent}>{children}</W3cCredentialRecordProvider>\n </AgentContext.Provider>\n )\n}\n\nexport default AgentProvider\n","import { AskarModule } from '@credo-ts/askar'\nimport {\n Agent,\n CacheModule,\n type DidCreateOptions,\n DidRepository,\n DidsModule,\n type InitConfig,\n JwkDidRegistrar,\n JwkDidResolver,\n KeyDidRegistrar,\n KeyDidResolver,\n SingleContextStorageLruCache,\n type TagValue,\n} from '@credo-ts/core'\nimport type { AgentModulesInput } from '@credo-ts/core/build/agent/AgentModules'\nimport { agentDependencies } from '@credo-ts/react-native'\nimport { askar } from '@openwallet-foundation/askar-react-native'\nimport type { PropsWithChildren } from 'react'\nimport { useMobileSDK } from './contexts'\nimport AgentProvider from './providers/AgentProvider'\n\nconst getCoreModules = () => {\n return {\n askar: new AskarModule({\n askar,\n }),\n dids: new DidsModule({\n registrars: [new JwkDidRegistrar(), new KeyDidRegistrar()],\n resolvers: [new JwkDidResolver(), new KeyDidResolver()],\n }),\n cache: new CacheModule({\n cache: new SingleContextStorageLruCache({\n limit: 50,\n }),\n }),\n }\n}\n\nexport interface MobileSDKModule {\n initialize(agent: Agent): void\n getAgentModules(): AgentModulesInput\n}\n\nexport type MobileSDKOptions<T extends Record<string, MobileSDKModule> = Record<string, MobileSDKModule>> = {\n agentConfig: InitConfig\n modules: T\n defaultModules?: AgentModulesInput\n}\n\nexport class MobileSDK<T extends Record<string, MobileSDKModule> = Record<string, MobileSDKModule>> {\n private localAgent: Agent | null = null\n public readonly configuration: MobileSDKOptions<T>\n public readonly modules: T\n\n public constructor(options: MobileSDKOptions<T>) {\n this.configuration = options\n this.modules = options.modules\n }\n\n public async initialize() {\n const defaultModules = this.configuration.defaultModules ?? {}\n const coreModules = getCoreModules()\n Object.assign(defaultModules, coreModules)\n\n const modules = Object.entries(this.configuration.modules).reduce((acc, [, module]) => {\n const moduleModules = module.getAgentModules()\n Object.assign(acc, moduleModules)\n return acc\n }, defaultModules)\n\n const agent = new Agent({\n config: this.configuration.agentConfig,\n dependencies: agentDependencies,\n modules,\n })\n await agent.initialize()\n\n // Initialize modules after agent is initialized to ensure all dependencies are available\n for await (const [, module] of Object.entries(this.configuration.modules)) {\n module.initialize(agent)\n }\n\n this.localAgent = agent\n return agent\n }\n\n public get agent() {\n return this.localAgent\n }\n\n public assertAndGetAgent() {\n if (!this.agent) {\n throw new Error('Agent not initialized')\n }\n\n return this.agent\n }\n\n public static AppProvider({ children }: PropsWithChildren) {\n const { sdk } = useMobileSDK()\n\n if (!sdk?.agent) {\n throw new Error('Mobile SDK not initialized')\n }\n\n return <AgentProvider agent={sdk.agent}>{children}</AgentProvider>\n }\n\n public async createDid<T extends DidCreateOptions>(options: T) {\n const agent = this.assertAndGetAgent()\n\n const did = await agent.dids.create<T>(options)\n\n return did\n }\n\n public async getDids({\n method,\n did,\n tag,\n tagValue,\n }: { method?: string; did?: string; tag?: string; tagValue?: TagValue }) {\n const agent = this.assertAndGetAgent()\n\n if (tag) {\n const didRepository = await agent.dependencyManager.resolve(DidRepository)\n\n const didRecord = await didRepository.findSingleByQuery(agent.context, tagValue ? { [tag]: tagValue } : { tag })\n return didRecord ? [didRecord] : []\n }\n\n const dids = await agent.dids.getCreatedDids({\n method,\n did,\n })\n return dids\n }\n\n public async addTagToDid({ did, tag, tagValue }: { did: string; tag: string; tagValue: TagValue }) {\n const agent = this.assertAndGetAgent()\n const didRecords = await this.getDids({ did })\n\n if (didRecords.length === 0) {\n throw new Error('Did not found')\n }\n\n const didRecord = didRecords[0]\n await didRecord.setTag(tag, tagValue)\n\n const didRepository = await agent.dependencyManager.resolve(DidRepository)\n\n await didRepository.update(agent.context, didRecord)\n\n return didRecord\n }\n\n public async resolveDid({ did }: { did: string }) {\n const agent = this.assertAndGetAgent()\n const didResolutionResult = await agent.dids.resolve(did)\n return didResolutionResult\n }\n}\n","import { useCallback } from 'react'\nimport { MobileSDK, type MobileSDKOptions } from '../MobileSDK'\nimport { useMobileSDK } from '../contexts'\n\nexport const useMobileSDKInitializer = () => {\n const { initialize, isInitialized, sdk } = useMobileSDK()\n\n const initializeSDK = useCallback(\n async (options: MobileSDKOptions) => {\n if (isInitialized) {\n throw new Error('Mobile SDK is already initialized')\n }\n\n const sdk = new MobileSDK(options)\n\n await sdk.initialize()\n initialize(sdk)\n\n return sdk\n },\n [isInitialized, initialize]\n )\n\n return { initializeSDK, isInitialized, sdk }\n}\n"],"mappings":";;;;;;;;;AAWA,MAAM,mBAAmB,cAAgD,OAAU;AAEnF,MAAa,qBAAkG;CAC7G,MAAM,UAAU,WAAW,iBAAiB;AAC5C,KAAI,YAAY,OACd,OAAM,IAAI,MAAM,wDAAwD;AAE1E,QAAO;EACL,KAAK,QAAQ;EACb,eAAe,QAAQ;EACvB,YA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/index",
|
|
4
|
+
"types": "src/index",
|
|
5
|
+
"version": "2.0.2-alpha-20251027052801",
|
|
6
|
+
"files": ["build"],
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"main": "build/index",
|
|
10
|
+
"types": "build/index",
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/credebl/mobile-sdk/tree/main/packages/core",
|
|
14
|
+
"repository": {
|
|
15
|
+
"url": "https://github.com/credebl/mobile-sdk/tree/main/packages/core",
|
|
16
|
+
"type": "git",
|
|
17
|
+
"directory": "packages/core"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"check-types": "pnpm compile --noEmit",
|
|
21
|
+
"build": "pnpm clean && pnpm compile",
|
|
22
|
+
"clean": "rimraf -rf ./build",
|
|
23
|
+
"compile": "tsdown -p tsconfig.build.json"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@credo-ts/askar": "0.6.0-alpha-20250325224513",
|
|
27
|
+
"@credo-ts/core": "0.6.0-alpha-20250325224513",
|
|
28
|
+
"@credo-ts/react-native": "0.6.0-alpha-20250325224513",
|
|
29
|
+
"rxjs": "^7.8.2"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@openwallet-foundation/askar-react-native": "catalog:"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/react": "catalog:",
|
|
36
|
+
"rimraf": "catalog:",
|
|
37
|
+
"typescript": "catalog:"
|
|
38
|
+
}
|
|
39
|
+
}
|