decentralized-identity / did-jwt

Create and verify DID verifiable JWT's in Javascript
Apache License 2.0
331 stars 70 forks source link

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

Closed theblockstalk closed 1 year ago

theblockstalk commented 1 year ago

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: https://github.com/Tonomy-Foundation/Tonomy-App-Websites/issues/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

mirceanis commented 1 year ago

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

uport-automation-bot commented 1 year ago

:tada: This issue has been resolved in version 7.2.3 :tada:

The release is available on:

Your semantic-release bot :package::rocket: