@totemsdk/txpow 1.0.2 → 1.1.1
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.
- package/LICENSE +202 -21
- package/NOTICE +60 -0
- package/dist/admission/challenge.js +14 -21
- package/dist/admission/commitment.js +9 -13
- package/dist/admission/index.js +6 -34
- package/dist/admission/mine.js +14 -17
- package/dist/admission/template.js +32 -47
- package/dist/admission/types.js +2 -5
- package/dist/admission/verify.js +12 -15
- package/dist/calibrate.js +4 -8
- package/dist/cjs/admission/challenge.d.ts +66 -0
- package/dist/cjs/admission/challenge.js +155 -0
- package/dist/cjs/admission/commitment.d.ts +34 -0
- package/dist/cjs/admission/commitment.js +73 -0
- package/dist/cjs/admission/index.d.ts +14 -0
- package/dist/cjs/admission/index.js +42 -0
- package/dist/cjs/admission/mine.d.ts +60 -0
- package/dist/cjs/admission/mine.js +146 -0
- package/dist/cjs/admission/template.d.ts +138 -0
- package/dist/cjs/admission/template.js +247 -0
- package/dist/cjs/admission/types.d.ts +224 -0
- package/dist/cjs/admission/types.js +24 -0
- package/dist/cjs/admission/verify.d.ts +50 -0
- package/dist/cjs/admission/verify.js +156 -0
- package/dist/cjs/calibrate.d.ts +35 -0
- package/dist/cjs/calibrate.js +68 -0
- package/dist/cjs/constants.d.ts +27 -0
- package/dist/cjs/constants.js +47 -0
- package/dist/cjs/difficulty.d.ts +44 -0
- package/dist/cjs/difficulty.js +111 -0
- package/dist/cjs/index.d.ts +28 -0
- package/dist/cjs/index.js +88 -0
- package/dist/cjs/magic.d.ts +19 -0
- package/dist/cjs/magic.js +35 -0
- package/dist/cjs/mine-wasm.d.ts +69 -0
- package/dist/cjs/mine-wasm.js +214 -0
- package/dist/cjs/mine.d.ts +131 -0
- package/dist/cjs/mine.js +407 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/serialization.d.ts +106 -0
- package/dist/cjs/serialization.js +160 -0
- package/dist/cjs/verify.d.ts +76 -0
- package/dist/cjs/verify.js +232 -0
- package/dist/cjs/wasm/miner.wasm +0 -0
- package/dist/cjs/workers/browser.worker.d.ts +26 -0
- package/dist/cjs/workers/browser.worker.js +63 -0
- package/dist/cjs/workers/node.worker.d.ts +33 -0
- package/dist/cjs/workers/node.worker.js +47 -0
- package/dist/constants.js +5 -8
- package/dist/difficulty.d.ts +24 -0
- package/dist/difficulty.js +54 -6
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -62
- package/dist/magic.js +12 -15
- package/dist/mine-wasm.js +8 -15
- package/dist/mine.js +23 -31
- package/dist/serialization.js +28 -34
- package/dist/verify.js +15 -20
- package/dist/workers/browser.worker.js +4 -6
- package/dist/workers/node.worker.js +2 -3
- package/package.json +34 -15
package/dist/verify.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* verify.ts — TxPoW proof-of-work verification.
|
|
4
3
|
*
|
|
@@ -30,14 +29,10 @@
|
|
|
30
29
|
* writeHashToStream(mPRNG) — [0x00,0x00,0x00,0x20, ...32 bytes] 36 bytes
|
|
31
30
|
* writeMiniData(mTxnDifficulty) — [0x00,0x00,0x00,len, ...len bytes] 4+len bytes
|
|
32
31
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const core_1 = require("@totemsdk/core");
|
|
38
|
-
const mine_js_1 = require("./mine.js");
|
|
39
|
-
const serialization_js_1 = require("./serialization.js");
|
|
40
|
-
const constants_js_1 = require("./constants.js");
|
|
32
|
+
import { sha3_256 } from '@totemsdk/core';
|
|
33
|
+
import { isLessThan } from './mine.js';
|
|
34
|
+
import { computeTxPoWId } from './serialization.js';
|
|
35
|
+
import { TX_POW_MIN_DIFFICULTY } from './constants.js';
|
|
41
36
|
function toHex(bytes) {
|
|
42
37
|
return '0x' + Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
|
|
43
38
|
}
|
|
@@ -57,7 +52,7 @@ function readUint32BE(bytes, offset) {
|
|
|
57
52
|
* @param txpowId The 32-byte TxPoW ID (SHA3-256 of the header).
|
|
58
53
|
* @param mTxnDifficulty The 32-byte difficulty target from the TxBody.
|
|
59
54
|
*/
|
|
60
|
-
function verifyProofOfWork(txpowId, mTxnDifficulty) {
|
|
55
|
+
export function verifyProofOfWork(txpowId, mTxnDifficulty) {
|
|
61
56
|
if (txpowId.length !== 32) {
|
|
62
57
|
return {
|
|
63
58
|
valid: false,
|
|
@@ -74,7 +69,7 @@ function verifyProofOfWork(txpowId, mTxnDifficulty) {
|
|
|
74
69
|
reason: `mTxnDifficulty must be 32 bytes, got ${mTxnDifficulty.length}`,
|
|
75
70
|
};
|
|
76
71
|
}
|
|
77
|
-
const valid =
|
|
72
|
+
const valid = isLessThan(txpowId, mTxnDifficulty);
|
|
78
73
|
return {
|
|
79
74
|
valid,
|
|
80
75
|
txpowId: toHex(txpowId),
|
|
@@ -129,7 +124,7 @@ function tryParseTxPoW(bytes) {
|
|
|
129
124
|
}
|
|
130
125
|
const embeddedBodyHash = headerBytes.slice(hashFieldStart + 4);
|
|
131
126
|
const bodyBytes = bytes.slice(i + 1);
|
|
132
|
-
const actualBodyHash =
|
|
127
|
+
const actualBodyHash = sha3_256(bodyBytes);
|
|
133
128
|
if (actualBodyHash.every((b, j) => b === embeddedBodyHash[j])) {
|
|
134
129
|
return { headerBytes, bodyBytes };
|
|
135
130
|
}
|
|
@@ -177,9 +172,9 @@ function extractTxnDifficulty(bodyBytes) {
|
|
|
177
172
|
* @param headerBytes Raw TxHeader bytes (SHA3-256 of these is the txpowId).
|
|
178
173
|
* @param bodyBytes Raw TxBody bytes (mTxnDifficulty extracted from here).
|
|
179
174
|
*/
|
|
180
|
-
function verifyTxPoWParts(headerBytes, bodyBytes) {
|
|
181
|
-
const txpowId =
|
|
182
|
-
const difficulty = extractTxnDifficulty(bodyBytes) ??
|
|
175
|
+
export function verifyTxPoWParts(headerBytes, bodyBytes) {
|
|
176
|
+
const txpowId = computeTxPoWId(headerBytes);
|
|
177
|
+
const difficulty = extractTxnDifficulty(bodyBytes) ?? TX_POW_MIN_DIFFICULTY;
|
|
183
178
|
return verifyProofOfWork(txpowId, difficulty);
|
|
184
179
|
}
|
|
185
180
|
/**
|
|
@@ -200,12 +195,12 @@ function verifyTxPoWParts(headerBytes, bodyBytes) {
|
|
|
200
195
|
*
|
|
201
196
|
* @param txpowHex Hex-encoded serialized TxPoW (TxHeader | 0x01 | TxBody).
|
|
202
197
|
*/
|
|
203
|
-
function verifyTxPoWWork(txpowHex) {
|
|
198
|
+
export function verifyTxPoWWork(txpowHex) {
|
|
204
199
|
if (typeof txpowHex !== 'string' || !/^[0-9a-fA-F]+$/.test(txpowHex)) {
|
|
205
200
|
return {
|
|
206
201
|
valid: false,
|
|
207
202
|
txpowId: '',
|
|
208
|
-
difficulty: toHex(
|
|
203
|
+
difficulty: toHex(TX_POW_MIN_DIFFICULTY),
|
|
209
204
|
reason: 'invalid hex encoding',
|
|
210
205
|
};
|
|
211
206
|
}
|
|
@@ -213,7 +208,7 @@ function verifyTxPoWWork(txpowHex) {
|
|
|
213
208
|
return {
|
|
214
209
|
valid: false,
|
|
215
210
|
txpowId: '',
|
|
216
|
-
difficulty: toHex(
|
|
211
|
+
difficulty: toHex(TX_POW_MIN_DIFFICULTY),
|
|
217
212
|
reason: 'TxPoW too short (spam filter: minimum 100 bytes)',
|
|
218
213
|
};
|
|
219
214
|
}
|
|
@@ -224,9 +219,9 @@ function verifyTxPoWWork(txpowHex) {
|
|
|
224
219
|
return verifyTxPoWParts(parsed.headerBytes, parsed.bodyBytes);
|
|
225
220
|
}
|
|
226
221
|
// Fallback: use sha3_256 of the full bytes as a proxy id against the floor
|
|
227
|
-
const proxyId =
|
|
222
|
+
const proxyId = computeTxPoWId(bytes);
|
|
228
223
|
return {
|
|
229
|
-
...verifyProofOfWork(proxyId,
|
|
224
|
+
...verifyProofOfWork(proxyId, TX_POW_MIN_DIFFICULTY),
|
|
230
225
|
reason: 'could not parse TxPoW structure — used proxy id against floor difficulty',
|
|
231
226
|
};
|
|
232
227
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* browser.worker.ts — Browser Web Worker entry point for TxPoW mining.
|
|
4
3
|
*
|
|
@@ -24,19 +23,18 @@
|
|
|
24
23
|
*
|
|
25
24
|
* See mine.ts for details on the BigInt string encoding and Uint8Array [] encoding.
|
|
26
25
|
*/
|
|
27
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
26
|
/// <reference lib="webworker" />
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
import { mineTxPoWInProcess } from '../mine.js';
|
|
28
|
+
import { setWasmUrl } from '../mine-wasm.js';
|
|
31
29
|
self.onmessage = async (e) => {
|
|
32
30
|
const input = e.data;
|
|
33
31
|
// Configure WASM inside this worker's JS context before mining starts.
|
|
34
32
|
// The main thread's setWasmUrl() call does not carry over to a Web Worker.
|
|
35
33
|
if (input.wasmUrl) {
|
|
36
|
-
|
|
34
|
+
setWasmUrl(input.wasmUrl);
|
|
37
35
|
}
|
|
38
36
|
try {
|
|
39
|
-
const result = await
|
|
37
|
+
const result = await mineTxPoWInProcess(new Uint8Array(input.txBodyBytes), new Uint8Array(input.txnDifficulty), {
|
|
40
38
|
chunkSize: input.chunkSize,
|
|
41
39
|
maxIterations: input.maxIterations !== undefined
|
|
42
40
|
? Number(input.maxIterations)
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* node.worker.ts — Node.js worker_threads entry point for TxPoW mining.
|
|
4
3
|
*
|
|
@@ -13,10 +12,9 @@
|
|
|
13
12
|
* to avoid structured-clone limitations on older Node.js versions.
|
|
14
13
|
* Uint8Arrays are transmitted as number[] for reliable structured-clone transfer.
|
|
15
14
|
*/
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
15
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
18
16
|
const { workerData, parentPort } = require('worker_threads');
|
|
19
|
-
const { mineTxPoWInProcess } = require('../mine');
|
|
17
|
+
const { mineTxPoWInProcess } = require('../mine.js');
|
|
20
18
|
async function run() {
|
|
21
19
|
const input = workerData;
|
|
22
20
|
try {
|
|
@@ -46,3 +44,4 @@ async function run() {
|
|
|
46
44
|
}
|
|
47
45
|
}
|
|
48
46
|
run();
|
|
47
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,28 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totemsdk/txpow",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "TxPoW envelope serialization, proof-of-work mining, and Machine Work Admission for the Minima protocol",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/cjs/index.js",
|
|
7
|
+
"types": "dist/cjs/index.d.ts",
|
|
8
|
+
"typesVersions": {
|
|
9
|
+
"*": {
|
|
10
|
+
"admission": [
|
|
11
|
+
"dist/cjs/admission/index.d.ts"
|
|
12
|
+
]
|
|
13
|
+
}
|
|
14
|
+
},
|
|
7
15
|
"exports": {
|
|
8
16
|
".": {
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
17
|
+
"import": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"require": {
|
|
22
|
+
"types": "./dist/cjs/index.d.ts",
|
|
23
|
+
"default": "./dist/cjs/index.js"
|
|
24
|
+
}
|
|
12
25
|
},
|
|
13
26
|
"./admission": {
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
27
|
+
"import": {
|
|
28
|
+
"types": "./dist/admission/index.d.ts",
|
|
29
|
+
"default": "./dist/admission/index.js"
|
|
30
|
+
},
|
|
31
|
+
"require": {
|
|
32
|
+
"types": "./dist/cjs/admission/index.d.ts",
|
|
33
|
+
"default": "./dist/cjs/admission/index.js"
|
|
34
|
+
}
|
|
17
35
|
}
|
|
18
36
|
},
|
|
19
37
|
"files": [
|
|
20
38
|
"dist",
|
|
21
39
|
"README.md",
|
|
22
|
-
"LICENSE"
|
|
40
|
+
"LICENSE",
|
|
41
|
+
"NOTICE"
|
|
23
42
|
],
|
|
24
43
|
"dependencies": {
|
|
25
|
-
"@totemsdk/core": "^1.2
|
|
44
|
+
"@totemsdk/core": "^1.4.2"
|
|
26
45
|
},
|
|
27
46
|
"devDependencies": {
|
|
28
47
|
"@types/jest": "^30.0.0",
|
|
@@ -37,7 +56,7 @@
|
|
|
37
56
|
"publishConfig": {
|
|
38
57
|
"access": "public"
|
|
39
58
|
},
|
|
40
|
-
"license": "
|
|
59
|
+
"license": "Apache-2.0",
|
|
41
60
|
"author": "Totem SDK",
|
|
42
61
|
"homepage": "https://docs.totem.ing",
|
|
43
62
|
"bugs": {
|
|
@@ -58,9 +77,9 @@
|
|
|
58
77
|
"utxo"
|
|
59
78
|
],
|
|
60
79
|
"scripts": {
|
|
61
|
-
"build": "npm run build:wasm && tsc",
|
|
62
|
-
"postbuild": "node -e \"const fs=require('fs'),p=require('path');const src=p.join(__dirname,'src','wasm','miner.wasm');const dst=p.join(__dirname,
|
|
63
|
-
"compile:wasm": "node scripts/compile-wasm.
|
|
80
|
+
"build": "npm run build:wasm && tsc && tsc -p tsconfig.cjs.json && node -e \"require('fs').writeFileSync('dist/cjs/package.json', JSON.stringify({type:'commonjs'})+'\\n')\"",
|
|
81
|
+
"postbuild": "node -e \"const fs=require('fs'),p=require('path');const src=p.join(__dirname,'src','wasm','miner.wasm');if(fs.existsSync(src)){for(const d of ['dist/wasm','dist/cjs/wasm']){const dst=p.join(__dirname,d,'miner.wasm');fs.mkdirSync(p.dirname(dst),{recursive:true});fs.copyFileSync(src,dst);}console.log('[@totemsdk/txpow] miner.wasm → dist/wasm/ + dist/cjs/wasm/')}else{console.log('[@totemsdk/txpow] Note: miner.wasm not compiled — JS fallback only. Run npm run build:wasm with Emscripten to enable WASM acceleration.')}\"",
|
|
82
|
+
"compile:wasm": "node scripts/compile-wasm.cjs",
|
|
64
83
|
"build:wasm": "npm run compile:wasm",
|
|
65
84
|
"clean": "rm -rf dist",
|
|
66
85
|
"test": "jest"
|