spark_sdk/signer/traits/secp256k1.rs
1use uuid::Uuid;
2
3use crate::error::SparkSdkError;
4
5pub(crate) enum KeygenMethod {
6 /// Child number, mapped from a UUID
7 Uuid(String),
8
9 /// Random
10 Random,
11}
12
13impl From<Uuid> for KeygenMethod {
14 fn from(uuid: Uuid) -> Self {
15 KeygenMethod::Uuid(uuid.to_string())
16 }
17}
18
19pub trait SparkSignerSecp256k1 {
20 /// Returns the identity public key of the signer. Identity keypair in Spark
21 /// is the first derived key from the master seed (using child index 0).
22 ///
23 /// # Returns
24 /// The identity public key of the signer in a 33-byte array of [`Vec<u8>`].
25 fn get_identity_public_key(&self) -> Result<Vec<u8>, SparkSdkError>;
26
27 /// Generates a random secp256k1 keypair and persists it in the signer space.
28 /// The returned public key is returned in a 33-byte array of [`Vec<u8>`].
29 ///
30 /// # Arguments
31 /// * `use_derivation_index` - Whether to use the derivation index for the keypair. If set to true, the derivation index will be incremented after the keypair is generated.
32 ///
33 /// # Returns
34 /// The public key of the generated keypair in a 33-byte array of [`Vec<u8>`].
35 #[allow(private_interfaces)]
36 fn new_secp256k1_keypair(&self, keygen_method: KeygenMethod) -> Result<Vec<u8>, SparkSdkError>;
37
38 /// Inserts a keypair into the signer by providing the private key. The function
39 /// derives the public key from the private key and persists it in the signer space.
40 ///
41 /// This function is used in the transfer flow, when the receiver needs to insert
42 /// a secret key into the signer at the time of receiving funds.
43 ///
44 /// # Arguments
45 /// * `secret_key_bytes` - The secret key to insert (implements `AsRef<[u8]>`;
46 /// e.g. [`Vec<u8>`], `&[u8]`, [`std::sync::Arc<Vec<u8>>`], etc.)
47 ///
48 /// # Returns
49 /// The public key of the inserted keypair in a 33-byte array of [`Vec<u8>`].
50 fn insert_secp256k1_keypair_from_secret_key<T>(
51 &self,
52 secret_key_bytes: T,
53 ) -> Result<Vec<u8>, SparkSdkError>
54 where
55 T: AsRef<[u8]>;
56
57 /// Subtracts the source secret key from a target secret key to get a new secret key. Returns the public key of the new secret key.
58 ///
59 /// # Arguments
60 /// * `target_pubkey` - The target public key (implements `AsRef<[u8]>`)
61 /// * `source_pubkey` - The source public key (implements `AsRef<[u8]>`)
62 /// * `save_new_key` - Whether to save the newly derived keypair in the signer space
63 ///
64 /// # Returns
65 /// The public key of the resulting secret key in a 33-byte array of [`Vec<u8>`].
66 fn subtract_secret_keys_given_pubkeys<T, U>(
67 &self,
68 target_pubkey: T,
69 source_pubkey: U,
70 save_new_key: bool,
71 ) -> Result<Vec<u8>, SparkSdkError>
72 where
73 T: AsRef<[u8]>,
74 U: AsRef<[u8]>;
75
76 /// Exposes the secret key for a given public key. This is a **highly sensitive**
77 /// operation from a security perspective because it reveals confidential material.
78 /// Use it **only** when absolutely necessary, and handle the returned secret key with caution.
79 ///
80 /// # Arguments
81 /// * `public_key` - The public key to expose the secret key for (implements `AsRef<[u8]>`)
82 /// * `delete_after_exposing` - Whether to delete the secret key after showing it
83 ///
84 /// # Returns
85 /// The secret key of the given public key in a 32-byte array of [`Vec<u8>`].
86 fn sensitive_expose_secret_key_from_pubkey<T>(
87 &self,
88 public_key: T,
89 delete_after_exposing: bool,
90 ) -> Result<Vec<u8>, SparkSdkError>
91 where
92 T: AsRef<[u8]>;
93}