generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 56
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
3 changed files
with
58 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,46 @@ | ||
import type { Web5Agent } from '@web5/agent'; | ||
import type { Web5ManagedAgent } from '@web5/agent'; | ||
|
||
import { VerifiableCredential, VerifiableCredentialTypeV1 } from '@web5/credentials'; | ||
|
||
export class VcApi { | ||
private agent: Web5Agent; | ||
private agent: Web5ManagedAgent; | ||
private connectedDid: string; | ||
|
||
constructor(options: { agent: Web5Agent, connectedDid: string }) { | ||
constructor(options: { agent: Web5ManagedAgent, connectedDid: string }) { | ||
this.agent = options.agent; | ||
this.connectedDid = options.connectedDid; | ||
} | ||
|
||
async create() { | ||
// TODO: implement | ||
throw new Error('Not implemented.'); | ||
getKeyId(did: string): string { | ||
const secondColonIndex = did.indexOf(':', 4); // start search for : from the method portion | ||
const methodSpecificId = did.substring(secondColonIndex + 1); | ||
const keyId = `${did}#${methodSpecificId}`; | ||
return keyId; | ||
} | ||
|
||
getSigner(keyId: string) { | ||
const km = this.agent.keyManager; | ||
|
||
return async function (data: Uint8Array): Promise<Uint8Array> { | ||
const key = await km.getKey({ keyRef: keyId }); | ||
// @ts-ignore | ||
const algorithm = key.algorithm || key.publicKey.algorithm; | ||
const signature = await km.sign({ data, keyRef: keyId, algorithm }); | ||
return signature; | ||
}; | ||
|
||
} | ||
|
||
async create({ verifiableCredential, subjectDid }: { verifiableCredential: VerifiableCredentialTypeV1, subjectDid: string }) { | ||
const keyId = this.getKeyId(this.connectedDid); | ||
|
||
const verifiableCredentialJWT = await VerifiableCredential.create({ | ||
kid : keyId, | ||
issuerDid : this.connectedDid, | ||
subjectDid, | ||
signer : this.getSigner(keyId) | ||
}, undefined, verifiableCredential); | ||
|
||
return verifiableCredentialJWT; | ||
} | ||
} |
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