Skip to content

Commit

Permalink
Merge pull request #32 from fleet-sdk/str-type-rep
Browse files Browse the repository at this point in the history
Add type string representation
  • Loading branch information
arobsn authored Aug 14, 2023
2 parents efc57f0 + e532fb8 commit 9f0363b
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 40 deletions.
1 change: 0 additions & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"ignore": ["*/_*"],
"linked": [["*/core", "*/common", "*/wallet", "*/mock-chain", "*/compiler", "*/crypto"]],
"access": "public",
"baseBranch": "master",
Expand Down
5 changes: 5 additions & 0 deletions .changeset/slow-lions-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fleet-sdk/serializer": minor
---

Add type string representation.
36 changes: 18 additions & 18 deletions packages/serializer/src/_test-vectors/constantVectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function buildCollVectors<T>(
}

export const collVectors: GenericTypeTestVector[] = [
...buildCollVectors("[SBool]", SBool, [
...buildCollVectors("SColl[SBool]", SBool, [
{
hex: "0d0c010e",
value: [true, false, false, false, false, false, false, false, false, true, true, true]
Expand All @@ -173,7 +173,7 @@ export const collVectors: GenericTypeTestVector[] = [
value: [false, true, true, true, false, false, false, false, true, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, true, false, false, false, false, true, true, false, true, false, false, false, false, true, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false] /* prettier-ignore */
}
]),
...buildCollVectors("[SByte]", SByte, [
...buildCollVectors("SColl[SByte]", SByte, [
{ hex: "0e0a46656d616c6520233035", value: utf8.decode("Female #05") },
{ hex: "0e0130", value: utf8.decode("0") },
{ hex: "0e00", value: u8a([]) },
Expand All @@ -186,7 +186,7 @@ export const collVectors: GenericTypeTestVector[] = [
value: utf8.decode('{"721":{"ergoat-00346":{"index":346,"generation":1,"background":"Psycho","back_accessory":"Green Parrot","body":"Purple","clothes":"White ERGoat Shirt","mouth":"Gritted Teeth","eyes":"Angry Red","wield":"Spirit Gun","accessory":"Silver Medal","head":"Cowboy Hat","earring":"None"}}}') /* prettier-ignore */
}
]),
...buildCollVectors("[SShort]", SShort, [
...buildCollVectors("SColl[SShort]", SShort, [
{ hex: "0f00", value: [] },
{ hex: "0f0102", value: [1] },
{ hex: "0f020000", value: [0, 0] },
Expand Down Expand Up @@ -223,7 +223,7 @@ export const collVectors: GenericTypeTestVector[] = [
value: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1] /* prettier-ignore */
}
]),
...buildCollVectors("[SInt]", SInt, [
...buildCollVectors("SColl[SInt]", SInt, [
{ hex: "1000", value: [] },
{ hex: "100102", value: [1] },
{ hex: "10020000", value: [0, 0] },
Expand Down Expand Up @@ -253,7 +253,7 @@ export const collVectors: GenericTypeTestVector[] = [
value: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1] /* prettier-ignore */
}
]),
...buildCollVectors("[SLong]", SLong, [
...buildCollVectors("SColl[SLong]", SLong, [
{ hex: "110100", value: [0n] },
{ hex: "110102", value: [1n] },
{ hex: "110124", value: [18n] },
Expand All @@ -262,7 +262,7 @@ export const collVectors: GenericTypeTestVector[] = [
{ hex: "11018081f0e5dd60", value: [1661856514112n] },
{ hex: "110182d2ad9dce60", value: [1659767207041n] }
]),
...buildCollVectors("[SBigInt]", SBigInt, [
...buildCollVectors("SColl[SBigInt]", SBigInt, [
{ hex: "12010100", value: [0n] },
{ hex: "1202010106018271d5b481", value: [1n, 1659767207041n] },
{ hex: "12010112", value: [18n] },
Expand All @@ -271,7 +271,7 @@ export const collVectors: GenericTypeTestVector[] = [
{ hex: "1201060182ee5e0040", value: [1661856514112n] },
{ hex: "120206018271d5b48106018271d5b481", value: [1659767207041n, 1659767207041n] }
]),
...buildCollVectors("[[SByte]]", SColl(SByte), [
...buildCollVectors("SColl[SColl[SByte]]", SColl(SByte), [
{
hex: "1a031c4c657427732063656c656272617465204572676f526166666c6521201c4c657427732063656c656272617465204572676f526166666c65212020e730bbae0463346f8ce72be23ab8391d1e7a58f48ed857fcf4ee9aecf6915307",
value: [
Expand All @@ -281,11 +281,11 @@ export const collVectors: GenericTypeTestVector[] = [
]
}
]),
...buildCollVectors("[[[[SByte]]]]", SColl(SColl(SColl(SByte))), [
...buildCollVectors("SColl[SColl[SColl[SColl[SByte]]]]", SColl(SColl(SColl(SByte))), [
{ hex: "0c0c1a00", value: [] },
{ hex: "0c0c1a0101010201ff", value: [[[u8a([0x01, 0xff])]]] }
]),
...buildCollVectors("[(SInt, SLong)]", SPair(SInt, SLong), [
...buildCollVectors("SColl[(SInt, SLong)]", SPair(SInt, SLong), [
{
hex: "0c400504b40180febe81027880d4d4ab015a80bfdf80013c80aaea55",
value: [
Expand All @@ -296,7 +296,7 @@ export const collVectors: GenericTypeTestVector[] = [
]
}
]),
...buildCollVectors("Coll[(Coll[SByte], SInt)]", SPair(SColl(SByte), SInt), [
...buildCollVectors("SColl[(SColl[SByte], SInt)]", SPair(SColl(SByte), SInt), [
{
hex: "0c4c0e01240008cd0302122c332fd4e3c901f045ac18f559dcecf8dc61f6f94fbb34d0c7c3aac71fb714",
value: [
Expand All @@ -318,7 +318,7 @@ export const collVectors: GenericTypeTestVector[] = [
]
}
]),
...buildCollVectors("Coll[(Coll[SByte], SInt)]", SPair(SColl(SByte), SColl(SByte)), [
...buildCollVectors("SColl[(SColl[SByte], SColl[SByte])]", SPair(SColl(SByte), SColl(SByte)), [
{
hex: "0c3c0e0e02240008cd03f2d7187f56156cbedde84dffd873f59db7c0e16408c475145a0415317d85cf573339694a6b696558536f6f6b4c74615972384a5a3841386e4b75657639647a524d77786b476a75795165626e5167436a387a6443240008cd02d481d399b808586e94dfd907439b2671999e1d7a97b1705d3363707930a6ec59333967386569796970477666557a675239586a6761423577597641426f447a535969716a754a6b39676769446b334a533476454a",
value: [
Expand All @@ -333,7 +333,7 @@ export const collVectors: GenericTypeTestVector[] = [
]
}
]),
...buildCollVectors("Coll[Coll[(Int, Int)]]", SColl(SPair(SInt, SInt)), [
...buildCollVectors("SColl[SColl[(SInt, SInt)]]", SColl(SPair(SInt, SInt)), [
{
hex: "0c0c580202020406080208060402",
value: [
Expand Down Expand Up @@ -376,13 +376,13 @@ export const tupleTestVectors: GenericTypeTestVector[] = [
hex: "40020e01"
},
{
name: "(SInt, [SByte])",
name: "(SInt, SColl[SByte])",
sconst: SPair(SInt(1), SColl(SByte, hex.decode("0a0c"))),
value: [1, Uint8Array.from([10, 12])],
hex: "400e02020a0c"
},
{
name: "([SByte], [SByte])",
name: "(SColl[SByte], SColl[SByte])",
sconst: SPair(
SColl(SByte, hex.decode("505250")),
SColl(SByte, hex.decode("596f7572206c6f616e204a616e75617279"))
Expand All @@ -391,13 +391,13 @@ export const tupleTestVectors: GenericTypeTestVector[] = [
hex: "3c0e0e0350525011596f7572206c6f616e204a616e75617279"
},
{
name: "([SByte], SBool, SByte)",
name: "(SColl[SByte], SBool, SByte)",
sconst: STuple(SColl(SByte, [10, 12]), SBool(true), SByte(2)),
value: [u8a([10, 12]), true, 2],
hex: "480e0102020a0c0102"
},
{
name: "([SByte], SGroupElement)",
name: "(SColl[SByte], SGroupElement)",
sconst: SPair(
SColl(SByte, hex.decode("8743542e50d2195907ce017595f8adf1f496c796d9bcc1148ff9ec94d0bf5006")),
SGroupElement(
Expand All @@ -411,7 +411,7 @@ export const tupleTestVectors: GenericTypeTestVector[] = [
hex: "4f0e208743542e50d2195907ce017595f8adf1f496c796d9bcc1148ff9ec94d0bf5006036ebe10da76e99b081b5893635db7518a062bd0f89b07fc056ad9b77c2abce607"
},
{
name: "([([Byte], [Byte])], ([([Byte], (Int, Int))], [([Byte], (Int, Int))]))",
name: "(SColl[(SColl[SByte], SColl[SByte])], (SColl[(SColl[SByte], (SInt, SInt))], SColl[(SColl[SByte], (SInt, SInt))]))",
sconst: SPair(
SColl(SPair(SColl(SByte), SColl(SByte)), [[u8a([1, 2, 3]), u8a([4, 5, 6])]]),
SPair(
Expand All @@ -426,7 +426,7 @@ export const tupleTestVectors: GenericTypeTestVector[] = [
hex: "3c0c3c0e0e3c0c3c0e580c3c0e58010301020303040506010301020314160103040506181a"
},
{
name: "([([Byte], [Byte])], ([([Byte], (Int, Int))], [([Byte], (Int, Int))]))",
name: "(SColl[(SColl[SByte], SColl[SByte])], (SColl[(SColl[SByte], (SInt, SInt))], SColl[(SColl[SByte], (SInt, SInt))]))",
sconst: SPair(
SColl(SPair(SColl(SByte), SColl(SByte)), [
[u8a([98, 97, 99, 107, 103, 114, 111, 117, 110, 100]), u8a([98, 108, 117, 101])],
Expand Down
54 changes: 35 additions & 19 deletions packages/serializer/src/sigmaConstant.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,60 +33,73 @@ import { STuple } from "./types/constructors";

describe("Primitive types serialization and parsing", () => {
it.each(boolVectors)("Should road-trip SBool($value)", (tv) => {
expect(SBool(tv.value).toHex()).to.be.equal(tv.hex);
const sconst = SBool(tv.value);

expect(sconst.toHex()).to.be.equal(tv.hex);
expect(sconst.type.toString()).to.be.equal("SBool");
expect(SConstant.from(tv.hex).data).to.be.equal(tv.value);
});

it.each(byteVectors)("Should road-trip SByte($value)", (tv) => {
expect(SByte(tv.value).toHex()).to.be.equal(tv.hex);
const sconst = SByte(tv.value);

expect(sconst.toHex()).to.be.equal(tv.hex);
expect(sconst.type.toString()).to.be.equal("SByte");
expect(SConstant.from(tv.hex).data).to.be.equal(tv.value);
});

it.each(shortVectors)("Should road-trip SShort($value)", (tv) => {
expect(SShort(tv.value).toHex()).to.be.equal(tv.hex);
const sconst = SShort(tv.value);

expect(sconst.toHex()).to.be.equal(tv.hex);
expect(sconst.type.toString()).to.be.equal("SShort");
expect(SConstant.from(tv.hex).data).to.be.equal(tv.value);
});

it.each(intVectors)("Should road-trip SInt($value)", (tv) => {
expect(SInt(tv.value).toHex()).to.be.equal(tv.hex);
const sconst = SInt(tv.value);

expect(sconst.toHex()).to.be.equal(tv.hex);
expect(sconst.type.toString()).to.be.equal("SInt");
expect(SConstant.from(tv.hex).data).to.be.equal(tv.value);
});

it.each(longVectors)("Should road-trip SLong($value)", (tv) => {
expect(SLong(tv.value).toHex()).to.be.equal(tv.hex);
expect(SLong(String(tv.value)).toHex()).to.be.equal(tv.hex);
const sconst = SLong(tv.value);

expect(sconst.toHex()).to.be.equal(tv.hex);
expect(sconst.type.toString()).to.be.equal("SLong");
expect(SLong(String(tv.value)).toHex()).to.be.equal(tv.hex);
expect(SConstant.from(tv.hex).data).to.be.equal(ensureBigInt(tv.value));
});

it.each(longVectors)("Should road-trip SLong($value)", (tv) => {
expect(SLong(tv.value).toHex()).to.be.equal(tv.hex);
expect(SLong(String(tv.value)).toHex()).to.be.equal(tv.hex);

it.each(bigintVectors)("Should road-trip SBigInt($value)", (tv) => {
const sconst = SBigInt(tv.value);
expect(sconst.toHex()).to.be.equal(tv.hex);
expect(sconst.type.toString()).to.be.equal("SBigInt");
expect(SBigInt(BigInt(tv.value)).toHex()).to.be.equal(tv.hex);
expect(SConstant.from(tv.hex).data).to.be.equal(ensureBigInt(tv.value));
});

it.each(groupElementVectors)("Should road-trip SGroupElement($value)", (tv) => {
expect(SGroupElement(tv.value).toHex()).to.be.equal(tv.hex);
const sconst = SGroupElement(tv.value);
expect(sconst.toHex()).to.be.equal(tv.hex);
expect(sconst.type.toString()).to.be.equal("SGroupElement");
expect(SGroupElement(hex.decode(tv.value)).toHex()).to.be.equal(tv.hex);

expect(SConstant.from(tv.hex).data).to.be.deep.equal(hex.decode(tv.value));
});

it.each(sigmaPropVectors)("Should road-trip SSigmaProp(ProveDlog($value))", (tv) => {
expect(SSigmaProp(SGroupElement(tv.value)).toHex()).to.be.equal(tv.hex);
const sconst = SSigmaProp(SGroupElement(tv.value));

expect(sconst.toHex()).to.be.equal(tv.hex);
expect(sconst.type.toString()).to.be.equal("SSigmaProp");
expect(SSigmaProp(SGroupElement(hex.decode(tv.value))).toHex()).to.be.equal(tv.hex);

expect(SConstant.from(tv.hex).data).to.be.deep.equal(hex.decode(tv.value));
});

it.each(bigintVectors)("Should road-trip SBigInt($value)", (tv) => {
expect(SBigInt(tv.value).toHex()).to.be.equal(tv.hex);
expect(SBigInt(BigInt(tv.value)).toHex()).to.be.equal(tv.hex);

expect(SConstant.from(tv.hex).data).to.be.equal(BigInt(tv.value));
});

it("Should coerce alternative input types", () => {
const expectedBytes = Uint8Array.from([0xde, 0xad, 0xbe, 0xef]);

Expand All @@ -100,6 +113,7 @@ describe("Monomorphic types serialization and parsing", () => {
it("Should serialize SUnit", () => {
const sUnitHex = "62";
expect(SUnit().toHex()).toBe(sUnitHex);
expect(SUnit().type.toString()).toBe("SUnit");
expect(SConstant.from(sUnitHex).data).to.be.undefined;
});
});
Expand All @@ -108,6 +122,7 @@ describe("SColl serialization and parsing", () => {
it.each(collVectors)("Should serialize $name", (tv) => {
expect(tv.sconst.toHex()).to.be.equal(tv.hex);
expect(SConstant.from(tv.hex).data).to.be.deep.equal(tv.value);
expect(tv.sconst.type.toString()).to.be.equal(tv.name);
});

it("Should coerce alternative input types", () => {
Expand Down Expand Up @@ -182,6 +197,7 @@ describe("Not implemented types", () => {
describe("Tuple serialization", () => {
it.each(tupleTestVectors)("Should road-trip $name", (tv) => {
expect(tv.sconst.toHex()).to.be.equal(tv.hex);
expect(tv.sconst.type.toString()).to.be.equal(tv.name);
expect(SConstant.from(tv.hex).data).to.be.deep.equal(tv.value);
});

Expand Down
4 changes: 3 additions & 1 deletion packages/serializer/src/types/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ export abstract class SType<I = unknown, O = I> {
abstract get embeddable(): boolean;

coerce(data: I): O {
// a bit hacky but most of types will not need an specific coercion function.
// a bit hacky but most of types will not need a specific coercion function.
return data as unknown as O;
}

abstract toString(): string;
}

export abstract class SMonomorphicType<I, O = I> extends SType<I, O> {
Expand Down
8 changes: 8 additions & 0 deletions packages/serializer/src/types/generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export class SCollType<T extends SType = SType> extends SGenericType<T> {

return elements.map((el) => this.elementsType.coerce(el)) as O[];
}

toString(): string {
return `SColl[${this.elementsType.toString()}]`;
}
}

export class STupleType<T extends SType[] = SType[]> extends SGenericType<T> {
Expand All @@ -31,4 +35,8 @@ export class STupleType<T extends SType[] = SType[]> extends SGenericType<T> {

return output;
}

toString(): string {
return `(${this.elementsType.map((el) => el.toString()).join(", ")})`;
}
}
4 changes: 4 additions & 0 deletions packages/serializer/src/types/monomorphics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ export class SUnitType extends SMonomorphicType<undefined> {
get code(): 0x62 {
return 0x62;
}

toString(): string {
return "SUnit";
}
}
Loading

0 comments on commit 9f0363b

Please sign in to comment.