1+ import _JB from 'json-bigint' ;
12import fs from 'node:fs' ;
23
34import { AttributeKeyString , IdentityObjectV1 , IdentityProvider , IpInfo } from '../../../src/pub/types.ts' ;
@@ -9,109 +10,129 @@ import {
910import { createAccountDID , createIdentityCommitmentInputWithHdWallet } from '../../../src/pub/web3-id.ts' ;
1011import { BlockHash } from '../../../src/types/index.ts' ;
1112import { 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