@metalntwx/metal-pay 0.2.0 → 0.3.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.
Files changed (75) hide show
  1. package/README.md +104 -38
  2. package/dist/clearing-abi.d.ts +1836 -0
  3. package/dist/clearing-abi.js +2361 -0
  4. package/dist/clearing-client.d.ts +96 -159
  5. package/dist/clearing-client.js +412 -553
  6. package/dist/clearing-codec.d.ts +57 -0
  7. package/dist/clearing-codec.js +173 -0
  8. package/dist/clearing-custody.d.ts +202 -0
  9. package/dist/clearing-custody.js +552 -0
  10. package/dist/clearing-server.d.ts +30 -49
  11. package/dist/clearing-server.js +87 -67
  12. package/dist/clearing-x402.d.ts +38 -0
  13. package/dist/clearing-x402.js +249 -0
  14. package/dist/client.d.ts +14 -15
  15. package/dist/client.js +53 -22
  16. package/dist/index.d.ts +4 -1
  17. package/dist/index.js +4 -1
  18. package/dist/mandates/account.d.ts +9 -4
  19. package/dist/mandates/account.js +15 -11
  20. package/dist/mandates/binding.d.ts +4 -27
  21. package/dist/mandates/binding.js +5 -9
  22. package/dist/mandates/checkout.js +5 -5
  23. package/dist/mandates/client.js +4 -3
  24. package/dist/mandates/l3a.d.ts +2 -2
  25. package/dist/mandates/l3a.js +5 -4
  26. package/dist/mandates/seller.d.ts +1 -1
  27. package/dist/mandates/seller.js +4 -4
  28. package/dist/mandates/verify.d.ts +4 -6
  29. package/dist/mandates/verify.js +147 -82
  30. package/dist/mandates/vi/credential.js +4 -4
  31. package/dist/networks.d.ts +9 -1
  32. package/dist/networks.js +105 -4
  33. package/dist/node.d.ts +11 -0
  34. package/dist/node.js +97 -0
  35. package/package.json +18 -11
  36. package/src/clearing-abi.ts +2361 -0
  37. package/src/clearing-client.test.ts +850 -0
  38. package/src/clearing-client.ts +612 -0
  39. package/src/clearing-codec.test.ts +57 -0
  40. package/src/clearing-codec.ts +251 -0
  41. package/src/clearing-custody.test.ts +763 -0
  42. package/src/clearing-custody.ts +743 -0
  43. package/src/clearing-fixture.json +37 -0
  44. package/src/clearing-server.test.ts +450 -0
  45. package/src/clearing-server.ts +123 -0
  46. package/src/clearing-x402.test.ts +181 -0
  47. package/src/clearing-x402.ts +328 -0
  48. package/src/client.test.ts +333 -0
  49. package/src/client.ts +195 -0
  50. package/src/index.ts +33 -0
  51. package/src/mandates/account.ts +95 -0
  52. package/src/mandates/binding.ts +280 -0
  53. package/src/mandates/chain.ts +68 -0
  54. package/src/mandates/checkout.ts +368 -0
  55. package/src/mandates/client.ts +159 -0
  56. package/src/mandates/extension.ts +48 -0
  57. package/src/mandates/index.ts +121 -0
  58. package/src/mandates/ingest.ts +42 -0
  59. package/src/mandates/jcs.ts +16 -0
  60. package/src/mandates/l3a.ts +216 -0
  61. package/src/mandates/seller.ts +91 -0
  62. package/src/mandates/verify.ts +625 -0
  63. package/src/mandates/vi/credential.ts +477 -0
  64. package/src/mandates/vi/issue.ts +118 -0
  65. package/src/mandates/vi/sd-jwt.ts +189 -0
  66. package/src/networks.test.ts +177 -0
  67. package/src/networks.ts +242 -0
  68. package/src/node.test.ts +24 -0
  69. package/src/node.ts +96 -0
  70. package/src/server.test.ts +223 -0
  71. package/src/server.ts +77 -0
  72. package/dist/clearing.d.ts +0 -204
  73. package/dist/clearing.js +0 -151
  74. package/dist/file-storage.d.ts +0 -39
  75. package/dist/file-storage.js +0 -195
package/dist/node.js ADDED
@@ -0,0 +1,97 @@
1
+ import { DatabaseSync } from "node:sqlite";
2
+ import { mkdir, open, readFile, rename } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { randomBytes } from "node:crypto";
5
+ import { createClearingSigner } from "./clearing-codec.js";
6
+ /** A persistent P-256 account and receipt journal for a single server process. */
7
+ export async function openClearingWallet(directory) {
8
+ await mkdir(directory, { recursive: true, mode: 0o700 });
9
+ // SQLite holds an OS lock for the wallet lifetime and releases it on crash.
10
+ const owner = new DatabaseSync(join(directory, "owner.sqlite"));
11
+ try {
12
+ owner.exec("PRAGMA locking_mode=EXCLUSIVE; BEGIN EXCLUSIVE; CREATE TABLE IF NOT EXISTS owner (id INTEGER)");
13
+ }
14
+ catch (error) {
15
+ owner.close();
16
+ throw new Error("clearing wallet is already in use", { cause: error });
17
+ }
18
+ try {
19
+ const keyPath = join(directory, "account.key");
20
+ let key;
21
+ try {
22
+ key = (await readFile(keyPath, "utf8")).trim();
23
+ }
24
+ catch (error) {
25
+ if (error.code !== "ENOENT")
26
+ throw error;
27
+ for (;;) {
28
+ key = `0x${randomBytes(32).toString("hex")}`;
29
+ try {
30
+ createClearingSigner(key);
31
+ break;
32
+ }
33
+ catch {
34
+ /* Rejection sample the scalar. */
35
+ }
36
+ }
37
+ const file = await open(keyPath, "wx", 0o600);
38
+ try {
39
+ await file.writeFile(key);
40
+ await file.sync();
41
+ }
42
+ finally {
43
+ await file.close();
44
+ }
45
+ }
46
+ const parent = await open(directory, "r");
47
+ try {
48
+ await parent.sync();
49
+ }
50
+ finally {
51
+ await parent.close();
52
+ }
53
+ const signer = createClearingSigner(key);
54
+ const statePath = join(directory, "payments.json");
55
+ const store = {
56
+ async load() {
57
+ try {
58
+ return JSON.parse(await readFile(statePath, "utf8"));
59
+ }
60
+ catch (error) {
61
+ if (error.code === "ENOENT")
62
+ return null;
63
+ throw error;
64
+ }
65
+ },
66
+ async save(state) {
67
+ const file = await open(statePath + ".next", "w", 0o600);
68
+ try {
69
+ await file.writeFile(JSON.stringify(state));
70
+ await file.sync();
71
+ }
72
+ finally {
73
+ await file.close();
74
+ }
75
+ await rename(statePath + ".next", statePath);
76
+ const parent = await open(directory, "r");
77
+ try {
78
+ await parent.sync();
79
+ }
80
+ finally {
81
+ await parent.close();
82
+ }
83
+ },
84
+ };
85
+ return {
86
+ signer,
87
+ store,
88
+ async close() {
89
+ owner.close();
90
+ },
91
+ };
92
+ }
93
+ catch (error) {
94
+ owner.close();
95
+ throw error;
96
+ }
97
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@metalntwx/metal-pay",
3
- "version": "0.2.0",
4
- "description": "Agent payments on Metal for the official x402 packages: the Metal devnet and testnet as `exact`-scheme networks priced in `*USDZ` for payers and sellers, a one-call payment client with a spending limit, Metal's seller token, the clearing lane for payers who pay per call without a transaction each time, and mandates, which let an agent pay from its owner's account within limits Metal enforces.",
3
+ "version": "0.3.1",
4
+ "description": "Agent payments on Metal for the official x402 packages: native Bajillion clearing, on-chain exact payments, durable payer wallets, seller integration, and mandates, which let an agent pay from its owner's account within limits Metal enforces.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -9,6 +9,9 @@
9
9
  "directory": "sdks/metal-pay"
10
10
  },
11
11
  "type": "module",
12
+ "engines": {
13
+ "node": "^22.13.0 || >=23.4.0"
14
+ },
12
15
  "main": "./dist/index.js",
13
16
  "types": "./dist/index.d.ts",
14
17
  "exports": {
@@ -16,9 +19,9 @@
16
19
  "types": "./dist/index.d.ts",
17
20
  "default": "./dist/index.js"
18
21
  },
19
- "./file-storage": {
20
- "types": "./dist/file-storage.d.ts",
21
- "default": "./dist/file-storage.js"
22
+ "./node": {
23
+ "types": "./dist/node.d.ts",
24
+ "default": "./dist/node.js"
22
25
  },
23
26
  "./mandates": {
24
27
  "types": "./dist/mandates/index.d.ts",
@@ -26,21 +29,25 @@
26
29
  }
27
30
  },
28
31
  "files": [
29
- "dist"
32
+ "dist",
33
+ "src"
30
34
  ],
31
35
  "dependencies": {
32
- "@noble/curves": "^1.9.1",
33
- "@x402/core": "^2.21.0",
34
- "@x402/evm": "^2.21.0",
35
- "@x402/fetch": "^2.21.0"
36
+ "@noble/curves": "1.9.7"
36
37
  },
37
38
  "peerDependencies": {
39
+ "@x402/core": ">=2.28.0 <3",
40
+ "@x402/evm": ">=2.28.0 <3",
41
+ "@x402/fetch": ">=2.28.0 <3",
38
42
  "viem": "^2"
39
43
  },
40
44
  "devDependencies": {
41
45
  "@metalntwx/metal-ts": "0.3.2",
42
46
  "@types/node": "^24.13.2",
43
- "@x402/hono": "2.21.0",
47
+ "@x402/core": "2.28.0",
48
+ "@x402/evm": "2.28.0",
49
+ "@x402/fetch": "2.28.0",
50
+ "@x402/hono": "2.28.0",
44
51
  "hono": "4.13.7",
45
52
  "tsx": "^4.23.0",
46
53
  "typescript": "~6.0.2",