Skip to content

Commit

Permalink
feat(core): accept hex string on for Coll[Byte] serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
capt-nemo429 committed Dec 14, 2022
1 parent fe62741 commit 0253b2a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/common/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const TOKEN_ID_HEX_LENGTH = 64;
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./utils";
export * from "./types";
export * from "./constants";
6 changes: 4 additions & 2 deletions packages/core/src/serializer/sigma/constantSerializer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hexToBytes } from "@noble/hashes/utils";
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
import { stringToBytes } from "@scure/base";
import {
collBoolTestVectors,
Expand Down Expand Up @@ -114,7 +114,9 @@ describe("SColl serialization", () => {

it("Should serialize 'Coll[SByte]'", () => {
for (const tv of collByteTestVectors) {
expect(SConstant(SColl(SByte, stringToBytes("utf8", tv.string)))).toBe(tv.hex);
const bytes = stringToBytes("utf8", tv.string);
expect(SConstant(SColl(SByte, bytes))).toBe(tv.hex);
expect(SConstant(SColl(SByte, bytesToHex(bytes)))).toBe(tv.hex);
}
});

Expand Down
19 changes: 16 additions & 3 deletions packages/core/src/serializer/sigma/dataSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { hexToBytes } from "@noble/hashes/utils";
import { vlqEncode } from "../vlq";
import { SigmaByteReader } from "./sigmaByteReader";
import { SigmaByteWriter } from "./sigmaByteWriter";
Expand Down Expand Up @@ -46,15 +47,27 @@ export class DataSerializer {
throw Error("Not implemented");
}
} else if (isColl(data)) {
buffer.writeBytes(vlqEncode(data.value.length));
if (typeof data.value === "string") {
buffer.writeBytes(vlqEncode(data.value.length / 2));
} else {
buffer.writeBytes(vlqEncode(data.value.length));
}

switch (data.elementsType) {
case SigmaTypeCode.Boolean:
buffer.writeBits(data.value as boolean[]);
break;
case SigmaTypeCode.Byte:
buffer.writeBytes(Uint8Array.from(data.value as number[]));
case SigmaTypeCode.Byte: {
let bytes!: Uint8Array;
if (typeof data.value === "string") {
bytes = hexToBytes(data.value);
} else {
bytes = Uint8Array.from(data.value as number[]);
}

buffer.writeBytes(bytes);
break;
}
default:
for (let i = 0; i < data.value.length; i++) {
DataSerializer.serialize(
Expand Down

0 comments on commit 0253b2a

Please sign in to comment.