@bolyra/sdk 0.2.0 → 0.3.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 +201 -0
- package/NOTICE +63 -0
- package/README.md +2 -2
- package/dist/delegation.d.ts +64 -16
- package/dist/delegation.d.ts.map +1 -1
- package/dist/delegation.js +200 -17
- package/dist/delegation.js.map +1 -1
- package/dist/errors.d.ts +12 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +32 -1
- package/dist/errors.js.map +1 -1
- package/dist/handshake.d.ts +2 -0
- package/dist/handshake.d.ts.map +1 -1
- package/dist/handshake.js +55 -13
- package/dist/handshake.js.map +1 -1
- package/dist/identity.d.ts +24 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +46 -0
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +8 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +26 -3
- package/dist/index.js.map +1 -1
- package/dist/model-binding.d.ts +113 -0
- package/dist/model-binding.d.ts.map +1 -0
- package/dist/model-binding.js +195 -0
- package/dist/model-binding.js.map +1 -0
- package/dist/offchain.d.ts +89 -0
- package/dist/offchain.d.ts.map +1 -0
- package/dist/offchain.js +300 -0
- package/dist/offchain.js.map +1 -0
- package/dist/prover.d.ts +21 -0
- package/dist/prover.d.ts.map +1 -0
- package/dist/prover.js +171 -0
- package/dist/prover.js.map +1 -0
- package/dist/types.d.ts +29 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +4 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +14 -0
- package/dist/utils.js.map +1 -1
- package/package.json +5 -3
- package/src/delegation.ts +268 -30
- package/src/errors.ts +46 -0
- package/src/handshake.ts +69 -20
- package/src/identity.ts +55 -1
- package/src/index.ts +29 -2
- package/src/offchain.ts +344 -0
- package/src/prover.ts +178 -0
- package/src/types.ts +32 -0
- package/src/utils.ts +23 -0
package/dist/prover.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.proveGroth16 = proveGroth16;
|
|
37
|
+
exports.activeProverBackend = activeProverBackend;
|
|
38
|
+
const snarkjs = __importStar(require("snarkjs"));
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
const fs = __importStar(require("fs"));
|
|
41
|
+
const os = __importStar(require("os"));
|
|
42
|
+
const child_process_1 = require("child_process");
|
|
43
|
+
const util_1 = require("util");
|
|
44
|
+
const execFileAsync = (0, util_1.promisify)(child_process_1.execFile);
|
|
45
|
+
let cachedRapidsnarkPath = undefined;
|
|
46
|
+
const wcCache = new Map();
|
|
47
|
+
const wcQueue = new Map();
|
|
48
|
+
function getWitnessCalculator(wasmPath) {
|
|
49
|
+
const cached = wcCache.get(wasmPath);
|
|
50
|
+
if (cached)
|
|
51
|
+
return cached;
|
|
52
|
+
const promise = (async () => {
|
|
53
|
+
// witness_calculator.js sits next to the .wasm in circuit_js/
|
|
54
|
+
const wcDir = path.dirname(wasmPath);
|
|
55
|
+
const builderPath = path.join(wcDir, 'witness_calculator.js');
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
57
|
+
const builder = require(builderPath);
|
|
58
|
+
const wasmBuf = fs.readFileSync(wasmPath);
|
|
59
|
+
return (await builder(wasmBuf));
|
|
60
|
+
})();
|
|
61
|
+
wcCache.set(wasmPath, promise);
|
|
62
|
+
return promise;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Compute a witness using the cached calculator for `wasmPath`, serialized so
|
|
66
|
+
* concurrent calls on the same wasm don't race on the shared WASM instance.
|
|
67
|
+
*/
|
|
68
|
+
async function computeWitness(wasmPath, input) {
|
|
69
|
+
const wc = await getWitnessCalculator(wasmPath);
|
|
70
|
+
const prev = wcQueue.get(wasmPath) ?? Promise.resolve();
|
|
71
|
+
const next = prev.then(() => wc.calculateWTNSBin(input, 0));
|
|
72
|
+
// Keep the chain alive but swallow errors so a failing call doesn't poison the queue.
|
|
73
|
+
wcQueue.set(wasmPath, next.catch(() => undefined));
|
|
74
|
+
return next;
|
|
75
|
+
}
|
|
76
|
+
/** Find the rapidsnark prover binary, or return null if not available. */
|
|
77
|
+
function findRapidsnarkBinary() {
|
|
78
|
+
if (cachedRapidsnarkPath !== undefined)
|
|
79
|
+
return cachedRapidsnarkPath;
|
|
80
|
+
// 1) Explicit env override
|
|
81
|
+
if (process.env.BOLYRA_RAPIDSNARK) {
|
|
82
|
+
if (fs.existsSync(process.env.BOLYRA_RAPIDSNARK)) {
|
|
83
|
+
cachedRapidsnarkPath = process.env.BOLYRA_RAPIDSNARK;
|
|
84
|
+
return cachedRapidsnarkPath;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// 2) Bundled in circuits/build/rapidsnark_prover (matches benchmark setup)
|
|
88
|
+
const bundled = path.join(__dirname, '../../circuits/build/rapidsnark_prover');
|
|
89
|
+
if (fs.existsSync(bundled)) {
|
|
90
|
+
cachedRapidsnarkPath = bundled;
|
|
91
|
+
return cachedRapidsnarkPath;
|
|
92
|
+
}
|
|
93
|
+
// 3) PATH lookup for `prover` or `rapidsnark`
|
|
94
|
+
for (const name of ['rapidsnark_prover', 'rapidsnark', 'prover']) {
|
|
95
|
+
try {
|
|
96
|
+
const out = (0, child_process_1.execFileSync)('which', [name], { encoding: 'utf8' }).trim();
|
|
97
|
+
if (out) {
|
|
98
|
+
cachedRapidsnarkPath = out;
|
|
99
|
+
return cachedRapidsnarkPath;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
// not in PATH
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
cachedRapidsnarkPath = null;
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
/** Generate a Groth16 proof using rapidsnark (witness gen via snarkjs WASM). */
|
|
110
|
+
async function proveWithRapidsnark(input, wasmPath, zkeyPath, binary) {
|
|
111
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'bolyra-rs-'));
|
|
112
|
+
try {
|
|
113
|
+
const wtnsPath = path.join(tmp, 'witness.wtns');
|
|
114
|
+
const proofPath = path.join(tmp, 'proof.json');
|
|
115
|
+
const publicPath = path.join(tmp, 'public.json');
|
|
116
|
+
const wtnsBuf = await computeWitness(wasmPath, input);
|
|
117
|
+
fs.writeFileSync(wtnsPath, Buffer.from(wtnsBuf));
|
|
118
|
+
// Async exec so concurrent proofs (e.g., human + agent in a handshake)
|
|
119
|
+
// actually run in parallel instead of serializing on the event loop.
|
|
120
|
+
await execFileAsync(binary, [zkeyPath, wtnsPath, proofPath, publicPath]);
|
|
121
|
+
const proof = JSON.parse(fs.readFileSync(proofPath, 'utf8'));
|
|
122
|
+
const publicSignals = JSON.parse(fs.readFileSync(publicPath, 'utf8'));
|
|
123
|
+
return { proof, publicSignals };
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
try {
|
|
127
|
+
fs.rmSync(tmp, { recursive: true, force: true });
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
// best-effort cleanup
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Generate a Groth16 proof using the fastest available backend.
|
|
136
|
+
* rapidsnark is ~5x faster than snarkjs but requires the native binary.
|
|
137
|
+
*
|
|
138
|
+
* @param input - Circuit input (string-encoded bigints)
|
|
139
|
+
* @param wasmPath - Path to circuit_js/circuit.wasm (witness generator)
|
|
140
|
+
* @param zkeyPath - Path to circuit_final.zkey
|
|
141
|
+
* @param backend - 'auto' (default), 'rapidsnark', or 'snarkjs'
|
|
142
|
+
*/
|
|
143
|
+
async function proveGroth16(input, wasmPath, zkeyPath, backend = 'auto') {
|
|
144
|
+
if (backend === 'snarkjs') {
|
|
145
|
+
const { proof, publicSignals } = await snarkjs.groth16.fullProve(input, wasmPath, zkeyPath);
|
|
146
|
+
return { proof, publicSignals };
|
|
147
|
+
}
|
|
148
|
+
if (backend === 'rapidsnark') {
|
|
149
|
+
const bin = findRapidsnarkBinary();
|
|
150
|
+
if (!bin) {
|
|
151
|
+
throw new Error('rapidsnark requested but not found. Set BOLYRA_RAPIDSNARK=/path/to/prover, place binary at circuits/build/rapidsnark_prover, or install on PATH.');
|
|
152
|
+
}
|
|
153
|
+
return proveWithRapidsnark(input, wasmPath, zkeyPath, bin);
|
|
154
|
+
}
|
|
155
|
+
// auto: try rapidsnark, fall back to snarkjs
|
|
156
|
+
const bin = findRapidsnarkBinary();
|
|
157
|
+
if (bin) {
|
|
158
|
+
return proveWithRapidsnark(input, wasmPath, zkeyPath, bin);
|
|
159
|
+
}
|
|
160
|
+
const { proof, publicSignals } = await snarkjs.groth16.fullProve(input, wasmPath, zkeyPath);
|
|
161
|
+
return { proof, publicSignals };
|
|
162
|
+
}
|
|
163
|
+
/** Returns the active backend that would be used (for diagnostics/logging). */
|
|
164
|
+
function activeProverBackend(backend = 'auto') {
|
|
165
|
+
if (backend === 'snarkjs')
|
|
166
|
+
return 'snarkjs';
|
|
167
|
+
if (backend === 'rapidsnark')
|
|
168
|
+
return 'rapidsnark';
|
|
169
|
+
return findRapidsnarkBinary() ? 'rapidsnark' : 'snarkjs';
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=prover.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prover.js","sourceRoot":"","sources":["../src/prover.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8IA,oCA4BC;AAGD,kDAIC;AAjLD,iDAAmC;AACnC,2CAA6B;AAC7B,uCAAyB;AACzB,uCAAyB;AACzB,iDAAuD;AACvD,+BAAiC;AAEjC,MAAM,aAAa,GAAG,IAAA,gBAAS,EAAC,wBAAQ,CAAC,CAAC;AAW1C,IAAI,oBAAoB,GAA8B,SAAS,CAAC;AAUhE,MAAM,OAAO,GAAG,IAAI,GAAG,EAAsC,CAAC;AAC9D,MAAM,OAAO,GAAG,IAAI,GAAG,EAA4B,CAAC;AAEpD,SAAS,oBAAoB,CAAC,QAAgB;IAC5C,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,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AAK,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAC5F,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC;IAClC,CAAC;IAED,IAAI,OAAO,KAAK,YAAY,EAAE,CAAC;QAC7B,MAAM,GAAG,GAAG,oBAAoB,EAAE,CAAC;QACnC,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,MAAM,IAAI,KAAK,CACb,kJAAkJ,CACnJ,CAAC;QACJ,CAAC;QACD,OAAO,mBAAmB,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,CAAC,CAAC;IAC7D,CAAC;IAED,6CAA6C;IAC7C,MAAM,GAAG,GAAG,oBAAoB,EAAE,CAAC;IACnC,IAAI,GAAG,EAAE,CAAC;QACR,OAAO,mBAAmB,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,CAAC,CAAC;IAC7D,CAAC;IACD,MAAM,EAAE,KAAK,EAAE,aAAa,EAAE,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAC5F,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC;AAClC,CAAC;AAED,+EAA+E;AAC/E,SAAgB,mBAAmB,CAAC,UAAyB,MAAM;IACjE,IAAI,OAAO,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC5C,IAAI,OAAO,KAAK,YAAY;QAAE,OAAO,YAAY,CAAC;IAClD,OAAO,oBAAoB,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC;AAC3D,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -60,14 +60,43 @@ export interface DelegationResult {
|
|
|
60
60
|
newScopeCommitment: bigint;
|
|
61
61
|
/** Delegation nullifier (unique per delegation per session) */
|
|
62
62
|
delegationNullifier: bigint;
|
|
63
|
+
/** Delegatee Merkle root — checked on-chain against agentRootExists (CIP-1) */
|
|
64
|
+
delegateeMerkleRoot: bigint;
|
|
63
65
|
/** Hop number in the chain (0-indexed) */
|
|
64
66
|
hopIndex: number;
|
|
65
67
|
}
|
|
68
|
+
/** Optional Merkle inclusion proof for the delegatee's enrollment in agentTree.
|
|
69
|
+
* If omitted, delegate() defaults to the single-leaf pattern (depth 1, index 0,
|
|
70
|
+
* 20 zero siblings) — matches the conformance test layout. Real deployments
|
|
71
|
+
* pass the actual proof against the live agentTree.
|
|
72
|
+
*/
|
|
73
|
+
export interface DelegateeMerkleProof {
|
|
74
|
+
length: number;
|
|
75
|
+
index: number;
|
|
76
|
+
/** Always length 20 (Delegation circuit MAX_DEPTH) */
|
|
77
|
+
siblings: bigint[];
|
|
78
|
+
}
|
|
66
79
|
/** Proof with public signals ready for on-chain verification */
|
|
67
80
|
export interface Proof {
|
|
68
81
|
proof: any;
|
|
69
82
|
publicSignals: string[];
|
|
70
83
|
}
|
|
84
|
+
/** Result of an off-chain handshake verification (batched for later on-chain checkpoint) */
|
|
85
|
+
export interface OffchainVerificationResult extends HandshakeResult {
|
|
86
|
+
/** Index of this session within the current batch */
|
|
87
|
+
batchIndex: number;
|
|
88
|
+
/** Merkle root of the batch at the time this result was added (undefined until batch is sealed) */
|
|
89
|
+
batchRoot?: bigint;
|
|
90
|
+
}
|
|
91
|
+
/** On-chain checkpoint representing a batch of off-chain verified sessions */
|
|
92
|
+
export interface BatchCheckpoint {
|
|
93
|
+
/** Poseidon Merkle root of all session commitments in the batch */
|
|
94
|
+
root: bigint;
|
|
95
|
+
/** Unix timestamp (seconds) when the batch was posted on-chain */
|
|
96
|
+
timestamp: number;
|
|
97
|
+
/** Number of sessions included in this batch */
|
|
98
|
+
sessionCount: number;
|
|
99
|
+
}
|
|
71
100
|
/** Configuration for the SDK */
|
|
72
101
|
export interface BolyraConfig {
|
|
73
102
|
/** RPC URL for the target chain (default: Base Sepolia) */
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{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,MAAM,CAAC;CACpB;AAED,8EAA8E;AAC9E,MAAM,WAAW,eAAe;IAC9B,mEAAmE;IACnE,IAAI,EAAE,MAAM,CAAC;IACb,kEAAkE;IAClE,SAAS,EAAE,MAAM,CAAC;IAClB,gDAAgD;IAChD,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,gCAAgC;AAChC,MAAM,WAAW,YAAY;IAC3B,2DAA2D;IAC3D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,+CAA+C;IAC/C,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,oDAAoD;IACpD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,4CAA4C;IAC5C,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
*/
|
|
5
5
|
/** Poseidon hash with 2 inputs. Returns a bigint. */
|
|
6
6
|
export declare function poseidon2(a: bigint, b: bigint): Promise<bigint>;
|
|
7
|
+
/** Poseidon hash with 3 inputs. Returns a bigint. */
|
|
8
|
+
export declare function poseidon3(a: bigint, b: bigint, c: bigint): Promise<bigint>;
|
|
9
|
+
/** Poseidon hash with 4 inputs. Returns a bigint. */
|
|
10
|
+
export declare function poseidon4(a: bigint, b: bigint, c: bigint, d: bigint): Promise<bigint>;
|
|
7
11
|
/** Poseidon hash with 5 inputs. Returns a bigint. */
|
|
8
12
|
export declare function poseidon5(a: bigint, b: bigint, c: bigint, d: bigint, e: bigint): Promise<bigint>;
|
|
9
13
|
/**
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAgBH,qDAAqD;AACrD,wBAAsB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAIrE;AAED,qDAAqD;AACrD,w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OAAO,CAAC,MAAM,CAAC,CAIjB;AAED,qDAAqD;AACrD,wBAAsB,SAAS,CAC7B,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,GACR,OAAO,CAAC,MAAM,CAAC,CAIjB;AAED;;;;;;;;;GASG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,MAAM,GAAG,MAAM,GACtB,OAAO,CAAC;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CAYnC;AAED;;;GAGG;AACH,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,MAAM,GACb,OAAO,CAAC;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CAOnC;AAED,iDAAiD;AACjD,wBAAsB,SAAS,CAC7B,UAAU,EAAE,MAAM,GAAG,MAAM,EAC3B,OAAO,EAAE,MAAM,GACd,OAAO,CAAC;IAAE,EAAE,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CAYtD"}
|
package/dist/utils.js
CHANGED
|
@@ -38,6 +38,8 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
38
38
|
})();
|
|
39
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
40
|
exports.poseidon2 = poseidon2;
|
|
41
|
+
exports.poseidon3 = poseidon3;
|
|
42
|
+
exports.poseidon4 = poseidon4;
|
|
41
43
|
exports.poseidon5 = poseidon5;
|
|
42
44
|
exports.derivePublicKey = derivePublicKey;
|
|
43
45
|
exports.derivePublicKeyScalar = derivePublicKeyScalar;
|
|
@@ -61,6 +63,18 @@ async function poseidon2(a, b) {
|
|
|
61
63
|
const hash = _poseidon([a, b]);
|
|
62
64
|
return _F.toObject(hash);
|
|
63
65
|
}
|
|
66
|
+
/** Poseidon hash with 3 inputs. Returns a bigint. */
|
|
67
|
+
async function poseidon3(a, b, c) {
|
|
68
|
+
await ensureCrypto();
|
|
69
|
+
const hash = _poseidon([a, b, c]);
|
|
70
|
+
return _F.toObject(hash);
|
|
71
|
+
}
|
|
72
|
+
/** Poseidon hash with 4 inputs. Returns a bigint. */
|
|
73
|
+
async function poseidon4(a, b, c, d) {
|
|
74
|
+
await ensureCrypto();
|
|
75
|
+
const hash = _poseidon([a, b, c, d]);
|
|
76
|
+
return _F.toObject(hash);
|
|
77
|
+
}
|
|
64
78
|
/** Poseidon hash with 5 inputs. Returns a bigint. */
|
|
65
79
|
async function poseidon5(a, b, c, d, e) {
|
|
66
80
|
await ensureCrypto();
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBH,8BAIC;AAGD,8BAUC;AAYD,0CAcC;AAMD,sDASC;AAGD,8BAeC;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBH,8BAIC;AAGD,8BAQC;AAGD,8BASC;AAGD,8BAUC;AAYD,0CAcC;AAMD,sDASC;AAGD,8BAeC;AAlHD,IAAI,SAAS,GAAQ,IAAI,CAAC;AAC1B,IAAI,MAAM,GAAQ,IAAI,CAAC;AACvB,IAAI,QAAQ,GAAQ,IAAI,CAAC;AACzB,IAAI,EAAE,GAAQ,IAAI,CAAC;AAEnB,KAAK,UAAU,YAAY;IACzB,IAAI,SAAS;QAAE,OAAO;IACtB,MAAM,WAAW,GAAG,wDAAa,aAAa,GAAC,CAAC;IAChD,SAAS,GAAG,MAAM,WAAW,CAAC,a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AAC,EAAE,KAAK,CAAC;QAC/D,CAAC,CAAC,UAAU,CAAC;IACjB,MAAM,KAAK,GAAG,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;IAC5B,MAAM,GAAG,GAAG,MAAM,CAAC,YAAY,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAC5C,OAAO;QACL,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE;QAC5D,CAAC,EAAE,GAAG,CAAC,CAAC;KACT,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bolyra/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "TypeScript SDK for Bolyra — mutual ZKP authentication for humans and AI agents",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
26
|
"dist/",
|
|
27
|
-
"src/"
|
|
27
|
+
"src/",
|
|
28
|
+
"LICENSE",
|
|
29
|
+
"NOTICE"
|
|
28
30
|
],
|
|
29
31
|
"repository": {
|
|
30
32
|
"type": "git",
|
|
@@ -33,5 +35,5 @@
|
|
|
33
35
|
"publishConfig": {
|
|
34
36
|
"access": "public"
|
|
35
37
|
},
|
|
36
|
-
"license": "
|
|
38
|
+
"license": "Apache-2.0"
|
|
37
39
|
}
|
package/src/delegation.ts
CHANGED
|
@@ -1,45 +1,283 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import * as snarkjs from 'snarkjs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
import {
|
|
5
|
+
AgentCredential,
|
|
6
|
+
BolyraConfig,
|
|
7
|
+
DelegateeMerkleProof,
|
|
8
|
+
DelegationResult,
|
|
9
|
+
Proof,
|
|
10
|
+
} from './types';
|
|
11
|
+
import {
|
|
12
|
+
BolyraError,
|
|
13
|
+
CircuitArtifactNotFoundError,
|
|
14
|
+
ProofGenerationError,
|
|
15
|
+
ScopeEscalationError,
|
|
16
|
+
VerificationError,
|
|
17
|
+
} from './errors';
|
|
18
|
+
import { eddsaSign, poseidon3, poseidon4 } from './utils';
|
|
19
|
+
import { proveGroth16, ProverBackend } from './prover';
|
|
20
|
+
|
|
21
|
+
const DEFAULT_CIRCUIT_DIR =
|
|
22
|
+
process.env.BOLYRA_CIRCUITS_DIR ?? path.join(__dirname, '../../circuits/build');
|
|
23
|
+
|
|
24
|
+
/** Delegation circuit MAX_DEPTH constant (matches circuits/src/Delegation.circom). */
|
|
25
|
+
const DELEGATION_MAX_DEPTH = 20;
|
|
26
|
+
|
|
27
|
+
/** snarkjs publicSignals layout for the Delegation circuit (outputs first):
|
|
28
|
+
* [0] newScopeCommitment
|
|
29
|
+
* [1] delegationNullifier
|
|
30
|
+
* [2] delegateeMerkleRoot
|
|
31
|
+
* [3] previousScopeCommitment
|
|
32
|
+
* [4] sessionNonce
|
|
33
|
+
* [5] currentTimestamp
|
|
34
|
+
* This MUST match IdentityRegistry.verifyDelegation()'s pubSignals layout.
|
|
35
|
+
*/
|
|
36
|
+
const PUBSIG_NEW_SCOPE = 0;
|
|
37
|
+
const PUBSIG_NULLIFIER = 1;
|
|
38
|
+
const PUBSIG_DELEGATEE_ROOT = 2;
|
|
39
|
+
const PUBSIG_PREV_SCOPE = 3;
|
|
40
|
+
const PUBSIG_SESSION_NONCE = 4;
|
|
41
|
+
const PUBSIG_CURRENT_TS = 5;
|
|
42
|
+
|
|
43
|
+
/** Build the single-leaf Merkle proof default (matches the conformance test pattern). */
|
|
44
|
+
function defaultMerkleProof(): DelegateeMerkleProof {
|
|
45
|
+
return {
|
|
46
|
+
length: 1,
|
|
47
|
+
index: 0,
|
|
48
|
+
siblings: new Array(DELEGATION_MAX_DEPTH).fill(0n),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface DelegateInput {
|
|
53
|
+
/** The delegating agent's credential (provides modelHash, operator pubkey, scope, expiry). */
|
|
54
|
+
delegator: AgentCredential;
|
|
55
|
+
/** Operator's EdDSA private key — used to sign the delegation token.
|
|
56
|
+
* Same key that minted the delegator credential. */
|
|
57
|
+
delegatorOperatorPrivateKey: bigint | Buffer;
|
|
58
|
+
/** Identity commitment of the recipient (leaf in agentTree). */
|
|
59
|
+
delegateeCommitment: bigint;
|
|
60
|
+
/** Narrowed scope being granted. Must be a subset of delegator.permissionBitmask
|
|
61
|
+
* under the cumulative-bit rules. Circuit enforces; SDK pre-checks for a clean error. */
|
|
62
|
+
delegateeScope: bigint;
|
|
63
|
+
/** Expiry being granted. Must be <= delegator.expiryTimestamp. */
|
|
64
|
+
delegateeExpiry: bigint;
|
|
65
|
+
/** Scope commitment from the prior chain link.
|
|
66
|
+
* For hop 1, this is the agent's scopeCommitment output from the handshake.
|
|
67
|
+
* For hop N+1, this is the previous delegation's newScopeCommitment. */
|
|
68
|
+
previousScopeCommitment: bigint;
|
|
69
|
+
/** Session nonce. Must match the nonce of the originating handshake. */
|
|
70
|
+
sessionNonce: bigint;
|
|
71
|
+
/** Unix-seconds timestamp the proof is bound to. Must be within MAX_CLOCK_SKEW
|
|
72
|
+
* of block.timestamp at on-chain verification time (300s in IdentityRegistry).
|
|
73
|
+
* Defaults to floor(Date.now() / 1000). */
|
|
74
|
+
currentTimestamp?: bigint;
|
|
75
|
+
/** Optional Merkle inclusion proof for the delegatee in agentTree.
|
|
76
|
+
* Defaults to the single-leaf pattern (sufficient for tests and demos). */
|
|
77
|
+
delegateeMerkleProof?: DelegateeMerkleProof;
|
|
78
|
+
/** Informational hop index (0-indexed). Not consumed by the circuit;
|
|
79
|
+
* echoed in DelegationResult for caller bookkeeping. */
|
|
80
|
+
hopIndex?: number;
|
|
81
|
+
config?: BolyraConfig;
|
|
82
|
+
backend?: ProverBackend;
|
|
83
|
+
}
|
|
3
84
|
|
|
4
85
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
86
|
+
* Generate a Delegation proof.
|
|
87
|
+
*
|
|
88
|
+
* Produces a Groth16 proof matching the Delegation circuit. The caller submits
|
|
89
|
+
* `{ proof, publicSignals }` to `IdentityRegistry.verifyDelegation()` along with
|
|
90
|
+
* the session nonce.
|
|
7
91
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* @
|
|
92
|
+
* Scope narrowing is one-way: the circuit (and contract) reject any delegatee
|
|
93
|
+
* scope that is not a subset of the delegator's, and any expiry past the
|
|
94
|
+
* delegator's. The cumulative-bit invariants (bit 4 ⇒ 2+3, bit 3 ⇒ 2) are
|
|
95
|
+
* enforced on the delegatee scope.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* const { proof, result } = await delegate({
|
|
100
|
+
* delegator: parentCredential,
|
|
101
|
+
* delegatorOperatorPrivateKey: operatorSecret,
|
|
102
|
+
* delegateeCommitment: childCredential.commitment,
|
|
103
|
+
* delegateeScope: 0b00000011n, // read + write (narrower)
|
|
104
|
+
* delegateeExpiry: parentCredential.expiryTimestamp - 3600n,
|
|
105
|
+
* previousScopeCommitment: handshake.scopeCommitment,
|
|
106
|
+
* sessionNonce: handshake.sessionNonce,
|
|
107
|
+
* });
|
|
108
|
+
* ```
|
|
14
109
|
*/
|
|
15
110
|
export async function delegate(
|
|
16
|
-
|
|
17
|
-
_delegatee: AgentCredential,
|
|
18
|
-
_parentScopeCommitment: bigint,
|
|
19
|
-
_hopIndex: number,
|
|
20
|
-
_config?: BolyraConfig,
|
|
111
|
+
input: DelegateInput,
|
|
21
112
|
): Promise<{ proof: Proof; result: DelegationResult }> {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
113
|
+
// Pre-flight scope check — gives a clean error before paying for proof generation.
|
|
114
|
+
if (
|
|
115
|
+
(input.delegateeScope & ~input.delegator.permissionBitmask) !== 0n
|
|
116
|
+
) {
|
|
117
|
+
throw new ScopeEscalationError(
|
|
118
|
+
input.delegator.permissionBitmask,
|
|
119
|
+
input.delegateeScope,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
if (input.delegateeExpiry > input.delegator.expiryTimestamp) {
|
|
123
|
+
throw new BolyraError(
|
|
124
|
+
`Delegatee expiry (${input.delegateeExpiry}) exceeds delegator expiry (${input.delegator.expiryTimestamp}). Delegations may only narrow expiry, not extend it.`,
|
|
125
|
+
'EXPIRY_ESCALATION',
|
|
126
|
+
{
|
|
127
|
+
delegateeExpiry: input.delegateeExpiry.toString(),
|
|
128
|
+
delegatorExpiry: input.delegator.expiryTimestamp.toString(),
|
|
129
|
+
},
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Sanity check: the previousScopeCommitment passed in must equal the
|
|
134
|
+
// identity-bound chain link Poseidon3(delegatorScope, delegatorCredCommitment,
|
|
135
|
+
// delegatorExpiry). The circuit will assert this; we precheck for a clean error.
|
|
136
|
+
// Ordered before artifact loading so CI environments without circuits still
|
|
137
|
+
// surface CHAIN_LINK_MISMATCH instead of CIRCUIT_ARTIFACT_NOT_FOUND.
|
|
138
|
+
const expectedPrev = await poseidon3(
|
|
139
|
+
input.delegator.permissionBitmask,
|
|
140
|
+
input.delegator.commitment,
|
|
141
|
+
input.delegator.expiryTimestamp,
|
|
142
|
+
);
|
|
143
|
+
if (expectedPrev !== input.previousScopeCommitment) {
|
|
144
|
+
throw new BolyraError(
|
|
145
|
+
`previousScopeCommitment does not match the delegator's identity-bound chain link. Got ${input.previousScopeCommitment}, expected ${expectedPrev} (= Poseidon3(scope, credCommitment, expiry) for this delegator). For hop 1, pass the agent's scopeCommitment output from proveHandshake.`,
|
|
146
|
+
'CHAIN_LINK_MISMATCH',
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const circuitDir = input.config?.circuitDir ?? DEFAULT_CIRCUIT_DIR;
|
|
151
|
+
const backend = input.backend ?? 'auto';
|
|
152
|
+
const currentTimestamp =
|
|
153
|
+
input.currentTimestamp ?? BigInt(Math.floor(Date.now() / 1000));
|
|
154
|
+
const merkleProof = input.delegateeMerkleProof ?? defaultMerkleProof();
|
|
155
|
+
if (merkleProof.siblings.length !== DELEGATION_MAX_DEPTH) {
|
|
156
|
+
throw new BolyraError(
|
|
157
|
+
`Delegatee Merkle proof must have exactly ${DELEGATION_MAX_DEPTH} siblings (got ${merkleProof.siblings.length}).`,
|
|
158
|
+
'INVALID_MERKLE_PROOF',
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const wasmPath = path.join(circuitDir, 'Delegation_js/Delegation.wasm');
|
|
163
|
+
const zkeyPath = path.join(circuitDir, 'Delegation_final.zkey');
|
|
164
|
+
if (!fs.existsSync(wasmPath)) {
|
|
165
|
+
throw new CircuitArtifactNotFoundError(wasmPath, 'wasm');
|
|
166
|
+
}
|
|
167
|
+
if (!fs.existsSync(zkeyPath)) {
|
|
168
|
+
throw new CircuitArtifactNotFoundError(zkeyPath, 'zkey');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// The delegator signs the delegation token, binding this delegation to a
|
|
172
|
+
// specific recipient with specific scope+expiry.
|
|
173
|
+
// Token = Poseidon4(previousScopeCommitment, delegateeCommitment, delegateeScope, delegateeExpiry).
|
|
174
|
+
const tokenHash = await poseidon4(
|
|
175
|
+
input.previousScopeCommitment,
|
|
176
|
+
input.delegateeCommitment,
|
|
177
|
+
input.delegateeScope,
|
|
178
|
+
input.delegateeExpiry,
|
|
25
179
|
);
|
|
180
|
+
const sig = await eddsaSign(input.delegatorOperatorPrivateKey, tokenHash);
|
|
181
|
+
|
|
182
|
+
const witnessInput: Record<string, unknown> = {
|
|
183
|
+
delegatorScope: input.delegator.permissionBitmask.toString(),
|
|
184
|
+
delegateeScope: input.delegateeScope.toString(),
|
|
185
|
+
delegateeExpiry: input.delegateeExpiry.toString(),
|
|
186
|
+
delegatorExpiry: input.delegator.expiryTimestamp.toString(),
|
|
187
|
+
delegatorModelHash: input.delegator.modelHash.toString(),
|
|
188
|
+
delegatorPubkeyAx: input.delegator.operatorPublicKey.x.toString(),
|
|
189
|
+
delegatorPubkeyAy: input.delegator.operatorPublicKey.y.toString(),
|
|
190
|
+
sigR8x: sig.R8.x.toString(),
|
|
191
|
+
sigR8y: sig.R8.y.toString(),
|
|
192
|
+
sigS: sig.S.toString(),
|
|
193
|
+
delegatorCredCommitment: input.delegator.commitment.toString(),
|
|
194
|
+
delegateeCredCommitment: input.delegateeCommitment.toString(),
|
|
195
|
+
delegateeMerkleProofLength: merkleProof.length.toString(),
|
|
196
|
+
delegateeMerkleProofIndex: merkleProof.index.toString(),
|
|
197
|
+
delegateeMerkleProofSiblings: merkleProof.siblings.map((s) => s.toString()),
|
|
198
|
+
previousScopeCommitment: input.previousScopeCommitment.toString(),
|
|
199
|
+
sessionNonce: input.sessionNonce.toString(),
|
|
200
|
+
currentTimestamp: currentTimestamp.toString(),
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
let proof: Proof;
|
|
204
|
+
try {
|
|
205
|
+
proof = await proveGroth16(witnessInput, wasmPath, zkeyPath, backend);
|
|
206
|
+
} catch (err: any) {
|
|
207
|
+
throw new ProofGenerationError('Delegation', err.message ?? String(err));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const result: DelegationResult = {
|
|
211
|
+
newScopeCommitment: BigInt(proof.publicSignals[PUBSIG_NEW_SCOPE]),
|
|
212
|
+
delegationNullifier: BigInt(proof.publicSignals[PUBSIG_NULLIFIER]),
|
|
213
|
+
delegateeMerkleRoot: BigInt(proof.publicSignals[PUBSIG_DELEGATEE_ROOT]),
|
|
214
|
+
hopIndex: input.hopIndex ?? 0,
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
return { proof, result };
|
|
26
218
|
}
|
|
27
219
|
|
|
28
220
|
/**
|
|
29
|
-
* Verify a delegation proof
|
|
221
|
+
* Verify a delegation proof off-chain (snarkjs Groth16 verify).
|
|
30
222
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
223
|
+
* For on-chain enforcement, submit `proof` and `proof.publicSignals` to
|
|
224
|
+
* `IdentityRegistry.verifyDelegation(proof, pubSignals, sessionNonce)` — that
|
|
225
|
+
* call additionally enforces chain state, hop count, expiry binding, and
|
|
226
|
+
* nullifier replay. Off-chain verify here only confirms the proof itself is
|
|
227
|
+
* mathematically valid and that the publicSignals match the expected chain link.
|
|
35
228
|
*/
|
|
36
229
|
export async function verifyDelegation(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
230
|
+
proof: Proof,
|
|
231
|
+
previousScopeCommitment: bigint,
|
|
232
|
+
sessionNonce: bigint,
|
|
233
|
+
currentTimestamp: bigint,
|
|
234
|
+
config?: BolyraConfig,
|
|
40
235
|
): Promise<DelegationResult> {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
236
|
+
if (!proof || !proof.proof || !Array.isArray(proof.publicSignals)) {
|
|
237
|
+
throw new VerificationError(
|
|
238
|
+
'Invalid Delegation proof structure: expected { proof: object, publicSignals: string[] }.',
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
if (proof.publicSignals.length < 6) {
|
|
242
|
+
throw new VerificationError(
|
|
243
|
+
`Delegation proof has ${proof.publicSignals.length} public signals, expected 6.`,
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Bind the public signals to the expected chain context before trusting the proof.
|
|
248
|
+
if (BigInt(proof.publicSignals[PUBSIG_PREV_SCOPE]) !== previousScopeCommitment) {
|
|
249
|
+
throw new VerificationError(
|
|
250
|
+
`previousScopeCommitment mismatch: proof binds ${proof.publicSignals[PUBSIG_PREV_SCOPE]}, caller expected ${previousScopeCommitment}.`,
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
if (BigInt(proof.publicSignals[PUBSIG_SESSION_NONCE]) !== sessionNonce) {
|
|
254
|
+
throw new VerificationError(
|
|
255
|
+
`sessionNonce mismatch: proof binds ${proof.publicSignals[PUBSIG_SESSION_NONCE]}, caller expected ${sessionNonce}.`,
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
if (BigInt(proof.publicSignals[PUBSIG_CURRENT_TS]) !== currentTimestamp) {
|
|
259
|
+
throw new VerificationError(
|
|
260
|
+
`currentTimestamp mismatch: proof binds ${proof.publicSignals[PUBSIG_CURRENT_TS]}, caller expected ${currentTimestamp}.`,
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const circuitDir = config?.circuitDir ?? DEFAULT_CIRCUIT_DIR;
|
|
265
|
+
const vkeyPath = path.join(circuitDir, 'Delegation_groth16_vkey.json');
|
|
266
|
+
if (!fs.existsSync(vkeyPath)) {
|
|
267
|
+
throw new CircuitArtifactNotFoundError(vkeyPath, 'vkey');
|
|
268
|
+
}
|
|
269
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
270
|
+
const vkey = require(vkeyPath);
|
|
271
|
+
|
|
272
|
+
const valid = await snarkjs.groth16.verify(vkey, proof.publicSignals, proof.proof);
|
|
273
|
+
if (!valid) {
|
|
274
|
+
throw new VerificationError('Delegation proof failed Groth16 verification.');
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
newScopeCommitment: BigInt(proof.publicSignals[PUBSIG_NEW_SCOPE]),
|
|
279
|
+
delegationNullifier: BigInt(proof.publicSignals[PUBSIG_NULLIFIER]),
|
|
280
|
+
delegateeMerkleRoot: BigInt(proof.publicSignals[PUBSIG_DELEGATEE_ROOT]),
|
|
281
|
+
hopIndex: 0,
|
|
282
|
+
};
|
|
45
283
|
}
|