-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ update error messages from the pow claim endpoint
- Loading branch information
Showing
11 changed files
with
260 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "html-solver" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
tokio = { version = "1", features = ["rt", "macros"] } | ||
hyper = { version = "0.14", features = ["server", "http1", "tcp"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use hyper::{ | ||
service::{make_service_fn, service_fn}, | ||
Body, Request, Response, Server, StatusCode, | ||
}; | ||
use std::{convert::Infallible, fs, net::SocketAddr, path::Path}; | ||
|
||
async fn serve_file(req: Request<Body>) -> Result<Response<Body>, Infallible> { | ||
let path = match req.uri().path() { | ||
"/" => "static/index.html", | ||
path => &path[1..], // strip leading '/' | ||
}; | ||
|
||
let file_path = Path::new(path); | ||
|
||
match fs::read(file_path) { | ||
Ok(contents) => { | ||
let mime_type = match file_path.extension().and_then(|ext| ext.to_str()) { | ||
Some("html") => "text/html", | ||
Some("js") => "application/javascript", | ||
_ => "application/octet-stream", | ||
}; | ||
|
||
Ok(Response::builder() | ||
.header("Content-Type", mime_type) | ||
.body(Body::from(contents)) | ||
.unwrap()) | ||
} | ||
Err(_) => Ok(Response::builder() | ||
.status(StatusCode::NOT_FOUND) | ||
.body(Body::from("404 Not Found")) | ||
.unwrap()), | ||
} | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() { | ||
let addr = SocketAddr::from(([127, 0, 0, 1], 3001)); | ||
|
||
let make_svc = make_service_fn(|_conn| { | ||
async { Ok::<_, Infallible>(service_fn(serve_file)) } | ||
}); | ||
|
||
let server = Server::bind(&addr).serve(make_svc); | ||
|
||
println!("Listening on http://{}", addr); | ||
|
||
if let Err(e) = server.await { | ||
eprintln!("server error: {}", e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>PoW Solver</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
margin: 20px; | ||
} | ||
.container { | ||
max-width: 600px; | ||
margin: auto; | ||
padding: 20px; | ||
border: 1px solid #ccc; | ||
border-radius: 10px; | ||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | ||
} | ||
input, button { | ||
margin: 10px 0; | ||
padding: 10px; | ||
width: calc(100% - 22px); | ||
} | ||
button { | ||
cursor: pointer; | ||
} | ||
.result { | ||
margin-top: 20px; | ||
padding: 10px; | ||
border: 1px solid #ccc; | ||
border-radius: 5px; | ||
background-color: #f9f9f9; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h1>Proof-of-Work Solver</h1> | ||
<form id="powForm"> | ||
<label for="nonce">Nonce (16-byte hex string):</label> | ||
<input type="text" id="nonce" required pattern="[0-9a-fA-F]{32}" title="16-byte hex string (32 hex characters)"> | ||
|
||
<label for="difficulty">Difficulty (0-255):</label> | ||
<input type="number" id="difficulty" required min="0" max="255"> | ||
|
||
<button type="submit">Solve PoW</button> | ||
</form> | ||
<div id="result" class="result" style="display: none;"></div> | ||
</div> | ||
|
||
<script> | ||
if (window.Worker) { | ||
const worker = new Worker('static/solver.js'); | ||
|
||
document.getElementById('powForm').addEventListener('submit', function(event) { | ||
event.preventDefault(); | ||
const nonce = document.getElementById('nonce').value; | ||
const difficulty = parseInt(document.getElementById('difficulty').value, 10); | ||
document.getElementById('result').style.display = 'none'; | ||
document.getElementById('result').textContent = 'Solving...'; | ||
|
||
worker.postMessage({ nonce, difficulty }); | ||
|
||
worker.onmessage = function(event) { | ||
document.getElementById('result').textContent = 'Solution: ' + event.data.solution; | ||
document.getElementById('result').style.display = 'block'; | ||
}; | ||
}); | ||
} else { | ||
alert('Your browser does not support Web Workers.'); | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
async function sha256(data) { | ||
const buffer = await crypto.subtle.digest('SHA-256', data); | ||
return new Uint8Array(buffer); | ||
} | ||
|
||
function countLeadingZeros(data) { | ||
let leadingZeros = 0; | ||
for (let byte of data) { | ||
if (byte === 0) { | ||
leadingZeros += 8; | ||
} else { | ||
leadingZeros += byte.toString(2).padStart(8, '0').indexOf('1'); | ||
break; | ||
} | ||
} | ||
return leadingZeros; | ||
} | ||
|
||
async function findSolution(nonce, difficulty) { | ||
const salt = new Uint8Array([ | ||
0x61, 0x6c, 0x70, 0x65, 0x6e, 0x20, 0x6c, 0x61, | ||
0x62, 0x73, 0x20, 0x66, 0x61, 0x75, 0x63, 0x65, | ||
0x74, 0x20, 0x32, 0x30, 0x32, 0x34 | ||
]); | ||
nonce = new Uint8Array(nonce.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); | ||
let solution = new Uint8Array(8); | ||
|
||
while (true) { | ||
const hashInput = new Uint8Array([...salt, ...nonce, ...solution]); | ||
const hash = await sha256(hashInput); | ||
if (countLeadingZeros(hash) >= difficulty) { | ||
return Array.from(solution).map(byte => byte.toString(16).padStart(2, '0')).join(''); | ||
} | ||
// Increment solution | ||
for (let i = 7; i >= 0; i--) { | ||
if (solution[i] < 0xFF) { | ||
solution[i]++; | ||
break; | ||
} else { | ||
solution[i] = 0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
onmessage = async function(event) { | ||
const { nonce, difficulty } = event.data; | ||
const solution = await findSolution(nonce, difficulty); | ||
postMessage({ solution }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import hashlib | ||
|
||
# SHA-256 hashing function | ||
def sha256(data): | ||
return hashlib.sha256(data).digest() | ||
|
||
# Count leading zeros in a byte array | ||
def count_leading_zeros(data): | ||
leading_zeros = 0 | ||
for byte in data: | ||
if byte == 0: | ||
leading_zeros += 8 | ||
else: | ||
leading_zeros += bin(byte).find('1') | ||
break | ||
return leading_zeros | ||
|
||
# Find solution | ||
def find_solution(nonce, difficulty): | ||
salt = bytes.fromhex("616c70656e206c616273206661756365742032303234") | ||
nonce = bytes.fromhex(nonce) | ||
solution = bytearray(8) | ||
|
||
while True: | ||
hash_input = salt + nonce + solution | ||
hash = sha256(hash_input) | ||
print(hash.hex()) | ||
print(count_leading_zeros(hash)) | ||
if count_leading_zeros(hash) >= difficulty: | ||
return solution.hex() | ||
# Increment solution | ||
for i in range(7, -1, -1): | ||
if solution[i] < 0xFF: | ||
solution[i] += 1 | ||
break | ||
else: | ||
solution[i] = 0 | ||
|
||
# Example usage | ||
nonce = "4bbbefa849c59704f7f13745ca47161a" # Replace with actual nonce | ||
difficulty = 17 # Replace with actual difficulty | ||
solution = find_solution(nonce, difficulty) | ||
print("Solution:", solution) |