Skip to content

Commit

Permalink
Test for did:dht signed VCs + release (#322)
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <frankhinek@users.noreply.github.com>
Co-authored-by: Frank Hinek <frankhinek@users.noreply.github.com>
  • Loading branch information
phoebe-lew and frankhinek authored Nov 29, 2023
1 parent 7828b69 commit 9c7c866
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 8 deletions.
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web5/api",
"version": "0.8.3",
"version": "0.8.2",
"description": "SDK for accessing the features and capabilities of Web5",
"type": "module",
"main": "./dist/cjs/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/credentials/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web5/credentials",
"version": "0.3.1",
"version": "0.3.2",
"description": "Verifiable Credentials",
"type": "module",
"main": "./dist/cjs/index.js",
Expand Down
169 changes: 168 additions & 1 deletion packages/credentials/tests/verifiable-credential.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect } from 'chai';
import { VerifiableCredential, SignOptions } from '../src/verifiable-credential.js';
import { Ed25519, Jose } from '@web5/crypto';
import { DidKeyMethod } from '@web5/dids';
import { DidDhtMethod, DidKeyMethod, PortableDid } from '@web5/dids';
import sinon from 'sinon';

type Signer = (data: Uint8Array) => Promise<Uint8Array>;

Expand Down Expand Up @@ -180,6 +181,172 @@ describe('Verifiable Credential Tests', () => {

await VerifiableCredential.verify(vcJwt);
});

it('verify does not throw an exception with vaild vc signed by did:dht', async () => {
const mockDocument: PortableDid = {
keySet: {
identityKey: {
privateKeyJwk: {
d : '_8gihSI-m8aOCCM6jHg33d8kxdImPBN4C5_bZIu10XU',
alg : 'EdDSA',
crv : 'Ed25519',
kty : 'OKP',
ext : 'true',
key_ops : [
'sign'
],
x : 'Qm88q6jAN9tfnrLt5V2zAiZs7wD_jnewHp7HIvM3dGo',
kid : '0'
},
publicKeyJwk: {
alg : 'EdDSA',
crv : 'Ed25519',
kty : 'OKP',
ext : 'true',
key_ops : [
'verify'
],
x : 'Qm88q6jAN9tfnrLt5V2zAiZs7wD_jnewHp7HIvM3dGo',
kid : '0'
}
},
verificationMethodKeys: [
{
privateKeyJwk: {
d : '_8gihSI-m8aOCCM6jHg33d8kxdImPBN4C5_bZIu10XU',
alg : 'EdDSA',
crv : 'Ed25519',
kty : 'OKP',
ext : 'true',
key_ops : [
'sign'
],
x : 'Qm88q6jAN9tfnrLt5V2zAiZs7wD_jnewHp7HIvM3dGo',
kid : '0'
},
publicKeyJwk: {
alg : 'EdDSA',
crv : 'Ed25519',
kty : 'OKP',
ext : 'true',
key_ops : [
'verify'
],
x : 'Qm88q6jAN9tfnrLt5V2zAiZs7wD_jnewHp7HIvM3dGo',
kid : '0'
},
relationships: [
'authentication',
'assertionMethod',
'capabilityInvocation',
'capabilityDelegation'
]
}
]

},
did : 'did:dht:ejzu3k7eay57szh6sms6kzpuyeug35ay9688xcy6u5d1fh3zqtiy',
document : {
id : 'did:dht:ejzu3k7eay57szh6sms6kzpuyeug35ay9688xcy6u5d1fh3zqtiy',
verificationMethod : [
{
id : 'did:dht:ejzu3k7eay57szh6sms6kzpuyeug35ay9688xcy6u5d1fh3zqtiy#0',
type : 'JsonWebKey2020',
controller : 'did:dht:ejzu3k7eay57szh6sms6kzpuyeug35ay9688xcy6u5d1fh3zqtiy',
publicKeyJwk : {
crv : 'Ed25519',
kty : 'OKP',
alg : 'EdDSA',
kid : '0',
x : 'Qm88q6jAN9tfnrLt5V2zAiZs7wD_jnewHp7HIvM3dGo'
}
}
],
authentication: [
'#0'
],
assertionMethod: [
'#0'
],
capabilityInvocation: [
'#0'
],
capabilityDelegation: [
'#0'
]
}
};
const didDhtCreateSpy = sinon.stub(DidDhtMethod, 'create').resolves(mockDocument);

const alice = await DidDhtMethod.create({ publish: true });

const [signingKeyPair] = alice.keySet.verificationMethodKeys!;
const privateKey = (await Jose.jwkToKey({ key: signingKeyPair.privateKeyJwk! })).keyMaterial;
signer = EdDsaSigner(privateKey);
signOptions = {
issuerDid : alice.did,
subjectDid : alice.did,
kid : alice.did + '#0',
signer : signer
};

const vc = VerifiableCredential.create({
type : 'StreetCred',
issuer : signOptions.issuerDid,
subject : signOptions.subjectDid,
data : new StreetCredibility('high', true),
});

const dhtDidResolutionSpy = sinon.stub(DidDhtMethod, 'resolve').resolves({
'@context' : 'https://w3id.org/did-resolution/v1',
didDocument : {
id : 'did:dht:ejzu3k7eay57szh6sms6kzpuyeug35ay9688xcy6u5d1fh3zqtiy',
verificationMethod : [
{
id : 'did:dht:ejzu3k7eay57szh6sms6kzpuyeug35ay9688xcy6u5d1fh3zqtiy#0',
type : 'JsonWebKey2020',
controller : 'did:dht:ejzu3k7eay57szh6sms6kzpuyeug35ay9688xcy6u5d1fh3zqtiy',
publicKeyJwk : {
crv : 'Ed25519',
kty : 'OKP',
alg : 'EdDSA',
kid : '0',
x : 'Qm88q6jAN9tfnrLt5V2zAiZs7wD_jnewHp7HIvM3dGo'
}
}
],
authentication: [
'#0'
],
assertionMethod: [
'#0'
],
capabilityInvocation: [
'#0'
],
capabilityDelegation: [
'#0'
]
},
didDocumentMetadata : {},
didResolutionMetadata : {
contentType : 'application/did+ld+json',
did : {
didString : 'did:dht:ejzu3k7eay57szh6sms6kzpuyeug35ay9688xcy6u5d1fh3zqtiy',
methodSpecificId : 'ejzu3k7eay57szh6sms6kzpuyeug35ay9688xcy6u5d1fh3zqtiy',
method : 'dht'
}
}
});

const vcJwt = await vc.sign(signOptions);

await VerifiableCredential.verify(vcJwt);

sinon.assert.calledOnce(didDhtCreateSpy);
sinon.assert.calledOnce(dhtDidResolutionSpy);
sinon.restore();
});
});
});

Expand Down
1 change: 1 addition & 0 deletions packages/identity-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"@types/mocha": "10.0.1",
"@typescript-eslint/eslint-plugin": "6.4.0",
"@typescript-eslint/parser": "6.4.0",
"@web5/api": "0.8.2",
"c8": "8.0.1",
"chai": "4.3.10",
"chai-as-promised": "7.1.1",
Expand Down

0 comments on commit 9c7c866

Please sign in to comment.