Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs/node tutorial/keygen clarification #145

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 84 additions & 62 deletions docs/node_tutorials/node_setup_identifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ Install `sqlite`
yarn add sqlite3 typeorm
```

Generate your secret key and save for later in a safe place

```bash
npx @veramo/cli config create-secret-key
```

Add a tsconfig.json to your project

```json
Expand All @@ -74,8 +80,9 @@ Add a tsconfig.json to your project

## Bootstrap Veramo

We bootstrap Veramo by creating a setup file and initializing the agent. Create a setup file in `src/veramo/setup.ts`
and import the following dependencies:
We bootstrap Veramo by creating a setup file and initializing the agent. Create a setup file in `src/veramo/setup.ts` and import the following dependencies:

Note: In this file we'll use that secret key that we generated in an earlier step, so have it handy.

```typescript
// Core interfaces
Expand All @@ -87,85 +94,98 @@ import {
IDataStoreORM,
IKeyManager,
ICredentialPlugin,
} from '@veramo/core'
} from "@veramo/core";

// Core identity manager plugin
import { DIDManager } from '@veramo/did-manager'
import { DIDManager } from "@veramo/did-manager";

// Ethr did identity provider
import { EthrDIDProvider } from '@veramo/did-provider-ethr'
import { EthrDIDProvider } from "@veramo/did-provider-ethr";

// Core key manager plugin
import { KeyManager } from '@veramo/key-manager'
import { KeyManager } from "@veramo/key-manager";

// Custom key management system for RN
import { KeyManagementSystem, SecretBox } from '@veramo/kms-local'
import { KeyManagementSystem, SecretBox } from "@veramo/kms-local";

// W3C Verifiable Credential plugin
import { CredentialPlugin } from '@veramo/credential-w3c'
import { CredentialPlugin } from "@veramo/credential-w3c";

// Custom resolvers
import { DIDResolverPlugin } from '@veramo/did-resolver'
import { Resolver } from 'did-resolver'
import { getResolver as ethrDidResolver } from 'ethr-did-resolver'
import { getResolver as webDidResolver } from 'web-did-resolver'
import { DIDResolverPlugin } from "@veramo/did-resolver";
import { Resolver } from "did-resolver";
import { getResolver as ethrDidResolver } from "ethr-did-resolver";
import { getResolver as webDidResolver } from "web-did-resolver";

// Storage plugin using TypeOrm
import { Entities, KeyStore, DIDStore, PrivateKeyStore, migrations } from '@veramo/data-store'
import {
Entities,
KeyStore,
DIDStore,
PrivateKeyStore,
migrations,
} from "@veramo/data-store";

// TypeORM is installed with `@veramo/data-store`
import { DataSource } from 'typeorm'
import { DataSource } from "typeorm";
```

Create some variables that we will use later

```ts
// This will be the name for the local sqlite database for demo purposes
const DATABASE_FILE = 'database.sqlite'
const DATABASE_FILE = "database.sqlite";

// You will need to get a project ID from infura https://www.infura.io
const INFURA_PROJECT_ID = '<your PROJECT_ID here>'
const INFURA_PROJECT_ID = "<your PROJECT_ID here>";

// This will be the secret key for the KMS
// This will be the secret key for the KMS (replace this with your secret key)
const KMS_SECRET_KEY =
'< you can generate a key by running `npx @veramo/cli config create-secret-key` in a terminal>'
"< you can generate a key by running `npx @veramo/cli config create-secret-key` in a terminal>";
```

Initialise a database using TypeORM

```ts
const dbConnection = new DataSource({
type: 'sqlite',
type: "sqlite",
database: DATABASE_FILE,
synchronize: false,
migrations,
migrationsRun: true,
logging: ['error', 'info', 'warn'],
logging: ["error", "info", "warn"],
entities: Entities,
}).initialize()
}).initialize();
```

Create the agent by using the createAgent method from `@veramo/core`

```ts
export const agent = createAgent<
IDIDManager & IKeyManager & IDataStore & IDataStoreORM & IResolver & ICredentialPlugin
IDIDManager &
IKeyManager &
IDataStore &
IDataStoreORM &
IResolver &
ICredentialPlugin
>({
plugins: [
new KeyManager({
store: new KeyStore(dbConnection),
kms: {
local: new KeyManagementSystem(new PrivateKeyStore(dbConnection, new SecretBox(KMS_SECRET_KEY))),
local: new KeyManagementSystem(
new PrivateKeyStore(dbConnection, new SecretBox(KMS_SECRET_KEY))
),
},
}),
new DIDManager({
store: new DIDStore(dbConnection),
defaultProvider: 'did:ethr:goerli',
defaultProvider: "did:ethr:goerli",
providers: {
'did:ethr:goerli': new EthrDIDProvider({
defaultKms: 'local',
network: 'goerli',
rpcUrl: 'https://goerli.infura.io/v3/' + INFURA_PROJECT_ID,
"did:ethr:goerli": new EthrDIDProvider({
defaultKms: "local",
network: "goerli",
rpcUrl: "https://goerli.infura.io/v3/" + INFURA_PROJECT_ID,
}),
},
}),
Expand All @@ -177,7 +197,7 @@ export const agent = createAgent<
}),
new CredentialPlugin(),
],
})
});
```

> **Note:**
Expand All @@ -202,91 +222,93 @@ Create 4 files `./src/create-identifier.ts`, `./src/list-identifiers.ts`, `./src
Add the following code to `./src/list-identifiers.ts`

```ts
import { agent } from './veramo/setup.js'
import { agent } from "./veramo/setup.js";

async function main() {
const identifiers = await agent.didManagerFind()
const identifiers = await agent.didManagerFind();

console.log(`There are ${identifiers.length} identifiers`)
console.log(`There are ${identifiers.length} identifiers`);

if (identifiers.length > 0) {
identifiers.map((id) => {
console.log(id)
console.log('..................')
})
console.log(id);
console.log("..................");
});
}
}

main().catch(console.log)
main().catch(console.log);
```

Add the following code to `./src/create-identifier.ts`

```ts
import { agent } from './veramo/setup.js'
import { agent } from "./veramo/setup.js";

async function main() {
const identifier = await agent.didManagerCreate({ alias: 'default' })
console.log(`New identifier created`)
console.log(JSON.stringify(identifier, null, 2))
const identifier = await agent.didManagerCreate({ alias: "default" });
console.log(`New identifier created`);
console.log(JSON.stringify(identifier, null, 2));
}

main().catch(console.log)
main().catch(console.log);
```

Add the following code to `./src/create-credential.ts`

```ts
import { agent } from './veramo/setup.js'
import { agent } from "./veramo/setup.js";

async function main() {
const identifier = await agent.didManagerGetByAlias({ alias: 'default' })
const identifier = await agent.didManagerGetByAlias({ alias: "default" });

const verifiableCredential = await agent.createVerifiableCredential({
credential: {
issuer: { id: identifier.did },
credentialSubject: {
id: 'did:web:example.com',
you: 'Rock',
id: "did:web:example.com",
you: "Rock",
},
},
proofFormat: 'jwt',
})
console.log(`New credential created`)
console.log(JSON.stringify(verifiableCredential, null, 2))
proofFormat: "jwt",
});
console.log(`New credential created`);
console.log(JSON.stringify(verifiableCredential, null, 2));
}

main().catch(console.log)
main().catch(console.log);
```

Add the following code to `./src/verify-credential.ts`

```ts
import { agent } from './veramo/setup.js'
import { agent } from "./veramo/setup.js";

async function main() {
const result = await agent.verifyCredential({
credential: {
credentialSubject: {
you: 'Rock',
id: 'did:web:example.com',
you: "Rock",
id: "did:web:example.com",
},
issuer: {
id: 'did:ethr:goerli:0x0350eeeea1410c5b152f1a88e0ffe8bb8a0bc3df868b740eb2352b1dbf93b59c16',
id:
"did:ethr:goerli:0x0350eeeea1410c5b152f1a88e0ffe8bb8a0bc3df868b740eb2352b1dbf93b59c16",
},
type: ['VerifiableCredential'],
'@context': ['https://www.w3.org/2018/credentials/v1'],
issuanceDate: '2022-10-28T11:54:22.000Z',
type: ["VerifiableCredential"],
"@context": ["https://www.w3.org/2018/credentials/v1"],
issuanceDate: "2022-10-28T11:54:22.000Z",
proof: {
type: 'JwtProof2020',
jwt: 'eyJhbGciOiJFUzI1NksiLCJ0eXAiOiJKV1QifQ.eyJ2YyI6eyJAY29udGV4dCI6WyJodHRwczovL3d3dy53My5vcmcvMjAxOC9jcmVkZW50aWFscy92MSJdLCJ0eXBlIjpbIlZlcmlmaWFibGVDcmVkZW50aWFsIl0sImNyZWRlbnRpYWxTdWJqZWN0Ijp7InlvdSI6IlJvY2sifX0sInN1YiI6ImRpZDp3ZWI6ZXhhbXBsZS5jb20iLCJuYmYiOjE2NjY5NTgwNjIsImlzcyI6ImRpZDpldGhyOmdvZXJsaToweDAzNTBlZWVlYTE0MTBjNWIxNTJmMWE4OGUwZmZlOGJiOGEwYmMzZGY4NjhiNzQwZWIyMzUyYjFkYmY5M2I1OWMxNiJ9.EPeuQBpkK13V9wu66SLg7u8ebY2OS8b2Biah2Vw-RI-Atui2rtujQkVc2t9m1Eqm4XQFECfysgQBdWwnSDvIjw',
type: "JwtProof2020",
jwt:
"eyJhbGciOiJFUzI1NksiLCJ0eXAiOiJKV1QifQ.eyJ2YyI6eyJAY29udGV4dCI6WyJodHRwczovL3d3dy53My5vcmcvMjAxOC9jcmVkZW50aWFscy92MSJdLCJ0eXBlIjpbIlZlcmlmaWFibGVDcmVkZW50aWFsIl0sImNyZWRlbnRpYWxTdWJqZWN0Ijp7InlvdSI6IlJvY2sifX0sInN1YiI6ImRpZDp3ZWI6ZXhhbXBsZS5jb20iLCJuYmYiOjE2NjY5NTgwNjIsImlzcyI6ImRpZDpldGhyOmdvZXJsaToweDAzNTBlZWVlYTE0MTBjNWIxNTJmMWE4OGUwZmZlOGJiOGEwYmMzZGY4NjhiNzQwZWIyMzUyYjFkYmY5M2I1OWMxNiJ9.EPeuQBpkK13V9wu66SLg7u8ebY2OS8b2Biah2Vw-RI-Atui2rtujQkVc2t9m1Eqm4XQFECfysgQBdWwnSDvIjw",
},
},
})
console.log(`Credential verified`, result.verified)
});
console.log(`Credential verified`, result.verified);
}

main().catch(console.log)
main().catch(console.log);
```

### List Identifiers
Expand Down