@4mica/x402 1.2.4 → 2.0.0-alpha.2
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/CHANGELOG.md +63 -1
- package/README.md +67 -82
- package/dist/client/scheme.d.ts +40 -4
- package/dist/client/scheme.js +96 -38
- package/dist/domain.d.ts +12 -0
- package/dist/domain.js +30 -0
- package/dist/index.d.ts +2 -2
- package/dist/server/express/adapter.d.ts +2 -2
- package/dist/server/express/index.d.ts +13 -46
- package/dist/server/express/index.js +32 -71
- package/dist/server/facilitator.d.ts +2 -24
- package/dist/server/facilitator.js +0 -36
- package/dist/server/index.d.ts +2 -4
- package/dist/server/index.js +1 -2
- package/dist/server/scheme.d.ts +54 -10
- package/dist/server/scheme.js +118 -56
- package/dist/types.d.ts +36 -12
- package/package.json +33 -32
- package/.eslintrc.cjs +0 -29
- package/.prettierignore +0 -3
- package/.prettierrc +0 -6
- package/demo/.env.example +0 -8
- package/demo/README.md +0 -125
- package/demo/package.json +0 -26
- package/demo/src/client.ts +0 -54
- package/demo/src/deposit.ts +0 -37
- package/demo/src/server.ts +0 -81
- package/demo/tsconfig.json +0 -8
- package/demo/yarn.lock +0 -925
- package/eslint.config.mjs +0 -22
- package/src/client/index.ts +0 -1
- package/src/client/scheme.ts +0 -111
- package/src/index.ts +0 -9
- package/src/server/express/adapter.ts +0 -100
- package/src/server/express/index.ts +0 -499
- package/src/server/facilitator.ts +0 -206
- package/src/server/index.ts +0 -10
- package/src/server/scheme.ts +0 -229
- package/src/types.ts +0 -24
- package/tests/client-scheme.test.ts +0 -99
- package/tests/facilitator.test.ts +0 -174
- package/tsconfig.build.json +0 -5
- package/tsconfig.json +0 -17
- package/vitest.config.ts +0 -12
package/eslint.config.mjs
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import js from "@eslint/js";
|
|
2
|
-
import globals from "globals";
|
|
3
|
-
import tseslint from "typescript-eslint";
|
|
4
|
-
import pluginReact from "eslint-plugin-react";
|
|
5
|
-
import { defineConfig } from "eslint/config";
|
|
6
|
-
|
|
7
|
-
export default defineConfig([
|
|
8
|
-
{
|
|
9
|
-
ignores: ["dist/**", "coverage/**", "node_modules/**", "*.cjs"],
|
|
10
|
-
},
|
|
11
|
-
{
|
|
12
|
-
files: ["**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
|
|
13
|
-
plugins: { js },
|
|
14
|
-
extends: ["js/recommended"],
|
|
15
|
-
languageOptions: { globals: globals.browser },
|
|
16
|
-
},
|
|
17
|
-
tseslint.configs.recommended,
|
|
18
|
-
pluginReact.configs.flat.recommended,
|
|
19
|
-
{
|
|
20
|
-
settings: { react: { version: "detect" } },
|
|
21
|
-
},
|
|
22
|
-
]);
|
package/src/client/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './scheme.js'
|
package/src/client/scheme.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { SchemeNetworkClient, PaymentRequirements, PaymentPayload, Network } from '@x402/core/types'
|
|
2
|
-
import {
|
|
3
|
-
Client,
|
|
4
|
-
ConfigBuilder,
|
|
5
|
-
PaymentRequirementsV1,
|
|
6
|
-
X402Flow,
|
|
7
|
-
X402PaymentRequired,
|
|
8
|
-
X402ResourceInfo,
|
|
9
|
-
} from '@4mica/sdk'
|
|
10
|
-
import { Account } from 'viem/accounts'
|
|
11
|
-
import { SUPPORTED_NETWORKS } from '../server/scheme.js'
|
|
12
|
-
|
|
13
|
-
const NETWORK_RPC_URLS: Record<Network, string> = {
|
|
14
|
-
'eip155:11155111': 'https://ethereum.sepolia.api.4mica.xyz',
|
|
15
|
-
'eip155:84532': 'https://base.sepolia.api.4mica.xyz',
|
|
16
|
-
'eip155:8453': 'https://base.api.4mica.xyz',
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class FourMicaEvmScheme implements SchemeNetworkClient {
|
|
20
|
-
readonly scheme = '4mica-credit'
|
|
21
|
-
|
|
22
|
-
private constructor(
|
|
23
|
-
private readonly signer: Account,
|
|
24
|
-
// rpcUrl -> x402Flow
|
|
25
|
-
private readonly x402Flows: Map<string, X402Flow>
|
|
26
|
-
) {}
|
|
27
|
-
|
|
28
|
-
private static async createX402Flow(signer: Account, rpcUrl: string): Promise<X402Flow> {
|
|
29
|
-
const cfg = new ConfigBuilder().rpcUrl(rpcUrl).signer(signer).build()
|
|
30
|
-
const client = await Client.new(cfg)
|
|
31
|
-
|
|
32
|
-
return X402Flow.fromClient(client)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
static async create(signer: Account): Promise<FourMicaEvmScheme> {
|
|
36
|
-
const x402Flows = new Map<string, X402Flow>()
|
|
37
|
-
|
|
38
|
-
for (const network of SUPPORTED_NETWORKS) {
|
|
39
|
-
const rpcUrl = NETWORK_RPC_URLS[network]
|
|
40
|
-
if (!rpcUrl) continue
|
|
41
|
-
|
|
42
|
-
x402Flows.set(rpcUrl, await FourMicaEvmScheme.createX402Flow(signer, rpcUrl))
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return new FourMicaEvmScheme(signer, x402Flows)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async createPaymentPayload(
|
|
49
|
-
x402Version: number,
|
|
50
|
-
paymentRequirements: PaymentRequirements
|
|
51
|
-
): Promise<Pick<PaymentPayload, 'x402Version' | 'payload'>> {
|
|
52
|
-
const network = paymentRequirements.network as Network
|
|
53
|
-
if (!network) {
|
|
54
|
-
throw new Error('Network is required in PaymentRequirements')
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const rpcUrl = (paymentRequirements.extra?.rpcUrl as string) ?? NETWORK_RPC_URLS[network]
|
|
58
|
-
if (!rpcUrl) {
|
|
59
|
-
throw new Error(`No RPC URL configured for network ${network}`)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
let x402Flow = this.x402Flows.get(rpcUrl)
|
|
63
|
-
if (!x402Flow) {
|
|
64
|
-
x402Flow = await FourMicaEvmScheme.createX402Flow(this.signer, rpcUrl)
|
|
65
|
-
this.x402Flows.set(rpcUrl, x402Flow)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (x402Version === 1) {
|
|
69
|
-
const signed = await x402Flow.signPayment(
|
|
70
|
-
paymentRequirements as unknown as PaymentRequirementsV1,
|
|
71
|
-
this.signer.address
|
|
72
|
-
)
|
|
73
|
-
return {
|
|
74
|
-
x402Version: 1,
|
|
75
|
-
payload: signed.payload as unknown as Record<string, unknown>,
|
|
76
|
-
}
|
|
77
|
-
} else if (x402Version === 2) {
|
|
78
|
-
const resourcePayload =
|
|
79
|
-
paymentRequirements.extra &&
|
|
80
|
-
typeof paymentRequirements.extra === 'object' &&
|
|
81
|
-
'resource' in paymentRequirements.extra &&
|
|
82
|
-
typeof paymentRequirements.extra.resource === 'object' &&
|
|
83
|
-
paymentRequirements.extra.resource !== null
|
|
84
|
-
? (paymentRequirements.extra.resource as Record<string, unknown>)
|
|
85
|
-
: {}
|
|
86
|
-
|
|
87
|
-
const resource: X402ResourceInfo = {
|
|
88
|
-
url: String(resourcePayload.url ?? ''),
|
|
89
|
-
description: String(resourcePayload.description ?? ''),
|
|
90
|
-
mimeType: String(resourcePayload.mimeType ?? ''),
|
|
91
|
-
}
|
|
92
|
-
const paymentRequired: X402PaymentRequired = {
|
|
93
|
-
x402Version: 2,
|
|
94
|
-
resource,
|
|
95
|
-
accepts: [paymentRequirements],
|
|
96
|
-
}
|
|
97
|
-
const signed = await x402Flow.signPaymentV2(
|
|
98
|
-
paymentRequired,
|
|
99
|
-
paymentRequirements,
|
|
100
|
-
this.signer.address
|
|
101
|
-
)
|
|
102
|
-
|
|
103
|
-
return {
|
|
104
|
-
x402Version: 2,
|
|
105
|
-
payload: signed.payload as unknown as Record<string, unknown>,
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
throw new Error(`Unsupported x402Version: ${x402Version}`)
|
|
110
|
-
}
|
|
111
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { HTTPAdapter } from '@x402/core/server'
|
|
2
|
-
import { Request } from 'express'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Express adapter implementation
|
|
6
|
-
*/
|
|
7
|
-
export class ExpressAdapter implements HTTPAdapter {
|
|
8
|
-
/**
|
|
9
|
-
* Creates a new ExpressAdapter instance.
|
|
10
|
-
*
|
|
11
|
-
* @param req - The Express request object
|
|
12
|
-
*/
|
|
13
|
-
constructor(private req: Request) {}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Gets a header value from the request.
|
|
17
|
-
*
|
|
18
|
-
* @param name - The header name
|
|
19
|
-
* @returns The header value or undefined
|
|
20
|
-
*/
|
|
21
|
-
getHeader(name: string): string | undefined {
|
|
22
|
-
const value = this.req.header(name)
|
|
23
|
-
return Array.isArray(value) ? value[0] : value
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Gets the HTTP method of the request.
|
|
28
|
-
*
|
|
29
|
-
* @returns The HTTP method
|
|
30
|
-
*/
|
|
31
|
-
getMethod(): string {
|
|
32
|
-
return this.req.method
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Gets the path of the request.
|
|
37
|
-
*
|
|
38
|
-
* @returns The request path
|
|
39
|
-
*/
|
|
40
|
-
getPath(): string {
|
|
41
|
-
return this.req.path
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Gets the full URL of the request.
|
|
46
|
-
*
|
|
47
|
-
* @returns The full request URL
|
|
48
|
-
*/
|
|
49
|
-
getUrl(): string {
|
|
50
|
-
return `${this.req.protocol}://${this.req.headers.host}${this.req.originalUrl}`
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Gets the Accept header from the request.
|
|
55
|
-
*
|
|
56
|
-
* @returns The Accept header value or empty string
|
|
57
|
-
*/
|
|
58
|
-
getAcceptHeader(): string {
|
|
59
|
-
return this.req.header('Accept') || ''
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Gets the User-Agent header from the request.
|
|
64
|
-
*
|
|
65
|
-
* @returns The User-Agent header value or empty string
|
|
66
|
-
*/
|
|
67
|
-
getUserAgent(): string {
|
|
68
|
-
return this.req.header('User-Agent') || ''
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Gets all query parameters from the request URL.
|
|
73
|
-
*
|
|
74
|
-
* @returns Record of query parameter key-value pairs
|
|
75
|
-
*/
|
|
76
|
-
getQueryParams(): Record<string, string | string[]> {
|
|
77
|
-
return this.req.query as Record<string, string | string[]>
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Gets a specific query parameter by name.
|
|
82
|
-
*
|
|
83
|
-
* @param name - The query parameter name
|
|
84
|
-
* @returns The query parameter value(s) or undefined
|
|
85
|
-
*/
|
|
86
|
-
getQueryParam(name: string): string | string[] | undefined {
|
|
87
|
-
const value = this.req.query[name]
|
|
88
|
-
return value as string | string[] | undefined
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Gets the parsed request body.
|
|
93
|
-
* Requires express.json() or express.urlencoded() middleware.
|
|
94
|
-
*
|
|
95
|
-
* @returns The parsed request body
|
|
96
|
-
*/
|
|
97
|
-
getBody(): unknown {
|
|
98
|
-
return this.req.body
|
|
99
|
-
}
|
|
100
|
-
}
|