Skip to content

Commit

Permalink
Merge pull request #117 from zama-ai/forbidUnauthReencrypt
Browse files Browse the repository at this point in the history
Forbid unauth reencrypt
  • Loading branch information
jatZama authored Oct 22, 2024
2 parents 3e4be5b + eaaf0e2 commit 38cb3bc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,5 @@ dist
.pnp.*

bundle/
lib/
lib/
.DS_Store
6 changes: 3 additions & 3 deletions src/sdk/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ export const getPublicParams = async (config: FhevmInstanceConfig) => {
}
};

export const getKMSSignatures = async (
export const getKMSSigners = async (
provider: Provider,
config: FhevmInstanceConfig,
): Promise<string[]> => {
const kmsContract = new Contract(config.kmsContractAddress, abi, provider);
const signatures: string[] = await kmsContract.getSigners();
return signatures;
const signers: string[] = await kmsContract.getSigners();
return signers;
};
10 changes: 6 additions & 4 deletions src/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
FhevmInstanceConfig,
getChainId,
getKMSSignatures,
getKMSSigners,
getProvider,
getPublicParams,
getTfheCompactPublicKey,
Expand All @@ -11,7 +11,7 @@ import { PublicParams, ZKInput } from './encrypt';
import { createEncryptedInput } from './encrypt';
import { generateKeypair, createEIP712, EIP712 } from './keypair';
import { reencryptRequest } from './reencrypt';
import { isAddress } from 'ethers';
import { isAddress, ethers } from 'ethers';

export type FhevmInstance = {
createEncryptedInput: (
Expand Down Expand Up @@ -66,7 +66,7 @@ export const createInstance = async (

const pkePublicParams: PublicParams = await getPublicParams(config);

const kmsSignatures = await getKMSSignatures(provider, config);
const kmsSigners = await getKMSSigners(provider, config);

return {
createEncryptedInput: createEncryptedInput(
Expand All @@ -79,10 +79,12 @@ export const createInstance = async (
generateKeypair,
createEIP712: createEIP712(chainId),
reencrypt: reencryptRequest(
kmsSignatures,
kmsSigners,
chainId,
kmsContractAddress,
aclContractAddress,
cleanURL(config.gatewayUrl),
provider,
),
getPublicKey: () => publicKey || null,
getPublicParams: () => pkePublicParams || null,
Expand Down
3 changes: 3 additions & 0 deletions src/sdk/reencrypt.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { reencryptRequest } from './reencrypt';
import fetchMock from '@fetch-mock/core';
import { ethers } from 'ethers';

fetchMock.mockGlobal();

Expand All @@ -21,7 +22,9 @@ describe('reencrypt', () => {
[],
9000,
'0x8ba1f109551bd432803012645ac136ddd64dba72',
'0xa5e1defb98EFe38EBb2D958CEe052410247F4c80',
'https://test-gateway.net/',
new ethers.JsonRpcProvider('https://devnet.zama.ai'),
);
// const result = await reencrypt(
// BigInt(3333),
Expand Down
27 changes: 20 additions & 7 deletions src/sdk/reencrypt.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { Provider } from 'ethers';
import {
bytesToBigInt,
bigIntToBytes256,
toHexString,
fromHexString,
} from '../utils';
import { bytesToBigInt, fromHexString } from '../utils';
import {
u8vec_to_cryptobox_pk,
new_client,
process_reencryption_resp_from_js,
u8vec_to_cryptobox_sk,
} from 'node-tkms';
import { ethers } from 'ethers';

const aclABI = [
'function persistAllowed(uint256 handle, address account) view returns (bool)',
];

export const reencryptRequest =
(
kmsSignatures: string[],
chainId: number,
kmsContractAddress: string,
aclContractAddress: string,
gatewayUrl: string,
provider: ethers.JsonRpcProvider | ethers.BrowserProvider,
) =>
async (
handle: bigint,
Expand All @@ -27,6 +28,18 @@ export const reencryptRequest =
contractAddress: string,
userAddress: string,
) => {
const acl = new ethers.Contract(aclContractAddress, aclABI, provider);
const userAllowed = await acl.persistAllowed(handle, userAddress);
const contractAllowed = await acl.persistAllowed(handle, contractAddress);
const isAllowed = userAllowed && contractAllowed;
if (!isAllowed) {
throw new Error('User is not authorized to reencrypt this handle!');
}
if (userAddress === contractAddress) {
throw new Error(
'userAddress should not be equal to contractAddress when requesting reencryption!',
);
}
const payload = {
signature: signature.replace(/^(0x)/, ''),
user_address: userAddress.replace(/^(0x)/, ''),
Expand Down

0 comments on commit 38cb3bc

Please # to comment.