From 7b8c66d839fa3120e5a94b2ec83a271a891af122 Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Mon, 26 Feb 2024 21:27:38 +0000 Subject: [PATCH] Fix kettle addr padding (#52) * Fix kettle address in contextGet * More * Skip Chatgpt key if not found --- src/Context.sol | 10 ++++++++-- src/forge/ContextConnector.sol | 8 +++++++- test/protocols/ChatGPT.t.sol | 3 +++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Context.sol b/src/Context.sol index afe3799..4c04fb5 100644 --- a/src/Context.sol +++ b/src/Context.sol @@ -9,7 +9,13 @@ library Context { } function kettleAddress() internal returns (address) { - bytes memory addr = Suave.contextGet("kettleAddress"); - return abi.decode(addr, (address)); + bytes memory _bytes = Suave.contextGet("kettleAddress"); + + address addr; + assembly { + addr := mload(add(_bytes, 20)) + } + + return addr; } } diff --git a/src/forge/ContextConnector.sol b/src/forge/ContextConnector.sol index bf0fa5a..93928bd 100644 --- a/src/forge/ContextConnector.sol +++ b/src/forge/ContextConnector.sol @@ -29,7 +29,13 @@ contract ContextConnector { if (keyHash == keccak256(abi.encodePacked("confidentialInputs"))) { msgContent = confidentialInputs; } else if (keyHash == keccak256(abi.encodePacked("kettleAddress"))) { - msgContent = abi.encode(kettleAddress); + // pad the address to the rigth to 32 bytes + bytes20 addr = bytes20(kettleAddress); + bytes memory paddedAddress = new bytes(32); + for (uint256 i = 0; i < 20; i++) { + paddedAddress[i] = addr[i]; + } + msgContent = paddedAddress; } else { revert("Invalid context key"); } diff --git a/test/protocols/ChatGPT.t.sol b/test/protocols/ChatGPT.t.sol index 4cc68e6..313eea9 100644 --- a/test/protocols/ChatGPT.t.sol +++ b/test/protocols/ChatGPT.t.sol @@ -21,6 +21,9 @@ contract ChatGPTTest is Test, SuaveEnabled { function getChatGPT() public returns (ChatGPT chatgpt) { // NOTE: tried to do it with envOr but it did not worked try vm.envString("CHATGPT_API_KEY") returns (string memory apiKey) { + if (bytes(apiKey).length == 0) { + vm.skip(true); + } chatgpt = new ChatGPT(apiKey); } catch { vm.skip(true);