Skip to content

Commit

Permalink
execution-core: Hide transaction payloads from api
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed Aug 6, 2024
1 parent d6a187f commit f8d6da6
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 199 deletions.
1 change: 0 additions & 1 deletion execution-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ transfer::{
};
phoenix::{
Fee;
Payload;
Transaction;
TreeLeaf;
NOTES_TREE_DEPTH;
Expand Down
6 changes: 5 additions & 1 deletion execution-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dusk-poseidon = "0.39"
bls12_381-bls = { version = "0.4", default-features = false, features = ["rkyv-impl"] }
jubjub-schnorr = { version = "0.4", default-features = false, features = ["rkyv-impl"] }
phoenix-core = { version = "0.30.0-rc", default-features = false, features = ["rkyv-impl", "alloc"] }
poseidon-merkle = { version = "0.6", features = ["rkyv-impl"] }
piecrust-uplink = { version = "0.16" }
dusk-bytes = "0.1"
rkyv = { version = "0.7", default-features = false, features = ["size_32"] }
Expand All @@ -29,7 +30,10 @@ rand = "0.8"
parallel = ["bls12_381-bls/parallel"]

# It enables zk-capabilities
zk = ["dusk-plonk", "phoenix-circuits"]
zk = [
"dusk-plonk",
"phoenix-circuits",
]

# Enables std feature for dusk-plonk
std = ["dusk-plonk/std"]
4 changes: 2 additions & 2 deletions execution-core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ help: ## Display this help screen
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'

test:
cargo test --release
cargo test --release --features zk

clippy: ## Run clippy
@cargo clippy --release -- -D warnings
@cargo clippy --no-default-features --features=host --release -- -D warnings
@cargo clippy --no-default-features --release -- -D warnings

.PHONY: all help test
2 changes: 1 addition & 1 deletion execution-core/src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Transaction {
#[must_use]
pub fn root(&self) -> Option<&BlsScalar> {
match self {
Self::Phoenix(tx) => Some(&tx.root()),
Self::Phoenix(tx) => Some(tx.root()),
Self::Moonlight(_) => None,
}
}
Expand Down
85 changes: 53 additions & 32 deletions execution-core/src/transfer/moonlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use dusk_bytes::{DeserializableSlice, Error as BytesError, Serializable};
use rkyv::{Archive, Deserialize, Serialize};

use crate::{
signatures::bls::{PublicKey as BlsPublicKey, Signature as BlsSignature},
signatures::bls::{
PublicKey as AccountPublicKey, SecretKey as AccountSecretKey,
Signature as AccountSignature,
},
transfer::contract_exec::{
ContractBytecode, ContractCall, ContractDeploy, ContractExec,
},
Expand All @@ -36,32 +39,56 @@ pub struct AccountData {
#[archive_attr(derive(CheckBytes))]
pub struct Transaction {
payload: Payload,
signature: BlsSignature,
signature: AccountSignature,
}

impl Transaction {
/// Create a new transaction.
#[must_use]
pub fn new(payload: Payload, signature: BlsSignature) -> Self {
#[allow(clippy::too_many_arguments)]
pub fn new(
from_sk: &AccountSecretKey,
to_account: Option<AccountPublicKey>,
value: u64,
deposit: u64,
gas_limit: u64,
gas_price: u64,
nonce: u64,
exec: Option<impl Into<ContractExec>>,
) -> Self {
let payload = Payload {
from_account: AccountPublicKey::from(from_sk),
to_account,
value,
deposit,
gas_limit,
gas_price,
nonce,
exec: exec.map(Into::into),
};

let digest = payload.signature_message();
let signature = from_sk.sign(&digest);

Self { payload, signature }
}

/// The proof of the transaction.
#[must_use]
pub fn signature(&self) -> &BlsSignature {
pub fn signature(&self) -> &AccountSignature {
&self.signature
}

/// Return the sender of the transaction.
#[must_use]
pub fn from_account(&self) -> &BlsPublicKey {
&self.payload.from
pub fn from_account(&self) -> &AccountPublicKey {
&self.payload.from_account
}

/// Return the receiver of the transaction, if it exists.
#[must_use]
pub fn to_account(&self) -> Option<&BlsPublicKey> {
self.payload.to.as_ref()
pub fn to_account(&self) -> Option<&AccountPublicKey> {
self.payload.to_account.as_ref()
}

/// Return the value transferred in the transaction.
Expand Down Expand Up @@ -121,7 +148,7 @@ impl Transaction {
}

/// Creates a modified clone of this transaction if it contains data for
/// deployment, clones all fields except for the bytecode' 'bytes' part.
/// deployment, clones all fields except for the bytecode 'bytes' part.
/// Returns none if the transaction is not a deployment transaction.
#[must_use]
pub fn strip_off_bytecode(&self) -> Option<Self> {
Expand Down Expand Up @@ -175,7 +202,7 @@ impl Transaction {
let payload = Payload::from_slice(payload_buf)?;
buf = new_buf;

let signature = BlsSignature::from_bytes(
let signature = AccountSignature::from_bytes(
buf.try_into().map_err(|_| BytesError::InvalidData)?,
)
.map_err(|_| BytesError::InvalidData)?;
Expand All @@ -189,7 +216,7 @@ impl Transaction {
/// for hashing and *cannot* be used to deserialize the transaction again.
#[must_use]
pub fn to_hash_input_bytes(&self) -> Vec<u8> {
let mut bytes = self.payload.to_hash_input_bytes();
let mut bytes = self.payload.signature_message();
bytes.extend(self.signature.to_bytes());
bytes
}
Expand All @@ -198,10 +225,10 @@ impl Transaction {
/// transaction a valid one.
#[must_use]
pub fn signature_message(&self) -> Vec<u8> {
self.payload.to_hash_input_bytes()
self.payload.signature_message()
}

/// Create the payload hash.
/// Create the transaction hash.
#[must_use]
pub fn hash(&self) -> BlsScalar {
BlsScalar::hash_to_scalar(&self.to_hash_input_bytes())
Expand All @@ -211,11 +238,11 @@ impl Transaction {
/// The payload for a moonlight transaction.
#[derive(Debug, Clone, PartialEq, Eq, Archive, Serialize, Deserialize)]
#[archive_attr(derive(CheckBytes))]
pub struct Payload {
struct Payload {
/// Key of the sender of this transaction.
pub from: BlsPublicKey,
pub from_account: AccountPublicKey,
/// Key of the receiver of the funds.
pub to: Option<BlsPublicKey>,
pub to_account: Option<AccountPublicKey>,
/// Value to be transferred.
pub value: u64,
/// Deposit for a contract.
Expand All @@ -240,10 +267,10 @@ impl Payload {
pub fn to_var_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();

bytes.extend(self.from.to_bytes());
bytes.extend(self.from_account.to_bytes());

// serialize the recipient
match self.to {
match self.to_account {
Some(to) => {
bytes.push(1);
bytes.extend(to.to_bytes());
Expand Down Expand Up @@ -282,12 +309,12 @@ impl Payload {
pub fn from_slice(buf: &[u8]) -> Result<Self, BytesError> {
let mut buf = buf;

let from = BlsPublicKey::from_reader(&mut buf)?;
let from_account = AccountPublicKey::from_reader(&mut buf)?;

// deserialize recipient
let to = match u8::from_reader(&mut buf)? {
let to_account = match u8::from_reader(&mut buf)? {
0 => None,
1 => Some(BlsPublicKey::from_reader(&mut buf)?),
1 => Some(AccountPublicKey::from_reader(&mut buf)?),
_ => {
return Err(BytesError::InvalidData);
}
Expand All @@ -310,8 +337,8 @@ impl Payload {
};

Ok(Self {
from,
to,
from_account,
to_account,
value,
deposit,
gas_limit,
Expand All @@ -326,11 +353,11 @@ impl Payload {
/// Note: The result of this function is *only* meant to be used as an input
/// for hashing and *cannot* be used to deserialize the payload again.
#[must_use]
pub fn to_hash_input_bytes(&self) -> Vec<u8> {
pub fn signature_message(&self) -> Vec<u8> {
let mut bytes = Vec::new();

bytes.extend(self.from.to_bytes());
if let Some(to) = &self.to {
bytes.extend(self.from_account.to_bytes());
if let Some(to) = &self.to_account {
bytes.extend(to.to_bytes());
}
bytes.extend(self.value.to_bytes());
Expand All @@ -357,10 +384,4 @@ impl Payload {

bytes
}

/// Create the payload hash.
#[must_use]
pub fn hash(&self) -> BlsScalar {
BlsScalar::hash_to_scalar(&self.to_hash_input_bytes())
}
}
Loading

0 comments on commit f8d6da6

Please sign in to comment.