P256 (secp256r1)

P256 is one of NIST's prime-order elliptic curves. You may need this curve to implement protocols such as WebAuthn. This is the main reason this curve was added to CosmWasm.

Example

Signature verification

contract.rs
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(
    deps: Deps,
    _env: Env,
    msg: EcdsaVerifyMsg,
) -> StdResult<QueryResponse> {
    let public_key = msg.public_key;
    let signature = msg.signature;
    let message_hash = msg.message_hash;
 
    // Verify the signature. On chain!
    let is_valid = deps.api.secp256r1_verify(&message_hash, &signature, &public_key)?;
    let response = to_json_binary(&VerifyResponse {
        is_valid
    })?;
 
    Ok(response)
}

Public key recovery

contract.rs
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(
    deps: Deps,
    _env: Env,
    msg: EcdsaRecoverMsg,
) -> StdResult<QueryResponse> {
    let signature = msg.signature;
    let message_hash = msg.message_hash;
    let recovery_id = msg.recovery_id;
 
    // Recover the public key. On chain!
    let public_key = deps.api.secp256r1_recover_pubkey(&message_hash, &signature, recovery_id)?;
    let response = to_json_binary(&RecoveryResponse {
        public_key: public_key.into(),
    })?;
 
    Ok(response)
}