sign transactions from node's end

This commit is contained in:
Sergio Chouhy 2025-07-14 09:37:00 -03:00
parent f02c97e622
commit 6e48bcfd9e
8 changed files with 411 additions and 375 deletions

1
Cargo.lock generated
View File

@ -996,6 +996,7 @@ dependencies = [
"anyhow", "anyhow",
"elliptic-curve", "elliptic-curve",
"hex", "hex",
"k256",
"log", "log",
"reqwest 0.11.27", "reqwest 0.11.27",
"risc0-zkvm", "risc0-zkvm",

View File

@ -10,6 +10,7 @@ serde_json.workspace = true
serde.workspace = true serde.workspace = true
reqwest.workspace = true reqwest.workspace = true
risc0-zkvm = { git = "https://github.com/risc0/risc0.git", branch = "release-2.2" } risc0-zkvm = { git = "https://github.com/risc0/risc0.git", branch = "release-2.2" }
k256.workspace = true
rs_merkle.workspace = true rs_merkle.workspace = true
sha2.workspace = true sha2.workspace = true

View File

@ -88,3 +88,9 @@ impl ExecutionFailureKind {
Self::DBError(err) Self::DBError(err)
} }
} }
#[derive(Debug, thiserror::Error)]
pub enum TransactionSignatureError {
#[error("invalid signature for transaction body")]
InvalidSignature,
}

View File

@ -1,5 +1,6 @@
use k256::Scalar;
use log::info; use log::info;
use secp256k1_zkp::{PedersenCommitment, PublicKey, Scalar, Tweak}; use secp256k1_zkp::{PedersenCommitment, Tweak};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sha2::{digest::FixedOutput, Digest}; use sha2::{digest::FixedOutput, Digest};
@ -11,6 +12,8 @@ use elliptic_curve::{
}; };
use sha2::digest::typenum::{UInt, UTerm}; use sha2::digest::typenum::{UInt, UTerm};
use crate::TransactionSignatureError;
pub type CipherText = Vec<u8>; pub type CipherText = Vec<u8>;
pub type Nonce = GenericArray<u8, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>>; pub type Nonce = GenericArray<u8, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>>;
pub type Tag = u8; pub type Tag = u8;
@ -57,28 +60,6 @@ pub struct TransactionBody {
pub state_changes: (serde_json::Value, usize), pub state_changes: (serde_json::Value, usize),
} }
#[derive(Serialize, Deserialize)]
struct TransactionSignature;
#[derive(Serialize, Deserialize)]
struct TransactionHash;
/// A transaction with a signature.
/// Meant to be sent through the network to the sequencer
#[derive(Serialize, Deserialize)]
pub struct SignedTransaction {
body: TransactionBody,
signature: TransactionSignature,
}
/// A transaction with a valid signature over its hash.
/// Can only be constructed from an `UnverifiedSignedTransaction`
/// if the signature is valid
pub struct AuthenticatedTransaction {
hash: TransactionHash,
signed_tx: SignedTransaction
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct MintMoneyPublicTx { pub struct MintMoneyPublicTx {
pub acc: [u8; 32], pub acc: [u8; 32],
@ -172,9 +153,6 @@ impl ActionData {
} }
} }
type SignaturePrivateKey = Scalar;
type SignaturePublicKey = PublicKey;
impl TransactionBody { impl TransactionBody {
/// Computes and returns the SHA-256 hash of the JSON-serialized representation of `self`. /// Computes and returns the SHA-256 hash of the JSON-serialized representation of `self`.
pub fn hash(&self) -> TreeHashType { pub fn hash(&self) -> TreeHashType {
@ -187,16 +165,6 @@ impl TransactionBody {
TreeHashType::from(hasher.finalize_fixed()) TreeHashType::from(hasher.finalize_fixed())
} }
pub fn sign(self, _private_key: SignaturePrivateKey) -> SignedTransaction {
let _hash = self.hash();
// Implement actual signature over `hash`
let signature = TransactionSignature {};
SignedTransaction {
body: self,
signature,
}
}
pub fn log(&self) { pub fn log(&self) {
info!("Transaction hash is {:?}", hex::encode(self.hash())); info!("Transaction hash is {:?}", hex::encode(self.hash()));
info!("Transaction tx_kind is {:?}", self.tx_kind); info!("Transaction tx_kind is {:?}", self.tx_kind);
@ -249,38 +217,62 @@ impl TransactionBody {
} }
} }
type SignaturePrivateKey = Scalar;
#[derive(Debug, Serialize, Deserialize)]
struct TransactionSignature;
type TransactionHash = TreeHashType;
/// A transaction with a signature.
/// Meant to be sent through the network to the sequencer
#[derive(Debug, Serialize, Deserialize)]
pub struct SignedTransaction {
pub body: TransactionBody,
signature: TransactionSignature,
}
impl SignedTransaction { impl SignedTransaction {
pub fn into_authenticated(self) -> AuthenticatedTransaction { pub fn from_transaction_body(
let hash = TransactionHash; // self.body.hash(); body: TransactionBody,
// Check signature over hash _private_key: SignaturePrivateKey,
AuthenticatedTransaction { ) -> SignedTransaction {
hash, let _hash = body.hash();
signed_tx: self // TODO: Implement actual signature over `hash`
} let signature = TransactionSignature {};
Self { body, signature }
} }
pub fn into_authenticated(self) -> Result<AuthenticatedTransaction, TransactionSignatureError> {
let hash = self.body.hash();
// TODO: Check signature over hash
Err(TransactionSignatureError::InvalidSignature)
}
}
/// A transaction with a valid signature over the hash of its body.
/// Can only be constructed from an `SignedTransaction`
/// if the signature is valid
pub struct AuthenticatedTransaction {
hash: TransactionHash,
signed_tx: SignedTransaction,
} }
impl AuthenticatedTransaction { impl AuthenticatedTransaction {
pub fn as_signed(&self) -> &SignedTransaction { pub fn as_signed(&self) -> &SignedTransaction {
&self.signed_tx &self.signed_tx
} }
}
impl Serialize for AuthenticatedTransaction { pub fn body(&self) -> &TransactionBody {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> &self.signed_tx.body
where
S: serde::Serializer,
{
self.as_signed().serialize(serializer)
} }
}
impl<'de> Deserialize<'de> for AuthenticatedTransaction { pub fn signature(&self) -> &TransactionSignature {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> &self.signed_tx.signature
where }
D: serde::Deserializer<'de>,
{ pub fn hash(&self) -> &TransactionHash {
SignedTransaction::deserialize(deserializer).map(|signed_tx| signed_tx.into_authenticated()) &self.hash
} }
} }

View File

@ -3,7 +3,7 @@ use std::sync::{
Arc, Arc,
}; };
use common::ExecutionFailureKind; use common::{transaction::SignedTransaction, ExecutionFailureKind};
use accounts::account_core::{Account, AccountAddress}; use accounts::account_core::{Account, AccountAddress};
use anyhow::Result; use anyhow::Result;
@ -189,7 +189,7 @@ impl NodeCore {
&self, &self,
acc: AccountAddress, acc: AccountAddress,
amount: u128, amount: u128,
) -> Result<(TransactionBody, [u8; 32]), ExecutionFailureKind> { ) -> Result<(SignedTransaction, [u8; 32]), ExecutionFailureKind> {
let (utxo, receipt) = prove_mint_utxo(amount, acc)?; let (utxo, receipt) = prove_mint_utxo(amount, acc)?;
let result_hash = utxo.hash; let result_hash = utxo.hash;
@ -244,31 +244,34 @@ impl NodeCore {
let vec_public_info: Vec<u64> = vec_values_u64.into_iter().flatten().collect(); let vec_public_info: Vec<u64> = vec_values_u64.into_iter().flatten().collect();
let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info); let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info);
let transaction_body = TransactionBody {
tx_kind: TxKind::Private,
execution_input: vec![],
execution_output: vec![],
utxo_commitments_spent_hashes: vec![],
utxo_commitments_created_hashes: comm
.into_iter()
.map(|hash_data| hash_data.try_into().unwrap())
.collect(),
nullifier_created_hashes: vec![],
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(receipt)
.unwrap(),
encoded_data: vec![(encoded_data.0, encoded_data.1.to_vec(), tag)],
ephemeral_pub_key: eph_pub_key.to_vec(),
commitment,
tweak,
secret_r,
sc_addr,
state_changes,
};
// TODO: Change to the correct key once established.
let key_to_sign_transaction = account
.key_holder
.utxo_secret_key_holder
.nullifier_secret_key;
Ok(( Ok((
TransactionBody { SignedTransaction::from_transaction_body(transaction_body, key_to_sign_transaction),
tx_kind: TxKind::Private,
execution_input: vec![],
execution_output: vec![],
utxo_commitments_spent_hashes: vec![],
utxo_commitments_created_hashes: comm
.into_iter()
.map(|hash_data| hash_data.try_into().unwrap())
.collect(),
nullifier_created_hashes: vec![],
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
receipt,
)
.unwrap(),
encoded_data: vec![(encoded_data.0, encoded_data.1.to_vec(), tag)],
ephemeral_pub_key: eph_pub_key.to_vec(),
commitment,
tweak,
secret_r,
sc_addr,
state_changes,
}
.into(),
result_hash, result_hash,
)) ))
} }
@ -278,7 +281,7 @@ impl NodeCore {
acc: AccountAddress, acc: AccountAddress,
amount: u128, amount: u128,
number_of_assets: usize, number_of_assets: usize,
) -> Result<(TransactionBody, Vec<[u8; 32]>), ExecutionFailureKind> { ) -> Result<(SignedTransaction, Vec<[u8; 32]>), ExecutionFailureKind> {
let (utxos, receipt) = prove_mint_utxo_multiple_assets(amount, number_of_assets, acc)?; let (utxos, receipt) = prove_mint_utxo_multiple_assets(amount, number_of_assets, acc)?;
let result_hashes = utxos.iter().map(|utxo| utxo.hash).collect(); let result_hashes = utxos.iter().map(|utxo| utxo.hash).collect();
@ -342,30 +345,34 @@ impl NodeCore {
let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info); let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info);
Ok(( let transaction_body = TransactionBody {
TransactionBody { tx_kind: TxKind::Private,
tx_kind: TxKind::Private, execution_input: vec![],
execution_input: vec![], execution_output: vec![],
execution_output: vec![], utxo_commitments_spent_hashes: vec![],
utxo_commitments_spent_hashes: vec![], utxo_commitments_created_hashes: comm
utxo_commitments_created_hashes: comm .into_iter()
.into_iter() .map(|hash_data| hash_data.try_into().unwrap())
.map(|hash_data| hash_data.try_into().unwrap()) .collect(),
.collect(), nullifier_created_hashes: vec![],
nullifier_created_hashes: vec![], execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(receipt)
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
receipt,
)
.unwrap(), .unwrap(),
encoded_data, encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(), ephemeral_pub_key: eph_pub_key.to_vec(),
commitment, commitment,
tweak, tweak,
secret_r, secret_r,
sc_addr, sc_addr,
state_changes, state_changes,
} };
.into(), // TODO: Change to the correct key once established.
let key_to_sign_transaction = account
.key_holder
.utxo_secret_key_holder
.nullifier_secret_key;
Ok((
SignedTransaction::from_transaction_body(transaction_body, key_to_sign_transaction),
result_hashes, result_hashes,
)) ))
} }
@ -375,7 +382,7 @@ impl NodeCore {
utxo: UTXO, utxo: UTXO,
commitment_in: [u8; 32], commitment_in: [u8; 32],
receivers: Vec<(u128, AccountAddress)>, receivers: Vec<(u128, AccountAddress)>,
) -> Result<(TransactionBody, Vec<(AccountAddress, [u8; 32])>), ExecutionFailureKind> { ) -> Result<(SignedTransaction, Vec<(AccountAddress, [u8; 32])>), ExecutionFailureKind> {
let acc_map_read_guard = self.storage.read().await; let acc_map_read_guard = self.storage.read().await;
let account = acc_map_read_guard.acc_map.get(&utxo.owner).unwrap(); let account = acc_map_read_guard.acc_map.get(&utxo.owner).unwrap();
@ -458,30 +465,35 @@ impl NodeCore {
let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info); let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info);
Ok(( let transaction_body = TransactionBody {
TransactionBody { tx_kind: TxKind::Private,
tx_kind: TxKind::Private, execution_input: vec![],
execution_input: vec![], execution_output: vec![],
execution_output: vec![], utxo_commitments_spent_hashes: vec![commitment_in],
utxo_commitments_spent_hashes: vec![commitment_in], utxo_commitments_created_hashes: commitments
utxo_commitments_created_hashes: commitments .into_iter()
.into_iter() .map(|hash_data| hash_data.try_into().unwrap())
.map(|hash_data| hash_data.try_into().unwrap()) .collect(),
.collect(), nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
nullifier_created_hashes: vec![nullifier.try_into().unwrap()], execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(receipt)
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
receipt,
)
.unwrap(), .unwrap(),
encoded_data, encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(), ephemeral_pub_key: eph_pub_key.to_vec(),
commitment, commitment,
tweak, tweak,
secret_r, secret_r,
sc_addr, sc_addr,
state_changes, state_changes,
} };
.into(),
// TODO: Change to the correct key once established.
let key_to_sign_transaction = account
.key_holder
.utxo_secret_key_holder
.nullifier_secret_key;
Ok((
SignedTransaction::from_transaction_body(transaction_body, key_to_sign_transaction),
utxo_hashes, utxo_hashes,
)) ))
} }
@ -492,7 +504,7 @@ impl NodeCore {
commitments_in: Vec<[u8; 32]>, commitments_in: Vec<[u8; 32]>,
number_to_send: usize, number_to_send: usize,
receiver: AccountAddress, receiver: AccountAddress,
) -> Result<(TransactionBody, Vec<[u8; 32]>, Vec<[u8; 32]>), ExecutionFailureKind> { ) -> Result<(SignedTransaction, Vec<[u8; 32]>, Vec<[u8; 32]>), ExecutionFailureKind> {
let acc_map_read_guard = self.storage.read().await; let acc_map_read_guard = self.storage.read().await;
let account = acc_map_read_guard.acc_map.get(&utxos[0].owner).unwrap(); let account = acc_map_read_guard.acc_map.get(&utxos[0].owner).unwrap();
@ -603,30 +615,35 @@ impl NodeCore {
let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info); let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info);
Ok(( let transaction_body = TransactionBody {
TransactionBody { tx_kind: TxKind::Private,
tx_kind: TxKind::Private, execution_input: vec![],
execution_input: vec![], execution_output: vec![],
execution_output: vec![], utxo_commitments_spent_hashes: commitments_in,
utxo_commitments_spent_hashes: commitments_in, utxo_commitments_created_hashes: commitments
utxo_commitments_created_hashes: commitments .into_iter()
.into_iter() .map(|hash_data| hash_data.try_into().unwrap())
.map(|hash_data| hash_data.try_into().unwrap()) .collect(),
.collect(), nullifier_created_hashes: nullifiers,
nullifier_created_hashes: nullifiers, execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(receipt)
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
receipt,
)
.unwrap(), .unwrap(),
encoded_data, encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(), ephemeral_pub_key: eph_pub_key.to_vec(),
commitment, commitment,
tweak, tweak,
secret_r, secret_r,
sc_addr, sc_addr,
state_changes, state_changes,
} };
.into(),
// TODO: Change to the correct key once established.
let key_to_sign_transaction = account
.key_holder
.utxo_secret_key_holder
.nullifier_secret_key;
Ok((
SignedTransaction::from_transaction_body(transaction_body, key_to_sign_transaction),
utxo_hashes_receiver, utxo_hashes_receiver,
utxo_hashes_not_spent, utxo_hashes_not_spent,
)) ))
@ -637,7 +654,7 @@ impl NodeCore {
acc: AccountAddress, acc: AccountAddress,
balance: u64, balance: u64,
receivers: Vec<(u128, AccountAddress)>, receivers: Vec<(u128, AccountAddress)>,
) -> Result<(TransactionBody, Vec<(AccountAddress, [u8; 32])>), ExecutionFailureKind> { ) -> Result<(SignedTransaction, Vec<(AccountAddress, [u8; 32])>), ExecutionFailureKind> {
let acc_map_read_guard = self.storage.read().await; let acc_map_read_guard = self.storage.read().await;
let account = acc_map_read_guard.acc_map.get(&acc).unwrap(); let account = acc_map_read_guard.acc_map.get(&acc).unwrap();
@ -726,36 +743,41 @@ impl NodeCore {
let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info); let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info);
let transaction_body = TransactionBody {
tx_kind: TxKind::Shielded,
execution_input: serde_json::to_vec(&ActionData::SendMoneyShieldedTx(
SendMoneyShieldedTx {
acc_sender: acc,
amount: balance as u128,
},
))
.unwrap(),
execution_output: vec![],
utxo_commitments_spent_hashes: vec![],
utxo_commitments_created_hashes: commitments
.into_iter()
.map(|hash_data| hash_data.try_into().unwrap())
.collect(),
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(receipt)
.unwrap(),
encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(),
commitment,
tweak,
secret_r,
sc_addr,
state_changes,
};
// TODO: Change to the correct key once established.
let key_to_sign_transaction = account
.key_holder
.utxo_secret_key_holder
.nullifier_secret_key;
Ok(( Ok((
TransactionBody { SignedTransaction::from_transaction_body(transaction_body, key_to_sign_transaction),
tx_kind: TxKind::Shielded,
execution_input: serde_json::to_vec(&ActionData::SendMoneyShieldedTx(
SendMoneyShieldedTx {
acc_sender: acc,
amount: balance as u128,
},
))
.unwrap(),
execution_output: vec![],
utxo_commitments_spent_hashes: vec![],
utxo_commitments_created_hashes: commitments
.into_iter()
.map(|hash_data| hash_data.try_into().unwrap())
.collect(),
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
receipt,
)
.unwrap(),
encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(),
commitment,
tweak,
secret_r,
sc_addr,
state_changes,
}
.into(),
utxo_hashes, utxo_hashes,
)) ))
} }
@ -765,7 +787,7 @@ impl NodeCore {
utxo: UTXO, utxo: UTXO,
comm_gen_hash: [u8; 32], comm_gen_hash: [u8; 32],
receivers: Vec<(u128, AccountAddress)>, receivers: Vec<(u128, AccountAddress)>,
) -> Result<TransactionBody, ExecutionFailureKind> { ) -> Result<SignedTransaction, ExecutionFailureKind> {
let acc_map_read_guard = self.storage.read().await; let acc_map_read_guard = self.storage.read().await;
let commitment_in = acc_map_read_guard let commitment_in = acc_map_read_guard
@ -820,7 +842,7 @@ impl NodeCore {
let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info); let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info);
Ok(TransactionBody { let transaction_body = TransactionBody {
tx_kind: TxKind::Deshielded, tx_kind: TxKind::Deshielded,
execution_input: serde_json::to_vec(&ActionData::SendMoneyDeshieldedTx( execution_input: serde_json::to_vec(&ActionData::SendMoneyDeshieldedTx(
SendMoneyDeshieldedTx { SendMoneyDeshieldedTx {
@ -841,8 +863,18 @@ impl NodeCore {
secret_r, secret_r,
sc_addr, sc_addr,
state_changes, state_changes,
} };
.into())
// TODO: Change to the correct key once established.
let key_to_sign_transaction = account
.key_holder
.utxo_secret_key_holder
.nullifier_secret_key;
Ok(SignedTransaction::from_transaction_body(
transaction_body,
key_to_sign_transaction,
))
} }
pub async fn send_private_mint_tx( pub async fn send_private_mint_tx(
@ -855,10 +887,10 @@ impl NodeCore {
let point_before_prove = std::time::Instant::now(); let point_before_prove = std::time::Instant::now();
let (tx, utxo_hash) = self.mint_utxo_private(acc, amount).await?; let (tx, utxo_hash) = self.mint_utxo_private(acc, amount).await?;
tx.log(); tx.body.log();
let point_after_prove = std::time::Instant::now(); let point_after_prove = std::time::Instant::now();
let commitment_generated_hash = tx.utxo_commitments_created_hashes[0]; let commitment_generated_hash = tx.body.utxo_commitments_created_hashes[0];
let timedelta = (point_after_prove - point_before_prove).as_millis(); let timedelta = (point_after_prove - point_before_prove).as_millis();
info!("Mint utxo proof spent {timedelta:?} milliseconds"); info!("Mint utxo proof spent {timedelta:?} milliseconds");
@ -883,10 +915,10 @@ impl NodeCore {
let (tx, utxo_hashes) = self let (tx, utxo_hashes) = self
.mint_utxo_multiple_assets_private(acc, amount, number_of_assets) .mint_utxo_multiple_assets_private(acc, amount, number_of_assets)
.await?; .await?;
tx.log(); tx.body.log();
let point_after_prove = std::time::Instant::now(); let point_after_prove = std::time::Instant::now();
let commitment_generated_hashes = tx.utxo_commitments_created_hashes.clone(); let commitment_generated_hashes = tx.body.utxo_commitments_created_hashes.clone();
let timedelta = (point_after_prove - point_before_prove).as_millis(); let timedelta = (point_after_prove - point_before_prove).as_millis();
info!("Mint utxo proof spent {timedelta:?} milliseconds"); info!("Mint utxo proof spent {timedelta:?} milliseconds");
@ -898,50 +930,50 @@ impl NodeCore {
)) ))
} }
pub async fn send_public_deposit( // pub async fn send_public_deposit(
&self, // &self,
acc: AccountAddress, // acc: AccountAddress,
amount: u128, // amount: u128,
) -> Result<SendTxResponse, ExecutionFailureKind> { // ) -> Result<SendTxResponse, ExecutionFailureKind> {
//Considering proof time, needs to be done before proof // //Considering proof time, needs to be done before proof
let tx_roots = self.get_roots().await; // let tx_roots = self.get_roots().await;
//
let public_context = { // let public_context = {
let read_guard = self.storage.read().await; // let read_guard = self.storage.read().await;
//
read_guard.produce_context(acc) // read_guard.produce_context(acc)
}; // };
//
let (tweak, secret_r, commitment) = pedersen_commitment_vec( // let (tweak, secret_r, commitment) = pedersen_commitment_vec(
//Will not panic, as public context is serializable // //Will not panic, as public context is serializable
public_context.produce_u64_list_from_context().unwrap(), // public_context.produce_u64_list_from_context().unwrap(),
); // );
//
let sc_addr = hex::encode([0; 32]); // let sc_addr = hex::encode([0; 32]);
//
//Sc does not change its state // //Sc does not change its state
let state_changes: Vec<DataBlobChangeVariant> = vec![]; // let state_changes: Vec<DataBlobChangeVariant> = vec![];
let new_len = 0; // let new_len = 0;
let state_changes = (serde_json::to_value(state_changes).unwrap(), new_len); // let state_changes = (serde_json::to_value(state_changes).unwrap(), new_len);
//
let tx: TransactionBody = // let tx: TransactionBody =
sc_core::transaction_payloads_tools::create_public_transaction_payload( // sc_core::transaction_payloads_tools::create_public_transaction_payload(
serde_json::to_vec(&ActionData::MintMoneyPublicTx(MintMoneyPublicTx { // serde_json::to_vec(&ActionData::MintMoneyPublicTx(MintMoneyPublicTx {
acc, // acc,
amount, // amount,
})) // }))
.unwrap(), // .unwrap(),
commitment, // commitment,
tweak, // tweak,
secret_r, // secret_r,
sc_addr, // sc_addr,
state_changes, // state_changes,
) // )
.into(); // .into();
tx.log(); // tx.log();
//
Ok(self.sequencer_client.send_tx(tx, tx_roots).await?) // Ok(self.sequencer_client.send_tx(tx, tx_roots).await?)
} // }
pub async fn send_private_send_tx( pub async fn send_private_send_tx(
&self, &self,
@ -956,7 +988,7 @@ impl NodeCore {
let (tx, utxo_hashes) = self let (tx, utxo_hashes) = self
.transfer_utxo_private(utxo, comm_hash, receivers) .transfer_utxo_private(utxo, comm_hash, receivers)
.await?; .await?;
tx.log(); tx.body.log();
let point_after_prove = std::time::Instant::now(); let point_after_prove = std::time::Instant::now();
let timedelta = (point_after_prove - point_before_prove).as_millis(); let timedelta = (point_after_prove - point_before_prove).as_millis();
@ -982,7 +1014,7 @@ impl NodeCore {
let (tx, utxo_hashes_received, utxo_hashes_not_spent) = self let (tx, utxo_hashes_received, utxo_hashes_not_spent) = self
.transfer_utxo_multiple_assets_private(utxos, comm_hashes, number_to_send, receiver) .transfer_utxo_multiple_assets_private(utxos, comm_hashes, number_to_send, receiver)
.await?; .await?;
tx.log(); tx.body.log();
let point_after_prove = std::time::Instant::now(); let point_after_prove = std::time::Instant::now();
let timedelta = (point_after_prove - point_before_prove).as_millis(); let timedelta = (point_after_prove - point_before_prove).as_millis();
@ -1008,7 +1040,7 @@ impl NodeCore {
let (tx, utxo_hashes) = self let (tx, utxo_hashes) = self
.transfer_balance_shielded(acc, amount, receivers) .transfer_balance_shielded(acc, amount, receivers)
.await?; .await?;
tx.log(); tx.body.log();
let point_after_prove = std::time::Instant::now(); let point_after_prove = std::time::Instant::now();
let timedelta = (point_after_prove - point_before_prove).as_millis(); let timedelta = (point_after_prove - point_before_prove).as_millis();
@ -1033,7 +1065,7 @@ impl NodeCore {
let tx = self let tx = self
.transfer_utxo_deshielded(utxo, comm_gen_hash, receivers) .transfer_utxo_deshielded(utxo, comm_gen_hash, receivers)
.await?; .await?;
tx.log(); tx.body.log();
let point_after_prove = std::time::Instant::now(); let point_after_prove = std::time::Instant::now();
let timedelta = (point_after_prove - point_before_prove).as_millis(); let timedelta = (point_after_prove - point_before_prove).as_millis();
@ -1167,46 +1199,46 @@ impl NodeCore {
Ok(()) Ok(())
} }
pub async fn operate_account_deposit_public( // pub async fn operate_account_deposit_public(
&mut self, // &mut self,
acc_addr: AccountAddress, // acc_addr: AccountAddress,
amount: u128, // amount: u128,
) -> Result<(), ExecutionFailureKind> { // ) -> Result<(), ExecutionFailureKind> {
let old_balance = { // let old_balance = {
let acc_map_read_guard = self.storage.read().await; // let acc_map_read_guard = self.storage.read().await;
//
let acc = acc_map_read_guard.acc_map.get(&acc_addr).unwrap(); // let acc = acc_map_read_guard.acc_map.get(&acc_addr).unwrap();
//
acc.balance // acc.balance
}; // };
//
info!( // info!(
"Balance of {:?} now is {old_balance:?}", // "Balance of {:?} now is {old_balance:?}",
hex::encode(acc_addr) // hex::encode(acc_addr)
); // );
//
let resp = self.send_public_deposit(acc_addr, amount).await?; // let resp = self.send_public_deposit(acc_addr, amount).await?;
info!("Response for public deposit is {resp:?}"); // info!("Response for public deposit is {resp:?}");
//
info!("Awaiting new blocks"); // info!("Awaiting new blocks");
tokio::time::sleep(std::time::Duration::from_secs(BLOCK_GEN_DELAY_SECS)).await; // tokio::time::sleep(std::time::Duration::from_secs(BLOCK_GEN_DELAY_SECS)).await;
//
let new_balance = { // let new_balance = {
let acc_map_read_guard = self.storage.read().await; // let acc_map_read_guard = self.storage.read().await;
//
let acc = acc_map_read_guard.acc_map.get(&acc_addr).unwrap(); // let acc = acc_map_read_guard.acc_map.get(&acc_addr).unwrap();
//
acc.balance // acc.balance
}; // };
//
info!( // info!(
"Balance of {:?} now is {new_balance:?}, delta is {:?}", // "Balance of {:?} now is {new_balance:?}, delta is {:?}",
hex::encode(acc_addr), // hex::encode(acc_addr),
new_balance - old_balance // new_balance - old_balance
); // );
//
Ok(()) // Ok(())
} // }
pub async fn operate_account_send_shielded_one_receiver( pub async fn operate_account_send_shielded_one_receiver(
&mut self, &mut self,
@ -1361,7 +1393,7 @@ impl NodeCore {
commitment_in: [u8; 32], commitment_in: [u8; 32],
receivers: Vec<(u128, AccountAddress)>, receivers: Vec<(u128, AccountAddress)>,
visibility_list: [bool; 3], visibility_list: [bool; 3],
) -> Result<(TransactionBody, Vec<(AccountAddress, [u8; 32])>), ExecutionFailureKind> { ) -> Result<(SignedTransaction, Vec<(AccountAddress, [u8; 32])>), ExecutionFailureKind> {
let acc_map_read_guard = self.storage.read().await; let acc_map_read_guard = self.storage.read().await;
let account = acc_map_read_guard.acc_map.get(&utxo.owner).unwrap(); let account = acc_map_read_guard.acc_map.get(&utxo.owner).unwrap();
@ -1458,31 +1490,35 @@ impl NodeCore {
let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info); let (tweak, secret_r, commitment) = pedersen_commitment_vec(vec_public_info);
Ok(( let transaction_body = TransactionBody {
TransactionBody { tx_kind: TxKind::Shielded,
tx_kind: TxKind::Shielded, execution_input: vec![],
execution_input: vec![], execution_output: serde_json::to_vec(&publication).unwrap(),
execution_output: serde_json::to_vec(&publication).unwrap(), utxo_commitments_spent_hashes: vec![commitment_in],
utxo_commitments_spent_hashes: vec![commitment_in], utxo_commitments_created_hashes: commitments
utxo_commitments_created_hashes: commitments .clone()
.clone() .into_iter()
.into_iter() .map(|hash_data| hash_data.try_into().unwrap())
.map(|hash_data| hash_data.try_into().unwrap()) .collect(),
.collect(), nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
nullifier_created_hashes: vec![nullifier.try_into().unwrap()], execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(receipt)
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
receipt,
)
.unwrap(), .unwrap(),
encoded_data, encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(), ephemeral_pub_key: eph_pub_key.to_vec(),
commitment, commitment,
tweak, tweak,
secret_r, secret_r,
sc_addr, sc_addr,
state_changes, state_changes,
} };
.into(), // TODO: Change to the correct key once established.
let key_to_sign_transaction = account
.key_holder
.utxo_secret_key_holder
.nullifier_secret_key;
Ok((
SignedTransaction::from_transaction_body(transaction_body, key_to_sign_transaction),
utxo_hashes, utxo_hashes,
)) ))
} }
@ -1502,13 +1538,13 @@ impl NodeCore {
let (tx, utxo_hashes) = self let (tx, utxo_hashes) = self
.split_utxo(utxo, comm_hash, receivers, visibility_list) .split_utxo(utxo, comm_hash, receivers, visibility_list)
.await?; .await?;
tx.log(); tx.body.log();
let point_after_prove = std::time::Instant::now(); let point_after_prove = std::time::Instant::now();
let timedelta = (point_after_prove - point_before_prove).as_millis(); let timedelta = (point_after_prove - point_before_prove).as_millis();
info!("Send private utxo proof spent {timedelta:?} milliseconds"); info!("Send private utxo proof spent {timedelta:?} milliseconds");
let commitments = tx.utxo_commitments_created_hashes.clone(); let commitments = tx.body.utxo_commitments_created_hashes.clone();
Ok(( Ok((
self.sequencer_client.send_tx(tx, tx_roots).await?, self.sequencer_client.send_tx(tx, tx_roots).await?,
@ -1582,17 +1618,17 @@ impl NodeCore {
Ok(()) Ok(())
} }
///Deposit balance, make it private // ///Deposit balance, make it private
pub async fn subscenario_2(&mut self) -> Result<(), ExecutionFailureKind> { // pub async fn subscenario_2(&mut self) -> Result<(), ExecutionFailureKind> {
let acc_addr = self.create_new_account().await; // let acc_addr = self.create_new_account().await;
//
self.operate_account_deposit_public(acc_addr, 100).await?; // self.operate_account_deposit_public(acc_addr, 100).await?;
//
self.operate_account_send_shielded_one_receiver(acc_addr, acc_addr, 100) // self.operate_account_send_shielded_one_receiver(acc_addr, acc_addr, 100)
.await?; // .await?;
//
Ok(()) // Ok(())
} // }
///Mint utxo, privately send it to another user ///Mint utxo, privately send it to another user
pub async fn subscenario_3(&mut self) -> Result<(), ExecutionFailureKind> { pub async fn subscenario_3(&mut self) -> Result<(), ExecutionFailureKind> {
@ -1607,18 +1643,18 @@ impl NodeCore {
Ok(()) Ok(())
} }
///Deposit balance, shielded send it to another user // ///Deposit balance, shielded send it to another user
pub async fn subscenario_4(&mut self) -> Result<(), ExecutionFailureKind> { // pub async fn subscenario_4(&mut self) -> Result<(), ExecutionFailureKind> {
let acc_addr = self.create_new_account().await; // let acc_addr = self.create_new_account().await;
let acc_addr_rec = self.create_new_account().await; // let acc_addr_rec = self.create_new_account().await;
//
self.operate_account_deposit_public(acc_addr, 100).await?; // self.operate_account_deposit_public(acc_addr, 100).await?;
//
self.operate_account_send_shielded_one_receiver(acc_addr, acc_addr_rec, 100) // self.operate_account_send_shielded_one_receiver(acc_addr, acc_addr_rec, 100)
.await?; // .await?;
//
Ok(()) // Ok(())
} // }
///Mint utxo, deshielded send it to another user ///Mint utxo, deshielded send it to another user
pub async fn subscenario_5(&mut self) -> Result<(), ExecutionFailureKind> { pub async fn subscenario_5(&mut self) -> Result<(), ExecutionFailureKind> {

View File

@ -1,11 +1,11 @@
use common::transaction::TransactionBody; use common::transaction::SignedTransaction;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
//Requests //Requests
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct SendTxRequest { pub struct SendTxRequest {
pub transaction: TransactionBody, pub transaction: SignedTransaction,
///UTXO Commitment Root, Pub Tx Root ///UTXO Commitment Root, Pub Tx Root
pub tx_roots: [[u8; 32]; 2], pub tx_roots: [[u8; 32]; 2],
} }

View File

@ -4,7 +4,7 @@ use common::rpc_primitives::requests::{
GetBlockDataRequest, GetBlockDataResponse, GetGenesisIdRequest, GetGenesisIdResponse, GetBlockDataRequest, GetBlockDataResponse, GetGenesisIdRequest, GetGenesisIdResponse,
RegisterAccountRequest, RegisterAccountResponse, RegisterAccountRequest, RegisterAccountResponse,
}; };
use common::transaction::TransactionBody; use common::transaction::SignedTransaction;
use common::{SequencerClientError, SequencerRpcError}; use common::{SequencerClientError, SequencerRpcError};
use json::{SendTxRequest, SendTxResponse, SequencerRpcRequest, SequencerRpcResponse}; use json::{SendTxRequest, SendTxResponse, SequencerRpcRequest, SequencerRpcResponse};
use k256::elliptic_curve::group::GroupEncoding; use k256::elliptic_curve::group::GroupEncoding;
@ -72,7 +72,7 @@ impl SequencerClient {
pub async fn send_tx( pub async fn send_tx(
&self, &self,
transaction: TransactionBody, transaction: SignedTransaction,
tx_roots: [[u8; 32]; 2], tx_roots: [[u8; 32]; 2],
) -> Result<SendTxResponse, SequencerClientError> { ) -> Result<SendTxResponse, SequencerClientError> {
let tx_req = SendTxRequest { let tx_req = SendTxRequest {

View File

@ -42,7 +42,7 @@ pub const EXECUTE_SCENARIO_MULTIPLE_SEND: &str = "execute_scenario_multiple_send
pub const SHOW_ACCOUNT_PUBLIC_BALANCE: &str = "show_account_public_balance"; pub const SHOW_ACCOUNT_PUBLIC_BALANCE: &str = "show_account_public_balance";
pub const SHOW_ACCOUNT_UTXO: &str = "show_account_utxo"; pub const SHOW_ACCOUNT_UTXO: &str = "show_account_utxo";
pub const SHOW_TRANSACTION: &str = "show_transaction"; pub const SHOW_TRANSACTION: &str = "show_transaction";
pub const WRITE_DEPOSIT_PUBLIC_BALANCE: &str = "write_deposit_public_balance"; // pub const WRITE_DEPOSIT_PUBLIC_BALANCE: &str = "write_deposit_public_balance";
pub const WRITE_MINT_UTXO: &str = "write_mint_utxo"; pub const WRITE_MINT_UTXO: &str = "write_mint_utxo";
pub const WRITE_MINT_UTXO_MULTIPLE_ASSETS: &str = "write_mint_utxo_multiple_assets"; pub const WRITE_MINT_UTXO_MULTIPLE_ASSETS: &str = "write_mint_utxo_multiple_assets";
pub const WRITE_SEND_UTXO_PRIVATE: &str = "write_send_utxo_private"; pub const WRITE_SEND_UTXO_PRIVATE: &str = "write_send_utxo_private";
@ -84,18 +84,18 @@ impl JsonHandler {
.subscenario_1() .subscenario_1()
.await .await
.map_err(cast_common_execution_error_into_rpc_error)?, .map_err(cast_common_execution_error_into_rpc_error)?,
2 => store // 2 => store
.subscenario_2() // .subscenario_2()
.await // .await
.map_err(cast_common_execution_error_into_rpc_error)?, // .map_err(cast_common_execution_error_into_rpc_error)?,
3 => store 3 => store
.subscenario_3() .subscenario_3()
.await .await
.map_err(cast_common_execution_error_into_rpc_error)?, .map_err(cast_common_execution_error_into_rpc_error)?,
4 => store // 4 => store
.subscenario_4() // .subscenario_4()
.await // .await
.map_err(cast_common_execution_error_into_rpc_error)?, // .map_err(cast_common_execution_error_into_rpc_error)?,
5 => store 5 => store
.subscenario_5() .subscenario_5()
.await .await
@ -355,35 +355,35 @@ impl JsonHandler {
respond(helperstruct) respond(helperstruct)
} }
pub async fn process_write_deposit_public_balance( // pub async fn process_write_deposit_public_balance(
&self, // &self,
request: Request, // request: Request,
) -> Result<Value, RpcErr> { // ) -> Result<Value, RpcErr> {
let req = WriteDepositPublicBalanceRequest::parse(Some(request.params))?; // let req = WriteDepositPublicBalanceRequest::parse(Some(request.params))?;
//
let acc_addr_hex_dec = hex::decode(req.account_addr.clone()).map_err(|_| { // let acc_addr_hex_dec = hex::decode(req.account_addr.clone()).map_err(|_| {
RpcError::parse_error("Failed to decode account address from hex string".to_string()) // RpcError::parse_error("Failed to decode account address from hex string".to_string())
})?; // })?;
//
let acc_addr: [u8; 32] = acc_addr_hex_dec.try_into().map_err(|_| { // let acc_addr: [u8; 32] = acc_addr_hex_dec.try_into().map_err(|_| {
RpcError::parse_error("Failed to parse account address from bytes".to_string()) // RpcError::parse_error("Failed to parse account address from bytes".to_string())
})?; // })?;
//
{ // {
let mut cover_guard = self.node_chain_store.lock().await; // let mut cover_guard = self.node_chain_store.lock().await;
//
cover_guard // cover_guard
.operate_account_deposit_public(acc_addr, req.amount as u128) // .operate_account_deposit_public(acc_addr, req.amount as u128)
.await // .await
.map_err(cast_common_execution_error_into_rpc_error)?; // .map_err(cast_common_execution_error_into_rpc_error)?;
}; // };
//
let helperstruct = WriteDepositPublicBalanceResponse { // let helperstruct = WriteDepositPublicBalanceResponse {
status: SUCCESS.to_string(), // status: SUCCESS.to_string(),
}; // };
//
respond(helperstruct) // respond(helperstruct)
} // }
pub async fn process_write_mint_utxo(&self, request: Request) -> Result<Value, RpcErr> { pub async fn process_write_mint_utxo(&self, request: Request) -> Result<Value, RpcErr> {
let req = WriteMintPrivateUTXORequest::parse(Some(request.params))?; let req = WriteMintPrivateUTXORequest::parse(Some(request.params))?;
@ -777,9 +777,9 @@ impl JsonHandler {
SHOW_ACCOUNT_PUBLIC_BALANCE => self.process_show_account_public_balance(request).await, SHOW_ACCOUNT_PUBLIC_BALANCE => self.process_show_account_public_balance(request).await,
SHOW_ACCOUNT_UTXO => self.process_show_account_utxo_request(request).await, SHOW_ACCOUNT_UTXO => self.process_show_account_utxo_request(request).await,
SHOW_TRANSACTION => self.process_show_transaction(request).await, SHOW_TRANSACTION => self.process_show_transaction(request).await,
WRITE_DEPOSIT_PUBLIC_BALANCE => { // WRITE_DEPOSIT_PUBLIC_BALANCE => {
self.process_write_deposit_public_balance(request).await // self.process_write_deposit_public_balance(request).await
} // }
WRITE_MINT_UTXO => self.process_write_mint_utxo(request).await, WRITE_MINT_UTXO => self.process_write_mint_utxo(request).await,
WRITE_MINT_UTXO_MULTIPLE_ASSETS => { WRITE_MINT_UTXO_MULTIPLE_ASSETS => {
self.process_write_mint_utxo_multiple_assets(request).await self.process_write_mint_utxo_multiple_assets(request).await