Skip to content

Commit

Permalink
All cleaned up and working perfectly.
Browse files Browse the repository at this point in the history
Fixed a bug in the bitcoinjs lib that was breaking the link to your wallet on the win page.
  • Loading branch information
bigjosh committed Dec 9, 2022
1 parent e5511c3 commit 9eef974
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ <h2 style="margin-block-start:0.2em;">CONNECTION LOST</h2>
// Our wallet info, generated once on page load
window.testmode = false; // Test or Main. Set if URL contains "TESTMODE".
window.keyPair; // User's keypair
window.network;

// Info on current round, based on data received over the websocket
// TODO: Package this block stuff into an object to keep it neat
Expand Down Expand Up @@ -702,24 +703,23 @@ <h2 style="margin-block-start:0.2em;">CONNECTION LOST</h2>

<script>

function getAddressFromKeyPair( kp ) {
bitcoinjs.payments.p2pkh({ pubkey: kp.publicKey , network }).address;
}

let network;

if (testmode) {
network = bitcoinjs.networks.testnet;
window.network = bitcoinjs.networks.testnet;
} else {
network = bitcoinjs.networks.main;
window.network = bitcoinjs.networks.bitcoin;
}

window.keyPair = bitcoinjs.ECPair.makeRandom( {network: network} );
window.keyPair = bitcoinjs.ECPair.makeRandom( {network: window.network} );

console.log( "Prize redemption key (do not tell to ANYONE or they can claim your prizes!):"+ window.keyPair.toWIF());
// You can test WIFs at http://gobittest.appspot.com/PrivateKey or import them into bitcoin-core
// with the commmand "importprivkey [key] [name for key in wallet]"

// (This was not working due to a bug in this version of bitcoinjs, but now it does thanks to ChatGPT!)
function getAddressFromKeyPair( kp ) {
return bitcoinjs.payments.p2pkh({ pubkey: kp.publicKey }).address;
}

</script>

<!--- MEAT OF THE CODE RUNS HERE WHENEVER A KENO CELL IS CLICKED. WE UPDATE THE EXTRANONCE, GENERATE A BLOCK, AND CHECK THE NEW HASH FOR A WIN. --->
Expand Down Expand Up @@ -992,9 +992,9 @@ <h2 style="margin-block-start:0.2em;">CONNECTION LOST</h2>
document.writeln(" your prize. Do not share it with anyone who you do not 100% trust since anyone");
document.writeln(" with this code can claim your prize. </p>");
document.writeln("<h2>Status check links</h2>");
document.writeln("<a target='_blank' href='https://btc.com/"+blockHeaderHashHexString+"'>Check if solution was accepted by the network</a>");
document.writeln("<a target='_blank' href='https://explorer.btc.com/btc/search/"+blockHeaderHashHexString+"'>Check if solution was accepted by the network</a>");
document.writeln("<br>");
document.writeln("<a target='_blank' href='https://btc.com/"+getAddressFromKeyPair(window.keyPair)+"'>Check if prize is available to be claimed</a>");
document.writeln("<a target='_blank' href='https://explorer.btc.com/btc/search/"+getAddressFromKeyPair(window.keyPair)+"'>Check if prize is available to be claimed</a>");

document.writeln("<h2>Live Submision Updates Here</h2>");

Expand Down
3 changes: 2 additions & 1 deletion docs/info.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ <h1>FAQ</h1>
</p>

<p><B>Q: What does the blinking green HASHHUNT logo mean?</b><br>
A: This is a visual indication that you are still connected to the server. It should blink about once every 15 seconds.
A: This is a visual indication that you are still connected to the server. It should blink green about once every 15 seconds while
connected, or turn red if the connection is lost.
</p>

<p id="why-http"><b>Q: Why does Hash Hunt only run over http and not https?</b><br>
Expand Down
23 changes: 23 additions & 0 deletions node-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,29 @@ function starupNewWsConnection( ws , lastBlockBuffer ) {

}

// Convert difficulty that we get from `bitcoin-cli difficulty` to nbits that we can efficiently send to the client
// https://bitcoin.stackexchange.com/a/80974/113175
function difficulty2bits(difficulty) {
if (difficulty < 0) throw 'difficulty cannot be negative';
if (!isFinite(difficulty)) throw 'difficulty cannot be infinite';
for (var shiftBytes = 1; true; shiftBytes++) {
var word = (0x00ffff * Math.pow(0x100, shiftBytes)) / difficulty;
if (word >= 0xffff) break;
}
word &= 0xffffff; // convert to int < 0xffffff
var size = 0x1d - shiftBytes;
// the 0x00800000 bit denotes the sign, so if it is already set, divide the
// mantissa by 0x100 and increase the size by a byte
if (word & 0x800000) {
word >>= 8;
size++;
}
if ((word & ~0x007fffff) != 0) throw 'the \'bits\' \'word\' is out of bounds';
if (size > 0xff) throw 'the \'bits\' \'size\' is out of bounds';
var bits = (size << 24) | word;
return bits;
}

// Whenever bitcoin-core gets a new block, then calls the `blocknotify` batch file, which then makes
// a `curl` call to us with the new blockhash and difficulty.
// nbits and height are numbers, blockhash and lastBlockBuffer are buffers
Expand Down

0 comments on commit 9eef974

Please sign in to comment.