Skip to content

Instantly share code, notes, and snippets.

@tarassh
Last active March 23, 2023 20:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tarassh/5d4f0bf32288771e9a5679da0bd82ac6 to your computer and use it in GitHub Desktop.
Save tarassh/5d4f0bf32288771e9a5679da0bd82ac6 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.11;
import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Burnable.sol";
contract NFT1155 is ERC1155Burnable {
/**
*
* @dev `uri_` tokens base URI pattern.
*/
constructor()
ERC1155("ipfs://b{id}")
{}
/**
*
* @dev Mint token using external token ID and amount.
*/
function mint(
address to,
uint256 tokenId,
uint256 amount
) public onlyOperator {
_mint(to, tokenId, amount, "");
}
function uri(uint256 _tokenID)
public
pure
override
returns (string memory)
{
string memory cidV1tokenID;
cidV1tokenID = cidv1encode(_tokenID);
return string(abi.encodePacked("ipfs://b", cidV1tokenID));
}
function cidv1encode(uint256 id) public pure returns (string memory) {
if (id == 0) return "aa";
uint8[32] memory alphabet = [
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,
50,
51,
52,
53,
54,
55
];
uint256 bits = 0;
uint256 buffer = 0;
uint256 header = 0x01551220;
uint8 _byte = 0;
bytes memory bstr = new bytes(58);
uint256 idx = 0;
uint256 mask = 31;
for (uint256 i = 0; i < 36; ++i) {
if (i < 4) {
_byte = uint8(header >> (32 - (i + 1) * 8));
} else {
_byte = uint8(id >> (256 - (i - 3) * 8));
}
buffer = (buffer << 8) | _byte;
bits += 8;
while (bits > 5) {
bits -= 5;
bstr[idx++] = bytes1(alphabet[mask & (buffer >> bits)]);
}
}
if (bits != 0) {
bstr[idx] = bytes1(alphabet[mask & (buffer << (5 - bits))]);
}
return string(bstr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment