-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finishing the json parsing and serialization from transactions
- Loading branch information
1 parent
78705dc
commit 638410b
Showing
5 changed files
with
96 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
pub mod tx; | ||
pub mod tx; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,108 @@ | ||
#![allow(dead_code, unused)] | ||
|
||
// use serde::{Serialize, Deserialize} | ||
// use serde_json; | ||
// use ring::digest; | ||
|
||
#[derive(Debug)] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Tx{ | ||
pub version: u32, | ||
pub tx_input: Vec<TxInput>, | ||
pub tx_size: u64, | ||
pub tx_output: Vec<TxOutput> | ||
pub locktime: u32, | ||
pub vin: Vec<TxInput>, | ||
pub vout: Vec<TxOutput>, | ||
// tx_size: u64, | ||
} | ||
|
||
|
||
impl Tx{ | ||
pub fn new (version: u32, tx_input: Vec<TxInput>, tx_output: Vec<TxOutput>) -> Self { | ||
pub fn new (version: u32, locktime: u32, vin: Vec<TxInput>, vout: Vec<TxOutput>) -> Self { | ||
let mut tx_size = 32 + 64; | ||
|
||
tx_output.iter().for_each(|output| tx_size += output.get_tx_output_size_in_bytes()); | ||
tx_input.iter().for_each(|input| tx_size += input.get_tx_input_size_in_bytes()); | ||
vout.iter().for_each(|output| tx_size += output.get_tx_output_size_in_bytes()); | ||
vin.iter().for_each(|input| tx_size += input.get_tx_input_size_in_bytes()); | ||
|
||
|
||
Self { | ||
version, | ||
tx_input, | ||
tx_size, | ||
tx_output, | ||
locktime, | ||
vin, | ||
vout, | ||
// tx_size, | ||
} | ||
} | ||
|
||
// pub fn get_tx_size(&self) -> u64 { | ||
// self.tx_size | ||
// } | ||
} | ||
|
||
#[derive(Debug)] | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct TxInput{ | ||
tx_id: String, | ||
value: u64, | ||
signature_script: String, | ||
is_coinbase: bool, | ||
pub txid: String, | ||
pub vout: u32, | ||
pub prevout: TxPrevOut, | ||
pub scriptsig: String, | ||
pub scriptsig_asm: String, | ||
pub witness: Vec<String>, | ||
pub is_coinbase: bool, | ||
pub sequence: u64, | ||
} | ||
|
||
impl TxInput{ | ||
|
||
pub fn new(tx_id: String, value: u64, signature_script: String, is_coinbase: bool) -> Self{ | ||
pub fn new(txid: String,vout: u32,prevout: TxPrevOut,scriptsig: String,scriptsig_asm: String,witness: Vec<String>,is_coinbase: bool,sequence: u64,) -> Self{ | ||
Self{ | ||
tx_id, | ||
value, | ||
signature_script, | ||
is_coinbase | ||
txid, | ||
vout, | ||
prevout, | ||
scriptsig, | ||
scriptsig_asm, | ||
witness, | ||
is_coinbase, | ||
sequence, | ||
} | ||
} | ||
|
||
fn get_tx_input_size_in_bytes(&self) -> u64{ | ||
let sigscript_size: u64 = self.signature_script.len() as u64 * 32; | ||
let tx_id_size: u64 = self.tx_id.len() as u64 * 32; | ||
return 64 + sigscript_size + tx_id_size + 1; | ||
let sigscript_size: u64 = self.scriptsig.len() as u64 * 32; | ||
let txid_size: u64 = self.txid.len() as u64 * 32; | ||
return 64 + sigscript_size + txid_size + 1; | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct TxPrevOut{ | ||
pub scriptpubkey: String, | ||
pub scriptpubkey_asm: String, | ||
pub scriptpubkey_type: String, | ||
pub scriptpubkey_address: String, | ||
pub value: u64, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct TxOutput { | ||
pub scriptpubkey: String, | ||
pub scriptpubkey_asm: String, | ||
pub scriptpubkey_type: String, | ||
pub scriptpubkey_address: String, | ||
pub value: u64, | ||
pub pk_script: String, | ||
} | ||
|
||
impl TxOutput{ | ||
|
||
pub fn new(value:u64, pk_script: String) -> Self { | ||
pub fn new(scriptpubkey: String, scriptpubkey_asm: String, scriptpubkey_type: String, scriptpubkey_address: String, value: u64,) -> Self { | ||
Self{ | ||
value, | ||
pk_script | ||
scriptpubkey, | ||
scriptpubkey_asm, | ||
scriptpubkey_type, | ||
scriptpubkey_address, | ||
value | ||
} | ||
} | ||
|
||
pub fn get_tx_output_size_in_bytes(&self) -> u64{ | ||
let pk_script_size: u64 = self.pk_script.len() as u64 * 32; | ||
let pk_script_size: u64 = self.scriptpubkey.len() as u64 * 32; | ||
return pk_script_size + 64 | ||
} | ||
} | ||
} |