@verii/verifgen 1.0.0-pre.1752076816

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,129 @@
1
+ const mockPrintInfo = jest.fn();
2
+
3
+ const fs = require('fs').promises;
4
+
5
+ jest.mock('../src/common.js', () => {
6
+ const originalModule = jest.requireActual('../src/common.js');
7
+ return {
8
+ ...originalModule,
9
+ printInfo: mockPrintInfo,
10
+ };
11
+ });
12
+
13
+ const currentDir = process.cwd();
14
+
15
+ const createPersona = async (name, body) => {
16
+ await fs.writeFile(`${currentDir}/${name}.prv.key`, body);
17
+ await fs.writeFile(`${currentDir}/${name}.did`, JSON.stringify({}));
18
+ };
19
+
20
+ const deletePersona = async (name) => {
21
+ const path = `${currentDir}/${name}`;
22
+ const existsPrv = await fs
23
+ .access(`${path}.prv.key`)
24
+ .then(() => true)
25
+ .catch(() => false);
26
+ const existsDid = await fs
27
+ .access(`${path}.did`)
28
+ .then(() => true)
29
+ .catch(() => false);
30
+ // eslint-disable-next-line no-unused-expressions
31
+ existsPrv && (await fs.rm(`${path}.prv.key`));
32
+ // eslint-disable-next-line no-unused-expressions
33
+ existsDid && (await fs.rm(`${path}.did`));
34
+ };
35
+
36
+ const { generateKeyPair } = require('@verii/crypto');
37
+ const { jwtVerify, jwkFromSecp256k1Key } = require('@verii/jwt');
38
+ const { getDidUriFromJwk } = require('@verii/did-doc');
39
+ const { generateProof } = require('../src/verifgen-proof/generate-proof');
40
+
41
+ describe('Test proof cli tool', () => {
42
+ let keyPair;
43
+
44
+ beforeEach(async () => {
45
+ await deletePersona('persona1');
46
+ jest.resetAllMocks();
47
+ keyPair = await generateKeyPair();
48
+ await createPersona('persona1', keyPair.privateKey);
49
+ });
50
+
51
+ it('should not generate a proof if persona is missing', async () => {
52
+ expect(() =>
53
+ generateProof({
54
+ challenge: 'abc',
55
+ persona: 'not-exist',
56
+ audience: 'http://test.com',
57
+ })
58
+ ).rejects.toThrow('Persona not-exist DID File not found');
59
+ });
60
+
61
+ it('should generate a proof by a prv.key', async () => {
62
+ JSON.stringify(keyPair.privateKey);
63
+ const proof = await generateProof({
64
+ challenge: 'abc',
65
+ persona: 'persona1',
66
+ audience: 'http://test.com',
67
+ });
68
+ expect(proof).toBeDefined();
69
+ expect(mockPrintInfo).toHaveBeenCalled();
70
+ expect(mockPrintInfo).toHaveBeenLastCalledWith(
71
+ `\nGenerated proof jwt:\n${proof}\n`
72
+ );
73
+ const parsedJwt = await jwtVerify(
74
+ proof,
75
+ jwkFromSecp256k1Key(keyPair.publicKey, false)
76
+ );
77
+ const expectedKid = getDidUriFromJwk(
78
+ jwkFromSecp256k1Key(keyPair.publicKey, false)
79
+ );
80
+ expect(parsedJwt).toStrictEqual({
81
+ header: {
82
+ alg: 'ES256K',
83
+ typ: 'JWT',
84
+ kid: `${expectedKid}#0`,
85
+ },
86
+ payload: {
87
+ aud: 'http://test.com',
88
+ iat: expect.any(Number),
89
+ iss: expectedKid,
90
+ nonce: 'abc',
91
+ },
92
+ });
93
+ });
94
+
95
+ it('should generate a proof by a prv.key.json', async () => {
96
+ const persona2Keypair = await generateKeyPair({ format: 'jwk' });
97
+ const name = 'persona2';
98
+ await fs.writeFile(
99
+ `${currentDir}/${name}.prv.key.json`,
100
+ JSON.stringify(persona2Keypair.privateKey)
101
+ );
102
+ await fs.writeFile(`${currentDir}/${name}.did`, JSON.stringify({}));
103
+ const proof = await generateProof({
104
+ challenge: 'abc',
105
+ persona: 'persona2',
106
+ audience: 'http://test.com',
107
+ });
108
+ expect(proof).toBeDefined();
109
+ expect(mockPrintInfo).toHaveBeenCalled();
110
+ expect(mockPrintInfo).toHaveBeenLastCalledWith(
111
+ `\nGenerated proof jwt:\n${proof}\n`
112
+ );
113
+ const parsedJwt = await jwtVerify(proof, persona2Keypair.publicKey);
114
+ const expectedKid = getDidUriFromJwk(persona2Keypair.publicKey);
115
+ expect(parsedJwt).toStrictEqual({
116
+ header: {
117
+ alg: 'ES256K',
118
+ typ: 'JWT',
119
+ kid: `${expectedKid}#0`,
120
+ },
121
+ payload: {
122
+ aud: 'http://test.com',
123
+ iat: expect.any(Number),
124
+ iss: expectedKid,
125
+ nonce: 'abc',
126
+ },
127
+ });
128
+ });
129
+ });