Skip to content

Commit b8f47ed

Browse files
authored
Add BlockId, SchnorrSignature, Sha256Hash and fix serde deserialization for string types (#714)
1 parent ed11ac5 commit b8f47ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1120
-327
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Document versioning scheme
44
* Rework WASM API to return `TSResult<T> = { Success: T } | { Error: JsErrorData }` without triggering exceptions
55
* Rework `sum` and `currency` into a coherent `Sum` type that's ready for multi-currency and exchange rates (breaking DB change)
6+
* Add strong types for `SchnorrSignature`, `Sha256Hash`, `BlockId` (breaking DB change)
67

78
# 0.4.12
89

crates/bcr-ebill-api/src/data/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
pub use bcr_ebill_core::address;
22
pub use bcr_ebill_core::bill;
3+
pub use bcr_ebill_core::block_id;
34
pub use bcr_ebill_core::city;
45
pub use bcr_ebill_core::company;
56
pub use bcr_ebill_core::contact;
67
pub use bcr_ebill_core::country;
78
pub use bcr_ebill_core::date;
89
pub use bcr_ebill_core::email;
10+
pub use bcr_ebill_core::hash;
911
pub use bcr_ebill_core::identification;
1012
pub use bcr_ebill_core::identity;
1113
pub use bcr_ebill_core::identity_proof;
1214
pub use bcr_ebill_core::mint;
1315
pub use bcr_ebill_core::name;
1416
pub use bcr_ebill_core::nostr_contact;
1517
pub use bcr_ebill_core::notification;
18+
pub use bcr_ebill_core::signature;
1619
pub use bcr_ebill_core::sum;
1720
pub use bcr_ebill_core::zip;
1821

crates/bcr-ebill-api/src/external/mint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,8 @@ fn map_shared_bill(
461461
bill_id: bill_to_share.bill_id,
462462
data: bill_to_share.data,
463463
file_urls: bill_to_share.file_urls,
464-
hash: bill_to_share.hash,
465-
signature: bill_to_share.signature,
464+
hash: bill_to_share.hash.to_string(),
465+
signature: bill_to_share.signature.to_string(),
466466
receiver: bill_to_share.receiver.into(),
467467
}
468468
}

crates/bcr-ebill-api/src/service/bill_service/issue.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use bcr_ebill_core::{
1616
bill::{BillBlockchain, block::BillIssueBlockData},
1717
},
1818
contact::{BillAnonParticipant, BillIdentParticipant, BillParticipant},
19+
hash::Sha256Hash,
1920
util::BcrKeys,
2021
};
2122
use log::{debug, error, info};
@@ -36,7 +37,7 @@ impl BillService {
3637
upload_file_type.max_file_size(),
3738
)));
3839
}
39-
let file_hash = util::sha256_hash(file_bytes);
40+
let file_hash = Sha256Hash::from_bytes(file_bytes);
4041
let encrypted = util::crypto::encrypt_ecies(file_bytes, public_key)?;
4142
let nostr_hash = self.file_upload_client.upload(relay_url, encrypted).await?;
4243
info!("Saved file {file_name} with hash {file_hash} for bill {bill_id}");

crates/bcr-ebill-api/src/service/bill_service/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@ pub mod tests {
273273
BillRecourseStatus, BillSellStatus, BillWaitingForPaymentState,
274274
BillWaitingStatePaymentData, PastPaymentStatus, RecourseReason,
275275
},
276+
block_id::BlockId,
276277
blockchain::{
277-
Blockchain,
278+
Block, Blockchain,
278279
bill::{
279280
BillBlock, BillOpCode,
280281
block::{
@@ -293,6 +294,7 @@ pub mod tests {
293294
contact::{BillAnonParticipant, BillIdentParticipant, BillParticipant},
294295
country::Country,
295296
date::Date,
297+
hash::Sha256Hash,
296298
mint::{MintOffer, MintRequest, MintRequestStatus},
297299
name::Name,
298300
notification::ActionType,
@@ -829,7 +831,7 @@ pub mod tests {
829831
&bill_id_test(),
830832
&File {
831833
name: "some_file".into(),
832-
hash: "".into(),
834+
hash: Sha256Hash::new("some hash"),
833835
nostr_hash: "".into()
834836
},
835837
&private_key_test(),
@@ -2532,18 +2534,19 @@ pub mod tests {
25322534
let mut bill = get_baseline_bill(&bill_id_test());
25332535
bill.drawee = bill_identified_participant_only_node_id(identity.identity.node_id.clone());
25342536
let mut chain = get_genesis_chain(Some(bill.clone()));
2537+
let latest_block = chain.get_latest_block().clone();
25352538
chain.blocks_mut().push(
25362539
BillBlock::new(
25372540
bill_id_test(),
2538-
123456,
2539-
"prevhash".to_string(),
2541+
BlockId::next_from_previous_block_id(&latest_block.id()),
2542+
Sha256Hash::new("prevhash"),
25402543
"data".to_string(),
25412544
BillOpCode::Accept,
25422545
&keys,
25432546
None,
25442547
&BcrKeys::from_private_key(&private_key_test()).unwrap(),
25452548
1731593928,
2546-
"plain text hash".to_string(),
2549+
Sha256Hash::new("plain text hash"),
25472550
)
25482551
.unwrap(),
25492552
);

crates/bcr-ebill-api/src/service/bill_service/service.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use bcr_ebill_core::contact::{
4545
BillAnonParticipant, BillParticipant, Contact, LightBillParticipant,
4646
};
4747
use bcr_ebill_core::email::Email;
48+
use bcr_ebill_core::hash::Sha256Hash;
4849
use bcr_ebill_core::identity::{IdentityType, IdentityWithAll};
4950
use bcr_ebill_core::mint::{MintRequest, MintRequestState, MintRequestStatus};
5051
use bcr_ebill_core::notification::ActionType;
@@ -1007,7 +1008,7 @@ impl BillServiceApi for BillService {
10071008
.download(nostr_relay, &file.nostr_hash)
10081009
.await?;
10091010
let decrypted = util::crypto::decrypt_ecies(&file_bytes, bill_private_key)?;
1010-
let file_hash = util::sha256_hash(&decrypted);
1011+
let file_hash = Sha256Hash::from_bytes(&decrypted);
10111012
if file_hash != file.hash {
10121013
error!(
10131014
"Hash for bill file {} did not match uploaded file",

crates/bcr-ebill-api/src/service/company_service.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use bcr_ebill_core::city::City;
3636
use bcr_ebill_core::country::Country;
3737
use bcr_ebill_core::date::Date;
3838
use bcr_ebill_core::email::Email;
39+
use bcr_ebill_core::hash::Sha256Hash;
3940
use bcr_ebill_core::identification::Identification;
4041
use bcr_ebill_core::identity::IdentityType;
4142
use bcr_ebill_core::name::Name;
@@ -210,7 +211,7 @@ impl CompanyService {
210211
public_key: &PublicKey,
211212
relay_url: &url::Url,
212213
) -> Result<File> {
213-
let file_hash = util::sha256_hash(file_bytes);
214+
let file_hash = Sha256Hash::from_bytes(file_bytes);
214215
let encrypted = util::crypto::encrypt_ecies(file_bytes, public_key)?;
215216
let nostr_hash = self.file_upload_client.upload(relay_url, encrypted).await?;
216217
info!("Saved company file {file_name} with hash {file_hash} for company {id}");
@@ -947,7 +948,7 @@ impl CompanyServiceApi for CompanyService {
947948
.download(nostr_relay, &file.nostr_hash)
948949
.await?;
949950
let decrypted = util::crypto::decrypt_ecies(&file_bytes, private_key)?;
950-
let file_hash = util::sha256_hash(&decrypted);
951+
let file_hash = Sha256Hash::from_bytes(&decrypted);
951952
if file_hash != file.hash {
952953
error!("Hash for company file {file_name} did not match uploaded file");
953954
return Err(super::Error::NotFound);
@@ -2484,7 +2485,8 @@ pub mod tests {
24842485
.unwrap();
24852486
assert_eq!(
24862487
file.hash,
2487-
String::from("DULfJyE3WQqNxy3ymuhAChyNR3yufT88pmqvAazKFMG4")
2488+
Sha256Hash::from_str("DULfJyE3WQqNxy3ymuhAChyNR3yufT88pmqvAazKFMG4")
2489+
.expect("valid hash")
24882490
);
24892491
assert_eq!(file.name, String::from(file_name));
24902492

crates/bcr-ebill-api/src/service/contact_service.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use bcr_ebill_core::{
1111
country::Country,
1212
date::Date,
1313
email::Email,
14+
hash::Sha256Hash,
1415
identification::Identification,
1516
name::Name,
1617
nostr_contact::{NostrContact, NostrPublicKey, TrustLevel},
@@ -205,7 +206,7 @@ impl ContactService {
205206
public_key: &PublicKey,
206207
relay_url: &url::Url,
207208
) -> Result<File> {
208-
let file_hash = util::sha256_hash(file_bytes);
209+
let file_hash = Sha256Hash::from_bytes(file_bytes);
209210
let encrypted = util::crypto::encrypt_ecies(file_bytes, public_key)?;
210211
let nostr_hash = self.file_upload_client.upload(relay_url, encrypted).await?;
211212
info!("Saved contact file {file_name} with hash {file_hash} for contact {node_id}");
@@ -722,7 +723,7 @@ impl ContactServiceApi for ContactService {
722723
.download(nostr_relay, &file.nostr_hash)
723724
.await?;
724725
let decrypted = util::crypto::decrypt_ecies(&file_bytes, private_key)?;
725-
let file_hash = util::sha256_hash(&decrypted);
726+
let file_hash = Sha256Hash::from_bytes(&decrypted);
726727
if file_hash != file.hash {
727728
error!("Hash for contact file {file_name} did not match uploaded file");
728729
return Err(super::Error::NotFound);

crates/bcr-ebill-api/src/service/identity_proof_service.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,10 @@ impl IdentityProofServiceApi for IdentityProofService {
385385

386386
#[cfg(test)]
387387
pub mod tests {
388-
use bcr_ebill_core::blockchain::{Blockchain, identity::IdentityBlockchain};
388+
use bcr_ebill_core::{
389+
block_id::BlockId,
390+
blockchain::{Blockchain, identity::IdentityBlockchain},
391+
};
389392

390393
use crate::{
391394
external::identity_proof::MockIdentityProofApi,
@@ -557,7 +560,7 @@ pub mod tests {
557560
timestamp: 1731593928,
558561
status: IdentityProofStatus::Success,
559562
status_last_checked_timestamp: 1731593929,
560-
block_id: 2,
563+
block_id: BlockId::next_from_previous_block_id(&BlockId::first()),
561564
}))
562565
});
563566
let service = get_service(ctx);
@@ -577,7 +580,7 @@ pub mod tests {
577580
timestamp: 1731593928,
578581
status: IdentityProofStatus::Success,
579582
status_last_checked_timestamp: 1731593929,
580-
block_id: 2,
583+
block_id: BlockId::next_from_previous_block_id(&BlockId::first()),
581584
}))
582585
});
583586
let service = get_service(ctx);
@@ -607,7 +610,7 @@ pub mod tests {
607610
timestamp: 1731593928,
608611
status: IdentityProofStatus::Success,
609612
status_last_checked_timestamp: 1731593929,
610-
block_id: 2,
613+
block_id: BlockId::next_from_previous_block_id(&BlockId::first()),
611614
}))
612615
});
613616
ctx.store
@@ -645,7 +648,7 @@ pub mod tests {
645648
timestamp: 1731593928,
646649
status: IdentityProofStatus::Success,
647650
status_last_checked_timestamp: 1731593929,
648-
block_id: 2,
651+
block_id: BlockId::next_from_previous_block_id(&BlockId::first()),
649652
}))
650653
});
651654
let service = get_service(ctx);

crates/bcr-ebill-api/src/service/identity_service.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use bcr_ebill_core::city::City;
2323
use bcr_ebill_core::country::Country;
2424
use bcr_ebill_core::date::Date;
2525
use bcr_ebill_core::email::Email;
26+
use bcr_ebill_core::hash::Sha256Hash;
2627
use bcr_ebill_core::identification::Identification;
2728
use bcr_ebill_core::identity::validation::{validate_create_identity, validate_update_identity};
2829
use bcr_ebill_core::identity::{ActiveIdentityState, IdentityType};
@@ -187,7 +188,7 @@ impl IdentityService {
187188
public_key: &PublicKey,
188189
relay_url: &url::Url,
189190
) -> Result<File> {
190-
let file_hash = util::sha256_hash(file_bytes);
191+
let file_hash = Sha256Hash::from_bytes(file_bytes);
191192
let encrypted = util::crypto::encrypt_ecies(file_bytes, public_key)?;
192193
let nostr_hash = self.file_upload_client.upload(relay_url, encrypted).await?;
193194
info!("Saved identity file {file_name} with hash {file_hash} for identity {node_id}");
@@ -675,7 +676,7 @@ impl IdentityServiceApi for IdentityService {
675676
.download(nostr_relay, &file.nostr_hash)
676677
.await?;
677678
let decrypted = util::crypto::decrypt_ecies(&file_bytes, private_key)?;
678-
let file_hash = util::sha256_hash(&decrypted);
679+
let file_hash = Sha256Hash::from_bytes(&decrypted);
679680
if file_hash != file.hash {
680681
error!("Hash for identity file {file_name} did not match uploaded file");
681682
return Err(super::Error::NotFound);

0 commit comments

Comments
 (0)