Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
283 lines
9.1 KiB
Python
283 lines
9.1 KiB
Python
from __future__ import annotations
|
|
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
|
|
import blake3
|
|
import httpx
|
|
import pytest
|
|
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
|
|
|
|
from prism_gateway.settlement import (
|
|
HoldAuthorization,
|
|
MintSettlementClient,
|
|
MintSettlementError,
|
|
UnconfiguredMintSettlement,
|
|
)
|
|
|
|
|
|
_SIGNING_KEY = Ed25519PrivateKey.generate()
|
|
_KEY_ID = "mint-test"
|
|
_PINNED_KEYS = {_KEY_ID: _SIGNING_KEY.public_key()}
|
|
|
|
|
|
def _commit(domain: bytes, *parts: str) -> str:
|
|
digest = hashlib.sha256()
|
|
digest.update(domain)
|
|
for index, part in enumerate(parts):
|
|
if index:
|
|
digest.update(b"\0")
|
|
digest.update(part.encode())
|
|
return digest.hexdigest()
|
|
|
|
|
|
def _terminal_evidence(
|
|
operation: str,
|
|
amount_micros: int,
|
|
*,
|
|
hold_id: str,
|
|
payer: str,
|
|
payee: str,
|
|
idempotency_key: str,
|
|
reference: str,
|
|
sequence: int = 1,
|
|
) -> tuple[str, dict]:
|
|
operation_body = (
|
|
{
|
|
"op": operation,
|
|
"hold_id": hold_id,
|
|
"amount_micros": amount_micros,
|
|
"to": payee,
|
|
"idem_key": idempotency_key,
|
|
"ref": reference,
|
|
}
|
|
if operation == "hold.capture"
|
|
else {
|
|
"op": operation,
|
|
"hold_id": hold_id,
|
|
"idem_key": idempotency_key,
|
|
"ref": reference,
|
|
}
|
|
)
|
|
operation_hash = blake3.blake3(
|
|
json.dumps(operation_body, sort_keys=True, separators=(",", ":")).encode()
|
|
).hexdigest()
|
|
receipt_hash = hashlib.sha256(f"receipt-{sequence}".encode()).hexdigest()
|
|
receipt = {
|
|
"seq": sequence,
|
|
"op": operation,
|
|
"amount_micros": amount_micros,
|
|
"op_hash": operation_hash,
|
|
"hash": receipt_hash,
|
|
"sig": f"receipt-signature-{sequence}",
|
|
}
|
|
receipt_payload = json.dumps(receipt, separators=(",", ":")).encode()
|
|
encoded_receipt = base64.urlsafe_b64encode(receipt_payload).rstrip(b"=").decode()
|
|
no_payee = _commit(b"mint-hold-settlement-party-v1\0", "no-payee", operation_hash)
|
|
claims = {
|
|
"version": 1,
|
|
"asset": "AIT",
|
|
"unit": "micro_usd",
|
|
"sequence": sequence,
|
|
"operation": operation,
|
|
"operation_hash": operation_hash,
|
|
"amount_micros": amount_micros,
|
|
"receipt_hash": receipt_hash,
|
|
"timestamp_ms": 1_700_000_000_000,
|
|
"reference_hash": _commit(b"mint-hold-settlement-reference-v1\0", reference),
|
|
"hold_commitment": _commit(
|
|
b"mint-hold-settlement-hold-v1\0", hold_id, operation_hash
|
|
),
|
|
"payer_commitment": _commit(
|
|
b"mint-hold-settlement-party-v1\0", "payer", payer, operation_hash
|
|
),
|
|
"payee_commitment": (
|
|
_commit(
|
|
b"mint-hold-settlement-party-v1\0",
|
|
"payee",
|
|
payee,
|
|
operation_hash,
|
|
)
|
|
if operation == "hold.capture"
|
|
else no_payee
|
|
),
|
|
"signer_key_id": _KEY_ID,
|
|
}
|
|
claims_payload = json.dumps(claims, separators=(",", ":")).encode()
|
|
digest = hashlib.sha256(
|
|
b"mint-hold-settlement-attestation-v1\0" + claims_payload
|
|
).digest()
|
|
attestation = {
|
|
"format": "mint-hold-settlement-attestation-v1",
|
|
"payload_base64url": base64.urlsafe_b64encode(claims_payload)
|
|
.rstrip(b"=")
|
|
.decode(),
|
|
"digest_sha256": digest.hex(),
|
|
"signer_key_id": _KEY_ID,
|
|
"signature_hex": _SIGNING_KEY.sign(digest).hex(),
|
|
}
|
|
return encoded_receipt, attestation
|
|
|
|
|
|
def _client(handler) -> MintSettlementClient:
|
|
transport = httpx.MockTransport(handler)
|
|
return MintSettlementClient(
|
|
"https://mint.test",
|
|
"mint-api-key",
|
|
"acct_prism_treasury",
|
|
{"owner": "acct_owner"},
|
|
_PINNED_KEYS,
|
|
client=httpx.Client(base_url="https://mint.test", transport=transport),
|
|
)
|
|
|
|
|
|
def test_mint_client_uses_stable_idempotency_and_validates_terminal_identity():
|
|
requests: list[tuple[str, dict]] = []
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
body = json.loads(request.content)
|
|
requests.append((request.url.path, body))
|
|
if request.url.path == "/v1/holds":
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"success": True,
|
|
"data": {"hold_id": "hold_1", "amount_micros": 50_000},
|
|
},
|
|
)
|
|
if request.url.path.endswith("/capture"):
|
|
receipt, attestation = _terminal_evidence(
|
|
"hold.capture",
|
|
12_345,
|
|
hold_id="hold_1",
|
|
payer="acct_owner",
|
|
payee="acct_prism_treasury",
|
|
idempotency_key=body["idem_key"],
|
|
reference=body["ref"],
|
|
)
|
|
return httpx.Response(
|
|
200,
|
|
headers={"AITP-Receipt": receipt},
|
|
json={
|
|
"success": True,
|
|
"data": {"hold_id": "hold_1", "attestation": attestation},
|
|
},
|
|
)
|
|
receipt, attestation = _terminal_evidence(
|
|
"hold.release",
|
|
50_000,
|
|
hold_id="hold_1",
|
|
payer="acct_owner",
|
|
payee="",
|
|
idempotency_key=body["idem_key"],
|
|
reference=body["ref"],
|
|
sequence=2,
|
|
)
|
|
return httpx.Response(
|
|
200,
|
|
headers={"AITP-Receipt": receipt},
|
|
json={
|
|
"success": True,
|
|
"data": {"hold_id": "hold_1", "attestation": attestation},
|
|
},
|
|
)
|
|
|
|
mint = _client(handler)
|
|
first = mint.authorize("owner", "correlation_1", 50_000)
|
|
second = mint.authorize("owner", "correlation_1", 50_000)
|
|
captured = mint.capture("owner", first, "gen_1", 12_345)
|
|
released = mint.release("owner", HoldAuthorization("hold_1", 50_000), "gen_2")
|
|
|
|
assert first == second == HoldAuthorization("hold_1", 50_000)
|
|
assert requests[0][1]["from"] == "acct_owner"
|
|
assert requests[0][1]["idem_key"] == requests[1][1]["idem_key"]
|
|
assert requests[2][1]["to"] == "acct_prism_treasury"
|
|
assert captured.operation == "hold.capture"
|
|
assert captured.amount_micros == 12_345
|
|
assert released.operation == "hold.release"
|
|
assert released.amount_micros == 50_000
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("data", "receipt"),
|
|
[
|
|
({"hold_id": "different_hold"}, "ignored-invalid-receipt"),
|
|
({"hold_id": "hold_1"}, "wrong-operation"),
|
|
({"hold_id": "hold_1"}, "wrong-amount"),
|
|
({"hold_id": "hold_1"}, "not-base64url"),
|
|
],
|
|
)
|
|
def test_mint_client_rejects_unbound_or_invalid_capture_evidence(data, receipt):
|
|
if receipt == "wrong-operation":
|
|
receipt = _terminal_evidence(
|
|
"hold.release",
|
|
12_345,
|
|
hold_id="hold_1",
|
|
payer="acct_owner",
|
|
payee="",
|
|
idempotency_key="unused",
|
|
reference="unused",
|
|
)[0]
|
|
elif receipt == "wrong-amount":
|
|
receipt = _terminal_evidence(
|
|
"hold.capture",
|
|
99,
|
|
hold_id="hold_1",
|
|
payer="acct_owner",
|
|
payee="acct_prism_treasury",
|
|
idempotency_key="unused",
|
|
reference="unused",
|
|
)[0]
|
|
|
|
def handler(_request: httpx.Request) -> httpx.Response:
|
|
return httpx.Response(
|
|
200,
|
|
headers={"AITP-Receipt": receipt},
|
|
json={"success": True, "data": data},
|
|
)
|
|
|
|
with pytest.raises(MintSettlementError) as raised:
|
|
_client(handler).capture(
|
|
"owner", HoldAuthorization("hold_1", 50_000), "gen_1", 12_345
|
|
)
|
|
assert raised.value.ambiguous is True
|
|
|
|
|
|
def test_mint_client_fails_closed_for_unknown_payer_before_network_io():
|
|
calls = 0
|
|
|
|
def handler(_request: httpx.Request) -> httpx.Response:
|
|
nonlocal calls
|
|
calls += 1
|
|
return httpx.Response(500)
|
|
|
|
with pytest.raises(MintSettlementError) as raised:
|
|
_client(handler).authorize("unknown-owner", "correlation_1", 10_000)
|
|
assert raised.value.status_code == 402
|
|
assert raised.value.ambiguous is False
|
|
assert calls == 0
|
|
|
|
|
|
def test_invalid_environment_configuration_is_fail_closed(monkeypatch):
|
|
monkeypatch.setenv("PRISM_MINT_SERVER_URL", "https://mint.test")
|
|
monkeypatch.setenv("PRISM_MINT_API_KEY", "key")
|
|
monkeypatch.setenv("PRISM_MINT_PAYEE_ACCOUNT", "treasury")
|
|
monkeypatch.setenv("PRISM_MINT_PAYER_ACCOUNTS_JSON", "not-json")
|
|
|
|
settlement = MintSettlementClient.from_environment()
|
|
assert isinstance(settlement, UnconfiguredMintSettlement)
|
|
with pytest.raises(MintSettlementError) as raised:
|
|
settlement.authorize("owner", "correlation", 10_000)
|
|
assert raised.value.status_code == 503
|
|
|
|
|
|
def test_transport_failure_is_ambiguous_and_safe_to_replay():
|
|
def handler(_request: httpx.Request) -> httpx.Response:
|
|
raise httpx.ReadError("connection reset")
|
|
|
|
with pytest.raises(MintSettlementError) as raised:
|
|
_client(handler).authorize("owner", "correlation_1", 10_000)
|
|
assert raised.value.status_code == 502
|
|
assert raised.value.ambiguous is True
|