Skip to main content

breez_sdk_spark/signer/
external_spark.rs

1//! External (foreign) high-level Spark signer trait.
2//!
3//! Mirrors `spark_wallet::SparkSigner` using FFI-compatible types so an
4//! integrator can implement the Spark flow-signing surface directly.
5//! The SDK wraps an implementation in
6//! [`ExternalSparkSignerAdapter`](super::ExternalSparkSignerAdapter) to obtain a
7//! native `spark_wallet::SparkSigner`.
8
9use crate::error::SignerError;
10
11use super::external_spark_types::{
12    ExternalFrostJob, ExternalFrostShareResult, ExternalPrepareClaimRequest,
13    ExternalPrepareLightningReceiveRequest, ExternalPrepareStaticDepositClaimRequest,
14    ExternalPrepareStaticDepositRequest, ExternalPrepareTokenTransactionRequest,
15    ExternalPrepareTransferRequest, ExternalPreparedClaim, ExternalPreparedLightningReceive,
16    ExternalPreparedStaticDeposit, ExternalPreparedStaticDepositClaim,
17    ExternalPreparedTokenTransaction, ExternalPreparedTransfer, ExternalSignSparkInvoiceRequest,
18    ExternalSignStaticDepositRefundRequest, ExternalSignWatchtowerExitRecoveryRequest,
19    ExternalSignedSparkInvoice, ExternalStartStaticDepositRefundRequest,
20    ExternalStartWatchtowerExitRecoveryRequest, ExternalStartedStaticDepositRefund,
21    ExternalStartedWatchtowerExitRecovery,
22};
23use super::external_types::{
24    EcdsaSignatureBytes, ExternalFrostSignature, ExternalTreeNodeId, PublicKeyBytes,
25    SchnorrSignatureBytes,
26};
27
28/// FFI-compatible mirror of `spark_wallet::SparkSigner`.
29#[cfg_attr(
30    feature = "uniffi",
31    uniffi::export(with_foreign, async_runtime = "tokio")
32)]
33#[macros::async_trait]
34pub trait ExternalSparkSigner: Send + Sync {
35    /// The wallet identity public key (33 bytes compressed).
36    async fn get_identity_public_key(&self) -> Result<PublicKeyBytes, SignerError>;
37
38    /// The public key of the leaf signing key derived from `leaf_id`.
39    async fn get_public_key_for_leaf(
40        &self,
41        leaf_id: ExternalTreeNodeId,
42    ) -> Result<PublicKeyBytes, SignerError>;
43
44    /// Whether this signer is backed by a remote service, so its operations are
45    /// network round-trips rather than local computation. Local signers return
46    /// false; a hosted signer like Turnkey returns true. With a remote signer the
47    /// SDK skips redundant calls, e.g. re-deriving keys for leaves it has already
48    /// verified, and does not support the watchtower fee ladder.
49    fn is_remote(&self) -> bool {
50        false
51    }
52
53    /// Whether [`sign_frost`](Self::sign_frost) can sign jobs whose
54    /// `adaptor_v2` is set, with version 2 adaptor signing. The SDK only sets it
55    /// for a signer that returns true.
56    fn supports_adaptor_v2(&self) -> bool;
57
58    /// The static-deposit signing public key at `index`.
59    async fn get_static_deposit_public_key(
60        &self,
61        index: u32,
62    ) -> Result<PublicKeyBytes, SignerError>;
63
64    /// ECDSA-sign a server authentication challenge with the identity key.
65    async fn sign_authentication_challenge(
66        &self,
67        challenge: Vec<u8>,
68    ) -> Result<EcdsaSignatureBytes, SignerError>;
69
70    /// ECDSA-sign an arbitrary user message with the identity key.
71    async fn sign_message(&self, message: Vec<u8>) -> Result<EcdsaSignatureBytes, SignerError>;
72
73    /// Schnorr-sign `sighash` to spend a tree leaf's P2TR refund output as a
74    /// BIP341 key-path spend (empty script tree), with the leaf signing key
75    /// derived from `leaf_id`.
76    async fn sign_leaf_refund_spend(
77        &self,
78        leaf_id: ExternalTreeNodeId,
79        sighash: Vec<u8>,
80    ) -> Result<SchnorrSignatureBytes, SignerError>;
81
82    /// Produce FROST shares for a batch of jobs.
83    async fn sign_frost(
84        &self,
85        jobs: Vec<ExternalFrostJob>,
86    ) -> Result<Vec<ExternalFrostShareResult>, SignerError>;
87
88    /// Prepare an outbound transfer (key-tweak + packages + payload signature).
89    async fn prepare_transfer(
90        &self,
91        request: ExternalPrepareTransferRequest,
92    ) -> Result<ExternalPreparedTransfer, SignerError>;
93
94    /// Claim an inbound transfer (key-tweak step).
95    async fn prepare_claim(
96        &self,
97        request: ExternalPrepareClaimRequest,
98    ) -> Result<ExternalPreparedClaim, SignerError>;
99
100    /// Prepare a Lightning receive (in-enclave preimage + Feldman split).
101    async fn prepare_lightning_receive(
102        &self,
103        request: ExternalPrepareLightningReceiveRequest,
104    ) -> Result<ExternalPreparedLightningReceive, SignerError>;
105
106    /// Prepare a static deposit (export secret to SSP + FROST-sign tree txs).
107    async fn prepare_static_deposit(
108        &self,
109        request: ExternalPrepareStaticDepositRequest,
110    ) -> Result<ExternalPreparedStaticDeposit, SignerError>;
111
112    /// Begin a static-deposit refund (user-commits-first).
113    ///
114    /// The implementation keeps the nonce behind the returned commitment until
115    /// [`sign_static_deposit_refund`](Self::sign_static_deposit_refund)
116    /// consumes it, and signs with it once: signing twice under one nonce leaks
117    /// the signing share.
118    async fn start_static_deposit_refund(
119        &self,
120        request: ExternalStartStaticDepositRefundRequest,
121    ) -> Result<ExternalStartedStaticDepositRefund, SignerError>;
122
123    /// Finish a static-deposit refund (aggregate into the final signature).
124    async fn sign_static_deposit_refund(
125        &self,
126        request: ExternalSignStaticDepositRefundRequest,
127    ) -> Result<ExternalFrostSignature, SignerError>;
128
129    /// Begin the recovery of a watchtower-exited leaf: return the public key of
130    /// the leaf's signing key (derived from `leaf_id`), a fresh FROST nonce
131    /// commitment, and the identity key's ECDSA signature over the SHA-256 hash
132    /// of `user_statement`.
133    ///
134    /// The implementation keeps the nonce behind the returned commitment until
135    /// [`sign_watchtower_exit_recovery`](Self::sign_watchtower_exit_recovery)
136    /// consumes it, and signs with it once: signing twice under one nonce leaks
137    /// the signing share.
138    async fn start_watchtower_exit_recovery(
139        &self,
140        request: ExternalStartWatchtowerExitRecoveryRequest,
141    ) -> Result<ExternalStartedWatchtowerExitRecovery, SignerError>;
142
143    /// Finish the recovery: sign with the leaf's signing key under the nonce
144    /// committed by `start_watchtower_exit_recovery`, and aggregate the
145    /// operators' shares into the final signature.
146    async fn sign_watchtower_exit_recovery(
147        &self,
148        request: ExternalSignWatchtowerExitRecoveryRequest,
149    ) -> Result<ExternalFrostSignature, SignerError>;
150
151    /// Schnorr-sign a Spark invoice (sats or tokens) with the identity key.
152    async fn sign_spark_invoice(
153        &self,
154        request: ExternalSignSparkInvoiceRequest,
155    ) -> Result<ExternalSignedSparkInvoice, SignerError>;
156
157    /// Schnorr-sign a token-transaction digest with the identity key.
158    async fn prepare_token_transaction(
159        &self,
160        request: ExternalPrepareTokenTransactionRequest,
161    ) -> Result<ExternalPreparedTokenTransaction, SignerError>;
162
163    /// Prepare a static-deposit claim (export secret in the clear + sign).
164    async fn prepare_static_deposit_claim(
165        &self,
166        request: ExternalPrepareStaticDepositClaimRequest,
167    ) -> Result<ExternalPreparedStaticDepositClaim, SignerError>;
168}