Repository navigation
WIP: Silent Payments (Send) #1052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
reez
wants to merge
2
commits into
bitcoindevkit:master
Choose a base branch
from
reez:bdk-sp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,221 @@ | ||
| use crate::bitcoin::{Amount, Network}; | ||
|
|
||
| use bdk_sp::bitcoin::bech32::primitives::{decode::CheckedHrpstring, Bech32m}; | ||
| use bdk_sp::encoding::{ | ||
| ParseError as BdkSilentPaymentCodeParseError, SilentPaymentCode as BdkSilentPaymentCode, | ||
| }; | ||
| use bdk_sp::send::error::SpSendError; | ||
|
|
||
| use std::convert::TryFrom; | ||
| use std::fmt::Display; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Represents a silent payment code containing the necessary keys and network information. | ||
| #[derive(Clone, Debug, Eq, PartialEq, uniffi::Object)] | ||
| #[uniffi::export(Eq, Display)] | ||
| pub struct SilentPaymentCode(pub(crate) BdkSilentPaymentCode); | ||
|
|
||
| #[uniffi::export] | ||
| impl SilentPaymentCode { | ||
| /// Attempts to parse a string as a silent payment code. | ||
| #[uniffi::constructor] | ||
| pub fn new(code: String) -> Result<Self, SilentPaymentCodeParseError> { | ||
| let checked_code = CheckedHrpstring::new::<Bech32m>(&code).map_err(|error| { | ||
| SilentPaymentCodeParseError::Bech32 { | ||
| error_message: error.to_string(), | ||
| } | ||
| })?; | ||
| if checked_code | ||
| .fe32_iter::<&mut dyn Iterator<Item = u8>>() | ||
| .next() | ||
| .is_none() | ||
| { | ||
| return Err(SilentPaymentCodeParseError::Version { | ||
| error_message: "payload length does not match version spec".to_string(), | ||
| }); | ||
| } | ||
|
|
||
| BdkSilentPaymentCode::try_from(code.as_str()) | ||
| .map(Self) | ||
| .map_err(SilentPaymentCodeParseError::from) | ||
| } | ||
|
|
||
| /// Returns whether this silent payment code can be used on `network`. | ||
| pub fn is_valid_for_network(&self, network: Network) -> bool { | ||
| matches!( | ||
| (self.0.network, network), | ||
| (Network::Bitcoin, Network::Bitcoin) | ||
| | (Network::Regtest, Network::Regtest) | ||
| | ( | ||
| Network::Testnet | Network::Testnet4 | Network::Signet, | ||
| Network::Testnet | Network::Testnet4 | Network::Signet | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| impl Display for SilentPaymentCode { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| self.0.fmt(f) | ||
| } | ||
| } | ||
|
|
||
| /// A silent payment code and associated amount. | ||
| #[derive(uniffi::Record)] | ||
| pub struct SilentPaymentRecipient { | ||
| /// The recipient's silent payment code. | ||
| pub code: Arc<SilentPaymentCode>, | ||
| /// The amount to send. | ||
| pub amount: Arc<Amount>, | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error, uniffi::Error)] | ||
| pub enum SilentPaymentCodeParseError { | ||
| #[error("invalid Bech32m silent payment code: {error_message}")] | ||
| Bech32 { error_message: String }, | ||
|
|
||
| #[error("unsupported silent payment code version: {error_message}")] | ||
| Version { error_message: String }, | ||
|
|
||
| #[error("unsupported silent payment code human-readable prefix: {error_message}")] | ||
| UnknownHrp { error_message: String }, | ||
|
|
||
| #[error("invalid silent payment public key: {error_message}")] | ||
| InvalidPublicKey { error_message: String }, | ||
| } | ||
|
|
||
| impl From<BdkSilentPaymentCodeParseError> for SilentPaymentCodeParseError { | ||
| fn from(error: BdkSilentPaymentCodeParseError) -> Self { | ||
| match error { | ||
| BdkSilentPaymentCodeParseError::Bech32(error) => Self::Bech32 { | ||
| error_message: error.to_string(), | ||
| }, | ||
| BdkSilentPaymentCodeParseError::Version(error) => Self::Version { | ||
| error_message: error.to_string(), | ||
| }, | ||
| BdkSilentPaymentCodeParseError::UnknownHrp(error) => Self::UnknownHrp { | ||
| error_message: error.to_string(), | ||
| }, | ||
| BdkSilentPaymentCodeParseError::InvalidPubKey(error) => Self::InvalidPublicKey { | ||
| error_message: error.to_string(), | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error, uniffi::Error)] | ||
| pub enum SilentPaymentSendError { | ||
| #[error("at least one silent payment recipient is required")] | ||
| NoRecipients, | ||
|
|
||
| #[error("a silent payment scan key cannot have more than 2323 recipients")] | ||
| RecipientLimitExceeded, | ||
|
|
||
| #[error("silent payment code {code} is not valid for wallet network {wallet_network}")] | ||
| NetworkMismatch { | ||
| code: String, | ||
| wallet_network: String, | ||
| }, | ||
|
|
||
| #[error("foreign inputs are not supported for silent payment sending")] | ||
| ForeignInputsUnsupported, | ||
|
|
||
| #[error("only_witness_utxo is not supported for silent payment sending")] | ||
| OnlyWitnessUtxoUnsupported, | ||
|
|
||
| #[error("P2SH inputs are not supported for silent payment sending")] | ||
| P2shInputsUnsupported, | ||
|
|
||
| #[error("replace-by-fee is not supported for silent payment sending")] | ||
| RbfUnsupported, | ||
|
|
||
| #[error("silent payment sending only supports SIGHASH_ALL or Taproot SIGHASH_DEFAULT")] | ||
| UnsupportedSighash, | ||
|
|
||
| #[error("failed to create silent payment transaction: {error_message}")] | ||
| CreateTransaction { error_message: String }, | ||
|
|
||
| #[error("the initial silent payment signing pass failed: {error_message}")] | ||
| InitialSigning { error_message: String }, | ||
|
|
||
| #[error("the wallet could not finalize the initial silent payment signing pass")] | ||
| InitialSigningIncomplete, | ||
|
|
||
| #[error("silent payment secp256k1 derivation failed: {error_message}")] | ||
| Secp256k1 { error_message: String }, | ||
|
|
||
| #[error("silent payment BIP32 derivation failed: {error_message}")] | ||
| Bip32 { error_message: String }, | ||
|
|
||
| #[error("silent payment derivation requires at least one input outpoint: {error_message}")] | ||
| NoOutpoints { error_message: String }, | ||
|
|
||
| #[error("no eligible inputs are available for silent payment derivation")] | ||
| MissingInputsForSharedSecretDerivation, | ||
|
|
||
| #[error("an input is missing its finalized witness")] | ||
| MissingWitness, | ||
|
|
||
| #[error("an input is missing its previous output")] | ||
| MissingPrevout, | ||
|
|
||
| #[error("a transaction output index is invalid: {error_message}")] | ||
| OutputIndex { error_message: String }, | ||
|
|
||
| #[error("the PSBT is missing a silent payment placeholder output")] | ||
| MissingPlaceholderScript, | ||
|
|
||
| #[error("a required private key for an eligible silent payment input is unavailable")] | ||
| MissingEligibleInputKey, | ||
|
|
||
| #[error("there are fewer silent payment derivations than placeholder outputs")] | ||
| MissingDerivations, | ||
|
|
||
| #[error("there are fewer placeholder outputs than silent payment derivations")] | ||
| MissingOutputs, | ||
|
|
||
| #[error( | ||
| "expected {expected} silent payment placeholder outputs in the built transaction, found {actual}" | ||
| )] | ||
| PlaceholderOutputMismatch { expected: u64, actual: u64 }, | ||
|
|
||
| #[error("silent payment output derivation left a placeholder output in the transaction")] | ||
| PlaceholderNotReplaced, | ||
|
|
||
| #[error("the final silent payment signing pass failed: {error_message}")] | ||
| FinalSigning { error_message: String }, | ||
|
|
||
| #[error("the wallet could not finalize the final silent payment signing pass")] | ||
| FinalSigningIncomplete, | ||
|
|
||
| #[error("failed to extract the signed silent payment transaction: {error_message}")] | ||
| ExtractTransaction { error_message: String }, | ||
| } | ||
|
|
||
| impl From<SpSendError> for SilentPaymentSendError { | ||
| fn from(error: SpSendError) -> Self { | ||
| match error { | ||
| SpSendError::Secp256k1Error(error) => Self::Secp256k1 { | ||
| error_message: error.to_string(), | ||
| }, | ||
| SpSendError::Bip32Error(error) => Self::Bip32 { | ||
| error_message: error.to_string(), | ||
| }, | ||
| SpSendError::NoOutpoints(error) => Self::NoOutpoints { | ||
| error_message: error.to_string(), | ||
| }, | ||
| SpSendError::MissingInputsForSharedSecretDerivation => { | ||
| Self::MissingInputsForSharedSecretDerivation | ||
| } | ||
| SpSendError::MissingWitness => Self::MissingWitness, | ||
| SpSendError::MissingPrevout => Self::MissingPrevout, | ||
| SpSendError::IndexError(error) => Self::OutputIndex { | ||
| error_message: error.to_string(), | ||
| }, | ||
| SpSendError::MissingPlaceholderScript => Self::MissingPlaceholderScript, | ||
| SpSendError::KeyError => Self::MissingEligibleInputKey, | ||
| SpSendError::MissingDerivations => Self::MissingDerivations, | ||
| SpSendError::MissingOutputs => Self::MissingOutputs, | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking through the
BdkSilentPaymentCode::try_fromimplementation I realize the manual pre-check using CheckedHrpstring is not necessary, it should be dropped if that's ok, since theBdkSilentPaymentCode::try_fromimplementation already performs internal Bech32m validation and catches all wrong-length or empty payload scenarios on its own.