-
Notifications
You must be signed in to change notification settings - Fork 71
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
Sbcdn
wants to merge
2
commits into
txpipe:main
Choose a base branch
from
Sbcdn:cip14-impl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
cip14 fingerprint #367
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] { | ||
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"); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
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:
Likely returning a
Result
instead of theunwrap
here.