@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/demo/src/deposit.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import 'dotenv/config'
|
|
2
|
-
import { privateKeyToAccount } from 'viem/accounts'
|
|
3
|
-
import { Client, ConfigBuilder } from '@4mica/sdk'
|
|
4
|
-
|
|
5
|
-
async function main() {
|
|
6
|
-
const privateKey = process.env.PRIVATE_KEY
|
|
7
|
-
if (!privateKey || !privateKey.startsWith('0x')) {
|
|
8
|
-
console.error('Error: PRIVATE_KEY environment variable must be set and start with 0x')
|
|
9
|
-
console.error('Example: PRIVATE_KEY=0x1234... yarn deposit')
|
|
10
|
-
process.exit(1)
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const account = privateKeyToAccount(privateKey as `0x${string}`)
|
|
14
|
-
const cfg = new ConfigBuilder()
|
|
15
|
-
.rpcUrl('https://ethereum.sepolia.api.4mica.xyz')
|
|
16
|
-
.signer(account)
|
|
17
|
-
.build()
|
|
18
|
-
const client = await Client.new(cfg)
|
|
19
|
-
|
|
20
|
-
// const amount = 2_000_000 // 2 USDC in base units
|
|
21
|
-
|
|
22
|
-
// const allowance = await client.user.approveErc20(USDC_ADDRESS, amount)
|
|
23
|
-
// console.log('Approval receipt:', allowance)
|
|
24
|
-
|
|
25
|
-
// const depositReceipt = await client.user.deposit(amount, USDC_ADDRESS)
|
|
26
|
-
// console.log('Deposit receipt:', depositReceipt)
|
|
27
|
-
|
|
28
|
-
const userInfo = await client.user.getUser()
|
|
29
|
-
userInfo.forEach((user) => {
|
|
30
|
-
console.log('User asset:', user.asset, ', collateral:', Number(user.collateral))
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
main().catch((error) => {
|
|
35
|
-
console.error('Unhandled error:', error)
|
|
36
|
-
process.exit(1)
|
|
37
|
-
})
|
package/demo/src/server.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import 'dotenv/config'
|
|
2
|
-
import express from 'express'
|
|
3
|
-
import { paymentMiddlewareFromConfig } from '@4mica/x402/server/express'
|
|
4
|
-
|
|
5
|
-
const app = express()
|
|
6
|
-
app.use(express.json())
|
|
7
|
-
|
|
8
|
-
const PORT = process.env.PORT || 3000
|
|
9
|
-
const PAY_TO_ADDRESS = process.env.PAY_TO_ADDRESS
|
|
10
|
-
// This endpoint is hosted by this resource server. Clients call it after receiving
|
|
11
|
-
// a 402 response, and the middleware below forwards the tab-open request to the
|
|
12
|
-
// 4Mica facilitator on the server's behalf.
|
|
13
|
-
const ADVERTISED_ENDPOINT =
|
|
14
|
-
process.env.ADVERTISED_ENDPOINT || `http://localhost:${PORT}/payment/tab`
|
|
15
|
-
|
|
16
|
-
if (!PAY_TO_ADDRESS) {
|
|
17
|
-
console.error('Error: PAY_TO_ADDRESS environment variable is required')
|
|
18
|
-
process.exit(1)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
app.use(
|
|
22
|
-
paymentMiddlewareFromConfig(
|
|
23
|
-
{
|
|
24
|
-
'GET /api/premium-data': {
|
|
25
|
-
accepts: {
|
|
26
|
-
scheme: '4mica-credit',
|
|
27
|
-
price: '$0.01',
|
|
28
|
-
network: 'eip155:11155111', // Ethereum Sepolia
|
|
29
|
-
payTo: PAY_TO_ADDRESS,
|
|
30
|
-
},
|
|
31
|
-
description: 'Access to premium data endpoint',
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
// The middleware injects this URL into paymentRequirements.extra.tabEndpoint
|
|
36
|
-
// and also serves the POST route on this Express app.
|
|
37
|
-
advertisedEndpoint: ADVERTISED_ENDPOINT,
|
|
38
|
-
ttlSeconds: 3600, // 1 hour
|
|
39
|
-
}
|
|
40
|
-
)
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
app.get('/api/premium-data', (req, res) => {
|
|
44
|
-
res.json({
|
|
45
|
-
message: "Success! You've accessed the premium data.",
|
|
46
|
-
data: {
|
|
47
|
-
timestamp: new Date().toISOString(),
|
|
48
|
-
secret: 'This is protected content behind a paywall',
|
|
49
|
-
value: Math.random() * 1000,
|
|
50
|
-
},
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
app.get('/', (req, res) => {
|
|
55
|
-
res.json({
|
|
56
|
-
message: 'x402 Demo Server',
|
|
57
|
-
endpoints: {
|
|
58
|
-
free: ['/', '/health'],
|
|
59
|
-
protected: [
|
|
60
|
-
{
|
|
61
|
-
path: '/api/premium-data',
|
|
62
|
-
price: '$0.01',
|
|
63
|
-
description: 'Premium data endpoint (requires payment)',
|
|
64
|
-
},
|
|
65
|
-
],
|
|
66
|
-
},
|
|
67
|
-
})
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
app.get('/health', (req, res) => {
|
|
71
|
-
res.json({ status: 'ok' })
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
app.listen(PORT, () => {
|
|
75
|
-
console.log(`x402 Demo Server running on http://localhost:${PORT}`)
|
|
76
|
-
console.log(`Protected endpoint: http://localhost:${PORT}/api/premium-data`)
|
|
77
|
-
console.log(`Payment required: $0.01 (4mica credit on Sepolia)`)
|
|
78
|
-
console.log(`Tab endpoint hosted by this server: POST ${ADVERTISED_ENDPOINT}`)
|
|
79
|
-
console.log('Who calls it: payer clients after a 402 Payment Required response')
|
|
80
|
-
console.log('What it does: opens or reuses a tab via the 4Mica facilitator and returns tab JSON')
|
|
81
|
-
})
|