Skip to content

Commit

Permalink
make example check one of several nations
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Oct 25, 2024
1 parent 8e11ed4 commit c532729
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions examples/unique-hash.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ import {
} from '../src/index.ts';
import { issuerKey, owner, ownerKey } from '../tests/test-utils.ts';
import { validateCredential } from '../src/credential-index.ts';
import { array } from '../src/o1js-missing.ts';

// example schema of the credential, which has enough entropy to be hashed into a unique id
const Bytes32 = Bytes(32);
const Bytes16 = Bytes(16); // 16 bytes = 128 bits = enough entropy

const Data = {
nationality: Bytes32,
id: Bytes16,
};
const Data = { nationality: Bytes32, id: Bytes16 };

// ---------------------------------------------
// ISSUER: issue a signed credential to the owner

let data = {
nationality: Bytes32.fromString('United States of America'),
id: Bytes16.random(),
Expand All @@ -33,6 +32,7 @@ console.log('✅ ISSUER: issued credential:', credentialJson);

// ---------------------------------------------
// WALLET: deserialize, validate and store the credential

let storedCredential = Credential.fromJSON(credentialJson);

await validateCredential(storedCredential);
Expand All @@ -41,18 +41,19 @@ console.log('✅ WALLET: imported and validated credential');

// ---------------------------------------------
// VERIFIER: request a presentation

const spec = Spec(
{
signedData: Credential.Simple(Data), // schema needed here!
targetNationality: Claim(Bytes32),
targetNationalities: Claim(array(Bytes32, 3)), // TODO would make more sense as dynamic array
appId: Claim(Bytes32),
},
({ signedData, targetNationality, appId }) => ({
({ signedData, targetNationalities, appId }) => ({
// we assert that the owner has the target nationality
// TODO: add a one-of-many operation to make this more interesting
assert: Operation.equals(
assert: Operation.equalsOneOf(
Operation.property(signedData, 'nationality'),
targetNationality
targetNationalities
),
// we expose a unique hash of the credential data, as nullifier
data: Operation.record({
Expand All @@ -61,15 +62,19 @@ const spec = Spec(
})
);

const targetNationalities = ['United States of America', 'Canada', 'Mexico'];

let request = PresentationRequest.noContext(spec, {
targetNationality: Bytes32.fromString('United States of America'),
appId: Bytes32.fromString('my-app-id'),
targetNationalities: targetNationalities.map((s) => Bytes32.fromString(s)),
appId: Bytes32.fromString('my-app-id:123'),
});
let requestJson = PresentationRequest.toJSON(request);

console.log('✅ VERIFIER: created presentation request:', requestJson);

// ---------------------------------------------
// WALLET: deserialize request and create presentation

console.time('compile');
let deserialized = PresentationRequest.fromJSON<typeof request>(requestJson);
let compiled = await Presentation.compile(deserialized);
Expand All @@ -82,10 +87,12 @@ let presentation = await Presentation.create(ownerKey, {
});
console.timeEnd('create');
// TODO: to send the presentation back we need to serialize it as well

console.log('✅ WALLET: created presentation:', presentation);

// ---------------------------------------------
// VERIFIER: verify the presentation, and check that the nullifier was not used yet

let existingNullifiers = new Set([0x13c43f30n, 0x370f3473n, 0xe1fe0cdan]);

// TODO: claims and other I/O values should be plain JS types
Expand Down

0 comments on commit c532729

Please sign in to comment.