From a2f34be03056622dc7176f94a506a1c82b21e6d7 Mon Sep 17 00:00:00 2001 From: oXtxNt9U <120286271+oXtxNt9U@users.noreply.github.com> Date: Fri, 24 Apr 2026 12:55:23 +0900 Subject: [PATCH 1/4] service provider --- packages/serializer/package.json | 7 +++++ .../source/service-provider.test.ts | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 packages/serializer/source/service-provider.test.ts diff --git a/packages/serializer/package.json b/packages/serializer/package.json index 9ce8f04b9..ae22c5aaf 100644 --- a/packages/serializer/package.json +++ b/packages/serializer/package.json @@ -29,7 +29,14 @@ }, "devDependencies": { "@mainsail/contracts": "workspace:*", + "@mainsail/crypto-address-keccak256": "workspace:*", + "@mainsail/crypto-config": "workspace:*", + "@mainsail/crypto-hash-bcrypto": "workspace:*", + "@mainsail/crypto-key-pair-ecdsa": "workspace:*", + "@mainsail/crypto-signature-ecdsa": "workspace:*", + "@mainsail/crypto-signature-bls12-381": "workspace:*", "@mainsail/test-runner": "workspace:*", + "@mainsail/validation": "workspace:*", "uvu": "0.5.6" }, "engines": { diff --git a/packages/serializer/source/service-provider.test.ts b/packages/serializer/source/service-provider.test.ts new file mode 100644 index 000000000..946ae5d03 --- /dev/null +++ b/packages/serializer/source/service-provider.test.ts @@ -0,0 +1,27 @@ +import { Identifiers } from "@mainsail/constants"; + +import { Application } from "@mainsail/kernel"; + +import { describe } from "@mainsail/test-runner"; +import { ServiceProvider } from "./service-provider"; + +describe<{ + app: Application; + serviceProvider: ServiceProvider; +}>("ServiceProvider", ({ beforeEach, it, assert }) => { + beforeEach(async (context) => { + context.app = new Application(); + + context.serviceProvider = context.app.resolve(ServiceProvider); + }); + + it("should be ok", async ({ serviceProvider, app }) => { + await serviceProvider.register(); + + assert.true(app.isBound(Identifiers.Cryptography.Serializer)); + }); + + it("should be required", async ({ serviceProvider }) => { + assert.true(await serviceProvider.required()); + }); +}); From da8be5687615fdca896e820b4a06d457251b67e3 Mon Sep 17 00:00:00 2001 From: oXtxNt9U <120286271+oXtxNt9U@users.noreply.github.com> Date: Fri, 24 Apr 2026 12:55:27 +0900 Subject: [PATCH 2/4] serializer --- packages/serializer/source/serializer.test.ts | 176 ++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 packages/serializer/source/serializer.test.ts diff --git a/packages/serializer/source/serializer.test.ts b/packages/serializer/source/serializer.test.ts new file mode 100644 index 000000000..0c76e6543 --- /dev/null +++ b/packages/serializer/source/serializer.test.ts @@ -0,0 +1,176 @@ +import { Identifiers } from "@mainsail/constants"; +import { Application } from "@mainsail/kernel"; +import { ServiceProvider as CryptoConfigServiceProvider } from "@mainsail/crypto-config"; +import { ServiceProvider as ECDSA } from "@mainsail/crypto-key-pair-ecdsa"; +import { ServiceProvider as CryptoAddressKeccak256 } from "@mainsail/crypto-address-keccak256"; +import { ServiceProvider as CryptoSignatureEcdsa } from "@mainsail/crypto-signature-ecdsa"; +import { ServiceProvider as CryptoSignatureBls12381 } from "@mainsail/crypto-signature-bls12-381"; +import { ServiceProvider as CryptoHashBcrypto } from "@mainsail/crypto-hash-bcrypto"; +import { ServiceProvider as ValidationServiceProvider } from "@mainsail/validation"; +import { Contracts } from "@mainsail/contracts"; +import cryptoJson from "../../core/bin/config/devnet/core/crypto.json"; + +import { describe } from "@mainsail/test-runner"; +import { Serializer } from "./serializer"; +import { BigNumber, ByteBuffer } from "@mainsail/utils"; +import { NotImplemented } from "@mainsail/exceptions"; + +describe<{ + app: Application; + serializer: Serializer; +}>("Serializer", ({ beforeEach, it, assert }) => { + beforeEach(async (context) => { + context.app = new Application(); + context.app.get(Identifiers.Config.Repository).set("crypto", cryptoJson); + + await context.app.resolve(ValidationServiceProvider).register(); + await context.app.resolve(CryptoConfigServiceProvider).register(); + + await context.app.resolve(ECDSA).register(); + await context.app.resolve(CryptoAddressKeccak256).register(); + await context.app.resolve(CryptoSignatureEcdsa).register(); + await context.app.resolve(CryptoSignatureBls12381).register(); + await context.app.resolve(CryptoHashBcrypto).register(); + + context.serializer = context.app.resolve(Serializer); + }); + + it("should serialize and deserialize every supported schema type", async ({ serializer }) => { + const schema = { + uint8Value: { type: "uint8" }, + uint16Value: { type: "uint16" }, + uint32Value: { type: "uint32" }, + uint48Value: { type: "uint48" }, + uint64Value: { type: "uint64" }, + uint256Value: { type: "uint256" }, + bigintValue: { type: "bigint" }, + + hashValue: { type: "hash" }, + shortHashValue: { type: "hash", size: 4 }, + + blockHashValue: { type: "blockHash", optional: true }, + missingBlockHashValue: { type: "blockHash", optional: true }, + + addressValue: { type: "address" }, + publicKeyValue: { type: "publicKey" }, + consensusSignatureValue: { type: "consensusSignature" }, + + validatorSetValue: { type: "validatorSet" }, + hexValue: { type: "hex" }, + + transactionsCount: { type: "uint8" }, + transactions: { type: "transactions" }, + } as const; + + const publicKey = "03335579fc9eab385e3076e31a2bae5f7560e4697abe6d476c93d07ffae628a359"; + const consensusSignature = + "9529fb1b3001aa735a2d3a70ac8568c9e5757c7112d43de6a0463b3d4354b54a706dc3ab9ca49d32f2307059fe93c5b017949f54427e257af38f72cff36b041ce30fb5ebbac636b2f84ced80a16e0150b059771dae40a5baf86f5805baf061b0"; + + const data = { + uint8Value: 1, + uint16Value: 0x0203, + uint32Value: 0x04050607, + uint48Value: 0x010203040506, + uint64Value: 123_456_789, + uint256Value: BigNumber.make("12345678901234567890"), + bigintValue: BigNumber.make("987654321"), + + hashValue: "11".repeat(32), + shortHashValue: "22".repeat(4), + + blockHashValue: "33".repeat(32), + missingBlockHashValue: undefined, + + addressValue: "0x18F8a91Aa88af45329fb1650BCdD53aAC1Ff4bfe", + publicKeyValue: publicKey, + consensusSignatureValue: consensusSignature, + + validatorSetValue: [true, false, true, true, false, false, true, false, true], + hexValue: "deadbeefcafebabe", + + transactionsCount: 2, + transactions: [{ serialized: Buffer.from("aa", "hex") }, { serialized: Buffer.from("bbcc", "hex") }], + }; + + const serialized = await serializer.serialize(data, { + length: 512, + schema, + skip: 0, + }); + + const deserialized = await serializer.deserialize( + ByteBuffer.fromBuffer(serialized), + {}, + { schema }, + ); + + assert.equal(deserialized.uint8Value, data.uint8Value); + assert.equal(deserialized.uint16Value, data.uint16Value); + assert.equal(deserialized.uint32Value, data.uint32Value); + assert.equal(deserialized.uint48Value, data.uint48Value); + assert.equal(deserialized.uint64Value, data.uint64Value); + + assert.equal(deserialized.uint256Value.toString(), data.uint256Value.toString()); + assert.equal(deserialized.bigintValue.toString(), data.bigintValue.toString()); + + assert.equal(deserialized.hashValue, data.hashValue); + assert.equal(deserialized.shortHashValue, data.shortHashValue); + + assert.equal(deserialized.blockHashValue, data.blockHashValue); + assert.equal(deserialized.missingBlockHashValue, undefined); + + assert.equal(deserialized.addressValue, data.addressValue); + assert.equal(deserialized.publicKeyValue, data.publicKeyValue); + assert.equal(deserialized.consensusSignatureValue, data.consensusSignatureValue); + + assert.equal(deserialized.validatorSetValue, data.validatorSetValue); + assert.equal(deserialized.hexValue, data.hexValue); + + assert.equal(deserialized.transactionsCount, 2); + assert.equal(deserialized.transactions, [Buffer.from("aa", "hex"), Buffer.from("bbcc", "hex")]); + }); + + it("should skip bytes", async ({ serializer }) => { + const serialized = await serializer.serialize( + { value: 0xaa }, + { + length: 3, + skip: 2, + schema: { + value: { type: "uint8" }, + }, + }, + ); + + assert.equal(serialized, Buffer.from([0x00, 0x00, 0xaa])); + }); + + it("throws when serializing an unsupported schema type", async ({ serializer }) => { + try { + await serializer.serialize({ value: 1 }, { + length: 1, + schema: { + value: { type: "unsupported" }, + }, + } as any); + + assert.true(false); + } catch (ex) { + assert.equal(ex, new NotImplemented("Serializer", "unsupported")); + } + }); + + it("throws when deserializing an unsupported schema type", async ({ serializer }) => { + try { + await serializer.deserialize(ByteBuffer.fromBuffer(Buffer.alloc(0)), {}, { + schema: { + value: { type: "unsupported" }, + }, + } as any); + + assert.true(false); + } catch (ex) { + assert.equal(ex, new NotImplemented("Serializer", "unsupported")); + } + }); +}); From 7049ccc272ca31cd3b6c4e6e1da85bb8cc46d500 Mon Sep 17 00:00:00 2001 From: oXtxNt9U <120286271+oXtxNt9U@users.noreply.github.com> Date: Fri, 24 Apr 2026 12:55:40 +0900 Subject: [PATCH 3/4] update pnpm-lock.yaml --- pnpm-lock.yaml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a1dd844df..7b1bbc10c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2687,9 +2687,30 @@ importers: '@mainsail/contracts': specifier: workspace:* version: link:../contracts + '@mainsail/crypto-address-keccak256': + specifier: workspace:* + version: link:../crypto-address-keccak256 + '@mainsail/crypto-config': + specifier: workspace:* + version: link:../crypto-config + '@mainsail/crypto-hash-bcrypto': + specifier: workspace:* + version: link:../crypto-hash-bcrypto + '@mainsail/crypto-key-pair-ecdsa': + specifier: workspace:* + version: link:../crypto-key-pair-ecdsa + '@mainsail/crypto-signature-bls12-381': + specifier: workspace:* + version: link:../crypto-signature-bls12-381 + '@mainsail/crypto-signature-ecdsa': + specifier: workspace:* + version: link:../crypto-signature-ecdsa '@mainsail/test-runner': specifier: workspace:* version: link:../test-runner + '@mainsail/validation': + specifier: workspace:* + version: link:../validation uvu: specifier: 0.5.6 version: 0.5.6 From 5cb9acd4e3964c363d40c765776d24c7edc7b498 Mon Sep 17 00:00:00 2001 From: oXtxNt9U <120286271+oXtxNt9U@users.noreply.github.com> Date: Fri, 24 Apr 2026 03:58:42 +0000 Subject: [PATCH 4/4] style: resolve style guide violations [ci-lint-fix] --- packages/serializer/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/serializer/package.json b/packages/serializer/package.json index ae22c5aaf..ed1140092 100644 --- a/packages/serializer/package.json +++ b/packages/serializer/package.json @@ -33,8 +33,8 @@ "@mainsail/crypto-config": "workspace:*", "@mainsail/crypto-hash-bcrypto": "workspace:*", "@mainsail/crypto-key-pair-ecdsa": "workspace:*", - "@mainsail/crypto-signature-ecdsa": "workspace:*", "@mainsail/crypto-signature-bls12-381": "workspace:*", + "@mainsail/crypto-signature-ecdsa": "workspace:*", "@mainsail/test-runner": "workspace:*", "@mainsail/validation": "workspace:*", "uvu": "0.5.6"