Skip to content

Commit 4421d3f

Browse files
[hebaov2]add more test (#2352)
1 parent 8813e2a commit 4421d3f

File tree

11 files changed

+344
-16
lines changed

11 files changed

+344
-16
lines changed

packages/hebao_v2/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ node_modules/
55
artifacts/
66
cache/
77
build/
8+
ABI/
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright 2017 Loopring Technology Limited.
3+
pragma solidity ^0.7.0;
4+
5+
import "../lib/ERC1271.sol";
6+
import "../lib/OwnerManagable.sol";
7+
import "../lib/SignatureUtil.sol";
8+
9+
10+
/// @title OfficialGuardian
11+
/// @author Freeman Zhong - <[email protected]>
12+
contract OfficialGuardian is OwnerManagable, ERC1271
13+
{
14+
using SignatureUtil for bytes32;
15+
mapping (address => bool) public whitelist;
16+
17+
18+
/// @dev init owner for proxy contract:
19+
function initOwner(address _owner)
20+
external
21+
{
22+
require(owner == address(0), "INITIALIZED_ALREADY");
23+
owner = _owner;
24+
}
25+
26+
function isValidSignature(
27+
bytes32 _signHash,
28+
bytes memory _signature
29+
)
30+
public
31+
view
32+
override
33+
returns (bytes4)
34+
{
35+
return isManager(_signHash.recoverECDSASigner(_signature))?
36+
ERC1271_MAGICVALUE:
37+
bytes4(0);
38+
}
39+
40+
function addWhitelist(address target, bool toAdd)
41+
external
42+
onlyOwner
43+
{
44+
require(target != address(0), "ZERO_ADDRESS");
45+
require(whitelist[target] != toAdd, "SAME_VALUE");
46+
whitelist[target] = toAdd;
47+
}
48+
49+
function transact(
50+
address target,
51+
uint value,
52+
bytes calldata data
53+
)
54+
external
55+
onlyManager
56+
returns (
57+
bool success,
58+
bytes memory returnData
59+
)
60+
{
61+
require(whitelist[target], "INVALID_TARGET");
62+
// solium-disable-next-line security/no-call-value
63+
(success, returnData) = target.call{value: value}(data);
64+
}
65+
}

packages/hebao_v2/contracts/base/SmartWallet.sol

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ contract SmartWallet is ERC1271
5252
Wallet public wallet;
5353
// ----- DATA LAYOUT ENDS -----
5454

55-
// ---- Events definitions ----
56-
event GuardianAdded (address guardian, uint effectiveTime);
57-
event GuardianRemoved (address guardian, uint effectiveTime);
58-
// ---- Events definitions end ----
59-
6055
/// @dev We need to make sure the implemenation contract cannot be initialized
6156
/// and used to do delegate calls to arbitrary contracts.
6257
modifier disableInImplementationContract

packages/hebao_v2/contracts/base/libwallet/MetaTxLib.sol

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import "../../thirdparty/BytesUtil.sol";
1212
import "./WalletData.sol";
1313
import "./ERC20Lib.sol";
1414
import "./QuotaLib.sol";
15+
import "../SmartWallet.sol";
1516

1617

1718
/// @title MetaTxLib
@@ -31,7 +32,7 @@ library MetaTxLib
3132

3233
event MetaTxExecuted(
3334
address relayer,
34-
uint nonce,
35+
bytes32 metaTxHash,
3536
bool success,
3637
uint gasUsed
3738
);
@@ -56,6 +57,7 @@ library MetaTxLib
5657
)
5758
public
5859
view
60+
returns (bytes32)
5961
{
6062
bytes memory encoded = abi.encode(
6163
META_TX_TYPEHASH,
@@ -71,6 +73,7 @@ library MetaTxLib
7173
);
7274
bytes32 metaTxHash = EIP712.hashPacked(DOMAIN_SEPARATOR, encoded);
7375
require(metaTxHash.verifySignature(wallet.owner, metaTx.signature), "INVALID_SIGNATURE");
76+
return metaTxHash;
7477
}
7578

7679
function executeMetaTx(
@@ -90,17 +93,19 @@ library MetaTxLib
9093
require(gasLeft >= (metaTx.gasLimit.mul(64) / 63), "OPERATOR_INSUFFICIENT_GAS");
9194

9295
// Update the nonce before the call to protect against reentrancy
96+
require(isNonceValid(wallet, msg.sender, metaTx.nonce, metaTx.data.toBytes4(0)), "INVALID_NONCE");
9397
if (metaTx.nonce != 0) {
94-
require(isNonceValid(wallet, msg.sender, metaTx.nonce), "INVALID_NONCE");
9598
wallet.nonce[msg.sender] = metaTx.nonce;
99+
} else {
100+
require(metaTx.requiresSuccess, "META_TX_WITHOUT_NONCE_REQUIRES_SUCCESS");
96101
}
97102

98103
(success, ) = metaTx.to.call{gas : metaTx.gasLimit}(metaTx.data);
99104

100105
// These checks are done afterwards to use the latest state post meta-tx call
101106
require(!wallet.locked, "WALLET_LOCKED");
102107

103-
validateMetaTx(
108+
bytes32 metaTxHash = validateMetaTx(
104109
wallet,
105110
DOMAIN_SEPARATOR,
106111
metaTx
@@ -124,7 +129,7 @@ library MetaTxLib
124129

125130
emit MetaTxExecuted(
126131
msg.sender,
127-
metaTx.nonce,
132+
metaTxHash,
128133
success,
129134
gasUsed
130135
);
@@ -149,12 +154,27 @@ library MetaTxLib
149154
function isNonceValid(
150155
Wallet storage wallet,
151156
address relayer,
152-
uint nonce
157+
uint nonce,
158+
bytes4 methodId
153159
)
154160
public
155161
view
156162
returns (bool)
157163
{
158-
return nonce > wallet.nonce[relayer] && (nonce >> 128) <= block.number;
164+
if ( methodId == SmartWallet.changeMasterCopy.selector ||
165+
methodId == SmartWallet.addGuardianWA.selector ||
166+
methodId == SmartWallet.removeGuardianWA.selector ||
167+
methodId == SmartWallet.unlock.selector ||
168+
methodId == SmartWallet.changeDailyQuotaWA.selector ||
169+
methodId == SmartWallet.recover.selector ||
170+
methodId == SmartWallet.addToWhitelistWA.selector ||
171+
methodId == SmartWallet.transferTokenWA.selector ||
172+
methodId == SmartWallet.callContractWA.selector ||
173+
methodId == SmartWallet.approveTokenWA.selector ||
174+
methodId == SmartWallet.approveThenCallContractWA.selector ) {
175+
return nonce == 0;
176+
} else {
177+
return nonce > wallet.nonce[relayer] && (nonce >> 128) <= block.number;
178+
}
159179
}
160180
}

packages/hebao_v2/genAbi.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
3+
rm -rf ABI/*
4+
5+
solc \
6+
-o ABI/ --overwrite \
7+
--abi contracts/base/*.sol \
8+
--allow-paths contracts/base/libwallet/*.sol contracts/thirdparty/proxy/*.sol contracts/thirdparty/*.sol contracts/lib/*.sol contracts/iface/*.sol
9+
10+
ABI_PATH="ABI"
11+
12+
for file in $ABI_PATH/*
13+
do
14+
rename_file=$(echo $file | awk '{n=split($0,a,"_"); print a[n]}')
15+
mv $file $rename_file
16+
done

0 commit comments

Comments
 (0)