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

ContractId round-trip fails for some values #209

Closed
nicbus opened this issue Jan 23, 2024 · 5 comments
Closed

ContractId round-trip fails for some values #209

nicbus opened this issue Jan 23, 2024 · 5 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@nicbus
Copy link
Contributor

nicbus commented Jan 23, 2024

Some values come up different after a ContractId round-trip.

By round-trip I mean:

  • copy_from_slice
  • to_string
  • from_str

Here's an example:

use rgbstd::contract::ContractId;
use std::str::FromStr;

fn main() {
    let contract_id = ContractId::copy_from_slice([0u8; 32]).unwrap();

    let contract_id_string = contract_id.to_string();
    println!("contract ID string: {contract_id_string}");

    let result = ContractId::from_str(&contract_id_string);
    println!("contract ID result: {result:?}");
}

This outputs:

contract ID string: rgb:1111111-111111111-111111111-11111112k-9zwp
contract ID result: Err(Unparsable("rgb:1111111-111111111-111111111-11111112k-9zwp"))

The same error is returned by version 0.10.9 (using from_slice instead of copy_from_slice).

@dr-orlovsky dr-orlovsky transferred this issue from RGB-WG/rgb-std Feb 19, 2024
@dr-orlovsky
Copy link
Member

Ok, I have finally figured out what's going on.

It is the only zero string which has significantly shorter Base58 encoding (32 chars instead of 43/44), so it doesn't has a valid chunking and fails to parse.

The issue is not trivial to solve: we need to refactor all the way the chunking works. For v0.11 I think the better way is to leave the things as they are: contract ids containing all-0 (or starting with at least 16 initial zeros to make length short enough to fail chunking) are all invalid anyway and can't be generated in a real-world scenarious.

@nicbus
Copy link
Contributor Author

nicbus commented Feb 20, 2024

I agree there's no urgency in fixing this.

@dr-orlovsky dr-orlovsky modified the milestones: v0.11.0, v0.12.0 Feb 26, 2024
@dr-orlovsky
Copy link
Member

After many attempts to fix it I was able to find an ultimate solution: switch from Base58 to Base64.

Base64 has constant size for a given source binary data - unlike Base58. The only disadvantage is the use of "/" and "-" symbols in standard encoding; but I did a modification to use "@" and "$" instead, and it seems to be working great.

I played with it in ssi for identities and signatures (which we will use in RGB as well) and developed a dedicated baid64 crate to support URL prefixes, parsing, mnemonics etc, like we had in Baid58.

Switching to it in all develop branches and will see how it will go.

@dr-orlovsky dr-orlovsky self-assigned this Apr 25, 2024
@dr-orlovsky dr-orlovsky added the bug Something isn't working label Apr 25, 2024
@dr-orlovsky dr-orlovsky modified the milestones: v0.12.0, v0.11.0 Apr 25, 2024
@yanganto
Copy link
Contributor

yanganto commented Apr 26, 2024

Hi @dr-orlovsky,

May I try this? It is not an urgent issue, I can take this chance to learn more, go through the Contract stuff, and make more test cases.

@dr-orlovsky
Copy link
Member

Fixed by moving in beta-6 to baid64 encoding instead of baid58

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

No branches or pull requests

3 participants