Skip to content

Commit

Permalink
Add prepare_sign method to transaction (#9)
Browse files Browse the repository at this point in the history
* Add `prepare_sign` method to transaction

The transaction to be signed have some fields zeroed as in the specs.

This commit facilitates the bytes manipulation of a serialized
transaction so they can be zeroed with no hardcoded positions.

With the `prepare_sign` function, it is trivial to add `id` considering
some generic hash function - currently defined as Sha256.

* Change input offset to target predicate

The latest spec points the execution of a predicate verification is over
the predicate bytes and not the predicate data of the Input::Coin
instances of the transaction
  • Loading branch information
vlopes11 authored and xgreenx committed Dec 20, 2022
1 parent 7f10c71 commit 5d36c9e
Show file tree
Hide file tree
Showing 16 changed files with 526 additions and 151 deletions.
1 change: 1 addition & 0 deletions fuel-tx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ categories = ["cryptography::cryptocurrencies", "data-structures"]
[dependencies]
fuel-asm = {git="ssh://git@github.com/FuelLabs/fuel-asm.git"}
itertools = "0.10"
sha2 = "0.9"
42 changes: 41 additions & 1 deletion fuel-tx/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,47 @@ use std::{io, mem};

const WORD_SIZE: usize = mem::size_of::<Word>();

pub trait SizedBytes {
fn serialized_size(&self) -> usize;
}

pub trait SerializableVec: SizedBytes {
fn to_bytes(&mut self) -> Vec<u8>;
}

pub trait Deserializable: Sized {
fn from_bytes(bytes: &[u8]) -> io::Result<Self>;
}

impl<T> SerializableVec for T
where
T: SizedBytes + io::Read,
{
fn to_bytes(&mut self) -> Vec<u8> {
let n = self.serialized_size();

let mut bytes = vec![0u8; n];

self.read(bytes.as_mut_slice())
.expect("Incorrect `SizedBytes` implementation!");

bytes
}
}

impl<T> Deserializable for T
where
T: Default + io::Write,
{
fn from_bytes(bytes: &[u8]) -> io::Result<Self> {
let mut instance = Self::default();

instance.write(bytes)?;

Ok(instance)
}
}

pub fn eof() -> io::Error {
io::Error::new(io::ErrorKind::UnexpectedEof, "The provided buffer is not big enough!")
}
Expand All @@ -13,7 +54,6 @@ pub fn store_bytes<'a>(mut buf: &'a mut [u8], bytes: &[u8]) -> io::Result<(usize
let len = (bytes.len() as Word).to_be_bytes();
let pad = bytes.len() % WORD_SIZE;
let pad = if pad == 0 { 0 } else { WORD_SIZE - pad };

if buf.len() < WORD_SIZE + bytes.len() + pad {
return Err(eof());
}
Expand Down
8 changes: 8 additions & 0 deletions fuel-tx/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::Hash;
use sha2::{Digest, Sha256};

pub fn hash(data: &[u8]) -> Hash {
let mut hasher = Sha256::new();
hasher.update(data);
hasher.finalize().into()
}
1 change: 1 addition & 0 deletions fuel-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod transaction;

pub mod bytes;
pub mod consts;
pub mod crypto;

pub use transaction::{
Address, Color, ContractAddress, Hash, Input, Output, Salt, Transaction, ValidationError, Witness,
Expand Down
Loading

0 comments on commit 5d36c9e

Please sign in to comment.