@sabaaa1/common 0.0.11 → 0.0.12
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/assets/index-B4znXaQD.js +1377 -0
- package/assets/{snarkjsWorkerLauncher-C7k0eivz.js → snarkjsWorkerLauncher-Bi0P4lHT.js} +1 -1
- package/assets/{snarkjsWorkerLogic-BO6Ynqjj.js → snarkjsWorkerLogic-B_n9w-4s.js} +14966 -16301
- package/data-structures/tor/TorClient.cjs +1 -1
- package/data-structures/tor/TorClient.mjs +67 -65
- package/package.json +1 -1
- package/webworker/snarkjsWorker/snarkjsWorkerLauncher.cjs +1 -1
- package/webworker/snarkjsWorker/snarkjsWorkerLauncher.mjs +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";var E=Object.create;var p=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var k=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty;var N=(o,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of x(t))!P.call(o,i)&&i!==e&&p(o,i,{get:()=>t[i],enumerable:!(r=_(t,i))||r.enumerable});return o};var z=(o,t,e)=>(e=o!=null?E(k(o)):{},N(t||!o||!o.__esModule?p(e,"default",{value:o,enumerable:!0}):e,o));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@hazae41/echalote"),C=require("@hazae41/cadenas"),L=require("@hazae41/fleche"),b=require("@hazae41/ed25519"),g=require("@hazae41/x25519"),y=require("@hazae41/chacha20poly1305"),O=require("@hazae41/sha1"),q=require("./WebSocketDuplex.cjs"),c=require("../../error-handling/logger.cjs");require("../../types/circom-data.types.cjs");require("../../types/transactions.types.cjs");require("../../types/activities.types.cjs");const I=require("../../types/routing.types.cjs"),A=require("../../functions/utils/serialize.utils.cjs"),B=require("buffer");typeof globalThis.Buffer>"u"&&(globalThis.Buffer=B.Buffer);class w{static initializationPromise;cryptoInitialized=!1;socket;tor;consensus;abortController;middles=[];exits=[];circuits=[];circuitIndex=0;CIRCUIT_POOL_SIZE=5;TOP_RELAY_COUNT=50;static async create(){return this.initializationPromise?this.initializationPromise:(this.initializationPromise=(async()=>{try{const t=new w;return await t.initialize(),t}catch(t){throw this.initializationPromise=void 0,t}})(),this.initializationPromise)}async initialize(){await this.initializeCryptoOrThrow(),this.socket=await this.createSnowflakeSocketOrThrow(),this.tor=await this.createTorClientOrThrow(this.socket),await this.loadConsensus();const t=Array(this.CIRCUIT_POOL_SIZE).fill(null).map((r,i)=>this.createNewCircuit().catch(s=>(c.Logger.error(`Failed to build initial circuit ${i}`,s),null))),e=await Promise.all(t);if(this.circuits=e.filter(r=>r!==null),this.circuits.length===0)throw new Error("Failed to build any circuits")}async initializeCryptoOrThrow(){if(this.cryptoInitialized)return;const{WalletWasm:t}=await import("@brumewallet/wallet.wasm");await t.initBundled(),O.Sha1.set(O.Sha1.fromWasm(t)),b.Ed25519.set(await b.Ed25519.fromNativeOrWasm(t)),g.X25519.set(g.X25519.fromWasm(t)),y.ChaCha20Poly1305.set(y.ChaCha20Poly1305.fromWasm(t)),this.cryptoInitialized=!0}async createSnowflakeSocketOrThrow(){const t=new WebSocket("wss://snowflake.torproject.net/");return t.binaryType="arraybuffer",await new Promise((e,r)=>{t.onopen=e,t.onerror=r}),t}async createTorClientOrThrow(t){const e=new q.WebSocketDuplex(t),r=u.createSnowflakeStream(e),i=new u.TorClientDuplex;return this.abortController=new AbortController,r.outer.readable.pipeTo(i.inner.writable,{signal:this.abortController.signal}).catch(s=>{c.Logger.error("Pipe to Tor failed:",s)}),i.inner.readable.pipeTo(r.outer.writable,{signal:this.abortController.signal}).catch(s=>{c.Logger.error("Pipe to TCP failed:",s)}),await i.waitOrThrow(),i}async loadConsensus(){if(!this.tor)throw new Error("Tor client not initialized");const t=await this.tor.createOrThrow();if(this.consensus=await u.Consensus.fetchOrThrow(t),this.middles=this.consensus.microdescs.filter(e=>e.flags.includes("Fast")&&e.flags.includes("Stable")&&e.flags.includes("V2Dir")),this.middles.sort((e,r)=>{const i=Number(e.bandwidth?.Bandwidth)||0;return(Number(r.bandwidth?.Bandwidth)||0)-i}),this.middles=this.middles.slice(0,this.TOP_RELAY_COUNT),this.exits=this.consensus.microdescs.filter(e=>e.flags.includes("Fast")&&e.flags.includes("Stable")&&e.flags.includes("Exit")&&!e.flags.includes("BadExit")),this.exits.sort((e,r)=>{const i=Number(e.bandwidth?.Bandwidth)||0;return(Number(r.bandwidth?.Bandwidth)||0)-i}),this.exits=this.exits.slice(0,this.TOP_RELAY_COUNT),this.middles.length===0||this.exits.length===0)throw new Error("No suitable relays found")}async createNewCircuit(){if(!this.tor)throw new Error("Tor client not initialized");const t=await this.tor.createOrThrow();try{const e=this.middles[Math.floor(Math.random()*this.middles.length)];if(!e)throw new Error("No middle relays available");const r=await u.Consensus.Microdesc.fetchOrThrow(t,e);await t.extendOrThrow(r);const i=this.exits[Math.floor(Math.random()*this.exits.length)];if(!i)throw new Error("No exit relays available");const s=await u.Consensus.Microdesc.fetchOrThrow(t,i);return await t.extendOrThrow(s),t}catch(e){throw c.Logger.error("Failed to build circuit",e),e}}async openTlsConnectionOrThrow(t,e,r=443){const i=await t.openOrThrow(e,r),s=[C.Ciphers.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,C.Ciphers.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384],a=new C.TlsClientDuplex({host_name:e,ciphers:s});return i.outer.readable.pipeTo(a.inner.writable).catch(()=>{}),a.inner.readable.pipeTo(i.outer.writable).catch(()=>{}),a.outer}async fetchViaTor(t,e,r,i){if(this.circuits.length===0)throw new Error("Circuit pool is empty, client may be closed or failed to init.");const s=new URL(e),a=this.circuits.length;let T;for(let f=0;f<a;f+=1){const l=(this.circuitIndex+f)%a,m=this.circuits[l];if(m)try{const d=await this.openTlsConnectionOrThrow(m,s.hostname,443),n={Host:s.hostname,Connection:"keep-alive",...i},h={method:t,stream:d,headers:n};r&&t!==I.HttpMethod.GET&&(h.body=typeof r=="string"?r:A.CustomJSONStringify(r),!n["Content-Type"]&&!n["content-type"]&&(n["Content-Type"]="application/json"));const S=await L.fetch(e,h);return this.circuitIndex=l,S}catch(d){T=d;const n=d.message?.toLowerCase()||"";c.Logger.error(`[TorClient] Circuit ${l} failed:`,n),(n.includes("circuit")||n.includes("destroyed")||n.includes("closed"))&&this.createNewCircuit().then(h=>{this.circuits[l]=h,c.Logger.log(`[TorClient] Replaced circuit ${l}`)}).catch(h=>{c.Logger.error(`[TorClient] Failed to replace circuit ${l}`,h)})}}throw new Error(`[TorClient] All ${a} circuits failed. Last error: ${T?.message||"unknown"}`)}async close(){this.abortController&&this.abortController.abort(),this.socket&&(this.socket.readyState===WebSocket.OPEN||this.socket.readyState===WebSocket.CONNECTING)&&this.socket.close(),this.circuits=[],this.socket=void 0,this.tor=void 0,this.consensus=void 0,this.abortController=void 0,w.initializationPromise=void 0}}exports.TorClient=w;
|
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import { createSnowflakeStream as
|
|
2
|
-
import { Ciphers as
|
|
3
|
-
import { fetch as
|
|
4
|
-
import { Ed25519 as
|
|
5
|
-
import { X25519 as
|
|
6
|
-
import { ChaCha20Poly1305 as
|
|
7
|
-
import { Sha1 as
|
|
8
|
-
import {
|
|
9
|
-
import { WebSocketDuplex as k } from "./WebSocketDuplex.mjs";
|
|
1
|
+
import { createSnowflakeStream as g, TorClientDuplex as S, Consensus as w } from "@hazae41/echalote";
|
|
2
|
+
import { Ciphers as p, TlsClientDuplex as E } from "@hazae41/cadenas";
|
|
3
|
+
import { fetch as _ } from "@hazae41/fleche";
|
|
4
|
+
import { Ed25519 as T } from "@hazae41/ed25519";
|
|
5
|
+
import { X25519 as C } from "@hazae41/x25519";
|
|
6
|
+
import { ChaCha20Poly1305 as b } from "@hazae41/chacha20poly1305";
|
|
7
|
+
import { Sha1 as y } from "@hazae41/sha1";
|
|
8
|
+
import { WebSocketDuplex as x } from "./WebSocketDuplex.mjs";
|
|
10
9
|
import { Logger as a } from "../../error-handling/logger.mjs";
|
|
11
10
|
import "../../types/circom-data.types.mjs";
|
|
12
11
|
import "../../types/transactions.types.mjs";
|
|
13
12
|
import "../../types/activities.types.mjs";
|
|
14
|
-
import { HttpMethod as
|
|
15
|
-
import { CustomJSONStringify as
|
|
16
|
-
import { Buffer as
|
|
17
|
-
typeof globalThis.Buffer > "u" && (globalThis.Buffer =
|
|
18
|
-
class
|
|
13
|
+
import { HttpMethod as k } from "../../types/routing.types.mjs";
|
|
14
|
+
import { CustomJSONStringify as P } from "../../functions/utils/serialize.utils.mjs";
|
|
15
|
+
import { Buffer as N } from "buffer";
|
|
16
|
+
typeof globalThis.Buffer > "u" && (globalThis.Buffer = N);
|
|
17
|
+
class u {
|
|
19
18
|
static initializationPromise;
|
|
20
19
|
cryptoInitialized = !1;
|
|
21
20
|
socket;
|
|
@@ -31,32 +30,35 @@ class m {
|
|
|
31
30
|
static async create() {
|
|
32
31
|
return this.initializationPromise ? this.initializationPromise : (this.initializationPromise = (async () => {
|
|
33
32
|
try {
|
|
34
|
-
const
|
|
35
|
-
return await
|
|
36
|
-
} catch (
|
|
37
|
-
throw this.initializationPromise = void 0,
|
|
33
|
+
const t = new u();
|
|
34
|
+
return await t.initialize(), t;
|
|
35
|
+
} catch (t) {
|
|
36
|
+
throw this.initializationPromise = void 0, t;
|
|
38
37
|
}
|
|
39
38
|
})(), this.initializationPromise);
|
|
40
39
|
}
|
|
41
40
|
async initialize() {
|
|
42
41
|
await this.initializeCryptoOrThrow(), this.socket = await this.createSnowflakeSocketOrThrow(), this.tor = await this.createTorClientOrThrow(this.socket), await this.loadConsensus();
|
|
43
|
-
const
|
|
42
|
+
const t = Array(this.CIRCUIT_POOL_SIZE).fill(null).map(
|
|
44
43
|
(r, e) => this.createNewCircuit().catch((o) => (a.error(`Failed to build initial circuit ${e}`, o), null))
|
|
45
|
-
),
|
|
46
|
-
if (this.circuits =
|
|
44
|
+
), i = await Promise.all(t);
|
|
45
|
+
if (this.circuits = i.filter((r) => r !== null), this.circuits.length === 0)
|
|
47
46
|
throw new Error("Failed to build any circuits");
|
|
48
47
|
}
|
|
49
48
|
async initializeCryptoOrThrow() {
|
|
50
|
-
|
|
49
|
+
if (this.cryptoInitialized)
|
|
50
|
+
return;
|
|
51
|
+
const { WalletWasm: t } = await import("@brumewallet/wallet.wasm");
|
|
52
|
+
await t.initBundled(), y.set(y.fromWasm(t)), T.set(await T.fromNativeOrWasm(t)), C.set(C.fromWasm(t)), b.set(b.fromWasm(t)), this.cryptoInitialized = !0;
|
|
51
53
|
}
|
|
52
54
|
async createSnowflakeSocketOrThrow() {
|
|
53
|
-
const
|
|
54
|
-
return
|
|
55
|
-
|
|
56
|
-
}),
|
|
55
|
+
const t = new WebSocket("wss://snowflake.torproject.net/");
|
|
56
|
+
return t.binaryType = "arraybuffer", await new Promise((i, r) => {
|
|
57
|
+
t.onopen = i, t.onerror = r;
|
|
58
|
+
}), t;
|
|
57
59
|
}
|
|
58
|
-
async createTorClientOrThrow(
|
|
59
|
-
const
|
|
60
|
+
async createTorClientOrThrow(t) {
|
|
61
|
+
const i = new x(t), r = g(i), e = new S();
|
|
60
62
|
return this.abortController = new AbortController(), r.outer.readable.pipeTo(e.inner.writable, { signal: this.abortController.signal }).catch((o) => {
|
|
61
63
|
a.error("Pipe to Tor failed:", o);
|
|
62
64
|
}), e.inner.readable.pipeTo(r.outer.writable, { signal: this.abortController.signal }).catch((o) => {
|
|
@@ -66,16 +68,16 @@ class m {
|
|
|
66
68
|
async loadConsensus() {
|
|
67
69
|
if (!this.tor)
|
|
68
70
|
throw new Error("Tor client not initialized");
|
|
69
|
-
const
|
|
70
|
-
if (this.consensus = await
|
|
71
|
-
(
|
|
72
|
-
), this.middles.sort((
|
|
73
|
-
const e = Number(
|
|
71
|
+
const t = await this.tor.createOrThrow();
|
|
72
|
+
if (this.consensus = await w.fetchOrThrow(t), this.middles = this.consensus.microdescs.filter(
|
|
73
|
+
(i) => i.flags.includes("Fast") && i.flags.includes("Stable") && i.flags.includes("V2Dir")
|
|
74
|
+
), this.middles.sort((i, r) => {
|
|
75
|
+
const e = Number(i.bandwidth?.Bandwidth) || 0;
|
|
74
76
|
return (Number(r.bandwidth?.Bandwidth) || 0) - e;
|
|
75
77
|
}), this.middles = this.middles.slice(0, this.TOP_RELAY_COUNT), this.exits = this.consensus.microdescs.filter(
|
|
76
|
-
(
|
|
77
|
-
), this.exits.sort((
|
|
78
|
-
const e = Number(
|
|
78
|
+
(i) => i.flags.includes("Fast") && i.flags.includes("Stable") && i.flags.includes("Exit") && !i.flags.includes("BadExit")
|
|
79
|
+
), this.exits.sort((i, r) => {
|
|
80
|
+
const e = Number(i.bandwidth?.Bandwidth) || 0;
|
|
79
81
|
return (Number(r.bandwidth?.Bandwidth) || 0) - e;
|
|
80
82
|
}), this.exits = this.exits.slice(0, this.TOP_RELAY_COUNT), this.middles.length === 0 || this.exits.length === 0)
|
|
81
83
|
throw new Error("No suitable relays found");
|
|
@@ -83,52 +85,52 @@ class m {
|
|
|
83
85
|
async createNewCircuit() {
|
|
84
86
|
if (!this.tor)
|
|
85
87
|
throw new Error("Tor client not initialized");
|
|
86
|
-
const
|
|
88
|
+
const t = await this.tor.createOrThrow();
|
|
87
89
|
try {
|
|
88
|
-
const
|
|
89
|
-
if (!
|
|
90
|
+
const i = this.middles[Math.floor(Math.random() * this.middles.length)];
|
|
91
|
+
if (!i)
|
|
90
92
|
throw new Error("No middle relays available");
|
|
91
|
-
const r = await
|
|
92
|
-
await
|
|
93
|
+
const r = await w.Microdesc.fetchOrThrow(t, i);
|
|
94
|
+
await t.extendOrThrow(r);
|
|
93
95
|
const e = this.exits[Math.floor(Math.random() * this.exits.length)];
|
|
94
96
|
if (!e)
|
|
95
97
|
throw new Error("No exit relays available");
|
|
96
|
-
const o = await
|
|
97
|
-
return await
|
|
98
|
-
} catch (
|
|
99
|
-
throw a.error("Failed to build circuit",
|
|
98
|
+
const o = await w.Microdesc.fetchOrThrow(t, e);
|
|
99
|
+
return await t.extendOrThrow(o), t;
|
|
100
|
+
} catch (i) {
|
|
101
|
+
throw a.error("Failed to build circuit", i), i;
|
|
100
102
|
}
|
|
101
103
|
}
|
|
102
|
-
async openTlsConnectionOrThrow(
|
|
103
|
-
const e = await
|
|
104
|
+
async openTlsConnectionOrThrow(t, i, r = 443) {
|
|
105
|
+
const e = await t.openOrThrow(i, r), o = [p.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, p.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384], n = new E({ host_name: i, ciphers: o });
|
|
104
106
|
return e.outer.readable.pipeTo(n.inner.writable).catch(() => {
|
|
105
107
|
}), n.inner.readable.pipeTo(e.outer.writable).catch(() => {
|
|
106
108
|
}), n.outer;
|
|
107
109
|
}
|
|
108
|
-
async fetchViaTor(
|
|
110
|
+
async fetchViaTor(t, i, r, e) {
|
|
109
111
|
if (this.circuits.length === 0)
|
|
110
112
|
throw new Error("Circuit pool is empty, client may be closed or failed to init.");
|
|
111
|
-
const o = new URL(
|
|
112
|
-
let
|
|
113
|
-
for (let
|
|
114
|
-
const c = (this.circuitIndex +
|
|
115
|
-
if (
|
|
113
|
+
const o = new URL(i), n = this.circuits.length;
|
|
114
|
+
let m;
|
|
115
|
+
for (let d = 0; d < n; d += 1) {
|
|
116
|
+
const c = (this.circuitIndex + d) % n, f = this.circuits[c];
|
|
117
|
+
if (f)
|
|
116
118
|
try {
|
|
117
|
-
const
|
|
119
|
+
const h = await this.openTlsConnectionOrThrow(f, o.hostname, 443), s = {
|
|
118
120
|
Host: o.hostname,
|
|
119
121
|
Connection: "keep-alive",
|
|
120
122
|
...e
|
|
121
123
|
}, l = {
|
|
122
|
-
method:
|
|
123
|
-
stream:
|
|
124
|
+
method: t,
|
|
125
|
+
stream: h,
|
|
124
126
|
headers: s
|
|
125
127
|
};
|
|
126
|
-
r &&
|
|
127
|
-
const
|
|
128
|
-
return this.circuitIndex = c,
|
|
129
|
-
} catch (
|
|
130
|
-
|
|
131
|
-
const s =
|
|
128
|
+
r && t !== k.GET && (l.body = typeof r == "string" ? r : P(r), !s["Content-Type"] && !s["content-type"] && (s["Content-Type"] = "application/json"));
|
|
129
|
+
const O = await _(i, l);
|
|
130
|
+
return this.circuitIndex = c, O;
|
|
131
|
+
} catch (h) {
|
|
132
|
+
m = h;
|
|
133
|
+
const s = h.message?.toLowerCase() || "";
|
|
132
134
|
a.error(`[TorClient] Circuit ${c} failed:`, s), (s.includes("circuit") || s.includes("destroyed") || s.includes("closed")) && this.createNewCircuit().then((l) => {
|
|
133
135
|
this.circuits[c] = l, a.log(`[TorClient] Replaced circuit ${c}`);
|
|
134
136
|
}).catch((l) => {
|
|
@@ -136,12 +138,12 @@ class m {
|
|
|
136
138
|
});
|
|
137
139
|
}
|
|
138
140
|
}
|
|
139
|
-
throw new Error(`[TorClient] All ${n} circuits failed. Last error: ${
|
|
141
|
+
throw new Error(`[TorClient] All ${n} circuits failed. Last error: ${m?.message || "unknown"}`);
|
|
140
142
|
}
|
|
141
143
|
async close() {
|
|
142
|
-
this.abortController && this.abortController.abort(), this.socket && (this.socket.readyState === WebSocket.OPEN || this.socket.readyState === WebSocket.CONNECTING) && this.socket.close(), this.circuits = [], this.socket = void 0, this.tor = void 0, this.consensus = void 0, this.abortController = void 0,
|
|
144
|
+
this.abortController && this.abortController.abort(), this.socket && (this.socket.readyState === WebSocket.OPEN || this.socket.readyState === WebSocket.CONNECTING) && this.socket.close(), this.circuits = [], this.socket = void 0, this.tor = void 0, this.consensus = void 0, this.abortController = void 0, u.initializationPromise = void 0;
|
|
143
145
|
}
|
|
144
146
|
}
|
|
145
147
|
export {
|
|
146
|
-
|
|
148
|
+
u as TorClient
|
|
147
149
|
};
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=""+(typeof document>"u"?require("url").pathToFileURL(__dirname+"/../../assets/snarkjsWorkerLauncher-
|
|
1
|
+
"use strict";const e=""+(typeof document>"u"?require("url").pathToFileURL(__dirname+"/../../assets/snarkjsWorkerLauncher-Bi0P4lHT.js").href:new URL("../../assets/snarkjsWorkerLauncher-Bi0P4lHT.js",document.currentScript&&document.currentScript.src||document.baseURI).href);module.exports=e;
|