routstrd 0.3.11 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import {
3
+ getDaemonBaseUrl,
4
+ urlHost,
5
+ urlHosts,
6
+ } from "../../src/utils/daemon-client";
7
+ import type { RoutstrdConfig } from "../../src/utils/config";
8
+
9
+ function config(host: string): RoutstrdConfig {
10
+ return {
11
+ port: 8008,
12
+ host,
13
+ provider: null,
14
+ cocodPath: null,
15
+ };
16
+ }
17
+
18
+ describe("daemon URL host formatting", () => {
19
+ test("prefers IPv4 loopback for an IPv4 wildcard", () => {
20
+ expect(urlHosts("0.0.0.0")).toEqual(["127.0.0.1", "[::1]"]);
21
+ expect(getDaemonBaseUrl(config("0.0.0.0"))).toBe("http://127.0.0.1:8008");
22
+ });
23
+
24
+ test("prefers IPv6 loopback for an IPv6 wildcard", () => {
25
+ expect(urlHosts("::")).toEqual(["[::1]", "127.0.0.1"]);
26
+ expect(getDaemonBaseUrl(config("::"))).toBe("http://[::1]:8008");
27
+ });
28
+
29
+ test("brackets IPv6 literals", () => {
30
+ expect(urlHost("2001:db8::1")).toBe("[2001:db8::1]");
31
+ expect(getDaemonBaseUrl(config("2001:db8::1"))).toBe(
32
+ "http://[2001:db8::1]:8008",
33
+ );
34
+ });
35
+
36
+ test("does not bracket an already-bracketed IPv6 literal twice", () => {
37
+ expect(urlHost("[::1]")).toBe("[::1]");
38
+ });
39
+
40
+ test("escapes an IPv6 zone identifier for use in a URL", () => {
41
+ expect(urlHost("fe80::1%en0")).toBe("[fe80::1%25en0]");
42
+ });
43
+
44
+ test("leaves IPv4 addresses and hostnames unchanged", () => {
45
+ expect(urlHost("127.0.0.1")).toBe("127.0.0.1");
46
+ expect(urlHost("localhost")).toBe("localhost");
47
+ });
48
+
49
+ test("uses the default loopback when the host is absent", () => {
50
+ expect(urlHost()).toBe("127.0.0.1");
51
+ });
52
+
53
+ test("preserves an explicitly configured daemon URL", () => {
54
+ expect(
55
+ getDaemonBaseUrl({
56
+ ...config("::"),
57
+ daemonUrl: "https://daemon.example/",
58
+ }),
59
+ ).toBe("https://daemon.example");
60
+ });
61
+ });
@@ -0,0 +1,106 @@
1
+ import { describe, expect, it, mock } from "bun:test";
2
+ import {
3
+ Amount,
4
+ getDecodedToken,
5
+ getEncodedToken,
6
+ } from "@cashu/cashu-ts";
7
+ import {
8
+ createWalletAdapter,
9
+ decodeCashuTokenAmount,
10
+ } from "../../src/daemon/wallet";
11
+ import type { CocodClient } from "../../src/daemon/wallet/cocod-client";
12
+
13
+ // A full modern keyset ID with the same format as Minibits' post-migration
14
+ // active keyset. getEncodedToken stores only its first eight bytes in TokenV4.
15
+ const FULL_KEYSET_ID =
16
+ "01fc0ec0e59cd6fa01b7a88f8cd77fce81fd1e64bca67d752e984992b7a3c3a821";
17
+ const LEGACY_KEYSET_ID = "00107937db0cc865";
18
+
19
+ function makeToken({
20
+ amounts = [5],
21
+ keysetId = FULL_KEYSET_ID,
22
+ unit = "sat",
23
+ }: {
24
+ amounts?: number[];
25
+ keysetId?: string;
26
+ unit?: string;
27
+ } = {}): string {
28
+ return getEncodedToken({
29
+ mint: "https://mint.minibits.cash/Bitcoin",
30
+ unit,
31
+ proofs: amounts.map((amount, index) => ({
32
+ id: keysetId,
33
+ amount: Amount.from(amount),
34
+ secret: `short-keyset-regression-fixture-${index}`,
35
+ C: `02${(index + 1).toString(16).padStart(2, "0").repeat(32)}`,
36
+ })),
37
+ });
38
+ }
39
+
40
+ function makeWalletClient(
41
+ receiveCashu: CocodClient["receiveCashu"],
42
+ ): CocodClient {
43
+ // createWalletAdapter is intentionally lazy; this receive-path test only
44
+ // needs the one capability exercised by receiveToken.
45
+ return { receiveCashu } as CocodClient;
46
+ }
47
+
48
+ describe("short keyset TokenV4 compatibility", () => {
49
+ it("reads amount metadata without resolving the shortened proof keyset ID", () => {
50
+ const token = makeToken({ amounts: [1, 4] });
51
+
52
+ // Guard the fixture itself: a full proof decode with no mint keysets must
53
+ // exercise the same short-ID failure that triggered this regression.
54
+ expect(() => getDecodedToken(token, [])).toThrow(/short keyset ID/i);
55
+ expect(decodeCashuTokenAmount(token)).toEqual({
56
+ amount: 5,
57
+ unit: "sat",
58
+ });
59
+ });
60
+
61
+ for (const tokenCase of [
62
+ {
63
+ name: "modern keyset with msat unit",
64
+ input: { amounts: [500, 1000], unit: "msat" },
65
+ expected: { amount: 1500, unit: "msat" as const },
66
+ },
67
+ {
68
+ name: "legacy v0 keyset",
69
+ input: { amounts: [2, 8], keysetId: LEGACY_KEYSET_ID },
70
+ expected: { amount: 10, unit: "sat" as const },
71
+ },
72
+ ]) {
73
+ it(`reads ${tokenCase.name} metadata`, () => {
74
+ expect(decodeCashuTokenAmount(makeToken(tokenCase.input))).toEqual(
75
+ tokenCase.expected,
76
+ );
77
+ });
78
+ }
79
+
80
+ it("reports success after cocod receives a short-keyset token", async () => {
81
+ const receiveCashu = mock(async () => "Received 5");
82
+ const walletClient = makeWalletClient(receiveCashu);
83
+ const adapter = await createWalletAdapter({ walletClient });
84
+
85
+ const result = await adapter.receiveToken(makeToken());
86
+
87
+ expect(receiveCashu).toHaveBeenCalledTimes(1);
88
+ expect(result).toEqual({
89
+ success: true,
90
+ amount: 5,
91
+ unit: "sat",
92
+ message: "Received 5",
93
+ });
94
+ });
95
+
96
+ it("does not call the wallet when token metadata is invalid", async () => {
97
+ const receiveCashu = mock(async () => "should not be called");
98
+ const walletClient = makeWalletClient(receiveCashu);
99
+ const adapter = await createWalletAdapter({ walletClient });
100
+
101
+ const result = await adapter.receiveToken("cashuBnot-a-valid-token");
102
+
103
+ expect(receiveCashu).not.toHaveBeenCalled();
104
+ expect(result.success).toBe(false);
105
+ });
106
+ });