hugin-utils 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +29 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -49,12 +49,36 @@ function insertNonce(blobHex, nonceHex) {
49
49
  return { blobHex: blobBuffer.toString('hex'), offset };
50
50
  }
51
51
 
52
+ function nonceToHexLE(nonce) {
53
+ const buf = Buffer.alloc(4);
54
+ buf.writeUInt32LE(nonce >>> 0, 0);
55
+ return buf.toString('hex');
56
+ }
57
+
58
+ function parseTarget(targetHex) {
59
+ if (!targetHex) return null;
60
+ const targetBuffer = Buffer.from(targetHex, 'hex');
61
+ if (targetBuffer.length === 4) {
62
+ const raw = targetBuffer.readUInt32LE(0);
63
+ if (raw === 0) return null;
64
+ const numerator = 0xFFFFFFFFFFFFFFFFn;
65
+ const denom = 0xFFFFFFFFn / BigInt(raw);
66
+ if (denom === 0n) return null;
67
+ return numerator / denom;
68
+ }
69
+ if (targetBuffer.length === 8) {
70
+ return targetBuffer.readBigUInt64LE(0);
71
+ }
72
+ return null;
73
+ }
74
+
52
75
  function meetsTarget(hashHex, targetHex) {
53
- if (!targetHex || targetHex.length !== 8) return true;
76
+ const target = parseTarget(targetHex);
77
+ if (target === null) return true;
54
78
  const hash = Buffer.from(hashHex, 'hex');
55
- const targetNum = Buffer.from(targetHex, 'hex').readUInt32BE(0);
56
- const hashNum = Buffer.from(hash.slice(0, 4)).reverse().readUInt32BE(0);
57
- return hashNum <= targetNum;
79
+ if (hash.length < 32) return false;
80
+ const hashTail = hash.readBigUInt64LE(24);
81
+ return hashTail <= target;
58
82
  }
59
83
 
60
84
  async function findShare({
@@ -72,7 +96,7 @@ async function findShare({
72
96
  const start = Date.now();
73
97
 
74
98
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
75
- const nonceHex = (nonce & 0xFFFFFFFF).toString(16).padStart(8, '0');
99
+ const nonceHex = nonceToHexLE(nonce);
76
100
  const { blobHex, offset } = insertNonce(job.blob, nonceHex);
77
101
  if (log) log('nonce_offset', { jobId: job.job_id, offset });
78
102
  const result = await hashFn(blobHex);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hugin-utils",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "PoW utils for hugin node messages",
5
5
  "main": "index.js",
6
6
  "license": "MIT",