Skip to content

Commit 1d5195a

Browse files
committed
Fix bigint encoding
1 parent e3a4167 commit 1d5195a

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

packages/seroval/src/binary/deserializer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ async function deserializeRef(
185185
) {
186186
const ref = await deserializeUint(ctx);
187187
if (expected != null) {
188-
const marker = ctx.marker.get(expected);
188+
const marker = ctx.marker.get(ref);
189189
if (marker == null) {
190190
throw new SerovalMalformedBinaryTypeError(type);
191191
}
@@ -225,6 +225,7 @@ async function deserializeString(ctx: DeserializerContext) {
225225

226226
async function deserializeBigint(ctx: DeserializerContext) {
227227
const id = await deserializeId(ctx, SerovalBinaryType.BigInt);
228+
const sign = await deserializeByte(ctx);
228229
// Check if the value exists
229230
const current = (
230231
await deserializeRef(
@@ -233,7 +234,8 @@ async function deserializeBigint(ctx: DeserializerContext) {
233234
SerovalBinaryType.String,
234235
)
235236
).value;
236-
upsert(ctx, id, decodeBigint(current as string));
237+
const value = decodeBigint(current as string);
238+
upsert(ctx, id, sign ? -value : value);
237239
}
238240

239241
async function deserializeWKSymbol(ctx: DeserializerContext) {

packages/seroval/src/binary/encoder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function decodeBigint(value: string): bigint {
9898
let hex = '';
9999
for (let i = 0, len = value.length; i < len; i++) {
100100
const code = value.charCodeAt(i);
101-
hex += code.toString(16);
101+
hex += code.toString(16).padStart(2, '0');
102102
}
103103
return BigInt('0x' + hex);
104104
}

packages/seroval/src/binary/nodes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export type SerovalStringNode = [
118118
export type SerovalBigintNode = [
119119
type: SerovalBinaryType.BigInt,
120120
id: Uint8Array,
121+
isNegative: number,
121122
// value ref
122123
value: Uint8Array,
123124
];

packages/seroval/src/binary/serializer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ function serializeBigInt(ctx: SerializerContext, value: bigint) {
177177
onSerialize(ctx, [
178178
SerovalBinaryType.BigInt,
179179
id,
180-
serialize(ctx, encodeBigint(value)),
180+
value < 0 ? 1 : 0,
181+
serialize(ctx, encodeBigint(value < 0 ? -value : value)),
181182
]);
182183
return id;
183184
}

0 commit comments

Comments
 (0)