mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
daod/ money_contract: rename Transaction to FuncCall
This commit is contained in:
@@ -1,30 +1,70 @@
|
||||
// TODO
|
||||
// money-contract/
|
||||
// state.apply()
|
||||
// transfer/
|
||||
// Builder
|
||||
// Partial *
|
||||
// FuncCall
|
||||
use std::io;
|
||||
|
||||
use log::error;
|
||||
use pasta_curves::group::Group;
|
||||
|
||||
use darkfi::{
|
||||
crypto::{
|
||||
burn_proof::verify_burn_proof,
|
||||
keypair::PublicKey,
|
||||
mint_proof::verify_mint_proof,
|
||||
note::EncryptedNote,
|
||||
proof::VerifyingKey,
|
||||
schnorr,
|
||||
schnorr::SchnorrPublic,
|
||||
types::{DrkTokenId, DrkValueBlind, DrkValueCommit},
|
||||
util::{pedersen_commitment_base, pedersen_commitment_u64},
|
||||
BurnRevealedValues, MintRevealedValues, Proof,
|
||||
},
|
||||
util::serial::{Encodable, SerialDecodable, SerialEncodable, VarInt},
|
||||
Result, VerifyFailed, VerifyResult,
|
||||
};
|
||||
|
||||
pub mod transfer;
|
||||
|
||||
/*
|
||||
money-contract/
|
||||
state.apply()
|
||||
transfer/
|
||||
Builder
|
||||
Partial *
|
||||
FuncCall
|
||||
|
||||
/////////////////////////////////////////////////
|
||||
|
||||
let token_id = pallas::Base::random(&mut OsRng);
|
||||
|
||||
let builder = TransactionBuilder {
|
||||
clear_inputs: vec![TransactionBuilderClearInputInfo {
|
||||
value: 110,
|
||||
token_id,
|
||||
signature_secret: cashier_signature_secret,
|
||||
}],
|
||||
inputs: vec![],
|
||||
outputs: vec![TransactionBuilderOutputInfo {
|
||||
value: 110,
|
||||
token_id,
|
||||
public: keypair.public,
|
||||
}],
|
||||
};
|
||||
|
||||
let start = Instant::now();
|
||||
let mint_pk = ProvingKey::build(11, &MintContract::default());
|
||||
debug!("Mint PK: [{:?}]", start.elapsed());
|
||||
let start = Instant::now();
|
||||
let burn_pk = ProvingKey::build(11, &BurnContract::default());
|
||||
debug!("Burn PK: [{:?}]", start.elapsed());
|
||||
let tx = builder.build(&mint_pk, &burn_pk)?;
|
||||
|
||||
tx.verify(&money_state.mint_vk, &money_state.burn_vk)?;
|
||||
|
||||
let _note = tx.outputs[0].enc_note.decrypt(&keypair.secret)?;
|
||||
|
||||
let update = state_transition(&money_state, tx)?;
|
||||
money_state.apply(update);
|
||||
|
||||
// Now spend
|
||||
let owncoin = &money_state.own_coins[0];
|
||||
let note = &owncoin.note;
|
||||
let leaf_position = owncoin.leaf_position;
|
||||
let root = money_state.tree.root(0).unwrap();
|
||||
let merkle_path = money_state.tree.authentication_path(leaf_position, &root).unwrap();
|
||||
|
||||
let builder = TransactionBuilder {
|
||||
clear_inputs: vec![],
|
||||
inputs: vec![TransactionBuilderInputInfo {
|
||||
leaf_position,
|
||||
merkle_path,
|
||||
secret: keypair.secret,
|
||||
note: note.clone(),
|
||||
}],
|
||||
outputs: vec![TransactionBuilderOutputInfo {
|
||||
value: 110,
|
||||
token_id,
|
||||
public: keypair.public,
|
||||
}],
|
||||
};
|
||||
|
||||
let tx = builder.build(&mint_pk, &burn_pk)?;
|
||||
|
||||
let update = state_transition(&money_state, tx)?;
|
||||
money_state.apply(update);
|
||||
*/
|
||||
|
||||
@@ -2,8 +2,8 @@ use pasta_curves::group::ff::Field;
|
||||
use rand::rngs::OsRng;
|
||||
|
||||
use super::{
|
||||
partial::{PartialTransaction, PartialTransactionClearInput, PartialTransactionInput},
|
||||
Transaction, TransactionClearInput, TransactionInput, TransactionOutput,
|
||||
partial::{Partial, PartialClearInput, PartialInput},
|
||||
ClearInput, FuncCall, Input, Output,
|
||||
};
|
||||
|
||||
use darkfi::{
|
||||
@@ -21,34 +21,34 @@ use darkfi::{
|
||||
Result,
|
||||
};
|
||||
|
||||
pub struct TransactionBuilder {
|
||||
pub clear_inputs: Vec<TransactionBuilderClearInputInfo>,
|
||||
pub inputs: Vec<TransactionBuilderInputInfo>,
|
||||
pub outputs: Vec<TransactionBuilderOutputInfo>,
|
||||
pub struct Builder {
|
||||
pub clear_inputs: Vec<BuilderClearInputInfo>,
|
||||
pub inputs: Vec<BuilderInputInfo>,
|
||||
pub outputs: Vec<BuilderOutputInfo>,
|
||||
}
|
||||
|
||||
pub struct TransactionBuilderClearInputInfo {
|
||||
pub struct BuilderClearInputInfo {
|
||||
pub value: u64,
|
||||
pub token_id: DrkTokenId,
|
||||
pub signature_secret: SecretKey,
|
||||
}
|
||||
|
||||
pub struct TransactionBuilderInputInfo {
|
||||
pub struct BuilderInputInfo {
|
||||
pub leaf_position: incrementalmerkletree::Position,
|
||||
pub merkle_path: Vec<MerkleNode>,
|
||||
pub secret: SecretKey,
|
||||
pub note: Note,
|
||||
}
|
||||
|
||||
pub struct TransactionBuilderOutputInfo {
|
||||
pub struct BuilderOutputInfo {
|
||||
pub value: u64,
|
||||
pub token_id: DrkTokenId,
|
||||
pub public: PublicKey,
|
||||
}
|
||||
|
||||
impl TransactionBuilder {
|
||||
impl Builder {
|
||||
fn compute_remainder_blind(
|
||||
clear_inputs: &[PartialTransactionClearInput],
|
||||
clear_inputs: &[PartialClearInput],
|
||||
input_blinds: &[DrkValueBlind],
|
||||
output_blinds: &[DrkValueBlind],
|
||||
) -> DrkValueBlind {
|
||||
@@ -69,7 +69,7 @@ impl TransactionBuilder {
|
||||
total
|
||||
}
|
||||
|
||||
pub fn build(self, mint_pk: &ProvingKey, burn_pk: &ProvingKey) -> Result<Transaction> {
|
||||
pub fn build(self, mint_pk: &ProvingKey, burn_pk: &ProvingKey) -> Result<FuncCall> {
|
||||
assert!(self.clear_inputs.len() + self.inputs.len() > 0);
|
||||
|
||||
let mut clear_inputs = vec![];
|
||||
@@ -78,7 +78,7 @@ impl TransactionBuilder {
|
||||
let signature_public = PublicKey::from_secret(input.signature_secret);
|
||||
let value_blind = DrkValueBlind::random(&mut OsRng);
|
||||
|
||||
let clear_input = PartialTransactionClearInput {
|
||||
let clear_input = PartialClearInput {
|
||||
value: input.value,
|
||||
token_id: input.token_id,
|
||||
value_blind,
|
||||
@@ -114,7 +114,7 @@ impl TransactionBuilder {
|
||||
// First we make the tx then sign after
|
||||
signature_secrets.push(signature_secret);
|
||||
|
||||
let input = PartialTransactionInput { burn_proof: proof, revealed };
|
||||
let input = PartialInput { burn_proof: proof, revealed };
|
||||
inputs.push(input);
|
||||
}
|
||||
|
||||
@@ -158,11 +158,11 @@ impl TransactionBuilder {
|
||||
|
||||
let encrypted_note = note.encrypt(&output.public)?;
|
||||
|
||||
let output = TransactionOutput { mint_proof, revealed, enc_note: encrypted_note };
|
||||
let output = Output { mint_proof, revealed, enc_note: encrypted_note };
|
||||
outputs.push(output);
|
||||
}
|
||||
|
||||
let partial_tx = PartialTransaction { clear_inputs, inputs, outputs };
|
||||
let partial_tx = Partial { clear_inputs, inputs, outputs };
|
||||
|
||||
let mut unsigned_tx_data = vec![];
|
||||
partial_tx.encode(&mut unsigned_tx_data)?;
|
||||
@@ -171,7 +171,7 @@ impl TransactionBuilder {
|
||||
for (input, info) in partial_tx.clear_inputs.into_iter().zip(self.clear_inputs) {
|
||||
let secret = info.signature_secret;
|
||||
let signature = secret.sign(&unsigned_tx_data[..]);
|
||||
let input = TransactionClearInput::from_partial(input, signature);
|
||||
let input = ClearInput::from_partial(input, signature);
|
||||
clear_inputs.push(input);
|
||||
}
|
||||
|
||||
@@ -180,10 +180,10 @@ impl TransactionBuilder {
|
||||
partial_tx.inputs.into_iter().zip(signature_secrets.into_iter())
|
||||
{
|
||||
let signature = signature_secret.sign(&unsigned_tx_data[..]);
|
||||
let input = TransactionInput::from_partial(input, signature);
|
||||
let input = Input::from_partial(input, signature);
|
||||
inputs.push(input);
|
||||
}
|
||||
|
||||
Ok(Transaction { clear_inputs, inputs, outputs: partial_tx.outputs })
|
||||
Ok(FuncCall { clear_inputs, inputs, outputs: partial_tx.outputs })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,18 +25,18 @@ pub mod partial;
|
||||
|
||||
/// A DarkFi transaction
|
||||
#[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
|
||||
pub struct Transaction {
|
||||
pub struct FuncCall {
|
||||
/// Clear inputs
|
||||
pub clear_inputs: Vec<TransactionClearInput>,
|
||||
pub clear_inputs: Vec<ClearInput>,
|
||||
/// Anonymous inputs
|
||||
pub inputs: Vec<TransactionInput>,
|
||||
pub inputs: Vec<Input>,
|
||||
/// Anonymous outputs
|
||||
pub outputs: Vec<TransactionOutput>,
|
||||
pub outputs: Vec<Output>,
|
||||
}
|
||||
|
||||
/// A transaction's clear input
|
||||
#[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
|
||||
pub struct TransactionClearInput {
|
||||
pub struct ClearInput {
|
||||
/// Input's value (amount)
|
||||
pub value: u64,
|
||||
/// Input's token ID
|
||||
@@ -47,13 +47,13 @@ pub struct TransactionClearInput {
|
||||
pub token_blind: DrkValueBlind,
|
||||
/// Public key for the signature
|
||||
pub signature_public: PublicKey,
|
||||
/// Transaction signature
|
||||
/// signature
|
||||
pub signature: schnorr::Signature,
|
||||
}
|
||||
|
||||
/// A transaction's anonymous input
|
||||
#[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
|
||||
pub struct TransactionInput {
|
||||
pub struct Input {
|
||||
/// Zero-knowledge proof for the input
|
||||
pub burn_proof: Proof,
|
||||
/// Public inputs for the zero-knowledge proof
|
||||
@@ -64,7 +64,7 @@ pub struct TransactionInput {
|
||||
|
||||
/// A transaction's anonymous output
|
||||
#[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
|
||||
pub struct TransactionOutput {
|
||||
pub struct Output {
|
||||
/// Zero-knowledge proof for the output
|
||||
pub mint_proof: Proof,
|
||||
/// Public inputs for the zero-knowledge proof
|
||||
@@ -73,10 +73,10 @@ pub struct TransactionOutput {
|
||||
pub enc_note: EncryptedNote,
|
||||
}
|
||||
|
||||
impl Transaction {
|
||||
impl FuncCall {
|
||||
/// Verify the transaction
|
||||
pub fn verify(&self, mint_vk: &VerifyingKey, burn_vk: &VerifyingKey) -> VerifyResult<()> {
|
||||
// Transaction must have minimum 1 clear or anon input, and 1 output
|
||||
// must have minimum 1 clear or anon input, and 1 output
|
||||
if self.clear_inputs.len() + self.inputs.len() == 0 {
|
||||
error!("tx::verify(): Missing inputs");
|
||||
return Err(VerifyFailed::LackingInputs)
|
||||
@@ -178,11 +178,8 @@ impl Transaction {
|
||||
}
|
||||
}
|
||||
|
||||
impl TransactionClearInput {
|
||||
fn from_partial(
|
||||
partial: partial::PartialTransactionClearInput,
|
||||
signature: schnorr::Signature,
|
||||
) -> Self {
|
||||
impl ClearInput {
|
||||
fn from_partial(partial: partial::PartialClearInput, signature: schnorr::Signature) -> Self {
|
||||
Self {
|
||||
value: partial.value,
|
||||
token_id: partial.token_id,
|
||||
@@ -204,11 +201,8 @@ impl TransactionClearInput {
|
||||
}
|
||||
}
|
||||
|
||||
impl TransactionInput {
|
||||
pub fn from_partial(
|
||||
partial: partial::PartialTransactionInput,
|
||||
signature: schnorr::Signature,
|
||||
) -> Self {
|
||||
impl Input {
|
||||
pub fn from_partial(partial: partial::PartialInput, signature: schnorr::Signature) -> Self {
|
||||
Self { burn_proof: partial.burn_proof, revealed: partial.revealed, signature }
|
||||
}
|
||||
|
||||
@@ -239,5 +233,5 @@ macro_rules! impl_vec_without_signature {
|
||||
}
|
||||
};
|
||||
}
|
||||
impl_vec_without_signature!(TransactionClearInput);
|
||||
impl_vec_without_signature!(TransactionInput);
|
||||
impl_vec_without_signature!(ClearInput);
|
||||
impl_vec_without_signature!(Input);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::TransactionOutput;
|
||||
use super::Output;
|
||||
use darkfi::{
|
||||
crypto::{
|
||||
keypair::PublicKey,
|
||||
@@ -9,14 +9,14 @@ use darkfi::{
|
||||
};
|
||||
|
||||
#[derive(Clone, SerialEncodable, SerialDecodable)]
|
||||
pub struct PartialTransaction {
|
||||
pub clear_inputs: Vec<PartialTransactionClearInput>,
|
||||
pub inputs: Vec<PartialTransactionInput>,
|
||||
pub outputs: Vec<TransactionOutput>,
|
||||
pub struct Partial {
|
||||
pub clear_inputs: Vec<PartialClearInput>,
|
||||
pub inputs: Vec<PartialInput>,
|
||||
pub outputs: Vec<Output>,
|
||||
}
|
||||
|
||||
#[derive(Clone, SerialEncodable, SerialDecodable)]
|
||||
pub struct PartialTransactionClearInput {
|
||||
pub struct PartialClearInput {
|
||||
pub value: u64,
|
||||
pub token_id: DrkTokenId,
|
||||
pub value_blind: DrkValueBlind,
|
||||
@@ -25,7 +25,7 @@ pub struct PartialTransactionClearInput {
|
||||
}
|
||||
|
||||
#[derive(Clone, SerialEncodable, SerialDecodable)]
|
||||
pub struct PartialTransactionInput {
|
||||
pub struct PartialInput {
|
||||
pub burn_proof: Proof,
|
||||
pub revealed: BurnRevealedValues,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user