// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
/// @notice On-chain receipt rendering utilities. Changeme uses compact base64 JSON metadata;
/// the pure SVG helpers remain available to library consumers but are not part of its tokenURI.
library ReceiptArt {
bytes internal constant B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
uint256 internal constant LINE_CHARS = 24;
function tokenURI(string memory wish)
internal
pure
returns (string memory)
{
bytes memory json = abi.encodePacked(
'{"name":"changeme","description":"',
escapeJson(wish),
'","background_color":"BFE3FF"}'
);
return string(abi.encodePacked("data:application/json;base64,", base64(json)));
}
function image(uint256 tokenId, uint256 round, string memory wish) internal pure returns (string memory) {
return string(
abi.encodePacked(
'"
)
);
}
/// @dev The mint limit fits in six lines. Preserve spaces within each line.
function _lines(bytes memory wish) private pure returns (bytes memory out) {
uint256 length = wish.length;
for (uint256 start; start < length; start += LINE_CHARS) {
uint256 end = start + LINE_CHARS;
if (end > length) end = length;
out = bytes.concat(
out, '', _escape(wish, start, end, true), ""
);
}
}
function escapeXml(bytes memory text) internal pure returns (bytes memory) {
return _escape(text, 0, text.length, true);
}
function escapeJson(string memory text) internal pure returns (bytes memory) {
bytes memory out = new bytes(bytes(text).length * 2);
assembly ("memory-safe") {
let dest := add(out, 32)
let end := add(add(text, 32), mload(text))
for { let cursor := add(text, 32) } lt(cursor, end) { cursor := add(cursor, 1) } {
let c := byte(0, mload(cursor))
if or(lt(c, 0x20), gt(c, 0x7e)) { c := 0x3f }
if or(eq(c, 0x22), eq(c, 0x5c)) {
mstore8(dest, 0x5c)
dest := add(dest, 1)
}
mstore8(dest, c)
dest := add(dest, 1)
}
mstore(out, sub(dest, add(out, 32)))
}
return out;
}
function _escape(bytes memory text, uint256 start, uint256 end, bool xml) private pure returns (bytes memory out) {
// The longest XML entity is six bytes. JSON needs at most two bytes per byte.
out = new bytes((end - start) * (xml ? 6 : 2));
assembly ("memory-safe") {
let input := add(text, 32)
let dest := add(out, 32)
for { let i := start } lt(i, end) { i := add(i, 1) } {
// End the load at the current character to stay inside the input.
let c := and(mload(sub(add(input, i), 31)), 0xff)
let sequence := c
let width := 1
if or(lt(c, 0x20), gt(c, 0x7e)) { sequence := 0x3f }
switch xml
case 0 {
// Prefix JSON quotes and backslashes with a backslash.
if or(eq(c, 0x22), eq(c, 0x5c)) {
sequence := or(0x5c00, c)
width := 2
}
}
default {
switch c
case 0x26 {
sequence := 0x26616d703b
width := 5
} // &
case 0x3c {
sequence := 0x266c743b
width := 4
} // <
case 0x3e {
sequence := 0x2667743b
width := 4
} // >
case 0x22 {
sequence := 0x2671756f743b
width := 6
} // "
case 0x27 {
sequence := 0x262333393b
width := 5
} // '
}
for { let remaining := width } remaining {} {
remaining := sub(remaining, 1)
mstore8(dest, shr(mul(remaining, 8), sequence))
dest := add(dest, 1)
}
}
mstore(out, sub(dest, add(out, 32)))
}
}
function toString(uint256 value) internal pure returns (string memory out) {
// A uint256 has at most 78 decimal digits. Write them from right to left,
// then put the string length in the word immediately before the first digit.
assembly ("memory-safe") {
let buffer := mload(0x40)
let end := add(buffer, 128)
let cursor := end
for {} 1 {} {
cursor := sub(cursor, 1)
mstore8(cursor, add(48, mod(value, 10)))
value := div(value, 10)
if iszero(value) { break }
}
out := sub(cursor, 32)
mstore(out, sub(end, cursor))
mstore(end, 0)
mstore(0x40, add(end, 32))
}
}
function toHexString(address account) internal pure returns (string memory out) {
out = new string(42);
assembly ("memory-safe") {
let start := add(out, 32)
mstore8(start, 0x30)
mstore8(add(start, 1), 0x78)
let digits := 0x3031323334353637383961626364656600000000000000000000000000000000
let cursor := add(start, 42)
for {} gt(cursor, add(start, 2)) {} {
cursor := sub(cursor, 1)
mstore8(cursor, byte(and(account, 15), digits))
account := shr(4, account)
}
}
}
function base64(bytes memory data) internal pure returns (string memory out) {
bytes memory table = B64;
out = new string(4 * ((data.length + 2) / 3));
assembly ("memory-safe") {
// Offset the table pointer so each query reads the character at the end of the word.
// Use the last byte of each load to stay within the allocated table.
function encode(alphabet, dest, triple) {
mstore8(dest, mload(add(alphabet, shr(18, triple))))
mstore8(add(dest, 1), mload(add(alphabet, and(shr(12, triple), 63))))
mstore8(add(dest, 2), mload(add(alphabet, and(shr(6, triple), 63))))
mstore8(add(dest, 3), mload(add(alphabet, and(triple, 63))))
}
let alphabet := add(table, 1)
let input := add(data, 32)
let end := add(input, mload(data))
let dest := add(out, 32)
for {} lt(add(input, 2), end) {
input := add(input, 3)
dest := add(dest, 4)
} {
encode(alphabet, dest, and(mload(sub(input, 29)), 0xffffff))
}
let remaining := sub(end, input)
if remaining {
let padding := sub(3, remaining)
let mask := sub(shl(mul(8, remaining), 1), 1)
let triple := shl(mul(8, padding), and(mload(sub(end, 32)), mask))
encode(alphabet, dest, triple)
mstore8(add(dest, 3), 0x3d)
if eq(padding, 2) { mstore8(add(dest, 2), 0x3d) }
}
}
}
}