-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
29,049 additions
and
24,787 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import cors, { CorsOptions, CorsOptionsDelegate } from 'cors' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
// Helper method to wait for a middleware to execute before continuing | ||
// And to throw an error when an error happens in a middleware | ||
function initMiddleware(middleware: typeof cors) { | ||
return (req: NextApiRequest, res: NextApiResponse, options?: CorsOptions | CorsOptionsDelegate) => | ||
new Promise((resolve, reject) => { | ||
middleware(options)(req, res, (result: Error | unknown) => { | ||
if (result instanceof Error) { | ||
return reject(result) | ||
} | ||
|
||
return resolve(result) | ||
}) | ||
}) | ||
} | ||
|
||
// You can read more about the available options here: https://github.com/expressjs/cors#configuration-options | ||
const NextCors = initMiddleware(cors) | ||
|
||
export default NextCors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { | ||
ClaimTableEntry, | ||
CredentialTableEntry, | ||
DiffCallback, | ||
PresentationTableEntry, | ||
VeramoJsonCache, | ||
VeramoJsonStore, | ||
} from '@veramo/data-store-json' | ||
import * as fs from 'fs' | ||
import { IIdentifier, IMessage, ManagedKeyInfo } from '@veramo/core-types' | ||
import { ManagedPrivateKey } from '@veramo/key-manager' | ||
|
||
/** | ||
* A utility class that shows how a File based JSON storage system could work. | ||
* This is not recommended for large databases since every write operation rewrites the entire database. | ||
*/ | ||
export class JsonFileStore implements VeramoJsonStore { | ||
notifyUpdate: DiffCallback | ||
dids: Record<string, IIdentifier> | ||
keys: Record<string, ManagedKeyInfo> | ||
privateKeys: Record<string, ManagedPrivateKey> | ||
credentials: Record<string, CredentialTableEntry> | ||
claims: Record<string, ClaimTableEntry> | ||
presentations: Record<string, PresentationTableEntry> | ||
messages: Record<string, IMessage> | ||
private file: fs.PathLike | ||
|
||
private constructor(file: fs.PathLike) { | ||
this.file = file | ||
this.notifyUpdate = async (oldState: VeramoJsonCache, newState: VeramoJsonCache) => { | ||
await this.save(newState) | ||
} | ||
this.dids = {} | ||
this.keys = {} | ||
this.privateKeys = {} | ||
this.credentials = {} | ||
this.claims = {} | ||
this.presentations = {} | ||
this.messages = {} | ||
} | ||
|
||
public static async fromFile(file: fs.PathLike): Promise<JsonFileStore> { | ||
const store = new JsonFileStore(file) | ||
return await store.load() | ||
} | ||
|
||
private async load(): Promise<JsonFileStore> { | ||
|
||
let cache: VeramoJsonCache | ||
if (fs.existsSync(this.file)) { | ||
try { | ||
const rawCache = await fs.promises.readFile(this.file, { encoding: 'utf8' }) | ||
cache = JSON.parse(rawCache) | ||
} catch (e: any) { | ||
console.log(e) | ||
cache = {} | ||
} | ||
} else { | ||
cache = {} | ||
} | ||
; ({ | ||
dids: this.dids, | ||
keys: this.keys, | ||
credentials: this.credentials, | ||
claims: this.claims, | ||
presentations: this.presentations, | ||
messages: this.messages, | ||
privateKeys: this.privateKeys, | ||
} = { | ||
dids: {}, | ||
keys: {}, | ||
credentials: {}, | ||
claims: {}, | ||
presentations: {}, | ||
messages: {}, | ||
privateKeys: {}, | ||
...cache, | ||
}) | ||
return this | ||
} | ||
|
||
private async save(newState: VeramoJsonCache): Promise<void> { | ||
await fs.promises.writeFile(this.file, JSON.stringify(newState), { | ||
encoding: 'utf8', | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { createAgent } from '@veramo/core' | ||
import { DIDManager } from '@veramo/did-manager' | ||
import { KeyManager } from '@veramo/key-manager' | ||
import { KeyManagementSystem, SecretBox } from '@veramo/kms-local' | ||
import { CredentialPlugin } from '@veramo/credential-w3c' | ||
import { EthrDIDProvider } from '@veramo/did-provider-ethr' | ||
import { KeyDIDProvider } from '@veramo/did-provider-key' | ||
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 { DataStoreJson, KeyStoreJson, DIDStoreJson, PrivateKeyStoreJson } from '@veramo/data-store-json' | ||
import fs from 'fs' | ||
import { JsonFileStore } from './store' | ||
import { | ||
IDIDManager, | ||
IKeyManager, | ||
IResolver, | ||
IDataStore, | ||
ICredentialIssuer, | ||
TAgent | ||
} from '@veramo/core-types' | ||
type ConfiguredAgent = TAgent<IDIDManager & IKeyManager & IResolver & IDataStore & ICredentialIssuer> | ||
|
||
const INFURA_PROJECT_ID = process.env.INFURIA_API_KEY | ||
// const INFURA_PROJECT_ID = '62cfe5babc774c1aaffa9eac6dbbf47f' | ||
let KMS_SECRET_KEY = null | ||
const kmsfile = '/data/kms'; | ||
const storefile = '/data/store.json'; | ||
|
||
if (fs.existsSync(kmsfile)) { | ||
KMS_SECRET_KEY = fs.readFileSync(kmsfile, 'utf8') | ||
} else { | ||
KMS_SECRET_KEY = await SecretBox.createSecretKey() | ||
fs.writeFileSync(kmsfile, KMS_SECRET_KEY) | ||
} | ||
|
||
const jsonFileStore = await JsonFileStore.fromFile(storefile) | ||
|
||
export const agent: ConfiguredAgent = createAgent({ | ||
plugins: [ | ||
new KeyManager({ | ||
store: new KeyStoreJson(jsonFileStore), | ||
kms: { | ||
local: new KeyManagementSystem(new PrivateKeyStoreJson(jsonFileStore, new SecretBox(KMS_SECRET_KEY))), | ||
} | ||
}), | ||
new DIDManager({ | ||
store: new DIDStoreJson(jsonFileStore), | ||
defaultProvider: 'did:key', | ||
providers: { | ||
'did:ethr:sepolia': new EthrDIDProvider({ | ||
defaultKms: 'local', | ||
network: 'sepolia', | ||
rpcUrl: 'https://sepolia.infura.io/v3/' + INFURA_PROJECT_ID, | ||
}), | ||
'did:key': new KeyDIDProvider({ | ||
defaultKms: 'local' | ||
}) | ||
}, | ||
}), | ||
new DIDResolverPlugin({ | ||
resolver: new Resolver({ | ||
...ethrDidResolver({ infuraProjectId: INFURA_PROJECT_ID }), | ||
...webDidResolver(), | ||
}) | ||
}), | ||
new DataStoreJson(jsonFileStore), | ||
new CredentialPlugin() | ||
], | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.