Skip to content

Commit

Permalink
feat: int cleanup
Browse files Browse the repository at this point in the history
Easy to be explicit and consistent
  • Loading branch information
cpb8010 committed Dec 17, 2024
1 parent 0b7f4b3 commit 94c65c2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/helpers/Logger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ library Logger {
}
}

function logUint(uint intToLog) internal view {
function logUint(uint256 intToLog) internal view {
if (block.chainid == 260) {
console.logUint(intToLog);
}
Expand Down
56 changes: 28 additions & 28 deletions src/libraries/JsmnSolLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ library JsmnSolLib {
PRIMITIVE
}

uint constant RETURN_SUCCESS = 0;
uint constant RETURN_ERROR_INVALID_JSON = 1;
uint constant RETURN_ERROR_PART = 2;
uint constant RETURN_ERROR_NO_MEM = 3;
uint256 constant RETURN_SUCCESS = 0;
uint256 constant RETURN_ERROR_INVALID_JSON = 1;
uint256 constant RETURN_ERROR_PART = 2;
uint256 constant RETURN_ERROR_NO_MEM = 3;

struct Token {
JsmnType jsmnType;
uint start;
uint256 start;
bool startSet;
uint end;
uint256 end;
bool endSet;
uint8 size;
}

struct Parser {
uint pos;
uint toknext;
int toksuper;
uint256 pos;
uint256 toknext;
int256 toksuper;
}

function init(uint length) internal pure returns (Parser memory, Token[] memory) {
function init(uint256 length) internal pure returns (Parser memory, Token[] memory) {
Parser memory p = Parser(0, 0, -1);
Token[] memory t = new Token[](length);
return (p, t);
Expand All @@ -68,7 +68,7 @@ library JsmnSolLib {
return (true, token);
}

function fillToken(Token memory token, JsmnType jsmnType, uint start, uint end) internal pure {
function fillToken(Token memory token, JsmnType jsmnType, uint256 start, uint256 end) internal pure {
token.jsmnType = jsmnType;
token.start = start;
token.startSet = true;
Expand All @@ -78,7 +78,7 @@ library JsmnSolLib {
}

function parseString(Parser memory parser, Token[] memory tokens, bytes memory s) internal pure returns (uint) {
uint start = parser.pos;
uint256 start = parser.pos;
bool success;
Token memory token;
parser.pos++;
Expand Down Expand Up @@ -124,7 +124,7 @@ library JsmnSolLib {
function parsePrimitive(Parser memory parser, Token[] memory tokens, bytes memory s) internal pure returns (uint) {
bool found = false;
uint start = parser.pos;
uint256 start = parser.pos;
bool success;
bytes1 c;
Token memory token;
Expand Down Expand Up @@ -155,16 +155,16 @@ library JsmnSolLib {
return RETURN_SUCCESS;
}
function parse(string memory json, uint numberElements) internal pure returns (uint, Token[] memory tokens, uint) {
function parse(string memory json, uint256 numberElements) internal pure returns (uint, Token[] memory tokens, uint) {
bytes memory s = bytes(json);
bool success;
Parser memory parser;
(parser, tokens) = init(numberElements);
// Token memory token;
uint r;
uint count = parser.toknext;
uint i;
uint256 r;
uint256 count = parser.toknext;
uint256 i;
Token memory token;
for (; parser.pos < s.length; parser.pos++) {
Expand Down Expand Up @@ -296,10 +296,10 @@ library JsmnSolLib {
return (RETURN_SUCCESS, tokens, parser.toknext);
}
function getBytes(string memory json, uint start, uint end) internal pure returns (string memory) {
function getBytes(string memory json, uint256 start, uint256 end) internal pure returns (string memory) {
bytes memory s = bytes(json);
bytes memory result = new bytes(end - start);
for (uint i = start; i < end; i++) {
for (uint256 i = start; i < end; i++) {
result[i - start] = s[i];
}
return string(result);
Expand All @@ -311,12 +311,12 @@ library JsmnSolLib {
}
// parseInt(parseFloat*10^_b)
function parseInt(string memory _a, uint _b) internal pure returns (int) {
function parseInt(string memory _a, uint256 _b) internal pure returns (int) {
bytes memory bresult = bytes(_a);
int mint = 0;
int256 mint = 0;
bool decimals = false;
bool negative = false;
for (uint i = 0; i < bresult.length; i++) {
for (uint256 i = 0; i < bresult.length; i++) {
if ((i == 0) && (bresult[i] == "-")) {
negative = true;
}
Expand All @@ -334,16 +334,16 @@ library JsmnSolLib {
return mint;
}
function uint2str(uint i) internal pure returns (string memory) {
function uint2str(uint256 i) internal pure returns (string memory) {
if (i == 0) return "0";
uint j = i;
uint len;
uint256 j = i;
uint256 len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len - 1;
uint256 k = len - 1;
while (i != 0) {
bstr[k--] = bytes1(uint8(48 + (i % 10)));
i /= 10;
Expand All @@ -362,9 +362,9 @@ library JsmnSolLib {
function strCompare(string memory _a, string memory _b) internal pure returns (int) {
bytes memory a = bytes(_a);
bytes memory b = bytes(_b);
uint minLength = a.length;
uint256 minLength = a.length;
if (b.length < minLength) minLength = b.length;
for (uint i = 0; i < minLength; i++)
for (uint256 i = 0; i < minLength; i++)
if (a[i] < b[i]) return -1;
else if (a[i] > b[i]) return 1;
if (a.length < b.length) return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/validators/WebAuthValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ contract WebAuthValidator is VerifierCaller, IModuleValidator {

// parse out the important fields (type, challenge, and origin): https://goo.gl/yabPex
// TODO: test if the parse fails for more than 10 elements, otherwise can have a malicious header
(uint returnValue, JsmnSolLib.Token[] memory tokens, uint actualNum) = JsmnSolLib.parse(clientDataJSON, 20);
(uint256 returnValue, JsmnSolLib.Token[] memory tokens, uint256 actualNum) = JsmnSolLib.parse(clientDataJSON, 20);
if (returnValue != 0) {
return false;
}
Expand Down

0 comments on commit 94c65c2

Please sign in to comment.