Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/zod-solana-signature-blockhash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@macalinao/zod-solana": minor
---

Add `signatureSchema` and `blockhashSchema` for validating base58-encoded Solana
transaction signatures and blockhashes. Both parse a string and transform it into
the corresponding branded `@solana/kit` type (`Signature` / `Blockhash`).
16 changes: 16 additions & 0 deletions packages/zod-solana/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ A Zod schema that validates Solana addresses.
- **Output**: `Address` - A validated Address type from @solana/kit
- **Errors**: Throws/returns error with message "Invalid Solana address" for invalid inputs

### `signatureSchema`

A Zod schema that validates Solana transaction signatures.

- **Input**: `string` - A potential base58-encoded 64-byte signature
- **Output**: `Signature` - A validated Signature type from @solana/kit
- **Errors**: Throws/returns error with message "Invalid Solana signature" for invalid inputs

### `blockhashSchema`

A Zod schema that validates Solana blockhashes.

- **Input**: `string` - A potential base58-encoded 32-byte blockhash
- **Output**: `Blockhash` - A validated Blockhash type from @solana/kit
- **Errors**: Throws/returns error with message "Invalid Solana blockhash" for invalid inputs

## Examples

### Validating Multiple Addresses
Expand Down
69 changes: 69 additions & 0 deletions packages/zod-solana/src/blockhash-schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type * as z from "zod";
import { describe, expect, it } from "bun:test";
import { blockhash } from "@solana/kit";
import { blockhashSchema } from "./blockhash-schema.js";

const VALID_BLOCKHASH = "8qbHbw2BbbTHBW1sbeqakYXVKRQM8Ne7pLK7m6CVfeR";

describe("blockhashSchema", () => {
it("should validate and transform a valid blockhash", () => {
const result = blockhashSchema.parse(VALID_BLOCKHASH);

expect(result).toBe(blockhash(VALID_BLOCKHASH));
});

it("should accept the all-zero blockhash", () => {
const zeroBlockhash = "1".repeat(32);
const result = blockhashSchema.parse(zeroBlockhash);

expect(result).toBe(blockhash(zeroBlockhash));
});

it("should reject empty strings", () => {
expect(() => blockhashSchema.parse("")).toThrow();
});

it("should reject blockhashes with invalid characters", () => {
expect(() => blockhashSchema.parse(`0OIl+/${"1".repeat(38)}`)).toThrow();
});

it("should reject blockhashes that are too short", () => {
expect(() => blockhashSchema.parse("abc")).toThrow();
});

it("should reject a 64-byte value such as a signature", () => {
expect(() =>
blockhashSchema.parse(
"2AXDGYSE4f2sz7tvMMzyHvUfcoJmxudvdhBcmiUSo6ijwfYmfZYsKRxboQMPh3R4kUhXRVdtSXFXMheka4Rc4P2",
),
).toThrow();
});

it("should provide a helpful error message for invalid blockhashes", () => {
const result = blockhashSchema.safeParse("not-a-valid-blockhash");

expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues[0]?.message).toBe("Invalid Solana blockhash");
}
});

it("should work with safeParse", () => {
const validResult = blockhashSchema.safeParse(VALID_BLOCKHASH);
expect(validResult.success).toBe(true);
if (validResult.success) {
expect(validResult.data).toBe(blockhash(VALID_BLOCKHASH));
}

const invalidResult = blockhashSchema.safeParse("invalid");
expect(invalidResult.success).toBe(false);
});

it("should infer the correct TypeScript type", () => {
// This is a compile-time test
type InferredType = z.output<typeof blockhashSchema>;
const testBlockhash: InferredType = blockhash(VALID_BLOCKHASH);

expect(testBlockhash).toBeDefined();
});
});
23 changes: 23 additions & 0 deletions packages/zod-solana/src/blockhash-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Blockhash } from "@solana/kit";
import { blockhash } from "@solana/kit";
import * as z from "zod";

/**
* A Zod schema for Solana blockhashes.
* Validates that a string is a valid base58-encoded 32-byte blockhash and
* transforms it to a Blockhash type.
* Compatible with both Zod v3 and v4.
*/
export const blockhashSchema: z.ZodType<Blockhash, string> = z
.string()
.transform((val, ctx) => {
try {
return blockhash(val);
} catch {
ctx.addIssue({
code: "custom",
message: "Invalid Solana blockhash",
});
return z.NEVER;
}
});
2 changes: 2 additions & 0 deletions packages/zod-solana/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { addressSchema } from "./address-schema.js";
export { blockhashSchema } from "./blockhash-schema.js";
export { bpsSchema } from "./bps-schema.js";
export { U8_MAX, U16_MAX, U32_MAX, U64_MAX } from "./constants.js";
export {
Expand All @@ -10,6 +11,7 @@ export {
type TokenMetadataProperties,
tokenMetadataSchema,
} from "./token-metadata-schema.js";
export { signatureSchema } from "./signature-schema.js";
export { u8Schema } from "./u8-schema.js";
export { u16Schema } from "./u16-schema.js";
export { u32Schema } from "./u32-schema.js";
Expand Down
72 changes: 72 additions & 0 deletions packages/zod-solana/src/signature-schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type * as z from "zod";
import { describe, expect, it } from "bun:test";
import { signature } from "@solana/kit";
import { signatureSchema } from "./signature-schema.js";

const VALID_SIGNATURE =
"2AXDGYSE4f2sz7tvMMzyHvUfcoJmxudvdhBcmiUSo6ijwfYmfZYsKRxboQMPh3R4kUhXRVdtSXFXMheka4Rc4P2";

describe("signatureSchema", () => {
it("should validate and transform a valid signature", () => {
const result = signatureSchema.parse(VALID_SIGNATURE);

expect(result).toBe(signature(VALID_SIGNATURE));
});

it("should accept the all-zero signature", () => {
const zeroSignature = "1".repeat(64);
const result = signatureSchema.parse(zeroSignature);

expect(result).toBe(signature(zeroSignature));
});

it("should reject empty strings", () => {
expect(() => signatureSchema.parse("")).toThrow();
});

it("should reject signatures with invalid characters", () => {
expect(() => signatureSchema.parse(`0OIl+/${"1".repeat(82)}`)).toThrow();
});

it("should reject signatures that are too short", () => {
expect(() => signatureSchema.parse("abc")).toThrow();
});

it("should reject signatures that are too long", () => {
expect(() => signatureSchema.parse("1".repeat(96))).toThrow();
});

it("should reject a 32-byte value such as an address", () => {
expect(() =>
signatureSchema.parse("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),
).toThrow();
});

it("should provide a helpful error message for invalid signatures", () => {
const result = signatureSchema.safeParse("not-a-valid-signature");

expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues[0]?.message).toBe("Invalid Solana signature");
}
});

it("should work with safeParse", () => {
const validResult = signatureSchema.safeParse(VALID_SIGNATURE);
expect(validResult.success).toBe(true);
if (validResult.success) {
expect(validResult.data).toBe(signature(VALID_SIGNATURE));
}

const invalidResult = signatureSchema.safeParse("invalid");
expect(invalidResult.success).toBe(false);
});

it("should infer the correct TypeScript type", () => {
// This is a compile-time test
type InferredType = z.output<typeof signatureSchema>;
const testSignature: InferredType = signature(VALID_SIGNATURE);

expect(testSignature).toBeDefined();
});
});
23 changes: 23 additions & 0 deletions packages/zod-solana/src/signature-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Signature } from "@solana/kit";
import { signature } from "@solana/kit";
import * as z from "zod";

/**
* A Zod schema for Solana transaction signatures.
* Validates that a string is a valid base58-encoded 64-byte signature and
* transforms it to a Signature type.
* Compatible with both Zod v3 and v4.
*/
export const signatureSchema: z.ZodType<Signature, string> = z
.string()
.transform((val, ctx) => {
try {
return signature(val);
} catch {
ctx.addIssue({
code: "custom",
message: "Invalid Solana signature",
});
return z.NEVER;
}
});
Loading