Skip to content

Commit

Permalink
update resourceId type from uint256 to String
Browse files Browse the repository at this point in the history
  • Loading branch information
cromatikap committed Jul 1, 2024
1 parent 7618f17 commit e1d551e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion contracts/GatedKnowledgeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract GatedKnowledgeManager is ERC4908, ERC721URIStorage {

function mint(
address author,
uint256 resourceId,
string calldata resourceId,
address to
) public payable override(ERC4908) {
super.mint(author, resourceId, to);
Expand Down
20 changes: 10 additions & 10 deletions contracts/erc4908/ERC4908.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import {IERC4908} from "./IERC4908.sol";

abstract contract ERC4908 is IERC4908, ERC721, ERC721Enumerable {
struct Settings {
uint256 resourceId;
string resourceId;
uint256 price;
uint32 expirationTime;
}
mapping(bytes32 hash => Settings) public accessControl;
mapping(bytes32 => Settings) public accessControl;

// struct attached to each NFT id
struct Metadata {
bytes32 hash;
uint256 resourceId;
string resourceId;
uint32 expirationTime;
}

Expand All @@ -29,14 +29,14 @@ abstract contract ERC4908 is IERC4908, ERC721, ERC721Enumerable {

function _hash(
address author,
uint256 resourceId
string calldata resourceId
) private pure returns (bytes32) {
return keccak256(abi.encodePacked(author, resourceId));
}

function _setAccess(
address author,
uint256 resourceId,
string calldata resourceId,
uint256 price,
uint32 expirationTime
) private {
Expand All @@ -45,20 +45,20 @@ abstract contract ERC4908 is IERC4908, ERC721, ERC721Enumerable {
}

function setAccess(
uint256 resourceId,
string calldata resourceId,
uint256 price,
uint32 expirationTime
) external {
_setAccess(msg.sender, resourceId, price, expirationTime);
}

function existAccess(bytes32 hash) external view returns (bool) {
return accessControl[hash].resourceId != 0;
return bytes(accessControl[hash].resourceId).length != 0;
}

function mint(
address author,
uint256 resourceId,
string calldata resourceId,
address to
) public payable virtual {
bytes32 settingsIndex = _hash(author, resourceId);
Expand All @@ -84,7 +84,7 @@ abstract contract ERC4908 is IERC4908, ERC721, ERC721Enumerable {

function hasAccess(
address author,
uint256 resourceId,
string calldata resourceId,
address consumer
) external view returns (bool response, string memory message) {
bytes32 hash = _hash(author, resourceId);
Expand All @@ -107,7 +107,7 @@ abstract contract ERC4908 is IERC4908, ERC721, ERC721Enumerable {
return (false, "user doesn't own the NFT");
}

function delAccess(uint256 resourceId) external {
function delAccess(string calldata resourceId) external {
bytes32 hash = _hash(msg.sender, resourceId);
delete accessControl[hash];
}
Expand Down
10 changes: 5 additions & 5 deletions contracts/erc4908/IERC4908.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ interface IERC4908 {
/// @param price The mint price, in other terms the access price for this particular content
/// @param expirationTime The expiration time of the access
function setAccess(
uint256 resourceId,
string calldata resourceId,
uint256 price,
uint32 expirationTime
) external;

/// @notice Disallows content access NFT to be minted, the remaining NFTs can still be used
/// @dev This function is meant to be called by the content author
/// @param resourceId The content identification from the off-chain content service provider
function delAccess(uint256 resourceId) external;
function delAccess(string calldata resourceId) external;

/// @notice Check for the access to a particular content from a particular consumer
/// @dev This function is meant to be called by the content provider, the 2 first parameters
Expand All @@ -30,9 +30,9 @@ interface IERC4908 {
/// @return message A message indicating the access status: "access doesn't exist", "access is expired", "access granted" or "user doesn't own the NFT"
function hasAccess(
address author,
uint256 resourceId,
string calldata resourceId,
address consumer
) external view returns (bool response, string memory message);
) external view returns (bool response, string calldata message);

/// @notice Check if the given access hash exists
/// @dev This function is called internally but can be also handy for external use
Expand All @@ -47,7 +47,7 @@ interface IERC4908 {
/// @param to The address of the content consumer
function mint(
address author,
uint256 resourceId,
string calldata resourceId,
address to
) external payable;

Expand Down
18 changes: 9 additions & 9 deletions test/ERC4908Example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("ERC4908", function () {
const { resourceId, price, expirationTime } = paramsDefault;

const expectedHash = keccak256(encodePacked(
['address', 'uint256'],
['address', 'string'],
[wallet.account.address, resourceId]
));

Expand All @@ -50,7 +50,7 @@ describe("ERC4908", function () {
const { resourceId, price, expirationTime } = paramsDefault;

const expectedHash = keccak256(encodePacked(
['address', 'uint256'],
['address', 'string'],
[wallet.account.address, resourceId]
));

Expand All @@ -64,7 +64,7 @@ describe("ERC4908", function () {

expect(before).to.equal(false);
expect(after).to.equal(true);
})
});

it("Should delete access", async function () {

Expand All @@ -74,7 +74,7 @@ describe("ERC4908", function () {
const { resourceId, price, expirationTime } = paramsDefault;

const expectedHash = keccak256(encodePacked(
['address', 'uint256'],
['address', 'string'],
[wallet.account.address, resourceId]
));

Expand All @@ -100,7 +100,7 @@ describe("ERC4908", function () {
expect(before.settings[0]).to.equal(resourceId);
expect(before.settings[1]).to.equal(price);
expect(before.settings[2]).to.equal(expirationTime);
expect(after.settings[0]).to.equal(0n);
expect(after.settings[0]).to.equal("");
expect(after.settings[1]).to.equal(0n);
expect(after.settings[2]).to.equal(0);
});
Expand Down Expand Up @@ -137,7 +137,7 @@ describe("ERC4908", function () {
/* Assert */

await expect(mintUnavailableContent).to.be.rejectedWith(
'MintUnavailable("0x320723cfc0bfa9b0f7c5b275a01ffa5e0f111f05723ba5df2b2684ab86bebe06")'
'MintUnavailable("0x64b7b2d2900f927ae778f84917c8327d63cbb08f59126c14ead77f45b28ab7dd")'
);
await expect(mintAvailableContent).to.be.fulfilled;
});
Expand All @@ -161,7 +161,7 @@ describe("ERC4908", function () {
Alice.account.address,
resourceId,
Bob.account.address
], { value: price - 1n})
], { value: price - 1n })

const mintSufficientFunds = bob.write.mint([
Alice.account.address,
Expand Down Expand Up @@ -245,5 +245,5 @@ describe("ERC4908", function () {
expect(hasAccessAfterExpiration).to.equal(false);
expect(messageAfterExpiration).to.equal("access is expired");
});
})
})
});
});
2 changes: 1 addition & 1 deletion test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const impersonate = async (contract: any, account: any) =>


const paramsDefault = {
resourceId: BigInt(1),
resourceId: "7f0e683bd119688847070f0a4476d078b95399a2843ca1c549cdcdbafee0792f",
price: BigInt(2),
expirationTime: Math.floor(Date.now() / 1000) + 3600 // 1 hour in to future
};
Expand Down

0 comments on commit e1d551e

Please sign in to comment.