Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add identity chain actions, switch from hex to base58, switch to borsh for signature, refactor identity chain #273

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ rocket_dyn_templates = { version = "0.2.0", features = ["handlebars"] }
open = "5.3.0"
serde_json = "1.0.129"
serde = { version = "1.0.210", features = ["derive"] }
hex = "0.4.3"
log = "0.4.22"
futures = "0.3.31"
bitcoin = { version = "0.32.3", features = ["rand", "rand-std"] }
Expand All @@ -63,6 +62,7 @@ uuid = { version = "1.11.0", features = ["v4"] }
infer = { version = "0.16", default-features = false }
surrealdb = "2"
lazy_static = "1.5.0"
bs58 = "0.5.1"

[dev-dependencies]
mockall = "0.13.0"
Expand Down
132 changes: 78 additions & 54 deletions src/blockchain/bill/block.rs

Large diffs are not rendered by default.

31 changes: 16 additions & 15 deletions src/blockchain/bill/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use crate::service::bill_service::BillKeys;
use crate::service::bill_service::BitcreditBill;
use crate::service::contact_service::IdentityPublicData;
use crate::util::{rsa, BcrKeys};
use crate::util::{self, rsa, BcrKeys};
use borsh::{from_slice, to_vec};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -45,14 +45,13 @@
timestamp: i64,
) -> Result<Self> {
let drawer_bytes = serde_json::to_vec(&drawer)?;
let data_for_new_block = format!("{}{}", SIGNED_BY, hex::encode(drawer_bytes));
let data_for_new_block = format!("{}{}", SIGNED_BY, util::base58_encode(&drawer_bytes));

let genesis_hash: String = hex::encode(data_for_new_block.as_bytes());
let genesis_hash: String = util::base58_encode(data_for_new_block.as_bytes());

let encrypted_and_hashed_bill_data = hex::encode(rsa::encrypt_bytes_with_public_key(
&to_vec(bill)?,
&bill_public_key_pem,
)?);
let encrypted_and_hashed_bill_data = util::base58_encode(
&rsa::encrypt_bytes_with_public_key(&to_vec(bill)?, &bill_public_key_pem)?,
);

let first_block = BillBlock::new(
1,
Expand Down Expand Up @@ -135,7 +134,7 @@
{
let block_data_decrypted =
last_version_block_sell.get_decrypted_block_data(bill_keys)?;
let buyer: IdentityPublicData = serde_json::from_slice(&hex::decode(
let buyer: IdentityPublicData = serde_json::from_slice(&util::base58_decode(
&extract_after_phrase(&block_data_decrypted, SOLD_TO).ok_or(
Error::InvalidBlockdata(String::from("Sell: No buyer found")),
)?,
Expand All @@ -147,7 +146,7 @@
{
let block_data_decrypted =
last_version_block_endorse.get_decrypted_block_data(bill_keys)?;
let endorsee: IdentityPublicData = serde_json::from_slice(&hex::decode(
let endorsee: IdentityPublicData = serde_json::from_slice(&util::base58_decode(

Check warning on line 149 in src/blockchain/bill/chain.rs

View check run for this annotation

Codecov / codecov/patch

src/blockchain/bill/chain.rs#L149

Added line #L149 was not covered by tests
&extract_after_phrase(&block_data_decrypted, ENDORSED_TO).ok_or(
Error::InvalidBlockdata(String::from("Endorse: No endorsee found")),
)?,
Expand All @@ -159,7 +158,7 @@
{
let block_data_decrypted =
last_version_block_mint.get_decrypted_block_data(bill_keys)?;
let mint: IdentityPublicData = serde_json::from_slice(&hex::decode(
let mint: IdentityPublicData = serde_json::from_slice(&util::base58_decode(

Check warning on line 161 in src/blockchain/bill/chain.rs

View check run for this annotation

Codecov / codecov/patch

src/blockchain/bill/chain.rs#L161

Added line #L161 was not covered by tests
&extract_after_phrase(&block_data_decrypted, ENDORSED_TO)
.ok_or(Error::InvalidBlockdata(String::from("Mint: No mint found")))?,
)?)?;
Expand Down Expand Up @@ -220,12 +219,12 @@
let block_data_decrypted =
last_version_block_sell.get_decrypted_block_data(bill_keys)?;

let buyer: IdentityPublicData = serde_json::from_slice(&hex::decode(
let buyer: IdentityPublicData = serde_json::from_slice(&util::base58_decode(
&extract_after_phrase(&block_data_decrypted, SOLD_TO).ok_or(
Error::InvalidBlockdata(String::from("Sell: No buyer found")),
)?,
)?)?;
let seller: IdentityPublicData = serde_json::from_slice(&hex::decode(
let seller: IdentityPublicData = serde_json::from_slice(&util::base58_decode(
&extract_after_phrase(&block_data_decrypted, SOLD_BY).ok_or(
Error::InvalidBlockdata(String::from("Sell: No seller found")),
)?,
Expand Down Expand Up @@ -349,8 +348,8 @@
let mut seller = IdentityPublicData::new_empty();
let endorser_peer_id = PeerId::random().to_string();
seller.peer_id = endorser_peer_id.clone();
let hashed_buyer = hex::encode(serde_json::to_vec(&buyer).unwrap());
let hashed_seller = hex::encode(serde_json::to_vec(&seller).unwrap());
let hashed_buyer = util::base58_encode(&serde_json::to_vec(&buyer).unwrap());
let hashed_seller = util::base58_encode(&serde_json::to_vec(&seller).unwrap());

let data = format!(
"{}{}{}{}{}{}",
Expand All @@ -360,7 +359,9 @@
BillBlock::new(
2,
prevhash,
hex::encode(rsa::encrypt_bytes_with_public_key(data.as_bytes(), TEST_PUB_KEY).unwrap()),
util::base58_encode(
&rsa::encrypt_bytes_with_public_key(data.as_bytes(), TEST_PUB_KEY).unwrap(),
),
BillOpCode::Sell,
BcrKeys::new(),
1731593928,
Expand Down
3 changes: 2 additions & 1 deletion src/blockchain/bill/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use borsh_derive::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};

use super::{Blockchain, Result};
Expand All @@ -10,7 +11,7 @@ mod chain;
pub use block::BillBlock;
pub use chain::BillBlockchain;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum BillOpCode {
Issue,
Accept,
Expand Down
Loading
Loading