forked from Nodal-stellar/Nodal-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultisig_payment.test.ts
More file actions
188 lines (171 loc) · 5.87 KB
/
Copy pathmultisig_payment.test.ts
File metadata and controls
188 lines (171 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* tests/multisig_payment.test.ts
* Tests for MultiSigPaymentTool (#108)
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
import { MultiSigPaymentTool } from "../backend/tools/MultiSigPaymentTool";
import * as rpcClient from "../backend/rpc_client";
const { Keypair } = require("@stellar/stellar-sdk");
const TEST_SECRET = Keypair.random().secret();
vi.mock("../backend/rpc_client", () => ({
loadAccount: vi.fn(),
submitTransaction: vi.fn(),
horizonServer: {},
sorobanServer: {},
simulateSorobanTx: vi.fn(),
prepareSorobanTx: vi.fn(),
resolveNetworkPassphrase: vi.fn(() => "Test SDF Network ; September 2015"),
}));
vi.mock("../backend/config", () => {
const secret = TEST_SECRET;
return {
config: {
STELLAR_NETWORK: "testnet",
HORIZON_URL: "https://horizon-testnet.stellar.org",
SOROBAN_RPC_URL: "https://soroban-testnet.stellar.org",
AGENT_PUBLIC_KEY: Keypair.fromSecret(secret).publicKey(),
X402_ASSET_CODE: "USDC",
X402_ASSET_ISSUER: "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
MAX_RETRIES: 3,
RETRY_DELAY_MS: 100,
AGENT_SPENDING_LIMIT: "100",
agentKeypair: () => Keypair.fromSecret(secret),
},
};
});
const DEST = "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5";
const SIGNER2 = "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGZUK9AI4WDCBAHD9HTPFE7";
const ISSUER = "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN";
function makeMockAccount() {
const { Keypair } = require("@stellar/stellar-sdk");
const publicKey = Keypair.fromSecret(TEST_SECRET).publicKey();
return {
accountId: () => publicKey,
sequenceNumber: () => "100",
incrementSequenceNumber: vi.fn(),
sequence: "100",
incrementedSequenceNumber: () => "101",
thresholds: { low_threshold: 0, med_threshold: 0, high_threshold: 0 },
flags: { auth_required: false, auth_revocable: false, auth_immutable: false },
balances: [{ asset_type: "native", balance: "10000.0000000" }],
signers: [],
data_attr: {},
subentry_count: 0,
home_domain: "",
inflation_dest: null,
};
}
describe("MultiSigPaymentTool", () => {
let tool: MultiSigPaymentTool;
beforeEach(() => {
vi.clearAllMocks();
tool = new MultiSigPaymentTool(TEST_SECRET);
vi.mocked(rpcClient.loadAccount).mockResolvedValue(makeMockAccount() as any);
});
it("returns unsigned XDR when no signatures provided", async () => {
const result = await tool.execute({
destination: DEST,
amount: "100",
assetCode: "XLM",
additionalSigners: [SIGNER2],
minSignatures: 2,
});
expect(result.unsignedXDR).toBeDefined();
expect(typeof result.unsignedXDR).toBe("string");
expect(result.txHash).toBeUndefined();
});
it("rejects invalid additional signers before any network call", async () => {
await expect(
tool.execute({
destination: DEST,
amount: "100",
assetCode: "XLM",
additionalSigners: ["A".repeat(56)],
minSignatures: 2,
})
).rejects.toThrow(/Invalid signer public key/);
expect(rpcClient.loadAccount).not.toHaveBeenCalled();
});
it("unsigned XDR is valid base64-encoded XDR (non-empty)", async () => {
const result = await tool.execute({
destination: DEST,
amount: "50",
assetCode: "XLM",
additionalSigners: [SIGNER2],
minSignatures: 2,
});
// Valid XDR can be decoded from base64
expect(() => Buffer.from(result.unsignedXDR!, "base64")).not.toThrow();
expect(result.unsignedXDR!.length).toBeGreaterThan(50);
});
it("does not call submitTransaction when returning unsignedXDR", async () => {
await tool.execute({
destination: DEST,
amount: "100",
assetCode: "XLM",
additionalSigners: [SIGNER2],
minSignatures: 2,
});
expect(rpcClient.submitTransaction).not.toHaveBeenCalled();
});
it("returns unsignedXDR when signatures are insufficient (1 provided, 2 required)", async () => {
const result = await tool.execute({
destination: DEST,
amount: "100",
assetCode: "XLM",
additionalSigners: [SIGNER2],
minSignatures: 2,
signatures: ["sig1"], // Only 1 signature, but minSignatures is 2
});
expect(result.unsignedXDR).toBeDefined();
expect(typeof result.unsignedXDR).toBe("string");
expect(result.txHash).toBeUndefined();
expect(rpcClient.submitTransaction).not.toHaveBeenCalled();
});
it("submits and returns txHash when sufficient signatures provided", async () => {
vi.mocked(rpcClient.submitTransaction).mockResolvedValue({ hash: "multisig_hash", ledger: 10 } as any);
const result = await tool.execute({
destination: DEST,
amount: "100",
assetCode: "XLM",
additionalSigners: [SIGNER2],
minSignatures: 1,
signatures: ["sig1"],
});
expect(result.txHash).toBe("multisig_hash");
expect(result.ledger).toBe(10);
});
it("throws when minSignatures exceeds total available signers", async () => {
await expect(
tool.execute({
destination: DEST,
amount: "100",
assetCode: "XLM",
additionalSigners: [SIGNER2],
minSignatures: 5, // only 2 signers (agent + SIGNER2)
})
).rejects.toThrow(/minSignatures.*exceeds total available signers/);
});
it("throws when non-XLM asset has no issuer", async () => {
await expect(
tool.execute({
destination: DEST,
amount: "100",
assetCode: "USDC",
additionalSigners: [SIGNER2],
minSignatures: 1,
})
).rejects.toThrow(/Asset issuer is required/);
});
it("accepts custom asset with issuer and returns XDR", async () => {
const result = await tool.execute({
destination: DEST,
amount: "100",
assetCode: "USDC",
assetIssuer: ISSUER,
additionalSigners: [SIGNER2],
minSignatures: 2,
});
expect(result.unsignedXDR).toBeDefined();
});
});