@zama-fhe/relayer-sdk 0.3.0-2 → 0.3.0-4

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/web.js CHANGED
@@ -16282,6 +16282,9 @@ const bytesToBigInt = function (byteArray) {
16282
16282
  .join('');
16283
16283
  return BigInt(`0x${hex}`);
16284
16284
  };
16285
+ function ensure0x(s) {
16286
+ return !s.startsWith('0x') ? `0x${s}` : s;
16287
+ }
16285
16288
 
16286
16289
  function setAuth(init, auth) {
16287
16290
  if (auth) {
@@ -16788,6 +16791,17 @@ const NumEncryptedBits = {
16788
16791
  7: 160, // eaddress
16789
16792
  8: 256, // euint256
16790
16793
  };
16794
+ function getHandleType(handle) {
16795
+ if (handle.length !== 66) {
16796
+ throw new Error(`Handle ${handle} is not of valid length`);
16797
+ }
16798
+ const hexPair = handle.slice(-4, -2).toLowerCase();
16799
+ const typeDiscriminant = parseInt(hexPair, 16);
16800
+ if (!(typeDiscriminant in NumEncryptedBits)) {
16801
+ throw new Error(`Handle ${handle} is not of valid type`);
16802
+ }
16803
+ return typeDiscriminant;
16804
+ }
16791
16805
  function checkEncryptedBits(handles) {
16792
16806
  let total = 0;
16793
16807
  for (const handle of handles) {
@@ -16816,37 +16830,30 @@ const aclABI$1 = [
16816
16830
  ];
16817
16831
  const MAX_USER_DECRYPT_CONTRACT_ADDRESSES = 10;
16818
16832
  const MAX_USER_DECRYPT_DURATION_DAYS = BigInt(365);
16819
- function formatAccordingToType(decryptedBigInt, type) {
16833
+ function formatAccordingToType(clearValueAsBigInt, type) {
16820
16834
  if (type === 0) {
16821
16835
  // ebool
16822
- return decryptedBigInt === BigInt(1);
16836
+ return clearValueAsBigInt === BigInt(1);
16823
16837
  }
16824
16838
  else if (type === 7) {
16825
16839
  // eaddress
16826
- return getAddress$1('0x' + decryptedBigInt.toString(16).padStart(40, '0'));
16827
- }
16828
- else if (type === 9) {
16829
- // ebytes64
16830
- return '0x' + decryptedBigInt.toString(16).padStart(128, '0');
16840
+ return getAddress$1('0x' + clearValueAsBigInt.toString(16).padStart(40, '0'));
16831
16841
  }
16832
- else if (type === 10) {
16833
- // ebytes128
16834
- return '0x' + decryptedBigInt.toString(16).padStart(256, '0');
16842
+ else if (type > 8 || type == 1) {
16843
+ // type == 1 : euint4 (not supported)
16844
+ throw new Error(`Unsupported handle type ${type}`);
16835
16845
  }
16836
- else if (type === 11) {
16837
- // ebytes256
16838
- return '0x' + decryptedBigInt.toString(16).padStart(512, '0');
16839
- } // euintXXX
16840
- return decryptedBigInt;
16846
+ // euintXXX
16847
+ return clearValueAsBigInt;
16841
16848
  }
16842
- function buildUserDecryptedResult(handles, listBigIntDecryptions) {
16849
+ function buildUserDecryptResults(handles, listBigIntDecryptions) {
16843
16850
  let typesList = [];
16844
16851
  for (const handle of handles) {
16845
16852
  const hexPair = handle.slice(-4, -2).toLowerCase();
16846
16853
  const typeDiscriminant = parseInt(hexPair, 16);
16847
16854
  typesList.push(typeDiscriminant);
16848
16855
  }
16849
- let results = {};
16856
+ const results = {};
16850
16857
  handles.forEach((handle, idx) => (results[handle] = formatAccordingToType(listBigIntDecryptions[idx], typesList[idx])));
16851
16858
  return results;
16852
16859
  }
@@ -16952,7 +16959,7 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
16952
16959
  };
16953
16960
  const decryption = TKMS.process_user_decryption_resp_from_js(client, payloadForVerification, eip712Domain, json.response, pubKey, privKey, true);
16954
16961
  const listBigIntDecryptions = decryption.map((d) => bytesToBigInt(d.bytes));
16955
- const results = buildUserDecryptedResult(handles.map((h) => h.handle), listBigIntDecryptions);
16962
+ const results = buildUserDecryptResults(handles.map((h) => h.handle), listBigIntDecryptions);
16956
16963
  return results;
16957
16964
  }
16958
16965
  catch (e) {
@@ -17363,6 +17370,74 @@ function isThresholdReached(kmsSigners, recoveredAddresses, threshold) {
17363
17370
  }
17364
17371
  return recoveredAddresses.length >= threshold;
17365
17372
  }
17373
+ function abiEncodeClearValues(clearValues) {
17374
+ const handlesBytes32Hex = Object.keys(clearValues);
17375
+ const abiTypes = [];
17376
+ const abiValues = [];
17377
+ for (let i = 0; i < handlesBytes32Hex.length; ++i) {
17378
+ const handle = handlesBytes32Hex[i];
17379
+ const handleType = getHandleType(handle);
17380
+ let clearTextValue = clearValues[handle];
17381
+ if (typeof clearTextValue === 'boolean') {
17382
+ clearTextValue = clearTextValue ? '0x01' : '0x00';
17383
+ }
17384
+ const clearTextValueBigInt = BigInt(clearTextValue);
17385
+ //abiTypes.push(fhevmTypeInfo.solidityTypeName);
17386
+ abiTypes.push('uint256');
17387
+ switch (handleType) {
17388
+ // eaddress
17389
+ case 7: {
17390
+ // string
17391
+ abiValues.push(`0x${clearTextValueBigInt.toString(16).padStart(40, '0')}`);
17392
+ break;
17393
+ }
17394
+ // ebool
17395
+ case 0: {
17396
+ // bigint (0 or 1)
17397
+ if (clearTextValueBigInt !== BigInt(0) &&
17398
+ clearTextValueBigInt !== BigInt(1)) {
17399
+ throw new Error(`Invalid ebool clear text value ${clearTextValueBigInt}. Expecting 0 or 1.`);
17400
+ }
17401
+ abiValues.push(clearTextValueBigInt);
17402
+ break;
17403
+ }
17404
+ case 2: //euint8
17405
+ case 3: //euint16
17406
+ case 4: //euint32
17407
+ case 5: //euint64
17408
+ case 6: //euint128
17409
+ case 7: {
17410
+ //euint256
17411
+ // bigint
17412
+ abiValues.push(clearTextValueBigInt);
17413
+ break;
17414
+ }
17415
+ default: {
17416
+ throw new Error(`Unsupported Fhevm primitive type id: ${handleType}`);
17417
+ }
17418
+ }
17419
+ }
17420
+ const abiCoder = ethers.AbiCoder.defaultAbiCoder();
17421
+ // ABI encode the decryptedResult as done in the KMS, since all decrypted values
17422
+ // are native static types, thay have same abi-encoding as uint256:
17423
+ const abiEncodedClearValues = abiCoder.encode(abiTypes, abiValues);
17424
+ return {
17425
+ abiTypes,
17426
+ abiValues,
17427
+ abiEncodedClearValues,
17428
+ };
17429
+ }
17430
+ function buildDecryptionProof(kmsSignatures, extraData) {
17431
+ // Build the decryptionProof as numSigners + KMS signatures + extraData
17432
+ const packedNumSigners = ethers.solidityPacked(['uint8'], [kmsSignatures.length]);
17433
+ const packedSignatures = ethers.solidityPacked(Array(kmsSignatures.length).fill('bytes'), kmsSignatures);
17434
+ const decryptionProof = ethers.concat([
17435
+ packedNumSigners,
17436
+ packedSignatures,
17437
+ extraData,
17438
+ ]);
17439
+ return decryptionProof;
17440
+ }
17366
17441
  const CiphertextType = {
17367
17442
  0: 'bool',
17368
17443
  2: 'uint256',
@@ -17373,7 +17448,7 @@ const CiphertextType = {
17373
17448
  7: 'address',
17374
17449
  8: 'uint256',
17375
17450
  };
17376
- function deserializeDecryptedResult(handles, decryptedResult) {
17451
+ function deserializeClearValues(handles, decryptedResult) {
17377
17452
  let typesList = [];
17378
17453
  for (const handle of handles) {
17379
17454
  const hexPair = handle.slice(-4, -2).toLowerCase();
@@ -17392,7 +17467,7 @@ function deserializeDecryptedResult(handles, decryptedResult) {
17392
17467
  const decoded = coder.decode(['uint256', ...abiTypes, 'bytes[]'], restoredEncoded);
17393
17468
  // strip dummy first/last element
17394
17469
  const rawValues = decoded.slice(1, 1 + typesList.length);
17395
- let results = {};
17470
+ const results = {};
17396
17471
  handles.forEach((handle, idx) => (results[handle] = rawValues[idx]));
17397
17472
  return results;
17398
17473
  }
@@ -17437,22 +17512,25 @@ const publicDecryptRequest = (kmsSigners, thresholdSigners, gatewayChainId, veri
17437
17512
  ],
17438
17513
  };
17439
17514
  const result = json.response[0];
17440
- const decryptedResult = result.decrypted_value.startsWith('0x')
17441
- ? result.decrypted_value
17442
- : `0x${result.decrypted_value}`;
17443
- const signatures = result.signatures;
17515
+ const decryptedResult = ensure0x(result.decrypted_value);
17516
+ const kmsSignatures = result.signatures.map(ensure0x);
17444
17517
  const signedExtraData = '0x';
17445
- const recoveredAddresses = signatures.map((signature) => {
17446
- const sig = signature.startsWith('0x') ? signature : `0x${signature}`;
17447
- const recoveredAddress = ethers.verifyTypedData(domain, types, { ctHandles: handles, decryptedResult, extraData: signedExtraData }, sig);
17518
+ const recoveredAddresses = kmsSignatures.map((kmsSignature) => {
17519
+ const recoveredAddress = ethers.verifyTypedData(domain, types, { ctHandles: handles, decryptedResult, extraData: signedExtraData }, kmsSignature);
17448
17520
  return recoveredAddress;
17449
17521
  });
17450
17522
  const thresholdReached = isThresholdReached(kmsSigners, recoveredAddresses, thresholdSigners);
17451
17523
  if (!thresholdReached) {
17452
17524
  throw Error('KMS signers threshold is not reached');
17453
17525
  }
17454
- const results = deserializeDecryptedResult(handles, decryptedResult);
17455
- return results;
17526
+ const clearValues = deserializeClearValues(handles, decryptedResult);
17527
+ const abiEnc = abiEncodeClearValues(clearValues);
17528
+ const decryptionProof = buildDecryptionProof(kmsSignatures, signedExtraData);
17529
+ return {
17530
+ clearValues,
17531
+ abiEncodedClearValues: abiEnc.abiEncodedClearValues,
17532
+ decryptionProof,
17533
+ };
17456
17534
  };
17457
17535
 
17458
17536
  /**
@@ -17562,23 +17640,23 @@ const generateKeypair = () => {
17562
17640
  global.fetch = fetchRetry(global.fetch, { retries: 5, retryDelay: 500 });
17563
17641
  const SepoliaConfig = {
17564
17642
  // ACL_CONTRACT_ADDRESS (FHEVM Host chain)
17565
- aclContractAddress: '0x687820221192C5B662b25367F70076A37bc79b6c',
17643
+ aclContractAddress: '0xf0Ffdc93b7E186bC2f8CB3dAA75D86d1930A433D',
17566
17644
  // KMS_VERIFIER_CONTRACT_ADDRESS (FHEVM Host chain)
17567
- kmsContractAddress: '0x1364cBBf2cDF5032C47d8226a6f6FBD2AFCDacAC',
17645
+ kmsContractAddress: '0xbE0E383937d564D7FF0BC3b46c51f0bF8d5C311A',
17568
17646
  // INPUT_VERIFIER_CONTRACT_ADDRESS (FHEVM Host chain)
17569
- inputVerifierContractAddress: '0xbc91f3daD1A5F19F8390c400196e58073B6a0BC4',
17647
+ inputVerifierContractAddress: '0xBBC1fFCdc7C316aAAd72E807D9b0272BE8F84DA0',
17570
17648
  // DECRYPTION_ADDRESS (Gateway chain)
17571
- verifyingContractAddressDecryption: '0xb6E160B1ff80D67Bfe90A85eE06Ce0A2613607D1',
17649
+ verifyingContractAddressDecryption: '0x5D8BD78e2ea6bbE41f26dFe9fdaEAa349e077478',
17572
17650
  // INPUT_VERIFICATION_ADDRESS (Gateway chain)
17573
- verifyingContractAddressInputVerification: '0x7048C39f048125eDa9d678AEbaDfB22F7900a29F',
17651
+ verifyingContractAddressInputVerification: '0x483b9dE06E4E4C7D35CCf5837A1668487406D955',
17574
17652
  // FHEVM Host chain id
17575
17653
  chainId: 11155111,
17576
17654
  // Gateway chain id
17577
- gatewayChainId: 55815,
17655
+ gatewayChainId: 10901,
17578
17656
  // Optional RPC provider to host chain
17579
17657
  network: 'https://eth-sepolia.public.blastapi.io',
17580
17658
  // Relayer URL
17581
- relayerUrl: 'https://relayer.testnet.zama.cloud',
17659
+ relayerUrl: 'https://relayer.testnet.zama.org',
17582
17660
  };
17583
17661
  const createInstance = async (config) => {
17584
17662
  const { verifyingContractAddressDecryption, verifyingContractAddressInputVerification, publicKey, kmsContractAddress, aclContractAddress, gatewayChainId, auth, } = config;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zama-fhe/relayer-sdk",
3
- "version": "0.3.0-2",
3
+ "version": "0.3.0-4",
4
4
  "description": "fhevm Relayer SDK",
5
5
  "main": "lib/node.js",
6
6
  "types": "lib/node.d.ts",
@@ -24,7 +24,8 @@
24
24
  "import": "./lib/node.js",
25
25
  "require": "./lib/node.cjs",
26
26
  "types": "./lib/node.d.ts"
27
- }
27
+ },
28
+ "./package.json": "./package.json"
28
29
  },
29
30
  "scripts": {
30
31
  "lint": "eslint src/",