agent-js
agent-js copied to clipboard
fix: Cbor.encode(BigInt(n)) possible failure
Fix Cbor.encode(BigInt(n))
possible failure
Description
When use Cbor.encode
, if passed value is BigInt, first will run the BigInt encoder and transform BigInt value to be a hex string. However this BigInt(n).toString(16)
would probrably generate a hex string length not to be even, which won't pass the RegTest in the fromHex
function.
const hexRe = new RegExp(/^([0-9A-F]{2})*$/i);
Fixes # (issue)
Added a zero-padding while passing hex string length is not even.
export function fromHex(hex: string): ArrayBuffer {
//FIXME: always padding hex string length to be even
// then run the reg test
if (hex.length % 2 !== 0) {
hex = '0' + hex;
}
if (!hexRe.test(hex)) {
throw new Error('Invalid hexadecimal string.');
}
const buffer = [...hex]
.reduce((acc, curr, i) => {
// tslint:disable-next-line:no-bitwise
acc[(i / 2) | 0] = (acc[(i / 2) | 0] || '') + curr;
return acc;
}, [] as string[])
.map(x => Number.parseInt(x, 16));
return new Uint8Array(buffer).buffer;
}
How Has This Been Tested?
test cases are in cbor.tests.ts
Checklist:
- [x] My changes follow the guidelines in CONTRIBUTING.md.
- [x] The title of this PR complies with Conventional Commits.
- [ ] I have edited the CHANGELOG accordingly.
- [ ] I have made corresponding changes to the documentation.
Dear @neeboo,
In order to potentially merge your code in this open-source repository and therefore proceed with your contribution, we need to have your approval on DFINITY's CLA[^1].
If you decide to agree with it, please visit this issue and read the instructions there.
— The DFINITY Foundation [^1]: Contributor License Agreement
Dear @neeboo,
In order to potentially merge your code in this open-source repository and therefore proceed with your contribution, we need to have your approval on DFINITY's CLA[^1].
If you decide to agree with it, please visit this issue and read the instructions there.
— The DFINITY Foundation [^1]: Contributor License Agreement
When serializing objects that contain BigInt
s, I'm getting the error:
Invalid hexadecimal string.
Has some progress been made on this?