Skip to content

Commit

Permalink
contracts: create abstract stamp
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoAcosta committed Oct 11, 2024
1 parent bf95b76 commit 4158302
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
13 changes: 10 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"wake.compiler.solc.remappings": [
"forge-std/=lib/forge-std/src/"
]
}
"@openzeppelin/=lib/openzeppelin-contracts/",
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
"erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
"forge-std/=lib/forge-std/src/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/"
],
"solidity.packageDefaultDependenciesContractsDirectory": "src",
"solidity.packageDefaultDependenciesDirectory": "lib"
}
69 changes: 69 additions & 0 deletions src/stamps/Stamp.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";

abstract contract Stamp is ERC721Enumerable, EIP712 {
using ECDSA for bytes32;

// Signer address
address public immutable signer;

constructor(
string memory stampName,
string memory stampSymbol,
string memory eip712version,
address _signer
) ERC721(stampName, stampSymbol) EIP712("Plasa Stamps", eip712version) {
signer = _signer;
}

// Abstract function to be implemented by child contracts
function getTypedDataHash(
bytes memory data
) public view virtual returns (bytes32);

// Custom error for when a user has already minted a stamp
error AlreadyMintedStamp(address user, uint256 stampId);
// New custom error for invalid signature
error InvalidSignature();

// Internal minting function
function _mintStamp(
address to,
bytes calldata data,
bytes calldata signature
) internal virtual returns (uint256) {
if (!_verify(data, signature)) {
revert InvalidSignature();
}

if (balanceOf(to) > 0) {
revert AlreadyMintedStamp(to, tokenOfOwnerByIndex(to, 0));
}

uint256 newStampId = totalSupply() + 1;
_safeMint(to, newStampId);

return newStampId;
}

// Signature verification
function _verify(
bytes calldata data,
bytes calldata signature
) internal view returns (bool) {
return
signer ==
_hashTypedDataV4(getTypedDataHash(data)).recover(signature);
}

// Override required by Solidity
function supportsInterface(
bytes4 interfaceId
) public view virtual override(ERC721Enumerable) returns (bool) {
return super.supportsInterface(interfaceId);
}
}

0 comments on commit 4158302

Please sign in to comment.