pub fn hex_string_to_hash_bytes(hex_str: &str) -> Result<[u8; 32], &'static str>
Expand description
Convert a hex string to a 32-byte array (for hash values)
§Parameters
hex_str
: Hex string (with or without “0x” prefix)
§Returns
Ok([u8; 32])
if conversion succeedsErr(&str)
with error message if conversion fails
§Examples
use miracle_api::utils::hex::hex_string_to_hash_bytes;
let hash = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
let bytes = hex_string_to_hash_bytes(hash)?;
assert_eq!(bytes.len(), 32);
Examples found in repository?
examples/claim_instruction_example.rs (lines 113-115)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15 println!("🔍 Miracle API Utils Example: Building Claim Instructions");
16 println!("========================================================");
17
18 // ===== SCENARIO 1: Converting Database Hex Strings =====
19 println!("\n📊 Scenario 1: Converting Database Hex Strings");
20 println!("------------------------------------------------");
21
22 // Simulate database query output (hex strings from SQL function)
23 let db_customer_reward_pool = "0x1234567890abcdef";
24 let db_merchant_reward_pool = "0xfedcba0987654321";
25 let db_total_customer_activity = "0x0000000000000064"; // 100 in decimal
26 let db_total_merchant_activity = "0x00000000000000c8"; // 200 in decimal
27
28 println!("Database hex strings:");
29 println!(" customer_reward_pool: {}", db_customer_reward_pool);
30 println!(" merchant_reward_pool: {}", db_merchant_reward_pool);
31 println!(" total_customer_activity: {}", db_total_customer_activity);
32 println!(" total_merchant_activity: {}", db_total_merchant_activity);
33
34 // Convert hex strings to EpochClaimData using utils
35 let epoch_data = claim::epoch_claim_data_from_hex(
36 db_customer_reward_pool,
37 db_merchant_reward_pool,
38 db_total_customer_activity,
39 db_total_merchant_activity,
40 )?;
41
42 println!("\n✅ Converted to EpochClaimData:");
43 println!(
44 " customer_reward_pool bytes: {:?}",
45 epoch_data.customer_reward_pool
46 );
47 println!(
48 " merchant_reward_pool bytes: {:?}",
49 epoch_data.merchant_reward_pool
50 );
51 println!(
52 " total_customer_activity bytes: {:?}",
53 epoch_data.total_customer_activity
54 );
55 println!(
56 " total_merchant_activity bytes: {:?}",
57 epoch_data.total_merchant_activity
58 );
59
60 // ===== SCENARIO 2: Converting Native u64 Values =====
61 println!("\n📊 Scenario 2: Converting Native u64 Values");
62 println!("---------------------------------------------");
63
64 let native_customer_reward_pool = 1234567890u64;
65 let native_merchant_reward_pool = 9876543210u64;
66 let native_total_customer_activity = 100u64;
67 let native_total_merchant_activity = 200u64;
68
69 println!("Native u64 values:");
70 println!(" customer_reward_pool: {}", native_customer_reward_pool);
71 println!(" merchant_reward_pool: {}", native_merchant_reward_pool);
72 println!(
73 " total_customer_activity: {}",
74 native_total_customer_activity
75 );
76 println!(
77 " total_merchant_activity: {}",
78 native_total_merchant_activity
79 );
80
81 // Convert u64 values to EpochClaimData using utils
82 let epoch_data_u64 = claim::epoch_claim_data_from_u64(
83 native_customer_reward_pool,
84 native_merchant_reward_pool,
85 native_total_customer_activity,
86 native_total_merchant_activity,
87 );
88
89 println!("\n✅ Converted to EpochClaimData:");
90 println!(
91 " customer_reward_pool bytes: {:?}",
92 epoch_data_u64.customer_reward_pool
93 );
94 println!(
95 " merchant_reward_pool bytes: {:?}",
96 epoch_data_u64.merchant_reward_pool
97 );
98 println!(
99 " total_customer_activity bytes: {:?}",
100 epoch_data_u64.total_customer_activity
101 );
102 println!(
103 " total_merchant_activity bytes: {:?}",
104 epoch_data_u64.total_merchant_activity
105 );
106
107 // ===== SCENARIO 3: Individual Hex Conversions =====
108 println!("\n📊 Scenario 3: Individual Hex Conversions");
109 println!("-------------------------------------------");
110
111 // Convert individual hex strings to byte arrays
112 let reward_pool_bytes = hex::hex_string_to_u64_bytes("0x1234567890abcdef")?;
113 let hash_bytes = hex::hex_string_to_hash_bytes(
114 "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
115 )?;
116
117 println!("Individual conversions:");
118 println!(" reward_pool (8 bytes): {:?}", reward_pool_bytes);
119 println!(" hash (32 bytes): {:?}", hash_bytes);
120
121 // Convert bytes back to hex strings
122 let reward_pool_hex = hex::bytes_to_hex_string(&reward_pool_bytes);
123 println!(" reward_pool back to hex: {}", reward_pool_hex);
124
125 // ===== SCENARIO 4: Building Claim Instruction =====
126 println!("\n📊 Scenario 4: Building Claim Instruction");
127 println!("------------------------------------------");
128
129 // Create sample participant data
130 let participant_data = DailyParticipantData::new(
131 [1u8; 32], // participant_id
132 1723680000, // first_payment_timestamp
133 1723766400, // last_payment_timestamp
134 [1u8; 8], // first_payment_tx_sig_short
135 [2u8; 8], // last_payment_tx_sig_short
136 5, // payment_count
137 0, // participant_type (customer)
138 );
139
140 // Create payment proof (empty for this example)
141 let payment_proof = vec![[3u8; 32]];
142 let payment_indices = vec![true];
143
144 // Create seal proof (empty for this example)
145 let seal_proof = vec![[4u8; 32]];
146 let seal_indices = vec![false];
147
148 // Payment root (32 bytes)
149 let payment_root = [5u8; 32];
150
151 // Build claim instruction using the converted epoch_data
152 let claim_instruction = claim(
153 Pubkey::new_from_array([6u8; 32]), // signer (placeholder)
154 Pubkey::new_from_array([7u8; 32]), // beneficiary (placeholder)
155 0, // epoch
156 participant_data,
157 payment_proof,
158 payment_indices,
159 seal_proof,
160 seal_indices,
161 payment_root,
162 epoch_data, // Using the converted epoch_data
163 0, // participant_type
164 None, // social_data
165 );
166
167 println!("✅ Claim instruction built successfully!");
168 println!(
169 " Instruction data length: {} bytes",
170 claim_instruction.data.len()
171 );
172 println!(" Number of accounts: {}", claim_instruction.accounts.len());
173
174 // ===== SUMMARY =====
175 println!("\n🎯 Summary");
176 println!("===========");
177 println!("✅ Successfully converted database hex strings to EpochClaimData");
178 println!("✅ Successfully converted native u64 values to EpochClaimData");
179 println!("✅ Successfully built claim instruction with converted data");
180 println!("✅ All data type conversions working correctly");
181
182 println!("\n🚀 The utils module successfully bridges the gap between:");
183 println!(" - Database hex string output (from SQL functions)");
184 println!(" - On-chain byte array requirements (for EpochClaimData)");
185 println!(" - Claim instruction construction");
186
187 Ok(())
188}