Skip to content

Commit

Permalink
Refactor the UTxO selection library to simplify the usage
Browse files Browse the repository at this point in the history
  • Loading branch information
gostkin committed Mar 21, 2023
1 parent cd978a9 commit ea30d5c
Show file tree
Hide file tree
Showing 33 changed files with 1,576 additions and 1,398 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
@@ -1,7 +1,6 @@
[workspace]
members = [
"blockchain-source",
"cardano-utils",
"core",
"deps",
"fraos",
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli-tools/cip1852-first-address/Cargo.toml
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
anyhow = "1.0.66"
cardano-multiplatform-lib = "3.1.1"
cardano-multiplatform-lib = { version = "3.1.3" }
hex = "0.4.3"
miniserde = "0.1.27"
reqwest = { version = "0.11.13", features = ["blocking"] }
Expand Down
18 changes: 0 additions & 18 deletions cardano-utils/Cargo.toml

This file was deleted.

189 changes: 0 additions & 189 deletions cardano-utils/src/conversion.rs

This file was deleted.

2 changes: 1 addition & 1 deletion core/Cargo.toml
Expand Up @@ -11,7 +11,7 @@ deps = { version = "0.1.0", path = "../deps" }

anyhow = { version = "1.0" }
bech32 = { version = "0.9" }
cardano-multiplatform-lib = { git = "https://github.com/dcSpark/cardano-multiplatform-lib.git", rev = "a41264b88c9b48ac890c2d046fe916f224fccabb" }
cardano-multiplatform-lib = { version = "3.1.3" }
cryptoxide = {version = "0.4.2" }
hex = { version = "0.4" }
imbl = { version = "2.0" }
Expand Down
46 changes: 27 additions & 19 deletions cardano-utils/src/cip14.rs → core/src/cardano_utils/cip14.rs
@@ -1,11 +1,9 @@
use crate::{AssetName, PolicyId, TokenId};
use anyhow::{anyhow, Context as _, Result};
use bech32::ToBase32;
use cryptoxide::hashing::blake2b::Blake2b;
use dcspark_core::{AssetName, PolicyId, TokenId};

const HRP: &str = "asset";
use cryptoxide::hashing::blake2b::Blake2b;

pub fn fingerprint(policy: &PolicyId, name: &AssetName) -> Result<TokenId> {
fn fingerprint_hash(policy: &PolicyId, name: &AssetName) -> anyhow::Result<[u8; 20]> {
let mut buf = vec![0u8; 28 + name.as_ref().len() / 2];
hex::decode_to_slice(policy.as_ref(), &mut buf[..28])
.with_context(|| anyhow!("Failed to decode PolicyId: {policy}"))?;
Expand All @@ -17,20 +15,30 @@ pub fn fingerprint(policy: &PolicyId, name: &AssetName) -> Result<TokenId> {
let mut out = [0; 20];
b2b.finalize_at(&mut out);

bech32::encode(HRP, out.to_base32(), bech32::Variant::Bech32)
// this probably can't fail
.context("Couldn't compute bech32 asset fingerprint")
.map(TokenId::new)
Ok(out)
}

pub fn fingerprint(policy: &PolicyId, name: &AssetName) -> Result<TokenId> {
fingerprint_hash(policy, name).map(|hash| TokenId::new(format!("{:0<64}", hex::encode(hash))))
}

#[cfg(test)]
mod tests {
use super::*;
use bech32::ToBase32;

const HRP: &str = "asset";

pub fn fingerprint_bech32(policy: &PolicyId, name: &AssetName) -> Result<String> {
let bytes = fingerprint_hash(policy, name)?;
bech32::encode(HRP, bytes.to_base32(), bech32::Variant::Bech32)
.context("Couldn't compute bech32 asset fingerprint")
}

struct TestVector {
policy_id: PolicyId,
asset_name: AssetName,
asset_fingerprint: TokenId,
asset_fingerprint: &'static str,
}

const TESTS: &[TestVector] = &[
Expand All @@ -39,35 +47,35 @@ mod tests {
"7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
),
asset_name: AssetName::new_static(""),
asset_fingerprint: TokenId::new_static("asset1rjklcrnsdzqp65wjgrg55sy9723kw09mlgvlc3"),
asset_fingerprint: "asset1rjklcrnsdzqp65wjgrg55sy9723kw09mlgvlc3",
},
TestVector {
policy_id: PolicyId::new_static(
"7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc37e",
),
asset_name: AssetName::new_static(""),
asset_fingerprint: TokenId::new_static("asset1nl0puwxmhas8fawxp8nx4e2q3wekg969n2auw3"),
asset_fingerprint: "asset1nl0puwxmhas8fawxp8nx4e2q3wekg969n2auw3",
},
TestVector {
policy_id: PolicyId::new_static(
"1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209",
),
asset_name: AssetName::new_static(""),
asset_fingerprint: TokenId::new_static("asset1uyuxku60yqe57nusqzjx38aan3f2wq6s93f6ea"),
asset_fingerprint: "asset1uyuxku60yqe57nusqzjx38aan3f2wq6s93f6ea",
},
TestVector {
policy_id: PolicyId::new_static(
"7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
),
asset_name: AssetName::new_static("504154415445"),
asset_fingerprint: TokenId::new_static("asset13n25uv0yaf5kus35fm2k86cqy60z58d9xmde92"),
asset_fingerprint: "asset13n25uv0yaf5kus35fm2k86cqy60z58d9xmde92",
},
TestVector {
policy_id: PolicyId::new_static(
"1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209",
),
asset_name: AssetName::new_static("504154415445"),
asset_fingerprint: TokenId::new_static("asset1hv4p5tv2a837mzqrst04d0dcptdjmluqvdx9k3"),
asset_fingerprint: "asset1hv4p5tv2a837mzqrst04d0dcptdjmluqvdx9k3",
},
TestVector {
policy_id: PolicyId::new_static(
Expand All @@ -76,7 +84,7 @@ mod tests {
asset_name: AssetName::new_static(
"7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
),
asset_fingerprint: TokenId::new_static("asset1aqrdypg669jgazruv5ah07nuyqe0wxjhe2el6f"),
asset_fingerprint: "asset1aqrdypg669jgazruv5ah07nuyqe0wxjhe2el6f",
},
TestVector {
policy_id: PolicyId::new_static(
Expand All @@ -85,7 +93,7 @@ mod tests {
asset_name: AssetName::new_static(
"1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209",
),
asset_fingerprint: TokenId::new_static("asset17jd78wukhtrnmjh3fngzasxm8rck0l2r4hhyyt"),
asset_fingerprint: "asset17jd78wukhtrnmjh3fngzasxm8rck0l2r4hhyyt",
},
TestVector {
policy_id: PolicyId::new_static(
Expand All @@ -94,14 +102,14 @@ mod tests {
asset_name: AssetName::new_static(
"0000000000000000000000000000000000000000000000000000000000000000",
),
asset_fingerprint: TokenId::new_static("asset1pkpwyknlvul7az0xx8czhl60pyel45rpje4z8w"),
asset_fingerprint: "asset1pkpwyknlvul7az0xx8czhl60pyel45rpje4z8w",
},
];

#[test]
fn test_vectors() {
for (index, test) in TESTS.iter().enumerate() {
let computed = fingerprint(&test.policy_id, &test.asset_name).unwrap();
let computed = fingerprint_bech32(&test.policy_id, &test.asset_name).unwrap();

assert_eq!(
computed, test.asset_fingerprint,
Expand Down
5 changes: 3 additions & 2 deletions cardano-utils/src/lib.rs → core/src/cardano_utils/mod.rs
@@ -1,4 +1,5 @@
pub mod cip14;
pub mod conversion;
mod cip14;
pub mod multisig_plan;
pub mod network_id;

pub use cip14::fingerprint;
File renamed without changes.
@@ -1,4 +1,4 @@
use crate::multisig_plan::MultisigPlan;
use crate::cardano_utils::multisig_plan::MultisigPlan;
use cardano_multiplatform_lib::builders::tx_builder::{
TransactionBuilderConfig, TransactionBuilderConfigBuilder,
};
Expand Down
2 changes: 2 additions & 0 deletions core/src/lib.rs
Expand Up @@ -3,6 +3,7 @@ mod asset_name;
mod balance;
mod block_id;
mod block_number;
mod cardano_utils;
pub mod error;
mod number_visitor;
mod output_index;
Expand All @@ -19,6 +20,7 @@ pub use asset_name::*;
pub use balance::*;
pub use block_id::*;
pub use block_number::*;
pub use cardano_utils::*;
pub use number_visitor::*;
pub use output_index::*;
pub use policy_id::*;
Expand Down

0 comments on commit ea30d5c

Please sign in to comment.