mod.rs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi_sdk::{
  19. crypto::{
  20. schnorr::{SchnorrPublic, Signature},
  21. PublicKey,
  22. },
  23. pasta::pallas,
  24. tx::ContractCall,
  25. };
  26. use darkfi_serial::{Encodable, SerialDecodable, SerialEncodable};
  27. use log::{debug, error};
  28. use crate::{crypto::Proof, Error, Result};
  29. /// A Transaction contains an arbitrary number of `ContractCall` objects,
  30. /// along with corresponding ZK proofs and Schnorr signatures.
  31. #[derive(Debug, Clone, Eq, PartialEq, SerialEncodable, SerialDecodable)]
  32. pub struct Transaction {
  33. /// Calls executed in this transaction
  34. pub calls: Vec<ContractCall>,
  35. /// Attached ZK proofs
  36. pub proofs: Vec<Vec<Proof>>,
  37. /// Attached Schnorr signatures
  38. pub signatures: Vec<Vec<Signature>>,
  39. }
  40. impl Transaction {
  41. /// Verify ZK proofs for the entire transaction.
  42. pub fn verify_zkps(&self, zkp_table: Vec<Vec<(String, Vec<pallas::Base>)>>) -> Result<()> {
  43. Ok(())
  44. }
  45. /// Verify Schnorr signatures for the entire transaction.
  46. pub fn verify_sigs(&self, pub_table: Vec<Vec<PublicKey>>) -> Result<()> {
  47. let tx_data = self.encode_without_sigs()?;
  48. let data_hash = blake3::hash(&tx_data);
  49. assert!(pub_table.len() == self.signatures.len());
  50. for (i, (sigs, pubkeys)) in self.signatures.iter().zip(pub_table.iter()).enumerate() {
  51. for (pubkey, signature) in pubkeys.iter().zip(sigs) {
  52. if !pubkey.verify(&data_hash.as_bytes()[..], &signature) {
  53. error!("tx::verify_sigs[{}] failed to verify", i);
  54. return Err(Error::InvalidSignature)
  55. }
  56. }
  57. debug!("tx::verify_sigs[{}] passed", i);
  58. }
  59. Ok(())
  60. }
  61. /// Encode the object into a byte vector for signing
  62. pub fn encode_without_sigs(&self) -> Result<Vec<u8>> {
  63. let mut buf = vec![];
  64. self.calls.encode(&mut buf)?;
  65. self.proofs.encode(&mut buf)?;
  66. Ok(buf)
  67. }
  68. }