@usemilkyway/agent-sdk 0.1.2 → 0.1.3
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/dist/verify.d.ts +1 -1
- package/dist/verify.d.ts.map +1 -1
- package/dist/verify.js +38 -71
- package/dist/verify.js.map +1 -1
- package/package.json +1 -2
package/dist/verify.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function verifyPayment(paymentHeader: string,
|
|
1
|
+
export declare function verifyPayment(paymentHeader: string, resource: string, amountUsdc: string, network?: string): Promise<void>;
|
|
2
2
|
//# sourceMappingURL=verify.d.ts.map
|
package/dist/verify.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../src/verify.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../src/verify.ts"],"names":[],"mappings":"AAIA,wBAAsB,aAAa,CACjC,aAAa,EAAE,MAAM,EACrB,QAAQ,EAAO,MAAM,EACrB,UAAU,EAAK,MAAM,EACrB,OAAO,CAAC,EAAO,MAAM,GACpB,OAAO,CAAC,IAAI,CAAC,CAoDf"}
|
package/dist/verify.js
CHANGED
|
@@ -1,83 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.verifyPayment = verifyPayment;
|
|
4
|
-
const ethers_1 = require("ethers");
|
|
5
4
|
const errors_js_1 = require("./errors.js");
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
version: "2",
|
|
22
|
-
chainId: CHAIN_ID,
|
|
23
|
-
verifyingContract: USDC_ADDRESS,
|
|
24
|
-
};
|
|
25
|
-
async function verifyPayment(paymentHeader, _resource, amountUsdc) {
|
|
26
|
-
const rawAmount = BigInt(Math.round(parseFloat(amountUsdc) * 1000000));
|
|
27
|
-
let payload;
|
|
5
|
+
const MILKYWAY_FACILITATOR = "https://facilitator.usemilkyway.com";
|
|
6
|
+
async function verifyPayment(paymentHeader, resource, amountUsdc, network) {
|
|
7
|
+
const facilitatorUrl = process.env.X402_FACILITATOR_URL || MILKYWAY_FACILITATOR;
|
|
8
|
+
const secret = process.env.FACILITATOR_SECRET;
|
|
9
|
+
if (!secret) {
|
|
10
|
+
throw new errors_js_1.PaymentError("FACILITATOR_SECRET is not set. " +
|
|
11
|
+
"Get it from usemilkyway.com/settings and add it to your .env");
|
|
12
|
+
}
|
|
13
|
+
const resolvedNetwork = network
|
|
14
|
+
|| process.env.X402_NETWORK
|
|
15
|
+
|| (process.env.NODE_ENV === "production"
|
|
16
|
+
? "eip155:42161"
|
|
17
|
+
: "eip155:421614");
|
|
18
|
+
const rawAmount = String(Math.round(parseFloat(amountUsdc) * 1000000));
|
|
19
|
+
let res;
|
|
28
20
|
try {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
if (now > Number(payload.validBefore)) {
|
|
44
|
-
throw new errors_js_1.PaymentError("Payment authorization expired");
|
|
45
|
-
}
|
|
46
|
-
// Recover signer from EIP-712 signature
|
|
47
|
-
try {
|
|
48
|
-
const recovered = ethers_1.ethers.verifyTypedData(DOMAIN, EIP_3009_TYPES, {
|
|
49
|
-
from: payload.from,
|
|
50
|
-
to: payload.to,
|
|
51
|
-
value: payload.value,
|
|
52
|
-
validAfter: payload.validAfter,
|
|
53
|
-
validBefore: payload.validBefore,
|
|
54
|
-
nonce: payload.nonce,
|
|
55
|
-
}, payload.signature);
|
|
56
|
-
if (recovered.toLowerCase() !== payload.from.toLowerCase()) {
|
|
57
|
-
throw new errors_js_1.PaymentError("Signature does not match payer address");
|
|
58
|
-
}
|
|
21
|
+
res = await fetch(`${facilitatorUrl}/verify`, {
|
|
22
|
+
method: "POST",
|
|
23
|
+
headers: {
|
|
24
|
+
"Content-Type": "application/json",
|
|
25
|
+
"X-Facilitator-Secret": secret,
|
|
26
|
+
},
|
|
27
|
+
body: JSON.stringify({
|
|
28
|
+
payment: paymentHeader,
|
|
29
|
+
resource,
|
|
30
|
+
amount: rawAmount,
|
|
31
|
+
network: resolvedNetwork,
|
|
32
|
+
}),
|
|
33
|
+
});
|
|
59
34
|
}
|
|
60
35
|
catch (err) {
|
|
61
|
-
if (err instanceof errors_js_1.PaymentError)
|
|
62
|
-
throw err;
|
|
63
36
|
const message = err instanceof Error ? err.message : String(err);
|
|
64
|
-
throw new errors_js_1.PaymentError(`
|
|
37
|
+
throw new errors_js_1.PaymentError(`Facilitator unreachable at ${facilitatorUrl}: ${message}`);
|
|
38
|
+
}
|
|
39
|
+
if (res.status === 401) {
|
|
40
|
+
throw new errors_js_1.PaymentError("Facilitator rejected the request — check FACILITATOR_SECRET");
|
|
41
|
+
}
|
|
42
|
+
if (!res.ok) {
|
|
43
|
+
throw new errors_js_1.PaymentError(`Facilitator returned HTTP ${res.status}`);
|
|
65
44
|
}
|
|
66
|
-
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
const provider = new ethers_1.ethers.JsonRpcProvider(ARBITRUM_RPC);
|
|
70
|
-
const usdc = new ethers_1.ethers.Contract(USDC_ADDRESS, ["function balanceOf(address) view returns (uint256)"], provider);
|
|
71
|
-
const balance = await usdc.balanceOf(payload.from);
|
|
72
|
-
if (balance < rawAmount) {
|
|
73
|
-
throw new errors_js_1.PaymentError("Payer has insufficient USDC balance on-chain");
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
catch (err) {
|
|
77
|
-
if (err instanceof errors_js_1.PaymentError)
|
|
78
|
-
throw err;
|
|
79
|
-
// RPC errors are non-fatal — trust the signature
|
|
80
|
-
}
|
|
45
|
+
const result = await res.json();
|
|
46
|
+
if (!result.isValid) {
|
|
47
|
+
throw new errors_js_1.PaymentError(result.invalidReason || "Payment invalid");
|
|
81
48
|
}
|
|
82
49
|
}
|
|
83
50
|
//# sourceMappingURL=verify.js.map
|
package/dist/verify.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verify.js","sourceRoot":"","sources":["../src/verify.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"verify.js","sourceRoot":"","sources":["../src/verify.ts"],"names":[],"mappings":";;AAIA,sCAyDC;AA7DD,2CAA2C;AAE3C,MAAM,oBAAoB,GAAG,qCAAqC,CAAC;AAE5D,KAAK,UAAU,aAAa,CACjC,aAAqB,EACrB,QAAqB,EACrB,UAAqB,EACrB,OAAqB;IAErB,MAAM,cAAc,GAAG,OAAO,CAAC,GAAG,CAAC,oBAAoB,IAAI,oBAAoB,CAAC;IAEhF,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC;IAC9C,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,wBAAY,CACpB,iCAAiC;YACjC,8DAA8D,CAC/D,CAAC;IACJ,CAAC;IAED,MAAM,eAAe,GAAG,OAAO;WAC1B,OAAO,CAAC,GAAG,CAAC,YAAY;WACxB,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;YACrC,CAAC,CAAC,cAAc;YAChB,CAAC,CAAC,eAAe,CAAC,CAAC;IAEzB,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,OAAS,CAAC,CAAC,CAAC;IAEzE,IAAI,GAAa,CAAC;IAClB,IAAI,CAAC;QACH,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,cAAc,SAAS,EAAE;YAC5C,MAAM,EAAG,MAAM;YACf,OAAO,EAAE;gBACP,cAAc,EAAU,kBAAkB;gBAC1C,sBAAsB,EAAE,MAAM;aAC/B;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;gBACnB,OAAO,EAAG,aAAa;gBACvB,QAAQ;gBACR,MAAM,EAAI,SAAS;gBACnB,OAAO,EAAG,eAAe;aAC1B,CAAC;SACH,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACtB,MAAM,OAAO,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACjE,MAAM,IAAI,wBAAY,CAAC,8BAA8B,cAAc,KAAK,OAAO,EAAE,CAAC,CAAC;IACrF,CAAC;IAED,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;QACvB,MAAM,IAAI,wBAAY,CAAC,6DAA6D,CAAC,CAAC;IACxF,CAAC;IAED,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;QACZ,MAAM,IAAI,wBAAY,CAAC,6BAA6B,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;IACpE,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,IAAI,EAAkD,CAAC;IAEhF,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,MAAM,IAAI,wBAAY,CAAC,MAAM,CAAC,aAAa,IAAI,iBAAiB,CAAC,CAAC;IACpE,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usemilkyway/agent-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Build and monetize AI agents on MilkyWay",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
"clean": "rm -rf dist"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@coinbase/x402": "^0.4.0",
|
|
20
19
|
"express": "^4.18.0",
|
|
21
20
|
"ethers": "^6.0.0",
|
|
22
21
|
"zod": "^3.22.0"
|