11import { CircuitString , Field , Poseidon , PrivateKey } from 'o1js' ;
22import * as o1js from 'o1js' ;
3- import { z } from 'zod' ;
4- import {
5- ClaimSchema ,
6- ClaimStandardSchema ,
7- IClaim ,
8- claimStandardHash
9- } from './claim.js' ;
10- import {
11- CredSubjectId ,
12- CredSubjectIdSchema ,
13- IssuerIdSchema ,
14- VCredIdSchema
15- } from './data/ids.js' ;
16- import {
17- FieldSchema ,
18- SignatureB58 ,
19- SignatureSchema ,
20- UnixTimestampSchema
21- } from './data/simple.js' ;
223import { arraysAreEqual } from './helpers/utils.js' ;
23- import { Issuer } from './issuer.js' ;
24- import { VCredStruct , VCredStructUnsigned } from './data/vcred.js' ;
254import { Logger } from 'tslog' ;
5+ import { CredSubjectId , Credential , CredentialData , CredentialSchema , CredentialStandard } from './data/transit/cred.js' ;
6+ import { claimStandardHash } from './claim.js' ;
7+ import { mkVCredStruct } from './data/o1js/vcred.js' ;
8+ import { SignatureSchema } from './data/transit/common.js' ;
9+ import { Issuer } from './issuer.js' ;
2610
2711const log = new Logger ( { name : 'credential.ts' } ) ;
2812
29- // =============== Credential Trasit Types ==================
30-
31- // ---------------- Credential Standard ----------------
32-
33- export const CredentialStandardSchema = z . object ( {
34- standardId : z . string ( ) . min ( 2 ) ,
35- description : z . string ( ) . min ( 2 ) ,
36- schema : z
37- . record ( z . string ( ) . min ( 2 ) , ClaimStandardSchema )
38- . refine ( ( schema ) => Object . keys ( schema ) . length > 0 , {
39- message : 'Must define at least one claim.'
40- } )
41- } ) ;
42-
43- export type CredentialStandard = z . infer < typeof CredentialStandardSchema > ;
44-
45- // ---------------- Credential Data ----------------
46-
47- export const CredentialDataSchema = z . object ( {
48- claims : z
49- . record ( z . string ( ) . min ( 1 ) , ClaimSchema ( z . unknown ( ) ) )
50- . refine ( ( claims ) => Object . keys ( claims ) . length > 0 , {
51- message : 'Must define at least one claim.'
52- } ) ,
53- id : VCredIdSchema ,
54- issuer : IssuerIdSchema ,
55- issuanceDate : UnixTimestampSchema ,
56- expirationDate : UnixTimestampSchema ,
57- subject : CredSubjectIdSchema ,
58- credentialStandardHash : FieldSchema
59- } ) ;
60-
61- export type CredentialData = z . infer < typeof CredentialDataSchema > ;
62-
63- // ---------------- Credential ----------------
64-
65- export const CredentialSchema = CredentialDataSchema . extend ( {
66- signature : SignatureSchema
67- } ) ;
68-
69- export type Credential = z . infer < typeof CredentialSchema > ;
70-
71- // =============== To fields-encoded types ===============
72-
73- export const mkStructCredentialUnsigned = ( credential : CredentialData ) => {
74- log . debug ( 'Entering mkStructCredentialUnsigned...' , credential ) ;
75- return VCredStructUnsigned (
76- Object . values ( credential . claims ) . map ( ( claim ) => claim . fieldsValue . length )
77- ) . schema . parse ( {
78- ...credential ,
79- claims : Object . values ( credential . claims ) . map ( ( claim ) => ( {
80- claimValue : claim . fieldsValue
81- } ) )
82- } ) ;
83- } ;
84-
85- // signed version
86- export const mkStructCredential = ( credential : Credential ) => {
87- return VCredStruct (
88- Object . values ( credential . claims ) . map ( ( claim ) => claim . fieldsValue . length )
89- ) . schema . parse ( {
90- ...credential ,
91- claims : Object . values ( credential . claims ) . map ( ( claim ) => ( {
92- claimValue : claim . fieldsValue
93- } ) )
94- } ) ;
95- } ;
9613
9714export const checkCredentialStandardConformance = (
9815 credential : Credential ,
@@ -139,10 +56,10 @@ export const verifyAndIssueCredential =
13956 log . debug ( 'Entering verifyAndIssueCredential...' ) ;
14057
14158 log . debug ( 'Converting the credential data to o1js fields.' ) ;
142- const flds = mkStructCredentialUnsigned ( credentialData ) . toFields ( ) ;
59+ const flds = mkVCredStruct ( credentialData ) . toFields ( ) ;
14360
14461 log . debug ( 'Creating signature for the fields data' ) ;
145- const signature : SignatureB58 = SignatureSchema . parse (
62+ const signature = SignatureSchema . parse (
14663 o1js . Signature . create ( privateKey , flds ) . toBase58 ( )
14764 ) ;
14865
@@ -163,24 +80,16 @@ export const verifyAndIssueCredential =
16380
16481 // additional assertions
16582 // issuer pubkey matches the private key
166- if ( ! issuer . pubkey . equals ( privateKey . toPublicKey ( ) ) ) {
83+ if ( issuer . pubkey !== privateKey . toPublicKey ( ) . toBase58 ( ) ) {
16784 throw new Error ( 'Issuer pubkey does not match the private key' ) ;
16885 }
16986
17087 // credential subject public key matches the given subject public key
171- if ( ! credential . subject . pubkey . equals ( subject . pubkey ) ) {
88+ if ( credential . subject . pubkey !== subject . pubkey ) {
17289 throw new Error ( 'Subject does not match the credential subject' ) ;
17390 }
17491
17592 return {
17693 credential
17794 } ;
17895 } ;
179-
180- // ==========================================
181- // inline tests
182-
183- const TypeCheckerTestSchema = ClaimSchema ( z . number ( ) ) ;
184- type TypeCheckerTestType = z . infer < typeof TypeCheckerTestSchema > ;
185- const typeCheckerTestValue = undefined as unknown as TypeCheckerTestType ;
186- typeCheckerTestValue satisfies IClaim < number > ;
0 commit comments