Skip to content

Commit

Permalink
transaction fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Apr 3, 2024
1 parent c1b1d58 commit 71cc5ed
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
13 changes: 6 additions & 7 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use core::fmt;
use crate::blake2b::Accumulator;
use blake2b_simd::Params;
use crate::blake2b::LEAF_HASH_PREFIX;
use crate::{SiaEncodable, HexParseError};
use ed25519_dalek::{SigningKey, Signer, VerifyingKey, Verifier, Signature};
use crate::{SiaEncodable, HexParseError, Signature};
use ed25519_dalek::{SigningKey, Signer, VerifyingKey, Verifier};

/// An ed25519 public key that can be used to verify a signature
#[derive(Debug, PartialEq, Clone, Copy)]
Expand All @@ -15,10 +15,9 @@ impl PublicKey {
self.0
}

pub fn verify_hash(&self, hash: &[u8;32], signature: &[u8;64]) -> bool {
pub fn verify_hash(&self, hash: &[u8;32], signature: Signature) -> bool {
let pk = VerifyingKey::from_bytes(&self.0).unwrap();
let sig = Signature::from_bytes(signature);
pk.verify(hash, &sig).is_ok()
pk.verify(hash, &signature.into()).is_ok()
}
}

Expand All @@ -40,9 +39,9 @@ impl PrivateKey {
PublicKey(self.0[32..].try_into().unwrap())
}

pub fn sign_hash(&self, hash: &[u8;32]) -> [u8;64] {
pub fn sign_hash(&self, hash: &[u8;32]) -> Signature {
let sk = SigningKey::from_bytes(&self.0[..32].try_into().unwrap());
sk.sign(hash).to_bytes()
Signature::new(sk.sign(hash).to_vec())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub enum HexParseError {
HexError(hex::FromHexError),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Hash256([u8;32]);

impl Hash256 {
Expand Down
47 changes: 46 additions & 1 deletion src/signing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use core::fmt;

use crate::consensus::ChainIndex;
use crate::transactions::{Transaction, CoveredFields};
use blake2b_simd::Params;
use crate::SiaEncodable;
use crate::{HexParseError, SiaEncodable};

pub struct NetworkHardforks {
pub asic_height: u64,
Expand All @@ -17,6 +19,49 @@ pub struct SigningState {
hardforks: NetworkHardforks,
}

#[derive(Debug, Clone)]
pub struct Signature(Vec<u8>);

impl Signature {
pub fn new(data: Vec<u8>) -> Self {
Signature(data)
}

pub fn data(&self) -> &[u8] {
&self.0
}

pub fn parse_string(s: &str) -> Result<Self, HexParseError> {
let s = match s.split_once(":"){
Some((_prefix, suffix)) => suffix,
None => s
};

let data = hex::decode(s).map_err(|e| HexParseError::HexError(e))?;
Ok(Signature(data))
}
}

impl Into<ed25519_dalek::Signature> for Signature {
fn into(self) -> ed25519_dalek::Signature {
ed25519_dalek::Signature::from_bytes(self.0.as_slice().try_into().unwrap())
}

}

impl SiaEncodable for Signature {
fn encode(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(&(self.0.len() as u64).to_le_bytes());
buf.extend_from_slice(&self.0);
}
}

impl fmt::Display for Signature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "sig:{}", hex::encode(&self.0))
}
}

impl SigningState {
pub fn new(index: ChainIndex, hardforks: NetworkHardforks) -> Self {
SigningState {
Expand Down
15 changes: 8 additions & 7 deletions src/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use core::fmt;

use crate::currency::Currency;
use crate::address::{Address, UnlockConditions};
use crate::Signature;
use crate::Currency;
use crate::{Address, UnlockConditions};
use crate::{HexParseError, Hash256, SiaEncodable};
use blake2b_simd::{Params, State};

Expand Down Expand Up @@ -318,21 +319,21 @@ impl SiaEncodable for CoveredFields {
}

#[derive(Debug, Clone)]
pub struct Signature {
pub struct TransactionSignature {
pub parent_id: Hash256,
pub public_key_index: u64,
pub timelock: u64,
pub covered_fields: CoveredFields,
pub signature: [u8;64],
pub signature: Signature,
}

impl SiaEncodable for Signature {
impl SiaEncodable for TransactionSignature {
fn encode(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(&self.parent_id.as_bytes());
buf.extend_from_slice(&self.public_key_index.to_le_bytes());
buf.extend_from_slice(&self.timelock.to_le_bytes());
self.covered_fields.encode(buf);
buf.extend_from_slice(&self.signature);
self.signature.encode(buf);
}
}

Expand Down Expand Up @@ -372,7 +373,7 @@ pub struct Transaction {
pub file_contracts: Vec<FileContract>,
pub file_contract_revisions: Vec<FileContractRevision>,
pub storage_proofs: Vec<StorageProof>,
pub signatures: Vec<Signature>,
pub signatures: Vec<TransactionSignature>,
pub arbitrary_data: Vec<Vec<u8>>,
}

Expand Down

0 comments on commit 71cc5ed

Please sign in to comment.