|
@@ -4,6 +4,8 @@ use std::str::FromStr;
|
|
|
use bitcoin::hashes::sha256::Hash as Sha256Hash;
|
|
|
use bitcoin::hashes::Hash;
|
|
|
use bitcoin::XOnlyPublicKey;
|
|
|
+use cdk_common::util::unix_time;
|
|
|
+use cdk_common::wallet::{Transaction, TransactionDirection};
|
|
|
use tracing::instrument;
|
|
|
|
|
|
use crate::amount::SplitTarget;
|
|
@@ -21,9 +23,8 @@ impl Wallet {
|
|
|
pub async fn receive_proofs(
|
|
|
&self,
|
|
|
proofs: Proofs,
|
|
|
- amount_split_target: SplitTarget,
|
|
|
- p2pk_signing_keys: &[SecretKey],
|
|
|
- preimages: &[String],
|
|
|
+ opts: ReceiveOptions,
|
|
|
+ memo: Option<String>,
|
|
|
) -> Result<Amount, Error> {
|
|
|
let mint_url = &self.mint_url;
|
|
|
// Add mint if it does not exist in the store
|
|
@@ -45,10 +46,14 @@ impl Wallet {
|
|
|
|
|
|
let mut proofs = proofs;
|
|
|
|
|
|
+ let proofs_amount = proofs.total_amount()?;
|
|
|
+ let proofs_ys = proofs.ys()?;
|
|
|
+
|
|
|
let mut sig_flag = SigFlag::SigInputs;
|
|
|
|
|
|
// Map hash of preimage to preimage
|
|
|
- let hashed_to_preimage: HashMap<String, &String> = preimages
|
|
|
+ let hashed_to_preimage: HashMap<String, &String> = opts
|
|
|
+ .preimages
|
|
|
.iter()
|
|
|
.map(|p| {
|
|
|
let hex_bytes = hex::decode(p)?;
|
|
@@ -56,7 +61,8 @@ impl Wallet {
|
|
|
})
|
|
|
.collect::<Result<HashMap<String, &String>, _>>()?;
|
|
|
|
|
|
- let p2pk_signing_keys: HashMap<XOnlyPublicKey, &SecretKey> = p2pk_signing_keys
|
|
|
+ let p2pk_signing_keys: HashMap<XOnlyPublicKey, &SecretKey> = opts
|
|
|
+ .p2pk_signing_keys
|
|
|
.iter()
|
|
|
.map(|s| (s.x_only_public_key(&SECP256K1).0, s))
|
|
|
.collect();
|
|
@@ -117,7 +123,7 @@ impl Wallet {
|
|
|
.await?;
|
|
|
|
|
|
let mut pre_swap = self
|
|
|
- .create_swap(None, amount_split_target, proofs, None, false)
|
|
|
+ .create_swap(None, opts.amount_split_target, proofs, None, false)
|
|
|
.await?;
|
|
|
|
|
|
if sig_flag.eq(&SigFlag::SigAll) {
|
|
@@ -155,6 +161,21 @@ impl Wallet {
|
|
|
)
|
|
|
.await?;
|
|
|
|
|
|
+ // Add transaction to store
|
|
|
+ self.localstore
|
|
|
+ .add_transaction(Transaction {
|
|
|
+ mint_url: self.mint_url.clone(),
|
|
|
+ direction: TransactionDirection::Incoming,
|
|
|
+ amount: total_amount,
|
|
|
+ fee: proofs_amount - total_amount,
|
|
|
+ unit: self.unit.clone(),
|
|
|
+ ys: proofs_ys,
|
|
|
+ timestamp: unix_time(),
|
|
|
+ memo,
|
|
|
+ metadata: opts.metadata,
|
|
|
+ })
|
|
|
+ .await?;
|
|
|
+
|
|
|
Ok(total_amount)
|
|
|
}
|
|
|
|
|
@@ -166,7 +187,7 @@ impl Wallet {
|
|
|
/// use cdk::amount::SplitTarget;
|
|
|
/// use cdk_sqlite::wallet::memory;
|
|
|
/// use cdk::nuts::CurrencyUnit;
|
|
|
- /// use cdk::wallet::Wallet;
|
|
|
+ /// use cdk::wallet::{ReceiveOptions, Wallet};
|
|
|
/// use rand::random;
|
|
|
///
|
|
|
/// #[tokio::main]
|
|
@@ -178,7 +199,7 @@ impl Wallet {
|
|
|
/// let localstore = memory::empty().await?;
|
|
|
/// let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None).unwrap();
|
|
|
/// let token = "cashuAeyJ0b2tlbiI6W3sicHJvb2ZzIjpbeyJhbW91bnQiOjEsInNlY3JldCI6ImI0ZjVlNDAxMDJhMzhiYjg3NDNiOTkwMzU5MTU1MGYyZGEzZTQxNWEzMzU0OTUyN2M2MmM5ZDc5MGVmYjM3MDUiLCJDIjoiMDIzYmU1M2U4YzYwNTMwZWVhOWIzOTQzZmRhMWEyY2U3MWM3YjNmMGNmMGRjNmQ4NDZmYTc2NWFhZjc3OWZhODFkIiwiaWQiOiIwMDlhMWYyOTMyNTNlNDFlIn1dLCJtaW50IjoiaHR0cHM6Ly90ZXN0bnV0LmNhc2h1LnNwYWNlIn1dLCJ1bml0Ijoic2F0In0=";
|
|
|
- /// let amount_receive = wallet.receive(token, SplitTarget::default(), &[], &[]).await?;
|
|
|
+ /// let amount_receive = wallet.receive(token, ReceiveOptions::default()).await?;
|
|
|
/// Ok(())
|
|
|
/// }
|
|
|
/// ```
|
|
@@ -186,9 +207,7 @@ impl Wallet {
|
|
|
pub async fn receive(
|
|
|
&self,
|
|
|
encoded_token: &str,
|
|
|
- amount_split_target: SplitTarget,
|
|
|
- p2pk_signing_keys: &[SecretKey],
|
|
|
- preimages: &[String],
|
|
|
+ opts: ReceiveOptions,
|
|
|
) -> Result<Amount, Error> {
|
|
|
let token = Token::from_str(encoded_token)?;
|
|
|
|
|
@@ -205,7 +224,7 @@ impl Wallet {
|
|
|
ensure_cdk!(self.mint_url == token.mint_url()?, Error::IncorrectMint);
|
|
|
|
|
|
let amount = self
|
|
|
- .receive_proofs(proofs, amount_split_target, p2pk_signing_keys, preimages)
|
|
|
+ .receive_proofs(proofs, opts, token.memo().clone())
|
|
|
.await?;
|
|
|
|
|
|
Ok(amount)
|
|
@@ -219,7 +238,7 @@ impl Wallet {
|
|
|
/// use cdk::amount::SplitTarget;
|
|
|
/// use cdk_sqlite::wallet::memory;
|
|
|
/// use cdk::nuts::CurrencyUnit;
|
|
|
- /// use cdk::wallet::Wallet;
|
|
|
+ /// use cdk::wallet::{ReceiveOptions, Wallet};
|
|
|
/// use cdk::util::hex;
|
|
|
/// use rand::random;
|
|
|
///
|
|
@@ -232,7 +251,7 @@ impl Wallet {
|
|
|
/// let localstore = memory::empty().await?;
|
|
|
/// let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None).unwrap();
|
|
|
/// let token_raw = hex::decode("6372617742a4617481a261694800ad268c4d1f5826617081a3616101617378403961366462623834376264323332626137366462306466313937323136623239643362386363313435353363643237383237666331636339343266656462346561635821038618543ffb6b8695df4ad4babcde92a34a96bdcd97dcee0d7ccf98d4721267926164695468616e6b20796f75616d75687474703a2f2f6c6f63616c686f73743a33333338617563736174").unwrap();
|
|
|
- /// let amount_receive = wallet.receive_raw(&token_raw, SplitTarget::default(), &[], &[]).await?;
|
|
|
+ /// let amount_receive = wallet.receive_raw(&token_raw, ReceiveOptions::default()).await?;
|
|
|
/// Ok(())
|
|
|
/// }
|
|
|
/// ```
|
|
@@ -240,17 +259,22 @@ impl Wallet {
|
|
|
pub async fn receive_raw(
|
|
|
&self,
|
|
|
binary_token: &Vec<u8>,
|
|
|
- amount_split_target: SplitTarget,
|
|
|
- p2pk_signing_keys: &[SecretKey],
|
|
|
- preimages: &[String],
|
|
|
+ opts: ReceiveOptions,
|
|
|
) -> Result<Amount, Error> {
|
|
|
let token_str = Token::try_from(binary_token)?.to_string();
|
|
|
- self.receive(
|
|
|
- token_str.as_str(),
|
|
|
- amount_split_target,
|
|
|
- p2pk_signing_keys,
|
|
|
- preimages,
|
|
|
- )
|
|
|
- .await
|
|
|
+ self.receive(token_str.as_str(), opts).await
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+/// Receive options
|
|
|
+#[derive(Debug, Clone, Default)]
|
|
|
+pub struct ReceiveOptions {
|
|
|
+ /// Amount split target
|
|
|
+ pub amount_split_target: SplitTarget,
|
|
|
+ /// P2PK signing keys
|
|
|
+ pub p2pk_signing_keys: Vec<SecretKey>,
|
|
|
+ /// Preimages
|
|
|
+ pub preimages: Vec<String>,
|
|
|
+ /// Metadata
|
|
|
+ pub metadata: HashMap<String, String>,
|
|
|
+}
|