Skip to content

Commit

Permalink
beginning of transparent tables
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2 committed Sep 16, 2024
1 parent 971d571 commit d1ee438
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 7 deletions.
3 changes: 3 additions & 0 deletions zcash_client_memory/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ pub(crate) use notes::*;
pub(crate) use nullifier::*;

pub(crate) use transaction::*;

pub(crate) mod transparent;
pub(crate) use transparent::*;
3 changes: 3 additions & 0 deletions zcash_client_memory/src/types/serialization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub use memo::*;
pub use array::*;
pub use bytes::*;

mod transparent;
pub use transparent::*;

impl ToArray<u8, 32> for BlockHash {
fn to_array(&self) -> [u8; 32] {
self.0
Expand Down
63 changes: 57 additions & 6 deletions zcash_client_memory/src/types/serialization/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};

use serde_with::serde_as;

use serde_with::FromInto;

use zcash_primitives::transaction::TxId;
use serde_with::{serde_as, TryFromInto};
use serde_with::{DeserializeAs, SerializeAs};

use super::FromArray;
use super::ToArray;
use serde_with::FromInto;
use zcash_primitives::legacy::Script;
use zcash_primitives::transaction::components::amount::NonNegativeAmount;
use zcash_primitives::transaction::components::TxOut;
use zcash_primitives::transaction::TxId;

impl ToArray<u8, 32> for TxId {
fn to_array(&self) -> [u8; 32] {
Expand Down Expand Up @@ -35,3 +37,52 @@ pub enum TransactionStatusDef {
/// block at the provided height.
Mined(#[serde_as(as = "FromInto<u32>")] zcash_primitives::consensus::BlockHeight),
}

#[serde_as]
#[derive(Serialize, Deserialize)]
#[serde(remote = "TxOut")]
pub struct TxOutDef {
#[serde_as(as = "TryFromInto<u64>")]
pub value: NonNegativeAmount,
#[serde_as(as = "ScriptDef")]
pub script_pubkey: Script,
}

impl SerializeAs<TxOut> for TxOutDef {
fn serialize_as<S>(value: &TxOut, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
TxOutDef::serialize(value, serializer)
}
}

impl<'de> DeserializeAs<'de, TxOut> for TxOutDef {
fn deserialize_as<D>(deserializer: D) -> Result<TxOut, D::Error>
where
D: Deserializer<'de>,
{
TxOutDef::deserialize(deserializer)
}
}

#[derive(Serialize, Deserialize)]
#[serde(remote = "Script")]
pub struct ScriptDef(pub Vec<u8>);
impl SerializeAs<Script> for ScriptDef {
fn serialize_as<S>(value: &Script, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
ScriptDef::serialize(value, serializer)
}
}

impl<'de> DeserializeAs<'de, Script> for ScriptDef {
fn deserialize_as<D>(deserializer: D) -> Result<Script, D::Error>
where
D: Deserializer<'de>,
{
ScriptDef::deserialize(deserializer)
}
}
25 changes: 25 additions & 0 deletions zcash_client_memory/src/types/serialization/transparent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// use sapling::{value::NoteValue, PaymentAddress, Rseed};
// use serde_with::{DeserializeAs, SerializeAs};

// use zcash_address::ZcashAddress;
// use zcash_client_backend::wallet::Note;
// use zip32::Scope;

// use std::io;

// use serde::{Deserialize, Deserializer, Serialize, Serializer};

// use serde_with::serde_as;

// use zcash_client_backend::wallet::{NoteId, Recipient};

// use crate::{ByteArray, TransparentAddressDef, TryByteArray, ZcashAddressDef};

// use zcash_primitives::{
// legacy::TransparentAddress,
// transaction::{components::OutPoint, TxId},
// };

// use zcash_protocol::{PoolType, ShieldedProtocol};

// use super::{ToArray, TryFromArray};
60 changes: 60 additions & 0 deletions zcash_client_memory/src/types/transparent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use serde_with::FromInto;
use zcash_client_backend::wallet::WalletTransparentOutput;
use zcash_primitives::{
legacy::TransparentAddress,
transaction::{
components::{OutPoint, TxOut},
TxId,
},
};
use zcash_protocol::consensus::BlockHeight;

use super::AccountId;
use crate::{ByteArray, OutPointDef, TransparentAddressDef, TxOutDef};

pub struct TransparentOutputTable(BTreeMap<OutPoint, ReceivedTransparentUtxo>);

impl TransparentOutputTable {
pub fn new() -> Self {
Self(BTreeMap::new())
}
}

#[serde_as]
#[derive(Serialize, Deserialize)]
pub struct ReceivedTransparentUtxoSpends(
#[serde_as(as = "BTreeMap<OutPointDef, ByteArray<32>>")] BTreeMap<OutPoint, TxId>,
);

#[serde_as]
#[derive(Serialize, Deserialize)]
// transparent_received_outputs
pub struct ReceivedTransparentUtxo {
// transaction_id, output_index
#[serde_as(as = "OutPointDef")]
outpoint: OutPoint,
// Spend authority of this utxo
account_id: AccountId,
// value_zat, script_pubkey
#[serde_as(as = "TxOutDef")]
txout: TxOut,
#[serde_as(as = "TransparentAddressDef")]
recipient_address: TransparentAddress,
#[serde_as(as = "Option<FromInto<u32>>")]
max_observed_unspent_height: Option<BlockHeight>,
// ??? do we need?
#[serde_as(as = "Option<FromInto<u32>>")]
mined_height: Option<BlockHeight>,
}

#[serde_as]
#[derive(Serialize, Deserialize)]
pub struct SentTransparentUtxoTable(
#[serde_as(as = "BTreeMap<OutPointDef,_>")] BTreeMap<OutPoint, SentUtxo>,
);
#[derive(Serialize, Deserialize)]
struct SentUtxo {}
26 changes: 25 additions & 1 deletion zcash_client_memory/src/wallet_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ use {secrecy::ExposeSecret, zip32::fingerprint::SeedFingerprint};
#[cfg(feature = "orchard")]
use zcash_protocol::ShieldedProtocol::Orchard;

#[cfg(feature = "transparent-inputs")]
use {
zcash_client_backend::wallet::TransparentAddressMetadata,
zcash_primitives::legacy::TransparentAddress,
};

impl<P: consensus::Parameters> WalletWrite for MemoryWalletDb<P> {
type UtxoRef = u32;

Expand Down Expand Up @@ -578,9 +584,16 @@ impl<P: consensus::Parameters> WalletWrite for MemoryWalletDb<P> {
/// Adds a transparent UTXO received by the wallet to the data store.
fn put_received_transparent_utxo(
&mut self,
_output: &WalletTransparentOutput,
output: &WalletTransparentOutput,
) -> Result<Self::UtxoRef, Self::Error> {
tracing::debug!("put_received_transparent_utxo");
let address = output.recipient_address();
// TODO: find_account_for_transparent_address??
if let Some(receiving_account) = self.find_account_for_ephemeral_address(address)? {}
#[cfg(not(feature = "transparent-inputs"))]
panic!(
"The wallet must be compiled with the transparent-inputs feature to use this method."
);
Ok(0)
}

Expand Down Expand Up @@ -700,6 +713,17 @@ Instead derive the ufvk in the calling code and import it using `import_account_
}
Ok(())
}
#[cfg(feature = "transparent-inputs")]
fn reserve_next_n_ephemeral_addresses(
&mut self,
_account_id: Self::AccountId,
_n: usize,
) -> Result<Vec<(TransparentAddress, TransparentAddressMetadata)>, Self::Error> {
// Default impl is required for feature-flagged trait methods to prevent
// breakage due to inadvertent activation of features by transitive dependencies
// of the implementing crate.
Ok(vec![])
}

fn set_transaction_status(
&mut self,
Expand Down

0 comments on commit d1ee438

Please sign in to comment.