feat: multi-wallet architecture with localStorage state, EIP-712 verification, cross-tab WS leadership
- wallets.js: WalletManager state management with chain validation - verifier.js: WalletVerifier EIP-712 signing via window.ethereum - stream.js: WalletStreamManager with BroadcastChannel leader election, backoff + jitter - AGENTS.md: updated with coding guidelines
This commit is contained in:
76
test/test_verifier.js
Normal file
76
test/test_verifier.js
Normal file
@ -0,0 +1,76 @@
|
||||
import { WalletManager } from '../wallets.js';
|
||||
import { WalletVerifier } from '../verifier.js';
|
||||
|
||||
// Setup basic environment mocks
|
||||
const mockStorage = {};
|
||||
globalThis.localStorage = {
|
||||
getItem: (key) => mockStorage[key] || null,
|
||||
setItem: (key, value) => { mockStorage[key] = String(value); }
|
||||
};
|
||||
|
||||
function assertError(result, expectedMsg, testName) {
|
||||
if (result.success === false && result.error.includes(expectedMsg)) {
|
||||
console.log(`✅ PASS: ${testName}`);
|
||||
} else {
|
||||
console.error(`❌ FAIL: ${testName} (Got: "${result.error ?? 'success'}", Expected: "${expectedMsg}")`);
|
||||
}
|
||||
}
|
||||
|
||||
async function runVerifierTests() {
|
||||
console.log("🚀 Starting WalletVerifier Error State Test Suite...\n");
|
||||
|
||||
// --- TEST 1: Missing Extension ---
|
||||
globalThis.window = {}; // No ethereum provider defined
|
||||
const wm1 = new WalletManager();
|
||||
let verifier = new WalletVerifier(wm1);
|
||||
assertError(
|
||||
await verifier.triggerWalletVerification('base', 'Test'),
|
||||
"No Web3 wallet extension found",
|
||||
"Missing Extension Detection"
|
||||
);
|
||||
|
||||
// --- TEST 2: User Rejects Account Connection (EIP-1193 Error 4001) ---
|
||||
globalThis.window = {
|
||||
ethereum: {
|
||||
request: async ({ method }) => {
|
||||
if (method === 'eth_requestAccounts') {
|
||||
const err = new Error("User denied connection");
|
||||
err.code = 4001;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const wm2 = new WalletManager();
|
||||
verifier = new WalletVerifier(wm2);
|
||||
assertError(
|
||||
await verifier.triggerWalletVerification('base', 'Test'),
|
||||
"Verification cancelled",
|
||||
"Account Rejection (Code 4001)"
|
||||
);
|
||||
|
||||
// --- TEST 3: User Rejects Signature ---
|
||||
globalThis.window = {
|
||||
ethereum: {
|
||||
request: async ({ method }) => {
|
||||
if (method === 'eth_requestAccounts') return ['0x04f728C520C438A000f7A5E9d904F0e725FFAEFE'];
|
||||
if (method === 'eth_signTypedData_v4') {
|
||||
const err = new Error("User rejected the signature request");
|
||||
err.code = 4001;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const wm3 = new WalletManager();
|
||||
verifier = new WalletVerifier(wm3);
|
||||
assertError(
|
||||
await verifier.triggerWalletVerification('base', 'Test'),
|
||||
"rejected",
|
||||
"Signature Rejection Mapping"
|
||||
);
|
||||
|
||||
console.log("\n🏁 Test Suite Complete.");
|
||||
}
|
||||
|
||||
runVerifierTests();
|
||||
Reference in New Issue
Block a user