|
| 1 | +#![no_std] |
| 2 | + |
| 3 | +mod errors; |
| 4 | +mod events; |
| 5 | + |
| 6 | +use soroban_sdk::{contract, contractimpl, Address, BytesN, Env}; |
| 7 | + |
| 8 | +pub use errors::Error; |
| 9 | +pub use events::VerificationSucceeded; |
| 10 | + |
| 11 | +#[contract] |
| 12 | +pub struct ClaimVerifierContract; |
| 13 | + |
| 14 | +#[contractimpl] |
| 15 | +impl ClaimVerifierContract { |
| 16 | + /// Initialize with the authorized signer public key |
| 17 | + /// |
| 18 | + /// # Arguments |
| 19 | + /// * `authorized_signer` - Ed25519 public key (32 bytes) |
| 20 | + /// |
| 21 | + /// # Errors |
| 22 | + /// * `Error::AlreadyInitialized` - called more than once |
| 23 | + pub fn initialize(env: Env, authorized_signer: BytesN<32>) -> Result<(), Error> { |
| 24 | + if env.storage().instance().has(&"signer") { |
| 25 | + return Err(Error::AlreadyInitialized); |
| 26 | + } |
| 27 | + |
| 28 | + env.storage() |
| 29 | + .instance() |
| 30 | + .set(&"signer", &authorized_signer); |
| 31 | + |
| 32 | + Ok(()) |
| 33 | + } |
| 34 | + |
| 35 | + /// Verify an Ed25519 sweep authorization signature |
| 36 | + /// |
| 37 | + /// Message format matches sweep_controller/authorization.rs: |
| 38 | + /// hash(destination + nonce + contract_id) |
| 39 | + /// |
| 40 | + /// # Arguments |
| 41 | + /// * `destination` - Destination wallet address |
| 42 | + /// * `nonce` - Current sweep nonce |
| 43 | + /// * `signature` - Ed25519 signature (64 bytes) |
| 44 | + /// |
| 45 | + /// # Errors |
| 46 | + /// * `Error::NotInitialized` - contract not initialized |
| 47 | + /// * `Error::AuthorizedSignerNotSet` - no signer stored |
| 48 | + /// * `Error::SignatureVerificationFailed` - signature is invalid |
| 49 | + pub fn verify( |
| 50 | + env: Env, |
| 51 | + destination: Address, |
| 52 | + nonce: u64, |
| 53 | + signature: BytesN<64>, |
| 54 | + ) -> Result<(), Error> { |
| 55 | + // Get authorized signer |
| 56 | + let authorized_signer: BytesN<32> = env |
| 57 | + .storage() |
| 58 | + .instance() |
| 59 | + .get(&"signer") |
| 60 | + .ok_or(Error::AuthorizedSignerNotSet)?; |
| 61 | + |
| 62 | + // Construct message: hash(destination + nonce + contract_id) |
| 63 | + let message = Self::construct_message(&env, &destination, nonce); |
| 64 | + |
| 65 | + // Verify Ed25519 signature |
| 66 | + env.crypto() |
| 67 | + .ed25519_verify(&authorized_signer, &message.into(), &signature); |
| 68 | + |
| 69 | + // Emit success event |
| 70 | + events::emit_verification_succeeded(&env, destination, nonce); |
| 71 | + |
| 72 | + Ok(()) |
| 73 | + } |
| 74 | + |
| 75 | + // Private helper — constructs the message hash identical to |
| 76 | + // sweep_controller/authorization.rs construct_sweep_message |
| 77 | + fn construct_message(env: &Env, destination: &Address, nonce: u64) -> BytesN<32> { |
| 78 | + use soroban_sdk::xdr::ToXdr; |
| 79 | + |
| 80 | + let contract_id = env.current_contract_address(); |
| 81 | + let mut message = soroban_sdk::Bytes::new(env); |
| 82 | + |
| 83 | + let dest_bytes = destination.to_xdr(env); |
| 84 | + message.append(&dest_bytes); |
| 85 | + |
| 86 | + message.push_back(((nonce >> 56) & 0xFF) as u8); |
| 87 | + message.push_back(((nonce >> 48) & 0xFF) as u8); |
| 88 | + message.push_back(((nonce >> 40) & 0xFF) as u8); |
| 89 | + message.push_back(((nonce >> 32) & 0xFF) as u8); |
| 90 | + message.push_back(((nonce >> 24) & 0xFF) as u8); |
| 91 | + message.push_back(((nonce >> 16) & 0xFF) as u8); |
| 92 | + message.push_back(((nonce >> 8) & 0xFF) as u8); |
| 93 | + message.push_back((nonce & 0xFF) as u8); |
| 94 | + |
| 95 | + let contract_bytes = contract_id.to_xdr(env); |
| 96 | + message.append(&contract_bytes); |
| 97 | + |
| 98 | + env.crypto().sha256(&message).into() |
| 99 | + } |
| 100 | +} |
| 101 | + |
0 commit comments