-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathlocalAgent.test.ts
310 lines (295 loc) · 10.1 KB
/
localAgent.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// noinspection ES6PreferShortImport
/**
* This runs a suite of ./shared tests using an agent configured for local operations,
* using a SQLite db for storage of credentials, presentations, messages as well as keys and DIDs.
*
* This suite also runs a ganache local blockchain to run through some examples of DIDComm using did:ethr identifiers.
*/
import { createAgent } from '../packages/core/src'
import {
IAgentOptions,
ICredentialPlugin,
IDataStore,
IDataStoreORM,
IDIDManager,
IKeyManager,
IMessageHandler,
IResolver,
TAgent,
} from '../packages/core-types/src'
import { MessageHandler } from '../packages/message-handler/src'
import { KeyManager } from '../packages/key-manager/src'
import { AliasDiscoveryProvider, DIDManager } from '../packages/did-manager/src'
import { DIDResolverPlugin } from '../packages/did-resolver/src'
import { JwtMessageHandler } from '../packages/did-jwt/src'
import { CredentialPlugin, W3cMessageHandler } from '../packages/credential-w3c/src'
import { CredentialProviderEIP712 } from '../packages/credential-eip712/src'
import { CredentialProviderJWT } from '../packages/credential-jwt/src'
import {
CredentialProviderLD,
LdDefaultContexts,
VeramoEcdsaSecp256k1RecoverySignature2020,
VeramoEd25519Signature2018,
VeramoEd25519Signature2020,
VeramoJsonWebSignature2020,
} from '../packages/credential-ld/src'
import { EthrDIDProvider } from '../packages/did-provider-ethr/src'
import { WebDIDProvider } from '../packages/did-provider-web/src'
import { getResolver as getDidPeerResolver, PeerDIDProvider } from '../packages/did-provider-peer/src'
import { getDidKeyResolver, KeyDIDProvider } from '../packages/did-provider-key/src'
import { getDidPkhResolver, PkhDIDProvider } from '../packages/did-provider-pkh/src'
import { getDidJwkResolver, JwkDIDProvider } from '../packages/did-provider-jwk/src'
import { DIDComm, DIDCommHttpTransport, DIDCommMessageHandler, IDIDComm } from '../packages/did-comm/src'
import {
ISelectiveDisclosure,
SdrMessageHandler,
SelectiveDisclosure,
} from '../packages/selective-disclosure/src'
import { KeyManagementSystem, SecretBox } from '../packages/kms-local/src'
import { Web3KeyManagementSystem } from '../packages/kms-web3/src'
import { DIDDiscovery, IDIDDiscovery } from '../packages/did-discovery/src'
import {
DataStore,
DataStoreDiscoveryProvider,
DataStoreORM,
DIDStore,
Entities,
KeyStore,
migrations,
PrivateKeyStore,
} from '../packages/data-store/src'
import { BrokenDiscoveryProvider, FakeDidProvider, FakeDidResolver } from '../packages/test-utils/src'
import { DataSource } from 'typeorm'
import { createGanacheProvider } from './utils/ganache-provider'
import { createEthersProvider } from './utils/ethers-provider'
import { getResolver as ethrDidResolver } from 'ethr-did-resolver'
import { getResolver as webDidResolver } from 'web-did-resolver'
import { contexts as credential_contexts } from '@transmute/credentials-context'
import * as fs from 'fs'
import { jest } from '@jest/globals'
// Shared tests
import verifiableDataJWT from './shared/verifiableDataJWT'
import verifiableDataLD from './shared/verifiableDataLD'
import verifiableDataEIP712 from './shared/verifiableDataEIP712'
import handleSdrMessage from './shared/handleSdrMessage'
import resolveDid from './shared/resolveDid'
import webDidFlow from './shared/webDidFlow'
import saveClaims from './shared/saveClaims'
import documentationExamples from './shared/documentationExamples'
import keyManager from './shared/keyManager'
import didManager from './shared/didManager'
import didCommPacking from './shared/didCommPacking'
import messageHandler from './shared/messageHandler'
import didDiscovery from './shared/didDiscovery'
import dbInitOptions from './shared/dbInitOptions'
import didCommWithEthrDidFlow from './shared/didCommWithEthrDidFlow'
import utils from './shared/utils'
import web3 from './shared/web3'
import credentialStatus from './shared/credentialStatus'
import ethrDidFlowSigned from './shared/ethrDidFlowSigned'
import didCommWithPeerDidFlow from './shared/didCommWithPeerDidFlow.js'
import credentialPluginTests from './shared/credentialPluginTests.js'
jest.setTimeout(120000)
const infuraProjectId = '3586660d179141e3801c3895de1c2eba'
const secretKey = '29739248cad1bd1a0fc4d9b75cd4d2990de535baf5caadfdf8d8f86664aa830c'
let agent: TAgent<
IDIDManager &
IKeyManager &
IDataStore &
IDataStoreORM &
IResolver &
IMessageHandler &
IDIDComm &
ICredentialPlugin &
ISelectiveDisclosure &
IDIDDiscovery
>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let dbConnection: any // typeorm types don't seem to follow semantic release patterns leading to type errors
let databaseFile: string
const setup = async (options?: IAgentOptions): Promise<boolean> => {
databaseFile = options?.context?.databaseFile || ':memory:'
dbConnection = new DataSource({
name: options?.context?.['dbName'] || 'test',
type: 'sqlite',
database: databaseFile,
synchronize: false,
migrations: migrations,
migrationsRun: true,
logging: false,
entities: Entities,
// allow shared tests to override connection options
...options?.context?.dbConnectionOptions,
}).initialize()
const { provider, registry } = await createGanacheProvider()
const ethersProvider = createEthersProvider()
const eip712 = new CredentialProviderEIP712()
const jwt = new CredentialProviderJWT()
const ld = new CredentialProviderLD({
contextMaps: [LdDefaultContexts, credential_contexts as any],
suites: [
new VeramoEcdsaSecp256k1RecoverySignature2020(),
new VeramoEd25519Signature2018(),
new VeramoJsonWebSignature2020(),
new VeramoEd25519Signature2020(),
],
})
agent = createAgent<
IDIDManager &
IKeyManager &
IDataStore &
IDataStoreORM &
IResolver &
IMessageHandler &
IDIDComm &
ICredentialPlugin &
ISelectiveDisclosure &
IDIDDiscovery
>({
...options,
context: {
// authorizedDID: 'did:example:3456'
},
plugins: [
new KeyManager({
store: new KeyStore(dbConnection),
kms: {
local: new KeyManagementSystem(new PrivateKeyStore(dbConnection, new SecretBox(secretKey))),
web3: new Web3KeyManagementSystem({
ethers: ethersProvider as any, // different versions of ethers complain about a type mismatch here
}),
},
}),
new DIDManager({
store: new DIDStore(dbConnection),
defaultProvider: 'did:ethr:ganache',
providers: {
'did:ethr': new EthrDIDProvider({
defaultKms: 'local',
ttl: 60 * 60 * 24 * 30 * 12 + 1,
networks: [
{
name: 'mainnet',
chainId: 1,
rpcUrl: 'https://mainnet.infura.io/v3/' + infuraProjectId,
},
{
name: 'sepolia',
chainId: 11155111,
rpcUrl: 'https://sepolia.infura.io/v3/' + infuraProjectId,
},
{
chainId: 421613,
name: 'arbitrum:goerli',
rpcUrl: 'https://arbitrum-goerli.infura.io/v3/' + infuraProjectId,
registry: '0x8FFfcD6a85D29E9C33517aaf60b16FE4548f517E',
},
{
chainId: 1337,
name: 'ganache',
provider: provider as any, // different versions of ethers complain about a type mismatch here
registry,
},
],
}),
'did:web': new WebDIDProvider({
defaultKms: 'local',
}),
'did:key': new KeyDIDProvider({
defaultKms: 'local',
}),
'did:peer': new PeerDIDProvider({
defaultKms: 'local',
}),
'did:pkh': new PkhDIDProvider({
defaultKms: 'local',
}),
'did:jwk': new JwkDIDProvider({
defaultKms: 'local',
}),
'did:fake': new FakeDidProvider(),
},
}),
new DIDResolverPlugin({
...ethrDidResolver({
infuraProjectId,
networks: [
{
name: 'ganache',
chainId: 1337,
provider: provider as any,
registry,
},
],
}),
...webDidResolver(),
...getDidKeyResolver(),
...getDidPkhResolver(),
...getDidJwkResolver(),
...getDidPeerResolver(),
...new FakeDidResolver(() => agent).getDidFakeResolver(),
}),
new DataStore(dbConnection),
new DataStoreORM(dbConnection),
new MessageHandler({
messageHandlers: [
new DIDCommMessageHandler(),
new JwtMessageHandler(),
new W3cMessageHandler(),
new SdrMessageHandler(),
],
}),
new DIDComm({ transports: [new DIDCommHttpTransport()] }),
new CredentialPlugin({ issuers: [eip712, jwt, ld] }),
new SelectiveDisclosure(),
new DIDDiscovery({
providers: [
new AliasDiscoveryProvider(),
new DataStoreDiscoveryProvider(),
new BrokenDiscoveryProvider(),
],
}),
...(options?.plugins || []),
],
})
return true
}
const tearDown = async (): Promise<boolean> => {
try {
await (await dbConnection).dropDatabase()
await (await dbConnection).close()
} catch (e) {
// nop
}
try {
fs.unlinkSync(databaseFile)
} catch (e) {
// nop
}
return true
}
const getAgent = () => agent
const testContext = { getAgent, setup, tearDown }
describe('Local integration tests', () => {
verifiableDataJWT(testContext)
verifiableDataLD(testContext)
verifiableDataEIP712(testContext)
handleSdrMessage(testContext)
resolveDid(testContext)
webDidFlow(testContext)
saveClaims(testContext)
documentationExamples(testContext)
keyManager(testContext)
didManager(testContext)
messageHandler(testContext)
didCommPacking(testContext)
didDiscovery(testContext)
dbInitOptions(testContext)
utils(testContext)
web3(testContext)
didCommWithEthrDidFlow(testContext)
didCommWithPeerDidFlow(testContext)
credentialStatus(testContext)
ethrDidFlowSigned(testContext)
credentialPluginTests(testContext)
})