-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersonalwalletfactory.sol
208 lines (163 loc) · 6.64 KB
/
personalwalletfactory.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
pragma solidity ^0.4.24;
contract personalWallet {
using BytesLib for bytes;
address public owner;
address public recovery;
uint256 public nonce;
constructor(address _owner, address _recovery) public {
owner = _owner;
recovery = _recovery;
}
function isOwner(address _owner) public view returns(bool) {
return(_owner == owner);
}
function owners() public view returns(address[] memory _owners) {
_owners = new address[](1);
_owners[0] = owner;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
modifier onlyRecovery(){
require(msg.sender == recovery);
_;
}
function setOwner(address _owner) onlyRecovery external {
owner = _owner;
}
function setRecovery(address _recovery) onlyRecovery external {
recovery = _recovery;
}
event Execute(address to, uint256 value, bytes data);
function execute(address _to, uint256 _value, bytes _data) public onlyOwner {
require(_to.call.value(_value)(_data));
emit Execute(_to, _value, _data);
}
function batchExecute(
address[] _to, uint256[] _value,
uint256[] _idx, bytes _data) public onlyOwner {
uint256 start = 0;
for (uint256 i = 0; i < _idx.length; i++) {
require(_to[i].call.value(_value[i])(_data.slice(start, _idx[i])));
emit Execute(_to[i], _value[i], _data.slice(start, _idx[i]));
start += _idx[i];
}
}
function delegateExecute(
address _to, uint256 _value,
bytes _data, uint256 _nonce,
uint8 _v, bytes32 _r, bytes32 _s) public {
require(_nonce == nonce);
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 digest = keccak256(abi.encodePacked(_to, _value, _data, _nonce));
bytes32 txHash = keccak256(abi.encodePacked(prefix,digest));
require(ecrecover(txHash, _v, _r, _s) == owner);
require(_to.call.value(_value)(_data));
emit Execute(_to, _value, _data);
nonce = nonce + 1;
}
function batchDelegateExecute(
address[] _to, uint256[] _value,
uint256[] _idx, bytes _data, uint256 _nonce,
uint8 _v, bytes32 _r, bytes32 _s) public {
require(_nonce == nonce);
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 digest = keccak256(
abi.encodePacked(_to, _value, _idx, _data, _nonce));
bytes32 txHash = keccak256(abi.encodePacked(prefix, digest));
require(ecrecover(txHash, _v, _r, _s) == owner);
uint256 start = 0;
for (uint256 i = 0; i < _idx.length; i++) {
require(_to[i].call.value(_value[i])(_data.slice(start, _idx[i])));
emit Execute(_to[i], _value[i], _data.slice(start, _idx[i]));
start += _idx[i];
}
nonce = nonce + 1;
}
function () public payable {
}
// bytes4(keccak256("isValidSignature(bytes,bytes)")
bytes4 constant internal MAGICVALUE = 0x20c13b0b;
bytes4 constant internal FAILVALUE = 0xffffffff;
function isValidSignature(
bytes memory _data,
bytes memory _signature)
public view returns (bytes4 magicValue) {
bytes32 hash = keccak256(_data);
if (_signature.length != 65) return FAILVALUE;
bytes32 r;
bytes32 s;
uint8 v;
assembly {
r := mload(add(_signature, 0x20))
s := mload(add(_signature, 0x40))
v := byte(0, mload(add(_signature, 0x60)))
}
if(ecrecover(hash, v, r, s) == owner) return MAGICVALUE;
else return FAILVALUE;
}
}
contract personalWalletFactory {
mapping(address => bool) public isWallet;
function create(address _owner, address _recovery) public returns (address wallet) {
wallet = new personalWallet(_owner, _recovery);
isWallet[wallet] = true;
}
}
/*
* @title Solidity Bytes Arrays Utils
* @author Gonçalo Sá <goncalo.sa@consensys.net>
*
* @dev Bytes tightly packed arrays utility library for ethereum contracts written in Solidity.
* The library lets you concatenate, slice and type cast bytes arrays both in memory and storage.
*/
library BytesLib {
function slice(bytes _bytes, uint _start, uint _length) internal pure returns (bytes) {
require(_bytes.length >= (_start + _length));
bytes memory tempBytes;
assembly {
switch iszero(_length)
case 0 {
// Get a location of some free memory and store it in tempBytes as
// Solidity does for memory variables.
tempBytes := mload(0x40)
// The first word of the slice result is potentially a partial
// word read from the original array. To read it, we calculate
// the length of that partial word and start copying that many
// bytes into the array. The first word we copy will start with
// data we don't care about, but the last `lengthmod` bytes will
// land at the beginning of the contents of the new array. When
// we're done copying, we overwrite the full first word with
// the actual length of the slice.
let lengthmod := and(_length, 31)
// The multiplication in the next line is necessary
// because when slicing multiples of 32 bytes (lengthmod == 0)
// the following copy loop was copying the origin's length
// and then ending prematurely not copying everything it should.
let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod)))
let end := add(mc, _length)
for {
// The multiplication in the next line has the same exact purpose
// as the one above.
let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start)
} lt(mc, end) {
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
mstore(mc, mload(cc))
}
mstore(tempBytes, _length)
//update free-memory pointer
//allocating the array padded to 32 bytes like the compiler does now
mstore(0x40, and(add(mc, 31), not(31)))
}
//if we want a zero-length slice let's just return a zero-length array
default {
tempBytes := mload(0x40)
mstore(0x40, add(tempBytes, 0x20))
}
}
return tempBytes;
}
}