use vapory_types::{H256, U256, Address, BigEndianHash};
use bytes::Bytes;
use hash::keccak;
use tetsy_rlp::Encodable;
use crypto::publickey::Signature;
use types::transaction::signature::{add_chain_replay_protection, check_replay_protection};
#[derive(Default, Debug, Clone, PartialEq, RlpEncodable, RlpDecodable, Eq)]
pub struct PrivateTransaction {
encrypted: Bytes,
contract: Address,
hash: H256,
}
impl PrivateTransaction {
pub fn new(encrypted: Bytes, contract: Address) -> Self {
PrivateTransaction {
encrypted,
contract,
hash: H256::zero(),
}.compute_hash()
}
fn compute_hash(mut self) -> PrivateTransaction {
self.hash = keccak(&*self.rlp_bytes());
self
}
pub fn hash(&self) -> H256 {
self.hash
}
pub fn contract(&self) -> Address {
self.contract
}
pub fn encrypted(&self) -> Bytes {
self.encrypted.clone()
}
}
#[derive(Default, Debug, Clone, PartialEq, RlpEncodable, RlpDecodable, Eq)]
pub struct SignedPrivateTransaction {
private_transaction_hash: H256,
v: u64,
r: U256,
s: U256,
hash: H256,
}
impl SignedPrivateTransaction {
pub fn new(private_transaction_hash: H256, sig: Signature, chain_id: Option<u64>) -> Self {
SignedPrivateTransaction {
private_transaction_hash: private_transaction_hash,
r: sig.r().into(),
s: sig.s().into(),
v: add_chain_replay_protection(sig.v() as u64, chain_id),
hash: H256::zero(),
}.compute_hash()
}
fn compute_hash(mut self) -> SignedPrivateTransaction {
self.hash = keccak(&*self.rlp_bytes());
self
}
pub fn standard_v(&self) -> u8 { check_replay_protection(self.v) }
pub fn signature(&self) -> Signature {
Signature::from_rsv(
&BigEndianHash::from_uint(&self.r),
&BigEndianHash::from_uint(&self.s),
self.standard_v(),
)
}
pub fn private_transaction_hash(&self) -> H256 {
self.private_transaction_hash
}
pub fn hash(&self) -> H256 {
self.hash
}
}