From 44af80e5c12dc373181c07a9df4c3effd15bfb54 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 20 Oct 2024 11:45:14 +0700 Subject: [PATCH] Create CryptoLib.sol --- .../contracts/libraries/CryptoLib.sol | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 blockchain_integration/pi_network/pi-network-layer2-scaling/contracts/libraries/CryptoLib.sol diff --git a/blockchain_integration/pi_network/pi-network-layer2-scaling/contracts/libraries/CryptoLib.sol b/blockchain_integration/pi_network/pi-network-layer2-scaling/contracts/libraries/CryptoLib.sol new file mode 100644 index 000000000..ff1657c91 --- /dev/null +++ b/blockchain_integration/pi_network/pi-network-layer2-scaling/contracts/libraries/CryptoLib.sol @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +library CryptoLib { + // Hash a message using keccak256 + function hashMessage(bytes memory message) internal pure returns (bytes32) { + return keccak256(message); + } + + // Verify a signature + function verifySignature( + bytes32 messageHash, + bytes memory signature, + address signer + ) internal pure returns (bool) { + bytes32 ethSignedMessageHash = getEthSignedMessageHash(messageHash); + return recoverSigner(ethSignedMessageHash, signature) == signer; + } + + // Get the Ethereum signed message hash + function getEthSignedMessageHash(bytes32 messageHash) internal pure returns (bytes32) { + return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", messageHash)); + } + + // Recover the signer from a signature + function recoverSigner(bytes32 ethSignedMessageHash, bytes memory signature) internal pure returns (address) { + (bytes32 r, bytes32 s, uint8 v) = splitSignature(signature); + return ecrecover(ethSignedMessageHash, v, r, s); + } + + // Split the signature into r, s, and v + function splitSignature(bytes memory signature) internal pure returns (bytes32 r, bytes32 s, uint8 v) { + require(signature.length == 65, "CryptoLib: invalid signature length"); + assembly { + r := mload(add(signature, 0x20)) + s := mload(add(signature, 0x40)) + v := byte(0, mload(add(signature, 0x60))) + } + // Adjust v value + if (v < 27) { + v += 27; + } + } +}