@4mica/sdk 0.5.0 → 0.5.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/README.md +251 -395
- package/package.json +36 -3
- package/.eslintrc.cjs +0 -29
- package/.github/workflows/ci.yml +0 -31
- package/.prettierignore +0 -3
- package/.prettierrc +0 -6
- package/eslint.config.mjs +0 -22
- package/src/abi/core4mica.json +0 -1605
- package/src/abi/erc20.json +0 -187
- package/src/auth.ts +0 -325
- package/src/bls.ts +0 -76
- package/src/client.ts +0 -347
- package/src/config.ts +0 -149
- package/src/contract.ts +0 -194
- package/src/errors.ts +0 -40
- package/src/guarantee.ts +0 -96
- package/src/index.ts +0 -12
- package/src/models.ts +0 -309
- package/src/rpc.ts +0 -225
- package/src/signing.ts +0 -330
- package/src/utils.ts +0 -75
- package/src/x402/index.ts +0 -192
- package/src/x402/models.ts +0 -94
- package/tests/auth.integration.test.ts +0 -97
- package/tests/auth.test.ts +0 -292
- package/tests/config.test.ts +0 -56
- package/tests/guarantee.test.ts +0 -26
- package/tests/rpc.test.ts +0 -76
- package/tests/signing.test.ts +0 -52
- package/tests/utils.test.ts +0 -35
- package/tests/x402.test.ts +0 -152
- package/tsconfig.build.json +0 -5
- package/tsconfig.json +0 -15
- package/vitest.config.ts +0 -12
package/tests/x402.test.ts
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { PaymentGuaranteeRequestClaims, PaymentSignature, SigningScheme } from '../src/models';
|
|
3
|
-
import {
|
|
4
|
-
PaymentRequirementsV1,
|
|
5
|
-
PaymentRequirementsV2,
|
|
6
|
-
X402PaymentRequired,
|
|
7
|
-
TabResponse,
|
|
8
|
-
X402Flow,
|
|
9
|
-
} from '../src/x402';
|
|
10
|
-
import type { FetchFn } from '../src/rpc';
|
|
11
|
-
import { X402Error } from '../src/errors';
|
|
12
|
-
|
|
13
|
-
const SCHEME = '4mica-credit';
|
|
14
|
-
|
|
15
|
-
class StubSigner {
|
|
16
|
-
async signPayment(_claims: PaymentGuaranteeRequestClaims, _scheme: SigningScheme) {
|
|
17
|
-
void _claims;
|
|
18
|
-
void _scheme;
|
|
19
|
-
return { signature: 'deadbeef', scheme: SigningScheme.EIP712 } as PaymentSignature;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
class StubX402Flow extends X402Flow {
|
|
24
|
-
protected async requestTab(): Promise<TabResponse> {
|
|
25
|
-
return {
|
|
26
|
-
tabId: '2',
|
|
27
|
-
userAddress: '0x0000000000000000000000000000000000000001',
|
|
28
|
-
nextReqId: '7',
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
describe('X402Flow', () => {
|
|
34
|
-
it('rejects invalid scheme', async () => {
|
|
35
|
-
const flow = new StubX402Flow(new StubSigner());
|
|
36
|
-
const requirements: PaymentRequirementsV1 = {
|
|
37
|
-
scheme: 'http+pay',
|
|
38
|
-
network: 'testnet',
|
|
39
|
-
maxAmountRequired: '1',
|
|
40
|
-
payTo: '0x0000000000000000000000000000000000000003',
|
|
41
|
-
asset: '0x0000000000000000000000000000000000000000',
|
|
42
|
-
extra: { tabEndpoint: 'https://example.com' },
|
|
43
|
-
};
|
|
44
|
-
await expect(
|
|
45
|
-
flow.signPayment(requirements, '0x0000000000000000000000000000000000000001')
|
|
46
|
-
).rejects.toThrow(X402Error);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('builds header and payload', async () => {
|
|
50
|
-
const flow = new StubX402Flow(new StubSigner());
|
|
51
|
-
const requirements: PaymentRequirementsV1 = {
|
|
52
|
-
scheme: SCHEME,
|
|
53
|
-
network: 'testnet',
|
|
54
|
-
maxAmountRequired: '5',
|
|
55
|
-
payTo: '0x0000000000000000000000000000000000000003',
|
|
56
|
-
asset: '0x0000000000000000000000000000000000000000',
|
|
57
|
-
extra: { tabEndpoint: 'https://example.com' },
|
|
58
|
-
};
|
|
59
|
-
const userAddress = '0x0000000000000000000000000000000000000001';
|
|
60
|
-
const signed = await flow.signPayment(requirements, userAddress);
|
|
61
|
-
const decoded = Buffer.from(signed.header, 'base64').toString('utf8');
|
|
62
|
-
const envelope = JSON.parse(decoded);
|
|
63
|
-
|
|
64
|
-
expect(envelope.x402Version).toBe(1);
|
|
65
|
-
expect(envelope.scheme).toBe(SCHEME);
|
|
66
|
-
expect(envelope.payload.claims.tab_id).toBe('0x2');
|
|
67
|
-
expect(envelope.payload.claims.req_id).toBe('0x7');
|
|
68
|
-
|
|
69
|
-
expect(signed.payload.claims.tab_id).toBe('0x2');
|
|
70
|
-
expect(signed.payload.claims.req_id).toBe('0x7');
|
|
71
|
-
expect(signed.payload.claims.amount).toBe('0x5');
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('builds header and payload for V2', async () => {
|
|
75
|
-
const flow = new StubX402Flow(new StubSigner());
|
|
76
|
-
const accepted: PaymentRequirementsV2 = {
|
|
77
|
-
scheme: SCHEME,
|
|
78
|
-
network: 'testnet',
|
|
79
|
-
amount: '10',
|
|
80
|
-
payTo: '0x0000000000000000000000000000000000000003',
|
|
81
|
-
asset: '0x0000000000000000000000000000000000000000',
|
|
82
|
-
extra: { tabEndpoint: 'https://example.com' },
|
|
83
|
-
};
|
|
84
|
-
const paymentRequired: X402PaymentRequired = {
|
|
85
|
-
x402Version: 2,
|
|
86
|
-
resource: {
|
|
87
|
-
url: 'https://api.example.com/data',
|
|
88
|
-
description: 'Premium data access',
|
|
89
|
-
mimeType: 'application/json',
|
|
90
|
-
},
|
|
91
|
-
accepts: [accepted],
|
|
92
|
-
};
|
|
93
|
-
const userAddress = '0x0000000000000000000000000000000000000001';
|
|
94
|
-
const signed = await flow.signPaymentV2(paymentRequired, accepted, userAddress);
|
|
95
|
-
const decoded = Buffer.from(signed.header, 'base64').toString('utf8');
|
|
96
|
-
const envelope = JSON.parse(decoded);
|
|
97
|
-
|
|
98
|
-
expect(envelope.x402Version).toBe(2);
|
|
99
|
-
expect(envelope.accepted.scheme).toBe(SCHEME);
|
|
100
|
-
expect(envelope.accepted.amount).toBe('10');
|
|
101
|
-
expect(envelope.resource.url).toBe('https://api.example.com/data');
|
|
102
|
-
expect(envelope.payload.claims.tab_id).toBe('0x2');
|
|
103
|
-
expect(envelope.payload.claims.req_id).toBe('0x7');
|
|
104
|
-
|
|
105
|
-
expect(signed.payload.claims.tab_id).toBe('0x2');
|
|
106
|
-
expect(signed.payload.claims.req_id).toBe('0x7');
|
|
107
|
-
expect(signed.payload.claims.amount).toBe('0xa');
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it('settles payment through facilitator', async () => {
|
|
111
|
-
const userAddress = '0x0000000000000000000000000000000000000009';
|
|
112
|
-
const tabEndpoint = 'http://facilitator.test/tab';
|
|
113
|
-
const facilitatorUrl = 'http://facilitator.test';
|
|
114
|
-
const requirements: PaymentRequirementsV1 = {
|
|
115
|
-
scheme: SCHEME,
|
|
116
|
-
network: 'testnet',
|
|
117
|
-
maxAmountRequired: '5',
|
|
118
|
-
payTo: '0x00000000000000000000000000000000000000ff',
|
|
119
|
-
asset: '0x0000000000000000000000000000000000000000',
|
|
120
|
-
extra: { tabEndpoint },
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
const fetch = async (url: string, init?: RequestInit) => {
|
|
124
|
-
const u = new URL(url);
|
|
125
|
-
if (u.pathname === '/tab') {
|
|
126
|
-
const body = JSON.parse(init?.body as string);
|
|
127
|
-
expect(body.userAddress).toBe(userAddress);
|
|
128
|
-
return new Response(JSON.stringify({ tabId: '0x1234', userAddress, nextReqId: '4' }), {
|
|
129
|
-
status: 200,
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
if (u.pathname === '/settle') {
|
|
133
|
-
const payload = JSON.parse(init?.body as string);
|
|
134
|
-
expect(payload.paymentRequirements.payTo).toBe(requirements.payTo);
|
|
135
|
-
return new Response(JSON.stringify({ settled: true, networkId: requirements.network }), {
|
|
136
|
-
status: 200,
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
return new Response('not found', { status: 404 });
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
const flow = new X402Flow(new StubSigner(), fetch as FetchFn);
|
|
143
|
-
const payment = await flow.signPayment(requirements, userAddress);
|
|
144
|
-
expect(payment.payload.claims.tab_id).toBe('0x1234');
|
|
145
|
-
expect(payment.payload.claims.req_id).toBe('0x4');
|
|
146
|
-
|
|
147
|
-
const settled = await flow.settlePayment(payment, requirements, facilitatorUrl);
|
|
148
|
-
expect((settled.settlement as Record<string, unknown>).settled).toBe(true);
|
|
149
|
-
expect((settled.settlement as Record<string, unknown>).networkId).toBe(requirements.network);
|
|
150
|
-
expect(settled.payment.payload.claims.recipient_address).toBe(requirements.payTo);
|
|
151
|
-
});
|
|
152
|
-
});
|
package/tsconfig.build.json
DELETED
package/tsconfig.json
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "CommonJS",
|
|
5
|
-
"lib": ["ES2020", "DOM"],
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"outDir": "dist",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"resolveJsonModule": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"moduleResolution": "Node"
|
|
13
|
-
},
|
|
14
|
-
"include": ["src/**/*", "tests/**/*", "vitest.config.ts"]
|
|
15
|
-
}
|
package/vitest.config.ts
DELETED