@totemsdk/txpow 1.0.1 → 1.0.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.
@@ -109,33 +109,45 @@ async function verifyWorkAdmission(action, challenge, proof, templateProvider, o
109
109
  if (!isLessThan(txpowId, admissionTarget)) {
110
110
  return { valid: false, reason: 'txpowId does not beat admission target' };
111
111
  }
112
- // 5. Template freshness for admission
113
- const freshness = (0, template_js_1.templateFreshness)(proof.template, options?.latestTemplate ?? null, {
112
+ // 5. Resolve a trusted template. Template fields — notably `blockDifficulty`,
113
+ // which drives the Super-level claim — must come from a freshly-fetched
114
+ // trusted template, never from the prover's copy (RFC-020 H4).
115
+ let trustedTemplate = options?.latestTemplate ?? null;
116
+ if (templateProvider) {
117
+ if (templateProvider.getLatestTemplate) {
118
+ trustedTemplate = await templateProvider.getLatestTemplate();
119
+ }
120
+ else if (options?.latestTemplate) {
121
+ trustedTemplate = options.latestTemplate;
122
+ }
123
+ else {
124
+ trustedTemplate = await templateProvider.getCurrentTemplate();
125
+ }
126
+ }
127
+ // 5a. Template freshness for admission (uses the proof's capture time).
128
+ const freshness = (0, template_js_1.templateFreshness)(proof.template, trustedTemplate, {
114
129
  admissionWindowMs: options?.admissionWindowMs,
115
130
  now: options?.now,
116
131
  });
117
132
  if (!freshness.admissionValid) {
118
133
  return { valid: false, reason: 'proof template is stale for admission' };
119
134
  }
120
- // 6. Level B: independently recompute the exact Minima Super level. Never
121
- // trust proof.superLevel / proof.isBlock / proof.qualifiesAsMinimaBlock.
122
- const superLevel = (0, template_js_1.computeSuperLevel)(txpowId, proof.template.blockDifficulty);
123
- const isBlock = superLevel >= 0;
124
- // 7. Level C: broadcastability requires a live template provider AND a
125
- // current template. Offline mode leaves broadcastable undefined.
135
+ // 5b. The prover's declared block difficulty must byte-match the trusted
136
+ // template — otherwise a prover could set a max difficulty to fake a
137
+ // Minima block.
138
+ if (trustedTemplate && trustedTemplate.blockDifficulty !== proof.template.blockDifficulty) {
139
+ return { valid: false, reason: 'proof template blockDifficulty does not match the trusted template' };
140
+ }
141
+ // 6. Level B: recompute the exact Minima Super level from the TRUSTED block
142
+ // difficulty and the re-derived txpowId. Without a trusted template we
143
+ // cannot claim Minima block contribution (isBlock = false).
144
+ const superLevel = trustedTemplate ? (0, template_js_1.computeSuperLevel)(txpowId, trustedTemplate.blockDifficulty) : -1;
145
+ const isBlock = trustedTemplate ? superLevel >= 0 : false;
146
+ // 7. Level C: broadcastability requires a live template provider, a current
147
+ // template, and isBlock. Offline mode leaves broadcastable undefined.
126
148
  let broadcastable;
127
149
  if (templateProvider) {
128
- let latest;
129
- if (templateProvider.getLatestTemplate) {
130
- latest = await templateProvider.getLatestTemplate();
131
- }
132
- else if (options?.latestTemplate) {
133
- latest = options.latestTemplate;
134
- }
135
- else {
136
- latest = await templateProvider.getCurrentTemplate();
137
- }
138
- broadcastable = isBlock && latest.templateId === proof.template.templateId;
150
+ broadcastable = isBlock && trustedTemplate !== null && freshness.broadcastable;
139
151
  }
140
152
  return { valid: true, superLevel, isBlock, broadcastable };
141
153
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@totemsdk/txpow",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "TxPoW envelope serialization, proof-of-work mining, and Machine Work Admission for the Minima protocol",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,7 +22,7 @@
22
22
  "LICENSE"
23
23
  ],
24
24
  "dependencies": {
25
- "@totemsdk/core": "^1.2.10"
25
+ "@totemsdk/core": "^1.2.11"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/jest": "^30.0.0",