@dwn-protocol/id-sdk 0.2.5 → 0.2.6
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/package.json +2 -3
- package/src/agent/app-data-store.ts +0 -365
- package/src/agent/did-manager.ts +0 -393
- package/src/agent/dwn-manager.ts +0 -548
- package/src/agent/identity-manager.ts +0 -165
- package/src/agent/index.ts +0 -19
- package/src/agent/json-rpc.ts +0 -107
- package/src/agent/key-manager.ts +0 -302
- package/src/agent/kms-local.ts +0 -412
- package/src/agent/outbox.ts +0 -128
- package/src/agent/rpc-client.ts +0 -223
- package/src/agent/store-managed-did.ts +0 -295
- package/src/agent/store-managed-identity.ts +0 -243
- package/src/agent/store-managed-key.ts +0 -754
- package/src/agent/sync-manager.ts +0 -631
- package/src/agent/test-managed-agent.ts +0 -299
- package/src/agent/types/agent.ts +0 -145
- package/src/agent/types/managed-key.ts +0 -442
- package/src/agent/utils.ts +0 -190
- package/src/common/convert.ts +0 -424
- package/src/common/index.ts +0 -9
- package/src/common/multicodec.ts +0 -176
- package/src/common/object.ts +0 -43
- package/src/common/stores.ts +0 -125
- package/src/common/stream-node.ts +0 -381
- package/src/common/stream.ts +0 -406
- package/src/common/type-utils.ts +0 -117
- package/src/common/types.ts +0 -48
- package/src/credentials/credential-bbs.ts +0 -419
- package/src/credentials/credential.ts +0 -324
- package/src/credentials/index.ts +0 -5
- package/src/credentials/presentation.ts +0 -182
- package/src/credentials/status-list.ts +0 -365
- package/src/credentials/utils.ts +0 -58
- package/src/credentials/validators.ts +0 -52
- package/src/crypto/algorithms-api/aes/base.ts +0 -49
- package/src/crypto/algorithms-api/aes/ctr.ts +0 -51
- package/src/crypto/algorithms-api/aes/index.ts +0 -2
- package/src/crypto/algorithms-api/crypto-algorithm.ts +0 -127
- package/src/crypto/algorithms-api/crypto-key.ts +0 -56
- package/src/crypto/algorithms-api/ec/base.ts +0 -39
- package/src/crypto/algorithms-api/ec/ecdh.ts +0 -53
- package/src/crypto/algorithms-api/ec/ecdsa.ts +0 -37
- package/src/crypto/algorithms-api/ec/eddsa.ts +0 -30
- package/src/crypto/algorithms-api/ec/index.ts +0 -4
- package/src/crypto/algorithms-api/errors.ts +0 -29
- package/src/crypto/algorithms-api/index.ts +0 -6
- package/src/crypto/algorithms-api/pbkdf/index.ts +0 -1
- package/src/crypto/algorithms-api/pbkdf/pbkdf2.ts +0 -91
- package/src/crypto/crypto-algorithms/aes-ctr.ts +0 -70
- package/src/crypto/crypto-algorithms/bbs.ts +0 -110
- package/src/crypto/crypto-algorithms/ecdh.ts +0 -115
- package/src/crypto/crypto-algorithms/ecdsa.ts +0 -111
- package/src/crypto/crypto-algorithms/eddsa.ts +0 -110
- package/src/crypto/crypto-algorithms/index.ts +0 -6
- package/src/crypto/crypto-algorithms/pbkdf2.ts +0 -54
- package/src/crypto/crypto-primitives/aes-ctr.ts +0 -131
- package/src/crypto/crypto-primitives/aes-gcm.ts +0 -138
- package/src/crypto/crypto-primitives/bbs.ts +0 -183
- package/src/crypto/crypto-primitives/concat-kdf.ts +0 -207
- package/src/crypto/crypto-primitives/ed25519.ts +0 -201
- package/src/crypto/crypto-primitives/index.ts +0 -10
- package/src/crypto/crypto-primitives/pbkdf2.ts +0 -78
- package/src/crypto/crypto-primitives/secp256k1.ts +0 -322
- package/src/crypto/crypto-primitives/x25519.ts +0 -101
- package/src/crypto/crypto-primitives/xchacha20-poly1305.ts +0 -46
- package/src/crypto/crypto-primitives/xchacha20.ts +0 -34
- package/src/crypto/index.ts +0 -8
- package/src/crypto/jose.ts +0 -948
- package/src/crypto/types/crypto-key.ts +0 -4
- package/src/crypto/types/iddwn-crypto.ts +0 -119
- package/src/crypto/utils.ts +0 -200
- package/src/did-api.ts +0 -72
- package/src/dids/dht.ts +0 -412
- package/src/dids/did-dht.ts +0 -436
- package/src/dids/did-ion.ts +0 -613
- package/src/dids/did-key.ts +0 -791
- package/src/dids/did-resolver.ts +0 -107
- package/src/dids/index.ts +0 -9
- package/src/dids/resolver-cache-level.ts +0 -82
- package/src/dids/resolver-cache-noop.ts +0 -25
- package/src/dids/types.ts +0 -278
- package/src/dids/utils.ts +0 -129
- package/src/dwn-api.ts +0 -584
- package/src/iddwn.ts +0 -241
- package/src/identity-agent/index.ts +0 -270
- package/src/index.ts +0 -26
- package/src/interfaces/metadata.ts +0 -163
- package/src/interfaces/queue.ts +0 -108
- package/src/interfaces/services.ts +0 -122
- package/src/interfaces/transactions.ts +0 -220
- package/src/protocol.ts +0 -68
- package/src/proxy-agent/index.ts +0 -255
- package/src/record.ts +0 -521
- package/src/service-options.ts +0 -62
- package/src/typings/decentralized-identity__ion-pow-sdk.d.ts +0 -7
- package/src/user-agent/index.ts +0 -295
- package/src/utils.ts +0 -29
- package/src/vc-api.ts +0 -505
package/src/iddwn.ts
DELETED
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
import type { AppDataStore, IDAgent, IDManagedAgent } from './agent/index.js';
|
|
2
|
-
import * as Sdk from '@dwn-protocol/id';
|
|
3
|
-
import ms from 'ms';
|
|
4
|
-
import { IDUserAgent } from './user-agent/index.js';
|
|
5
|
-
|
|
6
|
-
import { DwnApi } from './dwn-api.js';
|
|
7
|
-
import { DidApi } from './did-api.js';
|
|
8
|
-
import { getServiceDwnEndpoints } from './service-options.js';
|
|
9
|
-
import { DidKeyMethod, DidDhtMethod, DidIonMethod, DidDht } from './dids/index.js';
|
|
10
|
-
import { Metadata } from './interfaces/metadata.js';
|
|
11
|
-
import { Queue } from './interfaces/queue.js';
|
|
12
|
-
import { Services } from './interfaces/services.js';
|
|
13
|
-
import { Transactions } from './interfaces/transactions.js';
|
|
14
|
-
import { Jose } from './crypto/index.js';
|
|
15
|
-
import { VcApi } from './vc-api.js';
|
|
16
|
-
import { Jws } from '@dwn-protocol/id';
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Override defaults.
|
|
20
|
-
*/
|
|
21
|
-
export type ServiceOptions = {
|
|
22
|
-
// Override default dwnEndpoints provided.
|
|
23
|
-
dwnEndpoints?: string[];
|
|
24
|
-
// Override the did:dht pkarr relay endpoint used by SDK resolution/publish.
|
|
25
|
-
dhtRelayUrl?: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Optional overrides that can be provided when calling {@link IDDwn.connect}.
|
|
30
|
-
*/
|
|
31
|
-
export type IDConnectOptions = {
|
|
32
|
-
/** Provide a {@link IDAgent} implementation. Defaults to creating a local
|
|
33
|
-
* {@link IDUserAgent} if one isn't provided */
|
|
34
|
-
agent?: IDAgent;
|
|
35
|
-
|
|
36
|
-
/** Provide an instance of a {@link AppDataStore} implementation. Defaults to
|
|
37
|
-
* a LevelDB-backed store with an insecure, static unlock passphrase if one
|
|
38
|
-
* isn't provided. To allow the app user to enter a secure passphrase of
|
|
39
|
-
* their choosing, provide an initialized {@link AppDataStore} instance. */
|
|
40
|
-
appData?: AppDataStore;
|
|
41
|
-
|
|
42
|
-
// Specify an existing DID to connect to.
|
|
43
|
-
connectedDid?: string;
|
|
44
|
-
|
|
45
|
-
/** Specify the DID method to use when creating a new identity.
|
|
46
|
-
* Defaults to 'ion'. Supported methods: 'ion', 'dht', 'key' */
|
|
47
|
-
didMethod?: 'ion' | 'dht' | 'key';
|
|
48
|
-
|
|
49
|
-
/** Enable synchronization of DWN records between local and remote DWNs.
|
|
50
|
-
* Sync defaults to running every 30 seconds and can be set to any value accepted by `ms()`.
|
|
51
|
-
* To disable sync set to 'off'. */
|
|
52
|
-
sync?: string;
|
|
53
|
-
|
|
54
|
-
/** When true (default), failed outbound DWN sends are queued in an outbox and replayed when online.
|
|
55
|
-
* Set to false to throw on send failure instead of queuing. */
|
|
56
|
-
queueWhenOffline?: boolean;
|
|
57
|
-
|
|
58
|
-
/** When true and in a browser, run sync and outbox drain immediately when the window 'online' event fires.
|
|
59
|
-
* Default false. In Node, use flushOutboxAndSync() after your own connectivity check. */
|
|
60
|
-
flushWhenOnline?: boolean;
|
|
61
|
-
|
|
62
|
-
/** Override defaults service options.
|
|
63
|
-
* See {@link ServiceOptions} for available options. */
|
|
64
|
-
serviceOptions?: ServiceOptions;
|
|
65
|
-
|
|
66
|
-
passphrase?: string;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @see {@link IDConnectOptions}
|
|
71
|
-
*/
|
|
72
|
-
type IDOptions = {
|
|
73
|
-
agent: IDAgent;
|
|
74
|
-
connectedDid: string;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
type UtilsOptions = {
|
|
78
|
-
DidKeyMethod: DidKeyMethod;
|
|
79
|
-
DidDhtMethod: DidDhtMethod;
|
|
80
|
-
DidIonMethod: DidIonMethod;
|
|
81
|
-
Jose: Jose;
|
|
82
|
-
Jws: Jws;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export class IDDwn {
|
|
86
|
-
agent: IDAgent;
|
|
87
|
-
private connectedDid: string;
|
|
88
|
-
did: DidApi;
|
|
89
|
-
dwn: DwnApi;
|
|
90
|
-
metadata: Metadata;
|
|
91
|
-
jose: Jose;
|
|
92
|
-
protocol: any;
|
|
93
|
-
queue: Queue;
|
|
94
|
-
services: Services;
|
|
95
|
-
transactions: Transactions;
|
|
96
|
-
utils: UtilsOptions;
|
|
97
|
-
vc: VcApi;
|
|
98
|
-
|
|
99
|
-
constructor(options: IDOptions) {
|
|
100
|
-
const { agent, connectedDid } = options;
|
|
101
|
-
this.agent = agent;
|
|
102
|
-
this.connectedDid = connectedDid;
|
|
103
|
-
this.did = new DidApi({ agent, connectedDid });
|
|
104
|
-
this.dwn = new DwnApi({ agent, connectedDid });
|
|
105
|
-
this.metadata = new Metadata({ agent, connectedDid });
|
|
106
|
-
this.protocol = Sdk;
|
|
107
|
-
this.queue = new Queue({ agent, connectedDid });
|
|
108
|
-
this.services = new Services({ agent, connectedDid });
|
|
109
|
-
this.transactions = new Transactions({ agent, connectedDid });
|
|
110
|
-
this.utils = { DidKeyMethod, DidDhtMethod, DidIonMethod, Jose, Jws } as UtilsOptions;
|
|
111
|
-
this.vc = new VcApi({ agent, connectedDid, dwnApi: this.dwn });
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Connects to a {@link IDAgent}. Defaults to creating a local {@link IDUserAgent}
|
|
116
|
-
* if one isn't provided.
|
|
117
|
-
*
|
|
118
|
-
* @param options - optional overrides
|
|
119
|
-
* @returns
|
|
120
|
-
*/
|
|
121
|
-
static async connect(options: IDConnectOptions = {}) {
|
|
122
|
-
let { agent, appData, connectedDid, sync, serviceOptions, passphrase, didMethod, queueWhenOffline, flushWhenOnline } = options;
|
|
123
|
-
|
|
124
|
-
// Default to 'ion' for backward compatibility
|
|
125
|
-
didMethod = didMethod ?? 'ion';
|
|
126
|
-
|
|
127
|
-
// Apply did:dht relay target globally.
|
|
128
|
-
// Priority:
|
|
129
|
-
// 1) Explicit serviceOptions.dhtRelayUrl
|
|
130
|
-
// 2) Derive from first configured DWN endpoint as `${endpoint}/dht`
|
|
131
|
-
if (serviceOptions?.dhtRelayUrl) {
|
|
132
|
-
DidDht.setRelayUrl(serviceOptions.dhtRelayUrl.replace(/\/+$/, ''));
|
|
133
|
-
} else if (serviceOptions?.dwnEndpoints?.length) {
|
|
134
|
-
const derivedRelayUrl = `${serviceOptions.dwnEndpoints[0]!.replace(/\/+$/, '')}/dht`;
|
|
135
|
-
DidDht.setRelayUrl(derivedRelayUrl);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (agent === undefined) {
|
|
139
|
-
|
|
140
|
-
// Create the agent (with outbox when queueWhenOffline is not disabled).
|
|
141
|
-
const userAgent: IDManagedAgent = await IDUserAgent.create({ appData, queueWhenOffline });
|
|
142
|
-
agent = userAgent;
|
|
143
|
-
|
|
144
|
-
if (passphrase === undefined) {
|
|
145
|
-
passphrase = 'insecure-static-phrase';
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// Start the agent.
|
|
149
|
-
await userAgent.start({ passphrase });
|
|
150
|
-
|
|
151
|
-
// Connect attempt failed or was rejected so fallback to local user agent.
|
|
152
|
-
// if (IDUserAgent.isConnected() === false) {
|
|
153
|
-
|
|
154
|
-
// Query the Agent's DWN tenant for identity records.
|
|
155
|
-
const identities = await userAgent.identityManager.list();
|
|
156
|
-
const storedIdentities = identities.length;
|
|
157
|
-
|
|
158
|
-
// If an existing identity is not found, create a new one.
|
|
159
|
-
if (storedIdentities === 0) {
|
|
160
|
-
// Use the specified DWN endpoints or get default relayer nodes.
|
|
161
|
-
const serviceEndpointNodes = serviceOptions?.dwnEndpoints ?? await getServiceDwnEndpoints();
|
|
162
|
-
|
|
163
|
-
// Generate DID options based on the selected method
|
|
164
|
-
let didOptions;
|
|
165
|
-
switch (didMethod) {
|
|
166
|
-
case 'dht':
|
|
167
|
-
didOptions = await DidDhtMethod.generateDwnOptions({ serviceEndpointNodes });
|
|
168
|
-
break;
|
|
169
|
-
case 'ion':
|
|
170
|
-
didOptions = await DidIonMethod.generateDwnOptions({ serviceEndpointNodes });
|
|
171
|
-
break;
|
|
172
|
-
case 'key':
|
|
173
|
-
// did:key doesn't need service endpoints or key generation options
|
|
174
|
-
didOptions = {};
|
|
175
|
-
break;
|
|
176
|
-
default:
|
|
177
|
-
throw new Error(`Unsupported DID method: ${didMethod}`);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Generate a new Identity for the end-user.
|
|
181
|
-
const identity = await userAgent.identityManager.create({
|
|
182
|
-
name : 'Default',
|
|
183
|
-
didMethod,
|
|
184
|
-
didOptions,
|
|
185
|
-
kms : 'local'
|
|
186
|
-
});
|
|
187
|
-
/** Import the Identity metadata to the User Agent's tenant so that it can be restored
|
|
188
|
-
* on subsequent launches or page reloads. */
|
|
189
|
-
await userAgent.identityManager.import({ identity, context: userAgent.agentDid });
|
|
190
|
-
// Set the newly created identity as the connected DID.
|
|
191
|
-
// connectedDid = restoreDid? restoreDid : identity.did;
|
|
192
|
-
connectedDid = identity.did;
|
|
193
|
-
|
|
194
|
-
} else {
|
|
195
|
-
// An existing identity was found in the User Agent's tenant.
|
|
196
|
-
const [ identity ] = identities;
|
|
197
|
-
// Set the stored identity as the connected DID.
|
|
198
|
-
// connectedDid = restoreDid? restoreDid : identity.did;
|
|
199
|
-
connectedDid = identity.did;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// }
|
|
203
|
-
|
|
204
|
-
// Enable sync, unless disabled.
|
|
205
|
-
if (sync !== 'off') {
|
|
206
|
-
// First, register the user identity for sync.
|
|
207
|
-
await userAgent.syncManager.registerIdentity({ did: connectedDid });
|
|
208
|
-
|
|
209
|
-
// Enable sync using the specified interval or default.
|
|
210
|
-
sync ??= '1m';
|
|
211
|
-
userAgent.syncManager.startSync({ interval: ms(sync) })
|
|
212
|
-
.catch(async (error: Error) => {
|
|
213
|
-
console.error(`Sync failed: ${error}`);
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const iddwn = new IDDwn({ agent, connectedDid });
|
|
220
|
-
|
|
221
|
-
if (flushWhenOnline && typeof window !== 'undefined') {
|
|
222
|
-
window.addEventListener('online', () => {
|
|
223
|
-
iddwn.flushOutboxAndSync().catch((err: Error) => {
|
|
224
|
-
console.error('flushOutboxAndSync on online:', err);
|
|
225
|
-
});
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
return { iddwn, did: connectedDid };
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Run outbox drain then one sync cycle (push + pull). Use when back online
|
|
234
|
-
* to flush queued sends and sync immediately without waiting for the next interval.
|
|
235
|
-
*/
|
|
236
|
-
async flushOutboxAndSync(): Promise<void> {
|
|
237
|
-
const managed = this.agent as IDManagedAgent;
|
|
238
|
-
if (managed.outbox) await managed.outbox.drain();
|
|
239
|
-
if (managed.syncManager?.runNow) await managed.syncManager.runNow();
|
|
240
|
-
}
|
|
241
|
-
}
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
IDRpc,
|
|
3
|
-
DidRequest,
|
|
4
|
-
VcResponse,
|
|
5
|
-
DidResponse,
|
|
6
|
-
DwnResponse,
|
|
7
|
-
SyncManager,
|
|
8
|
-
AppDataStore,
|
|
9
|
-
SendVcRequest,
|
|
10
|
-
SendDwnRequest,
|
|
11
|
-
ProcessVcRequest,
|
|
12
|
-
IDManagedAgent,
|
|
13
|
-
ProcessDwnRequest,
|
|
14
|
-
} from '../agent/index.js';
|
|
15
|
-
|
|
16
|
-
import { LevelStore } from '../common/index.js';
|
|
17
|
-
import { EdDsaAlgorithm } from '../crypto/index.js';
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
19
|
-
import { DidIonMethod, DidKeyMethod, DidResolverCacheLevel, DidResolver } from '../dids/index.js';
|
|
20
|
-
import {
|
|
21
|
-
LocalKms,
|
|
22
|
-
DidManager,
|
|
23
|
-
DidMessage,
|
|
24
|
-
DwnManager,
|
|
25
|
-
KeyManager,
|
|
26
|
-
DidStoreDwn,
|
|
27
|
-
KeyStoreDwn,
|
|
28
|
-
AppDataVault,
|
|
29
|
-
IDRpcClient,
|
|
30
|
-
IdentityManager,
|
|
31
|
-
IdentityStoreDwn,
|
|
32
|
-
SyncManagerLevel,
|
|
33
|
-
PrivateKeyStoreDwn,
|
|
34
|
-
cryptoToPortableKeyPair,
|
|
35
|
-
} from '../agent/index.js';
|
|
36
|
-
|
|
37
|
-
export type IdentityAgentOptions = {
|
|
38
|
-
agentDid: string;
|
|
39
|
-
appData: AppDataStore;
|
|
40
|
-
didManager: DidManager;
|
|
41
|
-
didResolver: DidResolver;
|
|
42
|
-
dwnManager: DwnManager;
|
|
43
|
-
identityManager: IdentityManager;
|
|
44
|
-
keyManager: KeyManager;
|
|
45
|
-
rpcClient: IDRpc;
|
|
46
|
-
syncManager: SyncManager;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export class IdentityAgent implements IDManagedAgent {
|
|
50
|
-
agentDid: string;
|
|
51
|
-
appData: AppDataStore;
|
|
52
|
-
didManager: DidManager;
|
|
53
|
-
didResolver: DidResolver;
|
|
54
|
-
dwnManager: DwnManager;
|
|
55
|
-
identityManager: IdentityManager;
|
|
56
|
-
keyManager: KeyManager;
|
|
57
|
-
rpcClient: IDRpc;
|
|
58
|
-
syncManager: SyncManager;
|
|
59
|
-
|
|
60
|
-
constructor(options: IdentityAgentOptions) {
|
|
61
|
-
this.agentDid = options.agentDid;
|
|
62
|
-
this.appData = options.appData;
|
|
63
|
-
this.didManager = options.didManager;
|
|
64
|
-
this.didResolver = options.didResolver;
|
|
65
|
-
this.dwnManager = options.dwnManager;
|
|
66
|
-
this.identityManager = options.identityManager;
|
|
67
|
-
this.keyManager = options.keyManager;
|
|
68
|
-
this.rpcClient = options.rpcClient;
|
|
69
|
-
this.syncManager = options.syncManager;
|
|
70
|
-
|
|
71
|
-
// Set this agent to be the default agent.
|
|
72
|
-
this.didManager.agent = this;
|
|
73
|
-
this.dwnManager.agent = this;
|
|
74
|
-
this.identityManager.agent = this;
|
|
75
|
-
this.keyManager.agent = this;
|
|
76
|
-
this.syncManager.agent = this;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
static async create(options: Partial<IdentityAgentOptions> = {}): Promise<IdentityAgent> {
|
|
80
|
-
let {
|
|
81
|
-
agentDid, appData, didManager, didResolver, dwnManager,
|
|
82
|
-
identityManager, keyManager, rpcClient, syncManager
|
|
83
|
-
} = options;
|
|
84
|
-
|
|
85
|
-
if (agentDid === undefined) {
|
|
86
|
-
// An Agent DID was not specified, so set to empty string.
|
|
87
|
-
agentDid = '';
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (appData === undefined) {
|
|
91
|
-
// A custom AppDataStore implementation was not specified, so
|
|
92
|
-
// instantiate a LevelDB backed secure AppDataVault.
|
|
93
|
-
appData = new AppDataVault({
|
|
94
|
-
store: new LevelStore('data/AGENT/VAULT')
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (didManager === undefined) {
|
|
99
|
-
// A custom DidManager implementation was not specified, so
|
|
100
|
-
// instantiate a default with in-memory store.
|
|
101
|
-
didManager = new DidManager({
|
|
102
|
-
didMethods : [DidIonMethod, DidKeyMethod],
|
|
103
|
-
store : new DidStoreDwn()
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (didResolver === undefined) {
|
|
108
|
-
// A custom DidManager implementation was not specified, so
|
|
109
|
-
// instantiate a default with in-memory store.
|
|
110
|
-
didResolver = new DidResolver({
|
|
111
|
-
// cache : new DidResolverCacheLevel(),
|
|
112
|
-
didResolvers: [DidIonMethod, DidKeyMethod]
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (dwnManager === undefined) {
|
|
117
|
-
// A custom DwnManager implementation was not specified, so
|
|
118
|
-
// instantiate a default.
|
|
119
|
-
dwnManager = await DwnManager.create({ didResolver });
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (identityManager === undefined) {
|
|
123
|
-
// A custom IdentityManager implementation was not specified, so
|
|
124
|
-
// instantiate a default that uses a DWN store.
|
|
125
|
-
identityManager = new IdentityManager({
|
|
126
|
-
store: new IdentityStoreDwn()
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (keyManager === undefined) {
|
|
131
|
-
// A custom KeyManager implementation was not specified, so
|
|
132
|
-
// instantiate a default with KMSs.
|
|
133
|
-
const localKmsDwn = new LocalKms({
|
|
134
|
-
kmsName : 'local',
|
|
135
|
-
keyStore : new KeyStoreDwn({ schema: 'https://abaxx.tech/schemas/dwn/kms-key' }),
|
|
136
|
-
privateKeyStore : new PrivateKeyStoreDwn()
|
|
137
|
-
});
|
|
138
|
-
const localKmsMemory = new LocalKms({
|
|
139
|
-
kmsName: 'memory'
|
|
140
|
-
});
|
|
141
|
-
keyManager = new KeyManager({
|
|
142
|
-
kms: {
|
|
143
|
-
local : localKmsDwn,
|
|
144
|
-
memory : localKmsMemory
|
|
145
|
-
},
|
|
146
|
-
store: new KeyStoreDwn({ schema: 'https://abaxx.tech/schemas/dwn/managed-key' })
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (rpcClient === undefined) {
|
|
151
|
-
// A custom RPC Client implementation was not specified, so
|
|
152
|
-
// instantiate a default.
|
|
153
|
-
rpcClient = new IDRpcClient();
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
if (syncManager === undefined) {
|
|
157
|
-
// A custom SyncManager implementation was not specified, so
|
|
158
|
-
// instantiate a LevelDB-backed default.
|
|
159
|
-
syncManager = new SyncManagerLevel();
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Instantiate the Identity Agent.
|
|
163
|
-
const agent = new IdentityAgent({
|
|
164
|
-
agentDid,
|
|
165
|
-
appData,
|
|
166
|
-
didManager,
|
|
167
|
-
didResolver,
|
|
168
|
-
dwnManager,
|
|
169
|
-
identityManager,
|
|
170
|
-
keyManager,
|
|
171
|
-
rpcClient,
|
|
172
|
-
syncManager
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
return agent;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
async firstLaunch(): Promise<boolean> {
|
|
179
|
-
// Check whether data vault is already initialized.
|
|
180
|
-
const { initialized } = await this.appData.getStatus();
|
|
181
|
-
return initialized === false;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Executed once the first time the Identity Agent is launched.
|
|
186
|
-
* The passphrase should be input by the end-user.
|
|
187
|
-
*/
|
|
188
|
-
async initialize(options: { passphrase: string }) {
|
|
189
|
-
const { passphrase } = options;
|
|
190
|
-
|
|
191
|
-
// Generate an Ed25519 key pair for the Identity Agent.
|
|
192
|
-
const agentKeyPair = await new EdDsaAlgorithm().generateKey({
|
|
193
|
-
algorithm : { name: 'EdDSA', namedCurve: 'Ed25519' },
|
|
194
|
-
extractable : true,
|
|
195
|
-
keyUsages : ['sign', 'verify']
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
/** Initialize the AppDataStore with the Identity Agent's
|
|
199
|
-
* private key and passphrase, which also unlocks the data vault. */
|
|
200
|
-
await this.appData.initialize({
|
|
201
|
-
passphrase : passphrase,
|
|
202
|
-
keyPair : agentKeyPair,
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async processDidRequest(request: DidRequest): Promise<DidResponse> {
|
|
207
|
-
switch (request.messageType) {
|
|
208
|
-
case DidMessage.Resolve: {
|
|
209
|
-
const { didUrl, resolutionOptions } = request.messageOptions;
|
|
210
|
-
const result = await this.didResolver.resolve(didUrl, resolutionOptions);
|
|
211
|
-
return { result };
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
default: {
|
|
215
|
-
return this.didManager.processRequest(request);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
async processDwnRequest(request: ProcessDwnRequest): Promise<DwnResponse> {
|
|
221
|
-
return this.dwnManager.processRequest(request);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
async processVcRequest(_request: ProcessVcRequest): Promise<VcResponse> {
|
|
225
|
-
throw new Error('Not implemented');
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async sendDidRequest(_request: DidRequest): Promise<DidResponse> {
|
|
229
|
-
throw new Error('Not implemented');
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
async sendDwnRequest(request: SendDwnRequest): Promise<DwnResponse> {
|
|
233
|
-
return this.dwnManager.sendRequest(request);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
async sendVcRequest(_request: SendVcRequest): Promise<VcResponse> {
|
|
237
|
-
throw new Error('Not implemented');
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
async start(options: { passphrase: string }) {
|
|
241
|
-
const { passphrase } = options;
|
|
242
|
-
|
|
243
|
-
if (await this.firstLaunch()) {
|
|
244
|
-
// 1A. Agent's first launch so initialize.
|
|
245
|
-
await this.initialize({ passphrase });
|
|
246
|
-
} else {
|
|
247
|
-
// 1B. Agent was previously initialized.
|
|
248
|
-
// Unlock the data vault and cache the vault unlock key (VUK) in memory.
|
|
249
|
-
await this.appData.unlock({ passphrase });
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// 2. Set the Identity Agent's root did:key identifier.
|
|
253
|
-
this.agentDid = await this.appData.getDid();
|
|
254
|
-
|
|
255
|
-
// 3. Import the Identity Agent's signing key pair to KeyManager.
|
|
256
|
-
const defaultSigningKey = cryptoToPortableKeyPair({
|
|
257
|
-
cryptoKeyPair: {
|
|
258
|
-
privateKey : await this.appData.getPrivateKey(),
|
|
259
|
-
publicKey : await this.appData.getPublicKey()
|
|
260
|
-
},
|
|
261
|
-
keyData: {
|
|
262
|
-
alias : await this.didManager.getDefaultSigningKey({ did: this.agentDid }),
|
|
263
|
-
kms : 'memory'
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
// Import the Agent's signing key pair to the in-memory KMS key stores.
|
|
268
|
-
await this.keyManager.setDefaultSigningKey({ key: defaultSigningKey });
|
|
269
|
-
}
|
|
270
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Making developing with decentralized identity components simple.
|
|
3
|
-
*
|
|
4
|
-
* ID SDK consists of the following components:
|
|
5
|
-
* - Decentralized Identifiers
|
|
6
|
-
* - Verifiable Credentials
|
|
7
|
-
* - DWN personal and shareable datastores
|
|
8
|
-
*
|
|
9
|
-
* [Link to GitHub Repo](https://github.com/d-protocol/id-sdk)
|
|
10
|
-
*
|
|
11
|
-
* @packageDocumentation
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
export * from './did-api.js';
|
|
15
|
-
export * from './dwn-api.js';
|
|
16
|
-
export * from './protocol.js';
|
|
17
|
-
export * from './record.js';
|
|
18
|
-
export * from './vc-api.js';
|
|
19
|
-
export * from './iddwn.js';
|
|
20
|
-
export * from './service-options.js';
|
|
21
|
-
export * from './credentials/credential-bbs.js';
|
|
22
|
-
export { Bbs } from './crypto/crypto-primitives/bbs.js';
|
|
23
|
-
export { BbsAlgorithm } from './crypto/crypto-algorithms/bbs.js';
|
|
24
|
-
|
|
25
|
-
import * as utils from './utils.js';
|
|
26
|
-
export { utils };
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import type { IDAgent } from '../agent/index.js';
|
|
2
|
-
|
|
3
|
-
import { CID } from 'multiformats';
|
|
4
|
-
import { Encoder, Encryption } from '@dwn-protocol/id';
|
|
5
|
-
|
|
6
|
-
import bs58 from 'bs58';
|
|
7
|
-
import IPFS from 'ipfs-infura';
|
|
8
|
-
|
|
9
|
-
import { getServiceDwnEndpoints } from '../service-options.js';
|
|
10
|
-
import _ from 'lodash';
|
|
11
|
-
|
|
12
|
-
export class Metadata {
|
|
13
|
-
private agent: IDAgent;
|
|
14
|
-
private connectedDid: string;
|
|
15
|
-
private _ipfs: IPFS;
|
|
16
|
-
|
|
17
|
-
constructor(options: { agent: IDAgent, connectedDid: string }) {
|
|
18
|
-
this.agent = options.agent;
|
|
19
|
-
this.connectedDid = options.connectedDid;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async config() {
|
|
23
|
-
const h = '1220' + '0x32216e417b6f98f95febedf6a747c5020ea95558fbebd98ba98a155791b0b6d2'.slice(2);
|
|
24
|
-
const b = Buffer.from(h, 'hex');
|
|
25
|
-
const c = bs58.encode(b);
|
|
26
|
-
const r = await fetch(`https://dwn.infura-ipfs.io/ipfs/${CID.parse(c).toV1().toString()}`);
|
|
27
|
-
return JSON.parse(bs58.decode(await r.text()).toString());
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async aliasGet(alias) {
|
|
31
|
-
const relayer = _.sample(await getServiceDwnEndpoints());
|
|
32
|
-
const response = await fetch(`${relayer}/did/${alias}`, {
|
|
33
|
-
method : 'GET',
|
|
34
|
-
mode : 'cors',
|
|
35
|
-
cache : 'no-cache',
|
|
36
|
-
headers : {
|
|
37
|
-
'Accept' : '*/*',
|
|
38
|
-
'Content-Type' : 'application/json',
|
|
39
|
-
},
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
return await response.text();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async aliasSet(alias, did, metadata = { VerifiableCredentials: [] }) {
|
|
46
|
-
const relayer = _.sample(await getServiceDwnEndpoints());
|
|
47
|
-
const response = await fetch(`${relayer}/did`, {
|
|
48
|
-
method : 'POST',
|
|
49
|
-
mode : 'cors',
|
|
50
|
-
cache : 'no-cache',
|
|
51
|
-
headers : {
|
|
52
|
-
'Accept' : '*/*',
|
|
53
|
-
'Content-Type' : 'application/json',
|
|
54
|
-
},
|
|
55
|
-
body: JSON.stringify({
|
|
56
|
-
alias,
|
|
57
|
-
did,
|
|
58
|
-
metadata,
|
|
59
|
-
}),
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
return await response.text();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async aliasPut(alias, did, metadata = { VerifiableCredentials: [] }) {
|
|
66
|
-
const relayer = _.sample(await getServiceDwnEndpoints());
|
|
67
|
-
const response = await fetch(`${relayer}/did`, {
|
|
68
|
-
method : 'PUT',
|
|
69
|
-
mode : 'cors',
|
|
70
|
-
cache : 'no-cache',
|
|
71
|
-
headers : {
|
|
72
|
-
'Accept' : '*/*',
|
|
73
|
-
'Content-Type' : 'application/json',
|
|
74
|
-
},
|
|
75
|
-
body: JSON.stringify({
|
|
76
|
-
alias,
|
|
77
|
-
did,
|
|
78
|
-
metadata,
|
|
79
|
-
}),
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
return await response.text();
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async save(data) {
|
|
86
|
-
try {
|
|
87
|
-
const b = Buffer.from(data);
|
|
88
|
-
const d = bs58.encode(b);
|
|
89
|
-
this._ipfs = new IPFS(await this.config());
|
|
90
|
-
const cid = await this._ipfs.add(d);
|
|
91
|
-
this._ipfs = undefined;
|
|
92
|
-
return this.getBytes32FromIpfsHash(cid.toString());
|
|
93
|
-
} catch (e) {
|
|
94
|
-
throw new Error('Failure to submit file to IPFS');
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async get(id) {
|
|
99
|
-
try {
|
|
100
|
-
const cid = this.getIpfsHashFromBytes32(id);
|
|
101
|
-
const response = await fetch(`https://dwn.infura-ipfs.io/ipfs/${CID.parse(cid).toV1().toString()}`);
|
|
102
|
-
const text = await response.text();
|
|
103
|
-
return bs58.decode(text).toString();
|
|
104
|
-
} catch (e) {
|
|
105
|
-
throw new Error('Failure to get file from IPFS');
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async saveJson(jsonData) {
|
|
110
|
-
try {
|
|
111
|
-
this._ipfs = new IPFS(await this.config());
|
|
112
|
-
const cid = await this._ipfs.addJSON(jsonData);
|
|
113
|
-
this._ipfs = undefined;
|
|
114
|
-
return this.getBytes32FromIpfsHash(cid.toString());
|
|
115
|
-
} catch (e) {
|
|
116
|
-
throw new Error('Failure to submit file to IPFS');
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async getJson(id) {
|
|
121
|
-
try {
|
|
122
|
-
const cid = this.getIpfsHashFromBytes32(id);
|
|
123
|
-
const response = await fetch(`https://dwn.infura-ipfs.io/ipfs/${CID.parse(cid).toV1().toString()}`);
|
|
124
|
-
const json = await response.json();
|
|
125
|
-
return json;
|
|
126
|
-
} catch (e) {
|
|
127
|
-
throw new Error('Failure to get file from IPFS');
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
async encrypt(publicKey, input) {
|
|
132
|
-
let encryptionOutput = await Encryption.eciesSecp256k1Encrypt(publicKey, input);
|
|
133
|
-
return Buffer.from(Encoder.bytesToString(Encoder.objectToBytes(encryptionOutput))).toString('base64');
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
async decrypt(privateKey, output) {
|
|
137
|
-
let newOutput = {};
|
|
138
|
-
let json = Buffer.from(output, 'base64').toString('ascii');
|
|
139
|
-
Object.entries(JSON.parse(json)).forEach((entry) => {
|
|
140
|
-
const [key, value] = entry;
|
|
141
|
-
//@ts-ignore
|
|
142
|
-
newOutput[key] = value.type == 'Buffer' ? Buffer.from(value.data) : value;
|
|
143
|
-
});
|
|
144
|
-
const decryptionInput = { privateKey, ...newOutput };
|
|
145
|
-
//@ts-ignore
|
|
146
|
-
const decryptedPlaintext = await Encryption.eciesSecp256k1Decrypt(decryptionInput);
|
|
147
|
-
return new TextDecoder().decode(decryptedPlaintext);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
private getBytes32FromIpfsHash(ipfsHash) {
|
|
151
|
-
return (
|
|
152
|
-
'0x' + bs58.decode(ipfsHash).slice(2).toString('hex')
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
private getIpfsHashFromBytes32(bytes32Hex) {
|
|
157
|
-
const hashHex = '1220' + bytes32Hex.slice(2);
|
|
158
|
-
const hashBytes = Buffer.from(hashHex, 'hex');
|
|
159
|
-
const hashStr = bs58.encode(hashBytes);
|
|
160
|
-
return hashStr;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
}
|