@reclaimprotocol/attestor-core 5.0.4 → 5.0.5
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 +660 -660
- package/browser/resources/attestor-browser.min.mjs +31 -31
- package/lib/server/tee/acme-http-server.d.ts +13 -0
- package/lib/server/tee/attestation-generate.d.ts +29 -0
- package/lib/server/tee/bootstrap.d.ts +11 -0
- package/lib/server/tee/cert-manager.d.ts +24 -0
- package/lib/server/tee/cloud-logging.d.ts +23 -0
- package/lib/server/tee/secret-loader.d.ts +10 -0
- package/lib/server/tee/secret-manager.d.ts +3 -0
- package/lib/utils/gcp-attestation.d.ts +23 -0
- package/package.json +3 -3
- package/lib/avs/abis/avsDirectoryABI.js +0 -340
- package/lib/avs/abis/delegationABI.js +0 -1
- package/lib/avs/abis/registryABI.js +0 -725
- package/lib/avs/client/create-claim-on-avs.js +0 -138
- package/lib/avs/config.js +0 -20
- package/lib/avs/contracts/ReclaimServiceManager.js +0 -1
- package/lib/avs/contracts/common.js +0 -1
- package/lib/avs/contracts/factories/ReclaimServiceManager__factory.js +0 -1169
- package/lib/avs/contracts/factories/index.js +0 -4
- package/lib/avs/contracts/index.js +0 -2
- package/lib/avs/types/index.js +0 -1
- package/lib/avs/utils/contracts.js +0 -33
- package/lib/avs/utils/register.js +0 -78
- package/lib/avs/utils/tasks.js +0 -40
- package/lib/client/create-claim.js +0 -433
- package/lib/client/index.js +0 -3
- package/lib/client/tunnels/make-rpc-tcp-tunnel.js +0 -51
- package/lib/client/tunnels/make-rpc-tls-tunnel.js +0 -131
- package/lib/client/utils/attestor-pool.js +0 -25
- package/lib/client/utils/client-socket.js +0 -98
- package/lib/client/utils/message-handler.js +0 -87
- package/lib/config/index.js +0 -44
- package/lib/external-rpc/benchmark.js +0 -69
- package/lib/external-rpc/event-bus.js +0 -14
- package/lib/external-rpc/handle-incoming-msg.js +0 -233
- package/lib/external-rpc/jsc-polyfills/1.js +0 -82
- package/lib/external-rpc/jsc-polyfills/2.js +0 -20
- package/lib/external-rpc/jsc-polyfills/event.js +0 -14
- package/lib/external-rpc/jsc-polyfills/index.js +0 -2
- package/lib/external-rpc/jsc-polyfills/ws.js +0 -81
- package/lib/external-rpc/setup-browser.js +0 -33
- package/lib/external-rpc/setup-jsc.js +0 -22
- package/lib/external-rpc/types.js +0 -1
- package/lib/external-rpc/utils.js +0 -100
- package/lib/external-rpc/zk.js +0 -63
- package/lib/mechain/abis/governanceABI.js +0 -458
- package/lib/mechain/abis/taskABI.js +0 -509
- package/lib/mechain/client/create-claim-on-mechain.js +0 -28
- package/lib/mechain/client/index.js +0 -1
- package/lib/mechain/constants/index.js +0 -3
- package/lib/mechain/index.js +0 -2
- package/lib/mechain/types/index.js +0 -1
- package/lib/proto/api.js +0 -4273
- package/lib/proto/tee-bundle.js +0 -1316
- package/lib/providers/http/index.js +0 -658
- package/lib/providers/http/patch-parse5-tree.js +0 -33
- package/lib/providers/http/utils.js +0 -324
- package/lib/providers/index.js +0 -4
- package/lib/scripts/fetch-ec2-metadata.d.ts +0 -1
- package/lib/server/create-server.js +0 -103
- package/lib/server/handlers/claimTeeBundle.js +0 -252
- package/lib/server/handlers/claimTunnel.js +0 -73
- package/lib/server/handlers/completeClaimOnChain.js +0 -22
- package/lib/server/handlers/createClaimOnChain.js +0 -26
- package/lib/server/handlers/createTaskOnMechain.js +0 -47
- package/lib/server/handlers/createTunnel.js +0 -93
- package/lib/server/handlers/disconnectTunnel.js +0 -5
- package/lib/server/handlers/fetchCertificateBytes.js +0 -41
- package/lib/server/handlers/index.js +0 -22
- package/lib/server/handlers/init.js +0 -32
- package/lib/server/handlers/toprf.js +0 -16
- package/lib/server/index.js +0 -4
- package/lib/server/socket.js +0 -109
- package/lib/server/tunnels/make-tcp-tunnel.js +0 -177
- package/lib/server/utils/apm.js +0 -36
- package/lib/server/utils/assert-valid-claim-request.js +0 -204
- package/lib/server/utils/config-env.js +0 -4
- package/lib/server/utils/dns.js +0 -18
- package/lib/server/utils/gcp-attestation.js +0 -289
- package/lib/server/utils/generics.js +0 -51
- package/lib/server/utils/iso.js +0 -256
- package/lib/server/utils/keep-alive.js +0 -38
- package/lib/server/utils/nitro-attestation.js +0 -325
- package/lib/server/utils/process-handshake.js +0 -215
- package/lib/server/utils/proxy-session.js +0 -6
- package/lib/server/utils/tee-oprf-mpc-verification.js +0 -90
- package/lib/server/utils/tee-oprf-verification.js +0 -174
- package/lib/server/utils/tee-transcript-reconstruction.js +0 -187
- package/lib/server/utils/tee-verification.js +0 -421
- package/lib/server/utils/validation.js +0 -38
- package/lib/types/bgp.js +0 -1
- package/lib/types/claims.js +0 -1
- package/lib/types/client.js +0 -1
- package/lib/types/general.js +0 -1
- package/lib/types/handlers.js +0 -1
- package/lib/types/index.js +0 -10
- package/lib/types/providers.gen.js +0 -10
- package/lib/types/providers.js +0 -1
- package/lib/types/rpc.js +0 -1
- package/lib/types/signatures.js +0 -1
- package/lib/types/tunnel.js +0 -1
- package/lib/types/zk.js +0 -1
- package/lib/utils/auth.js +0 -59
- package/lib/utils/b64-json.js +0 -17
- package/lib/utils/bgp-listener.js +0 -119
- package/lib/utils/claims.js +0 -99
- package/lib/utils/env.js +0 -15
- package/lib/utils/error.js +0 -50
- package/lib/utils/generics.js +0 -317
- package/lib/utils/http-parser.js +0 -246
- package/lib/utils/index.js +0 -13
- package/lib/utils/logger.js +0 -91
- package/lib/utils/prepare-packets.js +0 -62
- package/lib/utils/redactions.js +0 -148
- package/lib/utils/retries.js +0 -24
- package/lib/utils/signatures/eth.js +0 -29
- package/lib/utils/signatures/index.js +0 -7
- package/lib/utils/socket-base.js +0 -90
- package/lib/utils/tls.js +0 -58
- package/lib/utils/ws.js +0 -22
- package/lib/utils/zk.js +0 -577
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { MAX_CLAIM_TIMESTAMP_DIFF_S } from "../../config/index.js";
|
|
2
|
-
import { ClaimTunnelResponse } from "../../proto/api.js";
|
|
3
|
-
import { getApm } from "../utils/apm.js";
|
|
4
|
-
import { assertTranscriptsMatch, assertValidClaimRequest } from "../utils/assert-valid-claim-request.js";
|
|
5
|
-
import { getAttestorAddress, signAsAttestor } from "../utils/generics.js";
|
|
6
|
-
import { AttestorError, createSignDataForClaim, getIdentifierFromClaimInfo, unixTimestampSeconds } from "../../utils/index.js";
|
|
7
|
-
export const claimTunnel = async (claimRequest, { tx, logger, client }) => {
|
|
8
|
-
const { request, data: { timestampS } = {}, } = claimRequest;
|
|
9
|
-
const tunnel = client.getTunnel(request?.id);
|
|
10
|
-
try {
|
|
11
|
-
await tunnel.close();
|
|
12
|
-
}
|
|
13
|
-
catch (err) {
|
|
14
|
-
logger.debug({ err }, 'error closing tunnel');
|
|
15
|
-
}
|
|
16
|
-
if (tx) {
|
|
17
|
-
const transcriptBytes = tunnel.transcript.reduce((acc, { message }) => acc + message.length, 0);
|
|
18
|
-
tx?.setLabel('transcriptBytes', transcriptBytes.toString());
|
|
19
|
-
}
|
|
20
|
-
// we throw an error for cases where the attestor cannot prove
|
|
21
|
-
// the user's request is faulty. For eg. if the user sends a
|
|
22
|
-
// "createRequest" that does not match the tunnel's actual
|
|
23
|
-
// create request -- the attestor cannot prove that the user
|
|
24
|
-
// is lying. In such cases, we throw a bad request error.
|
|
25
|
-
// Same goes for matching the transcript.
|
|
26
|
-
if (tunnel.createRequest?.host !== request?.host
|
|
27
|
-
|| tunnel.createRequest?.port !== request?.port
|
|
28
|
-
|| tunnel.createRequest?.geoLocation !== request?.geoLocation
|
|
29
|
-
|| tunnel.createRequest?.proxySessionId !== request?.proxySessionId) {
|
|
30
|
-
throw AttestorError.badRequest('Tunnel request does not match');
|
|
31
|
-
}
|
|
32
|
-
assertTranscriptsMatch(claimRequest.transcript, tunnel.transcript);
|
|
33
|
-
const res = ClaimTunnelResponse.create({ request: claimRequest });
|
|
34
|
-
try {
|
|
35
|
-
const now = unixTimestampSeconds();
|
|
36
|
-
if (Math.floor(timestampS - now) > MAX_CLAIM_TIMESTAMP_DIFF_S) {
|
|
37
|
-
throw new AttestorError('ERROR_INVALID_CLAIM', `Timestamp provided ${timestampS} is too far off. Current time is ${now}`);
|
|
38
|
-
}
|
|
39
|
-
const assertTx = getApm()
|
|
40
|
-
?.startTransaction('assertValidClaimRequest', { childOf: tx });
|
|
41
|
-
try {
|
|
42
|
-
const claim = await assertValidClaimRequest(claimRequest, client.metadata, logger);
|
|
43
|
-
res.claim = {
|
|
44
|
-
...claim,
|
|
45
|
-
identifier: getIdentifierFromClaimInfo(claim),
|
|
46
|
-
// hardcode for compatibility with V1 claims
|
|
47
|
-
epoch: 1
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
catch (err) {
|
|
51
|
-
assertTx?.setOutcome('failure');
|
|
52
|
-
throw err;
|
|
53
|
-
}
|
|
54
|
-
finally {
|
|
55
|
-
assertTx?.end();
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
catch (err) {
|
|
59
|
-
logger.error({ err }, 'invalid claim request');
|
|
60
|
-
const attestorErr = AttestorError.fromError(err, 'ERROR_INVALID_CLAIM');
|
|
61
|
-
res.error = attestorErr.toProto();
|
|
62
|
-
}
|
|
63
|
-
res.signatures = {
|
|
64
|
-
attestorAddress: getAttestorAddress(client.metadata.signatureType),
|
|
65
|
-
claimSignature: res.claim
|
|
66
|
-
? await signAsAttestor(createSignDataForClaim(res.claim), client.metadata.signatureType)
|
|
67
|
-
: new Uint8Array(),
|
|
68
|
-
resultSignature: await signAsAttestor(ClaimTunnelResponse.encode(res).finish(), client.metadata.signatureType)
|
|
69
|
-
};
|
|
70
|
-
// remove tunnel from client -- to free up our mem
|
|
71
|
-
client.removeTunnel(request.id);
|
|
72
|
-
return res;
|
|
73
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { getContracts } from "../../avs/utils/contracts.js";
|
|
2
|
-
import { getEnvVariable } from "../../utils/env.js";
|
|
3
|
-
import { AttestorError, ethersStructToPlainObject } from "../../utils/index.js";
|
|
4
|
-
const ACCEPT_CLAIM_PAYMENT_REQUESTS = getEnvVariable('ACCEPT_CLAIM_PAYMENT_REQUESTS') === '1';
|
|
5
|
-
export const completeClaimOnChain = async ({ chainId: chainIdNum, taskIndex, completedTaskJson }) => {
|
|
6
|
-
if (!ACCEPT_CLAIM_PAYMENT_REQUESTS) {
|
|
7
|
-
throw new AttestorError('ERROR_PAYMENT_REFUSED', 'Payment requests are not accepted at this time');
|
|
8
|
-
}
|
|
9
|
-
const chainId = chainIdNum.toString();
|
|
10
|
-
const { contract } = getContracts(chainId.toString());
|
|
11
|
-
const task = JSON.parse(completedTaskJson);
|
|
12
|
-
const tx = await contract.taskCompleted(task, taskIndex);
|
|
13
|
-
const rslt = await tx.wait();
|
|
14
|
-
// check task created event was emitted
|
|
15
|
-
const ev = rslt.events?.[0];
|
|
16
|
-
const obj = ev?.args;
|
|
17
|
-
const plainObj = ethersStructToPlainObject(obj);
|
|
18
|
-
return {
|
|
19
|
-
txHash: rslt.transactionHash,
|
|
20
|
-
taskCompletedObjectJson: JSON.stringify(plainObj)
|
|
21
|
-
};
|
|
22
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { getContracts } from "../../avs/utils/contracts.js";
|
|
2
|
-
import { createNewClaimRequestOnChain } from "../../avs/utils/tasks.js";
|
|
3
|
-
import { getEnvVariable } from "../../utils/env.js";
|
|
4
|
-
import { AttestorError, ethersStructToPlainObject } from "../../utils/index.js";
|
|
5
|
-
const ACCEPT_CLAIM_PAYMENT_REQUESTS = getEnvVariable('ACCEPT_CLAIM_PAYMENT_REQUESTS') === '1';
|
|
6
|
-
export const createClaimOnChain = async ({ chainId: chainIdNum, jsonCreateClaimRequest, requestSignature }) => {
|
|
7
|
-
if (!ACCEPT_CLAIM_PAYMENT_REQUESTS) {
|
|
8
|
-
throw new AttestorError('ERROR_PAYMENT_REFUSED', 'Payment requests are not accepted at this time');
|
|
9
|
-
}
|
|
10
|
-
const chainId = chainIdNum.toString();
|
|
11
|
-
const { wallet } = getContracts(chainId.toString());
|
|
12
|
-
const request = JSON.parse(jsonCreateClaimRequest);
|
|
13
|
-
const { task, tx } = await createNewClaimRequestOnChain({
|
|
14
|
-
request,
|
|
15
|
-
owner: request.owner,
|
|
16
|
-
payer: wallet,
|
|
17
|
-
chainId,
|
|
18
|
-
requestSignature: requestSignature
|
|
19
|
-
});
|
|
20
|
-
const plainTask = ethersStructToPlainObject(task);
|
|
21
|
-
return {
|
|
22
|
-
txHash: tx.transactionHash,
|
|
23
|
-
taskIndex: task.taskIndex,
|
|
24
|
-
jsonTask: JSON.stringify(plainTask)
|
|
25
|
-
};
|
|
26
|
-
};
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { Contract, providers, utils, Wallet } from 'ethers';
|
|
2
|
-
import { governanceABI } from "../../mechain/abis/governanceABI.js";
|
|
3
|
-
import { taskABI } from "../../mechain/abis/taskABI.js";
|
|
4
|
-
import { GOVERNANCE_CONTRACT_ADDRESS, RPC_URL, TASK_CONTRACT_ADDRESS } from "../../mechain/constants/index.js";
|
|
5
|
-
import { getEnvVariable } from "../../utils/env.js";
|
|
6
|
-
export const createTaskOnMechain = async ({ timestamp }) => {
|
|
7
|
-
const { taskContract } = await getContracts();
|
|
8
|
-
const seed = utils.randomBytes(32);
|
|
9
|
-
// Perform a static call to fetch taskId and attestors for the next task
|
|
10
|
-
const result = await taskContract.callStatic.createNewTaskRequest(seed, timestamp);
|
|
11
|
-
const taskId = result[0];
|
|
12
|
-
// Fetch requiredAttestors to determine how many proofs to request
|
|
13
|
-
const requiredAttestors = await taskContract.requiredAttestors();
|
|
14
|
-
const hosts = [];
|
|
15
|
-
// Fetch attestors's WebSocket URI, e.g. wss://attestor.reclaimprotocol.org:444/ws
|
|
16
|
-
for (let i = 0; i < requiredAttestors; i++) {
|
|
17
|
-
hosts.push(result[1][i].host);
|
|
18
|
-
}
|
|
19
|
-
// Perform the call that was statically-called previously
|
|
20
|
-
const tx = await taskContract.createNewTaskRequest(seed, timestamp);
|
|
21
|
-
await tx.wait();
|
|
22
|
-
return {
|
|
23
|
-
taskId: taskId,
|
|
24
|
-
requiredAttestors: requiredAttestors,
|
|
25
|
-
hosts: hosts
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
async function getContracts() {
|
|
29
|
-
const privateKey = getEnvVariable('MECHAIN_PRIVATE_KEY');
|
|
30
|
-
const taskContractAddress = getEnvVariable('TASK_CONTRACT_ADDRESS') || TASK_CONTRACT_ADDRESS;
|
|
31
|
-
const governanceContractAddress = getEnvVariable('GOVERNANCE_CONTRACT_ADDRESS') || GOVERNANCE_CONTRACT_ADDRESS;
|
|
32
|
-
if (!privateKey) {
|
|
33
|
-
throw new Error('MECHAIN_PRIVATE_KEY environment variable is not set');
|
|
34
|
-
}
|
|
35
|
-
try {
|
|
36
|
-
const provider = new providers.JsonRpcProvider(RPC_URL);
|
|
37
|
-
// Validate connection to provider
|
|
38
|
-
await provider.getNetwork();
|
|
39
|
-
const signer = new Wallet(privateKey, provider);
|
|
40
|
-
const taskContract = new Contract(taskContractAddress, taskABI, signer);
|
|
41
|
-
const governanceContract = new Contract(governanceContractAddress, governanceABI, signer);
|
|
42
|
-
return { taskContract, governanceContract };
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
throw new Error(`Failed to initialize contracts: ${error.message || error}`);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { makeTcpTunnel } from "../tunnels/make-tcp-tunnel.js";
|
|
2
|
-
import { getApm } from "../utils/apm.js";
|
|
3
|
-
import { resolveHostnames } from "../utils/dns.js";
|
|
4
|
-
import { AttestorError } from "../../utils/index.js";
|
|
5
|
-
export const createTunnel = async ({ id, ...opts }, { tx, logger, client }) => {
|
|
6
|
-
if (client.tunnels[id]) {
|
|
7
|
-
throw AttestorError.badRequest(`Tunnel "${id}" already exists`);
|
|
8
|
-
}
|
|
9
|
-
const allowedHosts = client.metadata?.auth?.data?.hostWhitelist;
|
|
10
|
-
if (allowedHosts?.length && !allowedHosts.includes(opts.host)) {
|
|
11
|
-
throw AttestorError.badRequest(`Host "${opts.host}" not allowed by auth request`);
|
|
12
|
-
}
|
|
13
|
-
let cancelBgp;
|
|
14
|
-
const apm = getApm();
|
|
15
|
-
const sessionTx = apm
|
|
16
|
-
?.startTransaction('tunnelConnection', { childOf: tx });
|
|
17
|
-
sessionTx?.setLabel('tunnelId', id.toString());
|
|
18
|
-
sessionTx?.setLabel('hostPort', `${opts.host}:${opts.port}`);
|
|
19
|
-
sessionTx?.setLabel('geoLocation', opts.geoLocation);
|
|
20
|
-
sessionTx?.setLabel('proxySessionId', opts.proxySessionId);
|
|
21
|
-
try {
|
|
22
|
-
const tunnel = await makeTcpTunnel({
|
|
23
|
-
...opts,
|
|
24
|
-
logger,
|
|
25
|
-
onMessage(message) {
|
|
26
|
-
if (!client.isOpen) {
|
|
27
|
-
logger.warn('client is closed, dropping message');
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
return client
|
|
31
|
-
.sendMessage({ tunnelMessage: { tunnelId: id, message } });
|
|
32
|
-
},
|
|
33
|
-
onClose(err) {
|
|
34
|
-
cancelBgp?.();
|
|
35
|
-
if (err) {
|
|
36
|
-
apm?.captureError(err, { parent: sessionTx });
|
|
37
|
-
sessionTx?.setOutcome('failure');
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
sessionTx?.setOutcome('success');
|
|
41
|
-
}
|
|
42
|
-
sessionTx?.end();
|
|
43
|
-
if (!client.isOpen) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
client.sendMessage({
|
|
47
|
-
tunnelDisconnectEvent: {
|
|
48
|
-
tunnelId: id,
|
|
49
|
-
error: err
|
|
50
|
-
? AttestorError
|
|
51
|
-
.fromError(err)
|
|
52
|
-
.toProto()
|
|
53
|
-
: undefined
|
|
54
|
-
}
|
|
55
|
-
})
|
|
56
|
-
.catch(err => {
|
|
57
|
-
logger.error({ err }, 'failed to send tunnel disconnect event');
|
|
58
|
-
});
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
try {
|
|
62
|
-
await checkForBgp(tunnel);
|
|
63
|
-
}
|
|
64
|
-
catch (err) {
|
|
65
|
-
logger.warn({ err, host: opts.host }, 'failed to start BGP overlap check');
|
|
66
|
-
}
|
|
67
|
-
client.tunnels[id] = tunnel;
|
|
68
|
-
return {};
|
|
69
|
-
}
|
|
70
|
-
catch (err) {
|
|
71
|
-
apm?.captureError(err, { parent: sessionTx });
|
|
72
|
-
sessionTx?.setOutcome('failure');
|
|
73
|
-
sessionTx?.end();
|
|
74
|
-
cancelBgp?.();
|
|
75
|
-
throw err;
|
|
76
|
-
}
|
|
77
|
-
async function checkForBgp(tunnel) {
|
|
78
|
-
if (!client.bgpListener) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
// listen to all IPs for the host -- in case any of them
|
|
82
|
-
// has a BGP announcement overlap, we'll close the tunnel
|
|
83
|
-
// so the user can retry
|
|
84
|
-
const ips = await resolveHostnames(opts.host);
|
|
85
|
-
cancelBgp = client.bgpListener.onOverlap(ips, (info) => {
|
|
86
|
-
logger.warn({ info, host: opts.host }, 'BGP announcement overlap detected');
|
|
87
|
-
// track how many times we've seen a BGP overlap
|
|
88
|
-
sessionTx?.addLabels({ bgpOverlap: true, ...info });
|
|
89
|
-
void tunnel?.close(new AttestorError('ERROR_BGP_ANNOUNCEMENT_OVERLAP', `BGP announcement overlap detected for ${opts.host}`));
|
|
90
|
-
});
|
|
91
|
-
logger.debug({ ips }, 'checking for BGP overlap');
|
|
92
|
-
}
|
|
93
|
-
};
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { concatenateUint8Arrays, loadX509FromPem } from '@reclaimprotocol/tls';
|
|
2
|
-
import { CERT_ALLOWED_MIMETYPES, MAX_CERT_SIZE_BYTES } from "../../config/index.js";
|
|
3
|
-
import { AttestorError } from "../../utils/error.js";
|
|
4
|
-
export const fetchCertificateBytes = async ({ url }) => {
|
|
5
|
-
const res = await fetch(url, {
|
|
6
|
-
redirect: 'follow',
|
|
7
|
-
signal: AbortSignal.timeout(10_000)
|
|
8
|
-
});
|
|
9
|
-
if (!res.ok) {
|
|
10
|
-
res.body?.cancel('Not ok');
|
|
11
|
-
throw new AttestorError('ERROR_CERTIFICATE_FETCH_FAILED', `Failed to fetch certificate from URL: ${url}, status: ${res.status}`);
|
|
12
|
-
}
|
|
13
|
-
const contentType = res.headers.get('content-type');
|
|
14
|
-
if (!contentType || !CERT_ALLOWED_MIMETYPES.includes(contentType)) {
|
|
15
|
-
res.body?.cancel('Mismatch');
|
|
16
|
-
throw new AttestorError('ERROR_CERTIFICATE_FETCH_FAILED', `Invalid content-type when fetching certificate from URL: ${url},`
|
|
17
|
-
+ ` content-type: ${contentType}`);
|
|
18
|
-
}
|
|
19
|
-
if (!res.body) {
|
|
20
|
-
throw new AttestorError('ERROR_CERTIFICATE_FETCH_FAILED', `No body in response when fetching certificate from URL: ${url}`);
|
|
21
|
-
}
|
|
22
|
-
let total = 0;
|
|
23
|
-
const byteArr = [];
|
|
24
|
-
for await (const chunk of res.body) {
|
|
25
|
-
total += chunk.length;
|
|
26
|
-
if (total > MAX_CERT_SIZE_BYTES) {
|
|
27
|
-
res.body.cancel('Too many bytes');
|
|
28
|
-
throw new AttestorError('ERROR_CERTIFICATE_FETCH_FAILED', `Certificate size exceeds maximum limit of ${MAX_CERT_SIZE_BYTES}b`);
|
|
29
|
-
}
|
|
30
|
-
byteArr.push(chunk);
|
|
31
|
-
}
|
|
32
|
-
const bytes = concatenateUint8Arrays(byteArr);
|
|
33
|
-
try {
|
|
34
|
-
const cert = loadX509FromPem(bytes);
|
|
35
|
-
TLS_INTERMEDIATE_CA_CACHE[url] = cert;
|
|
36
|
-
}
|
|
37
|
-
catch (err) {
|
|
38
|
-
throw new AttestorError('ERROR_CERTIFICATE_FETCH_FAILED', `Failed to parse certificate, error: ${err.message}`);
|
|
39
|
-
}
|
|
40
|
-
return { bytes: concatenateUint8Arrays(byteArr) };
|
|
41
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { claimTeeBundle } from "./claimTeeBundle.js";
|
|
2
|
-
import { claimTunnel } from "./claimTunnel.js";
|
|
3
|
-
import { completeClaimOnChain } from "./completeClaimOnChain.js";
|
|
4
|
-
import { createClaimOnChain } from "./createClaimOnChain.js";
|
|
5
|
-
import { createTaskOnMechain } from "./createTaskOnMechain.js";
|
|
6
|
-
import { createTunnel } from "./createTunnel.js";
|
|
7
|
-
import { disconnectTunnel } from "./disconnectTunnel.js";
|
|
8
|
-
import { fetchCertificateBytes } from "./fetchCertificateBytes.js";
|
|
9
|
-
import { init } from "./init.js";
|
|
10
|
-
import { toprf } from "./toprf.js";
|
|
11
|
-
export const HANDLERS = {
|
|
12
|
-
createTunnel,
|
|
13
|
-
disconnectTunnel,
|
|
14
|
-
claimTunnel,
|
|
15
|
-
claimTeeBundle,
|
|
16
|
-
init,
|
|
17
|
-
createClaimOnChain,
|
|
18
|
-
completeClaimOnChain,
|
|
19
|
-
toprf,
|
|
20
|
-
createTaskOnMechain,
|
|
21
|
-
fetchCertificateBytes
|
|
22
|
-
};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { ethers } from 'ethers';
|
|
2
|
-
import { getAttestorAddress } from "../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
|
-
export 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
|
|
28
|
-
? ethers.utils.arrayify(TOPRF_PUBLIC_KEY)
|
|
29
|
-
: new Uint8Array(),
|
|
30
|
-
attestorAddress: getAttestorAddress(initRequest.signatureType)
|
|
31
|
-
};
|
|
32
|
-
};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { ethers } from 'ethers';
|
|
2
|
-
import { getEnvVariable } from "../../utils/env.js";
|
|
3
|
-
import { getEngineString, makeDefaultOPRFOperator } from "../../utils/index.js";
|
|
4
|
-
export 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 = ethers.utils.arrayify(PRIVATE_KEY_STR);
|
|
11
|
-
const PUBLIC_KEY = ethers.utils.arrayify(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
|
-
};
|
package/lib/server/index.js
DELETED
package/lib/server/socket.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
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 "./handlers/index.js";
|
|
5
|
-
import { getApm } from "./utils/apm.js";
|
|
6
|
-
import { getInitialMessagesFromQuery } from "./utils/generics.js";
|
|
7
|
-
import { AttestorError, generateSessionId } from "../utils/index.js";
|
|
8
|
-
import { AttestorSocket } from "../utils/socket-base.js";
|
|
9
|
-
export class AttestorServerSocket extends AttestorSocket {
|
|
10
|
-
tunnels = {};
|
|
11
|
-
sessionId;
|
|
12
|
-
bgpListener;
|
|
13
|
-
constructor(socket, sessionId, bgpListener, logger) {
|
|
14
|
-
// @ts-ignore
|
|
15
|
-
super(socket, {}, logger);
|
|
16
|
-
this.sessionId = sessionId;
|
|
17
|
-
this.bgpListener = bgpListener;
|
|
18
|
-
// handle RPC requests
|
|
19
|
-
this.addEventListener('rpc-request', handleRpcRequest.bind(this));
|
|
20
|
-
// forward packets to the appropriate tunnel
|
|
21
|
-
this.addEventListener('tunnel-message', handleTunnelMessage.bind(this));
|
|
22
|
-
// close all tunnels when the connection is terminated
|
|
23
|
-
// since this tunnel can no longer be written to
|
|
24
|
-
this.addEventListener('connection-terminated', () => {
|
|
25
|
-
for (const tunnelId in this.tunnels) {
|
|
26
|
-
const tunnel = this.tunnels[tunnelId];
|
|
27
|
-
void tunnel.close(new Error('WS session terminated'));
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
getTunnel(tunnelId) {
|
|
32
|
-
const tunnel = this.tunnels[tunnelId];
|
|
33
|
-
if (!tunnel) {
|
|
34
|
-
throw new AttestorError('ERROR_NOT_FOUND', `Tunnel "${tunnelId}" not found`);
|
|
35
|
-
}
|
|
36
|
-
return tunnel;
|
|
37
|
-
}
|
|
38
|
-
removeTunnel(tunnelId) {
|
|
39
|
-
delete this.tunnels[tunnelId];
|
|
40
|
-
}
|
|
41
|
-
static async acceptConnection(socket, { req, logger, bgpListener }) {
|
|
42
|
-
// promisify ws.send -- so the sendMessage method correctly
|
|
43
|
-
// awaits the send operation
|
|
44
|
-
const bindSend = socket.send.bind(socket);
|
|
45
|
-
socket.send = promisify(bindSend);
|
|
46
|
-
const sessionId = generateSessionId();
|
|
47
|
-
logger = logger.child({ sessionId });
|
|
48
|
-
const client = new AttestorServerSocket(socket, sessionId, bgpListener, logger);
|
|
49
|
-
try {
|
|
50
|
-
const initMsgs = getInitialMessagesFromQuery(req);
|
|
51
|
-
logger.trace({ initMsgs: initMsgs.length }, 'new connection, validating...');
|
|
52
|
-
for (const msg of initMsgs) {
|
|
53
|
-
await handleMessage.call(client, msg);
|
|
54
|
-
}
|
|
55
|
-
logger.debug('connection accepted');
|
|
56
|
-
}
|
|
57
|
-
catch (err) {
|
|
58
|
-
logger.error({ err }, 'error in new connection');
|
|
59
|
-
if (client.isOpen) {
|
|
60
|
-
await client.terminateConnection(err instanceof AttestorError
|
|
61
|
-
? err
|
|
62
|
-
: AttestorError.badRequest(err.message));
|
|
63
|
-
}
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
return client;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
async function handleTunnelMessage({ data: { tunnelId, message } }) {
|
|
70
|
-
try {
|
|
71
|
-
const tunnel = this.getTunnel(tunnelId);
|
|
72
|
-
await tunnel.write(message);
|
|
73
|
-
}
|
|
74
|
-
catch (err) {
|
|
75
|
-
this.logger?.error({ err, tunnelId }, 'error writing to tunnel');
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
async function handleRpcRequest({ data: { data, requestId, respond, type } }) {
|
|
79
|
-
const logger = this.logger.child({ rpc: type, requestId });
|
|
80
|
-
const apm = getApm();
|
|
81
|
-
const tx = apm?.startTransaction(type);
|
|
82
|
-
tx?.setLabel('requestId', requestId);
|
|
83
|
-
tx?.setLabel('sessionId', this.sessionId.toString());
|
|
84
|
-
const userId = this.metadata.auth?.data?.id;
|
|
85
|
-
if (userId) {
|
|
86
|
-
tx?.setLabel('authUserId', userId);
|
|
87
|
-
}
|
|
88
|
-
const timeout = setTimeout(() => {
|
|
89
|
-
logger.warn({ type, requestId }, 'RPC took too long to respond');
|
|
90
|
-
}, DEFAULT_RPC_TIMEOUT_MS);
|
|
91
|
-
try {
|
|
92
|
-
logger.debug({ data }, 'handling RPC request');
|
|
93
|
-
const handler = HANDLERS[type];
|
|
94
|
-
const res = await handler(data, { client: this, logger, tx });
|
|
95
|
-
respond(res);
|
|
96
|
-
logger.debug({ res }, 'handled RPC request');
|
|
97
|
-
tx?.setOutcome('success');
|
|
98
|
-
}
|
|
99
|
-
catch (err) {
|
|
100
|
-
logger.error({ err }, 'error in RPC request');
|
|
101
|
-
respond(AttestorError.fromError(err));
|
|
102
|
-
tx?.setOutcome('failure');
|
|
103
|
-
apm?.captureError(err, { parent: tx });
|
|
104
|
-
}
|
|
105
|
-
finally {
|
|
106
|
-
clearTimeout(timeout);
|
|
107
|
-
tx?.end();
|
|
108
|
-
}
|
|
109
|
-
}
|