Skip to content

Commit 3572465

Browse files
authored
Merge pull request #8 from veramolabs/nickreynolds/veramo-v6
chore: update finished tutorial with changes required for veramo v6
2 parents 4b00151 + 6317432 commit 3572465

File tree

6 files changed

+2414
-1820
lines changed

6 files changed

+2414
-1820
lines changed

App.tsx

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
// shims
21
import '@sinonjs/text-encoding'
32
import 'react-native-get-random-values'
43
import '@ethersproject/shims'
54
import 'cross-fetch/polyfill'
6-
// filename: App.tsx
75

8-
// ... shims
96

107
import React, { useEffect, useState } from 'react'
118
import { SafeAreaView, ScrollView, View, Text, Button } from 'react-native'
129

1310
// Import the agent from our earlier setup
1411
import { agent } from './setup'
1512
// import some data types:
16-
import { DIDResolutionResult, IIdentifier } from '@veramo/core'
13+
import { IIdentifier } from '@veramo/core'
14+
import { DIDResolutionResult } from '@veramo/core'
15+
import { VerifiableCredential } from '@veramo/core'
16+
import { IVerifyResult } from '@veramo/core'
1717

1818
const App = () => {
1919
const [identifiers, setIdentifiers] = useState<IIdentifier[]>([])
2020
const [resolutionResult, setResolutionResult] = useState<DIDResolutionResult | undefined>()
21-
22-
// Resolve a DID
23-
const resolveDID = async (did: string) => {
24-
const result = await agent.resolveDid({ didUrl: did })
25-
console.log(JSON.stringify(result, null, 2))
26-
setResolutionResult(result)
27-
}
21+
const [credential, setCredential] = useState<VerifiableCredential | undefined>()
22+
const [verificationResult, setVerificationResult] = useState<IVerifyResult | undefined>()
2823

2924
// Add the new identifier to state
3025
const createIdentifier = async () => {
3126
const _id = await agent.didManagerCreate({
32-
provider: 'did:ethr:goerli',
27+
provider: 'did:peer',
28+
options: {
29+
num_algo: 2,
30+
service: { id: '1', type: 'DIDCommMessaging', serviceEndpoint: "did:web:dev-didcomm-mediator.herokuapp.com", description: 'for messaging' }
31+
}
3332
})
3433
setIdentifiers((s) => s.concat([_id]))
3534
}
@@ -47,6 +46,39 @@ const App = () => {
4746
getIdentifiers()
4847
}, [])
4948

49+
// Resolve a DID
50+
const resolveDID = async (did: string) => {
51+
const result = await agent.resolveDid({ didUrl: did })
52+
console.log(JSON.stringify(result, null, 2))
53+
setResolutionResult(result)
54+
}
55+
56+
const createCredential = async () => {
57+
if (identifiers[0].did) {
58+
const verifiableCredential = await agent.createVerifiableCredential({
59+
credential: {
60+
issuer: { id: identifiers[0].did },
61+
issuanceDate: new Date().toISOString(),
62+
credentialSubject: {
63+
id: 'did:web:community.veramo.io',
64+
you: 'Rock',
65+
},
66+
},
67+
save: false,
68+
proofFormat: 'jwt',
69+
})
70+
71+
setCredential(verifiableCredential)
72+
}
73+
}
74+
75+
const verifyCredential = async () => {
76+
if (credential) {
77+
const result = await agent.verifyCredential({ credential })
78+
setVerificationResult(result)
79+
}
80+
}
81+
5082
return (
5183
<SafeAreaView>
5284
<ScrollView>
@@ -56,7 +88,7 @@ const App = () => {
5688
<View style={{ marginBottom: 50, marginTop: 20 }}>
5789
{identifiers && identifiers.length > 0 ? (
5890
identifiers.map((id: IIdentifier) => (
59-
<Button onPress={() => resolveDID(id.did)} title={id.did} key={id.did} />
91+
<Button key={id.did} onPress={() => resolveDID(id.did)} title={id.did} />
6092
))
6193
) : (
6294
<Text>No identifiers created yet</Text>
@@ -71,6 +103,18 @@ const App = () => {
71103
)}
72104
</View>
73105
</View>
106+
<View style={{ padding: 20 }}>
107+
<Button
108+
title={'Create Credential'}
109+
disabled={!identifiers || identifiers.length === 0}
110+
onPress={() => createCredential()}
111+
/>
112+
<Text style={{ fontSize: 10 }}>{JSON.stringify(credential, null, 2)}</Text>
113+
</View>
114+
<View style={{ padding: 20 }}>
115+
<Button title={'Verify Credential'} onPress={() => verifyCredential()} disabled={!credential} />
116+
<Text style={{ fontSize: 10 }}>{JSON.stringify(verificationResult, null, 2)}</Text>
117+
</View>
74118
</ScrollView>
75119
</SafeAreaView>
76120
)

babel.config.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
module.exports = function (api) {
2-
api.cache(true)
2+
api.cache(true);
33
return {
4-
presets: ['babel-preset-expo'],
4+
presets: [['babel-preset-expo', { lazyImports: true }]],
55
plugins: [
6+
'@babel/transform-react-jsx-source',
7+
[`@babel/plugin-transform-private-methods`, { loose: true }],
8+
'babel-plugin-transform-typescript-metadata',
9+
[
10+
'module-resolver',
11+
{
12+
alias: {
13+
'~': './src',
14+
},
15+
},
16+
],
617
'@babel/plugin-syntax-import-assertions',
718
[
819
'babel-plugin-rewrite-require',
@@ -13,6 +24,7 @@ module.exports = function (api) {
1324
},
1425
},
1526
],
27+
// 'react-native-reanimated/plugin',
1628
],
17-
}
18-
}
29+
};
30+
};

metro.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ const { getDefaultConfig } = require('expo/metro-config')
33
const config = getDefaultConfig(__dirname);
44

55
config.resolver.sourceExts.push('cjs');
6+
config.resolver.unstable_enablePackageExports = true;
67

78
module.exports = config;

package.json

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,42 @@
11
{
2-
"name": "veramo-react-native-tutorial",
2+
"name": "veramomobile",
33
"version": "1.0.0",
44
"main": "node_modules/expo/AppEntry.js",
55
"scripts": {
6-
"start": "expo start",
6+
"start": "expo start --offline",
77
"android": "expo start --android",
88
"ios": "expo start --ios",
99
"web": "expo start --web"
1010
},
1111
"dependencies": {
12-
"@babel/plugin-syntax-import-assertions": "^7.20.0",
1312
"@ethersproject/shims": "^5.7.0",
1413
"@sinonjs/text-encoding": "^0.7.2",
15-
"@veramo/core": "^5.1.2",
16-
"@veramo/data-store": "^5.1.2",
17-
"@veramo/did-manager": "^5.1.2",
18-
"@veramo/did-provider-ethr": "^5.1.2",
19-
"@veramo/did-resolver": "^5.1.2",
20-
"@veramo/key-manager": "^5.1.2",
21-
"@veramo/kms-local": "^5.1.2",
22-
"cross-fetch": "^3.1.5",
14+
"@veramo/core": "^6.0.0",
15+
"@veramo/credential-w3c": "^6.0.0",
16+
"@veramo/data-store": "^6.0.0",
17+
"@veramo/did-manager": "^6.0.0",
18+
"@veramo/did-provider-peer": "^6.0.0",
19+
"@veramo/did-resolver": "^6.0.0",
20+
"@veramo/key-manager": "^6.0.0",
21+
"@veramo/kms-local": "^6.0.0",
22+
"cross-fetch": "^4.0.0",
2323
"crypto-browserify": "^3.12.0",
24-
"ethr-did-resolver": "^8.0.0",
25-
"expo": "^48.0.19",
26-
"expo-sqlite": "^11.1.1",
27-
"expo-status-bar": "^1.4.4",
28-
"react": "^18.2.0",
29-
"react-dom": "^18.2.0",
30-
"react-native": "^0.71.8",
31-
"react-native-get-random-values": "^1.8.0",
32-
"react-native-web": "~0.18.9",
24+
"expo": "~49.0.6",
25+
"expo-sqlite": "^11.3.3",
26+
"expo-status-bar": "~1.6.0",
27+
"react": "18.2.0",
28+
"react-native": "0.72.3",
29+
"react-native-get-random-values": "^1.11.0",
3330
"stream-browserify": "^3.0.0",
34-
"web-did-resolver": "^2.0.21"
31+
"web-did-resolver": "^2.0.27"
3532
},
3633
"devDependencies": {
37-
"@babel/core": "^7.12.9",
34+
"@babel/core": "^7.20.0",
35+
"@babel/plugin-syntax-import-assertions": "^7.24.1",
3836
"@types/react": "~18.0.14",
39-
"@types/react-native": "~0.70.6",
4037
"babel-plugin-rewrite-require": "^1.14.5",
41-
"typescript": "^4.6.3"
38+
"babel-plugin-transform-typescript-metadata": "^0.3.2",
39+
"typescript": "^5.1.3"
4240
},
43-
"private": true,
44-
"engines": {
45-
"node": ">=20"
46-
}
41+
"private": true
4742
}

setup.ts

Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
// filename: setup.ts
2-
31
// imports:
42
// Core interfaces
5-
import { createAgent, IDataStore, IDataStoreORM, IDIDManager, IKeyManager, IResolver } from '@veramo/core'
3+
import { createAgent, IDataStore, IDataStoreORM, IDIDManager, IKeyManager } from '@veramo/core'
64

75
// Core identity manager plugin. This allows you to create and manage DIDs by orchestrating different DID provider packages.
86
// This implements `IDIDManager`
@@ -12,85 +10,73 @@ import { DIDManager } from '@veramo/did-manager'
1210
// This implements `IKeyManager`
1311
import { KeyManager } from '@veramo/key-manager'
1412

15-
// This plugin allows us to create and manage `did:ethr` DIDs. (used by DIDManager)
16-
import { EthrDIDProvider } from '@veramo/did-provider-ethr'
13+
// This plugin allows us to create and manage `did:peer` DIDs (used by DIDManager)
14+
import { PeerDIDProvider } from '@veramo/did-provider-peer'
1715

1816
// A key management system that uses a local database to store keys (used by KeyManager)
1917
import { KeyManagementSystem, SecretBox } from '@veramo/kms-local'
2018

2119
// Storage plugin using TypeORM to link to a database
2220
import { Entities, KeyStore, DIDStore, migrations, PrivateKeyStore } from '@veramo/data-store'
2321

22+
// TypeORM is installed with '@veramo/data-store'
23+
import { DataSource } from 'typeorm'
24+
25+
// Core interfaces
26+
import { IResolver } from '@veramo/core'
27+
2428
// Core DID resolver plugin. This plugin orchestrates different DID resolver drivers to resolve the corresponding DID Documents for the given DIDs.
2529
// This plugin implements `IResolver`
2630
import { DIDResolverPlugin } from '@veramo/did-resolver'
2731

28-
// the did:ethr resolver package
29-
import { getResolver as ethrDidResolver } from 'ethr-did-resolver'
32+
// the did:peer resolver package
33+
import { getResolver as peerDidResolver } from '@veramo/did-provider-peer'
3034
// the did:web resolver package
3135
import { getResolver as webDidResolver } from 'web-did-resolver'
3236

33-
// TypeORM is installed with '@veramo/data-store'
34-
import { DataSource } from 'typeorm'
35-
36-
// filename: setup.ts
37-
38-
// ... imports
37+
// This plugin allows us to issue and verify W3C Verifiable Credentials with JWT proof format
38+
import { CredentialPlugin, ICredentialIssuer, ICredentialVerifier } from '@veramo/credential-w3c'
3939

4040
// CONSTANTS
41-
// You will need to get a project ID from infura https://www.infura.io
42-
const INFURA_PROJECT_ID = '3586660d179141e3801c3895de1c2eba'
4341

4442
// This is a raw X25519 private key, provided as an example.
4543
// You can run `npx @veramo/cli config create-secret-key` in a terminal to generate a new key.
4644
// In a production app, this MUST NOT be hardcoded in your source code.
4745
const DB_ENCRYPTION_KEY = '29739248cad1bd1a0fc4d9b75cd4d2990de535baf5caadfdf8d8f86664aa830c'
4846

49-
// filename: setup.ts
50-
51-
// ... imports & CONSTANTS
52-
5347
// DB setup:
5448
let dbConnection = new DataSource({
55-
type: 'expo',
56-
driver: require('expo-sqlite'),
57-
database: 'veramo.sqlite',
58-
migrations: migrations,
59-
migrationsRun: true,
60-
logging: ['error', 'info', 'warn'],
61-
entities: Entities,
62-
}).initialize()
63-
64-
// filename: src/veramo/setup.ts
65-
66-
// ... imports & CONSTANTS & DB setup
67-
68-
// Veramo agent setup
69-
export const agent = createAgent<IDIDManager & IKeyManager & IDataStore & IDataStoreORM>({
70-
plugins: [
71-
new KeyManager({
72-
store: new KeyStore(dbConnection),
73-
kms: {
74-
local: new KeyManagementSystem(new PrivateKeyStore(dbConnection, new SecretBox(DB_ENCRYPTION_KEY))),
75-
},
76-
}),
77-
new DIDManager({
78-
store: new DIDStore(dbConnection),
79-
defaultProvider: 'did:ethr:goerli',
80-
providers: {
81-
'did:ethr:goerli': new EthrDIDProvider({
82-
defaultKms: 'local',
83-
network: 'goerli',
84-
name: 'goerli',
85-
rpcUrl: 'https://goerli.infura.io/v3/' + INFURA_PROJECT_ID,
86-
gas: 1000001,
87-
ttl: 31104001,
88-
}),
89-
},
90-
}),
91-
new DIDResolverPlugin({
92-
...ethrDidResolver({ infuraProjectId: INFURA_PROJECT_ID }), // and set it up to support `did:ethr`
49+
type: 'expo',
50+
driver: require('expo-sqlite'),
51+
database: 'veramo.sqlite',
52+
migrations: migrations,
53+
migrationsRun: true,
54+
logging: ['error', 'info', 'warn'],
55+
entities: Entities,
56+
}).initialize()
57+
58+
// Veramo agent setup
59+
export const agent = createAgent<IDIDManager & IKeyManager & IDataStore & IDataStoreORM & IResolver & ICredentialIssuer & ICredentialVerifier>({
60+
plugins: [
61+
new KeyManager({
62+
store: new KeyStore(dbConnection),
63+
kms: {
64+
local: new KeyManagementSystem(new PrivateKeyStore(dbConnection, new SecretBox(DB_ENCRYPTION_KEY))),
65+
},
66+
}),
67+
new DIDManager({
68+
store: new DIDStore(dbConnection),
69+
defaultProvider: 'did:peer',
70+
providers: {
71+
'did:peer': new PeerDIDProvider({
72+
defaultKms: 'local'
73+
}),
74+
},
75+
}),
76+
new DIDResolverPlugin({
77+
...peerDidResolver(), // and set it up to support `did:peer`
9378
...webDidResolver(), // and `did:web`
9479
}),
95-
],
96-
})
80+
new CredentialPlugin(),
81+
],
82+
})

0 commit comments

Comments
 (0)