-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProductFactory.sol
257 lines (227 loc) · 7.67 KB
/
ProductFactory.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.24;
import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol";
import {EIP712Upgradeable} from "@openzeppelin/contracts-upgradeable/utils/cryptography/EIP712Upgradeable.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol";
import {PausableUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/PausableUpgradeable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {IProductFactory} from "./IProductFactory.sol";
import {IProduct} from "./IProduct.sol";
contract ProductFactory is
IProductFactory,
OwnableUpgradeable,
EIP712Upgradeable,
ReentrancyGuardUpgradeable,
PausableUpgradeable,
UUPSUpgradeable
{
string public constant DEPHY_PREFIX = "DEPHY_ID_SIGNED_MESSAGE:";
bytes32 public constant ACTIVATE_DEVICE_TYPEHASH =
keccak256(
bytes(
"ActivateDevice(address device,bytes deviceSignature,uint256 deviceDeadline,uint256 deadline)"
)
);
mapping(address => address) internal _vendorByProduct;
mapping(address => DeviceBinding) internal _deviceBindings;
function initialize(address initialOwner) public virtual initializer {
__Ownable_init(initialOwner);
__EIP712_init("ProductFactory", "1");
__ReentrancyGuard_init();
__Pausable_init();
__UUPSUpgradeable_init();
}
modifier onlyVendor(address product) {
if (msg.sender != _vendorByProduct[product]) {
revert NotVendor();
}
_;
}
/**
* @inheritdoc IProductFactory
*/
function getDomainSeparator() external view returns (bytes32) {
return _domainSeparatorV4();
}
/**
* @inheritdoc IProductFactory
*/
function createProduct(
CreateProductArgs memory args
) public nonReentrant whenNotPaused returns (address) {
if (
!IProduct(args.productImpl).supportsInterface(
type(IProduct).interfaceId
)
) {
revert NotProductTemplate();
}
address product = Clones.clone(args.productImpl);
IProduct(product).initialize(args.name, args.symbol, args.baseTokenURI);
_vendorByProduct[product] = msg.sender;
emit ProductCreated(msg.sender, args.productImpl, product);
return product;
}
/**
* @inheritdoc IProductFactory
*/
function createDevice(
CreateDeviceArgs memory args
) public nonReentrant whenNotPaused onlyVendor(args.product) {
_createDevice(args.product, args.device);
}
/**
* @inheritdoc IProductFactory
*/
function createDevices(
CreateDevicesArgs memory args
) public nonReentrant whenNotPaused onlyVendor(args.product) {
for (uint256 i = 0; i < args.devices.length; ++i) {
_createDevice(args.product, args.devices[i]);
}
}
/**
* @inheritdoc IProductFactory
*/
function createActivatedDevice(
CreateActivatedDeviceArgs memory args
)
public
nonReentrant
whenNotPaused
onlyVendor(args.product)
returns (uint256)
{
return _createActivatedDevice(args.product, args.device, args.receiver);
}
/**
* @inheritdoc IProductFactory
*/
function createActivatedDevices(
CreateActivatedDevicesArgs memory args
) public nonReentrant whenNotPaused onlyVendor(args.product) {
for (uint256 i = 0; i < args.devices.length; ++i) {
_createActivatedDevice(
args.product,
args.devices[i],
args.receivers[i]
);
}
}
/**
* @inheritdoc IProductFactory
*/
function activateDevice(
ActivateDeviceArgs memory args,
EIP712Signature memory signature
) public nonReentrant whenNotPaused {
address recoveredAddr = _recoverEIP712Signer(
_hashTypedDataV4(
keccak256(
abi.encode(
ACTIVATE_DEVICE_TYPEHASH,
args.device,
keccak256(args.deviceSignature),
args.deviceDeadline,
signature.deadline
)
)
),
signature
);
if (signature.signer != recoveredAddr) {
revert EIP712SignatureMismatch();
}
address recoveredDeviceAddr = _recoverDeviceSigner(
_hashTypedDeviceMessage(keccak256(abi.encode(args.deviceDeadline))),
args.deviceSignature,
args.deviceDeadline
);
if (args.device != recoveredDeviceAddr) {
revert DeviceSignatureMismatch();
}
DeviceBinding memory deviceBinding = _deviceBindings[args.device];
IProduct(deviceBinding.product).transferFrom(
address(this),
recoveredAddr,
deviceBinding.tokenId
);
emit DeviceActivated(deviceBinding.product, args.device, recoveredAddr);
}
/**
* @inheritdoc IProductFactory
*/
function getVendorByProduct(address product) public view returns (address) {
return _vendorByProduct[product];
}
/**
* @inheritdoc IProductFactory
*/
function getDeviceBinding(
address device
) public view returns (DeviceBinding memory) {
return _deviceBindings[device];
}
function _createDevice(
address product,
address device
) internal returns (uint256 tokenId) {
if (_deviceBindings[device].product != address(0)) {
revert DeviceAlreadyCreated();
}
tokenId = IProduct(product).mint(address(this));
_deviceBindings[device] = DeviceBinding({
product: product,
tokenId: tokenId
});
emit DeviceCreated(product, device, tokenId);
}
function _createActivatedDevice(
address product,
address device,
address receiver
) internal returns (uint256 tokenId) {
if (_deviceBindings[device].product != address(0)) {
revert DeviceAlreadyCreated();
}
tokenId = IProduct(product).mint(receiver);
_deviceBindings[device] = DeviceBinding({
product: product,
tokenId: tokenId
});
emit DeviceCreated(product, device, tokenId);
emit DeviceActivated(product, device, receiver);
}
function _hashTypedDeviceMessage(
bytes32 hashedMessage
) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(DEPHY_PREFIX, hashedMessage));
}
function _recoverDeviceSigner(
bytes32 digest,
bytes memory signature,
uint256 deadline
) internal view returns (address) {
if (deadline < block.timestamp) revert DeviceSignatureExpired();
return ECDSA.recover(digest, signature);
}
function _recoverEIP712Signer(
bytes32 digest,
EIP712Signature memory signature
) internal view returns (address) {
if (signature.deadline < block.timestamp)
revert EIP712SignatureExpired();
address recoveredAddress = ecrecover(
digest,
signature.v,
signature.r,
signature.s
);
return recoveredAddress;
}
function _authorizeUpgrade(
address newImplementation
) internal virtual override onlyOwner {}
}