nirium-avalanche 0.2.0 → 0.3.0
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/README.md +2 -1
- package/package.json +2 -1
- package/sdk/decision.mjs +108 -0
- package/sdk/index.mjs +2 -0
package/README.md
CHANGED
|
@@ -37,12 +37,13 @@ app. Settlement is done by PayAI, an independent x402 facilitator.
|
|
|
37
37
|
| `nirium-avalanche/policy` | The pre-sign policy gate (decodes what is about to be signed, asks your policy, signs only on a bound ALLOW) |
|
|
38
38
|
| `nirium-avalanche/policy/rules` | A small reference policy engine: max amount, approval line, allowlists |
|
|
39
39
|
| `nirium-avalanche/receipt` | `bindReceipt` and `verifyReceipt` on their own, without the Express middleware |
|
|
40
|
+
| `nirium-avalanche/decision` | Decision receipts: `bindDecision`, `verifyDecision`, `collectDecisions` (also exported from `nirium-avalanche`) |
|
|
40
41
|
| `nirium-avalanche/verify` | The receipt verifier as a library. No Node-only code, so it runs in Node and in the browser |
|
|
41
42
|
|
|
42
43
|
Check a receipt from the command line:
|
|
43
44
|
|
|
44
45
|
```bash
|
|
45
|
-
npx nirium-verify-evm receipt.json
|
|
46
|
+
npx -p nirium-avalanche nirium-verify-evm receipt.json
|
|
46
47
|
```
|
|
47
48
|
|
|
48
49
|
Exit code 0 means valid, 1 means a check failed, 2 means bad input.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nirium-avalanche",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Pay and charge with x402 on Avalanche, refuse a payment before it is signed, and keep a payer-signed receipt bound to the exact USDC transfer",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"./policy/x402-fetch": "./policy/x402-fetch.mjs",
|
|
30
30
|
"./policy/rules": "./policy/rules-policy.mjs",
|
|
31
31
|
"./receipt": "./sdk/receipt.mjs",
|
|
32
|
+
"./decision": "./sdk/decision.mjs",
|
|
32
33
|
"./verify": "./verify-evm-core.mjs",
|
|
33
34
|
"./package.json": "./package.json"
|
|
34
35
|
},
|
package/sdk/decision.mjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// Decision receipts ("nirium-decision-v1") on Avalanche: a payer-signed record of what the pre-sign
|
|
2
|
+
// policy decided about an x402 payment, including the ones that were never signed (DENY, WAIT and
|
|
3
|
+
// every other refusal), which leave no trace on the chain. Format: docs/decision-receipt-v1.md.
|
|
4
|
+
//
|
|
5
|
+
// No Node-only APIs: runs in Node and in the browser, like the receipt verifier.
|
|
6
|
+
|
|
7
|
+
import { getAddress, isAddressEqual, verifyMessage } from "viem";
|
|
8
|
+
import { canonicalize, contentSha256, STATEMENT_PREFIX } from "../verify-evm-core.mjs";
|
|
9
|
+
import { NETWORKS, USDC_DECIMALS } from "./networks.mjs";
|
|
10
|
+
|
|
11
|
+
export const DECISION_VERSION = "nirium-decision-v1";
|
|
12
|
+
export const DECISION_OUTCOMES = Object.freeze([
|
|
13
|
+
"ALLOWED", "SIGNED", "DENY", "WAIT", "TIMEOUT", "ENGINE_ERROR", "EXPIRED", "UNBOUND", "MALFORMED",
|
|
14
|
+
"STALE", "VERSION_UNAVAILABLE", "SIGNER_CHANGED", "CLOCK_INVALID",
|
|
15
|
+
]);
|
|
16
|
+
const EVM_NETWORK = /^eip155:\d{1,12}$/;
|
|
17
|
+
const HEX64 = /^[0-9a-f]{64}$/;
|
|
18
|
+
const nonempty = (v) => typeof v === "string" && v.trim().length > 0;
|
|
19
|
+
const sameAddr = (a, b) => { try { return isAddressEqual(a, b); } catch { return false; } };
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The record for one policy decision, in canonical key order, or null when the policy refused
|
|
23
|
+
* before it had decoded a payment (CONTEXT_MISMATCH): there is nothing to describe then.
|
|
24
|
+
* @param {object} policyRecord what the policy passed to onDecision: { outcome, context, decision, error }
|
|
25
|
+
* @param {object} [o] { ref, note, labels }
|
|
26
|
+
*/
|
|
27
|
+
export function decisionRecord(policyRecord, { ref, note, labels = [] } = {}) {
|
|
28
|
+
const c = policyRecord?.context;
|
|
29
|
+
const a = c?.authorization;
|
|
30
|
+
const network = c?.requirements?.network;
|
|
31
|
+
if (!a || !network) return null;
|
|
32
|
+
const outcome = policyRecord.outcome;
|
|
33
|
+
if (!DECISION_OUTCOMES.includes(outcome)) throw new Error(`decisionRecord: unknown outcome ${outcome}`);
|
|
34
|
+
const usdc = NETWORKS[network]?.usdc;
|
|
35
|
+
if (!usdc || !sameAddr(a.verifyingContract, usdc)) throw new Error(`decisionRecord: ${a.verifyingContract} is not USDC on ${network}`);
|
|
36
|
+
const d = policyRecord.decision ?? {};
|
|
37
|
+
const decision = { outcome };
|
|
38
|
+
if (nonempty(d.policyVersion)) decision.policyVersion = String(d.policyVersion);
|
|
39
|
+
const reason = nonempty(d.reason) ? d.reason : (nonempty(policyRecord.error) ? policyRecord.error : undefined);
|
|
40
|
+
if (reason) decision.reason = String(reason).slice(0, 280);
|
|
41
|
+
const intent = {
|
|
42
|
+
amount: String(a.amount), asset: getAddress(a.verifyingContract), contextHash: String(c.contextHash),
|
|
43
|
+
decimals: USDC_DECIMALS, from: getAddress(a.from), method: String(c.method), network: String(network),
|
|
44
|
+
nonce: String(a.nonce), payloadHash: String(a.payloadHash), resource: String(c.url), to: getAddress(a.to),
|
|
45
|
+
};
|
|
46
|
+
const record = { decision, intent, labels: [...labels].map(String) };
|
|
47
|
+
if (nonempty(ref)) record.purpose = note ? { note: String(note), ref } : { ref };
|
|
48
|
+
return JSON.parse(canonicalize(record));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Builds and signs a decision receipt with the payer's viem account (EIP-191 over the statement).
|
|
53
|
+
* Returns { doc, record } (null when there is no decoded payment). With hashOnly, `doc` has no
|
|
54
|
+
* record: keep `record` yourself to show it later.
|
|
55
|
+
*/
|
|
56
|
+
export async function bindDecision(policyRecord, { signer, ref, note, labels, hashOnly = false } = {}) {
|
|
57
|
+
const record = decisionRecord(policyRecord, { ref, note, labels });
|
|
58
|
+
if (!record) return null;
|
|
59
|
+
if (!signer || typeof signer.signMessage !== "function") throw new Error("bindDecision: signer must be the payer's viem account (signMessage)");
|
|
60
|
+
if (!sameAddr(signer.address, record.intent.from)) throw new Error(`bindDecision: signer ${signer.address} is not the payer ${record.intent.from}`);
|
|
61
|
+
const content_sha256 = contentSha256(record);
|
|
62
|
+
const statement = STATEMENT_PREFIX + content_sha256;
|
|
63
|
+
const agent = { alg: "eip191-secp256k1", key: getAddress(signer.address), signature: await signer.signMessage({ message: statement }), statement };
|
|
64
|
+
const network = record.intent.network;
|
|
65
|
+
const doc = hashOnly
|
|
66
|
+
? { version: DECISION_VERSION, content_sha256, agent, network }
|
|
67
|
+
: { version: DECISION_VERSION, content_sha256, record, agent, network };
|
|
68
|
+
return { doc, record };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Checks a decision receipt: format, hash (with a record), payer signature. Nothing on-chain: a
|
|
73
|
+
* refusal leaves nothing there. For a hash-only document, pass { record } to check that it is the
|
|
74
|
+
* record behind content_sha256. Returns { ok, checks: [{ name, ok, detail }] }.
|
|
75
|
+
*/
|
|
76
|
+
export async function verifyDecision(doc, { record: heldRecord } = {}) {
|
|
77
|
+
const checks = [];
|
|
78
|
+
const check = (name, ok, detail) => { checks.push({ name, ok: Boolean(ok), ...(detail ? { detail } : {}) }); return Boolean(ok); };
|
|
79
|
+
const d = doc ?? {};
|
|
80
|
+
check("version == nirium-decision-v1", d.version === DECISION_VERSION, String(d.version));
|
|
81
|
+
check("network is an EVM CAIP-2 id", EVM_NETWORK.test(String(d.network)), String(d.network));
|
|
82
|
+
check("content_sha256 is 64 hex", HEX64.test(String(d.content_sha256)));
|
|
83
|
+
const record = d.record ?? heldRecord;
|
|
84
|
+
if (record) {
|
|
85
|
+
let canon;
|
|
86
|
+
try { canon = canonicalize(record); } catch { canon = null; }
|
|
87
|
+
check("content_sha256 == sha256(canonical(record))", canon !== null && contentSha256(record) === d.content_sha256,
|
|
88
|
+
d.record ? undefined : "record held by the verifier (hash-only document)");
|
|
89
|
+
check("decision.outcome is a known outcome", DECISION_OUTCOMES.includes(record?.decision?.outcome), String(record?.decision?.outcome));
|
|
90
|
+
check("record.intent.network == network", record?.intent?.network === d.network);
|
|
91
|
+
}
|
|
92
|
+
const ag = d.agent ?? {};
|
|
93
|
+
const statementOk = check("agent.statement == nirium-audit-v1:<content_sha256>", ag.statement === STATEMENT_PREFIX + d.content_sha256);
|
|
94
|
+
check("agent.alg == eip191-secp256k1", ag.alg === "eip191-secp256k1", String(ag.alg));
|
|
95
|
+
if (record) check("agent.key == record.intent.from (the payer signs)", sameAddr(ag.key, record?.intent?.from));
|
|
96
|
+
let sigOk = false;
|
|
97
|
+
if (statementOk && typeof ag.key === "string" && typeof ag.signature === "string") {
|
|
98
|
+
try { sigOk = await verifyMessage({ address: ag.key, message: ag.statement, signature: ag.signature }); } catch { sigOk = false; }
|
|
99
|
+
}
|
|
100
|
+
check("EIP-191 signature by agent.key over agent.statement", sigOk);
|
|
101
|
+
return { ok: checks.every((c) => c.ok), checks };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** An onDecision observer that keeps every policy record, to sign after the payment attempt. */
|
|
105
|
+
export function collectDecisions() {
|
|
106
|
+
const records = [];
|
|
107
|
+
return { records, onDecision: (r) => { records.push(r); } };
|
|
108
|
+
}
|
package/sdk/index.mjs
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
// pay: createX402Fetch, settlementOf (optional pre-sign policy)
|
|
3
3
|
// charge: x402Serve (Express middleware, PayAI by default)
|
|
4
4
|
// prove: bindReceipt, verifyReceipt (payer-signed receipt bound to the transfer)
|
|
5
|
+
// decide: bindDecision, verifyDecision (payer-signed receipt of a policy decision, incl. DENY/WAIT)
|
|
5
6
|
export { createX402Fetch, settlementOf } from "./pay.mjs";
|
|
6
7
|
export { x402Serve } from "./serve.mjs";
|
|
7
8
|
export { bindReceipt, verifyReceipt } from "./receipt.mjs";
|
|
9
|
+
export { bindDecision, verifyDecision, decisionRecord, collectDecisions, DECISION_VERSION, DECISION_OUTCOMES } from "./decision.mjs";
|
|
8
10
|
export { NETWORKS, USDC_EXTRA, PAYAI_FACILITATOR, getNetwork, dollarsToAtomic } from "./networks.mjs";
|
|
9
11
|
export { createRulesPolicy } from "../policy/rules-policy.mjs";
|
|
10
12
|
export { X402PolicyError } from "../policy/evm-policy.mjs";
|