Skip to content

Commit 4220071

Browse files
committed
fix rewards admin hmac import
1 parent 43e6d4b commit 4220071

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

node/rewards_implementation_rip200.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import sqlite3
1313
import time
1414
import os
15+
import hmac
1516
try:
1617
from flask import request, jsonify
1718
except ImportError:
@@ -286,7 +287,6 @@ def register_rewards_rip200(app, DB_PATH):
286287
@app.route('/rewards/settle', methods=['POST'])
287288
def settle_rewards():
288289
# ── Authentication: settlement is a privileged operation ──────
289-
import hmac
290290
settle_key = os.environ.get("RC_SETTLE_KEY", "")
291291
if not settle_key:
292292
return jsonify({"error": "RC_SETTLE_KEY not configured — settle endpoint disabled"}), 503
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# SPDX-License-Identifier: MIT
2+
3+
import sqlite3
4+
import sys
5+
6+
from flask import Flask
7+
8+
9+
rewards = sys.modules["rewards_mod"]
10+
ADMIN_KEY = "abcdefghijklmnopqrstuvwxyz123456"
11+
12+
13+
def _app_with_balances(tmp_path, monkeypatch):
14+
db_path = tmp_path / "rewards.db"
15+
with sqlite3.connect(db_path) as db:
16+
db.execute("CREATE TABLE balances (miner_id TEXT PRIMARY KEY, amount_i64 INTEGER)")
17+
db.execute("INSERT INTO balances VALUES (?, ?)", ("alice", 1_000_000))
18+
19+
monkeypatch.setenv("RC_ADMIN_KEY", ADMIN_KEY)
20+
app = Flask(__name__)
21+
app.config["TESTING"] = True
22+
rewards.register_rewards_rip200(app, str(db_path))
23+
return app
24+
25+
26+
def test_wallet_balance_admin_guard_uses_module_hmac(tmp_path, monkeypatch):
27+
app = _app_with_balances(tmp_path, monkeypatch)
28+
29+
response = app.test_client().get(
30+
"/wallet/balance?miner_id=alice",
31+
headers={"X-Admin-Key": ADMIN_KEY},
32+
)
33+
34+
assert response.status_code == 200
35+
assert response.get_json()["amount_i64"] == 1_000_000
36+
37+
38+
def test_wallet_balance_rejects_wrong_admin_key_without_500(tmp_path, monkeypatch):
39+
app = _app_with_balances(tmp_path, monkeypatch)
40+
41+
response = app.test_client().get(
42+
"/wallet/balance?miner_id=alice",
43+
headers={"X-Admin-Key": "wrong"},
44+
)
45+
46+
assert response.status_code == 401
47+
assert response.get_json()["error"].startswith("Unauthorized")
48+
49+
50+
def test_all_balances_admin_guard_uses_module_hmac(tmp_path, monkeypatch):
51+
app = _app_with_balances(tmp_path, monkeypatch)
52+
53+
response = app.test_client().get(
54+
"/wallet/balances/all",
55+
headers={"X-Admin-Key": ADMIN_KEY},
56+
)
57+
58+
assert response.status_code == 200
59+
assert response.get_json()["total_urtc"] == 1_000_000

0 commit comments

Comments
 (0)