-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from kyonRay/master
Release v3.7.0
- Loading branch information
Showing
13 changed files
with
442 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 91 additions & 60 deletions
151
src/main/java/org/fisco/bcos/sdk/demo/contract/DmcTransfer.java
Large diffs are not rendered by default.
Oops, something went wrong.
156 changes: 82 additions & 74 deletions
156
src/main/java/org/fisco/bcos/sdk/demo/contract/MultiTableTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
src/main/java/org/fisco/bcos/sdk/demo/contract/sol/ECRecoverSMTest.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.6.0 <0.8.12; | ||
|
||
contract ECRecoverSMTest { | ||
event Address(address addr); | ||
|
||
function genValidParams() private pure returns(bytes memory){ | ||
bytes memory data = abi.encode( | ||
bytes32(0xaa0f7414b7f8648410f9818df3a1f43419d5c30313f430712033937ae57854c8), | ||
uint8(28), | ||
bytes32(0xacd0d6c91242e514655815073f5f0e9aed671f68a4ed3e3e9d693095779f704b), | ||
bytes32(0x01932751f4431c3b4c9d6fb1c826d138ee155ea72ac9013d66929f6a265386b4)); | ||
|
||
return data; | ||
} | ||
|
||
function genInvalidParams() private pure returns(bytes memory){ | ||
bytes memory data = abi.encodeWithSignature("ecrecover(bytes32,uint8,bytes32,bytes32)", | ||
bytes32(0xaa0f7414b7f8648410f9818df3a1f43419d5c30313f430712033937ae57854c8), | ||
uint8(28), | ||
bytes32(0xacd0d6c91242e514655815073f5f0e9aed671f68a4ed3e3e9d693095779f704b), | ||
bytes32(0x01932751f4431c3b4c9d6fb1c826d138ee155ea72ac9013d66929f6a265386b5)); | ||
|
||
return data; | ||
} | ||
|
||
function genLargerInvalidParams(uint addNum) private pure returns(bytes memory){ | ||
// append genValidParams() addNum byte | ||
bytes memory addBytes = new bytes(addNum); | ||
bytes memory data = abi.encodePacked(genValidParams(), addBytes); | ||
require(genValidParams().length + addNum == data.length, "genLargerInvalidParams failed"); | ||
|
||
return data; | ||
} | ||
|
||
function genShorterInvalidParams(uint cutNum) private pure returns(bytes memory){ | ||
// remove genValidParams() cutNum byte | ||
bytes memory validParams = genValidParams(); | ||
require(validParams.length >= cutNum, "genShorterInvalidParams failed"); | ||
bytes memory data = new bytes(validParams.length - cutNum); | ||
for(uint i = 0; i < validParams.length - cutNum; i++){ | ||
data[i] = validParams[i]; | ||
} | ||
|
||
require(genValidParams().length - cutNum == data.length, "genShorterInvalidParams failed"); | ||
return data; | ||
} | ||
|
||
function callECRecover(bytes memory params) public returns (address){ | ||
// 调用预编译合约的ECRecover函数 | ||
address precompiledContract = address(0x1); | ||
(bool success, bytes memory result) = precompiledContract.call(params); | ||
require(success, "Call to precompiled contract failed"); | ||
|
||
if (result.length == 0) { | ||
return address(0); | ||
} | ||
|
||
address addr; | ||
assembly { | ||
addr := mload(add(result, 32)) | ||
} | ||
|
||
return addr; | ||
} | ||
|
||
function callECRecoverValid() public returns (address){ | ||
bytes memory params = genValidParams(); | ||
return callECRecover(params); | ||
} | ||
|
||
function callECRecoverInvalid() public returns (address){ | ||
bytes memory params = genInvalidParams(); | ||
return callECRecover(params); // must return 0x0000000000000000000000000000000000000084 | ||
} | ||
|
||
function callECRecoverDirect() public pure returns (address){ | ||
bytes32 hash = bytes32(0xaa0f7414b7f8648410f9818df3a1f43419d5c30313f430712033937ae57854c8); | ||
uint8 v = uint8(28); | ||
bytes32 r = bytes32(0xacd0d6c91242e514655815073f5f0e9aed671f68a4ed3e3e9d693095779f704b); | ||
bytes32 s = bytes32(0x01932751f4431c3b4c9d6fb1c826d138ee155ea72ac9013d66929f6a265386b4); | ||
|
||
address addr = ecrecover(hash, v, r, s); | ||
require(addr == address(0x6DA0599583855f1618b380f6782c0c5c25cB96Ec), "ecrecover failed"); | ||
return addr; | ||
} | ||
|
||
function testRecoverEmptyParams() public { | ||
bytes memory data = abi.encode(); | ||
address addr = callECRecover(data); | ||
require(addr == address(0x0), "should recover failed"); | ||
} | ||
|
||
function testRecoverVRange() public { | ||
uint8[9] memory vs = [uint8(0), uint8(1), uint8(2), uint8(10), uint8(27), uint8(28), uint8(100), uint8(200), uint8(255)]; | ||
|
||
for(uint i = 0; i < vs.length; i++){ | ||
uint8 v = vs[i]; | ||
bytes memory data = abi.encode( | ||
bytes32(0xaa0f7414b7f8648410f9818df3a1f43419d5c30313f430712033937ae57854c8), | ||
v, | ||
bytes32(0xacd0d6c91242e514655815073f5f0e9aed671f68a4ed3e3e9d693095779f704b), | ||
bytes32(0x01932751f4431c3b4c9d6fb1c826d138ee155ea72ac9013d66929f6a265386b4)); | ||
|
||
address addr = callECRecover(data); | ||
// only v = 27 or 28 is valid | ||
if (v == 27){ | ||
require(addr == address(0xA5B4792Dcad4FE78D13f6aBD7bA1f302945De4F7), "ecrecover failed"); | ||
} else if (v == 28){ | ||
require(addr == address(0x6DA0599583855f1618b380f6782c0c5c25cB96Ec), "ecrecover failed"); | ||
} else { | ||
require(addr == address(0x0), "should recover failed"); | ||
} | ||
} | ||
} | ||
|
||
function testRecoverLargerParams() public { | ||
uint[10] memory addNums = [uint(1), uint(5), uint(32), uint(33), uint(64), uint(65), uint(256), uint(257), uint(512), uint(513)]; | ||
|
||
for(uint i = 0; i < addNums.length; i++){ | ||
uint addNum = addNums[i]; | ||
bytes memory data = genLargerInvalidParams(addNum); | ||
address addr = callECRecover(data); | ||
require(addr == address(0x6DA0599583855f1618b380f6782c0c5c25cB96Ec), "added bytes should be ignored"); | ||
} | ||
} | ||
|
||
function testRecoverShorterParams() public { | ||
uint[11] memory cutNums = [uint(1), uint(5), uint(10), uint(18), uint(27), uint(31), uint(32), uint(33), uint(63), uint(64), uint(65)]; | ||
|
||
for(uint i = 0; i < cutNums.length; i++){ | ||
uint cutNum = cutNums[i]; | ||
bytes memory data = genShorterInvalidParams(cutNum); | ||
address addr = callECRecover(data); | ||
// should failed when cutNum >= 32 | ||
|
||
if (cutNum == 1) { | ||
require(addr == address(0x509EAd8b20064F21e35f920cb0c6d6cbC0c0AA0D), "should success when cutNum == 1"); | ||
} else if (cutNum == 5) { | ||
require(addr == address(0x571A110cE923c9354B11B247f087b6DAB1AD9089), "should success when cutNum == 5"); | ||
} else if (cutNum == 10) { | ||
require(addr == address(0xFaCF3c4d9C0197bF621c39D461970E7a5D2F6947), "should success when cutNum == 10"); | ||
} else if (cutNum == 18) { | ||
require(addr == address(0xCc63021A8a9E4c5C58F275C1DbA8536D398C46f5), "should success when cutNum == 18"); | ||
} else if (cutNum == 27) { | ||
require(addr == address(0xc6a74652861114A92a30b8399e6eBe2e2e90313e), "should success when cutNum == 27"); | ||
} else if (cutNum == 31) { | ||
require(addr == address(0xC275cac475391eEEAdc7a4d0A09781177776D8b5), "should success when cutNum == 31"); | ||
} else { | ||
require(addr == address(0x0), "should failed when cutNum >= 32"); | ||
} | ||
|
||
} | ||
} | ||
|
||
function testRecoverBasic() public { | ||
require(callECRecoverValid() == address(0x6DA0599583855f1618b380f6782c0c5c25cB96Ec), "callECRecoverValid failed"); | ||
require(callECRecoverDirect() == address(0x6DA0599583855f1618b380f6782c0c5c25cB96Ec), "callECRecoverDirect failed"); | ||
} | ||
|
||
function testEmitEvent() public { | ||
emit Address(address(0x6DA0599583855f1618b380f6782c0c5c25cB96Ec)); | ||
callECRecoverDirect(); | ||
} | ||
|
||
function compareAddressAndString(address _address, string memory _string) public pure returns (bool) { | ||
bytes32 addressHash = keccak256(abi.encodePacked(_address)); | ||
bytes32 stringHash = keccak256(bytes(_string)); | ||
|
||
return (addressHash == stringHash); | ||
} | ||
|
||
function check() public { | ||
testRecoverBasic(); | ||
testRecoverEmptyParams(); | ||
testRecoverVRange(); | ||
testRecoverLargerParams(); | ||
testRecoverShorterParams(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.