diff --git a/controller/Cargo.toml b/controller/Cargo.toml index 6a3a5ca..e02f5bb 100644 --- a/controller/Cargo.toml +++ b/controller/Cargo.toml @@ -11,6 +11,7 @@ exclude = ["**/*.grin", "**/*.grin2"] edition = "2021" [dependencies] +ed25519-dalek = "1.0.0-pre.4" futures = "0.3" hyper = "0.14" rand = "0.7" diff --git a/controller/src/command.rs b/controller/src/command.rs index f7e5979..f541970 100644 --- a/controller/src/command.rs +++ b/controller/src/command.rs @@ -24,8 +24,9 @@ use crate::keychain; use crate::libwallet::api_impl::owner; use crate::libwallet::contract::can_finalize; use crate::libwallet::contract::types::{ - ContractNewArgsAPI, ContractRevokeArgsAPI, ContractSetupArgsAPI, OutputSelectionArgs, + ContractNewArgsAPI, ContractRevokeArgsAPI, ContractSetupArgsAPI, OutputSelectionArgs, ProofArgs, }; +use ed25519_dalek::PublicKey as DalekPublicKey; use crate::libwallet::{ self, InitTxArgs, IssueInvoiceTxArgs, NodeClient, PaymentProof, Slate, SlateState, Slatepack, SlatepackAddress, Slatepacker, SlatepackerArgs, WalletLCProvider, @@ -1646,6 +1647,8 @@ pub struct ContractNewArgs { pub outfile: Option, /// Select outputs early pub add_outputs: bool, + /// Sender address for a requested payment proof (set when --payment-proof is passed) + pub payment_proof_sender: Option, } impl ContractNewArgs { @@ -1663,6 +1666,11 @@ impl ContractNewArgs { // Create a ContractNewArgsAPI from the ContractNewArgs fn to_api_args(&self) -> ContractNewArgsAPI { let net_change = self.get_net_change(); + let mut proof_args = ProofArgs::default(); + if let Some(sender) = self.payment_proof_sender { + proof_args.suppress_proof = false; + proof_args.sender_address = Some(sender); + } ContractNewArgsAPI { setup_args: ContractSetupArgsAPI { src_acct_name: match self.src_acct_name.as_ref() { @@ -1683,7 +1691,7 @@ impl ContractNewArgs { }, ..Default::default() }, - proof_args: Default::default(), + proof_args, }, ..Default::default() } @@ -1745,6 +1753,8 @@ pub struct ContractSetupArgs { pub outfile: Option, /// Add outputs pub add_outputs: bool, // lock outputs early + /// Sender address for a requested payment proof (set when --payment-proof is passed) + pub payment_proof_sender: Option, } impl ContractSetupArgs { @@ -1766,11 +1776,17 @@ impl ContractSetupArgs { // Create a ContractSetupArgsAPI from the ContractSetupArgs fn to_api_args(&self) -> ContractSetupArgsAPI { let net_change = self.get_net_change(); + let mut proof_args = ProofArgs::default(); + if let Some(sender) = self.payment_proof_sender { + proof_args.suppress_proof = false; + proof_args.sender_address = Some(sender); + } ContractSetupArgsAPI { // TODO: num_participants is derived here. It should be an Option and read from the slate. // Need to check no attack are possible regarding kernel fee contribution. net_change: net_change, add_outputs: self.add_outputs, + proof_args, selection_args: OutputSelectionArgs { use_inputs: match self.use_inputs.as_ref() { Some(v) => Some(v.to_string()), diff --git a/src/bin/grin-wallet.yml b/src/bin/grin-wallet.yml index bc0b316..cfc53c8 100644 --- a/src/bin/grin-wallet.yml +++ b/src/bin/grin-wallet.yml @@ -497,6 +497,10 @@ subcommands: short: o long: make-outputs takes_value: true + - payment-proof: + help: Request a payment proof (receiver signs a receipt). Requires --encrypt-for. + long: payment-proof + takes_value: false # - setup: # about: Perform a key setup on a contract # args: @@ -578,6 +582,10 @@ subcommands: short: o long: make-outputs takes_value: true + - payment-proof: + help: Request a payment proof (receiver signs a receipt). Requires --encrypt-for. + long: payment-proof + takes_value: false - no-broadcast: help: Don't broadcast the transaction if it was finalized long: no-broadcast diff --git a/src/cmd/wallet_args.rs b/src/cmd/wallet_args.rs index a1f50c1..125e778 100644 --- a/src/cmd/wallet_args.rs +++ b/src/cmd/wallet_args.rs @@ -1044,6 +1044,23 @@ pub fn parse_contract_new_args( None => 2, }; + // A payment proof commits to the sender's address, so it requires --encrypt-for + // (the counterparty) to derive that address from. + let payment_proof_sender = if args.is_present("payment-proof") { + let addr_str = counterparty_addr.as_deref().ok_or_else(|| { + ParseError::ArgumentError("--payment-proof requires --encrypt-for".into()) + })?; + let addr = SlatepackAddress::try_from(addr_str).map_err(|e| { + ParseError::ArgumentError(format!( + "Invalid --encrypt-for address for payment proof: {}", + e + )) + })?; + Some(addr.pub_key) + } else { + None + }; + Ok(command::ContractNewArgs { counterparty_addr: counterparty_addr, receive: receive, @@ -1054,6 +1071,7 @@ pub fn parse_contract_new_args( add_outputs: add_outputs, use_inputs: use_inputs, make_outputs: make_outputs, + payment_proof_sender: payment_proof_sender, // TODO: Future features below fee_rate: None, outfile: None, @@ -1118,6 +1136,23 @@ pub fn parse_contract_setup_args( // TODO: should we catch if the person calls "--receive=5" when it should be "--send=5"? // Perhaps we could detect this from the slate state e.g. S1 -> receive, I1 -> send? + // A payment proof commits to the sender's address; require --encrypt-for so we can + // derive it (when signing, the counterparty is the sender). + let payment_proof_sender = if args.is_present("payment-proof") { + let addr_str = counterparty_addr.as_ref().ok_or_else(|| { + ParseError::ArgumentError("--payment-proof requires --encrypt-for".into()) + })?; + let addr = SlatepackAddress::try_from(addr_str.as_str()).map_err(|e| { + ParseError::ArgumentError(format!( + "Invalid --encrypt-for address for payment proof: {}", + e + )) + })?; + Some(addr.pub_key) + } else { + None + }; + Ok(command::ContractSetupArgs { counterparty_addr: counterparty_addr, receive: receive, @@ -1126,6 +1161,7 @@ pub fn parse_contract_setup_args( add_outputs: false, use_inputs: use_inputs, make_outputs: make_outputs, + payment_proof_sender: payment_proof_sender, // TODO: Future features below fee_rate: None, outfile: None,