@arcanetech/privacy-sdk-stellar 0.4.1 → 0.5.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.
@@ -701,6 +701,111 @@ function resolveZkConfigNonce(transactEnvironment) {
701
701
  return transactEnvironment.zkConfigNonce ?? DEFAULT_ZK_CONFIG_NONCE;
702
702
  }
703
703
 
704
+ // src/transact/zk/browser-artifact-cache.ts
705
+ import { ZK_SDK_PACKAGE_VERSION } from "@arcanetech/stellar-privacy-pool-zk-sdk";
706
+ var ZK_ARTIFACT_CACHE_NAME = `privacy-sdk-stellar-zk-artifacts:${ZK_SDK_PACKAGE_VERSION}`;
707
+ function isBrowserCacheApiAvailable() {
708
+ return browserCacheStorage() !== void 0;
709
+ }
710
+ function readCaches() {
711
+ return globalThis.caches;
712
+ }
713
+ function browserCacheStorage() {
714
+ const cachesValue = readCaches();
715
+ if (cachesValue === void 0 || cachesValue === null) {
716
+ return void 0;
717
+ }
718
+ return cachesValue;
719
+ }
720
+ function absoluteArtifactUrl(url) {
721
+ if (/^[a-z][a-z0-9+.-]*:/i.test(url)) {
722
+ return url;
723
+ }
724
+ const href = globalThis.location?.href;
725
+ if (href === void 0) {
726
+ return url;
727
+ }
728
+ return new URL(url, href).href;
729
+ }
730
+ async function rememberResponse(url, response) {
731
+ const storage = browserCacheStorage();
732
+ if (storage === void 0) {
733
+ return;
734
+ }
735
+ try {
736
+ const cache = await storage.open(ZK_ARTIFACT_CACHE_NAME);
737
+ await cache.put(url, response.clone());
738
+ } catch {
739
+ return;
740
+ }
741
+ }
742
+ async function fetchNetworkArrayBuffer(url) {
743
+ const response = await fetch(url);
744
+ if (!response.ok) {
745
+ throw new Error(`Failed to fetch ZK artifact ${url}: ${String(response.status)}`);
746
+ }
747
+ await rememberResponse(url, response);
748
+ return response.arrayBuffer();
749
+ }
750
+ async function fetchArrayBufferWithBrowserCache(url) {
751
+ const requestUrl = absoluteArtifactUrl(url);
752
+ const storage = browserCacheStorage();
753
+ if (storage !== void 0) {
754
+ const cache = await storage.open(ZK_ARTIFACT_CACHE_NAME);
755
+ const cached = await cache.match(requestUrl);
756
+ if (cached?.ok === true) {
757
+ return cached.arrayBuffer();
758
+ }
759
+ }
760
+ return fetchNetworkArrayBuffer(requestUrl);
761
+ }
762
+
763
+ // src/transact/zk/prefetch-bundled-circuit.ts
764
+ import {
765
+ bundledCircuitFileNames,
766
+ defaultZkArtifactBaseUrl
767
+ } from "@arcanetech/stellar-privacy-pool-zk-sdk";
768
+ function resolveArtifactBaseUrl(explicit) {
769
+ const trimmed = explicit?.trim();
770
+ if (trimmed === void 0 || trimmed === "") {
771
+ return defaultZkArtifactBaseUrl();
772
+ }
773
+ let withoutSlash = trimmed;
774
+ while (withoutSlash.endsWith("/")) {
775
+ withoutSlash = withoutSlash.slice(0, -1);
776
+ }
777
+ return withoutSlash;
778
+ }
779
+ function artifactHref(base, fileName) {
780
+ const withSlash = `${base}/`;
781
+ if (/^https?:\/\//i.test(withSlash) || withSlash.startsWith("file:")) {
782
+ return new URL(fileName, withSlash).href;
783
+ }
784
+ const href = globalThis.location?.href;
785
+ if (href !== void 0) {
786
+ return new URL(fileName, new URL(withSlash, href)).href;
787
+ }
788
+ return `${withSlash}${fileName}`;
789
+ }
790
+ async function materializeBundledCircuitWithBrowserCache(definition, zkArtifactBaseUrl) {
791
+ const base = resolveArtifactBaseUrl(zkArtifactBaseUrl);
792
+ const names = bundledCircuitFileNames(definition.circuit);
793
+ const [circuitGraph, r1cs, provingKey] = await Promise.all([
794
+ fetchArrayBufferWithBrowserCache(artifactHref(base, names.graphFileName)),
795
+ fetchArrayBufferWithBrowserCache(artifactHref(base, names.r1csFileName)),
796
+ fetchArrayBufferWithBrowserCache(artifactHref(base, names.provingKeyFileName))
797
+ ]);
798
+ return {
799
+ nIns: definition.nIns,
800
+ nOuts: definition.nOuts,
801
+ publicNInputs: definition.publicNInputs,
802
+ publicNOutputs: definition.publicNOutputs,
803
+ circuitGraph,
804
+ r1cs,
805
+ provingKey
806
+ };
807
+ }
808
+
704
809
  // src/transact/zk/circuit-config.ts
705
810
  import { BundledZkCircuit as BundledZkCircuit2 } from "@arcanetech/stellar-privacy-pool-zk-sdk";
706
811
  var DEFAULT_STELLAR_ZK_CIRCUITS = {
@@ -722,31 +827,23 @@ var DEFAULT_STELLAR_ZK_CIRCUITS = {
722
827
  function isBundledDefinition(definition) {
723
828
  return "circuit" in definition;
724
829
  }
725
- async function fetchBuffer(url) {
726
- const response = await fetch(url);
727
- if (!response.ok) {
728
- throw new Error(`Failed to fetch ZK artifact ${url}: ${String(response.status)}`);
729
- }
730
- return response.arrayBuffer();
830
+ function bundledConfig(definition) {
831
+ return {
832
+ nIns: definition.nIns,
833
+ nOuts: definition.nOuts,
834
+ publicNInputs: definition.publicNInputs,
835
+ publicNOutputs: definition.publicNOutputs,
836
+ circuit: definition.circuit
837
+ };
731
838
  }
732
- async function materializeDefinition(definition) {
733
- if (isBundledDefinition(definition)) {
734
- const bundled = {
735
- nIns: definition.nIns,
736
- nOuts: definition.nOuts,
737
- publicNInputs: definition.publicNInputs,
738
- publicNOutputs: definition.publicNOutputs,
739
- circuit: definition.circuit
740
- };
741
- return bundled;
742
- }
839
+ async function materializeCustomDefinition(definition) {
743
840
  const [zkey, circuitGraph, r1cs, provingKey] = await Promise.all([
744
- fetchBuffer(definition.zkeyUrl),
745
- fetchBuffer(definition.circuitGraphUrl),
746
- fetchBuffer(definition.r1csUrl),
747
- fetchBuffer(definition.provingKeyUrl)
841
+ fetchArrayBufferWithBrowserCache(definition.zkeyUrl),
842
+ fetchArrayBufferWithBrowserCache(definition.circuitGraphUrl),
843
+ fetchArrayBufferWithBrowserCache(definition.r1csUrl),
844
+ fetchArrayBufferWithBrowserCache(definition.provingKeyUrl)
748
845
  ]);
749
- const custom = {
846
+ return {
750
847
  nIns: definition.nIns,
751
848
  nOuts: definition.nOuts,
752
849
  publicNInputs: definition.publicNInputs,
@@ -756,16 +853,24 @@ async function materializeDefinition(definition) {
756
853
  r1cs,
757
854
  provingKey
758
855
  };
759
- return custom;
760
856
  }
761
- async function materializeSelectedZkCircuit(circuits, nonce) {
857
+ async function materializeDefinition(definition, zkArtifactBaseUrl) {
858
+ if (!isBundledDefinition(definition)) {
859
+ return materializeCustomDefinition(definition);
860
+ }
861
+ if (isBrowserCacheApiAvailable()) {
862
+ return materializeBundledCircuitWithBrowserCache(definition, zkArtifactBaseUrl);
863
+ }
864
+ return bundledConfig(definition);
865
+ }
866
+ async function materializeSelectedZkCircuit(circuits, nonce, zkArtifactBaseUrl) {
762
867
  const map = stellarCircuitsOrDefault(circuits);
763
868
  const key = nonce.toString();
764
869
  const definition = Object.entries(map).find(([nonceKey]) => nonceKey === key)?.[1];
765
870
  if (!definition) {
766
871
  throw new Error(`no zk circuit configured for nonce ${key}`);
767
872
  }
768
- return { [key]: await materializeDefinition(definition) };
873
+ return { [key]: await materializeDefinition(definition, zkArtifactBaseUrl) };
769
874
  }
770
875
  function stellarCircuitsOrDefault(circuits) {
771
876
  return circuits ?? DEFAULT_STELLAR_ZK_CIRCUITS;
@@ -881,7 +986,8 @@ var PrivacyPoolService = class {
881
986
  const zkConfigNonce = this.zkConfigNonce ?? DEFAULT_ZK_CONFIG_NONCE;
882
987
  const zkCircuits = await materializeSelectedZkCircuit(
883
988
  this.zkCircuits,
884
- zkConfigNonce
989
+ zkConfigNonce,
990
+ this.zkArtifactBaseUrl
885
991
  );
886
992
  this.sdk = await PrivacyPoolSDK2.init({
887
993
  wasmBinary: this.assets.sdkWasm,