Skip to content

Commit

Permalink
update vc sign (#332)
Browse files Browse the repository at this point in the history
* update vc sign

* fix spec

* support signing with secp256k1 and ed25519 keys

* remove unused import

* add did ion test

---------

Co-authored-by: Moe Jangda <[email protected]>
  • Loading branch information
2 people authored and finn-tbd committed Mar 19, 2024
1 parent 60b2ac1 commit 97eb878
Show file tree
Hide file tree
Showing 6 changed files with 517 additions and 580 deletions.
41 changes: 5 additions & 36 deletions .web5-spec/credentials.ts
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;
};
}
Loading

0 comments on commit 97eb878

Please sign in to comment.