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

cip14 fingerprint #367

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 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 @@ -29,7 +29,7 @@ serde_json = { version = "1.0.81", features = ["arbitrary_precision"] }
strum = "0.24"
strum_macros = "0.24"
prometheus_exporter = { version = "0.8.4", default-features = false }

cryptoxide = { git = "https://github.com/typed-io/cryptoxide.git", branch="master", features = ["blake2"]}
# feature logs
file-rotate = { version = "0.6.0", optional = true }

Expand Down
1 change: 1 addition & 0 deletions src/mapper/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl EventWriter {
asset: asset.to_hex(),
asset_ascii: String::from_utf8(asset.to_vec()).ok(),
amount,
fingerprint: crate::utils::cip14::cip14_fingerprint(&policy, &asset),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub struct OutputAssetRecord {
pub asset: String,
pub asset_ascii: Option<String>,
pub amount: u64,
pub fingerprint : Option<String>,
}

impl From<OutputAssetRecord> for EventData {
Expand Down
22 changes: 22 additions & 0 deletions src/utils/bech32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ impl Bech32Config {
address_hrp: info.address_hrp.to_owned(),
}
}

pub(crate) fn for_cip14() -> Self {
Self {
address_hrp: "asset".to_string(),
}
}
}

impl Default for Bech32Config {
Expand Down Expand Up @@ -47,6 +53,22 @@ impl Bech32Provider {

Ok(enc)
}

pub fn encode_cip14(&self, data: &[u8]) -> Option<String> {
match bech32::encode(
&self.0.address_hrp,
data.to_base32(),
bech32::Variant::Bech32,
){
Ok(ok) => {
Some(ok)
},
Err(e) => {
log::error!("Could not encode cip14 assetname: '{}'",e.to_string());
None
}
}
}
}

#[cfg(test)]
Expand Down
120 changes: 120 additions & 0 deletions src/utils/cip14.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//! An utility to build fingerprints for Cardano assets CIP14
//! https://cips.cardano.org/cips/cip14/
//!
use cryptoxide::{digest::Digest, blake2b::Blake2b};
use crate::utils::bech32::{Bech32Config,Bech32Provider};

pub fn blake2b160(data: &[u8]) -> [u8;20] {
Copy link
Collaborator

@SmaugPool SmaugPool Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code already exists in Pallas: https://github.com/txpipe/pallas/blob/main/pallas-crypto/src/hash/hasher.rs.

You just have to add:

common_hasher!(160);

at https://github.com/txpipe/pallas/blob/1482303616e9ba68f322eceb9ed03118efee28d5/pallas-crypto/src/hash/hasher.rs#L109 and some comments in the header, then you can do something like:

use pallas::crypto::hash::{Hash, Hasher};

fn asset_fingerprint(policy: &ByteVec, asset: &ByteVec) -> String {
    let mut hasher = Hasher::<160>::new();
    hasher.input(policy);
    hasher.input(asset);
    bech32::encode(
        &"asset".to_string(),
        hasher.finalize().to_base32(),
        bech32::Variant::Bech32,
    )
    .unwrap()
}

Likely returning a Result instead of the unwrap here.

let mut out = [0u8; 20];
let mut context = Blake2b::new(20);
context.input(data);
context.result(&mut out);
Blake2b::blake2b(&mut out, data, &[]);
out
}

pub fn cip14_fingerprint(p: &Vec::<u8>, a: &Vec::<u8>) -> Option<String> {
let data = [&p[..],&a[..]].concat();
let hash = blake2b160(&data);
let bech32_provider = Bech32Provider::new(Bech32Config::for_cip14());
let fingerprint = bech32_provider.encode_cip14(hash.as_slice());
log::debug!("CIP14 Fingerprint: {:?}",fingerprint);
fingerprint
}

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

#[test]
fn cip14_ok_1() {
let policy = hex::decode("bb3ce45d5272654e58ad076f114d8f683ae4553e3c9455b18facfea1").unwrap();
let assetname =hex::decode("4261627943726f63202332323237").unwrap();

let bech32 = cip14_fingerprint(&policy,&assetname).unwrap();
log::debug!("{}",bech32);
assert_eq!(bech32, "asset1et8j5whwuqrxvdyxfh4grmmrx4exeg4juzx88z");
}

#[test]
fn cip14_ok_2() {
let policy = hex::decode("7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373").unwrap();
let assetname =hex::decode("").unwrap();

let bech32 = cip14_fingerprint(&policy,&assetname).unwrap();
log::debug!("{}",bech32);
assert_eq!(bech32, "asset1rjklcrnsdzqp65wjgrg55sy9723kw09mlgvlc3");
}

#[test]
fn cip14_ok_3() {
let policy = hex::decode("7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc37e").unwrap();
let assetname =hex::decode("").unwrap();

let bech32 = cip14_fingerprint(&policy,&assetname).unwrap();
log::debug!("{}",bech32);
assert_eq!(bech32, "asset1nl0puwxmhas8fawxp8nx4e2q3wekg969n2auw3");
}

#[test]
fn cip14_ok_4() {
let policy = hex::decode("1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209").unwrap();
let assetname =hex::decode("").unwrap();

let bech32 = cip14_fingerprint(&policy,&assetname).unwrap();
log::debug!("{}",bech32);
assert_eq!(bech32, "asset1uyuxku60yqe57nusqzjx38aan3f2wq6s93f6ea");
}

#[test]
fn cip14_ok_5() {
let policy = hex::decode("7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373").unwrap();
let assetname =hex::decode("504154415445").unwrap();

let bech32 = cip14_fingerprint(&policy,&assetname).unwrap();
log::debug!("{}",bech32);
assert_eq!(bech32, "asset13n25uv0yaf5kus35fm2k86cqy60z58d9xmde92");
}

#[test]
fn cip14_ok_6() {
let policy = hex::decode("1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209").unwrap();
let assetname =hex::decode("504154415445").unwrap();

let bech32 = cip14_fingerprint(&policy,&assetname).unwrap();
log::debug!("{}",bech32);
assert_eq!(bech32, "asset1hv4p5tv2a837mzqrst04d0dcptdjmluqvdx9k3");
}

#[test]
fn cip14_ok_7() {
let policy = hex::decode("1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209").unwrap();
let assetname =hex::decode("7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373").unwrap();

let bech32 = cip14_fingerprint(&policy,&assetname).unwrap();
log::debug!("{}",bech32);
assert_eq!(bech32, "asset1aqrdypg669jgazruv5ah07nuyqe0wxjhe2el6f");
}

#[test]
fn cip14_ok_8() {
let policy = hex::decode("7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373").unwrap();
let assetname =hex::decode("1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209").unwrap();

let bech32 = cip14_fingerprint(&policy,&assetname).unwrap();
log::debug!("{}",bech32);
assert_eq!(bech32, "asset17jd78wukhtrnmjh3fngzasxm8rck0l2r4hhyyt");
}

#[test]
fn cip14_ok_9() {
let policy = hex::decode("7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373").unwrap();
let assetname =hex::decode("0000000000000000000000000000000000000000000000000000000000000000").unwrap();

let bech32 = cip14_fingerprint(&policy,&assetname).unwrap();
log::debug!("{}",bech32);
assert_eq!(bech32, "asset1pkpwyknlvul7az0xx8czhl60pyel45rpje4z8w");

}

}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub mod throttle;
pub(crate) mod bech32;
pub(crate) mod retry;
pub(crate) mod time;
pub(crate) mod cip14;

mod facade;

Expand Down