Created
November 13, 2024 16:01
-
-
Save jaonoctus/9e3ae75bef4d19e635c2e07743bdea78 to your computer and use it in GitHub Desktop.
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
// Convert decimal difficulty to target hex | |
function difficultyToTarget(difficulty) { | |
// maxTarget is the hex target for difficulty 1 | |
const maxTarget = BigInt("0x00000000FFFF0000000000000000000000000000000000000000000000000000"); | |
// Convert difficulty to BigInt and handle decimal places | |
const difficultyBigInt = BigInt(Math.floor(difficulty * 100000000)) / BigInt(100000000); | |
// Calculate target: maxTarget / difficulty | |
const target = maxTarget / difficultyBigInt; | |
// Convert to hex string and pad with leading zeros | |
return target.toString(16).padStart(64, '0'); | |
} | |
// Convert target hex to decimal difficulty | |
function targetToDifficulty(targetHex) { | |
// maxTarget is the hex target for difficulty 1 | |
const maxTarget = BigInt("0x00000000FFFF0000000000000000000000000000000000000000000000000000"); | |
// Convert hex target to BigInt | |
const target = BigInt("0x" + targetHex); | |
// Calculate difficulty: maxTarget / target | |
const difficulty = Number(maxTarget) / Number(target); | |
return difficulty; | |
} | |
function formatDifficulty(difficulty) { | |
const units = ['', 'K', 'M', 'B', 'T', 'P', 'E']; | |
let unitIndex = 0; | |
let value = difficulty; | |
while (value >= 1000 && unitIndex < units.length - 1) { | |
value /= 1000; | |
unitIndex++; | |
} | |
// Round to 2 decimal places | |
value = Math.round(value * 100) / 100; | |
return `${value}${units[unitIndex]}`; | |
} | |
function bitsToTarget(bits) { | |
// Convert bits to hex string if it's a number | |
const bitsHex = typeof bits === 'number' ? bits.toString(16) : bits; | |
// Get exponent and coefficient | |
const exponent = parseInt(bitsHex.slice(0, 2), 16); | |
const coefficient = bitsHex.slice(2); | |
// Calculate the target by shifting coefficient left by (8 * (exponent - 3)) | |
let target = coefficient + '0'.repeat(2 * (exponent - 3)); | |
// Pad to 64 characters (32 bytes) | |
target = target.padStart(64, '0'); | |
return target; | |
} | |
function targetToBits(targetHex) { | |
// Remove leading zeros | |
let significantTarget = targetHex.replace(/^0+/, ''); | |
// Calculate exponent and coefficient | |
const size = Math.ceil(significantTarget.length / 2); | |
const exponent = size; | |
const coefficient = significantTarget.slice(0, 6); | |
// Combine exponent and coefficient | |
return (exponent.toString(16).padStart(2, '0') + coefficient.padEnd(6, '0')); | |
} | |
function isValidPoW(powHex, targetHex) { | |
// Ensure both inputs are 64 characters (32 bytes) long | |
powHex = powHex.padStart(64, '0'); | |
targetHex = targetHex.padStart(64, '0'); | |
// Convert hex strings to BigInt for comparison | |
const powValue = BigInt('0x' + powHex); | |
const targetValue = BigInt('0x' + targetHex); | |
// PoW is valid if hash is less than or equal to target | |
return powValue <= targetValue; | |
} | |
// Helper function to get human readable representation of hex numbers | |
function formatHexForComparison(hex1, hex2) { | |
// Ensure both inputs are 64 characters long | |
hex1 = hex1.padStart(64, '0'); | |
hex2 = hex2.padStart(64, '0'); | |
// Find the first different character to highlight the comparison point | |
let firstDiff = 0; | |
for (let i = 0; i < 64; i++) { | |
if (hex1[i] !== hex2[i]) { | |
firstDiff = i; | |
break; | |
} | |
} | |
return { | |
firstDiff, | |
formattedHex1: `${hex1.slice(0, firstDiff)}[${hex1[firstDiff]}]${hex1.slice(firstDiff + 1)}`, | |
formattedHex2: `${hex2.slice(0, firstDiff)}[${hex2[firstDiff]}]${hex2.slice(firstDiff + 1)}` | |
}; | |
} | |
const targetHex = bitsToTarget('1702c4e4') | |
const targetDiff = targetToDifficulty(targetHex) | |
// console.log(targetHex) | |
console.log(targetDiff) | |
console.log(formatDifficulty(targetDiff)) | |
// const diff = 54200000 | |
// const targetHex2 = difficultyToTarget(diff) | |
const targetHex2 = '00000000000000000000a285a8f6da65e165aa380e56986ea282098228dc9d56' | |
const diff = targetToDifficulty(targetHex2) | |
// console.log(targetHex2) | |
console.log(formatDifficulty(diff)) | |
console.log(isValidPoW(targetHex2, targetHex)) | |
console.log(formatHexForComparison(targetHex2, targetHex).formattedHex1) | |
console.log(formatHexForComparison(targetHex2, targetHex).formattedHex2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment