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

[BUG] "Unexpected end of data" during bigintToBytes() #283

Closed
theblockstalk opened this issue May 16, 2023 · 2 comments · Fixed by #288
Closed

[BUG] "Unexpected end of data" during bigintToBytes() #283

theblockstalk opened this issue May 16, 2023 · 2 comments · Fixed by #288
Labels
bug Something isn't working pinned a known issue or feature that should not be closed by bots released

Comments

@theblockstalk
Copy link
Contributor

theblockstalk commented May 16, 2023

Current Behavior

biginToBytes() converts a bigInt to a hex string, which sometimes misses left padding, which causes a conversion error

Expected Behavior

biginToBytes() converts a bigInt to a hex string

Failure Information

Solution:

export function bigintToBytes(n: bigint): Uint8Array {
    let b64 = n.toString(16);

    // Pad an extra '0' if the hex string is an odd length
    if (b64.length % 2 !== 0) {
        b64 = `0${b64}`;
    }

    return u8a.fromString(b64, 'base16');
}

Issue we found: Tonomy-Foundation/Tonomy-App-Websites#76
Solution we made: https://github.com/Tonomy-Foundation/Tonomy-ID-SDK/pull/228/files#diff-4335ca2354d2963bc461d6d160ca76c2ac0bd3524a6a8eaba6c627407ca1cf41R26

Steps to Reproduce

const bi = 1881146970754576322752261068397796891246589699629597037555588131642783231506n
const biBytes = bigintToBytes(bi);
const base64 = bytesToBase64(biBytes);
expect(base64).toBe('BCixAySH6XqSNMR6MVnd4SCluKq3Ey5RQIy0/0Eu7hI=');
// fails with error "Unexpected end of data"

const bi = 100968908336250941489582664670319762383316987426946165788206218268821633081179n
const biBytes = bigintToBytes(bi);
const base64 = bytesToBase64(biBytes);
expect(base64).toBe('3zpgfkpIN/0k/xkychS26ElYP4Bnb24RcYACzsbzn1s=');
// succeeds

Environment Details

  • node version: v18.12.1
  • OS Version: ubuntu 20.04
@theblockstalk theblockstalk added the bug Something isn't working label May 16, 2023
@mirceanis
Copy link
Member

Great catch, @theblockstalk!
Since you already have the solution ready, would you mind adding a PR with the fix and a test for it?

@mirceanis mirceanis added the pinned a known issue or feature that should not be closed by bots label May 17, 2023
uport-automation-bot pushed a commit that referenced this issue Jun 26, 2023
## [7.2.3](7.2.2...7.2.3) (2023-06-26)

### Bug Fixes

* add padding to bigints whose byte-length is expected ([#288](#288)) ([bfa8e71](bfa8e71)), closes [#283](#283)
@uport-automation-bot
Copy link
Collaborator

🎉 This issue has been resolved in version 7.2.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pinned a known issue or feature that should not be closed by bots released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants