Skip to content

Commit

Permalink
txid implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
TAdev0 committed Aug 8, 2024
1 parent 9f1e27c commit 2641eda
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
20 changes: 10 additions & 10 deletions src/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,6 @@ pub struct Transaction {
pub lock_time: u32,
}

/// Output of a transaction.
/// https://learnmeabitcoin.com/technical/transaction/output/
#[derive(Drop, Copy)]
pub struct TxOut {
/// The value of the output in satoshis.
pub value: i64,
/// The spending script (aka locking code) for this output.
pub pk_script: @ByteArray,
}

/// Input of a transaction.
/// https://learnmeabitcoin.com/technical/transaction/input/
///
Expand All @@ -131,3 +121,13 @@ pub struct TxIn {
/// The index of output in the utreexo set (meta field).
pub txo_index: u64,
}

/// Output of a transaction.
/// https://learnmeabitcoin.com/technical/transaction/output/
#[derive(Drop, Copy)]
pub struct TxOut {
/// The value of the output in satoshis.
pub value: i64,
/// The spending script (aka locking code) for this output.
pub pk_script: @ByteArray,
}
49 changes: 46 additions & 3 deletions src/validation.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use super::state::{Block, ChainState, Transaction, UtreexoState};
use core::sha256::compute_sha256_byte_array;
use core::to_byte_array::FormatAsByteArray;
use core::num::traits::{Zero, One, BitSize};
use super::state::{Block, ChainState, Transaction, UtreexoState, TxIn, TxOut};
use super::utils::{shl, shr};

const MAX_TARGET: u256 = 0x00000000FFFF0000000000000000000000000000000000000000000000000000;
Expand Down Expand Up @@ -37,10 +40,50 @@ impl BlockValidatorImpl of BlockValidator {

#[generate_trait]
impl TransactionValidatorImpl of TransactionValidator {
// marker, flag, and witness fields in segwit transactions are not included
// this means txid computation is the same for legacy and segwit tx
fn txid(self: @Transaction) -> u256 {
// TODO: implement
0
let version: ByteArray = (*self.version).format_as_byte_array(10);
let inputs: Span<TxIn> = *self.inputs;
let outputs: Span<TxOut> = *self.outputs;
let locktime: ByteArray = (*self.lock_time).format_as_byte_array(10);

let mut sha256_input: ByteArray = "";
sha256_input.append(@version);

let mut i = 0;
while i < inputs.len() {
sha256_input.append(*inputs.at(i).script);
sha256_input.append(@(*inputs.at(i).sequence).format_as_byte_array(10));
sha256_input.append(@(*inputs.at(i).txo_index).format_as_byte_array(10));

i += 1;
};

let mut i = 0;
while i < outputs.len() {
let value: felt252 = (*outputs.at(i).value).into();
sha256_input.append(@value.format_as_byte_array(10));

i += 1;
};

sha256_input.append(@locktime);

let txid = compute_sha256_byte_array(@sha256_input).span();

let mut result: u256 = 0;
let mut i: u32 = 0;
while i != 8 {
let byte: u256 = (*txid[i]).into();
result += shl(byte, (8 * i).into());

i += 1;
};

result
}

fn fee(self: @Transaction) -> u256 {
// TODO: implement
0
Expand Down

0 comments on commit 2641eda

Please sign in to comment.