Skip to main content

world_id_authenticator/
traits.rs

1use ark_serialize::CanonicalSerialize;
2use eddsa_babyjubjub::{EdDSAPublicKey, EdDSASignature};
3use ruint::aliases::U256;
4use secrecy::ExposeSecret;
5use world_id_primitives::{FieldElement, PrimitiveError, authenticator::ProtocolSigner};
6
7use crate::authenticator::Authenticator;
8
9impl ProtocolSigner for Authenticator {
10    fn sign(&self, message: FieldElement) -> EdDSASignature {
11        self.signer
12            .offchain_signer_private_key()
13            .expose_secret()
14            .sign(*message)
15    }
16}
17
18/// A trait for types that can be represented as a `U256` on-chain.
19pub trait OnchainKeyRepresentable {
20    /// Converts an off-chain public key into a `U256` representation for on-chain use in the `WorldIDRegistry` contract.
21    ///
22    /// The `U256` representation is a 32-byte little-endian encoding of the **compressed** (single point) public key.
23    ///
24    /// # Errors
25    /// Will error if the public key unexpectedly fails to serialize.
26    fn to_ethereum_representation(&self) -> Result<U256, PrimitiveError>;
27}
28
29impl OnchainKeyRepresentable for EdDSAPublicKey {
30    // REVIEW: updating to BE
31    fn to_ethereum_representation(&self) -> Result<U256, PrimitiveError> {
32        let mut compressed_bytes = Vec::new();
33        self.pk
34            .serialize_compressed(&mut compressed_bytes)
35            .map_err(|e| PrimitiveError::Serialization(e.to_string()))?;
36        Ok(U256::from_le_slice(&compressed_bytes))
37    }
38}