Skip to content

Commit 2cfe1c1

Browse files
author
adamczykm
committed
Reorganize data types #2
1 parent cbaba55 commit 2cfe1c1

File tree

13 files changed

+70
-533
lines changed

13 files changed

+70
-533
lines changed
Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
import { CircuitString, Field, Poseidon } from 'o1js';
2-
import { z } from 'zod';
3-
import { ClaimStruct, IClaimStruct } from './data/claims.js';
4-
import { FieldsSchema } from './data/simple.js';
5-
import { ClaimSchema_, ClaimStandard } from './data/transit-cred.js';
6-
7-
8-
export interface IClaim<V> {
9-
get name(): string;
10-
get value(): V;
11-
get fieldsValue(): Field[];
12-
get standard(): ClaimStandard;
13-
}
14-
15-
export const claimToStruct = (claim: IClaim<any>): IClaimStruct => {
16-
const l = claim.fieldsValue.length;
17-
return ClaimStruct(l).fromFields(claim.fieldsValue);
18-
};
2+
import { ClaimStandard } from './data/transit/cred';
193

204
export const claimStandardHash = (standard: ClaimStandard): Field => {
215
const idHash = Poseidon.hash(
@@ -26,11 +10,3 @@ export const claimStandardHash = (standard: ClaimStandard): Field => {
2610
]);
2711
return Poseidon.hash([idHash, optsHash]);
2812
};
29-
30-
// ==========================================
31-
// inline tests
32-
33-
const TypeCheckerTestSchema = ClaimSchema_(z.number());
34-
type TypeCheckerTestType = z.infer<typeof TypeCheckerTestSchema>;
35-
const typeCheckerTestValue = undefined as unknown as TypeCheckerTestType;
36-
typeCheckerTestValue satisfies IClaim<number>;
Lines changed: 9 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,15 @@
11
import { CircuitString, Field, Poseidon, PrivateKey } from 'o1js';
22
import * 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';
223
import { arraysAreEqual } from './helpers/utils.js';
23-
import { Issuer } from './issuer.js';
24-
import { VCredStruct, VCredStructUnsigned } from './data/vcred.js';
254
import { 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

2711
const 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

9714
export 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>;

minauth-plugins/minauth-verified-zkdocument-plugin/src/data/claims.ts

Lines changed: 0 additions & 156 deletions
This file was deleted.

0 commit comments

Comments
 (0)