@4mica/x402 0.1.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/.eslintrc.cjs +29 -0
- package/.prettierignore +3 -0
- package/.prettierrc +6 -0
- package/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/README.md +389 -0
- package/demo/.env.example +8 -0
- package/demo/README.md +125 -0
- package/demo/package.json +26 -0
- package/demo/src/client.ts +54 -0
- package/demo/src/deposit.ts +39 -0
- package/demo/src/server.ts +74 -0
- package/demo/tsconfig.json +8 -0
- package/demo/yarn.lock +925 -0
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.js +1 -0
- package/dist/client/scheme.d.ts +11 -0
- package/dist/client/scheme.js +65 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/server/express/adapter.d.ts +71 -0
- package/dist/server/express/adapter.js +90 -0
- package/dist/server/express/index.d.ts +122 -0
- package/dist/server/express/index.js +340 -0
- package/dist/server/facilitator.d.ts +35 -0
- package/dist/server/facilitator.js +52 -0
- package/dist/server/index.d.ts +6 -0
- package/dist/server/index.js +4 -0
- package/dist/server/scheme.d.ts +93 -0
- package/dist/server/scheme.js +179 -0
- package/eslint.config.mjs +22 -0
- package/package.json +79 -0
- package/src/client/index.ts +1 -0
- package/src/client/scheme.ts +95 -0
- package/src/index.ts +7 -0
- package/src/server/express/adapter.ts +100 -0
- package/src/server/express/index.ts +466 -0
- package/src/server/facilitator.ts +90 -0
- package/src/server/index.ts +10 -0
- package/src/server/scheme.ts +223 -0
- package/tsconfig.build.json +5 -0
- package/tsconfig.json +17 -0
- package/vitest.config.ts +12 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import 'dotenv/config'
|
|
2
|
+
import { privateKeyToAccount } from 'viem/accounts'
|
|
3
|
+
import { Client, ConfigBuilder } from '@4mica/sdk'
|
|
4
|
+
|
|
5
|
+
const USDC_ADDRESS = '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238'
|
|
6
|
+
|
|
7
|
+
async function main() {
|
|
8
|
+
const privateKey = process.env.PRIVATE_KEY
|
|
9
|
+
if (!privateKey || !privateKey.startsWith('0x')) {
|
|
10
|
+
console.error('Error: PRIVATE_KEY environment variable must be set and start with 0x')
|
|
11
|
+
console.error('Example: PRIVATE_KEY=0x1234... yarn deposit')
|
|
12
|
+
process.exit(1)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const account = privateKeyToAccount(privateKey as `0x${string}`)
|
|
16
|
+
const cfg = new ConfigBuilder()
|
|
17
|
+
.rpcUrl('https://ethereum.sepolia.api.4mica.xyz')
|
|
18
|
+
.signer(account)
|
|
19
|
+
.build()
|
|
20
|
+
const client = await Client.new(cfg)
|
|
21
|
+
|
|
22
|
+
// const amount = 2_000_000 // 2 USDC in base units
|
|
23
|
+
|
|
24
|
+
// const allowance = await client.user.approveErc20(USDC_ADDRESS, amount)
|
|
25
|
+
// console.log('Approval receipt:', allowance)
|
|
26
|
+
|
|
27
|
+
// const depositReceipt = await client.user.deposit(amount, USDC_ADDRESS)
|
|
28
|
+
// console.log('Deposit receipt:', depositReceipt)
|
|
29
|
+
|
|
30
|
+
const userInfo = await client.user.getUser()
|
|
31
|
+
userInfo.forEach((user) => {
|
|
32
|
+
console.log('User asset:', user.asset, ', collateral:', Number(user.collateral))
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
main().catch((error) => {
|
|
37
|
+
console.error('Unhandled error:', error)
|
|
38
|
+
process.exit(1)
|
|
39
|
+
})
|
|
@@ -0,0 +1,74 @@
|
|
|
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
|
+
const ADVERTISED_ENDPOINT =
|
|
11
|
+
process.env.ADVERTISED_ENDPOINT || `http://localhost:${PORT}/payment/tab`
|
|
12
|
+
|
|
13
|
+
if (!PAY_TO_ADDRESS) {
|
|
14
|
+
console.error('Error: PAY_TO_ADDRESS environment variable is required')
|
|
15
|
+
process.exit(1)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
app.use(
|
|
19
|
+
paymentMiddlewareFromConfig(
|
|
20
|
+
{
|
|
21
|
+
'GET /api/premium-data': {
|
|
22
|
+
accepts: {
|
|
23
|
+
scheme: '4mica-credit',
|
|
24
|
+
price: '$0.01',
|
|
25
|
+
network: 'eip155:11155111', // Ethereum Sepolia
|
|
26
|
+
payTo: PAY_TO_ADDRESS,
|
|
27
|
+
},
|
|
28
|
+
description: 'Access to premium data endpoint',
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
advertisedEndpoint: ADVERTISED_ENDPOINT,
|
|
33
|
+
ttlSeconds: 3600, // 1 hour
|
|
34
|
+
}
|
|
35
|
+
)
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
app.get('/api/premium-data', (req, res) => {
|
|
39
|
+
res.json({
|
|
40
|
+
message: "Success! You've accessed the premium data.",
|
|
41
|
+
data: {
|
|
42
|
+
timestamp: new Date().toISOString(),
|
|
43
|
+
secret: 'This is protected content behind a paywall',
|
|
44
|
+
value: Math.random() * 1000,
|
|
45
|
+
},
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
app.get('/', (req, res) => {
|
|
50
|
+
res.json({
|
|
51
|
+
message: 'x402 Demo Server',
|
|
52
|
+
endpoints: {
|
|
53
|
+
free: ['/', '/health'],
|
|
54
|
+
protected: [
|
|
55
|
+
{
|
|
56
|
+
path: '/api/premium-data',
|
|
57
|
+
price: '$0.01',
|
|
58
|
+
description: 'Premium data endpoint (requires payment)',
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
app.get('/health', (req, res) => {
|
|
66
|
+
res.json({ status: 'ok' })
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
app.listen(PORT, () => {
|
|
70
|
+
console.log(`x402 Demo Server running on http://localhost:${PORT}`)
|
|
71
|
+
console.log(`Protected endpoint: http://localhost:${PORT}/api/premium-data`)
|
|
72
|
+
console.log(`Payment required: $0.01 (4mica credit on Sepolia)`)
|
|
73
|
+
console.log(`Payment tab endpoint: ${ADVERTISED_ENDPOINT}`)
|
|
74
|
+
})
|