|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * We are switching to the "node" environment because jsdom does not provide a full WebCrypto API, |
| 5 | + * specifically window.crypto.subtle, which is required by @noble/ed25519 for signature |
| 6 | + * verification. Node.js (v16.8+) includes native crypto.subtle support, making it suitable for |
| 7 | + * cryptographic tests. Current browsers support window.crypto.subtle natively, so this issue only |
| 8 | + * affects the test environment. */ |
| 9 | + |
| 10 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 11 | +import { validateSignature } from "../validateSignature"; |
| 12 | + |
| 13 | +describe("validateSignature", () => { |
| 14 | + const validEdInput = { |
| 15 | + // just for the sake of verification |
| 16 | + // privateKey: "A1CFF8671EDD404DCB27EC6101BD2B50FCB5B9B97790052B59BFC21647F1030B" |
| 17 | + message: `{"title": "Ryan Blockchain Ecosystem Budget - 100M ada", "abstract": "This is Ryan's proposal for a Blockchain Ecosystem Budget, which allocates 100 million ada to various projects and initiatives within the ecosystem.", "rationale": "uhhhhhhhhhhhhhhhh", "motivation": "Without funding Ryan cannot buy an island.", "references": [{"uri": "ipfs://xd", "@type": "Other", "label": "Lol"}]}`, |
| 18 | + publicKey: |
| 19 | + "6A29D3C5C6280FBD9EF17EFFB29F8E8435D404BCF2FCED9336ABAEF1D06B62CB", |
| 20 | + algorithm: "ed25519", |
| 21 | + signature: |
| 22 | + "23CFF1D1DA358AF8D835CD2F1F2A972D6E09DA5BD05C08E076017F83FEF1CAC082A853C12E7EA6BB44FC322809E6554ED69302CEE03DF6ABEF2C9D709C0E8906", |
| 23 | + }; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + vi.spyOn(console, "error").mockImplementation(() => {}); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + vi.restoreAllMocks(); |
| 31 | + vi.clearAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + it("returns false if signature is missing", async () => { |
| 35 | + const result = await validateSignature({ |
| 36 | + ...validEdInput, |
| 37 | + signature: undefined, |
| 38 | + }); |
| 39 | + expect(result).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + it("returns false if publicKey is missing", async () => { |
| 43 | + const result = await validateSignature({ |
| 44 | + ...validEdInput, |
| 45 | + publicKey: undefined, |
| 46 | + }); |
| 47 | + expect(result).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it("returns false if algorithm is missing", async () => { |
| 51 | + const result = await validateSignature({ |
| 52 | + ...validEdInput, |
| 53 | + algorithm: undefined, |
| 54 | + }); |
| 55 | + expect(result).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it("returns false if message is missing", async () => { |
| 59 | + const result = await validateSignature({ |
| 60 | + ...validEdInput, |
| 61 | + message: undefined, |
| 62 | + }); |
| 63 | + expect(result).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + it("returns false for unsupported algorithm", async () => { |
| 67 | + vi.spyOn(console, "error").mockImplementation(() => {}); |
| 68 | + const result = await validateSignature({ |
| 69 | + ...validEdInput, |
| 70 | + algorithm: "rsa", |
| 71 | + }); |
| 72 | + expect(result).toBe(false); |
| 73 | + expect(console.error).toHaveBeenCalledWith("Unsupported algorithm:", "rsa"); |
| 74 | + }); |
| 75 | + |
| 76 | + describe("Ed25519 algorithm", () => { |
| 77 | + it("accepts 'Ed25519' (case-insensitive)", async () => { |
| 78 | + const result = await validateSignature({ |
| 79 | + ...validEdInput, |
| 80 | + algorithm: "Ed25519", |
| 81 | + }); |
| 82 | + |
| 83 | + expect(result).toBe(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it("accepts 'ed25519' (case-insensitive)", async () => { |
| 87 | + const result = await validateSignature({ |
| 88 | + ...validEdInput, |
| 89 | + algorithm: "ed25519", |
| 90 | + }); |
| 91 | + expect(result).toBe(true); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments