forked from Fly143/deepseek-free-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpow_solver.js
More file actions
122 lines (103 loc) · 3.97 KB
/
Copy pathpow_solver.js
File metadata and controls
122 lines (103 loc) · 3.97 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
/**
* DeepSeek PoW WASM Solver — Node.js bridge (with debug)
*/
const fs = require('fs');
const path = require('path');
function findWasm(dir) {
// 递归搜索 .wasm 文件,优先匹配已知文件名
const candidates = [];
function walk(d) {
try {
for (const entry of fs.readdirSync(d)) {
const full = path.join(d, entry);
const stat = fs.statSync(full);
if (stat.isFile() && entry.endsWith('.wasm')) {
candidates.push(full);
} else if (stat.isDirectory()) {
walk(full);
}
}
} catch (_) {}
}
walk(dir);
if (candidates.length === 0) {
throw new Error(`No .wasm file found under ${dir}`);
}
// 优先带 'sha3' 的文件名
const preferred = candidates.find(f => path.basename(f).includes('sha3'));
return preferred || candidates[0];
}
const WASM_PATH = findWasm(__dirname);
async function main() {
const input = process.argv[2];
if (!input) {
console.error('Usage: node pow_solver.js \'<json_config>\'');
process.exit(1);
}
const config = JSON.parse(input);
const wasmBuffer = fs.readFileSync(WASM_PATH);
const wasmModule = await WebAssembly.compile(wasmBuffer);
const instance = await WebAssembly.instantiate(wasmModule, {});
const mem = instance.exports.memory;
// Check initial memory
console.error(`Initial memory pages: ${mem.buffer.byteLength / 65536}`);
const prefix = `${config.salt}_${config.expire_at}_`;
console.error(`Challenge: ${config.challenge}`);
console.error(`Prefix: ${prefix}`);
console.error(`Difficulty: ${config.difficulty}`);
// Write strings to WASM memory
function writeString(str) {
const encoded = Buffer.from(str, 'utf-8');
const length = encoded.length;
const ptr = instance.exports.__wbindgen_export_0(length, 1);
console.error(`Allocated ptr=${ptr}, len=${length}, mem pages=${mem.buffer.byteLength / 65536}`);
const view = new Uint8Array(mem.buffer);
for (let i = 0; i < length; i++) {
view[ptr + i] = encoded[i];
}
return { ptr, length };
}
const retptr = instance.exports.__wbindgen_add_to_stack_pointer(-16);
console.error(`Stack ptr: ${retptr}`);
try {
const challengeInfo = writeString(config.challenge);
const prefixInfo = writeString(prefix);
console.error(`Calling wasm_solve(retptr=${retptr}, ch_ptr=${challengeInfo.ptr}, ch_len=${challengeInfo.length}, pfx_ptr=${prefixInfo.ptr}, pfx_len=${prefixInfo.length}, diff=${config.difficulty})`);
instance.exports.wasm_solve(
retptr,
challengeInfo.ptr,
challengeInfo.length,
prefixInfo.ptr,
prefixInfo.length,
config.difficulty
);
const view = new Int32Array(mem.buffer);
const status = view[retptr / 4];
console.error(`Status: ${status}`);
if (status === 0) {
console.error('wasm_solve returned 0 — no solution found');
process.exit(1);
}
const floatView = new Float64Array(mem.buffer);
const value = floatView[(retptr + 8) / 8];
const answer = Math.floor(value);
console.error(`Answer: ${answer}`);
const result = {
algorithm: config.algorithm,
challenge: config.challenge,
salt: config.salt,
answer: answer,
signature: config.signature,
target_path: config.target_path
};
const response = Buffer.from(JSON.stringify(result)).toString('base64');
console.log(response);
} finally {
instance.exports.__wbindgen_add_to_stack_pointer(16);
}
}
main().catch(err => {
console.error('Error:', err.message);
console.error(err.stack);
process.exit(1);
});