Repository navigation
fix(schema): preserve precision when serializing integers - #190
Conversation
Coverage Report for CI Build 43896967037Warning Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes. Coverage increased (+0.2%) to 74.432%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
donskov
left a comment
There was a problem hiding this comment.
I checked each comment below by running the old (new asn1js.Integer({ value: +value })) and new toASN against asn1js 4.0.0.
| return asn1js.Integer.fromBigInt(value); | ||
| } catch (error) { | ||
| // Preserve legacy numeric syntax only when it produces a safe integer. | ||
| if (!(error instanceof SyntaxError) || !Number.isSafeInteger(+value)) { |
There was a problem hiding this comment.
Regression: exact integers above 2^53 written in exponent or decimal form used to encode and now throw.
| input | before | after |
|---|---|---|
"1e20" |
056bc75e2d63100000 |
SyntaxError |
"9007199254740992.0" |
20000000000000 |
SyntaxError |
"18446744073709551616.0" |
010000000000000000 |
SyntaxError |
BigInt() rejects these, and Number.isSafeInteger(+value) is false, so the error is rethrown. If this is intended, it's a breaking change (fix!: / BREAKING CHANGE:).
There was a problem hiding this comment.
Fixed in 2979f77. Non-integer string forms now use the legacy conversion, restoring all three examples. Added exact DER regressions.
| if (typeof value === "string") { | ||
| try { | ||
| return asn1js.Integer.fromBigInt(value); | ||
| } catch (error) { |
There was a problem hiding this comment.
Error type changed: invalid strings now throw SyntaxError. Before, they threw RangeError.
"invalid", "1.5", "1_000": before → RangeError: Integer value must be an integer. After → SyntaxError: Cannot convert ... to a BigInt. Numbers and undefined still throw RangeError, so the error type now depends on the input type, and existing instanceof RangeError handlers stop catching it.
There was a problem hiding this comment.
Fixed in 2979f77. Invalid numeric strings again go through the legacy constructor and throw RangeError; tests cover the reported inputs.
| toASN: (value: IntegerConverterType) => { | ||
| if (typeof value === "string") { | ||
| try { | ||
| return asn1js.Integer.fromBigInt(value); |
There was a problem hiding this comment.
Returned object shape differs for strings of 4 bytes or more. fromBigInt builds the integer from valueHex, so the result is hex-only:
toASN("2147483647") → valueDec=0, isHexOnly=true
toASN(2147483647) → valueDec=2147483647, isHexOnly=false
The DER is identical. This only affects callers that use the exported converter directly and read valueDec or toJSON().
There was a problem hiding this comment.
Fixed in 2979f77. Exactly representable decimal strings retain the legacy constructor and metadata, verified with valueDec, isHexOnly, and toJSON() tests. Other decimal integers keep exact BigInt encoding.
| ? value.valueBlock.toString() // use string format | ||
| : value.valueBlock.valueDec, // use number format | ||
| toASN: (value: IntegerConverterType) => new asn1js.Integer({ value: +value }), | ||
| toASN: (value: IntegerConverterType) => { |
There was a problem hiding this comment.
Suggestion: check the string format first instead of catching an exception. This avoids the regression and the error-type change above:
toASN: (value: IntegerConverterType) =>
typeof value === "string" && /^\s*[+-]?\d+\s*$/.test(value)
? asn1js.Integer.fromBigInt(value)
: new asn1js.Integer({ value: +value }),Plain decimal strings get exact encoding. Anything else (1e20, 0x10, 1.0, invalid input) behaves exactly as it did before this PR.
There was a problem hiding this comment.
Applied the format check in 2979f77, with an additional exact-value comparison to preserve legacy object metadata where possible. Other string forms retain their original conversion behavior; fromASN is unchanged.
| const obj = new RepeatedTest(); | ||
| delete obj.values[0]; | ||
|
|
||
| expect(() => AsnConvert.serialize(obj)).toThrow(); |
There was a problem hiding this comment.
Bare toThrow() passes on any error. Assert the type, as the undefined test above does: toThrow(RangeError).
There was a problem hiding this comment.
Updated to toThrow(RangeError) in 2979f77.
Summary
Preserve the exact value of decimal strings when serializing ASN.1 INTEGERs. Previously, converting a parsed integer string to a JavaScript number rounded values outside the safe integer range, changing the encoded DER on a round trip.
Use
asn1js.Integer.fromBigIntfor strings while keeping the existing number serialization path. Add DER and round-trip coverage for signed values, safe-integer boundaries, and integers larger than 64 bits.Closes #189
Validation
npm run lintnpm run format:checknpm test(462 passed, 1 skipped)npm run coveragenpm run build(with a temporary npm cache)npm run typecheck