From 21e419eff05cfab283a4fb63de4f4f1a67532a2c Mon Sep 17 00:00:00 2001 From: Mykola Sakhno Date: Sat, 27 Jul 2024 14:43:45 +0300 Subject: [PATCH] make functions internal --- src/BTCDepositAddressDeriver.sol | 1 - src/Bech32m.sol | 30 +++++++++++++++--------------- src/Deriver.sol | 6 +++--- src/Tools.sol | 11 ++++------- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/BTCDepositAddressDeriver.sol b/src/BTCDepositAddressDeriver.sol index 00ac591..6e8ff61 100644 --- a/src/BTCDepositAddressDeriver.sol +++ b/src/BTCDepositAddressDeriver.sol @@ -5,7 +5,6 @@ pragma solidity ^0.8.24; import {Deriver} from "./Deriver.sol"; import {Bech32m} from "./Bech32m.sol"; import {BitcoinNetworkEncoder} from "./BitcoinNetworkEncoder.sol"; -import {console} from "forge-std/console.sol"; error SeedWasNotSetYet(); error UnsupportedBtcAddress(string btcAddress); diff --git a/src/Bech32m.sol b/src/Bech32m.sol index 7340d51..252478b 100644 --- a/src/Bech32m.sol +++ b/src/Bech32m.sol @@ -50,7 +50,7 @@ library Bech32m { IncorrectEncodingForSegwitVn } - function explainDecodeError(DecodeError err) public pure returns (string memory) { + function explainDecodeError(DecodeError err) internal pure returns (string memory) { if (err == DecodeError.NoError) { return string("No error"); } else if (err == DecodeError.IncorrectPadding) { @@ -135,7 +135,7 @@ library Bech32m { // Expand the HRP into values for checksum computation. // hrpExpand DOES NOT check the validity of the HRP - function hrpExpand(bytes memory hrp) public pure returns (bytes memory) { + function hrpExpand(bytes memory hrp) internal pure returns (bytes memory) { bytes memory a = new bytes(hrp.length + hrp.length + 1); for (uint i = 0; i < hrp.length; i += 1) { @@ -151,7 +151,7 @@ library Bech32m { bytes memory hrp, bytes memory data, BechEncoding spec - ) public pure returns (bytes memory) { + ) internal pure returns (bytes memory) { // TODO(mkl): add check for UNKNOWN encoding uint const = spec == BechEncoding.BECH32M ? BECH32M_CONST : 1; @@ -215,7 +215,7 @@ library Bech32m { bytes memory hrp, bytes memory data, BechEncoding spec - ) public pure returns (bytes memory) { + ) internal pure returns (bytes memory) { if (spec == BechEncoding.UNKNOWN) { revert EncodingIsUnknown(); @@ -319,7 +319,7 @@ library Bech32m { bytes memory hrp, uint8 witVer, bytes memory witProg - ) public pure returns (bytes memory) { + ) internal pure returns (bytes memory) { BechEncoding spec = witVer == 0 ? BechEncoding.BECH32 : BechEncoding.BECH32M; @@ -333,7 +333,7 @@ library Bech32m { // Convert 8 groups of 5 bits to 5 bytes function convert5to8( bytes memory data5Bits - ) public pure returns (bytes memory, DecodeError) { + ) internal pure returns (bytes memory, DecodeError) { uint vRest; uint nRest5Bits = data5Bits.length % 8; uint nRest8Bits; @@ -441,7 +441,7 @@ library Bech32m { // check that all characters are in the range 33-126 inclusive\ function isValidCharacterRange( bytes memory bech - ) public pure returns (bool) { + ) internal pure returns (bool) { for (uint i = 0; i < bech.length; i += 1) { if (uint8(bech[i]) < 33 || uint8(bech[i]) > 126) { return false; @@ -450,7 +450,7 @@ library Bech32m { return true; } - function isMixedCase(bytes memory b) public pure returns (bool) { + function isMixedCase(bytes memory b) internal pure returns (bool) { bool hasLower = false; bool hasUpper = false; @@ -470,7 +470,7 @@ library Bech32m { return false; } - function toLower(bytes memory a) public pure returns (bytes memory) { + function toLower(bytes memory a) internal pure returns (bytes memory) { bytes memory b = new bytes(a.length); for (uint i = 0; i < a.length; i += 1) { if (uint8(a[i]) >= 65 && uint8(a[i]) <= 90) { @@ -488,7 +488,7 @@ library Bech32m { bytes memory bech, uint start, uint stop - ) public pure returns (bytes memory, DecodeError) { + ) internal pure returns (bytes memory, DecodeError) { bytes memory decoded = new bytes(stop - start); for (uint i = start; i < stop; i += 1) { uint8 c = uint8(bech[i]); @@ -503,7 +503,7 @@ library Bech32m { function bech32Decode( bytes memory bech ) - public + internal pure returns (bytes memory, bytes memory, BechEncoding, DecodeError) { @@ -603,7 +603,7 @@ library Bech32m { function areBytesEqual( bytes memory a, bytes memory b - ) public pure returns (bool) { + ) internal pure returns (bool) { if (a.length != b.length) { return false; } @@ -617,9 +617,9 @@ library Bech32m { // Decode a segwit address function decodeSegwitAddress( - bytes calldata expectedHrp, - bytes calldata addr - ) public pure returns (uint8, bytes memory, DecodeError) { + bytes memory expectedHrp, + bytes memory addr + ) internal pure returns (uint8, bytes memory, DecodeError) { ( bytes memory hrpGot, bytes memory data5Bit, diff --git a/src/Deriver.sol b/src/Deriver.sol index dea32bb..17717f9 100644 --- a/src/Deriver.sol +++ b/src/Deriver.sol @@ -69,7 +69,7 @@ library Deriver { uint256 p2x, uint256 p2y, address addr - ) public pure returns (uint256, uint256) { + ) internal pure returns (uint256, uint256) { uint256 c1 = getCoefficient(p1x, p1y, addr); uint256 c2 = getCoefficient(p2x, p2y, addr); return getCombinedPubkey(p1x, p1y, p2x, p2y, c1, c2); @@ -83,7 +83,7 @@ library Deriver { uint256 p2y, bytes memory hrp, address ethAddr - ) public pure returns (string memory) { + ) internal pure returns (string memory) { (uint256 x, uint256 y) = getPubkeyFromAddress( p1x, p1y, @@ -95,7 +95,7 @@ library Deriver { } // calculate y coordinate from x coordinate - function liftX(uint256 x) public pure returns (uint256) { + function liftX(uint256 x) internal pure returns (uint256) { return EllipticCurve.deriveY(0x02, x, AA, BB, PP); } } diff --git a/src/Tools.sol b/src/Tools.sol index 6d9a810..d36b759 100644 --- a/src/Tools.sol +++ b/src/Tools.sol @@ -2,22 +2,19 @@ pragma solidity ^0.8.24; -import {Script} from "forge-std/Script.sol"; -import {Strings} from "@openzeppelin/contracts/utils/Strings.sol"; - library Tools { // https://www.educative.io/answers/how-to-compare-two-strings-in-solidity function areStringsEqual( string memory str1, string memory str2 - ) public pure returns (bool) { + ) internal pure returns (bool) { return keccak256(abi.encodePacked(str1)) == keccak256(abi.encodePacked(str2)); } // https://stackoverflow.com/questions/69551020/trying-to-convert-address-string-to-type-address-in-solidity - function fromHexChar(uint8 c) public pure returns (uint8) { + function fromHexChar(uint8 c) internal pure returns (uint8) { if (bytes1(c) >= bytes1("0") && bytes1(c) <= bytes1("9")) { return c - uint8(bytes1("0")); } @@ -33,9 +30,9 @@ library Tools { // https://stackoverflow.com/questions/69551020/trying-to-convert-address-string-to-type-address-in-solidity function hexStringToAddress( string memory s - ) public pure returns (bytes memory) { + ) internal pure returns (bytes memory) { bytes memory ss = bytes(s); - require(ss.length % 2 == 0); // length must be even + require(ss.length % 2 == 0, "string with hex encoded data should have even length"); // length must be even bytes memory r = new bytes(ss.length / 2); for (uint i = 0; i < ss.length / 2; ++i) { r[i] = bytes1(