|
| 1 | +require 'rails_helper' |
| 2 | +require 'base64' |
| 3 | +require_relative '../../lib/protocol_event_reader' |
| 4 | + |
| 5 | +RSpec.describe "Header-Based Collections Protocol", type: :integration do |
| 6 | + include EthscriptionsTestHelper |
| 7 | + |
| 8 | + let(:alice) { valid_address("alice") } |
| 9 | + let(:bob) { valid_address("bob") } |
| 10 | + let(:carol) { valid_address("carol") } |
| 11 | + let(:media_type) { 'image/png' } |
| 12 | + |
| 13 | + # Small "image" payloads live in the data URI body; protocol data lives in headers |
| 14 | + let(:items_manifest) do |
| 15 | + [ |
| 16 | + { |
| 17 | + item_index: 0, |
| 18 | + name: "Header Genesis", |
| 19 | + background_color: "#111111", |
| 20 | + description: "Leader image stored with header metadata", |
| 21 | + attributes: [ |
| 22 | + {"trait_type" => "Tier", "value" => "Genesis"}, |
| 23 | + {"trait_type" => "Artist", "value" => "Alice"} |
| 24 | + ], |
| 25 | + base64_content: Base64.strict_encode64("header-genesis-image") |
| 26 | + }, |
| 27 | + { |
| 28 | + item_index: 1, |
| 29 | + name: "Header Entry #1", |
| 30 | + background_color: "#222222", |
| 31 | + description: "Bob adds via header path", |
| 32 | + attributes: [ |
| 33 | + {"trait_type" => "Tier", "value" => "Member"}, |
| 34 | + {"trait_type" => "Artist", "value" => "Bob"} |
| 35 | + ], |
| 36 | + base64_content: Base64.strict_encode64("header-bob-image") |
| 37 | + }, |
| 38 | + { |
| 39 | + item_index: 2, |
| 40 | + name: "Header Entry #2", |
| 41 | + background_color: "#333333", |
| 42 | + description: "Carol adds via header path", |
| 43 | + attributes: [ |
| 44 | + {"trait_type" => "Tier", "value" => "Member"}, |
| 45 | + {"trait_type" => "Artist", "value" => "Carol"} |
| 46 | + ], |
| 47 | + base64_content: Base64.strict_encode64("header-carol-image") |
| 48 | + } |
| 49 | + ] |
| 50 | + end |
| 51 | + |
| 52 | + let(:merkle_plan) { build_merkle_plan(items_manifest) } |
| 53 | + let(:merkle_root) { merkle_plan[:root] } |
| 54 | + let(:proofs) { merkle_plan[:proofs] } |
| 55 | + |
| 56 | + it "mints collection/items from headers and enforces merkle proofs" do |
| 57 | + collection_uri = header_data_uri( |
| 58 | + op: 'create_collection_and_add_self', |
| 59 | + payload: { |
| 60 | + "metadata" => metadata_payload(merkle_root: merkle_root, initial_owner: alice), |
| 61 | + "item" => item_payload(items_manifest[0], proofs[0]) |
| 62 | + }, |
| 63 | + content_base64: items_manifest[0][:base64_content] |
| 64 | + ) |
| 65 | + |
| 66 | + creation_results = import_l1_block( |
| 67 | + [create_input(creator: alice, to: alice, data_uri: collection_uri)], |
| 68 | + esip_overrides: { esip6_is_enabled: true } |
| 69 | + ) |
| 70 | + creation_receipt = creation_results[:l2_receipts].first |
| 71 | + expect(creation_receipt[:status]).to eq('0x1') |
| 72 | + |
| 73 | + collection_id = creation_results[:ethscription_ids].first |
| 74 | + expect(collection_id).to be_present |
| 75 | + metadata = get_collection_metadata(collection_id) |
| 76 | + expect(metadata[:merkleRoot].downcase).to eq(merkle_root.downcase) |
| 77 | + |
| 78 | + leader_item = get_collection_item(collection_id, 0) |
| 79 | + expect(leader_item[:ethscriptionId]).to eq(collection_id) |
| 80 | + expect(leader_item[:name]).to eq(items_manifest[0][:name]) |
| 81 | + |
| 82 | + bob_uri = header_data_uri( |
| 83 | + op: 'add_self_to_collection', |
| 84 | + payload: add_item_payload(collection_id, items_manifest[1], proofs[1]), |
| 85 | + content_base64: items_manifest[1][:base64_content] |
| 86 | + ) |
| 87 | + bob_results = import_l1_block( |
| 88 | + [create_input(creator: bob, to: bob, data_uri: bob_uri)], |
| 89 | + esip_overrides: { esip6_is_enabled: true } |
| 90 | + ) |
| 91 | + bob_receipt = bob_results[:l2_receipts].first |
| 92 | + expect(bob_receipt[:status]).to eq('0x1') |
| 93 | + bob_events = ProtocolEventReader.parse_receipt_events(bob_receipt) |
| 94 | + expect(bob_events.any? { |e| e[:event] == 'ItemsAdded' }).to eq(true) |
| 95 | + expect(bob_events.any? { |e| e[:event] == 'ProtocolHandlerSuccess' }).to eq(true) |
| 96 | + item1_id = bob_results[:ethscription_ids].first |
| 97 | + expect(get_collection_item(collection_id, 1)[:ethscriptionId]).to eq(item1_id) |
| 98 | + |
| 99 | + carol_uri = header_data_uri( |
| 100 | + op: 'add_self_to_collection', |
| 101 | + payload: add_item_payload(collection_id, items_manifest[2], proofs[2]), |
| 102 | + content_base64: items_manifest[2][:base64_content] |
| 103 | + ) |
| 104 | + carol_results = import_l1_block( |
| 105 | + [create_input(creator: carol, to: carol, data_uri: carol_uri)], |
| 106 | + esip_overrides: { esip6_is_enabled: true } |
| 107 | + ) |
| 108 | + carol_receipt = carol_results[:l2_receipts].first |
| 109 | + expect(carol_receipt[:status]).to eq('0x1') |
| 110 | + carol_events = ProtocolEventReader.parse_receipt_events(carol_receipt) |
| 111 | + expect(carol_events.any? { |e| e[:event] == 'ItemsAdded' }).to eq(true) |
| 112 | + expect(carol_events.any? { |e| e[:event] == 'ProtocolHandlerSuccess' }).to eq(true) |
| 113 | + item2_id = carol_results[:ethscription_ids].first |
| 114 | + expect(get_collection_item(collection_id, 2)[:ethscriptionId]).to eq(item2_id) |
| 115 | + |
| 116 | + expect(get_collection_state(collection_id)[:currentSize]).to eq(3) |
| 117 | + |
| 118 | + forged_item = { |
| 119 | + item_index: 3, |
| 120 | + name: "Forged Entry", |
| 121 | + background_color: "#444444", |
| 122 | + description: "Should fail merkle proof", |
| 123 | + attributes: [ |
| 124 | + {"trait_type" => "Tier", "value" => "Spoof"}, |
| 125 | + {"trait_type" => "Artist", "value" => "Mallory"} |
| 126 | + ], |
| 127 | + base64_content: Base64.strict_encode64("forged-header-image") |
| 128 | + } |
| 129 | + |
| 130 | + forged_uri = header_data_uri( |
| 131 | + op: 'add_self_to_collection', |
| 132 | + payload: add_item_payload(collection_id, forged_item, proofs[0]), # wrong proof on purpose |
| 133 | + content_base64: forged_item[:base64_content] |
| 134 | + ) |
| 135 | + # Use the hard-coded force-merkle sender so enforcement applies even in import mode |
| 136 | + force_merkle_sender = "0x0000000000000000000000000000000000000042" |
| 137 | + forged_results = import_l1_block( |
| 138 | + [create_input(creator: force_merkle_sender, to: force_merkle_sender, data_uri: forged_uri)], |
| 139 | + esip_overrides: { esip6_is_enabled: true } |
| 140 | + ) |
| 141 | + forged_receipt = forged_results[:l2_receipts].first |
| 142 | + forged_events = ProtocolEventReader.parse_receipt_events(forged_receipt) |
| 143 | + |
| 144 | + failure = forged_events.find { |e| e[:event] == 'ProtocolHandlerFailed' } |
| 145 | + expect(failure).not_to be_nil |
| 146 | + expect(failure[:reason].to_s).to match(/Invalid Merkle proof/i) |
| 147 | + expect(get_collection_state(collection_id)[:currentSize]).to eq(3) |
| 148 | + end |
| 149 | + |
| 150 | + def metadata_payload(merkle_root:, initial_owner:) |
| 151 | + { |
| 152 | + "name" => "Header Merkle Collection", |
| 153 | + "symbol" => "HDR", |
| 154 | + "max_supply" => "4", |
| 155 | + "description" => "Header-based minting flow", |
| 156 | + "logo_image_uri" => "esc://logo/header", |
| 157 | + "banner_image_uri" => "", |
| 158 | + "background_color" => "#000000", |
| 159 | + "website_link" => "https://example.com", |
| 160 | + "twitter_link" => "https://twitter.com/example", |
| 161 | + "discord_link" => "", |
| 162 | + "merkle_root" => merkle_root, |
| 163 | + "initial_owner" => initial_owner |
| 164 | + } |
| 165 | + end |
| 166 | + |
| 167 | + def item_payload(item, proof) |
| 168 | + { |
| 169 | + "item_index" => item[:item_index].to_s, |
| 170 | + "name" => item[:name], |
| 171 | + "background_color" => item[:background_color], |
| 172 | + "description" => item[:description], |
| 173 | + "attributes" => item[:attributes], |
| 174 | + "merkle_proof" => proof |
| 175 | + } |
| 176 | + end |
| 177 | + |
| 178 | + def add_item_payload(collection_id, item, proof) |
| 179 | + { |
| 180 | + "collection_id" => collection_id, |
| 181 | + "item" => item_payload(item, proof) |
| 182 | + } |
| 183 | + end |
| 184 | + |
| 185 | + def header_data_uri(op:, payload:, content_base64:) |
| 186 | + encoded_payload = Base64.strict_encode64(JSON.generate(payload)) |
| 187 | + "data:#{media_type};p=erc-721-ethscriptions-collection;op=#{op};d=#{encoded_payload};base64,#{content_base64}" |
| 188 | + end |
| 189 | + |
| 190 | + def build_merkle_plan(manifest) |
| 191 | + leaves_bin = [] |
| 192 | + proofs = {} |
| 193 | + |
| 194 | + manifest.each do |item| |
| 195 | + content_bytes = Base64.strict_decode64(item[:base64_content]) |
| 196 | + content_hash_hex = '0x' + Eth::Util.keccak256(content_bytes).unpack1('H*') |
| 197 | + leaf_hex = compute_leaf_hash(content_hash_hex: content_hash_hex, item: item) |
| 198 | + leaves_bin << [leaf_hex.delete_prefix('0x')].pack('H*') |
| 199 | + end |
| 200 | + |
| 201 | + levels = build_merkle_tree_levels(leaves_bin) |
| 202 | + root_hex = '0x' + levels.last.first.unpack1('H*') |
| 203 | + |
| 204 | + leaves_bin.each_with_index do |leaf, idx| |
| 205 | + proofs[idx] = build_proof_for_index(levels, idx) |
| 206 | + raise "invalid merkle proof for leaf #{idx}" unless verify_proof(leaf, proofs[idx], root_hex) |
| 207 | + end |
| 208 | + |
| 209 | + { root: root_hex, proofs: proofs } |
| 210 | + end |
| 211 | + |
| 212 | + def build_merkle_tree_levels(leaves) |
| 213 | + return [[''.b]] if leaves.empty? |
| 214 | + |
| 215 | + levels = [leaves] |
| 216 | + while levels.last.length > 1 |
| 217 | + current = levels.last |
| 218 | + next_level = [] |
| 219 | + |
| 220 | + current.each_slice(2) do |left, right| |
| 221 | + right ||= left |
| 222 | + a, b = [left, right].sort |
| 223 | + next_level << Eth::Util.keccak256(a + b) |
| 224 | + end |
| 225 | + |
| 226 | + levels << next_level |
| 227 | + end |
| 228 | + |
| 229 | + levels |
| 230 | + end |
| 231 | + |
| 232 | + def build_proof_for_index(levels, index) |
| 233 | + proof = [] |
| 234 | + level_index = index |
| 235 | + |
| 236 | + levels[0...-1].each do |level| |
| 237 | + sibling_index = level_index ^ 1 |
| 238 | + sibling = level[sibling_index] || level[level_index] |
| 239 | + proof << '0x' + sibling.unpack1('H*') |
| 240 | + level_index /= 2 |
| 241 | + end |
| 242 | + |
| 243 | + proof |
| 244 | + end |
| 245 | + |
| 246 | + def verify_proof(leaf, proof_hex, expected_root_hex) |
| 247 | + computed = leaf |
| 248 | + |
| 249 | + proof_hex.each do |hex| |
| 250 | + sibling = [hex.delete_prefix('0x')].pack('H*') |
| 251 | + a, b = [computed, sibling].sort |
| 252 | + computed = Eth::Util.keccak256(a + b) |
| 253 | + end |
| 254 | + |
| 255 | + ('0x' + computed.unpack1('H*')).casecmp(expected_root_hex).zero? |
| 256 | + end |
| 257 | + |
| 258 | + def compute_leaf_hash(content_hash_hex:, item:) |
| 259 | + content_hash_bytes = [content_hash_hex.delete_prefix('0x')].pack('H*') |
| 260 | + attrs = item[:attributes].map { |attr| [attr["trait_type"], attr["value"]] } |
| 261 | + |
| 262 | + encoded = Eth::Abi.encode( |
| 263 | + ['bytes32', 'uint256', 'string', 'string', 'string', '(string,string)[]'], |
| 264 | + [content_hash_bytes, item[:item_index], item[:name], item[:background_color], item[:description], attrs] |
| 265 | + ) |
| 266 | + |
| 267 | + '0x' + Eth::Util.keccak256(encoded).unpack1('H*') |
| 268 | + end |
| 269 | +end |
0 commit comments