From 104825e63d488622f13258aaa642c7cb807c7c74 Mon Sep 17 00:00:00 2001 From: Roman Bredehoft Date: Fri, 15 Nov 2024 16:47:32 +0100 Subject: [PATCH 1/3] fix: propagate publicKeyId to getKeysFromGateway --- src/sdk/config.ts | 4 ++-- src/sdk/network.test.ts | 2 +- src/sdk/network.ts | 28 +++++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/sdk/config.ts b/src/sdk/config.ts index 18d97f0..114ed9e 100644 --- a/src/sdk/config.ts +++ b/src/sdk/config.ts @@ -50,7 +50,7 @@ export const getChainId = async ( export const getTfheCompactPublicKey = async (config: FhevmInstanceConfig) => { if (config.gatewayUrl && !config.publicKey) { - const inputs = await getKeysFromGateway(cleanURL(config.gatewayUrl)); + const inputs = await getKeysFromGateway(cleanURL(config.gatewayUrl), config.publicKeyId); return { publicKey: inputs.publicKey, publicKeyId: inputs.publicKeyId }; } else if (config.publicKey && config.publicKeyId) { const buff = fromHexString(config.publicKey); @@ -66,7 +66,7 @@ export const getTfheCompactPublicKey = async (config: FhevmInstanceConfig) => { export const getPublicParams = async (config: FhevmInstanceConfig) => { if (config.gatewayUrl && !config.publicParams) { - const inputs = await getKeysFromGateway(cleanURL(config.gatewayUrl)); + const inputs = await getKeysFromGateway(cleanURL(config.gatewayUrl), config.publicKeyId); return inputs.publicParams; } else if (config.publicParams && config.publicParams['2048']) { const buff = fromHexString(config.publicParams['2048'].publicParams); diff --git a/src/sdk/network.test.ts b/src/sdk/network.test.ts index 1f6d082..9e38408 100644 --- a/src/sdk/network.test.ts +++ b/src/sdk/network.test.ts @@ -99,7 +99,7 @@ fetchMock.get( describe('network', () => { it('getInputsFromGateway', async () => { - const material = await getKeysFromGateway('https://test-gateway.net/'); + const material = await getKeysFromGateway('https://test-gateway.net/', undefined); expect( material.publicKey.safe_serialize(SERIALIZED_SIZE_LIMIT_PK), diff --git a/src/sdk/network.ts b/src/sdk/network.ts index e92d21a..387fd76 100644 --- a/src/sdk/network.ts +++ b/src/sdk/network.ts @@ -35,19 +35,41 @@ export type GatewayKeys = { }; const keyurlCache: { [key: string]: any } = {}; -export const getKeysFromGateway = async (url: string) => { +export const getKeysFromGateway = async (url: string, publicKeyId: string | undefined) => { if (keyurlCache[url]) { return keyurlCache[url]; } try { + console.log('fetching keys from', url); const response = await fetch(`${url}keyurl`); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data: GatewayKeys = await response.json(); if (data) { - const pubKeyUrl = data.response.fhe_key_info[0].fhe_public_key.urls[0]; - const publicKeyId = data.response.fhe_key_info[0].fhe_public_key.data_id; + let pubKeyUrl: string; + + // If no publicKeyId is provided, use the first one + // Warning: if there are multiple keys available, the first one will most likely never be the + // same between several calls (fetching the infos is non-deterministic) + if (!publicKeyId) { + pubKeyUrl = data.response.fhe_key_info[0].fhe_public_key.urls[0]; + publicKeyId = data.response.fhe_key_info[0].fhe_public_key.data_id; + } else { + // If a publicKeyId is provided, get the corresponding info + const keyInfo = data.response.fhe_key_info.find(info => info.fhe_public_key.data_id === publicKeyId); + + if (!keyInfo) { + throw new Error(`Could not find FHE key info with data_id ${publicKeyId}`); + } + + // TODO: Get a given party's public key url instead of the first one + pubKeyUrl = keyInfo.fhe_public_key.urls[0]; + } + + console.log('publicKeyId', publicKeyId); + console.log('pubKeyUrl', pubKeyUrl); + const publicKeyResponse = await fetch(pubKeyUrl); const publicKey = await publicKeyResponse.arrayBuffer(); const publicParamsUrl = data.response.crs['2048'].urls[0]; From d38b62de06577f3a7887512f4395ff58d8a0ead7 Mon Sep 17 00:00:00 2001 From: Roman Bredehoft Date: Mon, 18 Nov 2024 11:14:12 +0100 Subject: [PATCH 2/3] chore: fix review --- src/sdk/network.test.ts | 2 +- src/sdk/network.ts | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/sdk/network.test.ts b/src/sdk/network.test.ts index 9e38408..1f6d082 100644 --- a/src/sdk/network.test.ts +++ b/src/sdk/network.test.ts @@ -99,7 +99,7 @@ fetchMock.get( describe('network', () => { it('getInputsFromGateway', async () => { - const material = await getKeysFromGateway('https://test-gateway.net/', undefined); + const material = await getKeysFromGateway('https://test-gateway.net/'); expect( material.publicKey.safe_serialize(SERIALIZED_SIZE_LIMIT_PK), diff --git a/src/sdk/network.ts b/src/sdk/network.ts index 387fd76..ecab4d5 100644 --- a/src/sdk/network.ts +++ b/src/sdk/network.ts @@ -35,7 +35,7 @@ export type GatewayKeys = { }; const keyurlCache: { [key: string]: any } = {}; -export const getKeysFromGateway = async (url: string, publicKeyId: string | undefined) => { +export const getKeysFromGateway = async (url: string, publicKeyId?: string) => { if (keyurlCache[url]) { return keyurlCache[url]; } @@ -67,9 +67,6 @@ export const getKeysFromGateway = async (url: string, publicKeyId: string | unde pubKeyUrl = keyInfo.fhe_public_key.urls[0]; } - console.log('publicKeyId', publicKeyId); - console.log('pubKeyUrl', pubKeyUrl); - const publicKeyResponse = await fetch(pubKeyUrl); const publicKey = await publicKeyResponse.arrayBuffer(); const publicParamsUrl = data.response.crs['2048'].urls[0]; From abbb9b6d48aa418cfd5f62803425bad17dc5dab2 Mon Sep 17 00:00:00 2001 From: Roman Bredehoft Date: Mon, 18 Nov 2024 12:31:05 +0100 Subject: [PATCH 3/3] chore: remove console log --- src/sdk/network.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sdk/network.ts b/src/sdk/network.ts index ecab4d5..80865d6 100644 --- a/src/sdk/network.ts +++ b/src/sdk/network.ts @@ -40,7 +40,6 @@ export const getKeysFromGateway = async (url: string, publicKeyId?: string) => { return keyurlCache[url]; } try { - console.log('fetching keys from', url); const response = await fetch(`${url}keyurl`); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`);