Currently, I'm working with hashing string related which required the string to be precise(even the new line symbols).
For example I have the following code to read:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract GetSet {
uint256 private total;
mapping(address => uint256) private numbers;
mapping(uint256 => uint256) private heavyArr;
function getTotal() external view returns (uint256) {
return total;
}
function setNumber(uint256 _num) external returns (bool) {
total = total - numbers[msg.sender] + _num;
numbers[msg.sender] = _num;
return true;
}
function setHeavy() external returns (bool) {
for(uint i = 0; i < 100; i++) {
heavyArr[i] = heavyArr[i] + 1;
}
return true;
}
function getNumber(address _user) external view returns (uint256) {
return numbers[_user];
}
}
I tried to get this content when I pasted it into the textarea on the form and receive it from req.body in NodeJs. I already used
app.use(bodyParser.json());
console.log(JSON.stringify(contractCode));
to see the new line symbol, it return the following:
// SPDX-License-Identifier: MIT\r\npragma solidity ^0.8.0;\r\n\r\ncontract GetSet {\r\n uint256 private total;\r\n mapping(address => uint256) private numbers;\r\n mapping(uint256 => uint256) private heavyArr;\r\n\r\n function getTotal() external view returns (uint256) {\r\n return total;\r\n }\r\n\r\n function setNumber(uint256 _num) external returns (bool) {\r\n total = total - numbers[msg.sender] + _num;\r\n numbers[msg.sender] = _num;\r\n return true;\r\n }\r\n\r\n function setHeavy() external returns (bool) {\r\n for(uint i = 0; i < 100; i++) {\r\n heavyArr[i] = heavyArr[i] + 1;\r\n }\r\n return true;\r\n }\r\n\r\n function getNumber(address _user) external view returns (uint256) {\r\n return numbers[_user];\r\n }\r\n}
The second one is when I put the code in the file and read from it with
console.log(JSON.stringify(fs.readFileSync('GetSet.sol', 'utf8')));
It return the following:
// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ncontract GetSet {\n uint256 private total;\n mapping(address => uint256) private numbers;\n mapping(uint256 => uint256) private heavyArr;\n\n function getTotal() external view returns (uint256) {\n return total;\n }\n\n function setNumber(uint256 _num) external returns (bool) {\n total = total - numbers[msg.sender] + _num;\n numbers[msg.sender] = _num;\n return true;\n }\n\n function setHeavy() external returns (bool) {\n for(uint i = 0; i < 100; i++) {\n heavyArr[i] = heavyArr[i] + 1;\n }\n return true;\n }\n\n function getNumber(address _user) external view returns (uint256) {\n return numbers[_user];\n }\n}
as you can see, the return strings from textarea req.body and the read file are different with new line symbol(req.body have \r\n while read file only have \n)
I don't understand why the 2 ways of getting the text return different results, and how to make them similar(because I need them to be similar to match the hash string every time). I searched and there doesn't seem to be any answer on why textarea/req.body has a different new line symbol so currently I'm kinda stuck on this issue.
The only thing I was able to look up is this which explain what those new line symbols mean https://stackoverflow.com/a/15433263/11225821
