@reclaimprotocol/attestor-core 5.0.1-beta.1 → 5.0.1-beta.2
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/lib/avs/abis/avsDirectoryABI.d.ts +60 -0
- package/lib/avs/abis/avsDirectoryABI.js +343 -0
- package/lib/avs/abis/delegationABI.d.ts +126 -0
- package/lib/avs/abis/delegationABI.js +4 -0
- package/lib/avs/abis/registryABI.d.ts +136 -0
- package/lib/avs/abis/registryABI.js +728 -0
- package/lib/avs/client/create-claim-on-avs.d.ts +12 -0
- package/lib/avs/client/create-claim-on-avs.js +168 -0
- package/lib/avs/config.d.ts +7 -0
- package/lib/avs/config.js +26 -0
- package/lib/avs/contracts/ReclaimServiceManager.d.ts +601 -0
- package/lib/avs/contracts/ReclaimServiceManager.js +0 -0
- package/lib/avs/contracts/common.d.ts +50 -0
- package/lib/avs/contracts/common.js +0 -0
- package/lib/avs/contracts/factories/ReclaimServiceManager__factory.d.ts +890 -0
- package/lib/avs/contracts/factories/ReclaimServiceManager__factory.js +1183 -0
- package/lib/avs/contracts/factories/index.d.ts +1 -0
- package/lib/avs/contracts/factories/index.js +4 -0
- package/lib/avs/contracts/index.d.ts +3 -0
- package/lib/avs/contracts/index.js +6 -0
- package/lib/avs/types/index.d.ts +55 -0
- package/lib/avs/types/index.js +0 -0
- package/lib/avs/utils/contracts.d.ts +21 -0
- package/lib/avs/utils/contracts.js +53 -0
- package/lib/avs/utils/register.d.ts +27 -0
- package/lib/avs/utils/register.js +74 -0
- package/lib/avs/utils/tasks.d.ts +22 -0
- package/lib/avs/utils/tasks.js +48 -0
- package/lib/client/create-claim.d.ts +5 -0
- package/lib/client/create-claim.js +461 -0
- package/lib/client/index.d.ts +3 -0
- package/lib/client/index.js +3 -0
- package/lib/client/tunnels/make-rpc-tcp-tunnel.d.ts +16 -0
- package/lib/client/tunnels/make-rpc-tcp-tunnel.js +53 -0
- package/lib/client/tunnels/make-rpc-tls-tunnel.d.ts +26 -0
- package/lib/client/tunnels/make-rpc-tls-tunnel.js +127 -0
- package/lib/client/utils/attestor-pool.d.ts +8 -0
- package/lib/client/utils/attestor-pool.js +24 -0
- package/lib/client/utils/client-socket.d.ts +11 -0
- package/lib/client/utils/client-socket.js +120 -0
- package/lib/client/utils/message-handler.d.ts +4 -0
- package/lib/client/utils/message-handler.js +97 -0
- package/lib/config/index.d.ts +31 -0
- package/lib/config/index.js +62 -0
- package/lib/external-rpc/benchmark.d.ts +1 -0
- package/lib/external-rpc/benchmark.js +82 -0
- package/lib/external-rpc/event-bus.d.ts +7 -0
- package/lib/external-rpc/event-bus.js +17 -0
- package/lib/external-rpc/global.d.js +0 -0
- package/lib/external-rpc/handle-incoming-msg.d.ts +2 -0
- package/lib/external-rpc/handle-incoming-msg.js +241 -0
- package/lib/external-rpc/index.d.ts +3 -0
- package/lib/external-rpc/index.js +3 -0
- package/lib/external-rpc/jsc-polyfills/1.d.ts +14 -0
- package/lib/external-rpc/jsc-polyfills/1.js +80 -0
- package/lib/external-rpc/jsc-polyfills/2.d.ts +1 -0
- package/lib/external-rpc/jsc-polyfills/2.js +15 -0
- package/lib/external-rpc/jsc-polyfills/event.d.ts +10 -0
- package/lib/external-rpc/jsc-polyfills/event.js +19 -0
- package/lib/external-rpc/jsc-polyfills/index.d.ts +2 -0
- package/lib/external-rpc/jsc-polyfills/index.js +2 -0
- package/lib/external-rpc/jsc-polyfills/ws.d.ts +21 -0
- package/lib/external-rpc/jsc-polyfills/ws.js +83 -0
- package/lib/external-rpc/setup-browser.d.ts +6 -0
- package/lib/external-rpc/setup-browser.js +33 -0
- package/lib/external-rpc/setup-jsc.d.ts +24 -0
- package/lib/external-rpc/setup-jsc.js +22 -0
- package/lib/external-rpc/types.d.ts +213 -0
- package/lib/external-rpc/types.js +0 -0
- package/lib/external-rpc/utils.d.ts +20 -0
- package/lib/external-rpc/utils.js +100 -0
- package/lib/external-rpc/zk.d.ts +14 -0
- package/lib/external-rpc/zk.js +58 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +13 -0
- package/lib/mechain/abis/governanceABI.d.ts +50 -0
- package/lib/mechain/abis/governanceABI.js +461 -0
- package/lib/mechain/abis/taskABI.d.ts +157 -0
- package/lib/mechain/abis/taskABI.js +512 -0
- package/lib/mechain/client/create-claim-on-mechain.d.ts +10 -0
- package/lib/mechain/client/create-claim-on-mechain.js +33 -0
- package/lib/mechain/client/index.d.ts +1 -0
- package/lib/mechain/client/index.js +1 -0
- package/lib/mechain/constants/index.d.ts +3 -0
- package/lib/mechain/constants/index.js +8 -0
- package/lib/mechain/index.d.ts +2 -0
- package/lib/mechain/index.js +2 -0
- package/lib/mechain/types/index.d.ts +23 -0
- package/lib/mechain/types/index.js +0 -0
- package/lib/proto/api.d.ts +651 -0
- package/lib/proto/api.js +4250 -0
- package/lib/proto/tee-bundle.d.ts +156 -0
- package/lib/proto/tee-bundle.js +1296 -0
- package/lib/providers/http/index.d.ts +18 -0
- package/lib/providers/http/index.js +640 -0
- package/lib/providers/http/patch-parse5-tree.d.ts +6 -0
- package/lib/providers/http/patch-parse5-tree.js +34 -0
- package/lib/providers/http/utils.d.ts +77 -0
- package/lib/providers/http/utils.js +283 -0
- package/lib/providers/index.d.ts +4 -0
- package/lib/providers/index.js +7 -0
- package/lib/scripts/build-browser.d.ts +1 -0
- package/lib/scripts/build-jsc.d.ts +1 -0
- package/lib/scripts/build-lib.d.ts +1 -0
- package/lib/scripts/check-avs-registration.d.ts +1 -0
- package/lib/scripts/check-avs-registration.js +28 -0
- package/lib/scripts/fallbacks/crypto.d.ts +1 -0
- package/lib/scripts/fallbacks/crypto.js +4 -0
- package/lib/scripts/fallbacks/empty.d.ts +3 -0
- package/lib/scripts/fallbacks/empty.js +4 -0
- package/lib/scripts/fallbacks/re2.d.ts +1 -0
- package/lib/scripts/fallbacks/re2.js +7 -0
- package/lib/scripts/fallbacks/snarkjs.d.ts +1 -0
- package/lib/scripts/fallbacks/snarkjs.js +10 -0
- package/lib/scripts/fallbacks/stwo.d.ts +6 -0
- package/lib/scripts/fallbacks/stwo.js +159 -0
- package/lib/scripts/generate-provider-types.d.ts +5 -0
- package/lib/scripts/generate-provider-types.js +101 -0
- package/lib/scripts/generate-receipt.d.ts +9 -0
- package/lib/scripts/generate-receipt.js +101 -0
- package/lib/scripts/generate-toprf-keys.d.ts +1 -0
- package/lib/scripts/generate-toprf-keys.js +24 -0
- package/lib/scripts/jsc-cli-rpc.d.ts +1 -0
- package/lib/scripts/jsc-cli-rpc.js +35 -0
- package/lib/scripts/register-avs-operator.d.ts +1 -0
- package/lib/scripts/register-avs-operator.js +3 -0
- package/lib/scripts/start-server.d.ts +1 -0
- package/lib/scripts/start-server.js +11 -0
- package/lib/scripts/update-avs-metadata.d.ts +1 -0
- package/lib/scripts/update-avs-metadata.js +20 -0
- package/lib/scripts/utils.d.ts +1 -0
- package/lib/scripts/utils.js +10 -0
- package/lib/scripts/whitelist-operator.d.ts +1 -0
- package/lib/scripts/whitelist-operator.js +16 -0
- package/lib/server/create-server.d.ts +8 -0
- package/lib/server/create-server.js +105 -0
- package/lib/server/handlers/claimTeeBundle.d.ts +6 -0
- package/lib/server/handlers/claimTeeBundle.js +232 -0
- package/lib/server/handlers/claimTunnel.d.ts +2 -0
- package/lib/server/handlers/claimTunnel.js +80 -0
- package/lib/server/handlers/completeClaimOnChain.d.ts +2 -0
- package/lib/server/handlers/completeClaimOnChain.js +29 -0
- package/lib/server/handlers/createClaimOnChain.d.ts +2 -0
- package/lib/server/handlers/createClaimOnChain.js +32 -0
- package/lib/server/handlers/createTaskOnMechain.d.ts +2 -0
- package/lib/server/handlers/createTaskOnMechain.js +57 -0
- package/lib/server/handlers/createTunnel.d.ts +2 -0
- package/lib/server/handlers/createTunnel.js +98 -0
- package/lib/server/handlers/disconnectTunnel.d.ts +2 -0
- package/lib/server/handlers/disconnectTunnel.js +8 -0
- package/lib/server/handlers/fetchCertificateBytes.d.ts +2 -0
- package/lib/server/handlers/fetchCertificateBytes.js +57 -0
- package/lib/server/handlers/index.d.ts +4 -0
- package/lib/server/handlers/index.js +25 -0
- package/lib/server/handlers/init.d.ts +2 -0
- package/lib/server/handlers/init.js +33 -0
- package/lib/server/handlers/toprf.d.ts +2 -0
- package/lib/server/handlers/toprf.js +19 -0
- package/lib/server/index.d.ts +4 -0
- package/lib/server/index.js +4 -0
- package/lib/server/socket.d.ts +13 -0
- package/lib/server/socket.js +112 -0
- package/lib/server/tunnels/make-tcp-tunnel.d.ts +22 -0
- package/lib/server/tunnels/make-tcp-tunnel.js +202 -0
- package/lib/server/utils/apm.d.ts +11 -0
- package/lib/server/utils/apm.js +29 -0
- package/lib/server/utils/assert-valid-claim-request.d.ts +31 -0
- package/lib/server/utils/assert-valid-claim-request.js +354 -0
- package/lib/server/utils/config-env.d.ts +1 -0
- package/lib/server/utils/config-env.js +4 -0
- package/lib/server/utils/dns.d.ts +1 -0
- package/lib/server/utils/dns.js +24 -0
- package/lib/server/utils/gcp-attestation.d.ts +17 -0
- package/lib/server/utils/gcp-attestation.js +237 -0
- package/lib/server/utils/generics.d.ts +22 -0
- package/lib/server/utils/generics.js +45 -0
- package/lib/server/utils/iso.d.ts +1 -0
- package/lib/server/utils/iso.js +259 -0
- package/lib/server/utils/keep-alive.d.ts +7 -0
- package/lib/server/utils/keep-alive.js +38 -0
- package/lib/server/utils/nitro-attestation.d.ts +33 -0
- package/lib/server/utils/nitro-attestation.js +249 -0
- package/lib/server/utils/oprf-raw.d.ts +21 -0
- package/lib/server/utils/oprf-raw.js +61 -0
- package/lib/server/utils/process-handshake.d.ts +13 -0
- package/lib/server/utils/process-handshake.js +233 -0
- package/lib/server/utils/proxy-session.d.ts +1 -0
- package/lib/server/utils/proxy-session.js +6 -0
- package/lib/server/utils/tee-oprf-mpc-verification.d.ts +16 -0
- package/lib/server/utils/tee-oprf-mpc-verification.js +86 -0
- package/lib/server/utils/tee-oprf-verification.d.ts +24 -0
- package/lib/server/utils/tee-oprf-verification.js +151 -0
- package/lib/server/utils/tee-transcript-reconstruction.d.ts +24 -0
- package/lib/server/utils/tee-transcript-reconstruction.js +140 -0
- package/lib/server/utils/tee-verification.d.ts +28 -0
- package/lib/server/utils/tee-verification.js +358 -0
- package/lib/server/utils/validation.d.ts +2 -0
- package/lib/server/utils/validation.js +45 -0
- package/lib/types/bgp.d.ts +11 -0
- package/lib/types/bgp.js +0 -0
- package/lib/types/claims.d.ts +70 -0
- package/lib/types/claims.js +0 -0
- package/lib/types/client.d.ts +163 -0
- package/lib/types/client.js +0 -0
- package/lib/types/general.d.ts +76 -0
- package/lib/types/general.js +0 -0
- package/lib/types/handlers.d.ts +10 -0
- package/lib/types/handlers.js +0 -0
- package/lib/types/index.d.ts +10 -0
- package/lib/types/index.js +10 -0
- package/lib/types/providers.d.ts +161 -0
- package/lib/types/providers.gen.d.ts +443 -0
- package/lib/types/providers.gen.js +16 -0
- package/lib/types/providers.js +0 -0
- package/lib/types/rpc.d.ts +35 -0
- package/lib/types/rpc.js +0 -0
- package/lib/types/signatures.d.ts +28 -0
- package/lib/types/signatures.js +0 -0
- package/lib/types/tunnel.d.ts +18 -0
- package/lib/types/tunnel.js +0 -0
- package/lib/types/zk.d.ts +38 -0
- package/lib/types/zk.js +0 -0
- package/lib/utils/auth.d.ts +8 -0
- package/lib/utils/auth.js +71 -0
- package/lib/utils/b64-json.d.ts +2 -0
- package/lib/utils/b64-json.js +17 -0
- package/lib/utils/bgp-listener.d.ts +7 -0
- package/lib/utils/bgp-listener.js +123 -0
- package/lib/utils/claims.d.ts +33 -0
- package/lib/utils/claims.js +89 -0
- package/lib/utils/env.d.ts +3 -0
- package/lib/utils/env.js +19 -0
- package/lib/utils/error.d.ts +26 -0
- package/lib/utils/error.js +54 -0
- package/lib/utils/generics.d.ts +114 -0
- package/lib/utils/generics.js +268 -0
- package/lib/utils/http-parser.d.ts +59 -0
- package/lib/utils/http-parser.js +201 -0
- package/lib/utils/index.d.ts +13 -0
- package/lib/utils/index.js +13 -0
- package/lib/utils/logger.d.ts +13 -0
- package/lib/utils/logger.js +82 -0
- package/lib/utils/prepare-packets.d.ts +16 -0
- package/lib/utils/prepare-packets.js +69 -0
- package/lib/utils/redactions.d.ts +73 -0
- package/lib/utils/redactions.js +135 -0
- package/lib/utils/retries.d.ts +12 -0
- package/lib/utils/retries.js +26 -0
- package/lib/utils/signatures/eth.d.ts +2 -0
- package/lib/utils/signatures/eth.js +31 -0
- package/lib/utils/signatures/index.d.ts +5 -0
- package/lib/utils/signatures/index.js +12 -0
- package/lib/utils/socket-base.d.ts +23 -0
- package/lib/utils/socket-base.js +96 -0
- package/lib/utils/tls.d.ts +2 -0
- package/lib/utils/tls.js +58 -0
- package/lib/utils/ws.d.ts +7 -0
- package/lib/utils/ws.js +22 -0
- package/lib/utils/zk.d.ts +71 -0
- package/lib/utils/zk.js +625 -0
- package/package.json +2 -2
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { getBytes } from "ethers";
|
|
2
|
+
import { getAttestorAddress } from "../../server/utils/generics.js";
|
|
3
|
+
import { assertValidAuthRequest } from "../../utils/auth.js";
|
|
4
|
+
import { getEnvVariable } from "../../utils/env.js";
|
|
5
|
+
import { AttestorError } from "../../utils/index.js";
|
|
6
|
+
import { SIGNATURES } from "../../utils/signatures/index.js";
|
|
7
|
+
const TOPRF_PUBLIC_KEY = getEnvVariable("TOPRF_PUBLIC_KEY");
|
|
8
|
+
const init = async (initRequest, { client }) => {
|
|
9
|
+
if (client.isInitialised) {
|
|
10
|
+
throw AttestorError.badRequest("Client already initialised");
|
|
11
|
+
}
|
|
12
|
+
if (!SIGNATURES[initRequest.signatureType]) {
|
|
13
|
+
throw AttestorError.badRequest("Unsupported signature type");
|
|
14
|
+
}
|
|
15
|
+
if (initRequest.clientVersion <= 0) {
|
|
16
|
+
throw AttestorError.badRequest("Unsupported client version");
|
|
17
|
+
}
|
|
18
|
+
await assertValidAuthRequest(initRequest.auth, initRequest.signatureType);
|
|
19
|
+
if (initRequest.auth?.data) {
|
|
20
|
+
client.logger = client.logger.child({
|
|
21
|
+
userId: initRequest.auth.data.id
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
client.metadata = initRequest;
|
|
25
|
+
client.isInitialised = true;
|
|
26
|
+
return {
|
|
27
|
+
toprfPublicKey: TOPRF_PUBLIC_KEY ? getBytes(TOPRF_PUBLIC_KEY) : new Uint8Array(),
|
|
28
|
+
attestorAddress: getAttestorAddress(initRequest.signatureType)
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export {
|
|
32
|
+
init
|
|
33
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getBytes } from "ethers";
|
|
2
|
+
import { getEnvVariable } from "../../utils/env.js";
|
|
3
|
+
import { getEngineString, makeDefaultOPRFOperator } from "../../utils/index.js";
|
|
4
|
+
const toprf = async ({ maskedData, engine }, { logger }) => {
|
|
5
|
+
const PRIVATE_KEY_STR = getEnvVariable("TOPRF_SHARE_PRIVATE_KEY");
|
|
6
|
+
const PUBLIC_KEY_STR = getEnvVariable("TOPRF_SHARE_PUBLIC_KEY");
|
|
7
|
+
if (!PRIVATE_KEY_STR || !PUBLIC_KEY_STR) {
|
|
8
|
+
throw new Error("private/public keys not set. Cannot execute OPRF");
|
|
9
|
+
}
|
|
10
|
+
const PRIVATE_KEY = getBytes(PRIVATE_KEY_STR);
|
|
11
|
+
const PUBLIC_KEY = getBytes(PUBLIC_KEY_STR);
|
|
12
|
+
const engineStr = getEngineString(engine);
|
|
13
|
+
const operator = makeDefaultOPRFOperator("chacha20", engineStr, logger);
|
|
14
|
+
const res = await operator.evaluateOPRF(PRIVATE_KEY, maskedData);
|
|
15
|
+
return { ...res, publicKeyShare: PUBLIC_KEY };
|
|
16
|
+
};
|
|
17
|
+
export {
|
|
18
|
+
toprf
|
|
19
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { WebSocket as WS } from 'ws';
|
|
2
|
+
import type { TunnelMessage } from '#src/proto/api.ts';
|
|
3
|
+
import type { AcceptNewConnectionOpts, BGPListener, IAttestorServerSocket } from '#src/types/index.ts';
|
|
4
|
+
import { AttestorSocket } from '#src/utils/socket-base.ts';
|
|
5
|
+
export declare class AttestorServerSocket extends AttestorSocket implements IAttestorServerSocket {
|
|
6
|
+
tunnels: IAttestorServerSocket['tunnels'];
|
|
7
|
+
readonly sessionId: number;
|
|
8
|
+
readonly bgpListener: BGPListener | undefined;
|
|
9
|
+
private constructor();
|
|
10
|
+
getTunnel(tunnelId: number): import("#src/types/index.ts").Tunnel<import("#src/types/index.ts").TCPSocketProperties>;
|
|
11
|
+
removeTunnel(tunnelId: TunnelMessage['tunnelId']): void;
|
|
12
|
+
static acceptConnection(socket: WS, { req, logger, bgpListener }: AcceptNewConnectionOpts): Promise<AttestorServerSocket | undefined>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { promisify } from "util";
|
|
2
|
+
import { handleMessage } from "../client/utils/message-handler.js";
|
|
3
|
+
import { DEFAULT_RPC_TIMEOUT_MS } from "../config/index.js";
|
|
4
|
+
import { HANDLERS } from "../server/handlers/index.js";
|
|
5
|
+
import { getApm } from "../server/utils/apm.js";
|
|
6
|
+
import { getInitialMessagesFromQuery } from "../server/utils/generics.js";
|
|
7
|
+
import { AttestorError, generateSessionId } from "../utils/index.js";
|
|
8
|
+
import { AttestorSocket } from "../utils/socket-base.js";
|
|
9
|
+
class AttestorServerSocket extends AttestorSocket {
|
|
10
|
+
tunnels = {};
|
|
11
|
+
sessionId;
|
|
12
|
+
bgpListener;
|
|
13
|
+
constructor(socket, sessionId, bgpListener, logger) {
|
|
14
|
+
super(socket, {}, logger);
|
|
15
|
+
this.sessionId = sessionId;
|
|
16
|
+
this.bgpListener = bgpListener;
|
|
17
|
+
this.addEventListener("rpc-request", handleRpcRequest.bind(this));
|
|
18
|
+
this.addEventListener("tunnel-message", handleTunnelMessage.bind(this));
|
|
19
|
+
this.addEventListener("connection-terminated", () => {
|
|
20
|
+
for (const tunnelId in this.tunnels) {
|
|
21
|
+
const tunnel = this.tunnels[tunnelId];
|
|
22
|
+
void tunnel.close(new Error("WS session terminated"));
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
getTunnel(tunnelId) {
|
|
27
|
+
const tunnel = this.tunnels[tunnelId];
|
|
28
|
+
if (!tunnel) {
|
|
29
|
+
throw new AttestorError(
|
|
30
|
+
"ERROR_NOT_FOUND",
|
|
31
|
+
`Tunnel "${tunnelId}" not found`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
return tunnel;
|
|
35
|
+
}
|
|
36
|
+
removeTunnel(tunnelId) {
|
|
37
|
+
delete this.tunnels[tunnelId];
|
|
38
|
+
}
|
|
39
|
+
static async acceptConnection(socket, { req, logger, bgpListener }) {
|
|
40
|
+
const bindSend = socket.send.bind(socket);
|
|
41
|
+
socket.send = promisify(bindSend);
|
|
42
|
+
const sessionId = generateSessionId();
|
|
43
|
+
logger = logger.child({ sessionId });
|
|
44
|
+
const client = new AttestorServerSocket(
|
|
45
|
+
socket,
|
|
46
|
+
sessionId,
|
|
47
|
+
bgpListener,
|
|
48
|
+
logger
|
|
49
|
+
);
|
|
50
|
+
try {
|
|
51
|
+
const initMsgs = getInitialMessagesFromQuery(req);
|
|
52
|
+
logger.trace(
|
|
53
|
+
{ initMsgs: initMsgs.length },
|
|
54
|
+
"new connection, validating..."
|
|
55
|
+
);
|
|
56
|
+
for (const msg of initMsgs) {
|
|
57
|
+
await handleMessage.call(client, msg);
|
|
58
|
+
}
|
|
59
|
+
logger.debug("connection accepted");
|
|
60
|
+
} catch (err) {
|
|
61
|
+
logger.error({ err }, "error in new connection");
|
|
62
|
+
if (client.isOpen) {
|
|
63
|
+
await client.terminateConnection(
|
|
64
|
+
err instanceof AttestorError ? err : AttestorError.badRequest(err.message)
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
return client;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function handleTunnelMessage({ data: { tunnelId, message } }) {
|
|
73
|
+
try {
|
|
74
|
+
const tunnel = this.getTunnel(tunnelId);
|
|
75
|
+
await tunnel.write(message);
|
|
76
|
+
} catch (err) {
|
|
77
|
+
this.logger?.error({ err, tunnelId }, "error writing to tunnel");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async function handleRpcRequest({ data: { data, requestId, respond, type } }) {
|
|
81
|
+
const logger = this.logger.child({ rpc: type, requestId });
|
|
82
|
+
const apm = getApm();
|
|
83
|
+
const tx = apm?.startTransaction(type);
|
|
84
|
+
tx?.setLabel("requestId", requestId);
|
|
85
|
+
tx?.setLabel("sessionId", this.sessionId.toString());
|
|
86
|
+
const userId = this.metadata.auth?.data?.id;
|
|
87
|
+
if (userId) {
|
|
88
|
+
tx?.setLabel("authUserId", userId);
|
|
89
|
+
}
|
|
90
|
+
const timeout = setTimeout(() => {
|
|
91
|
+
logger.warn({ type, requestId }, "RPC took too long to respond");
|
|
92
|
+
}, DEFAULT_RPC_TIMEOUT_MS);
|
|
93
|
+
try {
|
|
94
|
+
logger.debug({ data }, "handling RPC request");
|
|
95
|
+
const handler = HANDLERS[type];
|
|
96
|
+
const res = await handler(data, { client: this, logger, tx });
|
|
97
|
+
respond(res);
|
|
98
|
+
logger.debug({ res }, "handled RPC request");
|
|
99
|
+
tx?.setOutcome("success");
|
|
100
|
+
} catch (err) {
|
|
101
|
+
logger.error({ err }, "error in RPC request");
|
|
102
|
+
respond(AttestorError.fromError(err));
|
|
103
|
+
tx?.setOutcome("failure");
|
|
104
|
+
apm?.captureError(err, { parent: tx });
|
|
105
|
+
} finally {
|
|
106
|
+
clearTimeout(timeout);
|
|
107
|
+
tx?.end();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
export {
|
|
111
|
+
AttestorServerSocket
|
|
112
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CreateTunnelRequest } from '#src/proto/api.ts';
|
|
2
|
+
import type { Logger } from '#src/types/index.ts';
|
|
3
|
+
import type { MakeTunnelFn, TCPSocketProperties } from '#src/types/index.ts';
|
|
4
|
+
type ExtraOpts = Omit<CreateTunnelRequest, 'id' | 'initialMessage'> & {
|
|
5
|
+
logger: Logger;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Builds a TCP tunnel to the given host and port.
|
|
9
|
+
* If a geolocation is provided -- an HTTPS proxy is used
|
|
10
|
+
* to connect to the host.
|
|
11
|
+
* If a proxySessionId is provided -- a static ip is used with HTTPS proxy
|
|
12
|
+
* across multiple requests with this same proxySessionId.
|
|
13
|
+
*
|
|
14
|
+
* HTTPS proxy essentially creates an opaque tunnel to the
|
|
15
|
+
* host using the CONNECT method. Any data can be sent through
|
|
16
|
+
* this tunnel to the end host.
|
|
17
|
+
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT
|
|
18
|
+
*
|
|
19
|
+
* The tunnel also retains a transcript of all messages sent and received.
|
|
20
|
+
*/
|
|
21
|
+
export declare const makeTcpTunnel: MakeTunnelFn<ExtraOpts, TCPSocketProperties>;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { HttpsProxyAgent } from "https-proxy-agent";
|
|
2
|
+
import { Socket } from "net";
|
|
3
|
+
import { CONNECTION_TIMEOUT_MS } from "../../config/index.js";
|
|
4
|
+
import { resolveHostnames } from "../../server/utils/dns.js";
|
|
5
|
+
import { isValidCountryCode } from "../../server/utils/iso.js";
|
|
6
|
+
import { isValidProxySessionId } from "../../server/utils/proxy-session.js";
|
|
7
|
+
import { getEnvVariable } from "../../utils/env.js";
|
|
8
|
+
import { AttestorError } from "../../utils/index.js";
|
|
9
|
+
const HTTPS_PROXY_URL = getEnvVariable("HTTPS_PROXY_URL");
|
|
10
|
+
const makeTcpTunnel = async ({
|
|
11
|
+
onClose,
|
|
12
|
+
onMessage,
|
|
13
|
+
logger,
|
|
14
|
+
...opts
|
|
15
|
+
}) => {
|
|
16
|
+
const transcript = [];
|
|
17
|
+
const socket = await connectTcp({ ...opts, logger });
|
|
18
|
+
let closed = false;
|
|
19
|
+
socket.on("data", (message) => {
|
|
20
|
+
if (closed) {
|
|
21
|
+
logger.warn("socket is closed, dropping message");
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
onMessage?.(message);
|
|
25
|
+
transcript.push({ sender: "server", message });
|
|
26
|
+
});
|
|
27
|
+
socket.once("close", () => onSocketClose(void 0));
|
|
28
|
+
return {
|
|
29
|
+
socket,
|
|
30
|
+
transcript,
|
|
31
|
+
createRequest: opts,
|
|
32
|
+
async write(data) {
|
|
33
|
+
transcript.push({ sender: "client", message: data });
|
|
34
|
+
await new Promise((resolve, reject) => {
|
|
35
|
+
socket.write(data, (err) => {
|
|
36
|
+
if (err) {
|
|
37
|
+
reject(err);
|
|
38
|
+
} else {
|
|
39
|
+
resolve();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
close(err) {
|
|
45
|
+
if (closed) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
socket.destroy(err);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
function onSocketClose(err) {
|
|
52
|
+
if (closed) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
logger.debug({ err }, "closing socket");
|
|
56
|
+
closed = true;
|
|
57
|
+
onClose?.(err);
|
|
58
|
+
onClose = void 0;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
async function connectTcp({ host, port, geoLocation, proxySessionId, logger }) {
|
|
62
|
+
let connectTimeout;
|
|
63
|
+
let socket;
|
|
64
|
+
try {
|
|
65
|
+
await new Promise(async (resolve, reject) => {
|
|
66
|
+
try {
|
|
67
|
+
connectTimeout = setTimeout(
|
|
68
|
+
() => reject(
|
|
69
|
+
new AttestorError(
|
|
70
|
+
"ERROR_NETWORK_ERROR",
|
|
71
|
+
"Server connection timed out"
|
|
72
|
+
)
|
|
73
|
+
),
|
|
74
|
+
CONNECTION_TIMEOUT_MS
|
|
75
|
+
);
|
|
76
|
+
socket = await getSocket({
|
|
77
|
+
host,
|
|
78
|
+
port,
|
|
79
|
+
geoLocation,
|
|
80
|
+
proxySessionId,
|
|
81
|
+
logger
|
|
82
|
+
});
|
|
83
|
+
socket.once("connect", resolve);
|
|
84
|
+
socket.once("error", reject);
|
|
85
|
+
socket.once("end", () => reject(
|
|
86
|
+
new AttestorError(
|
|
87
|
+
"ERROR_NETWORK_ERROR",
|
|
88
|
+
"connection closed"
|
|
89
|
+
)
|
|
90
|
+
));
|
|
91
|
+
} catch (err) {
|
|
92
|
+
reject(err);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
logger.debug({ addr: `${host}:${port}` }, "connected");
|
|
96
|
+
return socket;
|
|
97
|
+
} catch (err) {
|
|
98
|
+
socket?.end();
|
|
99
|
+
throw err;
|
|
100
|
+
} finally {
|
|
101
|
+
clearTimeout(connectTimeout);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async function getSocket(opts) {
|
|
105
|
+
const { logger } = opts;
|
|
106
|
+
try {
|
|
107
|
+
return await _getSocket(opts);
|
|
108
|
+
} catch (err) {
|
|
109
|
+
if (!(err instanceof AttestorError) || err.data?.code !== 403) {
|
|
110
|
+
throw err;
|
|
111
|
+
}
|
|
112
|
+
const addrs = await resolveHostnames(opts.host);
|
|
113
|
+
logger.info(
|
|
114
|
+
{ addrs, host: opts.host },
|
|
115
|
+
"failed to connect due to restricted IP, trying via raw addr"
|
|
116
|
+
);
|
|
117
|
+
for (const addr of addrs) {
|
|
118
|
+
try {
|
|
119
|
+
return await _getSocket({ ...opts, host: addr });
|
|
120
|
+
} catch (err2) {
|
|
121
|
+
logger.error(
|
|
122
|
+
{ addr, err: err2 },
|
|
123
|
+
"failed to connect to host"
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
throw err;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async function _getSocket({
|
|
131
|
+
host,
|
|
132
|
+
port,
|
|
133
|
+
geoLocation,
|
|
134
|
+
proxySessionId,
|
|
135
|
+
logger
|
|
136
|
+
}) {
|
|
137
|
+
const socket = new Socket();
|
|
138
|
+
if ((proxySessionId || geoLocation) && !HTTPS_PROXY_URL) {
|
|
139
|
+
logger.warn(
|
|
140
|
+
{ geoLocation, proxySessionId },
|
|
141
|
+
"geoLocation or proxySessionId provided but no proxy URL found"
|
|
142
|
+
);
|
|
143
|
+
geoLocation = "";
|
|
144
|
+
proxySessionId = "";
|
|
145
|
+
}
|
|
146
|
+
if (!geoLocation && !proxySessionId) {
|
|
147
|
+
socket.connect({ host, port });
|
|
148
|
+
return socket;
|
|
149
|
+
}
|
|
150
|
+
if (!isValidCountryCode(geoLocation)) {
|
|
151
|
+
throw AttestorError.badRequest(
|
|
152
|
+
`Geolocation "${geoLocation}" is invalid. Must be 2 letter ISO country code`,
|
|
153
|
+
{ geoLocation }
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
if (proxySessionId && !isValidProxySessionId(proxySessionId)) {
|
|
157
|
+
throw AttestorError.badRequest(
|
|
158
|
+
`proxySessionId "${proxySessionId}" is invalid. Must be a lowercase alphanumeric string of length 8-14 characters. eg. "mystring12345", "something1234".`,
|
|
159
|
+
{ proxySessionId }
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
const agentUrl = HTTPS_PROXY_URL.replace(
|
|
163
|
+
"{{geoLocation}}",
|
|
164
|
+
geoLocation?.toLowerCase() || ""
|
|
165
|
+
).replace(
|
|
166
|
+
"{{proxySessionId}}",
|
|
167
|
+
proxySessionId ? `-session-${proxySessionId}` : ""
|
|
168
|
+
);
|
|
169
|
+
const agent = new HttpsProxyAgent(agentUrl);
|
|
170
|
+
const waitForProxyRes = new Promise((resolve) => {
|
|
171
|
+
socket.once("proxyConnect", resolve);
|
|
172
|
+
});
|
|
173
|
+
const proxySocket = await agent.connect(
|
|
174
|
+
// ignore, because https-proxy-agent
|
|
175
|
+
// expects an http request object
|
|
176
|
+
// @ts-ignore
|
|
177
|
+
socket,
|
|
178
|
+
{ host, port, timeout: CONNECTION_TIMEOUT_MS }
|
|
179
|
+
);
|
|
180
|
+
const res = await waitForProxyRes;
|
|
181
|
+
if (res.statusCode !== 200) {
|
|
182
|
+
logger.error(
|
|
183
|
+
{ geoLocation, proxySessionId, res },
|
|
184
|
+
"Proxy geo location or session id failed"
|
|
185
|
+
);
|
|
186
|
+
throw new AttestorError(
|
|
187
|
+
"ERROR_PROXY_ERROR",
|
|
188
|
+
`Proxy via ${geoLocation ? `geo location "${geoLocation}"` : ""}${geoLocation && proxySessionId ? ", or " : ""}${proxySessionId ? `session id "${proxySessionId}"` : ""} failed with status code: ${res.statusCode}, message: ${res.statusText}`,
|
|
189
|
+
{
|
|
190
|
+
code: res.statusCode,
|
|
191
|
+
message: res.statusText
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
process.nextTick(() => {
|
|
196
|
+
proxySocket.emit("connect");
|
|
197
|
+
});
|
|
198
|
+
return proxySocket;
|
|
199
|
+
}
|
|
200
|
+
export {
|
|
201
|
+
makeTcpTunnel
|
|
202
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Agent } from 'elastic-apm-node';
|
|
2
|
+
/**
|
|
3
|
+
* Initialises the APM agent if required,
|
|
4
|
+
* and returns it.
|
|
5
|
+
* If ELASTIC_APM_SERVER_URL & ELASTIC_APM_SECRET_TOKEN
|
|
6
|
+
* are not set will return undefined
|
|
7
|
+
*
|
|
8
|
+
* Utilises the standard env variables mentioned
|
|
9
|
+
* here: https://www.elastic.co/guide/en/apm/agent/nodejs/current/custom-stack.html#custom-stack-advanced-configuration
|
|
10
|
+
*/
|
|
11
|
+
export declare function getApm(): Agent | undefined;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import ElasticAPM from "elastic-apm-node";
|
|
2
|
+
import { getEnvVariable } from "../../utils/env.js";
|
|
3
|
+
import { logger } from "../../utils/logger.js";
|
|
4
|
+
let apm;
|
|
5
|
+
function getApm() {
|
|
6
|
+
if (!getEnvVariable("ELASTIC_APM_SERVER_URL") || !getEnvVariable("ELASTIC_APM_SECRET_TOKEN")) {
|
|
7
|
+
logger.info(
|
|
8
|
+
"ELASTIC_APM_SERVER_URL or ELASTIC_APM_SECRET_TOKEN not found in env, APM agent not initialised"
|
|
9
|
+
);
|
|
10
|
+
return void 0;
|
|
11
|
+
}
|
|
12
|
+
if (!apm) {
|
|
13
|
+
const sampleRate = +(getEnvVariable("ELASTIC_APM_SAMPLE_RATE") || "0.1");
|
|
14
|
+
apm = ElasticAPM.start({
|
|
15
|
+
serviceName: "reclaim_attestor",
|
|
16
|
+
serviceVersion: "4.0.0",
|
|
17
|
+
transactionSampleRate: sampleRate,
|
|
18
|
+
instrumentIncomingHTTPRequests: true,
|
|
19
|
+
usePathAsTransactionName: true,
|
|
20
|
+
instrument: true,
|
|
21
|
+
captureHeaders: true
|
|
22
|
+
});
|
|
23
|
+
logger.info("initialised APM agent");
|
|
24
|
+
}
|
|
25
|
+
return apm;
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
getApm
|
|
29
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ZKEngine } from '@reclaimprotocol/zk-symmetric-crypto';
|
|
2
|
+
import type { InitRequest, ProviderClaimInfo } from '#src/proto/api.ts';
|
|
3
|
+
import { ClaimTunnelRequest } from '#src/proto/api.ts';
|
|
4
|
+
import type { IDecryptedTranscript, Logger, OPRFRawReplacement, ProviderCtx, TCPSocketProperties, Transcript } from '#src/types/index.ts';
|
|
5
|
+
/**
|
|
6
|
+
* Asserts that the claim request is valid.
|
|
7
|
+
*
|
|
8
|
+
* 1. We begin by verifying the signature of the claim request.
|
|
9
|
+
* 2. Next, we produce the transcript of the TLS exchange
|
|
10
|
+
* from the proofs provided by the client.
|
|
11
|
+
* 3. We then pull the provider the client is trying to claim
|
|
12
|
+
* from
|
|
13
|
+
* 4. We then use the provider's verification function to verify
|
|
14
|
+
* whether the claim is valid.
|
|
15
|
+
*
|
|
16
|
+
* If any of these steps fail, we throw an error.
|
|
17
|
+
*/
|
|
18
|
+
export declare function assertValidClaimRequest(request: ClaimTunnelRequest, metadata: InitRequest, logger: Logger): Promise<import("#src/proto/api.ts").ClaimRequestData>;
|
|
19
|
+
/**
|
|
20
|
+
* Verify that the transcript contains a valid claim
|
|
21
|
+
* for the provider.
|
|
22
|
+
*/
|
|
23
|
+
export declare function assertValidProviderTranscript<T extends ProviderClaimInfo>(applData: Transcript<Uint8Array>, info: T, logger: Logger, providerCtx: ProviderCtx, oprfRawReplacements?: OPRFRawReplacement[]): Promise<T>;
|
|
24
|
+
/**
|
|
25
|
+
* Verify that the transcript provided by the client
|
|
26
|
+
* matches the transcript of the tunnel, the server
|
|
27
|
+
* has created.
|
|
28
|
+
*/
|
|
29
|
+
export declare function assertTranscriptsMatch(clientTranscript: ClaimTunnelRequest['transcript'], tunnelTranscript: TCPSocketProperties['transcript']): void;
|
|
30
|
+
export declare function decryptTranscript(transcript: ClaimTunnelRequest['transcript'], logger: Logger, zkEngine: ZKEngine, serverIV: Uint8Array, clientIV: Uint8Array): Promise<IDecryptedTranscript>;
|
|
31
|
+
export declare function getWithoutHeader(message: Uint8Array): Uint8Array<ArrayBuffer>;
|