forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoroban.network-guard.test.ts
More file actions
212 lines (183 loc) · 6.3 KB
/
Copy pathsoroban.network-guard.test.ts
File metadata and controls
212 lines (183 loc) · 6.3 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { afterEach, describe, expect, it, vi } from "vitest";
const { assembleTransactionMock } = vi.hoisted(() => ({
assembleTransactionMock: vi.fn(),
}));
vi.mock("@stellar/stellar-sdk", async (importOriginal) => {
const actual = await importOriginal<typeof import("@stellar/stellar-sdk")>();
return {
...actual,
rpc: {
...actual.rpc,
assembleTransaction: assembleTransactionMock,
},
};
});
vi.mock("@/config", () => ({
networkPassphrase: "Test SDF Network ; September 2015",
rpcServer: { simulateTransaction: vi.fn() },
sorobanService: undefined,
}));
import {
Account,
Address,
Contract,
StrKey,
TransactionBuilder,
nativeToScVal,
xdr,
} from "@stellar/stellar-sdk";
import { SorobanService } from "./soroban";
import { FreighterError } from "./error-handler";
const POOL_CONTRACT_ID =
"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4";
const USER_PUBLIC_KEY = StrKey.encodeEd25519PublicKey(Buffer.alloc(32, 7));
const POOL_ID = "pool-xlm";
const CORRECT_PASSPHRASE = "Test SDF Network ; September 2015";
const WRONG_PASSPHRASE = "Public Global Stellar Network ; September 2015";
function makeAuthEntry(functionName: string) {
const contractFn = new xdr.InvokeContractArgs({
contractAddress: Address.fromString(POOL_CONTRACT_ID).toScAddress(),
functionName,
args: [],
});
return new xdr.SorobanAuthorizationEntry({
credentials: xdr.SorobanCredentials.sorobanCredentialsSourceAccount(),
rootInvocation: new xdr.SorobanAuthorizedInvocation({
function:
xdr.SorobanAuthorizedFunction.sorobanAuthorizedFunctionTypeContractFn(
contractFn,
),
subInvocations: [],
}),
});
}
function makeService() {
const rpcServer = {
getAccount: vi.fn().mockResolvedValue(new Account(USER_PUBLIC_KEY, "0")),
simulateTransaction: vi.fn().mockResolvedValue({
result: { auth: [makeAuthEntry("lock_assets")] },
minResourceFee: "100",
}),
sendTransaction: vi.fn().mockResolvedValue({ status: "PENDING", hash: "tx-hash" }),
getTransaction: vi.fn().mockResolvedValue({ status: "SUCCESS" }),
getLatestLedger: vi.fn().mockResolvedValue({ sequence: 200_000 }),
getEvents: vi.fn().mockResolvedValue({ events: [] }),
};
assembleTransactionMock.mockImplementation((_tx: unknown, sim: unknown) => ({
build: () => ({
toXDR: () => "mock-xdr",
}),
}));
const service = new SorobanService();
const svc = service as unknown as {
poolContracts: Map<string, Contract>;
rpcServer: typeof rpcServer;
};
svc.rpcServer = rpcServer;
const poolContract = new Contract(POOL_CONTRACT_ID);
svc.poolContracts.set(POOL_ID, poolContract);
return { service, rpcServer };
}
describe("SorobanService network passphrase guard", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("allows signing when wallet network matches", async () => {
const { service, rpcServer } = makeService();
const walletApi = {
signTransaction: vi.fn(async (xdr: string) => xdr),
getNetworkDetails: vi.fn(async () => ({
network: "TESTNET",
networkPassphrase: CORRECT_PASSPHRASE,
})),
};
const result = await service.lockAssets(
POOL_ID,
USER_PUBLIC_KEY,
"50000000",
walletApi,
);
expect(result.success).toBe(true);
expect(walletApi.getNetworkDetails).toHaveBeenCalled();
expect(walletApi.signTransaction).toHaveBeenCalled();
expect(rpcServer.sendTransaction).toHaveBeenCalled();
});
it("allows signing when getNetworkDetails is not available", async () => {
const { service } = makeService();
const walletApi = {
signTransaction: vi.fn(async (xdr: string) => xdr),
};
const result = await service.lockAssets(
POOL_ID,
USER_PUBLIC_KEY,
"50000000",
walletApi,
);
expect(result.success).toBe(true);
expect(walletApi.signTransaction).toHaveBeenCalled();
});
it("throws FreighterError when wallet network mismatches in lockAssets", async () => {
const { service } = makeService();
const walletApi = {
signTransaction: vi.fn(async (xdr: string) => xdr),
getNetworkDetails: vi.fn(async () => ({
network: "PUBLIC",
networkPassphrase: WRONG_PASSPHRASE,
})),
};
await expect(
service.lockAssets(POOL_ID, USER_PUBLIC_KEY, "50000000", walletApi),
).rejects.toThrow(FreighterError);
expect(walletApi.signTransaction).not.toHaveBeenCalled();
});
it("throws FreighterError when wallet network mismatches in unlockAssets", async () => {
const { service } = makeService();
rpcServer.simulateTransaction.mockResolvedValue({
result: { auth: [makeAuthEntry("unlock_assets")] },
minResourceFee: "100",
});
const walletApi = {
signTransaction: vi.fn(async (xdr: string) => xdr),
getNetworkDetails: vi.fn(async () => ({
network: "PUBLIC",
networkPassphrase: WRONG_PASSPHRASE,
})),
};
await expect(
service.unlockAssets(POOL_ID, USER_PUBLIC_KEY, "50000000", walletApi),
).rejects.toThrow(FreighterError);
expect(walletApi.signTransaction).not.toHaveBeenCalled();
});
it("throws FreighterError when wallet network mismatches in setBoost", async () => {
const { service } = makeService();
rpcServer.simulateTransaction.mockResolvedValue({
result: { auth: [makeAuthEntry("set_boost")] },
minResourceFee: "100",
});
const walletApi = {
signTransaction: vi.fn(async (xdr: string) => xdr),
getNetworkDetails: vi.fn(async () => ({
network: "PUBLIC",
networkPassphrase: WRONG_PASSPHRASE,
})),
};
await expect(
service.setBoost(POOL_ID, USER_PUBLIC_KEY, 50, walletApi),
).rejects.toThrow(FreighterError);
expect(walletApi.signTransaction).not.toHaveBeenCalled();
});
it("does not submit transaction when network guard rejects", async () => {
const { service, rpcServer } = makeService();
const walletApi = {
signTransaction: vi.fn(async (xdr: string) => xdr),
getNetworkDetails: vi.fn(async () => ({
network: "PUBLIC",
networkPassphrase: WRONG_PASSPHRASE,
})),
};
await expect(
service.lockAssets(POOL_ID, USER_PUBLIC_KEY, "50000000", walletApi),
).rejects.toThrow();
expect(rpcServer.sendTransaction).not.toHaveBeenCalled();
});
});