-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworker.js
More file actions
132 lines (118 loc) · 3.13 KB
/
worker.js
File metadata and controls
132 lines (118 loc) · 3.13 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
/**
* MANTRA Vanity Address Generator - Web Worker
*
* This worker handles parallel vanity address generation using multiple CPU cores.
* It uses the WASM module's batch processing functions for optimal performance.
*/
import init, {
generate_vanity_keypair_batch,
get_optimal_batch_size,
VanityPosition
} from "./vanity_wasm.js";
let wasmInitialized = false;
/**
* Initialize WASM module in worker context
*/
async function initWasm() {
if (!wasmInitialized) {
await init();
wasmInitialized = true;
}
}
/**
* Handle messages from main thread
*/
self.onmessage = async function(e) {
const { id, type, data } = e.data;
try {
// Initialize WASM if not already done
await initWasm();
switch (type) {
case 'GENERATE_VANITY_BATCH':
await handleVanityBatch(id, data);
break;
case 'PING':
// Health check
self.postMessage({
id,
type: 'PONG',
success: true
});
break;
default:
throw new Error(`Unknown message type: ${type}`);
}
} catch (error) {
self.postMessage({
id,
type: 'ERROR',
success: false,
error: error.message
});
}
};
/**
* Handle vanity address generation batch
*/
async function handleVanityBatch(id, { target, position, batchSize, workerId }) {
// Map position string to WASM enum
let vanityPosition;
switch (position) {
case 'prefix':
vanityPosition = VanityPosition.Prefix;
break;
case 'suffix':
vanityPosition = VanityPosition.Suffix;
break;
case 'anywhere':
default:
vanityPosition = VanityPosition.Anywhere;
break;
}
// Use optimal batch size if not specified
const actualBatchSize = batchSize || get_optimal_batch_size(target.length);
// Generate batch and check for matches
const result = generate_vanity_keypair_batch(target, vanityPosition, actualBatchSize);
if (result) {
// Found a match!
self.postMessage({
id,
type: 'VANITY_FOUND',
success: true,
data: {
keypair: {
address: result.address,
mnemonic: result.mnemonic
},
workerId,
attempts: actualBatchSize
}
});
} else {
// No match in this batch
self.postMessage({
id,
type: 'VANITY_BATCH_COMPLETE',
success: true,
data: {
workerId,
attempts: actualBatchSize
}
});
}
}
/**
* Handle worker errors
*/
self.onerror = function(error) {
self.postMessage({
type: 'WORKER_ERROR',
success: false,
error: error.message
});
};
// Signal that worker is ready
self.postMessage({
type: 'WORKER_READY',
success: true
});