Skip to content

Commit

Permalink
Add utility method to test register type
Browse files Browse the repository at this point in the history
Fixes #122
  • Loading branch information
arobsn committed Sep 17, 2024
1 parent 6f93d00 commit 62025c4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-knives-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fleet-sdk/serializer": minor
---

Introduce `stypeof` utility function
2 changes: 1 addition & 1 deletion packages/serializer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { estimateVLQSize, SigmaByteReader, SigmaByteWriter } from "./coders";
export { SConstant, parse, decode } from "./sigmaConstant";
export { SConstant, parse, decode, stypeof } from "./sigmaConstant";
export * from "./types";
export * from "./serializers";
29 changes: 28 additions & 1 deletion packages/serializer/src/sigmaConstant.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "./_test-vectors/constantVectors";
import { SigmaByteWriter } from "./coders";
import { dataSerializer } from "./serializers";
import { decode, parse, SConstant } from "./sigmaConstant";
import { decode, parse, SConstant, stypeof } from "./sigmaConstant";
import type { SGroupElementType } from "./types";
import {
SBigInt,
Expand All @@ -27,6 +27,7 @@ import {
SLong,
SShort,
SSigmaProp,
STupleType,
SUnit
} from "./types/";
import { STuple } from "./types/constructors";
Expand Down Expand Up @@ -144,6 +145,32 @@ describe("SColl serialization and parsing", () => {
});
});

describe("stypeof", () => {
it("Should return the type of a valid constant", () => {
expect(stypeof(SBool(true).toBytes())).to.be.instanceOf(SBool);
expect(stypeof(SByte(1).toBytes())).to.be.instanceOf(SByte);
expect(stypeof(SShort(1).toBytes())).to.be.instanceOf(SShort);
expect(stypeof(SInt(1).toBytes())).to.be.instanceOf(SInt);
expect(stypeof(SLong(1n).toBytes())).to.be.instanceOf(SLong);
expect(stypeof(SBigInt(1n).toBytes())).to.be.instanceOf(SBigInt);
expect(stypeof(SGroupElement(hex.decode("deadbeef")).toBytes())).to.be.instanceOf(
SGroupElement
);
expect(stypeof(SSigmaProp(SGroupElement("deadbeef")).toBytes())).to.be.instanceOf(
SSigmaProp
);
expect(stypeof(SColl(SByte, [1, 2, 3]).toBytes())).to.be.instanceOf(SColl);
expect(stypeof(STuple(SByte(1), SByte(2)).toBytes())).to.be.instanceOf(STupleType);
expect(stypeof(SUnit().toBytes())).to.be.instanceOf(SUnit);
});

it("Should return SUnit for empty bytes", () => {
expect(stypeof(Uint8Array.from([]))).to.be.equal(undefined);
expect(stypeof(undefined)).to.be.equal(undefined);
expect(stypeof("deadbeef")).to.be.equal(undefined);
});
});

describe("Safe decoding", () => {
const validVectors = [
{ hex: "40050002", data: [0, 1n], type: "(SInt, SLong)" },
Expand Down
17 changes: 17 additions & 0 deletions packages/serializer/src/sigmaConstant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ export function decode<D = unknown, T extends SType = SType>(
}
}

/**
* Returns the `SType` of the given value.
*
* @param value - The value to check the SType of.
* @returns The SType of the value, or `undefined` if the value is `undefined` or
* deserialization fails.
*/
export function stypeof(value?: ByteInput): SType | undefined {
if (!value) return;

try {
return typeSerializer.deserialize(new SigmaByteReader(value));
} catch {
return;
}
}

/** @deprecated use `decode` instead */
export function parse<T>(constant: ByteInput): T;
/** @deprecated use `decode` instead */
Expand Down

0 comments on commit 62025c4

Please sign in to comment.