-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.test.ts
46 lines (31 loc) · 2 KB
/
auth.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { GeneralJwsSigner, GeneralJwsVerifier } from "../source/dwn-sdk/jose/jws/general";
import sinon from 'sinon';
import { DIDResolver } from "../source/dwn-sdk/did/did-resolver";
import { makeTestJWS, makeMockResolutionResult, makeKeyPair, makeTestVerifiableCredential } from "./helpers";
import didkit from '@spruceid/didkit-wasm-node'
// from /dwn-sdk-jstests/jose/jws/general.spec.ts
describe('General JWS Sign/Verify', () => {
test('should sign and verify secp256k1 signature using a key vector correctly', async () => {
const keyPair = await makeKeyPair();
const jws = await makeTestJWS({hello: "darkness my old friend"}, keyPair, 'did:vc:alice');
const mockResolutionResult = makeMockResolutionResult('did:vc:alice', keyPair.publicJwk);
const resolverStub = sinon.createStubInstance(DIDResolver, {
// @ts-ignore
resolve: sinon.stub().withArgs('did:vc:alice').resolves(mockResolutionResult)
});
const verifier = new GeneralJwsVerifier(jws);
const verificationResult = await verifier.verify(resolverStub);
expect(verificationResult.signers.length).toEqual(1);
expect(verificationResult.signers).toContain('did:vc:alice');
});
test('didkit functions', async () => {
const issuerKeypair = didkit.generateEd25519Key(),
subjectKeypair = didkit.generateEd25519Key();;
const issuerDid = didkit.keyToDID('key', issuerKeypair), subjectDid = didkit.keyToDID('key', subjectKeypair);
expect (issuerDid).toContain('did:key')
expect (subjectDid).toContain('did:key')
const issued_credential = await didkit.issueCredential(JSON.stringify(makeTestVerifiableCredential(issuerDid, subjectDid)), JSON.stringify({challenge: 'come at me bro!'}), issuerKeypair)
const verified_credential = await didkit.verifyCredential(issued_credential, JSON.stringify({challenge: 'come at me bro!'}))
expect(JSON.parse(verified_credential).errors.length).toEqual(0)
});
})