Skip to content

Make AssetId::from_inner a const function #225

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

Merged
merged 1 commit into from
Mar 24, 2025

Conversation

pavel-kokolemin
Copy link
Contributor

This will allow constructing AssetId values like this:

const TESTNET_ASSET: elemets::AssetId = const_asset_id("144c654344aa716d6f3abcc1ca90e5641e4e2a7f633bc09fe3baf64585819a49");

/// Panics if the asset id string has wrong length or format
pub const fn const_asset_id(s: &str) -> elements::AssetId {
    let mut data: [u8; 32] = hex_literal::decode::<32>(&[s.as_bytes()]);

    let mut left = 0;
    let mut right = data.len() - 1;
    while left < right {
        let tmp = data[left];
        data[left] = data[right];
        data[right] = tmp;
        left += 1;
        right -= 1;
    }

    elements::AssetId::from_inner(bitcoin::hashes::sha256::Midstate(data))
}

It's a bit of a hack because hex_literal::decode should not be used directly.

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 1f3b402; successfully ran local tests; nice, good call

@apoelstra apoelstra merged commit 899b294 into ElementsProject:master Mar 24, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants