Skip to content

Commit 896c68b

Browse files
committed
remove unnecessary event emitting / improve test cases
1 parent e1f8fb4 commit 896c68b

File tree

5 files changed

+34
-46
lines changed

5 files changed

+34
-46
lines changed

src/IPNFT.sol

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ contract IPNFT is ERC721URIStorageUpgradeable, ERC721BurnableUpgradeable, IReser
4646

4747
event Reserved(address indexed reserver, uint256 indexed reservationId);
4848
event IPNFTMinted(address indexed owner, uint256 indexed tokenId, string tokenURI, string symbol);
49-
event IPNFTPOI(uint256 indexed tokenId, bytes poi);
5049
event ReadAccessGranted(uint256 indexed tokenId, address indexed reader, uint256 until);
5150
event AuthorizerUpdated(address authorizer);
5251

@@ -114,27 +113,14 @@ contract IPNFT is ERC721URIStorageUpgradeable, ERC721BurnableUpgradeable, IReser
114113
* @param authorization a bytes encoded parameter that ensures that the poi is owned by the owner (to param)
115114
* @return computedTokenId
116115
*/
117-
function mintWithPOI(address to, bytes calldata poi, string calldata _tokenURI, string calldata _symbol, bytes calldata authorization)
116+
function mintWithPOI(address to, bytes32 poi, string calldata _tokenURI, string calldata _symbol, bytes calldata authorization)
118117
external
119118
payable
120119
whenNotPaused
121120
returns (uint256)
122121
{
123-
uint256 computedTokenId = uint256(keccak256(poi));
124-
if (msg.value < SYMBOLIC_MINT_FEE) {
125-
revert MintingFeeTooLow();
126-
}
127-
128-
if (!mintAuthorizer.authorizeMint(_msgSender(), to, abi.encode(SignedMintAuthorization(computedTokenId, _tokenURI, authorization)))) {
129-
revert Unauthorized();
130-
}
131-
132-
mintAuthorizer.redeem(authorization);
133-
symbol[computedTokenId] = _symbol;
134-
_mint(to, computedTokenId);
135-
_setTokenURI(computedTokenId, _tokenURI);
136-
emit IPNFTMinted(to, computedTokenId, _tokenURI, _symbol);
137-
emit IPNFTPOI(computedTokenId, poi);
122+
uint256 computedTokenId = uint256(poi);
123+
_handleMint(to, computedTokenId, _tokenURI, _symbol, authorization);
138124
return computedTokenId;
139125
}
140126

@@ -160,22 +146,27 @@ contract IPNFT is ERC721URIStorageUpgradeable, ERC721BurnableUpgradeable, IReser
160146
revert NotOwningReservation(reservationId);
161147
}
162148

149+
150+
_handleMint(to, reservationId, _tokenURI, _symbol, authorization);
151+
delete reservations[reservationId];
152+
return reservationId;
153+
}
154+
155+
function _handleMint(address to, uint256 tokenId, string calldata _tokenURI, string calldata _symbol, bytes calldata authorization) internal {
163156
if (msg.value < SYMBOLIC_MINT_FEE) {
164157
revert MintingFeeTooLow();
165158
}
166159

167-
if (!mintAuthorizer.authorizeMint(_msgSender(), to, abi.encode(SignedMintAuthorization(reservationId, _tokenURI, authorization)))) {
160+
if (!mintAuthorizer.authorizeMint(_msgSender(), to, abi.encode(SignedMintAuthorization(tokenId, _tokenURI, authorization)))) {
168161
revert Unauthorized();
169162
}
170163

171-
delete reservations[reservationId];
172-
symbol[reservationId] = _symbol;
164+
symbol[tokenId] = _symbol;
173165
mintAuthorizer.redeem(authorization);
174166

175-
_mint(to, reservationId);
176-
_setTokenURI(reservationId, _tokenURI);
177-
emit IPNFTMinted(to, reservationId, _tokenURI, _symbol);
178-
return reservationId;
167+
_mint(to, tokenId);
168+
_setTokenURI(tokenId, _tokenURI);
169+
emit IPNFTMinted(to, tokenId, _tokenURI, _symbol);
179170
}
180171

181172
/**

subgraph/schema.graphql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ type Ipnft @entity {
99
ipts: [IPT!] @derivedFrom(field: "ipnft")
1010
metadata: IpnftMetadata
1111
updatedAtTimestamp: BigInt
12-
poi: Bytes
1312
}
1413

1514
type IpnftMetadata @entity {

subgraph/src/ipnftMapping.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import {
1313
MetadataUpdate as MetadataUpdateEvent,
1414
ReadAccessGranted as ReadAccessGrantedEvent,
1515
Reserved as ReservedEvent,
16-
Transfer as TransferEvent,
17-
IPNFTPOI as IPNFTPOIEvent
16+
Transfer as TransferEvent
1817
} from '../generated/IPNFT/IPNFT'
1918
import { IpnftMetadata as IpnftMetadataTemplate } from '../generated/templates'
2019
import { CanRead, Ipnft, Reservation } from '../generated/schema'
@@ -104,17 +103,6 @@ export function handleMint(event: IPNFTMintedEvent): void {
104103
ipnft.save()
105104
}
106105

107-
export function handlePOI(event: IPNFTPOIEvent): void {
108-
let ipnft = Ipnft.load(event.params.tokenId.toString())
109-
if (!ipnft) {
110-
log.error('ipnft {} not found', [event.params.tokenId.toString()])
111-
return
112-
}
113-
114-
ipnft.poi = event.params.poi
115-
ipnft.save()
116-
}
117-
118106
export function handleMetadataUpdated(event: MetadataUpdateEvent): void {
119107
let ipnft = Ipnft.load(event.params._tokenId.toString())
120108
if (!ipnft) {

subgraph/subgraph.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ dataSources:
2323
handler: handleReservation
2424
- event: IPNFTMinted(indexed address,indexed uint256,string,string)
2525
handler: handleMint
26-
- event: IPNFTPOI(indexed uint256,bytes)
27-
handler: handlePOI
2826
- event: Transfer(indexed address,indexed address,indexed uint256)
2927
handler: handleTransfer
3028
- event: ReadAccessGranted(indexed uint256,indexed address,uint256)

test/IPNFT.t.sol

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,35 @@ contract IPNFTTest is IPNFTMintHelper {
7373
}
7474

7575
function testMintWithPoi() public {
76-
bytes memory poiHash = "0x5d7ca63d6e6065ef928d3f7cf770062ddd621b84de99732662a71b966db97c3b";
77-
uint256 tokenId = uint256(keccak256(poiHash));
78-
bytes memory authorization = abi.encode("0xsignature");
76+
bytes32 poiHash = 0x073cb54264ef688e56531a2d09ab47b14086b5c7813e3a23a2bd7b1bb6458a52;
77+
uint256 tokenId = uint256(poiHash);
78+
(, uint256 maliciousSignerPk) = makeAddrAndKey("malicious");
79+
bytes32 authMessageHash = ECDSA.toEthSignedMessageHash(keccak256(abi.encodePacked(alice, alice, tokenId, ipfsUri)));
80+
(uint8 v, bytes32 r, bytes32 s) = vm.sign(maliciousSignerPk, authMessageHash);
81+
bytes memory maliciousAuthorization = abi.encodePacked(r, s, v);
82+
7983
vm.startPrank(deployer);
80-
ipnft.setAuthorizer(new AcceptAllAuthorizer());
84+
ipnft.setAuthorizer(new SignedMintAuthorizer(deployer));
8185
vm.stopPrank();
8286

8387
vm.startPrank(alice);
8488
vm.expectRevert(IPNFT.MintingFeeTooLow.selector);
85-
ipnft.mintWithPOI(alice, poiHash, ipfsUri, DEFAULT_SYMBOL, authorization);
89+
ipnft.mintWithPOI(alice, poiHash, ipfsUri, DEFAULT_SYMBOL, maliciousAuthorization);
8690

91+
vm.expectRevert(IPNFT.Unauthorized.selector);
92+
ipnft.mintWithPOI{ value: MINTING_FEE }(alice, poiHash, ipfsUri, DEFAULT_SYMBOL, maliciousAuthorization);
93+
94+
(v, r, s) = vm.sign(deployerPk, authMessageHash);
95+
bytes memory authorization = abi.encodePacked(r, s, v);
8796
vm.expectEmit(true, true, false, true);
8897
emit IPNFTMinted(alice, tokenId, ipfsUri, DEFAULT_SYMBOL);
89-
ipnft.mintWithPOI{ value: MINTING_FEE }(alice, poiHash, ipfsUri, DEFAULT_SYMBOL, authorization);
98+
ipnft.mintWithPOI{ value: MINTING_FEE }(
99+
alice, poiHash, ipfsUri, DEFAULT_SYMBOL, authorization
100+
);
90101
assertEq(ipnft.ownerOf(tokenId), alice);
91102
assertEq(ipnft.tokenURI(tokenId), ipfsUri);
92103
assertEq(ipnft.symbol(tokenId), DEFAULT_SYMBOL);
104+
assertEq(tokenId, 3273451770044532981553402679345217193568252544895634663440128735015952812626);
93105
vm.stopPrank();
94106
}
95107

0 commit comments

Comments
 (0)