Skip to content

Commit

Permalink
Merge pull request #34 from stroomnetwork/make_internal
Browse files Browse the repository at this point in the history
make functions internal
  • Loading branch information
mkl- authored Jul 30, 2024
2 parents b8cbd6f + 21e419e commit bf855de
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
1 change: 0 additions & 1 deletion src/BTCDepositAddressDeriver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 15 additions & 15 deletions src/Bech32m.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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) {
Expand All @@ -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]);
Expand All @@ -503,7 +503,7 @@ library Bech32m {
function bech32Decode(
bytes memory bech
)
public
internal
pure
returns (bytes memory, bytes memory, BechEncoding, DecodeError)
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/Deriver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -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);
}
}
11 changes: 4 additions & 7 deletions src/Tools.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand All @@ -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(
Expand Down

0 comments on commit bf855de

Please sign in to comment.