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.
* update vc sign * fix spec * support signing with secp256k1 and ed25519 keys * remove unused import * add did ion test --------- Co-authored-by: Moe Jangda <moe@tbd.email>
- Loading branch information
1 parent
60b2ac1
commit 97eb878
Showing
6 changed files
with
517 additions
and
580 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,58 +1,27 @@ | ||
import { Request, Response } from 'express'; | ||
import { VerifiableCredential, SignOptions } from '@web5/credentials'; | ||
import { DidKeyMethod, PortableDid } from '@web5/dids'; | ||
import { Ed25519, PrivateKeyJwk } from '@web5/crypto'; | ||
import { VerifiableCredential } from '@web5/credentials'; | ||
import { DidKeyMethod } from '@web5/dids'; | ||
import { paths } from './openapi.js'; | ||
|
||
type Signer = (data: Uint8Array) => Promise<Uint8Array>; | ||
|
||
let _ownDid: PortableDid; | ||
|
||
async function getOwnDid(): Promise<PortableDid> { | ||
if(_ownDid) { | ||
return _ownDid; | ||
} | ||
_ownDid = await DidKeyMethod.create(); | ||
return _ownDid; | ||
} | ||
|
||
export async function credentialIssue(req: Request, res: Response) { | ||
const body: paths["/credentials/issue"]["post"]["requestBody"]["content"]["application/json"] = | ||
req.body; | ||
|
||
const ownDid = await getOwnDid() | ||
|
||
// build signing options | ||
const [signingKeyPair] = ownDid.keySet.verificationMethodKeys!; | ||
const subjectIssuerDid = body.credential.credentialSubject["id"] as string; | ||
const signer = EdDsaSigner(signingKeyPair.privateKeyJwk as PrivateKeyJwk); | ||
const signOptions: SignOptions = { | ||
issuerDid : ownDid.did, | ||
subjectDid : subjectIssuerDid, | ||
kid : '#' + ownDid.did.split(':')[2], | ||
signer : signer | ||
}; | ||
const ownDid = await DidKeyMethod.create(); | ||
|
||
const vc: VerifiableCredential = VerifiableCredential.create({ | ||
type: body.credential.type[body.credential.type.length - 1], | ||
issuer: body.credential.issuer, | ||
subject: subjectIssuerDid, | ||
subject: body.credential.credentialSubject["id"] as string, | ||
data: body.credential.credentialSubject | ||
}); | ||
|
||
const vcJwt: string = await vc.sign(signOptions); | ||
const vcJwt: string = await vc.sign({did: ownDid}); | ||
|
||
const resp: paths["/credentials/issue"]["post"]["responses"]["200"]["content"]["application/json"] = | ||
{ | ||
verifiableCredential: {data: vcJwt} | ||
}; | ||
|
||
res.json(resp); | ||
} | ||
|
||
function EdDsaSigner(privateKey: PrivateKeyJwk): Signer { | ||
return async (data: Uint8Array): Promise<Uint8Array> => { | ||
const signature = await Ed25519.sign({ data, key: privateKey}); | ||
return signature; | ||
}; | ||
} |
Oops, something went wrong.