2022-07-09 22:53:52 +00:00
|
|
|
use core::cmp::Ordering;
|
|
|
|
|
2022-05-21 19:33:35 +00:00
|
|
|
use curve25519_dalek::edwards::EdwardsPoint;
|
|
|
|
|
2022-07-15 05:26:07 +00:00
|
|
|
use crate::{
|
|
|
|
hash,
|
|
|
|
serialize::*,
|
|
|
|
ringct::{RctPrunable, RctSignatures},
|
|
|
|
};
|
2022-05-21 19:33:35 +00:00
|
|
|
|
2022-07-22 06:34:36 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2022-05-21 19:33:35 +00:00
|
|
|
pub enum Input {
|
|
|
|
Gen(u64),
|
|
|
|
|
2022-07-15 05:26:07 +00:00
|
|
|
ToKey { amount: u64, key_offsets: Vec<u64>, key_image: EdwardsPoint },
|
2022-05-21 19:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Input {
|
2022-06-19 16:03:01 +00:00
|
|
|
// Worst-case predictive len
|
2022-07-27 09:05:43 +00:00
|
|
|
pub(crate) fn fee_weight(ring_len: usize) -> usize {
|
2022-06-19 16:03:01 +00:00
|
|
|
// Uses 1 byte for the VarInt amount due to amount being 0
|
|
|
|
// Uses 1 byte for the VarInt encoding of the length of the ring as well
|
2022-07-27 09:05:43 +00:00
|
|
|
1 + 1 + 1 + (8 * ring_len) + 32
|
2022-06-19 16:03:01 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 19:33:35 +00:00
|
|
|
pub fn serialize<W: std::io::Write>(&self, w: &mut W) -> std::io::Result<()> {
|
|
|
|
match self {
|
|
|
|
Input::Gen(height) => {
|
2022-05-22 01:35:25 +00:00
|
|
|
w.write_all(&[255])?;
|
2022-05-21 19:33:35 +00:00
|
|
|
write_varint(height, w)
|
2022-07-15 05:26:07 +00:00
|
|
|
}
|
2022-05-21 19:33:35 +00:00
|
|
|
|
|
|
|
Input::ToKey { amount, key_offsets, key_image } => {
|
|
|
|
w.write_all(&[2])?;
|
|
|
|
write_varint(amount, w)?;
|
|
|
|
write_vec(write_varint, key_offsets, w)?;
|
|
|
|
write_point(key_image, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-22 00:27:21 +00:00
|
|
|
|
|
|
|
pub fn deserialize<R: std::io::Read>(r: &mut R) -> std::io::Result<Input> {
|
2022-05-22 01:35:25 +00:00
|
|
|
let mut variant = [0];
|
2022-05-22 00:27:21 +00:00
|
|
|
r.read_exact(&mut variant)?;
|
2022-07-15 05:26:07 +00:00
|
|
|
Ok(match variant[0] {
|
|
|
|
255 => Input::Gen(read_varint(r)?),
|
|
|
|
2 => Input::ToKey {
|
|
|
|
amount: read_varint(r)?,
|
|
|
|
key_offsets: read_vec(read_varint, r)?,
|
|
|
|
key_image: read_point(r)?,
|
|
|
|
},
|
|
|
|
_ => Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Tried to deserialize unknown/unused input type",
|
|
|
|
))?,
|
|
|
|
})
|
2022-05-22 00:27:21 +00:00
|
|
|
}
|
2022-05-21 19:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't bother moving to an enum for the unused Script classes
|
2022-07-22 06:34:36 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2022-05-21 19:33:35 +00:00
|
|
|
pub struct Output {
|
|
|
|
pub amount: u64,
|
|
|
|
pub key: EdwardsPoint,
|
2022-07-27 10:29:14 +00:00
|
|
|
pub view_tag: Option<u8>,
|
2022-05-21 19:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Output {
|
2022-06-19 16:03:01 +00:00
|
|
|
pub(crate) fn fee_weight() -> usize {
|
|
|
|
1 + 1 + 32 + 1
|
|
|
|
}
|
|
|
|
|
2022-05-21 19:33:35 +00:00
|
|
|
pub fn serialize<W: std::io::Write>(&self, w: &mut W) -> std::io::Result<()> {
|
|
|
|
write_varint(&self.amount, w)?;
|
2022-07-27 10:29:14 +00:00
|
|
|
w.write_all(&[2 + (if self.view_tag.is_some() { 1 } else { 0 })])?;
|
2022-05-21 19:33:35 +00:00
|
|
|
write_point(&self.key, w)?;
|
2022-07-27 10:29:14 +00:00
|
|
|
if let Some(view_tag) = self.view_tag {
|
|
|
|
w.write_all(&[view_tag])?;
|
2022-05-21 19:33:35 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-05-22 00:27:21 +00:00
|
|
|
|
|
|
|
pub fn deserialize<R: std::io::Read>(r: &mut R) -> std::io::Result<Output> {
|
|
|
|
let amount = read_varint(r)?;
|
2022-07-27 10:29:14 +00:00
|
|
|
let mut output_type = [0];
|
|
|
|
r.read_exact(&mut output_type)?;
|
|
|
|
let view_tag = match output_type[0] {
|
|
|
|
2 => false,
|
|
|
|
3 => true,
|
|
|
|
_ => Err(std::io::Error::new(
|
2022-07-15 05:26:07 +00:00
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Tried to deserialize unknown/unused output type",
|
2022-07-27 10:29:14 +00:00
|
|
|
))?,
|
|
|
|
};
|
2022-05-22 00:27:21 +00:00
|
|
|
|
2022-07-15 05:26:07 +00:00
|
|
|
Ok(Output {
|
|
|
|
amount,
|
|
|
|
key: read_point(r)?,
|
2022-07-27 10:29:14 +00:00
|
|
|
view_tag: if view_tag {
|
|
|
|
let mut view_tag = [0];
|
|
|
|
r.read_exact(&mut view_tag)?;
|
|
|
|
Some(view_tag[0])
|
2022-07-15 05:26:07 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
})
|
2022-05-22 00:27:21 +00:00
|
|
|
}
|
2022-05-21 19:33:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 06:34:36 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
2022-06-02 04:00:26 +00:00
|
|
|
pub enum Timelock {
|
|
|
|
None,
|
|
|
|
Block(usize),
|
2022-07-15 05:26:07 +00:00
|
|
|
Time(u64),
|
2022-06-02 04:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Timelock {
|
|
|
|
fn from_raw(raw: u64) -> Timelock {
|
|
|
|
if raw == 0 {
|
|
|
|
Timelock::None
|
|
|
|
} else if raw < 500_000_000 {
|
|
|
|
Timelock::Block(usize::try_from(raw).unwrap())
|
|
|
|
} else {
|
|
|
|
Timelock::Time(raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 16:03:01 +00:00
|
|
|
pub(crate) fn fee_weight() -> usize {
|
|
|
|
8
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:00:26 +00:00
|
|
|
fn serialize<W: std::io::Write>(&self, w: &mut W) -> std::io::Result<()> {
|
|
|
|
write_varint(
|
|
|
|
&match self {
|
|
|
|
Timelock::None => 0,
|
|
|
|
Timelock::Block(block) => (*block).try_into().unwrap(),
|
2022-07-15 05:26:07 +00:00
|
|
|
Timelock::Time(time) => *time,
|
2022-06-02 04:00:26 +00:00
|
|
|
},
|
2022-07-15 05:26:07 +00:00
|
|
|
w,
|
2022-06-02 04:00:26 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-09 22:53:52 +00:00
|
|
|
impl PartialOrd for Timelock {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
match (self, other) {
|
|
|
|
(Timelock::None, _) => Some(Ordering::Less),
|
|
|
|
(Timelock::Block(a), Timelock::Block(b)) => a.partial_cmp(b),
|
|
|
|
(Timelock::Time(a), Timelock::Time(b)) => a.partial_cmp(b),
|
2022-07-15 05:26:07 +00:00
|
|
|
_ => None,
|
2022-07-09 22:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 06:34:36 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2022-05-21 19:33:35 +00:00
|
|
|
pub struct TransactionPrefix {
|
|
|
|
pub version: u64,
|
2022-06-02 04:00:26 +00:00
|
|
|
pub timelock: Timelock,
|
2022-05-21 19:33:35 +00:00
|
|
|
pub inputs: Vec<Input>,
|
|
|
|
pub outputs: Vec<Output>,
|
2022-07-15 05:26:07 +00:00
|
|
|
pub extra: Vec<u8>,
|
2022-05-21 19:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TransactionPrefix {
|
2022-07-27 09:05:43 +00:00
|
|
|
pub(crate) fn fee_weight(ring_len: usize, inputs: usize, outputs: usize, extra: usize) -> usize {
|
2022-06-19 16:03:01 +00:00
|
|
|
// Assumes Timelock::None since this library won't let you create a TX with a timelock
|
|
|
|
1 + 1 +
|
2022-07-15 05:26:07 +00:00
|
|
|
varint_len(inputs) +
|
2022-07-27 09:05:43 +00:00
|
|
|
(inputs * Input::fee_weight(ring_len)) +
|
2022-07-15 05:26:07 +00:00
|
|
|
1 +
|
|
|
|
(outputs * Output::fee_weight()) +
|
|
|
|
varint_len(extra) +
|
|
|
|
extra
|
2022-06-19 16:03:01 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 19:33:35 +00:00
|
|
|
pub fn serialize<W: std::io::Write>(&self, w: &mut W) -> std::io::Result<()> {
|
|
|
|
write_varint(&self.version, w)?;
|
2022-06-02 04:00:26 +00:00
|
|
|
self.timelock.serialize(w)?;
|
2022-05-21 19:33:35 +00:00
|
|
|
write_vec(Input::serialize, &self.inputs, w)?;
|
|
|
|
write_vec(Output::serialize, &self.outputs, w)?;
|
|
|
|
write_varint(&self.extra.len().try_into().unwrap(), w)?;
|
|
|
|
w.write_all(&self.extra)
|
|
|
|
}
|
2022-05-22 00:27:21 +00:00
|
|
|
|
|
|
|
pub fn deserialize<R: std::io::Read>(r: &mut R) -> std::io::Result<TransactionPrefix> {
|
|
|
|
let mut prefix = TransactionPrefix {
|
|
|
|
version: read_varint(r)?,
|
2022-06-02 04:00:26 +00:00
|
|
|
timelock: Timelock::from_raw(read_varint(r)?),
|
2022-05-22 00:27:21 +00:00
|
|
|
inputs: read_vec(Input::deserialize, r)?,
|
|
|
|
outputs: read_vec(Output::deserialize, r)?,
|
2022-07-15 05:26:07 +00:00
|
|
|
extra: vec![],
|
2022-05-22 00:27:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let len = read_varint(r)?;
|
|
|
|
prefix.extra.resize(len.try_into().unwrap(), 0);
|
|
|
|
r.read_exact(&mut prefix.extra)?;
|
|
|
|
|
|
|
|
Ok(prefix)
|
|
|
|
}
|
2022-05-21 19:33:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 06:34:36 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2022-05-21 19:33:35 +00:00
|
|
|
pub struct Transaction {
|
|
|
|
pub prefix: TransactionPrefix,
|
2022-07-15 05:26:07 +00:00
|
|
|
pub rct_signatures: RctSignatures,
|
2022-05-21 19:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Transaction {
|
2022-07-27 09:05:43 +00:00
|
|
|
pub(crate) fn fee_weight(ring_len: usize, inputs: usize, outputs: usize, extra: usize) -> usize {
|
|
|
|
TransactionPrefix::fee_weight(ring_len, inputs, outputs, extra) +
|
|
|
|
RctSignatures::fee_weight(ring_len, inputs, outputs)
|
2022-06-19 16:03:01 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 19:33:35 +00:00
|
|
|
pub fn serialize<W: std::io::Write>(&self, w: &mut W) -> std::io::Result<()> {
|
|
|
|
self.prefix.serialize(w)?;
|
|
|
|
self.rct_signatures.serialize(w)
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:27:21 +00:00
|
|
|
pub fn deserialize<R: std::io::Read>(r: &mut R) -> std::io::Result<Transaction> {
|
|
|
|
let prefix = TransactionPrefix::deserialize(r)?;
|
2022-07-15 05:26:07 +00:00
|
|
|
Ok(Transaction {
|
|
|
|
rct_signatures: RctSignatures::deserialize(
|
2022-05-22 00:27:21 +00:00
|
|
|
prefix
|
2022-07-15 05:26:07 +00:00
|
|
|
.inputs
|
|
|
|
.iter()
|
|
|
|
.map(|input| match input {
|
|
|
|
Input::Gen(_) => 0,
|
|
|
|
Input::ToKey { key_offsets, .. } => key_offsets.len(),
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
prefix.outputs.len(),
|
|
|
|
r,
|
|
|
|
)?,
|
|
|
|
prefix,
|
|
|
|
})
|
2022-05-22 00:27:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 19:33:35 +00:00
|
|
|
pub fn hash(&self) -> [u8; 32] {
|
|
|
|
let mut serialized = Vec::with_capacity(2048);
|
|
|
|
if self.prefix.version == 1 {
|
|
|
|
self.serialize(&mut serialized).unwrap();
|
|
|
|
hash(&serialized)
|
|
|
|
} else {
|
|
|
|
let mut sig_hash = Vec::with_capacity(96);
|
|
|
|
|
|
|
|
self.prefix.serialize(&mut serialized).unwrap();
|
|
|
|
sig_hash.extend(hash(&serialized));
|
|
|
|
serialized.clear();
|
|
|
|
|
2022-07-15 05:26:07 +00:00
|
|
|
self
|
|
|
|
.rct_signatures
|
|
|
|
.base
|
|
|
|
.serialize(&mut serialized, self.rct_signatures.prunable.rct_type())
|
|
|
|
.unwrap();
|
2022-05-21 19:33:35 +00:00
|
|
|
sig_hash.extend(hash(&serialized));
|
|
|
|
serialized.clear();
|
|
|
|
|
2022-05-22 03:16:06 +00:00
|
|
|
match self.rct_signatures.prunable {
|
|
|
|
RctPrunable::Null => serialized.resize(32, 0),
|
|
|
|
_ => {
|
|
|
|
self.rct_signatures.prunable.serialize(&mut serialized).unwrap();
|
|
|
|
serialized = hash(&serialized).to_vec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sig_hash.extend(&serialized);
|
2022-05-21 19:33:35 +00:00
|
|
|
|
|
|
|
hash(&sig_hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn signature_hash(&self) -> [u8; 32] {
|
|
|
|
let mut serialized = Vec::with_capacity(2048);
|
|
|
|
let mut sig_hash = Vec::with_capacity(96);
|
|
|
|
|
|
|
|
self.prefix.serialize(&mut serialized).unwrap();
|
|
|
|
sig_hash.extend(hash(&serialized));
|
|
|
|
serialized.clear();
|
|
|
|
|
2022-07-15 05:26:07 +00:00
|
|
|
self
|
|
|
|
.rct_signatures
|
|
|
|
.base
|
|
|
|
.serialize(&mut serialized, self.rct_signatures.prunable.rct_type())
|
|
|
|
.unwrap();
|
2022-05-21 19:33:35 +00:00
|
|
|
sig_hash.extend(hash(&serialized));
|
|
|
|
serialized.clear();
|
|
|
|
|
|
|
|
self.rct_signatures.prunable.signature_serialize(&mut serialized).unwrap();
|
|
|
|
sig_hash.extend(&hash(&serialized));
|
|
|
|
|
|
|
|
hash(&sig_hash)
|
|
|
|
}
|
|
|
|
}
|