-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Schnorr signing API (#518)
Co-authored-by: Severin Siffert <[email protected]>
- Loading branch information
Showing
7 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ pub mod ecdsa; | |
pub mod http_request; | ||
pub mod main; | ||
pub mod provisional; | ||
pub mod schnorr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//! Threshold Schnorr signing API. | ||
use crate::api::call::{call, call_with_payment128, CallResult}; | ||
use candid::Principal; | ||
|
||
mod types; | ||
pub use types::*; | ||
|
||
// Source: https://internetcomputer.org/docs/current/references/t-sigs-how-it-works/#fees-for-the-t-schnorr-production-key | ||
const SIGN_WITH_SCHNORR_FEE: u128 = 26_153_846_153; | ||
|
||
/// Return a SEC1 encoded Schnorr public key for the given canister using the given derivation path. | ||
/// | ||
/// See [IC method `schnorr_public_key`](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-schnorr_public_key). | ||
pub async fn schnorr_public_key( | ||
arg: SchnorrPublicKeyArgument, | ||
) -> CallResult<(SchnorrPublicKeyResponse,)> { | ||
call( | ||
Principal::management_canister(), | ||
"schnorr_public_key", | ||
(arg,), | ||
) | ||
.await | ||
} | ||
|
||
/// Return a new Schnorr signature of the given message that can be separately verified against a derived Schnorr public key. | ||
/// | ||
/// See [IC method `sign_with_schnorr`](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-sign_with_schnorr). | ||
/// | ||
/// This call requires cycles payment. | ||
/// This method handles the cycles cost under the hood. | ||
/// Check [Threshold signatures](https://internetcomputer.org/docs/current/references/t-sigs-how-it-works) for more details. | ||
pub async fn sign_with_schnorr( | ||
arg: SignWithSchnorrArgument, | ||
) -> CallResult<(SignWithSchnorrResponse,)> { | ||
call_with_payment128( | ||
Principal::management_canister(), | ||
"sign_with_schnorr", | ||
(arg,), | ||
SIGN_WITH_SCHNORR_FEE, | ||
) | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use candid::CandidType; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::super::main::CanisterId; | ||
|
||
/// Argument Type of [schnorr_public_key](super::schnorr_public_key). | ||
#[derive( | ||
CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default, | ||
)] | ||
pub struct SchnorrPublicKeyArgument { | ||
/// Canister id, default to the canister id of the caller if None. | ||
pub canister_id: Option<CanisterId>, | ||
/// A vector of variable length byte strings. | ||
pub derivation_path: Vec<Vec<u8>>, | ||
/// See [SchnorrKeyId]. | ||
pub key_id: SchnorrKeyId, | ||
} | ||
|
||
/// Response Type of [schnorr_public_key](super::schnorr_public_key). | ||
#[derive( | ||
CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default, | ||
)] | ||
pub struct SchnorrPublicKeyResponse { | ||
/// An Schnorr public key encoded in SEC1 compressed form. | ||
pub public_key: Vec<u8>, | ||
/// Can be used to deterministically derive child keys of the public_key. | ||
pub chain_code: Vec<u8>, | ||
} | ||
|
||
/// Argument Type of [sign_with_schnorr](super::sign_with_schnorr). | ||
#[derive( | ||
CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default, | ||
)] | ||
pub struct SignWithSchnorrArgument { | ||
/// Message to be signed. | ||
pub message: Vec<u8>, | ||
/// A vector of variable length byte strings. | ||
pub derivation_path: Vec<Vec<u8>>, | ||
/// See [SchnorrKeyId]. | ||
pub key_id: SchnorrKeyId, | ||
} | ||
|
||
/// Response Type of [sign_with_schnorr](super::sign_with_schnorr). | ||
#[derive( | ||
CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default, | ||
)] | ||
pub struct SignWithSchnorrResponse { | ||
/// The encoding of the signature depends on the key ID's algorithm. | ||
pub signature: Vec<u8>, | ||
} | ||
|
||
/// Schnorr KeyId. | ||
#[derive( | ||
CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default, | ||
)] | ||
pub struct SchnorrKeyId { | ||
/// See [SchnorrAlgorithm]. | ||
pub algorithm: SchnorrAlgorithm, | ||
/// Name. | ||
pub name: String, | ||
} | ||
|
||
/// Schnorr Algorithm. | ||
#[derive( | ||
CandidType, | ||
Serialize, | ||
Deserialize, | ||
Debug, | ||
PartialEq, | ||
Eq, | ||
PartialOrd, | ||
Ord, | ||
Hash, | ||
Clone, | ||
Copy, | ||
Default, | ||
)] | ||
pub enum SchnorrAlgorithm { | ||
/// BIP-340 secp256k1. | ||
#[serde(rename = "bip340secp256k1")] | ||
#[default] | ||
Bip340secp256k1, | ||
/// ed25519. | ||
#[serde(rename = "ed25519")] | ||
Ed25519, | ||
} |