Skip to content

Commit 9f043c4

Browse files
committed
feat(docs): add test for presentation request
1 parent ce61607 commit 9f043c4

File tree

2 files changed

+186
-30
lines changed

2 files changed

+186
-30
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
export const sampleCredential = {
2+
version: "v0",
3+
witness: {
4+
type: "simple",
5+
issuer: {
6+
_type: "PublicKey",
7+
value: "B62qqMxueXzenrchT5CKC5eCSmfcbHic9wJd9GEdHVcd9uCWrjPJjHS",
8+
},
9+
issuerSignature: {
10+
_type: "Signature",
11+
value: {
12+
r: "27355434072539307953235904941558417174103383443074165997458891331674091021280",
13+
s: "22156398191479529717864137276005168653180340733374387165875910835098679659803",
14+
},
15+
},
16+
},
17+
credential: {
18+
owner: {
19+
_type: "PublicKey",
20+
value: "B62qqCMx9YvvjhMFVcRXBqHtAbjWWUhyA9HmgpYCehLHTGKgXsxiZpz",
21+
},
22+
data: {
23+
age: {
24+
_type: "Field",
25+
value: "25",
26+
},
27+
},
28+
},
29+
};
30+
31+
export const samplePresentationRequest = {
32+
type: "zk-app",
33+
spec: {
34+
inputs: {
35+
data: {
36+
type: "credential",
37+
credentialType: "simple",
38+
witness: {
39+
type: { type: "Constant", value: "simple" },
40+
issuer: { _type: "PublicKey" },
41+
issuerSignature: { _type: "Signature" },
42+
},
43+
data: {
44+
person: {
45+
age: { _type: "Field" },
46+
name: { _type: "Bytes", size: 32 },
47+
},
48+
points: { _type: "Field" },
49+
},
50+
},
51+
targetAge: { type: "claim", data: { _type: "Field" } },
52+
targetPoints: { type: "claim", data: { _type: "Field" } },
53+
},
54+
logic: {
55+
assert: {
56+
type: "and",
57+
inputs: [
58+
{
59+
type: "equals",
60+
left: {
61+
type: "property",
62+
key: "age",
63+
inner: {
64+
type: "property",
65+
key: "person",
66+
inner: {
67+
type: "property",
68+
key: "data",
69+
inner: {
70+
type: "property",
71+
key: "data",
72+
inner: { type: "root" },
73+
},
74+
},
75+
},
76+
},
77+
right: {
78+
type: "property",
79+
key: "targetAge",
80+
inner: { type: "root" },
81+
},
82+
},
83+
{
84+
type: "equals",
85+
left: {
86+
type: "property",
87+
key: "points",
88+
inner: {
89+
type: "property",
90+
key: "data",
91+
inner: {
92+
type: "property",
93+
key: "data",
94+
inner: { type: "root" },
95+
},
96+
},
97+
},
98+
right: {
99+
type: "property",
100+
key: "targetPoints",
101+
inner: { type: "root" },
102+
},
103+
},
104+
],
105+
},
106+
outputClaim: {
107+
type: "property",
108+
key: "name",
109+
inner: {
110+
type: "property",
111+
key: "person",
112+
inner: {
113+
type: "property",
114+
key: "data",
115+
inner: { type: "property", key: "data", inner: { type: "root" } },
116+
},
117+
},
118+
},
119+
},
120+
},
121+
claims: {
122+
targetAge: { _type: "Field", value: "25" },
123+
targetPoints: { _type: "Field", value: "100" },
124+
},
125+
inputContext: {
126+
type: "zk-app",
127+
serverNonce: {
128+
_type: "Field",
129+
value:
130+
"13282950667393837968514931367603124110006503770513488711847457500412027340795",
131+
},
132+
action: { _type: "Field", value: "123" },
133+
},
134+
};

apps/docs/src/components/test-zkapp.tsx

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,10 @@ import { createStore } from "@mina-js/connect";
22
import { useLocalStorage, useObjectState } from "@uidotdev/usehooks";
33
import { clsx } from "clsx";
44
import { useState, useSyncExternalStore } from "react";
5+
import { sampleCredential, samplePresentationRequest } from "./sample-data";
56

67
const store = createStore();
78

8-
const sampleCredential = {
9-
version: "v0",
10-
witness: {
11-
type: "simple",
12-
issuer: {
13-
_type: "PublicKey",
14-
value: "B62qqMxueXzenrchT5CKC5eCSmfcbHic9wJd9GEdHVcd9uCWrjPJjHS",
15-
},
16-
issuerSignature: {
17-
_type: "Signature",
18-
value: {
19-
r: "27355434072539307953235904941558417174103383443074165997458891331674091021280",
20-
s: "22156398191479529717864137276005168653180340733374387165875910835098679659803",
21-
},
22-
},
23-
},
24-
credential: {
25-
owner: {
26-
_type: "PublicKey",
27-
value: "B62qqCMx9YvvjhMFVcRXBqHtAbjWWUhyA9HmgpYCehLHTGKgXsxiZpz",
28-
},
29-
data: {
30-
age: {
31-
_type: "Field",
32-
value: "25",
33-
},
34-
},
35-
},
36-
};
37-
389
export const TestZkApp = () => {
3910
const [currentProvider, setCurrentProvider] = useLocalStorage(
4011
"minajs:provider",
@@ -45,6 +16,9 @@ export const TestZkApp = () => {
4516
const [credentialInput, setCredentialInput] = useState(
4617
JSON.stringify(sampleCredential, null, 2),
4718
);
19+
const [presentationRequest, setPresentationRequest] = useState(
20+
JSON.stringify(samplePresentationRequest, null, 2),
21+
);
4822
const [transactionBody, setTransactionBody] = useObjectState({
4923
to: "B62qnVUL6A53E4ZaGd3qbTr6RCtEZYTu3kTijVrrquNpPo4d3MuJ3nb",
5024
amount: "3000000000",
@@ -61,6 +35,7 @@ export const TestZkApp = () => {
6135
mina_signTransaction: "",
6236
mina_switchChain: "",
6337
mina_storePrivateCredential: "",
38+
mina_requestPresentation: "",
6439
});
6540
const providers = useSyncExternalStore(store.subscribe, store.getProviders);
6641
const provider = providers.find(
@@ -85,6 +60,24 @@ export const TestZkApp = () => {
8560
}
8661
};
8762

63+
const requestPresentation = async () => {
64+
if (!provider) return;
65+
try {
66+
const parsedRequest = JSON.parse(presentationRequest);
67+
const { result } = await provider.request({
68+
method: "mina_requestPresentation",
69+
params: [parsedRequest],
70+
});
71+
setResults(() => ({
72+
mina_requestPresentation: JSON.stringify(result, null, 2),
73+
}));
74+
} catch (error) {
75+
setResults(() => ({
76+
mina_requestPresentation: `Error: ${error.message}`,
77+
}));
78+
}
79+
};
80+
8881
const fetchAccounts = async () => {
8982
if (!provider) return;
9083
const { result } = await provider.request({
@@ -479,6 +472,35 @@ export const TestZkApp = () => {
479472
</div>
480473
</div>
481474
</section>
475+
<section className="card bg-neutral">
476+
<div className="card-body gap-4">
477+
<h2 className="card-title">Request Presentation</h2>
478+
<p>mina_requestPresentation</p>
479+
<div className="flex flex-col gap-2">
480+
<div className="flex flex-col gap-4">
481+
<textarea
482+
value={presentationRequest}
483+
onChange={(event) => setPresentationRequest(event.target.value)}
484+
className="textarea textarea-bordered h-48 font-mono text-sm"
485+
placeholder="Enter presentation request JSON..."
486+
/>
487+
<button
488+
type="button"
489+
className="btn btn-primary"
490+
onClick={requestPresentation}
491+
>
492+
Request Presentation
493+
</button>
494+
</div>
495+
<label>Result</label>
496+
<textarea
497+
value={results.mina_requestPresentation}
498+
readOnly
499+
className="textarea textarea-bordered h-24 resize-none font-mono"
500+
/>
501+
</div>
502+
</div>
503+
</section>
482504
</main>
483505
);
484506
};

0 commit comments

Comments
 (0)