Skip to content

Commit 713d7c0

Browse files
committed
Add JSON fixtures
1 parent 06d657c commit 713d7c0

8 files changed

+635
-96
lines changed

packages/sdk/test/ci/wasm/VerifiablePresentationRequestV1.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import _JB from 'json-bigint';
33
import { AttributeKeyString, TransactionHash } from '../../../src/pub/types.ts';
44
import { VerifiablePresentationRequestV1 } from '../../../src/pub/wasm.ts';
55
import { IdentityProviderDID } from '../../../src/pub/web3-id.ts';
6+
import { vraFixture, vraFixtureEncoded } from './fixtures/VerifiablePresentationRequestV1.Anchor.fixture.ts';
7+
import presentationRequestFixture from './fixtures/VerifiablePresentationRequestV1.fixture.ts';
68

79
const JSONBig = _JB({ alwaysParseAsBig: true, useNativeBigInt: true });
810

@@ -27,6 +29,37 @@ describe('VerifiablePresentationRequestV1', () => {
2729
expect(presentationRequest).toEqual(roundtrip);
2830
});
2931

32+
it('should match the JSON fixture representation', () => {
33+
const context = VerifiablePresentationRequestV1.createSimpleContext(
34+
Uint8Array.from([0, 1, 2, 3]),
35+
'0102010201020102010201020102010201020102010201020102010201020102',
36+
'Wine payment'
37+
);
38+
const statement = VerifiablePresentationRequestV1.statementBuilder()
39+
.addIdentityStatement(
40+
[0, 1, 2].map((i) => new IdentityProviderDID('Testnet', i)),
41+
(b) => b.revealAttribute(AttributeKeyString.firstName)
42+
)
43+
.getStatements();
44+
const transactionRef = TransactionHash.fromHexString(
45+
'0102030401020304010203040102030401020304010203040102030401020304'
46+
);
47+
const presentationRequest = VerifiablePresentationRequestV1.create(context, statement, transactionRef);
48+
49+
const json = presentationRequest.toJSON();
50+
const jsonString = JSONBig.stringify(json);
51+
const expectedJsonString = JSONBig.stringify(presentationRequestFixture);
52+
53+
expect(jsonString).toBe(expectedJsonString);
54+
});
55+
56+
it('should deserialize from JSON fixture representation', () => {
57+
const request = VerifiablePresentationRequestV1.fromJSON(presentationRequestFixture);
58+
expect(request.toJSON()).toEqual(presentationRequestFixture);
59+
});
60+
});
61+
62+
describe('VerifiablePresentationRequestV1.Anchor', () => {
3063
it('should compute the correct anchor', () => {
3164
const context = VerifiablePresentationRequestV1.createSimpleContext(
3265
Uint8Array.from([0, 1, 2, 3]),
@@ -55,4 +88,45 @@ describe('VerifiablePresentationRequestV1', () => {
5588
};
5689
expect(roundtrip).toEqual(expectedData);
5790
});
91+
92+
it('should match the fixture anchor representation', () => {
93+
const context = VerifiablePresentationRequestV1.createSimpleContext(
94+
Uint8Array.from([0, 1, 2, 3]),
95+
'0102010201020102010201020102010201020102010201020102010201020102',
96+
'Wine payment'
97+
);
98+
const statement = VerifiablePresentationRequestV1.statementBuilder()
99+
.addIdentityStatement(
100+
[0, 1, 2].map((i) => new IdentityProviderDID('Testnet', i)),
101+
(b) => b.revealAttribute(AttributeKeyString.firstName)
102+
)
103+
.getStatements();
104+
105+
const anchor = VerifiablePresentationRequestV1.createAnchor(context, statement, {
106+
verifier: 'Test Verifier',
107+
purpose: 'Age verification',
108+
});
109+
110+
const anchorData = VerifiablePresentationRequestV1.decodeAnchor(anchor);
111+
const json = {
112+
type: anchorData.type,
113+
version: anchorData.version,
114+
hash: Buffer.from(anchorData.hash).toString('hex'),
115+
public: anchorData.public,
116+
};
117+
const jsonString = JSONBig.stringify(json);
118+
const expectedJsonString = JSONBig.stringify(vraFixture);
119+
120+
expect(jsonString).toBe(expectedJsonString);
121+
});
122+
123+
it('should decode from fixture anchor representation', () => {
124+
const anchor = Buffer.from(vraFixtureEncoded, 'hex');
125+
const decoded = VerifiablePresentationRequestV1.decodeAnchor(anchor);
126+
127+
expect(decoded.type).toBe(vraFixture.type);
128+
expect(decoded.version).toBe(Number(vraFixture.version));
129+
expect(Buffer.from(decoded.hash).toString('hex')).toBe(vraFixture.hash);
130+
expect(decoded.public).toEqual(vraFixture.public);
131+
});
58132
});
Lines changed: 116 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _JB from 'json-bigint';
12
import fs from 'node:fs';
23

34
import { AttributeKeyString, IdentityObjectV1, IdentityProvider, IpInfo } from '../../../src/pub/types.ts';
@@ -9,109 +10,129 @@ import {
910
import { createAccountDID, createIdentityCommitmentInputWithHdWallet } from '../../../src/pub/web3-id.ts';
1011
import { BlockHash } from '../../../src/types/index.ts';
1112
import { TESTNET_GLOBAL_CONTEXT, TEST_SEED_1 } from './constants.ts';
13+
import presentationFixture from './fixtures/VerifiablePresentationV1.fixture.ts';
1214

13-
test('create testnet account-based presentation v1', () => {
14-
const requestContext = VerifiablePresentationRequestV1.createContext({
15-
given: [{ label: 'Nonce', context: Uint8Array.from([0, 1, 2]) }],
16-
requested: ['BlockHash'],
17-
});
18-
const context = VerifiablePresentationV1.createContext(requestContext, [
19-
{ label: 'BlockHash', context: BlockHash.fromHexString('01'.repeat(32)) },
20-
]);
21-
22-
const values: Record<string, string> = {};
23-
values.dob = '0';
24-
values.firstName = 'a';
25-
26-
const statements: VerifiablePresentationV1.Statement[] = [
27-
{
28-
id: createAccountDID(
29-
'Testnet',
30-
'94d3e85bbc8ff0091e562ad8ef6c30d57f29b19f17c98ce155df2a30100df4cac5e161fb81aebe3a04300e63f086d0d8'
31-
),
32-
statement: [
33-
{
34-
attributeTag: AttributeKeyString.dob,
35-
lower: '81',
36-
type: 'AttributeInRange',
37-
upper: '1231',
15+
const JSONBig = _JB({ alwaysParseAsBig: true, useNativeBigInt: true });
16+
17+
describe('VerifiablePresentationV1', () => {
18+
test('create testnet account-based presentation v1', () => {
19+
const requestContext = VerifiablePresentationRequestV1.createContext({
20+
given: [{ label: 'Nonce', context: Uint8Array.from([0, 1, 2]) }],
21+
requested: ['BlockHash'],
22+
});
23+
const context = VerifiablePresentationV1.createContext(requestContext, [
24+
{ label: 'BlockHash', context: BlockHash.fromHexString('01'.repeat(32)) },
25+
]);
26+
27+
const values: Record<string, string> = {};
28+
values.dob = '0';
29+
values.firstName = 'a';
30+
31+
const statements: VerifiablePresentationV1.Statement[] = [
32+
{
33+
id: createAccountDID(
34+
'Testnet',
35+
'94d3e85bbc8ff0091e562ad8ef6c30d57f29b19f17c98ce155df2a30100df4cac5e161fb81aebe3a04300e63f086d0d8'
36+
),
37+
statement: [
38+
{
39+
attributeTag: AttributeKeyString.dob,
40+
lower: '81',
41+
type: 'AttributeInRange',
42+
upper: '1231',
43+
},
44+
{
45+
attributeTag: AttributeKeyString.firstName,
46+
type: 'RevealAttribute',
47+
},
48+
],
49+
},
50+
];
51+
const inputs: VerifiablePresentationV1.CommitmentInput[] = [
52+
{
53+
type: 'account',
54+
issuer: 1,
55+
values,
56+
randomness: {
57+
dob: '575851a4e0558d589a57544a4a9f5ad1bd8467126c1b6767d32f633ea03380e6',
58+
firstName: '575851a4e0558d589a57544a4a9f5ad1bd8467126c1b6767d32f633ea03380e6',
3859
},
39-
{
40-
attributeTag: AttributeKeyString.firstName,
41-
type: 'RevealAttribute',
60+
},
61+
];
62+
63+
const presentation = VerifiablePresentationV1.create(statements, inputs, context, TESTNET_GLOBAL_CONTEXT);
64+
65+
const json = JSON.stringify(presentation);
66+
const roundtrip = VerifiablePresentationV1.fromJSON(JSON.parse(json));
67+
expect(presentation).toEqual(roundtrip);
68+
// TODO: for now we just check that it does not fail - later we need to check the actual values
69+
});
70+
71+
test('create testnet id-based presentation v1', () => {
72+
const requestContext = VerifiablePresentationRequestV1.createContext({
73+
given: [{ label: 'Nonce', context: Uint8Array.from([0, 1, 2]) }],
74+
requested: ['BlockHash'],
75+
});
76+
const context = VerifiablePresentationV1.createContext(requestContext, [
77+
{ label: 'BlockHash', context: BlockHash.fromHexString('01'.repeat(32)) },
78+
]);
79+
80+
const wallet = ConcordiumHdWallet.fromHex(TEST_SEED_1, 'Testnet');
81+
const idObject: IdentityObjectV1 = JSON.parse(
82+
fs.readFileSync('./test/ci/resources/identity-object.json').toString()
83+
).value;
84+
const ipInfo: IpInfo = JSON.parse(fs.readFileSync('./test/ci/resources/ip_info.json').toString()).value;
85+
86+
const inputContext: IdentityProvider = {
87+
ipInfo,
88+
arsInfos: {
89+
[1]: {
90+
arPublicKey: '0102',
91+
arIdentity: 0,
92+
arDescription: { description: 'test', name: 'test', url: 'https://ar.com' },
4293
},
43-
],
44-
},
45-
];
46-
const inputs: VerifiablePresentationV1.CommitmentInput[] = [
47-
{
48-
type: 'account',
49-
issuer: 1,
50-
values,
51-
randomness: {
52-
dob: '575851a4e0558d589a57544a4a9f5ad1bd8467126c1b6767d32f633ea03380e6',
53-
firstName: '575851a4e0558d589a57544a4a9f5ad1bd8467126c1b6767d32f633ea03380e6',
5494
},
55-
},
56-
];
95+
};
96+
const input = createIdentityCommitmentInputWithHdWallet(idObject, inputContext, 0, wallet);
5797

58-
const presentation = VerifiablePresentationV1.create(statements, inputs, context, TESTNET_GLOBAL_CONTEXT);
98+
const statements: VerifiablePresentationV1.Statement[] = [
99+
{
100+
id: 'ccd:testnet:id:0:0',
101+
statement: [
102+
{
103+
attributeTag: AttributeKeyString.dob,
104+
lower: '81',
105+
type: 'AttributeInRange',
106+
upper: '1231',
107+
},
108+
{
109+
attributeTag: AttributeKeyString.firstName,
110+
type: 'RevealAttribute',
111+
},
112+
],
113+
},
114+
];
59115

60-
const json = JSON.stringify(presentation);
61-
const roundtrip = VerifiablePresentationV1.fromJSON(JSON.parse(json));
62-
expect(presentation).toEqual(roundtrip);
63-
// TODO: for now we just check that it does not fail - later we need to check the actual values
64-
});
116+
const presentation = VerifiablePresentationV1.create(statements, [input], context, TESTNET_GLOBAL_CONTEXT);
65117

66-
test('create testnet id-based presentation v1', () => {
67-
const requestContext = VerifiablePresentationRequestV1.createContext({
68-
given: [{ label: 'Nonce', context: Uint8Array.from([0, 1, 2]) }],
69-
requested: ['BlockHash'],
118+
const json = JSON.stringify(presentation);
119+
const roundtrip = VerifiablePresentationV1.fromJSON(JSON.parse(json));
120+
expect(presentation).toEqual(roundtrip);
121+
// TODO: for now we just check that it does not fail - later we need to check the actual values
70122
});
71-
const context = VerifiablePresentationV1.createContext(requestContext, [
72-
{ label: 'BlockHash', context: BlockHash.fromHexString('01'.repeat(32)) },
73-
]);
74-
75-
const wallet = ConcordiumHdWallet.fromHex(TEST_SEED_1, 'Testnet');
76-
const idObject: IdentityObjectV1 = JSON.parse(
77-
fs.readFileSync('./test/ci/resources/identity-object.json').toString()
78-
).value;
79-
const ipInfo: IpInfo = JSON.parse(fs.readFileSync('./test/ci/resources/ip_info.json').toString()).value;
80-
81-
const inputContext: IdentityProvider = {
82-
ipInfo,
83-
arsInfos: {
84-
[1]: {
85-
arPublicKey: '0102',
86-
arIdentity: 0,
87-
arDescription: { description: 'test', name: 'test', url: 'https://ar.com' },
88-
},
89-
},
90-
};
91-
const input = createIdentityCommitmentInputWithHdWallet(idObject, inputContext, 0, wallet);
92-
93-
const statements: VerifiablePresentationV1.Statement[] = [
94-
{
95-
id: 'ccd:testnet:id:0:0',
96-
statement: [
97-
{
98-
attributeTag: AttributeKeyString.dob,
99-
lower: '81',
100-
type: 'AttributeInRange',
101-
upper: '1231',
102-
},
103-
{
104-
attributeTag: AttributeKeyString.firstName,
105-
type: 'RevealAttribute',
106-
},
107-
],
108-
},
109-
];
110123

111-
const presentation = VerifiablePresentationV1.create(statements, [input], context, TESTNET_GLOBAL_CONTEXT);
124+
it('should match the JSON fixture representation', () => {
125+
const presentation = VerifiablePresentationV1.fromJSON(presentationFixture);
126+
127+
const json = presentation.toJSON();
128+
const jsonString = JSONBig.stringify(json);
129+
const expectedJsonString = JSONBig.stringify(presentationFixture);
112130

113-
const json = JSON.stringify(presentation);
114-
const roundtrip = VerifiablePresentationV1.fromJSON(JSON.parse(json));
115-
expect(presentation).toEqual(roundtrip);
116-
// TODO: for now we just check that it does not fail - later we need to check the actual values
131+
expect(jsonString).toBe(expectedJsonString);
132+
});
133+
134+
it('should deserialize from JSON fixture representation', () => {
135+
const presentation = VerifiablePresentationV1.fromJSON(presentationFixture);
136+
expect(presentation.toJSON()).toEqual(presentationFixture);
137+
});
117138
});

0 commit comments

Comments
 (0)