1+ #!/usr/bin/env ruby
2+
3+ require 'json'
4+ require 'digest'
5+ require 'base64'
6+
7+ # Read the existing genesis JSON file
8+ json_path = File . join ( File . dirname ( __FILE__ ) , 'genesisEthscriptions.json' )
9+ data = JSON . parse ( File . read ( json_path ) )
10+
11+ # Process each ethscription
12+ data [ 'ethscriptions' ] . each do |ethscription |
13+ content_uri = ethscription [ 'content_uri' ]
14+
15+ # Calculate content URI hash (as hex string with 0x prefix for JSON)
16+ content_uri_hash = '0x' + Digest ::SHA256 . hexdigest ( content_uri )
17+ ethscription [ 'content_uri_hash' ] = content_uri_hash
18+
19+ # Parse the data URI
20+ if content_uri . start_with? ( 'data:' )
21+ # Remove 'data:' prefix
22+ uri_without_prefix = content_uri [ 5 ..-1 ]
23+
24+ # Find the comma that separates metadata from data
25+ comma_index = uri_without_prefix . index ( ',' )
26+
27+ if comma_index
28+ metadata = uri_without_prefix [ 0 ...comma_index ]
29+ data_part = uri_without_prefix [ ( comma_index + 1 ) ..-1 ]
30+
31+ # Check if it's base64 encoded
32+ is_base64 = metadata . include? ( ';base64' )
33+
34+ # Get the content
35+ if is_base64
36+ # Decode from base64 for storage
37+ content = Base64 . decode64 ( data_part )
38+ # Store as hex string for JSON (with 0x prefix)
39+ ethscription [ 'content' ] = '0x' + content . unpack1 ( 'H*' )
40+ else
41+ # For non-base64, keep the original encoded form (preserves percent-encoding)
42+ # Store as hex string for JSON (with 0x prefix)
43+ ethscription [ 'content' ] = '0x' + data_part . unpack ( 'H*' ) [ 0 ]
44+ end
45+ else
46+ # Invalid data URI format, store empty content
47+ ethscription [ 'content' ] = '0x'
48+ end
49+ else
50+ # Not a data URI, store the whole thing as content
51+ ethscription [ 'content' ] = '0x' + content_uri . unpack ( 'H*' ) [ 0 ]
52+ end
53+ end
54+
55+ # Write the updated JSON back
56+ File . write ( json_path , JSON . pretty_generate ( data ) )
57+
58+ puts "Processed #{ data [ 'ethscriptions' ] . length } ethscriptions"
59+ puts "Added content_uri_hash and content fields"
0 commit comments