@arcanetech/privacy-sdk-stellar 0.4.1 → 0.5.1

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/dist/node.d.ts CHANGED
@@ -210,7 +210,7 @@ interface StellarTransactEnvironment {
210
210
  zkCircuits?: Record<string, StellarZkCircuitDefinition>;
211
211
  /**
212
212
  * Directory or HTTP prefix for graph / r1cs / proving-key files.
213
- * Defaults to the GitHub release for `@arcanetech/stellar-privacy-pool-zk-sdk`.
213
+ * Defaults to the CDN prefix baked into `@arcanetech/stellar-privacy-pool-zk-sdk`.
214
214
  * Staging and local stands override this to serve files from disk.
215
215
  */
216
216
  zkArtifactBaseUrl?: string;
package/dist/node.js CHANGED
@@ -1148,6 +1148,111 @@ function resolveZkConfigNonce(transactEnvironment) {
1148
1148
  return transactEnvironment.zkConfigNonce ?? DEFAULT_ZK_CONFIG_NONCE;
1149
1149
  }
1150
1150
 
1151
+ // src/transact/zk/browser-artifact-cache.ts
1152
+ import { ZK_ARTIFACT_VERSION } from "@arcanetech/stellar-privacy-pool-zk-sdk";
1153
+ var ZK_ARTIFACT_CACHE_NAME = `privacy-sdk-stellar-zk-artifacts:${ZK_ARTIFACT_VERSION}`;
1154
+ function isBrowserCacheApiAvailable() {
1155
+ return browserCacheStorage() !== void 0;
1156
+ }
1157
+ function readCaches() {
1158
+ return globalThis.caches;
1159
+ }
1160
+ function browserCacheStorage() {
1161
+ const cachesValue = readCaches();
1162
+ if (cachesValue === void 0 || cachesValue === null) {
1163
+ return void 0;
1164
+ }
1165
+ return cachesValue;
1166
+ }
1167
+ function absoluteArtifactUrl(url) {
1168
+ if (/^[a-z][a-z0-9+.-]*:/i.test(url)) {
1169
+ return url;
1170
+ }
1171
+ const href = globalThis.location?.href;
1172
+ if (href === void 0) {
1173
+ return url;
1174
+ }
1175
+ return new URL(url, href).href;
1176
+ }
1177
+ async function rememberResponse(url, response) {
1178
+ const storage = browserCacheStorage();
1179
+ if (storage === void 0) {
1180
+ return;
1181
+ }
1182
+ try {
1183
+ const cache = await storage.open(ZK_ARTIFACT_CACHE_NAME);
1184
+ await cache.put(url, response.clone());
1185
+ } catch {
1186
+ return;
1187
+ }
1188
+ }
1189
+ async function fetchNetworkArrayBuffer(url) {
1190
+ const response = await fetch(url);
1191
+ if (!response.ok) {
1192
+ throw new Error(`Failed to fetch ZK artifact ${url}: ${String(response.status)}`);
1193
+ }
1194
+ await rememberResponse(url, response);
1195
+ return response.arrayBuffer();
1196
+ }
1197
+ async function fetchArrayBufferWithBrowserCache(url) {
1198
+ const requestUrl = absoluteArtifactUrl(url);
1199
+ const storage = browserCacheStorage();
1200
+ if (storage !== void 0) {
1201
+ const cache = await storage.open(ZK_ARTIFACT_CACHE_NAME);
1202
+ const cached = await cache.match(requestUrl);
1203
+ if (cached?.ok === true) {
1204
+ return cached.arrayBuffer();
1205
+ }
1206
+ }
1207
+ return fetchNetworkArrayBuffer(requestUrl);
1208
+ }
1209
+
1210
+ // src/transact/zk/prefetch-bundled-circuit.ts
1211
+ import {
1212
+ bundledCircuitFileNames,
1213
+ defaultZkArtifactBaseUrl
1214
+ } from "@arcanetech/stellar-privacy-pool-zk-sdk";
1215
+ function resolveArtifactBaseUrl(explicit) {
1216
+ const trimmed = explicit?.trim();
1217
+ if (trimmed === void 0 || trimmed === "") {
1218
+ return defaultZkArtifactBaseUrl();
1219
+ }
1220
+ let withoutSlash = trimmed;
1221
+ while (withoutSlash.endsWith("/")) {
1222
+ withoutSlash = withoutSlash.slice(0, -1);
1223
+ }
1224
+ return withoutSlash;
1225
+ }
1226
+ function artifactHref(base, fileName) {
1227
+ const withSlash = `${base}/`;
1228
+ if (/^https?:\/\//i.test(withSlash) || withSlash.startsWith("file:")) {
1229
+ return new URL(fileName, withSlash).href;
1230
+ }
1231
+ const href = globalThis.location?.href;
1232
+ if (href !== void 0) {
1233
+ return new URL(fileName, new URL(withSlash, href)).href;
1234
+ }
1235
+ return `${withSlash}${fileName}`;
1236
+ }
1237
+ async function materializeBundledCircuitWithBrowserCache(definition, zkArtifactBaseUrl) {
1238
+ const base = resolveArtifactBaseUrl(zkArtifactBaseUrl);
1239
+ const names = bundledCircuitFileNames(definition.circuit);
1240
+ const [circuitGraph, r1cs, provingKey] = await Promise.all([
1241
+ fetchArrayBufferWithBrowserCache(artifactHref(base, names.graphFileName)),
1242
+ fetchArrayBufferWithBrowserCache(artifactHref(base, names.r1csFileName)),
1243
+ fetchArrayBufferWithBrowserCache(artifactHref(base, names.provingKeyFileName))
1244
+ ]);
1245
+ return {
1246
+ nIns: definition.nIns,
1247
+ nOuts: definition.nOuts,
1248
+ publicNInputs: definition.publicNInputs,
1249
+ publicNOutputs: definition.publicNOutputs,
1250
+ circuitGraph,
1251
+ r1cs,
1252
+ provingKey
1253
+ };
1254
+ }
1255
+
1151
1256
  // src/transact/zk/circuit-config.ts
1152
1257
  import { BundledZkCircuit as BundledZkCircuit2 } from "@arcanetech/stellar-privacy-pool-zk-sdk";
1153
1258
  var DEFAULT_STELLAR_ZK_CIRCUITS = {
@@ -1169,31 +1274,23 @@ var DEFAULT_STELLAR_ZK_CIRCUITS = {
1169
1274
  function isBundledDefinition(definition) {
1170
1275
  return "circuit" in definition;
1171
1276
  }
1172
- async function fetchBuffer(url) {
1173
- const response = await fetch(url);
1174
- if (!response.ok) {
1175
- throw new Error(`Failed to fetch ZK artifact ${url}: ${String(response.status)}`);
1176
- }
1177
- return response.arrayBuffer();
1277
+ function bundledConfig(definition) {
1278
+ return {
1279
+ nIns: definition.nIns,
1280
+ nOuts: definition.nOuts,
1281
+ publicNInputs: definition.publicNInputs,
1282
+ publicNOutputs: definition.publicNOutputs,
1283
+ circuit: definition.circuit
1284
+ };
1178
1285
  }
1179
- async function materializeDefinition(definition) {
1180
- if (isBundledDefinition(definition)) {
1181
- const bundled = {
1182
- nIns: definition.nIns,
1183
- nOuts: definition.nOuts,
1184
- publicNInputs: definition.publicNInputs,
1185
- publicNOutputs: definition.publicNOutputs,
1186
- circuit: definition.circuit
1187
- };
1188
- return bundled;
1189
- }
1286
+ async function materializeCustomDefinition(definition) {
1190
1287
  const [zkey, circuitGraph, r1cs, provingKey] = await Promise.all([
1191
- fetchBuffer(definition.zkeyUrl),
1192
- fetchBuffer(definition.circuitGraphUrl),
1193
- fetchBuffer(definition.r1csUrl),
1194
- fetchBuffer(definition.provingKeyUrl)
1288
+ fetchArrayBufferWithBrowserCache(definition.zkeyUrl),
1289
+ fetchArrayBufferWithBrowserCache(definition.circuitGraphUrl),
1290
+ fetchArrayBufferWithBrowserCache(definition.r1csUrl),
1291
+ fetchArrayBufferWithBrowserCache(definition.provingKeyUrl)
1195
1292
  ]);
1196
- const custom = {
1293
+ return {
1197
1294
  nIns: definition.nIns,
1198
1295
  nOuts: definition.nOuts,
1199
1296
  publicNInputs: definition.publicNInputs,
@@ -1203,16 +1300,24 @@ async function materializeDefinition(definition) {
1203
1300
  r1cs,
1204
1301
  provingKey
1205
1302
  };
1206
- return custom;
1207
1303
  }
1208
- async function materializeSelectedZkCircuit(circuits, nonce) {
1304
+ async function materializeDefinition(definition, zkArtifactBaseUrl) {
1305
+ if (!isBundledDefinition(definition)) {
1306
+ return materializeCustomDefinition(definition);
1307
+ }
1308
+ if (isBrowserCacheApiAvailable()) {
1309
+ return materializeBundledCircuitWithBrowserCache(definition, zkArtifactBaseUrl);
1310
+ }
1311
+ return bundledConfig(definition);
1312
+ }
1313
+ async function materializeSelectedZkCircuit(circuits, nonce, zkArtifactBaseUrl) {
1209
1314
  const map = stellarCircuitsOrDefault(circuits);
1210
1315
  const key = nonce.toString();
1211
1316
  const definition = Object.entries(map).find(([nonceKey]) => nonceKey === key)?.[1];
1212
1317
  if (!definition) {
1213
1318
  throw new Error(`no zk circuit configured for nonce ${key}`);
1214
1319
  }
1215
- return { [key]: await materializeDefinition(definition) };
1320
+ return { [key]: await materializeDefinition(definition, zkArtifactBaseUrl) };
1216
1321
  }
1217
1322
  function stellarCircuitsOrDefault(circuits) {
1218
1323
  return circuits ?? DEFAULT_STELLAR_ZK_CIRCUITS;
@@ -1328,7 +1433,8 @@ var PrivacyPoolService = class {
1328
1433
  const zkConfigNonce = this.zkConfigNonce ?? DEFAULT_ZK_CONFIG_NONCE;
1329
1434
  const zkCircuits = await materializeSelectedZkCircuit(
1330
1435
  this.zkCircuits,
1331
- zkConfigNonce
1436
+ zkConfigNonce,
1437
+ this.zkArtifactBaseUrl
1332
1438
  );
1333
1439
  this.sdk = await PrivacyPoolSDK2.init({
1334
1440
  wasmBinary: this.assets.sdkWasm,
@@ -6545,7 +6651,8 @@ async function privacyPoolInitOptions(assets, transactEnvironment) {
6545
6651
  const zkConfigNonce = resolveZkConfigNonce(transactEnvironment);
6546
6652
  const zkCircuits = await materializeSelectedZkCircuit(
6547
6653
  transactEnvironment.zkCircuits,
6548
- zkConfigNonce
6654
+ zkConfigNonce,
6655
+ transactEnvironment.zkArtifactBaseUrl
6549
6656
  );
6550
6657
  return {
6551
6658
  wasmBinary: assets.sdkWasm,