@artblocks/contracts 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/artifacts/contracts/DependencyRegistryV0.sol/DependencyRegistryV0.json +61 -148
  2. package/artifacts/contracts/generator/GenArt721GeneratorV0.sol/GenArt721GeneratorV0.json +15 -2
  3. package/artifacts/contracts/generator/InPlaceDocument.sol/InPlaceDocument.json +10 -0
  4. package/artifacts/contracts/interfaces/v0.8.x/IAbxfsFileDataReader.sol/IAbxfsFileDataReader.json +109 -0
  5. package/artifacts/contracts/interfaces/v0.8.x/IAbxfsFileStore.sol/IAbxfsFileStore.json +86 -0
  6. package/artifacts/contracts/interfaces/v0.8.x/IDependencyRegistryV0.sol/IDependencyRegistryV0.json +41 -27
  7. package/artifacts/contracts/libs/v0.8.x/AbxfsCanonical.sol/AbxfsCanonical.json +10 -0
  8. package/artifacts/contracts/libs/v0.8.x/DependencyRegistryStorageLib.sol/DependencyRegistryStorageLib.json +2 -2
  9. package/artifacts/contracts/mock/CallProbeMock.sol/CallProbeMock.json +45 -0
  10. package/artifacts/contracts/mock/GasProbeMock.sol/GasProbeMock.json +45 -0
  11. package/artifacts/contracts/mock/GenArt721GeneratorV0ScriptyReferenceMock.sol/GenArt721GeneratorV0ScriptyReferenceMock.json +346 -0
  12. package/artifacts/contracts/mock/GenArt721GeneratorV0ScriptyReferenceMock.sol/ILegacyGenArt721CoreV2_PBAB_Flex.json +47 -0
  13. package/artifacts/contracts/mock/InPlaceDocumentHarnessMock.sol/InPlaceDocumentHarnessMock.json +161 -0
  14. package/artifacts/contracts/mock/MockAbxfsFileDataReader.sol/IMockFileStore.json +91 -0
  15. package/artifacts/contracts/mock/MockAbxfsFileDataReader.sol/MockAbxfsFileDataReader.json +183 -0
  16. package/artifacts/contracts/mock/MockAbxfsFileStore.sol/MockAbxfsFileStore.json +204 -0
  17. package/artifacts/contracts/mock/RawResponseMock.sol/RawResponseMock.json +61 -0
  18. package/contracts/DependencyRegistryV0.sol +136 -226
  19. package/contracts/generator/GenArt721GeneratorV0.sol +541 -280
  20. package/contracts/generator/InPlaceDocument.sol +306 -0
  21. package/contracts/interfaces/v0.8.x/IAbxfsFileDataReader.sol +67 -0
  22. package/contracts/interfaces/v0.8.x/IAbxfsFileStore.sol +40 -0
  23. package/contracts/interfaces/v0.8.x/IDependencyRegistryV0.sol +33 -22
  24. package/contracts/libs/v0.8.x/AbxfsCanonical.sol +23 -0
  25. package/contracts/libs/v0.8.x/DependencyRegistryStorageLib.sol +10 -4
  26. package/contracts/mock/CallProbeMock.sol +20 -0
  27. package/contracts/mock/GasProbeMock.sol +21 -0
  28. package/contracts/mock/GenArt721GeneratorV0ScriptyReferenceMock.sol +1316 -0
  29. package/contracts/mock/InPlaceDocumentHarnessMock.sol +189 -0
  30. package/contracts/mock/MockAbxfsFileDataReader.sol +134 -0
  31. package/contracts/mock/MockAbxfsFileStore.sol +125 -0
  32. package/contracts/mock/RawResponseMock.sol +58 -0
  33. package/package.json +1 -1
@@ -0,0 +1,189 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-only
2
+ // Created By: Art Blocks Inc.
3
+ pragma solidity 0.8.22;
4
+
5
+ import {InPlaceDocument} from "../generator/InPlaceDocument.sol";
6
+ import {Base64} from "@openzeppelin-5.0/contracts/utils/Base64.sol";
7
+
8
+ /**
9
+ * @title InPlaceDocumentHarnessMock
10
+ * @notice TEST ONLY. Exposes InPlaceDocument: its base64 encoder in the buffer layout it encodes
11
+ * in place (the layout of ABX FS FileDataReader's own harness), and whole documents returned as
12
+ * they are or as data URIs, over memory deliberately left dirty.
13
+ */
14
+ contract InPlaceDocumentHarnessMock {
15
+ /**
16
+ * @notice Runs `encodeBase64` with the data `groups + 64` bytes into the space its encoding
17
+ * fills, and the word after the data zero. Everything else from the encoding's start to four
18
+ * words past the allocation starts as junk (all ones), so a read of any byte but the data and
19
+ * its zero word, or a write outside the encoding's words, shows.
20
+ * @param misalign Bytes past a word boundary the encoding starts at (the generator's starts 22
21
+ * bytes past one, after the data URI prefix).
22
+ * @return encoded The encoding, where it was written.
23
+ * @return clean Whether the encoder left everything else alone: scratch space, the free memory
24
+ * pointer, the zero slot, the length word before the encoding, and the four words past the
25
+ * allocation; and whether it zeroed the word after a nonempty encoding.
26
+ */
27
+ function encodeInPlace(
28
+ bytes memory data,
29
+ uint256 misalign
30
+ ) public pure returns (bytes memory encoded, bool clean) {
31
+ uint256 size = data.length;
32
+ uint256 groups = (size + 2) / 3;
33
+ uint256 outLength = groups << 2;
34
+ uint256 out;
35
+ uint256 at;
36
+ uint256 end;
37
+ assembly ("memory-safe") {
38
+ let lengthWord := add(mload(0x40), and(misalign, 0x1f))
39
+ out := add(lengthWord, 0x20)
40
+ at := add(out, add(groups, 0x40))
41
+ end := add(add(out, and(add(outLength, 0x1f), not(0x1f))), 0x20)
42
+ let dataEnd := add(add(at, size), 0x20)
43
+ if gt(dataEnd, end) {
44
+ end := dataEnd
45
+ }
46
+ end := and(add(end, 0x1f), not(0x1f))
47
+ mstore(0x40, end)
48
+ for {
49
+ let p := out
50
+ } lt(p, add(end, 0x80)) {
51
+ p := add(p, 0x20)
52
+ } {
53
+ mstore(p, not(0))
54
+ }
55
+ mstore(lengthWord, outLength)
56
+ for {
57
+ let i := 0
58
+ } lt(i, size) {
59
+ i := add(i, 0x20)
60
+ } {
61
+ mstore(add(at, i), mload(add(add(data, 0x20), i)))
62
+ }
63
+ mstore(add(at, size), 0)
64
+ mstore(0x00, 0x5c5c5c5c)
65
+ mstore(0x20, 0xa5a5a5a5)
66
+ }
67
+ InPlaceDocument.encodeBase64(at, size, out);
68
+ assembly ("memory-safe") {
69
+ clean := and(
70
+ eq(mload(0x00), 0x5c5c5c5c),
71
+ eq(mload(0x20), 0xa5a5a5a5)
72
+ )
73
+ clean := and(clean, and(eq(mload(0x40), end), iszero(mload(0x60))))
74
+ clean := and(clean, eq(mload(sub(out, 0x20)), outLength))
75
+ if size {
76
+ clean := and(clean, iszero(mload(add(out, outLength))))
77
+ }
78
+ for {
79
+ let p := end
80
+ } lt(p, add(end, 0x80)) {
81
+ p := add(p, 0x20)
82
+ } {
83
+ clean := and(clean, eq(mload(p), not(0)))
84
+ }
85
+ encoded := sub(out, 0x20)
86
+ }
87
+ // restore a word-aligned free memory pointer past the encoding for the caller
88
+ assembly ("memory-safe") {
89
+ mstore(0x40, add(end, 0x80))
90
+ }
91
+ }
92
+
93
+ /// @notice `encodeInPlace` over many inputs in one call.
94
+ function encodeInPlaceBatch(
95
+ bytes[] memory data,
96
+ uint256 misalign
97
+ ) external pure returns (bytes[] memory encoded, bool[] memory clean) {
98
+ encoded = new bytes[](data.length);
99
+ clean = new bool[](data.length);
100
+ for (uint256 i = 0; i < data.length; i++) {
101
+ (encoded[i], clean[i]) = encodeInPlace(data[i], misalign);
102
+ }
103
+ }
104
+
105
+ /// @dev Fills `junk` bytes past the free memory pointer with ones, so nothing can rely on
106
+ /// fresh (zero) memory, then shifts the free memory pointer by `shift` bytes.
107
+ function _dirty(uint256 junk, uint256 shift) private pure {
108
+ assembly ("memory-safe") {
109
+ let p := mload(0x40)
110
+ for {
111
+ let i := 0
112
+ } lt(i, junk) {
113
+ i := add(i, 0x20)
114
+ } {
115
+ mstore(add(p, i), not(0))
116
+ }
117
+ mstore(0x40, add(p, shift))
118
+ }
119
+ }
120
+
121
+ /// @dev Opens a document and appends `parts`, over dirty memory.
122
+ function _write(
123
+ bytes[] memory parts,
124
+ uint256 junk,
125
+ uint256 shift
126
+ ) private pure returns (uint256 doc, uint256 end) {
127
+ _dirty(junk, shift);
128
+ (doc, end) = InPlaceDocument.open();
129
+ for (uint256 i = 0; i < parts.length; i++) {
130
+ end = InPlaceDocument.appendBytes(end, parts[i]);
131
+ }
132
+ }
133
+
134
+ /// @notice The concatenation of `parts`, returned in place.
135
+ function document(
136
+ bytes[] memory parts,
137
+ uint256 junk,
138
+ uint256 shift
139
+ ) external pure returns (string memory) {
140
+ (uint256 doc, uint256 end) = _write(parts, junk, shift);
141
+ return InPlaceDocument.returnDocument(doc, end);
142
+ }
143
+
144
+ /// @notice `prefix` then the base64 of the concatenation of `parts`, returned in place.
145
+ function dataUri(
146
+ bytes[] memory parts,
147
+ uint256 junk,
148
+ uint256 shift,
149
+ bytes32 prefix,
150
+ uint256 prefixLength
151
+ ) external view returns (string memory) {
152
+ (uint256 doc, uint256 end) = _write(parts, junk, shift);
153
+ return
154
+ InPlaceDocument.returnBase64DataUri(doc, end, prefix, prefixLength);
155
+ }
156
+
157
+ /**
158
+ * @notice Gas of `encodeBase64` alone, in its in-place layout, and of OpenZeppelin's
159
+ * `Base64.encode` on the same bytes.
160
+ */
161
+ function gasOf(
162
+ bytes memory data
163
+ ) external view returns (uint256 ours, uint256 openZeppelin) {
164
+ uint256 size = data.length;
165
+ uint256 out;
166
+ uint256 at;
167
+ assembly ("memory-safe") {
168
+ out := add(mload(0x40), 0x20)
169
+ at := add(out, add(div(add(size, 2), 3), 0x40))
170
+ for {
171
+ let i := 0
172
+ } lt(i, size) {
173
+ i := add(i, 0x20)
174
+ } {
175
+ mstore(add(at, i), mload(add(add(data, 0x20), i)))
176
+ }
177
+ mstore(add(at, size), 0)
178
+ // touch the encoding's last word, so the count pays no memory expansion
179
+ mstore(add(out, shl(2, div(add(size, 2), 3))), 0)
180
+ mstore(0x40, add(add(at, size), 0x40))
181
+ }
182
+ uint256 g = gasleft();
183
+ InPlaceDocument.encodeBase64(at, size, out);
184
+ ours = g - gasleft();
185
+ g = gasleft();
186
+ Base64.encode(data);
187
+ openZeppelin = g - gasleft();
188
+ }
189
+ }
@@ -0,0 +1,134 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-only
2
+ // Created By: Art Blocks Inc.
3
+ pragma solidity 0.8.22;
4
+
5
+ import {IAbxfsFileDataReader} from "../interfaces/v0.8.x/IAbxfsFileDataReader.sol";
6
+ import {Base64} from "@openzeppelin-5.0/contracts/utils/Base64.sol";
7
+
8
+ /**
9
+ * @title MockAbxfsFileDataReader
10
+ * @notice Test double for the ABX FS FileDataReader: joins a file's chunks as read from the
11
+ * FileStore at the canonical address (in tests, a MockAbxfsFileStore placed there).
12
+ * @dev Reads only constant addresses, so its runtime code can be placed at the canonical
13
+ * FileDataReader address with `hardhat_setCode`. Reverts like FileDataReader when the chunks
14
+ * disagree with the recorded size or a limit is exceeded.
15
+ */
16
+ interface IMockFileStore {
17
+ struct FileInfo {
18
+ bytes32 root;
19
+ uint8 rootLevel;
20
+ uint256 chunkCount;
21
+ uint256 size;
22
+ string mimeType;
23
+ bytes32 codec;
24
+ bytes32 outputHash;
25
+ }
26
+
27
+ function getFileInfo(
28
+ bytes32 cid
29
+ ) external view returns (FileInfo memory info);
30
+
31
+ function readChunk(
32
+ bytes32 cid,
33
+ uint256 index
34
+ ) external view returns (bytes memory data);
35
+ }
36
+
37
+ contract MockAbxfsFileDataReader is IAbxfsFileDataReader {
38
+ IMockFileStore constant FILE_STORE =
39
+ IMockFileStore(0x8b6077a1d6953D96687005B9408D56A655b4d155);
40
+
41
+ error ByteLimitExceeded(uint256 bytesRequired, uint256 maxBytes);
42
+ error ChunkLimitExceeded(uint256 chunksRequired, uint256 maxChunks);
43
+ error InvalidFileLength();
44
+ error RangeOutOfBounds(uint256 start, uint256 length, uint256 size);
45
+ error UnalignedBase64Range(uint256 start, uint256 length);
46
+
47
+ function readFileDataBase64(
48
+ bytes32 cid,
49
+ uint256 maxBytes,
50
+ uint256 maxChunks
51
+ )
52
+ external
53
+ view
54
+ returns (
55
+ string memory mimeType,
56
+ bytes32 codec,
57
+ bytes32 outputHash,
58
+ string memory base64
59
+ )
60
+ {
61
+ IMockFileStore.FileInfo memory info = FILE_STORE.getFileInfo(cid);
62
+ if (info.size > maxBytes) {
63
+ revert ByteLimitExceeded(info.size, maxBytes);
64
+ }
65
+ if (info.chunkCount > maxChunks) {
66
+ revert ChunkLimitExceeded(info.chunkCount, maxChunks);
67
+ }
68
+ bytes memory data;
69
+ for (uint256 i = 0; i < info.chunkCount; i++) {
70
+ data = bytes.concat(data, FILE_STORE.readChunk(cid, i));
71
+ }
72
+ if (data.length != info.size) revert InvalidFileLength();
73
+ return (
74
+ info.mimeType,
75
+ info.codec,
76
+ info.outputHash,
77
+ Base64.encode(data)
78
+ );
79
+ }
80
+
81
+ function readFileDataRangeBase64(
82
+ bytes32 cid,
83
+ uint256 start,
84
+ uint256 length,
85
+ uint256 /* maxBytes */,
86
+ uint256 /* maxChunks */
87
+ )
88
+ external
89
+ view
90
+ returns (
91
+ string memory mimeType,
92
+ bytes32 codec,
93
+ bytes32 outputHash,
94
+ string memory base64
95
+ )
96
+ {
97
+ IMockFileStore.FileInfo memory info = FILE_STORE.getFileInfo(cid);
98
+ if (start + length > info.size) {
99
+ revert RangeOutOfBounds(start, length, info.size);
100
+ }
101
+ if (
102
+ start % 3 != 0 || (length % 3 != 0 && start + length != info.size)
103
+ ) {
104
+ revert UnalignedBase64Range(start, length);
105
+ }
106
+ bytes memory data;
107
+ for (uint256 i = 0; i < info.chunkCount; i++) {
108
+ data = bytes.concat(data, FILE_STORE.readChunk(cid, i));
109
+ }
110
+ // @dev word copy in assembly: a per-byte Solidity loop over a 25 KB range is too slow
111
+ // under coverage instrumentation
112
+ bytes memory range = new bytes(length);
113
+ assembly {
114
+ let src := add(add(data, 0x20), start)
115
+ let dst := add(range, 0x20)
116
+ for {
117
+ let i := 0
118
+ } lt(i, length) {
119
+ i := add(i, 0x20)
120
+ } {
121
+ mstore(add(dst, i), mload(add(src, i)))
122
+ }
123
+ // clear anything copied past the range's end, then restore the length word
124
+ mstore(add(dst, length), 0)
125
+ mstore(range, length)
126
+ }
127
+ return (
128
+ info.mimeType,
129
+ info.codec,
130
+ info.outputHash,
131
+ Base64.encode(range)
132
+ );
133
+ }
134
+ }
@@ -0,0 +1,125 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-only
2
+ // Created By: Art Blocks Inc.
3
+ pragma solidity ^0.8.17;
4
+
5
+ import {IAbxfsFileStore} from "../interfaces/v0.8.x/IAbxfsFileStore.sol";
6
+
7
+ /**
8
+ * @title MockAbxfsFileStore
9
+ * @notice Test double for the ABX FS FileStore read interface. Files are set directly, with an
10
+ * explicit codec and size so tests can describe malformed files as well as valid ones.
11
+ * @dev Reads only constant addresses and its own storage, so its runtime code can be placed at
12
+ * the canonical FileStore address with `hardhat_setCode`. Like the real FileStore,
13
+ * getFileInfo, locateByte and readChunk revert for a CID that is not a file.
14
+ */
15
+ contract MockAbxfsFileStore is IAbxfsFileStore {
16
+ error FileNotFound(bytes32 cid);
17
+ error OffsetOutOfRange(uint256 offset, uint256 size);
18
+
19
+ struct MockFile {
20
+ bool exists;
21
+ bytes32 codec;
22
+ uint256 size;
23
+ // chunk data held as contract code (SSTORE2 layout: 0x00 then the data), like ABX FS
24
+ address[] chunks;
25
+ }
26
+
27
+ mapping(bytes32 cid => MockFile file) internal _files;
28
+
29
+ /**
30
+ * @notice Sets file `cid`. `size` is reported as-is, so a test can make it disagree with the
31
+ * chunks.
32
+ */
33
+ function setFile(
34
+ bytes32 cid,
35
+ bytes32 codec,
36
+ uint256 size,
37
+ bytes[] calldata chunks
38
+ ) external {
39
+ MockFile storage file = _files[cid];
40
+ file.exists = true;
41
+ file.codec = codec;
42
+ file.size = size;
43
+ delete file.chunks;
44
+ for (uint256 i = 0; i < chunks.length; i++) {
45
+ file.chunks.push(_writeCode(chunks[i]));
46
+ }
47
+ }
48
+
49
+ function fileExists(bytes32 cid) external view returns (bool) {
50
+ return _files[cid].exists;
51
+ }
52
+
53
+ function getFileInfo(
54
+ bytes32 cid
55
+ ) external view returns (FileInfo memory info) {
56
+ MockFile storage file = _file(cid);
57
+ info.root = keccak256(abi.encode(cid));
58
+ info.chunkCount = file.chunks.length;
59
+ info.size = file.size;
60
+ info.mimeType = "text/javascript";
61
+ info.codec = file.codec;
62
+ info.outputHash = file.codec == bytes32(0)
63
+ ? bytes32(0)
64
+ : keccak256(abi.encode(cid, "content"));
65
+ }
66
+
67
+ function locateByte(
68
+ bytes32 cid,
69
+ uint256 offset
70
+ )
71
+ external
72
+ view
73
+ returns (
74
+ uint256 index,
75
+ uint256 chunkStart,
76
+ bytes32 chunkId,
77
+ uint256 chunkSize
78
+ )
79
+ {
80
+ MockFile storage file = _file(cid);
81
+ for (; index < file.chunks.length; index++) {
82
+ chunkSize = file.chunks[index].code.length - 1;
83
+ if (offset < chunkStart + chunkSize) {
84
+ return (
85
+ index,
86
+ chunkStart,
87
+ keccak256(abi.encode(file.chunks[index])),
88
+ chunkSize
89
+ );
90
+ }
91
+ chunkStart += chunkSize;
92
+ }
93
+ revert OffsetOutOfRange(offset, chunkStart);
94
+ }
95
+
96
+ function readChunk(
97
+ bytes32 cid,
98
+ uint256 index
99
+ ) external view returns (bytes memory data) {
100
+ address pointer = _file(cid).chunks[index];
101
+ data = new bytes(pointer.code.length - 1);
102
+ assembly {
103
+ extcodecopy(pointer, add(data, 0x20), 1, mload(data))
104
+ }
105
+ }
106
+
107
+ function _writeCode(bytes memory data) internal returns (address pointer) {
108
+ // returns `0x00 ‖ data` as the deployed code
109
+ bytes memory init = abi.encodePacked(
110
+ hex"61",
111
+ uint16(data.length + 1),
112
+ hex"80600a3d393df300",
113
+ data
114
+ );
115
+ assembly {
116
+ pointer := create(0, add(init, 0x20), mload(init))
117
+ }
118
+ require(pointer != address(0), "chunk write failed");
119
+ }
120
+
121
+ function _file(bytes32 cid) internal view returns (MockFile storage file) {
122
+ file = _files[cid];
123
+ if (!file.exists) revert FileNotFound(cid);
124
+ }
125
+ }
@@ -0,0 +1,58 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-only
2
+ // Created By: Art Blocks Inc.
3
+ pragma solidity 0.8.22;
4
+
5
+ /**
6
+ * @title RawResponseMock
7
+ * @notice TEST ONLY. Answers any call with bytes set in advance, returned or reverted with
8
+ * exactly as set: for the whole call data, else for its selector. Calls with neither revert
9
+ * with no data. Lets tests hand a contract malformed return data.
10
+ * @dev Reads only its own storage, so its runtime code can be placed at any address with
11
+ * `hardhat_setCode` and configured there.
12
+ */
13
+ contract RawResponseMock {
14
+ struct Response {
15
+ bool set;
16
+ bool reverts;
17
+ bytes data;
18
+ }
19
+
20
+ mapping(bytes32 callDataHash => Response response) internal _byCallData;
21
+ mapping(bytes4 selector => Response response) internal _bySelector;
22
+
23
+ function setResponse(
24
+ bytes calldata callData,
25
+ bool reverts,
26
+ bytes calldata data
27
+ ) external {
28
+ _byCallData[keccak256(callData)] = Response(true, reverts, data);
29
+ }
30
+
31
+ function setSelectorResponse(
32
+ bytes4 selector,
33
+ bool reverts,
34
+ bytes calldata data
35
+ ) external {
36
+ _bySelector[selector] = Response(true, reverts, data);
37
+ }
38
+
39
+ fallback(bytes calldata callData) external returns (bytes memory) {
40
+ Response memory response = _byCallData[keccak256(callData)];
41
+ if (!response.set) {
42
+ response = _bySelector[msg.sig];
43
+ }
44
+ if (!response.set) {
45
+ revert();
46
+ }
47
+ bytes memory data = response.data;
48
+ if (response.reverts) {
49
+ assembly {
50
+ revert(add(data, 0x20), mload(data))
51
+ }
52
+ }
53
+ // @dev returned raw: not ABI-encoded again
54
+ assembly {
55
+ return(add(data, 0x20), mload(data))
56
+ }
57
+ }
58
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@artblocks/contracts",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "Smart contracts used by Art Blocks",
5
5
  "files": [
6
6
  "/contracts/**/*.sol",