1use std::error::Error;
2use tofuri_address::address;
3use tofuri_api_core::Block;
4use tofuri_api_core::Stake;
5use tofuri_api_core::Transaction;
6use tofuri_block::BlockA;
7use tofuri_stake::StakeA;
8use tofuri_stake::StakeB;
9use tofuri_transaction::TransactionA;
10use tofuri_transaction::TransactionB;
11pub fn block(block_a: &BlockA) -> Block {
12 Block {
13 hash: hex::encode(block_a.hash),
14 previous_hash: hex::encode(block_a.previous_hash),
15 timestamp: block_a.timestamp,
16 beta: hex::encode(block_a.beta),
17 pi: hex::encode(block_a.pi),
18 forger_address: address::encode(&block_a.input_address()),
19 signature: hex::encode(block_a.signature),
20 transactions: block_a.transactions.iter().map(|x| hex::encode(x.hash)).collect(),
21 stakes: block_a.stakes.iter().map(|x| hex::encode(x.hash)).collect(),
22 }
23}
24pub fn transaction(transaction_a: &TransactionA) -> Transaction {
25 Transaction {
26 input_address: address::encode(&transaction_a.input_address),
27 output_address: address::encode(&transaction_a.output_address),
28 amount: tofuri_int::to_string(transaction_a.amount),
29 fee: tofuri_int::to_string(transaction_a.fee),
30 timestamp: transaction_a.timestamp,
31 hash: hex::encode(transaction_a.hash),
32 signature: hex::encode(transaction_a.signature),
33 }
34}
35pub fn stake(stake_a: &StakeA) -> Stake {
36 Stake {
37 amount: tofuri_int::to_string(stake_a.amount),
38 fee: tofuri_int::to_string(stake_a.fee),
39 deposit: stake_a.deposit,
40 timestamp: stake_a.timestamp,
41 signature: hex::encode(stake_a.signature),
42 input_address: address::encode(&stake_a.input_address),
43 hash: hex::encode(stake_a.hash),
44 }
45}
46pub fn transaction_b(transaction: &Transaction) -> Result<TransactionB, Box<dyn Error>> {
47 Ok(TransactionB {
48 output_address: address::decode(&transaction.output_address)?,
49 amount: tofuri_int::to_be_bytes(tofuri_int::from_str(&transaction.amount)?),
50 fee: tofuri_int::to_be_bytes(tofuri_int::from_str(&transaction.fee)?),
51 timestamp: transaction.timestamp,
52 signature: hex::decode(&transaction.signature)?.as_slice().try_into()?,
53 })
54}
55pub fn stake_b(stake: &Stake) -> Result<StakeB, Box<dyn Error>> {
56 Ok(StakeB {
57 amount: tofuri_int::to_be_bytes(tofuri_int::from_str(&stake.amount)?),
58 fee: tofuri_int::to_be_bytes(tofuri_int::from_str(&stake.fee)?),
59 deposit: stake.deposit,
60 timestamp: stake.timestamp,
61 signature: hex::decode(&stake.signature)?.as_slice().try_into()?,
62 })
63}