Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions integration_tests/tests/auth_transfer/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use std::time::Duration;

use anyhow::{Context as _, Result};
use authenticated_transfer_core::Instruction as AuthTransferInstruction;
use common::transaction::LeeTransaction;
use integration_tests::{
TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, verify_commitment_is_in_state,
};
use lee::{privacy_preserving_transaction::circuit::ProgramWithDependencies, program::Program};
use sequencer_service_rpc::RpcClient as _;
use tokio::test;
use wallet::{AccountIdentity, poller::TxPoller};

#[test]
async fn public_build_requires_explicit_submission() -> Result<()> {
let mut ctx = TestContext::new().await?;
let (account_id, _) = ctx.wallet_mut().create_new_account_public(None);
let before_build = ctx.sequencer_client().get_account(account_id).await?;

let transaction = ctx
.wallet()
.build_pub_tx(
vec![AccountIdentity::Public(account_id)],
Program::serialize_instruction(AuthTransferInstruction::Initialize)?,
programs::authenticated_transfer().id(),
)
.await?;
let transaction = LeeTransaction::Public(transaction);
let transaction_hash = transaction.hash();

// Let a sequencer block interval pass: accidental submission would index the transaction.
tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await;
assert!(
ctx.sequencer_client()
.get_transaction(transaction_hash)
.await?
.is_none(),
"build_pub_tx must not submit the transaction"
);

let after_build = ctx.sequencer_client().get_account(account_id).await?;
assert_eq!(after_build.balance, before_build.balance);
assert_eq!(after_build.nonce, before_build.nonce);
assert_eq!(after_build.program_owner, before_build.program_owner);

let hash = ctx.wallet().submit_transaction(transaction).await?;
assert_eq!(hash, transaction_hash);
let (submitted, _) = TxPoller::new(ctx.wallet().config(), ctx.sequencer_client().clone())
.poll_tx(hash)
.await?;
assert!(matches!(submitted, LeeTransaction::Public(_)));

let after_submit = ctx.sequencer_client().get_account(account_id).await?;
assert_eq!(
after_submit.program_owner,
programs::authenticated_transfer().id()
);
assert_eq!(after_submit.nonce.0, before_build.nonce.0 + 1);

Ok(())
}

#[test]
async fn private_build_requires_explicit_submission() -> Result<()> {
let ctx = TestContext::new().await?;
let from = ctx.existing_private_accounts()[0];
let to = ctx.existing_private_accounts()[1];
let from_commitment = ctx
.wallet()
.get_private_account_commitment(from)
.context("sender commitment should exist")?;
let to_commitment = ctx
.wallet()
.get_private_account_commitment(to)
.context("recipient commitment should exist")?;
assert!(verify_commitment_is_in_state(from_commitment.clone(), ctx.sequencer_client()).await);
assert!(verify_commitment_is_in_state(to_commitment.clone(), ctx.sequencer_client()).await);
let program: ProgramWithDependencies = programs::authenticated_transfer().into();

let (transaction, shared_secrets) = ctx
.wallet()
.build_privacy_preserving_tx(
vec![
AccountIdentity::PrivateOwned(from),
AccountIdentity::PrivateOwned(to),
],
Program::serialize_instruction(AuthTransferInstruction::Transfer { amount: 100 })?,
&program,
)
.await?;
let transaction = LeeTransaction::PrivacyPreserving(transaction);
let transaction_hash = transaction.hash();

assert_eq!(shared_secrets.len(), 2);

// Let a sequencer block interval pass: accidental submission would index the transaction.
tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await;
assert!(
ctx.sequencer_client()
.get_transaction(transaction_hash)
.await?
.is_none(),
"build_privacy_preserving_tx must not submit the transaction"
);
assert!(verify_commitment_is_in_state(from_commitment, ctx.sequencer_client()).await);
assert!(verify_commitment_is_in_state(to_commitment, ctx.sequencer_client()).await);

let hash = ctx.wallet().submit_transaction(transaction).await?;
assert_eq!(hash, transaction_hash);
let (submitted, _) = TxPoller::new(ctx.wallet().config(), ctx.sequencer_client().clone())
.poll_tx(hash)
.await?;
let LeeTransaction::PrivacyPreserving(submitted_transaction) = submitted else {
anyhow::bail!("expected a privacy-preserving transaction");
};

assert_eq!(submitted_transaction.message.new_commitments.len(), 2);
for commitment in submitted_transaction.message.new_commitments {
assert!(verify_commitment_is_in_state(commitment, ctx.sequencer_client()).await);
}

Ok(())
}
1 change: 1 addition & 0 deletions integration_tests/tests/auth_transfer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
reason = "We don't care about these in tests"
)]

mod build;
mod private;
mod public;
99 changes: 99 additions & 0 deletions lez/wallet/src/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ impl fmt::Debug for AccountIdentity {
}

impl AccountIdentity {
/// Returns the account ID represented by this identity.
#[must_use]
pub fn account_id(&self) -> AccountId {
match self {
Self::Public(account_id)
| Self::PublicNoSign(account_id)
| Self::PrivateOwned(account_id)
| Self::PrivatePdaOwned(account_id)
| Self::PublicKeycard { account_id, .. }
| Self::PrivatePdaForeign { account_id, .. }
| Self::PrivatePdaShared { account_id, .. } => *account_id,
Self::PrivateForeign {
npk,
vpk,
identifier,
}
| Self::PrivateShared {
npk,
vpk,
identifier,
..
} => AccountId::from((npk, vpk, *identifier)),
}
}

#[must_use]
/// Note: `PublicNoSign` still counts as public, the variant just suppresses the signing-key
/// lookup.
Expand Down Expand Up @@ -665,4 +690,78 @@ mod tests {
assert!(acc.is_private());
assert!(!acc.is_public());
}

#[test]
fn account_id_projects_every_identity_variant() {
let explicit_id = AccountId::new([7; 32]);
let npk = NullifierPublicKey([1; 32]);
let vpk = ViewingPublicKey::from_seed(&[2_u8; 32], &[3_u8; 32]);
let identifier = 42;
let derived_id = AccountId::from((&npk, &vpk, identifier));

assert_eq!(
AccountIdentity::Public(explicit_id).account_id(),
explicit_id
);
assert_eq!(
AccountIdentity::PublicNoSign(explicit_id).account_id(),
explicit_id
);
assert_eq!(
AccountIdentity::PublicKeycard {
account_id: explicit_id,
key_path: "m/0".to_owned(),
}
.account_id(),
explicit_id
);
assert_eq!(
AccountIdentity::PrivateOwned(explicit_id).account_id(),
explicit_id
);
assert_eq!(
AccountIdentity::PrivateForeign {
npk,
vpk: vpk.clone(),
identifier,
}
.account_id(),
derived_id
);
assert_eq!(
AccountIdentity::PrivatePdaOwned(explicit_id).account_id(),
explicit_id
);
assert_eq!(
AccountIdentity::PrivatePdaForeign {
account_id: explicit_id,
npk,
vpk: vpk.clone(),
identifier,
}
.account_id(),
explicit_id
);
assert_eq!(
AccountIdentity::PrivateShared {
nsk: [4; 32],
npk,
vpk: vpk.clone(),
identifier,
}
.account_id(),
derived_id
);
assert_eq!(
AccountIdentity::PrivatePdaShared {
account_id: explicit_id,
nsk: [4; 32],
npk,
vpk,
identifier,
}
.account_id(),
explicit_id
);
}
}
Loading
Loading