support add/read multiple arbitrary tx data (#189)

* support add/read multiple arbitrary tx  data

* fix clippy errors

* resolve pr issues
This commit is contained in:
akildemir
2022-12-09 18:58:11 +03:00
committed by GitHub
parent 9e82416e7d
commit d5a5704ba4
6 changed files with 128 additions and 34 deletions

View File

@@ -4,7 +4,10 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
use crate::{
Protocol,
wallet::{address::MoneroAddress, Fee, SpendableOutput, SignableTransaction, TransactionError},
wallet::{
address::MoneroAddress, Fee, SpendableOutput, SignableTransaction, TransactionError,
extra::MAX_TX_EXTRA_NONCE_SIZE,
},
};
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)]
@@ -15,14 +18,14 @@ struct SignableTransactionBuilderInternal {
inputs: Vec<SpendableOutput>,
payments: Vec<(MoneroAddress, u64)>,
change_address: Option<MoneroAddress>,
data: Option<Vec<u8>>,
data: Vec<Vec<u8>>,
}
impl SignableTransactionBuilderInternal {
// Takes in the change address so users don't miss that they have to manually set one
// If they don't, all leftover funds will become part of the fee
fn new(protocol: Protocol, fee: Fee, change_address: Option<MoneroAddress>) -> Self {
Self { protocol, fee, inputs: vec![], payments: vec![], change_address, data: None }
Self { protocol, fee, inputs: vec![], payments: vec![], change_address, data: vec![] }
}
fn add_input(&mut self, input: SpendableOutput) {
@@ -39,8 +42,8 @@ impl SignableTransactionBuilderInternal {
self.payments.extend(payments);
}
fn set_data(&mut self, data: Vec<u8>) {
self.data = Some(data);
fn add_data(&mut self, data: Vec<u8>) {
self.data.push(data);
}
}
@@ -100,9 +103,12 @@ impl SignableTransactionBuilder {
self.shallow_copy()
}
pub fn set_data(&mut self, data: Vec<u8>) -> Self {
self.0.write().unwrap().set_data(data);
self.shallow_copy()
pub fn add_data(&mut self, data: Vec<u8>) -> Result<Self, TransactionError> {
if data.len() > MAX_TX_EXTRA_NONCE_SIZE {
Err(TransactionError::TooMuchData)?;
}
self.0.write().unwrap().add_data(data);
Ok(self.shallow_copy())
}
pub fn build(self) -> Result<SignableTransaction, TransactionError> {