@veridid/poe 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +39 -2
- package/build/PoeApi.d.mts +150 -0
- package/build/PoeApi.d.mts.map +1 -0
- package/build/PoeApi.mjs +262 -0
- package/build/PoeApi.mjs.map +1 -0
- package/build/PoeEvents.d.mts +187 -0
- package/build/PoeEvents.d.mts.map +1 -0
- package/build/PoeEvents.mjs +28 -0
- package/build/PoeEvents.mjs.map +1 -0
- package/build/PoeModule.d.mts +61 -0
- package/build/PoeModule.d.mts.map +1 -0
- package/build/PoeModule.mjs +139 -0
- package/build/PoeModule.mjs.map +1 -0
- package/build/PoeModuleConfig.d.mts +71 -0
- package/build/PoeModuleConfig.d.mts.map +1 -0
- package/build/PoeRole.d.mts +21 -0
- package/build/PoeRole.d.mts.map +1 -0
- package/build/PoeRole.mjs +23 -0
- package/build/PoeRole.mjs.map +1 -0
- package/build/PoeState.d.mts +76 -0
- package/build/PoeState.d.mts.map +1 -0
- package/build/PoeState.mjs +78 -0
- package/build/PoeState.mjs.map +1 -0
- package/build/_virtual/_@oxc-project_runtime@0.112.0/helpers/decorate.mjs +10 -0
- package/build/_virtual/_@oxc-project_runtime@0.112.0/helpers/decorateMetadata.mjs +7 -0
- package/build/_virtual/_@oxc-project_runtime@0.112.0/helpers/decorateParam.mjs +9 -0
- package/build/_virtual/_rolldown/runtime.mjs +8 -0
- package/build/errors/PoeError.d.mts +173 -0
- package/build/errors/PoeError.d.mts.map +1 -0
- package/build/errors/PoeError.mjs +147 -0
- package/build/errors/PoeError.mjs.map +1 -0
- package/build/errors/index.d.mts +1 -0
- package/build/errors/index.mjs +1 -0
- package/build/executor/ProgramExecutor.d.mts +148 -0
- package/build/executor/ProgramExecutor.d.mts.map +1 -0
- package/build/executor/ProgramExecutorRegistry.d.mts +111 -0
- package/build/executor/ProgramExecutorRegistry.d.mts.map +1 -0
- package/build/executor/ProgramExecutorRegistry.mjs +145 -0
- package/build/executor/ProgramExecutorRegistry.mjs.map +1 -0
- package/build/executor/__mocks__/MockProgramExecutor.d.mts +118 -0
- package/build/executor/__mocks__/MockProgramExecutor.d.mts.map +1 -0
- package/build/executor/__mocks__/MockProgramExecutor.mjs +96 -0
- package/build/executor/__mocks__/MockProgramExecutor.mjs.map +1 -0
- package/build/executor/__mocks__/MockProofVerifier.d.mts +97 -0
- package/build/executor/__mocks__/MockProofVerifier.d.mts.map +1 -0
- package/build/executor/__mocks__/MockProofVerifier.mjs +79 -0
- package/build/executor/__mocks__/MockProofVerifier.mjs.map +1 -0
- package/build/executor/__mocks__/index.d.mts +2 -0
- package/build/executor/index.d.mts +5 -0
- package/build/executor/index.mjs +3 -0
- package/build/handlers/AcceptPoeMessageHandler.mjs +24 -0
- package/build/handlers/AcceptPoeMessageHandler.mjs.map +1 -0
- package/build/handlers/ChallengeMessageHandler.mjs +24 -0
- package/build/handlers/ChallengeMessageHandler.mjs.map +1 -0
- package/build/handlers/CompleteMessageHandler.mjs +24 -0
- package/build/handlers/CompleteMessageHandler.mjs.map +1 -0
- package/build/handlers/DeclinePoeMessageHandler.mjs +24 -0
- package/build/handlers/DeclinePoeMessageHandler.mjs.map +1 -0
- package/build/handlers/PoeProblemReportMessageHandler.mjs +18 -0
- package/build/handlers/PoeProblemReportMessageHandler.mjs.map +1 -0
- package/build/handlers/ProposePoeMessageHandler.mjs +18 -0
- package/build/handlers/ProposePoeMessageHandler.mjs.map +1 -0
- package/build/handlers/RequestPoeMessageHandler.mjs +18 -0
- package/build/handlers/RequestPoeMessageHandler.mjs.map +1 -0
- package/build/handlers/SubmitPoeMessageHandler.mjs +18 -0
- package/build/handlers/SubmitPoeMessageHandler.mjs.map +1 -0
- package/build/handlers/index.mjs +8 -0
- package/build/index.d.mts +38 -0
- package/build/index.mjs +33 -0
- package/build/messages/AcceptPoeMessage.d.mts +48 -0
- package/build/messages/AcceptPoeMessage.d.mts.map +1 -0
- package/build/messages/AcceptPoeMessage.mjs +61 -0
- package/build/messages/AcceptPoeMessage.mjs.map +1 -0
- package/build/messages/ChallengeMessage.d.mts +46 -0
- package/build/messages/ChallengeMessage.d.mts.map +1 -0
- package/build/messages/ChallengeMessage.mjs +57 -0
- package/build/messages/ChallengeMessage.mjs.map +1 -0
- package/build/messages/CompleteMessage.d.mts +56 -0
- package/build/messages/CompleteMessage.d.mts.map +1 -0
- package/build/messages/CompleteMessage.mjs +69 -0
- package/build/messages/CompleteMessage.mjs.map +1 -0
- package/build/messages/DeclinePoeMessage.d.mts +36 -0
- package/build/messages/DeclinePoeMessage.d.mts.map +1 -0
- package/build/messages/DeclinePoeMessage.mjs +45 -0
- package/build/messages/DeclinePoeMessage.mjs.map +1 -0
- package/build/messages/PoeProblemReportMessage.d.mts +51 -0
- package/build/messages/PoeProblemReportMessage.d.mts.map +1 -0
- package/build/messages/PoeProblemReportMessage.mjs +64 -0
- package/build/messages/PoeProblemReportMessage.mjs.map +1 -0
- package/build/messages/ProposePoeMessage.d.mts +51 -0
- package/build/messages/ProposePoeMessage.d.mts.map +1 -0
- package/build/messages/ProposePoeMessage.mjs +69 -0
- package/build/messages/ProposePoeMessage.mjs.map +1 -0
- package/build/messages/RequestPoeMessage.d.mts +64 -0
- package/build/messages/RequestPoeMessage.d.mts.map +1 -0
- package/build/messages/RequestPoeMessage.mjs +82 -0
- package/build/messages/RequestPoeMessage.mjs.map +1 -0
- package/build/messages/SubmitPoeMessage.d.mts +58 -0
- package/build/messages/SubmitPoeMessage.d.mts.map +1 -0
- package/build/messages/SubmitPoeMessage.mjs +75 -0
- package/build/messages/SubmitPoeMessage.mjs.map +1 -0
- package/build/messages/index.d.mts +9 -0
- package/build/messages/index.mjs +11 -0
- package/build/models/BindingContext.d.mts +36 -0
- package/build/models/BindingContext.d.mts.map +1 -0
- package/build/models/BindingContext.mjs +42 -0
- package/build/models/BindingContext.mjs.map +1 -0
- package/build/models/ProgramExecution.d.mts +142 -0
- package/build/models/ProgramExecution.d.mts.map +1 -0
- package/build/models/ProofArtifact.d.mts +260 -0
- package/build/models/ProofArtifact.d.mts.map +1 -0
- package/build/programs/AppAttestationProgram.d.mts +179 -0
- package/build/programs/AppAttestationProgram.d.mts.map +1 -0
- package/build/programs/AppAttestationProgram.mjs +296 -0
- package/build/programs/AppAttestationProgram.mjs.map +1 -0
- package/build/programs/PoeProgram.d.mts +204 -0
- package/build/programs/PoeProgram.d.mts.map +1 -0
- package/build/programs/PoeProgram.mjs +155 -0
- package/build/programs/PoeProgram.mjs.map +1 -0
- package/build/programs/examples/MockProgram.d.mts +98 -0
- package/build/programs/examples/MockProgram.d.mts.map +1 -0
- package/build/programs/examples/MockProgram.mjs +117 -0
- package/build/programs/examples/MockProgram.mjs.map +1 -0
- package/build/programs/examples/index.d.mts +1 -0
- package/build/programs/index.d.mts +4 -0
- package/build/programs/index.mjs +3 -0
- package/build/programs/vkeys/app_attestation_vkey.mjs +136 -0
- package/build/programs/vkeys/app_attestation_vkey.mjs.map +1 -0
- package/build/registry/ProgramRegistry.d.mts +108 -0
- package/build/registry/ProgramRegistry.d.mts.map +1 -0
- package/build/registry/ProgramRegistry.mjs +87 -0
- package/build/registry/ProgramRegistry.mjs.map +1 -0
- package/build/registry/ProofSystemRegistry.d.mts +111 -0
- package/build/registry/ProofSystemRegistry.d.mts.map +1 -0
- package/build/registry/ProofSystemRegistry.mjs +87 -0
- package/build/registry/ProofSystemRegistry.mjs.map +1 -0
- package/build/registry/index.d.mts +2 -0
- package/build/registry/index.mjs +2 -0
- package/build/repository/NonceRecord.d.mts +45 -0
- package/build/repository/NonceRecord.d.mts.map +1 -0
- package/build/repository/NonceRecord.mjs +34 -0
- package/build/repository/NonceRecord.mjs.map +1 -0
- package/build/repository/NonceRepository.d.mts +41 -0
- package/build/repository/NonceRepository.d.mts.map +1 -0
- package/build/repository/NonceRepository.mjs +63 -0
- package/build/repository/NonceRepository.mjs.map +1 -0
- package/build/repository/PoeSessionRecord.d.mts +107 -0
- package/build/repository/PoeSessionRecord.d.mts.map +1 -0
- package/build/repository/PoeSessionRecord.mjs +100 -0
- package/build/repository/PoeSessionRecord.mjs.map +1 -0
- package/build/repository/PoeSessionRepository.d.mts +29 -0
- package/build/repository/PoeSessionRepository.d.mts.map +1 -0
- package/build/repository/PoeSessionRepository.mjs +46 -0
- package/build/repository/PoeSessionRepository.mjs.map +1 -0
- package/build/repository/index.d.mts +4 -0
- package/build/repository/index.mjs +6 -0
- package/build/services/AutoProofExecutionService.d.mts +2 -0
- package/build/services/AutoProofExecutionService.mjs +144 -0
- package/build/services/AutoProofExecutionService.mjs.map +1 -0
- package/build/services/ContextBindingService.d.mts +51 -0
- package/build/services/ContextBindingService.d.mts.map +1 -0
- package/build/services/ContextBindingService.mjs +92 -0
- package/build/services/ContextBindingService.mjs.map +1 -0
- package/build/services/NonceService.d.mts +74 -0
- package/build/services/NonceService.d.mts.map +1 -0
- package/build/services/NonceService.mjs +126 -0
- package/build/services/NonceService.mjs.map +1 -0
- package/build/services/PoeService.d.mts +163 -0
- package/build/services/PoeService.d.mts.map +1 -0
- package/build/services/PoeService.mjs +487 -0
- package/build/services/PoeService.mjs.map +1 -0
- package/build/services/ProofVerificationService.d.mts +87 -0
- package/build/services/ProofVerificationService.d.mts.map +1 -0
- package/build/services/ProofVerificationService.mjs +164 -0
- package/build/services/ProofVerificationService.mjs.map +1 -0
- package/build/services/index.d.mts +5 -0
- package/build/services/index.mjs +5 -0
- package/build/workflow/PoeV1Actions.d.mts +149 -0
- package/build/workflow/PoeV1Actions.d.mts.map +1 -0
- package/build/workflow/PoeV1Actions.mjs +190 -0
- package/build/workflow/PoeV1Actions.mjs.map +1 -0
- package/package.json +44 -3
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { PoeProgram } from "./PoeProgram.mjs";
|
|
2
|
+
import app_attestation_vkey_default from "./vkeys/app_attestation_vkey.mjs";
|
|
3
|
+
import { createHash } from "crypto";
|
|
4
|
+
|
|
5
|
+
//#region src/programs/AppAttestationProgram.ts
|
|
6
|
+
/**
|
|
7
|
+
* SHA-256 of the canonical (sorted-key) JSON encoding of `value`. Returns a
|
|
8
|
+
* lowercase `0x`-prefixed hex string. Used as the `vk_hash` published in the
|
|
9
|
+
* program metadata and in every proof artifact.
|
|
10
|
+
*/
|
|
11
|
+
function canonicalVkHash(value) {
|
|
12
|
+
const canonical = canonicalJson(value);
|
|
13
|
+
return "0x" + createHash("sha256").update(canonical).digest("hex");
|
|
14
|
+
}
|
|
15
|
+
function canonicalJson(value) {
|
|
16
|
+
if (value === null || typeof value !== "object") return JSON.stringify(value);
|
|
17
|
+
if (Array.isArray(value)) return "[" + value.map((v) => canonicalJson(v)).join(",") + "]";
|
|
18
|
+
return "{" + Object.keys(value).sort().map((k) => JSON.stringify(k) + ":" + canonicalJson(value[k])).join(",") + "}";
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The order in which the on-device prover emits public outputs must match the
|
|
22
|
+
* order in which the Groth16 verifier feeds them to `snarkjs.groth16.verify`.
|
|
23
|
+
*
|
|
24
|
+
* Per `circuits/attestation/app_attestation.circom`:
|
|
25
|
+
*
|
|
26
|
+
* ```
|
|
27
|
+
* Public outputs (auto-public):
|
|
28
|
+
* [0] is_genuine
|
|
29
|
+
* [1] out_app_commit
|
|
30
|
+
* [2] out_se_key_commit
|
|
31
|
+
* [3] out_device_commit
|
|
32
|
+
* [4] out_boot_commit
|
|
33
|
+
* [5] out_patch_commit
|
|
34
|
+
* [6] out_timestamp
|
|
35
|
+
*
|
|
36
|
+
* Public inputs:
|
|
37
|
+
* [7] expected_nonce_commit
|
|
38
|
+
* [8] expected_context_commit
|
|
39
|
+
* [9] expected_session_commit
|
|
40
|
+
* [10] expected_app_commit
|
|
41
|
+
* [11] expected_se_key_commit
|
|
42
|
+
* [12] expected_verdict_commit
|
|
43
|
+
* [13] expected_device_commit
|
|
44
|
+
* [14] expected_boot_commit
|
|
45
|
+
* [15] expected_patch_commit
|
|
46
|
+
* [16] min_patch_level
|
|
47
|
+
* [17] proof_timestamp
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
const PUBLIC_INPUT_INDEX = {
|
|
51
|
+
isGenuine: 0,
|
|
52
|
+
outAppCommit: 1,
|
|
53
|
+
outSeKeyCommit: 2,
|
|
54
|
+
outDeviceCommit: 3,
|
|
55
|
+
outBootCommit: 4,
|
|
56
|
+
outPatchCommit: 5,
|
|
57
|
+
outTimestamp: 6,
|
|
58
|
+
expectedNonceCommit: 7,
|
|
59
|
+
expectedContextCommit: 8,
|
|
60
|
+
expectedSessionCommit: 9,
|
|
61
|
+
expectedAppCommit: 10,
|
|
62
|
+
expectedSeKeyCommit: 11,
|
|
63
|
+
expectedVerdictCommit: 12,
|
|
64
|
+
expectedDeviceCommit: 13,
|
|
65
|
+
expectedBootCommit: 14,
|
|
66
|
+
expectedPatchCommit: 15,
|
|
67
|
+
minPatchLevel: 16,
|
|
68
|
+
proofTimestamp: 17
|
|
69
|
+
};
|
|
70
|
+
/** Lazy snarkjs loader so verifier-side bundles can omit it when stubbed. */
|
|
71
|
+
async function defaultGroth16Verify(vkey, publicSignals, proof) {
|
|
72
|
+
return (await import("snarkjs")).groth16.verify(vkey, publicSignals, proof);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* PoE program implementing `ajna.app-attestation.v1` — cryptographic device
|
|
76
|
+
* attestation as a Proof of Execution.
|
|
77
|
+
*
|
|
78
|
+
* **Prover side** (mobile, RN): supply a `proofProvider` callback that calls
|
|
79
|
+
* `react-native-poe-proofs.generateAppAttestationProofPOE`. The callback
|
|
80
|
+
* orchestrates the AndroidKeyStore + Play Integrity (or DCAppAttestService)
|
|
81
|
+
* collectors, runs the Rust verifier on-device, and returns a Groth16 proof.
|
|
82
|
+
*
|
|
83
|
+
* **Verifier side** (Node): the verifier runs snarkjs.groth16.verify against
|
|
84
|
+
* the vendored verifying key, asserts `is_genuine === '1'`, checks the binding
|
|
85
|
+
* triple matches the expected context, validates the vk_hash whitelist, and
|
|
86
|
+
* confirms the program's commitments are echoed correctly.
|
|
87
|
+
*/
|
|
88
|
+
var AppAttestationProgram = class extends PoeProgram {
|
|
89
|
+
constructor(options = {}) {
|
|
90
|
+
super();
|
|
91
|
+
this.programId = options.programId ?? "ajna.app-attestation.v1";
|
|
92
|
+
this.version = options.version ?? "1.0.0";
|
|
93
|
+
this.vkey = options.verifyingKey ?? app_attestation_vkey_default;
|
|
94
|
+
this.publishedVkHash = canonicalVkHash(this.vkey);
|
|
95
|
+
this.allowedVkHashes = options.allowedVkHashes ?? [this.publishedVkHash];
|
|
96
|
+
this.defaultMinPatchLevel = options.defaultMinPatchLevel ?? 202401;
|
|
97
|
+
this.proofProvider = options.proofProvider;
|
|
98
|
+
this.groth16Verify = options.groth16Verifier ?? defaultGroth16Verify;
|
|
99
|
+
}
|
|
100
|
+
getMetadata() {
|
|
101
|
+
return {
|
|
102
|
+
program_id: this.programId,
|
|
103
|
+
version: this.version,
|
|
104
|
+
name: "App Attestation",
|
|
105
|
+
description: "Proves the app is genuine on a device with verified boot, locked bootloader, hardware-backed key, and current OS patch level. Android: KeyStore HW attestation + Play Integrity. iOS: DCAppAttestService attestKey + assertion.",
|
|
106
|
+
allowed_vk_hashes: this.allowedVkHashes,
|
|
107
|
+
public_schema: `${this.programId}/public@1`,
|
|
108
|
+
supports_interactive: false,
|
|
109
|
+
max_runtime_s: 5,
|
|
110
|
+
max_artifact_mb: 1
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
createExecutor(_context) {
|
|
114
|
+
if (!this.proofProvider) return void 0;
|
|
115
|
+
const proofProvider = this.proofProvider;
|
|
116
|
+
const programId = this.programId;
|
|
117
|
+
const publishedVkHash = this.publishedVkHash;
|
|
118
|
+
const defaultMinPatchLevel = this.defaultMinPatchLevel;
|
|
119
|
+
return {
|
|
120
|
+
programId,
|
|
121
|
+
async canExecute(_ctx, execution) {
|
|
122
|
+
if (execution.program_id !== programId) return `Program ID mismatch: expected ${programId}, got ${execution.program_id}`;
|
|
123
|
+
},
|
|
124
|
+
async execute(_ctx, execution, bindingContext) {
|
|
125
|
+
const minPatchLevel = readNumberFromInputs(execution, "min_patch_level", defaultMinPatchLevel);
|
|
126
|
+
const prover = await proofProvider({
|
|
127
|
+
bindingContext,
|
|
128
|
+
minPatchLevel,
|
|
129
|
+
platformOptions: readObjectFromInputs(execution, "platform_options"),
|
|
130
|
+
execution
|
|
131
|
+
});
|
|
132
|
+
const proof = JSON.parse(prover.proofJson);
|
|
133
|
+
const proofB64 = Buffer.from(JSON.stringify(proof), "utf8").toString("base64");
|
|
134
|
+
const idx = PUBLIC_INPUT_INDEX;
|
|
135
|
+
return {
|
|
136
|
+
program_id: programId,
|
|
137
|
+
result: prover.isGenuine ? "pass" : "fail",
|
|
138
|
+
public: {
|
|
139
|
+
schema: `${programId}/public@1`,
|
|
140
|
+
nonce: bindingContext.nonce,
|
|
141
|
+
context_hash: bindingContext.context_hash,
|
|
142
|
+
session_id: bindingContext.session_id,
|
|
143
|
+
vk_hash: publishedVkHash,
|
|
144
|
+
is_genuine: prover.publicInputs[idx.isGenuine] ?? (prover.isGenuine ? "1" : "0"),
|
|
145
|
+
app_commitment: prover.appCommitment,
|
|
146
|
+
se_key_commitment: prover.seKeyCommitment,
|
|
147
|
+
device_commitment: prover.deviceCommitment,
|
|
148
|
+
boot_commitment: prover.bootCommitment,
|
|
149
|
+
patch_commitment: prover.patchCommitment,
|
|
150
|
+
min_patch_level: prover.publicInputs[idx.minPatchLevel] ?? String(minPatchLevel),
|
|
151
|
+
timestamp: prover.publicInputs[idx.outTimestamp] ?? String(Math.floor(Date.now() / 1e3))
|
|
152
|
+
},
|
|
153
|
+
zk: {
|
|
154
|
+
scheme: "groth16",
|
|
155
|
+
circuit_id: "poe-app-attestation-v1",
|
|
156
|
+
vk_hash: publishedVkHash,
|
|
157
|
+
proof_b64: proofB64,
|
|
158
|
+
metadata: { publicSignals: prover.publicInputs }
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
createVerifier(_context) {
|
|
165
|
+
const programId = this.programId;
|
|
166
|
+
const allowedVkHashes = new Set(this.allowedVkHashes);
|
|
167
|
+
const vkey = this.vkey;
|
|
168
|
+
const groth16Verify = this.groth16Verify;
|
|
169
|
+
return {
|
|
170
|
+
programId,
|
|
171
|
+
async verify(_ctx, artifact, expectedContext) {
|
|
172
|
+
const errors = [];
|
|
173
|
+
if (artifact.program_id !== programId) errors.push(`Program ID mismatch: expected ${programId}, got ${artifact.program_id}`);
|
|
174
|
+
if (artifact.public.nonce !== expectedContext.nonce) errors.push("Nonce mismatch in binding context");
|
|
175
|
+
if (artifact.public.context_hash !== expectedContext.context_hash) errors.push("Context hash mismatch in binding context");
|
|
176
|
+
if (artifact.public.session_id !== expectedContext.session_id) errors.push("Session ID mismatch in binding context");
|
|
177
|
+
const publicVkHash = String(artifact.public.vk_hash ?? "");
|
|
178
|
+
const zkVkHash = String(artifact.zk?.vk_hash ?? "");
|
|
179
|
+
if (publicVkHash !== zkVkHash) errors.push(`vk_hash mismatch: public=${publicVkHash}, zk=${zkVkHash}`);
|
|
180
|
+
else if (!allowedVkHashes.has(publicVkHash)) errors.push(`vk_hash not allowed: ${publicVkHash}`);
|
|
181
|
+
if (artifact.zk?.scheme !== "groth16") errors.push(`Unsupported proof scheme: ${artifact.zk?.scheme}`);
|
|
182
|
+
if (artifact.zk?.circuit_id !== "poe-app-attestation-v1") errors.push(`Unexpected circuit_id: ${artifact.zk?.circuit_id}`);
|
|
183
|
+
if (errors.length > 0) return {
|
|
184
|
+
verified: false,
|
|
185
|
+
errors
|
|
186
|
+
};
|
|
187
|
+
const publicSignals = (artifact.zk?.metadata)?.publicSignals ?? publicSignalsFromArtifactPublic(artifact.public);
|
|
188
|
+
let proof;
|
|
189
|
+
try {
|
|
190
|
+
const decoded = Buffer.from(artifact.zk.proof_b64, "base64").toString("utf8");
|
|
191
|
+
proof = JSON.parse(decoded);
|
|
192
|
+
} catch (e) {
|
|
193
|
+
return {
|
|
194
|
+
verified: false,
|
|
195
|
+
errors: [`Could not decode proof_b64: ${e.message}`]
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
let snarkOk = false;
|
|
199
|
+
try {
|
|
200
|
+
snarkOk = await groth16Verify(vkey, publicSignals, proof);
|
|
201
|
+
} catch (e) {
|
|
202
|
+
return {
|
|
203
|
+
verified: false,
|
|
204
|
+
errors: [`snarkjs.verify error: ${e.message}`]
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
if (!snarkOk) return {
|
|
208
|
+
verified: false,
|
|
209
|
+
errors: ["snarkjs.verify returned false"]
|
|
210
|
+
};
|
|
211
|
+
if (String(artifact.public.is_genuine ?? "0") !== "1") return {
|
|
212
|
+
verified: false,
|
|
213
|
+
errors: ["Circuit reported is_genuine=0"]
|
|
214
|
+
};
|
|
215
|
+
const idx = PUBLIC_INPUT_INDEX;
|
|
216
|
+
const checks = [
|
|
217
|
+
[
|
|
218
|
+
"app_commitment",
|
|
219
|
+
artifact.public.app_commitment,
|
|
220
|
+
publicSignals[idx.outAppCommit]
|
|
221
|
+
],
|
|
222
|
+
[
|
|
223
|
+
"se_key_commitment",
|
|
224
|
+
artifact.public.se_key_commitment,
|
|
225
|
+
publicSignals[idx.outSeKeyCommit]
|
|
226
|
+
],
|
|
227
|
+
[
|
|
228
|
+
"device_commitment",
|
|
229
|
+
artifact.public.device_commitment,
|
|
230
|
+
publicSignals[idx.outDeviceCommit]
|
|
231
|
+
],
|
|
232
|
+
[
|
|
233
|
+
"boot_commitment",
|
|
234
|
+
artifact.public.boot_commitment,
|
|
235
|
+
publicSignals[idx.outBootCommit]
|
|
236
|
+
],
|
|
237
|
+
[
|
|
238
|
+
"patch_commitment",
|
|
239
|
+
artifact.public.patch_commitment,
|
|
240
|
+
publicSignals[idx.outPatchCommit]
|
|
241
|
+
]
|
|
242
|
+
];
|
|
243
|
+
for (const [label, claimed, fromCircuit] of checks) if (claimed !== void 0 && fromCircuit !== void 0 && claimed !== fromCircuit) errors.push(`Commitment ${label} echoed in 'public' does not match circuit output`);
|
|
244
|
+
return errors.length === 0 ? { verified: true } : {
|
|
245
|
+
verified: false,
|
|
246
|
+
errors
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
function readNumberFromInputs(execution, key, fallback) {
|
|
253
|
+
const raw = (execution.inputs?.by_value)?.[key];
|
|
254
|
+
if (typeof raw === "number") return raw;
|
|
255
|
+
if (typeof raw === "string") {
|
|
256
|
+
const n = Number(raw);
|
|
257
|
+
return Number.isFinite(n) ? n : fallback;
|
|
258
|
+
}
|
|
259
|
+
return fallback;
|
|
260
|
+
}
|
|
261
|
+
function readObjectFromInputs(execution, key) {
|
|
262
|
+
const raw = (execution.inputs?.by_value)?.[key];
|
|
263
|
+
if (raw && typeof raw === "object" && !Array.isArray(raw)) return raw;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Fallback path: if the prover didn't include the full public signals in
|
|
267
|
+
* `zk.metadata.publicSignals`, reconstruct them from the publicly disclosed
|
|
268
|
+
* outputs. This will only succeed if all expected/out commitments are echoed
|
|
269
|
+
* in `artifact.public` — which is the normal case for this program.
|
|
270
|
+
*/
|
|
271
|
+
function publicSignalsFromArtifactPublic(pub) {
|
|
272
|
+
return [
|
|
273
|
+
String(pub.is_genuine ?? "0"),
|
|
274
|
+
String(pub.app_commitment ?? "0"),
|
|
275
|
+
String(pub.se_key_commitment ?? "0"),
|
|
276
|
+
String(pub.device_commitment ?? "0"),
|
|
277
|
+
String(pub.boot_commitment ?? "0"),
|
|
278
|
+
String(pub.patch_commitment ?? "0"),
|
|
279
|
+
String(pub.timestamp ?? "0"),
|
|
280
|
+
String(pub.app_commitment ?? "0"),
|
|
281
|
+
String(pub.se_key_commitment ?? "0"),
|
|
282
|
+
String(pub.app_commitment ?? "0"),
|
|
283
|
+
String(pub.app_commitment ?? "0"),
|
|
284
|
+
String(pub.se_key_commitment ?? "0"),
|
|
285
|
+
String(pub.device_commitment ?? "0"),
|
|
286
|
+
String(pub.device_commitment ?? "0"),
|
|
287
|
+
String(pub.boot_commitment ?? "0"),
|
|
288
|
+
String(pub.patch_commitment ?? "0"),
|
|
289
|
+
String(pub.min_patch_level ?? "0"),
|
|
290
|
+
String(pub.timestamp ?? "0")
|
|
291
|
+
];
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
//#endregion
|
|
295
|
+
export { AppAttestationProgram, PUBLIC_INPUT_INDEX, canonicalVkHash };
|
|
296
|
+
//# sourceMappingURL=AppAttestationProgram.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AppAttestationProgram.mjs","names":["appAttestationVkey"],"sources":["../../src/programs/AppAttestationProgram.ts"],"sourcesContent":["import type { AgentContext } from '@credo-ts/core'\nimport type { ProgramMetadata } from '../registry/ProgramRegistry'\nimport type { ProgramExecutor, ProofVerifier } from '../executor/ProgramExecutor'\nimport type { BindingContext, ProofArtifact, ProgramExecution } from '../models'\n\nimport { createHash } from 'crypto'\n\nimport { PoeProgram } from './PoeProgram'\n\n// Vendored snarkjs verifying key for the app_attestation Groth16 circuit.\n// This is the authoritative VK; the on-device prover and this verifier MUST\n// share the exact same vkey, otherwise verification will silently fail.\nimport appAttestationVkey from './vkeys/app_attestation_vkey.json'\n\n// ============================================================\n// Public types\n// ============================================================\n\n/**\n * Proof bundle the on-device prover hands to the executor.\n *\n * Produced by `react-native-poe-proofs.generateAppAttestationProofPOE` on the\n * mobile prover. The credo-ts executor in this package is platform-agnostic —\n * it just takes the bundle and packages it into a PoE `ProofArtifact`.\n */\nexport interface AppAttestationProverResult {\n proofJson: string\n publicInputs: string[]\n isGenuine: boolean\n appCommitment: string\n seKeyCommitment: string\n deviceCommitment: string\n bootCommitment: string\n patchCommitment: string\n}\n\n/**\n * Callback the executor uses to obtain a proof from whichever prover is wired up.\n *\n * The credo-ts package can't directly import `react-native-poe-proofs` because\n * that would force a React Native dependency even for verifier-only Node\n * deployments. Instead, the host app passes a `proofProvider` callback at\n * program-construction time:\n *\n * ```ts\n * import { generateAppAttestationProofPOE } from 'react-native-poe-proofs'\n *\n * new AppAttestationProgram({\n * proofProvider: async ({ bindingContext, minPatchLevel, platformOptions }) => {\n * return await generateAppAttestationProofPOE({\n * nonce: bigintFromHex(bindingContext.nonce),\n * contextHash: bigintFromHex(bindingContext.context_hash),\n * sessionId: bigintFromHex(bindingContext.session_id),\n * minPatchLevel,\n * proofTimestamp: Math.floor(Date.now() / 1000),\n * ...platformOptions,\n * })\n * },\n * })\n * ```\n */\nexport type AppAttestationProofProvider = (\n args: AppAttestationExecuteArgs\n) => Promise<AppAttestationProverResult>\n\nexport interface AppAttestationExecuteArgs {\n bindingContext: BindingContext\n minPatchLevel: number\n platformOptions?: Record<string, unknown>\n execution: ProgramExecution\n}\n\nexport interface AppAttestationProgramOptions {\n /**\n * Override the program version (defaults to '1.0.0').\n */\n version?: string\n\n /**\n * Override the program ID (defaults to 'ajna.app-attestation.v1').\n */\n programId?: string\n\n /**\n * Minimum acceptable OS patch level (e.g. 202401 for Jan 2024).\n * Defaults to 202401.\n */\n defaultMinPatchLevel?: number\n\n /**\n * Override the vendored vkey (e.g. to test against a different trusted setup).\n * If omitted, the verifier uses the JSON imported from `./vkeys/`.\n */\n verifyingKey?: unknown\n\n /**\n * Whitelist of acceptable vk_hashes. Defaults to `[sha256(canonical(vkey))]`,\n * which is the same value the program publishes via `getMetadata().allowed_vk_hashes`.\n */\n allowedVkHashes?: string[]\n\n /**\n * Optional proof provider that bridges to a real prover (e.g. the mobile bridge).\n * If omitted, this program is registered as **verifier-only**.\n */\n proofProvider?: AppAttestationProofProvider\n\n /**\n * Override the snarkjs.groth16 verifier. Defaults to dynamic-importing the\n * `snarkjs` package at first use. Passing your own is mostly for testing.\n */\n groth16Verifier?: (\n vkey: unknown,\n publicSignals: string[],\n proof: unknown\n ) => Promise<boolean>\n}\n\n// ============================================================\n// Helpers\n// ============================================================\n\n/**\n * SHA-256 of the canonical (sorted-key) JSON encoding of `value`. Returns a\n * lowercase `0x`-prefixed hex string. Used as the `vk_hash` published in the\n * program metadata and in every proof artifact.\n */\nexport function canonicalVkHash(value: unknown): string {\n const canonical = canonicalJson(value)\n const hash = createHash('sha256').update(canonical).digest('hex')\n return '0x' + hash\n}\n\nfunction canonicalJson(value: unknown): string {\n if (value === null || typeof value !== 'object') {\n return JSON.stringify(value)\n }\n if (Array.isArray(value)) {\n return '[' + value.map((v) => canonicalJson(v)).join(',') + ']'\n }\n const keys = Object.keys(value as Record<string, unknown>).sort()\n return (\n '{' +\n keys.map((k) => JSON.stringify(k) + ':' + canonicalJson((value as Record<string, unknown>)[k])).join(',') +\n '}'\n )\n}\n\n/**\n * The order in which the on-device prover emits public outputs must match the\n * order in which the Groth16 verifier feeds them to `snarkjs.groth16.verify`.\n *\n * Per `circuits/attestation/app_attestation.circom`:\n *\n * ```\n * Public outputs (auto-public):\n * [0] is_genuine\n * [1] out_app_commit\n * [2] out_se_key_commit\n * [3] out_device_commit\n * [4] out_boot_commit\n * [5] out_patch_commit\n * [6] out_timestamp\n *\n * Public inputs:\n * [7] expected_nonce_commit\n * [8] expected_context_commit\n * [9] expected_session_commit\n * [10] expected_app_commit\n * [11] expected_se_key_commit\n * [12] expected_verdict_commit\n * [13] expected_device_commit\n * [14] expected_boot_commit\n * [15] expected_patch_commit\n * [16] min_patch_level\n * [17] proof_timestamp\n * ```\n */\nexport const PUBLIC_INPUT_INDEX = {\n isGenuine: 0,\n outAppCommit: 1,\n outSeKeyCommit: 2,\n outDeviceCommit: 3,\n outBootCommit: 4,\n outPatchCommit: 5,\n outTimestamp: 6,\n expectedNonceCommit: 7,\n expectedContextCommit: 8,\n expectedSessionCommit: 9,\n expectedAppCommit: 10,\n expectedSeKeyCommit: 11,\n expectedVerdictCommit: 12,\n expectedDeviceCommit: 13,\n expectedBootCommit: 14,\n expectedPatchCommit: 15,\n minPatchLevel: 16,\n proofTimestamp: 17,\n} as const\n\n/** Lazy snarkjs loader so verifier-side bundles can omit it when stubbed. */\nasync function defaultGroth16Verify(\n vkey: unknown,\n publicSignals: string[],\n proof: unknown\n): Promise<boolean> {\n // snarkjs lacks bundled type defs in some versions — cast to `any`.\n // The verifier-only deployment must include `snarkjs` as a dependency;\n // verifier-stubbed unit tests can inject a custom `groth16Verifier`.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const snarkjs = (await import('snarkjs' as any)) as { groth16: { verify: (vk: unknown, signals: string[], proof: unknown) => Promise<boolean> } }\n return snarkjs.groth16.verify(vkey, publicSignals, proof)\n}\n\n// ============================================================\n// AppAttestationProgram\n// ============================================================\n\n/**\n * PoE program implementing `ajna.app-attestation.v1` — cryptographic device\n * attestation as a Proof of Execution.\n *\n * **Prover side** (mobile, RN): supply a `proofProvider` callback that calls\n * `react-native-poe-proofs.generateAppAttestationProofPOE`. The callback\n * orchestrates the AndroidKeyStore + Play Integrity (or DCAppAttestService)\n * collectors, runs the Rust verifier on-device, and returns a Groth16 proof.\n *\n * **Verifier side** (Node): the verifier runs snarkjs.groth16.verify against\n * the vendored verifying key, asserts `is_genuine === '1'`, checks the binding\n * triple matches the expected context, validates the vk_hash whitelist, and\n * confirms the program's commitments are echoed correctly.\n */\nexport class AppAttestationProgram extends PoeProgram {\n public readonly programId: string\n public readonly version: string\n\n private readonly vkey: unknown\n private readonly allowedVkHashes: string[]\n private readonly publishedVkHash: string\n private readonly defaultMinPatchLevel: number\n private readonly proofProvider?: AppAttestationProofProvider\n private readonly groth16Verify: (\n vkey: unknown,\n publicSignals: string[],\n proof: unknown\n ) => Promise<boolean>\n\n public constructor(options: AppAttestationProgramOptions = {}) {\n super()\n this.programId = options.programId ?? 'ajna.app-attestation.v1'\n this.version = options.version ?? '1.0.0'\n this.vkey = options.verifyingKey ?? appAttestationVkey\n this.publishedVkHash = canonicalVkHash(this.vkey)\n this.allowedVkHashes = options.allowedVkHashes ?? [this.publishedVkHash]\n this.defaultMinPatchLevel = options.defaultMinPatchLevel ?? 202401\n this.proofProvider = options.proofProvider\n this.groth16Verify = options.groth16Verifier ?? defaultGroth16Verify\n }\n\n // -- Program metadata --\n\n public getMetadata(): ProgramMetadata {\n return {\n program_id: this.programId,\n version: this.version,\n name: 'App Attestation',\n description:\n 'Proves the app is genuine on a device with verified boot, locked bootloader, hardware-backed key, and current OS patch level. Android: KeyStore HW attestation + Play Integrity. iOS: DCAppAttestService attestKey + assertion.',\n allowed_vk_hashes: this.allowedVkHashes,\n public_schema: `${this.programId}/public@1`,\n supports_interactive: false,\n max_runtime_s: 5,\n max_artifact_mb: 1,\n }\n }\n\n // -- Prover side --\n\n public createExecutor(_context: AgentContext): ProgramExecutor | undefined {\n if (!this.proofProvider) return undefined\n\n const proofProvider = this.proofProvider\n const programId = this.programId\n const publishedVkHash = this.publishedVkHash\n const defaultMinPatchLevel = this.defaultMinPatchLevel\n\n return {\n programId,\n\n async canExecute(_ctx, execution): Promise<string | undefined> {\n if (execution.program_id !== programId) {\n return `Program ID mismatch: expected ${programId}, got ${execution.program_id}`\n }\n return undefined\n },\n\n async execute(\n _ctx: AgentContext,\n execution: ProgramExecution,\n bindingContext: BindingContext\n ): Promise<ProofArtifact> {\n const minPatchLevel = readNumberFromInputs(\n execution,\n 'min_patch_level',\n defaultMinPatchLevel\n )\n const platformOptions = readObjectFromInputs(execution, 'platform_options')\n\n const prover = await proofProvider({\n bindingContext,\n minPatchLevel,\n platformOptions,\n execution,\n })\n\n const proof = JSON.parse(prover.proofJson)\n const proofB64 = Buffer.from(JSON.stringify(proof), 'utf8').toString('base64')\n\n const idx = PUBLIC_INPUT_INDEX\n\n return {\n program_id: programId,\n result: prover.isGenuine ? 'pass' : 'fail',\n public: {\n schema: `${programId}/public@1`,\n nonce: bindingContext.nonce,\n context_hash: bindingContext.context_hash,\n session_id: bindingContext.session_id,\n vk_hash: publishedVkHash,\n is_genuine: prover.publicInputs[idx.isGenuine] ?? (prover.isGenuine ? '1' : '0'),\n app_commitment: prover.appCommitment,\n se_key_commitment: prover.seKeyCommitment,\n device_commitment: prover.deviceCommitment,\n boot_commitment: prover.bootCommitment,\n patch_commitment: prover.patchCommitment,\n min_patch_level: prover.publicInputs[idx.minPatchLevel] ?? String(minPatchLevel),\n timestamp: prover.publicInputs[idx.outTimestamp] ?? String(Math.floor(Date.now() / 1000)),\n },\n zk: {\n scheme: 'groth16',\n circuit_id: 'poe-app-attestation-v1',\n vk_hash: publishedVkHash,\n proof_b64: proofB64,\n metadata: {\n publicSignals: prover.publicInputs,\n },\n },\n }\n },\n }\n }\n\n // -- Verifier side --\n\n public createVerifier(_context: AgentContext): ProofVerifier {\n const programId = this.programId\n const allowedVkHashes = new Set(this.allowedVkHashes)\n const vkey = this.vkey\n const groth16Verify = this.groth16Verify\n\n return {\n programId,\n\n async verify(_ctx, artifact, expectedContext) {\n const errors: string[] = []\n\n // 1. Program id\n if (artifact.program_id !== programId) {\n errors.push(`Program ID mismatch: expected ${programId}, got ${artifact.program_id}`)\n }\n\n // 2. Binding triple\n if (artifact.public.nonce !== expectedContext.nonce) {\n errors.push('Nonce mismatch in binding context')\n }\n if (artifact.public.context_hash !== expectedContext.context_hash) {\n errors.push('Context hash mismatch in binding context')\n }\n if (artifact.public.session_id !== expectedContext.session_id) {\n errors.push('Session ID mismatch in binding context')\n }\n\n // 3. vk_hash whitelist — public AND zk values must match each other,\n // and the resulting value must be in the allowed set.\n const publicVkHash = String(artifact.public.vk_hash ?? '')\n const zkVkHash = String(artifact.zk?.vk_hash ?? '')\n if (publicVkHash !== zkVkHash) {\n errors.push(`vk_hash mismatch: public=${publicVkHash}, zk=${zkVkHash}`)\n } else if (!allowedVkHashes.has(publicVkHash)) {\n errors.push(`vk_hash not allowed: ${publicVkHash}`)\n }\n\n // 4. Scheme + circuit_id\n if (artifact.zk?.scheme !== 'groth16') {\n errors.push(`Unsupported proof scheme: ${artifact.zk?.scheme}`)\n }\n if (artifact.zk?.circuit_id !== 'poe-app-attestation-v1') {\n errors.push(`Unexpected circuit_id: ${artifact.zk?.circuit_id}`)\n }\n\n if (errors.length > 0) {\n return { verified: false, errors }\n }\n\n // 5. snarkjs Groth16 verification\n const publicSignals =\n (artifact.zk?.metadata as { publicSignals?: string[] } | undefined)?.publicSignals ??\n publicSignalsFromArtifactPublic(artifact.public)\n\n let proof: unknown\n try {\n const decoded = Buffer.from(artifact.zk!.proof_b64, 'base64').toString('utf8')\n proof = JSON.parse(decoded)\n } catch (e) {\n return { verified: false, errors: [`Could not decode proof_b64: ${(e as Error).message}`] }\n }\n\n let snarkOk = false\n try {\n snarkOk = await groth16Verify(vkey, publicSignals, proof)\n } catch (e) {\n return { verified: false, errors: [`snarkjs.verify error: ${(e as Error).message}`] }\n }\n\n if (!snarkOk) {\n return { verified: false, errors: ['snarkjs.verify returned false'] }\n }\n\n // 6. is_genuine must be 1\n const isGenuine = String(artifact.public.is_genuine ?? '0')\n if (isGenuine !== '1') {\n return { verified: false, errors: ['Circuit reported is_genuine=0'] }\n }\n\n // 7. Commitments echoed by public outputs must match the witness commitments\n const idx = PUBLIC_INPUT_INDEX\n const checks: Array<[string, unknown, string | undefined]> = [\n ['app_commitment', artifact.public.app_commitment, publicSignals[idx.outAppCommit]],\n ['se_key_commitment', artifact.public.se_key_commitment, publicSignals[idx.outSeKeyCommit]],\n ['device_commitment', artifact.public.device_commitment, publicSignals[idx.outDeviceCommit]],\n ['boot_commitment', artifact.public.boot_commitment, publicSignals[idx.outBootCommit]],\n ['patch_commitment', artifact.public.patch_commitment, publicSignals[idx.outPatchCommit]],\n ]\n for (const [label, claimed, fromCircuit] of checks) {\n if (claimed !== undefined && fromCircuit !== undefined && claimed !== fromCircuit) {\n errors.push(`Commitment ${label} echoed in 'public' does not match circuit output`)\n }\n }\n\n return errors.length === 0\n ? { verified: true }\n : { verified: false, errors }\n },\n }\n }\n}\n\n// ============================================================\n// Internal helpers\n// ============================================================\n\nfunction readNumberFromInputs(\n execution: ProgramExecution,\n key: string,\n fallback: number\n): number {\n const byValue = execution.inputs?.by_value as Record<string, unknown> | undefined\n const raw = byValue?.[key]\n if (typeof raw === 'number') return raw\n if (typeof raw === 'string') {\n const n = Number(raw)\n return Number.isFinite(n) ? n : fallback\n }\n return fallback\n}\n\nfunction readObjectFromInputs(\n execution: ProgramExecution,\n key: string\n): Record<string, unknown> | undefined {\n const byValue = execution.inputs?.by_value as Record<string, unknown> | undefined\n const raw = byValue?.[key]\n if (raw && typeof raw === 'object' && !Array.isArray(raw)) {\n return raw as Record<string, unknown>\n }\n return undefined\n}\n\n/**\n * Fallback path: if the prover didn't include the full public signals in\n * `zk.metadata.publicSignals`, reconstruct them from the publicly disclosed\n * outputs. This will only succeed if all expected/out commitments are echoed\n * in `artifact.public` — which is the normal case for this program.\n */\nfunction publicSignalsFromArtifactPublic(pub: ProofArtifact['public']): string[] {\n return [\n String(pub.is_genuine ?? '0'),\n String(pub.app_commitment ?? '0'),\n String(pub.se_key_commitment ?? '0'),\n String(pub.device_commitment ?? '0'),\n String(pub.boot_commitment ?? '0'),\n String(pub.patch_commitment ?? '0'),\n String(pub.timestamp ?? '0'),\n // Expected commitments (these MUST equal the out_ values per the circuit)\n String(pub.app_commitment ?? '0'),\n String(pub.se_key_commitment ?? '0'),\n String(pub.app_commitment ?? '0'),\n String(pub.app_commitment ?? '0'),\n String(pub.se_key_commitment ?? '0'),\n String(pub.device_commitment ?? '0'),\n String(pub.device_commitment ?? '0'),\n String(pub.boot_commitment ?? '0'),\n String(pub.patch_commitment ?? '0'),\n String(pub.min_patch_level ?? '0'),\n String(pub.timestamp ?? '0'),\n ]\n}\n"],"mappings":";;;;;;;;;;AA+HA,SAAgB,gBAAgB,OAAwB;CACtD,MAAM,YAAY,cAAc,MAAM;AAEtC,QAAO,OADM,WAAW,SAAS,CAAC,OAAO,UAAU,CAAC,OAAO,MAAM;;AAInE,SAAS,cAAc,OAAwB;AAC7C,KAAI,UAAU,QAAQ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AM,QAAQ,IAAI,CACvD,QAAO;;;;;;;;AAWX,SAAS,gCAAgC,KAAwC;AAC/E,QAAO;EACL,OAAO,IAAI,cAAc,IAAI;EAC7B,OAAO,IAAI,kBAAkB,IAAI;EACjC,OAAO,IAAI,qBAAqB,IAAI;EACpC,OAAO,IAAI,qBAAqB,IAAI;EACpC,OAAO,IAAI,mBAAmB,IAAI;EAClC,OAAO,IAAI,oBAAoB,IAAI;EACnC,OAAO,IAAI,aAAa,IAAI;EAE5B,OAAO,IAAI,kBAAkB,IAAI;EACjC,OAAO,IAAI,qBAAqB,IAAI;EACpC,OAAO,IAAI,kBAAkB,IAAI;EACjC,OAAO,IAAI,kBAAkB,IAAI;EACjC,OAAO,IAAI,qBAAqB,IAAI;EACpC,OAAO,IAAI,qBAAqB,IAAI;EACpC,OAAO,IAAI,qBAAqB,IAAI;EACpC,OAAO,IAAI,mBAAmB,IAAI;EAClC,OAAO,IAAI,oBAAoB,IAAI;EACnC,OAAO,IAAI,mBAAmB,IAAI;EAClC,OAAO,IAAI,aAAa,IAAI;EAC7B"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { ProgramMetadata } from "../registry/ProgramRegistry.mjs";
|
|
2
|
+
import { ProofSystem } from "../registry/ProofSystemRegistry.mjs";
|
|
3
|
+
import { ProgramExecutor, ProofVerifier } from "../executor/ProgramExecutor.mjs";
|
|
4
|
+
import { AgentContext } from "@credo-ts/core";
|
|
5
|
+
|
|
6
|
+
//#region src/programs/PoeProgram.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* Abstract base class for POE programs.
|
|
9
|
+
*
|
|
10
|
+
* Implement this class to create a new ZK program that can be
|
|
11
|
+
* registered with the POE module. The class bundles:
|
|
12
|
+
* - Program metadata (capabilities, allowed VK hashes, schemas)
|
|
13
|
+
* - Executor (prover side - generates proofs)
|
|
14
|
+
* - Verifier (verifier side - validates proofs)
|
|
15
|
+
* - Proof system (optional - cryptographic verification)
|
|
16
|
+
*
|
|
17
|
+
* This unified approach allows you to register a program with a single
|
|
18
|
+
* object instead of separately registering metadata, executor, and verifier.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* export class AadharAgeProgram extends PoeProgram {
|
|
23
|
+
* readonly programId = 'aadhaar.zk.age-threshold.v1'
|
|
24
|
+
* readonly version = '1.0.0'
|
|
25
|
+
*
|
|
26
|
+
* getMetadata(): ProgramMetadata {
|
|
27
|
+
* return {
|
|
28
|
+
* program_id: this.programId,
|
|
29
|
+
* version: this.version,
|
|
30
|
+
* name: 'Aadhar Age Verification',
|
|
31
|
+
* allowed_vk_hashes: ['0x...'],
|
|
32
|
+
* public_schema: 'aadhaar.zk.age-threshold.v1/public@1',
|
|
33
|
+
* supports_interactive: false,
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* createExecutor(context: AgentContext): ProgramExecutor {
|
|
38
|
+
* return {
|
|
39
|
+
* programId: this.programId,
|
|
40
|
+
* async execute(ctx, execution, bindingContext) {
|
|
41
|
+
* // Generate ZK proof
|
|
42
|
+
* return { ... }
|
|
43
|
+
* }
|
|
44
|
+
* }
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* createVerifier(context: AgentContext): ProofVerifier {
|
|
48
|
+
* return {
|
|
49
|
+
* programId: this.programId,
|
|
50
|
+
* async verify(ctx, artifact, expectedContext) {
|
|
51
|
+
* // Verify ZK proof
|
|
52
|
+
* return { verified: true }
|
|
53
|
+
* }
|
|
54
|
+
* }
|
|
55
|
+
* }
|
|
56
|
+
* }
|
|
57
|
+
*
|
|
58
|
+
* // Register with module
|
|
59
|
+
* new PoeModule({
|
|
60
|
+
* programs: [new AadharAgeProgram()],
|
|
61
|
+
* autoExecuteProofs: true,
|
|
62
|
+
* })
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
declare abstract class PoeProgram {
|
|
66
|
+
/**
|
|
67
|
+
* Unique program identifier (e.g., 'aadhaar.zk.age-threshold.v1')
|
|
68
|
+
* Must be globally unique and follow naming conventions.
|
|
69
|
+
*/
|
|
70
|
+
abstract readonly programId: string;
|
|
71
|
+
/**
|
|
72
|
+
* Program version (semver recommended, e.g., '1.0.0')
|
|
73
|
+
*/
|
|
74
|
+
abstract readonly version: string;
|
|
75
|
+
/**
|
|
76
|
+
* Get program metadata for registry registration.
|
|
77
|
+
*
|
|
78
|
+
* Defines the capabilities and constraints of this program:
|
|
79
|
+
* - Allowed verifying key hashes
|
|
80
|
+
* - Input/output schemas
|
|
81
|
+
* - Performance limits
|
|
82
|
+
* - Interactive support
|
|
83
|
+
*
|
|
84
|
+
* @returns Program metadata to register in ProgramRegistry
|
|
85
|
+
*/
|
|
86
|
+
abstract getMetadata(): ProgramMetadata;
|
|
87
|
+
/**
|
|
88
|
+
* Create an executor instance for this program (prover side).
|
|
89
|
+
*
|
|
90
|
+
* The executor is responsible for:
|
|
91
|
+
* 1. Retrieving necessary data
|
|
92
|
+
* 2. Generating the ZK proof
|
|
93
|
+
* 3. Returning a ProofArtifact
|
|
94
|
+
*
|
|
95
|
+
* Return `undefined` if this agent should only verify, not prove.
|
|
96
|
+
* This is useful for verifier-only deployments.
|
|
97
|
+
*
|
|
98
|
+
* @param context - Agent context for accessing storage, DID resolver, etc.
|
|
99
|
+
* @returns Executor instance, or undefined if proving not supported
|
|
100
|
+
*/
|
|
101
|
+
abstract createExecutor(context: AgentContext): ProgramExecutor | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Create a verifier instance for this program (verifier side).
|
|
104
|
+
*
|
|
105
|
+
* The verifier provides custom verification logic beyond generic verification.
|
|
106
|
+
* If registered, it takes precedence over ProofVerificationService's generic logic.
|
|
107
|
+
*
|
|
108
|
+
* Return `undefined` to use generic verification only.
|
|
109
|
+
* This is useful for prover-only deployments or when generic verification suffices.
|
|
110
|
+
*
|
|
111
|
+
* @param context - Agent context
|
|
112
|
+
* @returns Verifier instance, or undefined to use generic verification
|
|
113
|
+
*/
|
|
114
|
+
abstract createVerifier(context: AgentContext): ProofVerifier | undefined;
|
|
115
|
+
/**
|
|
116
|
+
* Get a custom proof system for this program (optional).
|
|
117
|
+
*
|
|
118
|
+
* Return `undefined` to use the default proof system from ProofSystemRegistry.
|
|
119
|
+
* Only override if this program uses a non-standard proof system.
|
|
120
|
+
*
|
|
121
|
+
* @returns Custom proof system, or undefined to use default
|
|
122
|
+
*/
|
|
123
|
+
getProofSystem(): ProofSystem | undefined;
|
|
124
|
+
/**
|
|
125
|
+
* Called during module initialization.
|
|
126
|
+
*
|
|
127
|
+
* Override to perform async setup tasks such as:
|
|
128
|
+
* - Loading cryptographic keys
|
|
129
|
+
* - Initializing circuit parameters
|
|
130
|
+
* - Connecting to external services
|
|
131
|
+
*
|
|
132
|
+
* @param context - Agent context
|
|
133
|
+
*/
|
|
134
|
+
initialize(context: AgentContext): Promise<void>;
|
|
135
|
+
/**
|
|
136
|
+
* Called during module shutdown.
|
|
137
|
+
*
|
|
138
|
+
* Override to cleanup resources such as:
|
|
139
|
+
* - Closing connections
|
|
140
|
+
* - Releasing memory
|
|
141
|
+
* - Flushing caches
|
|
142
|
+
*/
|
|
143
|
+
shutdown(): Promise<void>;
|
|
144
|
+
/**
|
|
145
|
+
* Get a human-readable description of this program.
|
|
146
|
+
* Used for logging and debugging.
|
|
147
|
+
*/
|
|
148
|
+
toString(): string;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Convenience base class for programs that only prove (prover-only agent).
|
|
152
|
+
*
|
|
153
|
+
* Use this when the agent will only generate proofs, never verify them.
|
|
154
|
+
* This is common for mobile wallets or edge devices.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* export class MyProverProgram extends ProverOnlyProgram {
|
|
159
|
+
* readonly programId = 'my.program.v1'
|
|
160
|
+
* readonly version = '1.0.0'
|
|
161
|
+
*
|
|
162
|
+
* getMetadata() { ... }
|
|
163
|
+
* createExecutor(context) { ... }
|
|
164
|
+
* // No need to implement createVerifier - it returns undefined
|
|
165
|
+
* }
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
declare abstract class ProverOnlyProgram extends PoeProgram {
|
|
169
|
+
/**
|
|
170
|
+
* Always returns undefined - prover-only programs don't verify.
|
|
171
|
+
*/
|
|
172
|
+
createVerifier(): undefined;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Convenience base class for programs that only verify (verifier-only agent).
|
|
176
|
+
*
|
|
177
|
+
* Use this when the agent will only verify proofs, never generate them.
|
|
178
|
+
* This is common for backend services or verification nodes.
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* export class MyVerifierProgram extends VerifierOnlyProgram {
|
|
183
|
+
* readonly programId = 'my.program.v1'
|
|
184
|
+
* readonly version = '1.0.0'
|
|
185
|
+
*
|
|
186
|
+
* getMetadata() { ... }
|
|
187
|
+
* createVerifier(context) { ... }
|
|
188
|
+
* // No need to implement createExecutor - it returns undefined
|
|
189
|
+
* }
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
declare abstract class VerifierOnlyProgram extends PoeProgram {
|
|
193
|
+
/**
|
|
194
|
+
* Always returns undefined - verifier-only programs don't execute.
|
|
195
|
+
*/
|
|
196
|
+
createExecutor(): undefined;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Type guard to check if an object is a PoeProgram
|
|
200
|
+
*/
|
|
201
|
+
declare function isPoeProgram(obj: unknown): obj is PoeProgram;
|
|
202
|
+
//#endregion
|
|
203
|
+
export { PoeProgram, ProverOnlyProgram, VerifierOnlyProgram, isPoeProgram };
|
|
204
|
+
//# sourceMappingURL=PoeProgram.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PoeProgram.d.mts","names":[],"sources":["../../src/programs/PoeProgram.ts"],"mappings":";;;;;;;;AA+DA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwHA;;;;;AA2BA;;;;;AAYA;;;;;;;;;uBA/JsB,UAAA;;;;;oBAKF,SAAA;;;;oBAKA,OAAA;;;;;;;;;;;;WAaT,WAAA,CAAA,GAAe,eAAA;;;;;;;;;;;;;;;WAgBf,cAAA,CAAe,OAAA,EAAS,YAAA,GAAe,eAAA;;;;;;;;;;;;;WAcvC,cAAA,CAAe,OAAA,EAAS,YAAA,GAAe,aAAA;;;;;;;;;EAUhD,cAAA,CAAA,GAAkB,WAAA;;;;;;;;;;;EAcZ,UAAA,CAAW,OAAA,EAAS,YAAA,GAAe,OAAA;;;;;;;;;EAYnC,QAAA,CAAA,GAAY,OAAA;;;;;EAQlB,QAAA,CAAA;AAAA;;;;;;;;;;;;;;;;;;;uBAuBoB,iBAAA,SAA0B,UAAA;;;;EAI9C,cAAA,CAAA;AAAA;;;;;;;;;;;;;;;;;;;uBAuBoB,mBAAA,SAA4B,UAAA;;;;EAIhD,cAAA,CAAA;AAAA;;;;iBAQc,YAAA,CAAa,GAAA,YAAe,GAAA,IAAO,UAAA"}
|