@4mica/sdk 1.2.16 → 1.3.1
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 +59 -25
- package/dist/index.cjs +5513 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2730 -0
- package/dist/{src/abi/core4mica.d.ts → index.d.ts} +1032 -1
- package/dist/index.js +5439 -0
- package/dist/index.js.map +1 -0
- package/dist/models-B15ouNYG.d.cts +386 -0
- package/dist/models-B15ouNYG.d.ts +386 -0
- package/dist/server.cjs +172 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +119 -0
- package/dist/server.d.ts +119 -0
- package/dist/server.js +165 -0
- package/dist/server.js.map +1 -0
- package/package.json +52 -43
- package/dist/package.json +0 -80
- package/dist/src/abi/core4mica.js +0 -2200
- package/dist/src/abi/erc20.d.ts +0 -151
- package/dist/src/abi/erc20.js +0 -201
- package/dist/src/auth.d.ts +0 -62
- package/dist/src/auth.js +0 -218
- package/dist/src/bls.d.ts +0 -6
- package/dist/src/bls.js +0 -247
- package/dist/src/chain.d.ts +0 -2
- package/dist/src/chain.js +0 -26
- package/dist/src/client/index.d.ts +0 -71
- package/dist/src/client/index.js +0 -120
- package/dist/src/client/recipient.d.ts +0 -129
- package/dist/src/client/recipient.js +0 -257
- package/dist/src/client/shared.d.ts +0 -11
- package/dist/src/client/shared.js +0 -17
- package/dist/src/client/user.d.ts +0 -109
- package/dist/src/client/user.js +0 -147
- package/dist/src/config.d.ts +0 -103
- package/dist/src/config.js +0 -197
- package/dist/src/constants.d.ts +0 -4
- package/dist/src/constants.js +0 -7
- package/dist/src/contract.d.ts +0 -55
- package/dist/src/contract.js +0 -312
- package/dist/src/debug.d.ts +0 -2
- package/dist/src/debug.js +0 -5
- package/dist/src/errors.d.ts +0 -58
- package/dist/src/errors.js +0 -81
- package/dist/src/guarantee.d.ts +0 -25
- package/dist/src/guarantee.js +0 -272
- package/dist/src/http.d.ts +0 -11
- package/dist/src/http.js +0 -57
- package/dist/src/index.d.ts +0 -17
- package/dist/src/index.js +0 -33
- package/dist/src/models.d.ts +0 -259
- package/dist/src/models.js +0 -356
- package/dist/src/networks.d.ts +0 -40
- package/dist/src/networks.js +0 -57
- package/dist/src/payment.d.ts +0 -82
- package/dist/src/payment.js +0 -67
- package/dist/src/rpc.d.ts +0 -38
- package/dist/src/rpc.js +0 -161
- package/dist/src/serde.d.ts +0 -5
- package/dist/src/serde.js +0 -33
- package/dist/src/signing.d.ts +0 -170
- package/dist/src/signing.js +0 -311
- package/dist/src/utils.d.ts +0 -11
- package/dist/src/utils.js +0 -90
- package/dist/src/validation.d.ts +0 -32
- package/dist/src/validation.js +0 -84
- package/dist/src/wallet/cdp.d.ts +0 -13
- package/dist/src/wallet/cdp.js +0 -78
- package/dist/src/wallet/index.d.ts +0 -2
- package/dist/src/wallet/index.js +0 -5
- package/dist/src/x402/index.d.ts +0 -90
- package/dist/src/x402/index.js +0 -243
- package/dist/src/x402/models.d.ts +0 -75
- package/dist/src/x402/models.js +0 -2
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RecipientClient = void 0;
|
|
4
|
-
const bls_1 = require("../bls");
|
|
5
|
-
const payment_1 = require("../payment");
|
|
6
|
-
const guarantee_1 = require("../guarantee");
|
|
7
|
-
const errors_1 = require("../errors");
|
|
8
|
-
const models_1 = require("../models");
|
|
9
|
-
const debug_1 = require("../debug");
|
|
10
|
-
const utils_1 = require("../utils");
|
|
11
|
-
const shared_1 = require("./shared");
|
|
12
|
-
/** Recipient-side operations: tab management, guarantee issuance, remuneration. */
|
|
13
|
-
class RecipientClient {
|
|
14
|
-
client;
|
|
15
|
-
constructor(client) {
|
|
16
|
-
this.client = client;
|
|
17
|
-
}
|
|
18
|
-
get recipientAddress() {
|
|
19
|
-
return (0, utils_1.normalizeAddress)(this.client.signer.signer.address);
|
|
20
|
-
}
|
|
21
|
-
get guaranteeDomain() {
|
|
22
|
-
return this.client.guaranteeDomain;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Create a payment tab via the core RPC.
|
|
26
|
-
*
|
|
27
|
-
* @param userAddress - Address of the payer.
|
|
28
|
-
* @param recipientAddress - Address of the recipient.
|
|
29
|
-
* @param erc20Token - ERC20 token address for the tab, or `null`/`undefined` for ETH.
|
|
30
|
-
* @param ttl - Optional time-to-live in seconds.
|
|
31
|
-
* @param guaranteeVersion - Guarantee version for the version-scoped tab identity.
|
|
32
|
-
* @returns `{ tabId, assetAddress, nextReqId }` — the tab ID, the asset address as stored
|
|
33
|
-
* by the core (use this for all subsequent claims), and the first request ID to use.
|
|
34
|
-
* @throws {@link RpcError} if the request fails.
|
|
35
|
-
*/
|
|
36
|
-
async createTab(userAddress, recipientAddress, erc20Token, ttl, guaranteeVersion = 1) {
|
|
37
|
-
const body = {
|
|
38
|
-
user_address: (0, utils_1.normalizeAddress)(userAddress),
|
|
39
|
-
recipient_address: (0, utils_1.normalizeAddress)(recipientAddress),
|
|
40
|
-
erc20_token: erc20Token ? (0, utils_1.normalizeAddress)(erc20Token) : null,
|
|
41
|
-
ttl: ttl ?? null,
|
|
42
|
-
guarantee_version: guaranteeVersion,
|
|
43
|
-
};
|
|
44
|
-
const result = await this.client.rpc.createPaymentTab(body);
|
|
45
|
-
const record = result;
|
|
46
|
-
const tabIdRaw = record.id ?? record.tabId ?? record.tab_id;
|
|
47
|
-
const tabId = (0, shared_1.isNumericLike)(tabIdRaw) ? tabIdRaw : 0;
|
|
48
|
-
const erc20Raw = record.erc20_token ?? record.erc20Token;
|
|
49
|
-
const assetAddress = typeof erc20Raw === 'string' && erc20Raw
|
|
50
|
-
? erc20Raw
|
|
51
|
-
: '0x0000000000000000000000000000000000000000';
|
|
52
|
-
const nextReqIdRaw = record.next_req_id ?? record.nextReqId ?? 0;
|
|
53
|
-
return {
|
|
54
|
-
tabId: (0, utils_1.parseU256)(tabId),
|
|
55
|
-
assetAddress,
|
|
56
|
-
nextReqId: (0, utils_1.parseU256)((0, shared_1.isNumericLike)(nextReqIdRaw) ? nextReqIdRaw : 0),
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Query the on-chain payment status of a tab.
|
|
61
|
-
*
|
|
62
|
-
* @param tabId - Tab identifier.
|
|
63
|
-
* @returns `{ paid, remunerated, asset }`.
|
|
64
|
-
*/
|
|
65
|
-
async getTabPaymentStatus(tabId) {
|
|
66
|
-
const status = await this.client.gateway.getPaymentStatus(tabId);
|
|
67
|
-
return (0, shared_1.tabStatusFromRpc)(status);
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Issue a BLS-signed payment guarantee certificate via the core RPC.
|
|
71
|
-
*
|
|
72
|
-
* The returned {@link BLSCert} can be stored and later passed to
|
|
73
|
-
* {@link remunerate} to claim the payment on-chain.
|
|
74
|
-
*
|
|
75
|
-
* @param claims - Signed payment claims (V1 or V2).
|
|
76
|
-
* @param signature - ECDSA signature hex string from the payer.
|
|
77
|
-
* @param scheme - Signing scheme used to produce the signature.
|
|
78
|
-
* @returns BLS certificate with ABI-encoded claims and BLS signature.
|
|
79
|
-
* @throws {@link RpcError} if the core service rejects the request.
|
|
80
|
-
*/
|
|
81
|
-
async issuePaymentGuarantee(claims, signature, scheme) {
|
|
82
|
-
const payload = (0, payment_1.buildPaymentPayload)(claims, signature, scheme);
|
|
83
|
-
const cert = await this.client.rpc.issueGuarantee(payload);
|
|
84
|
-
const record = cert;
|
|
85
|
-
const certClaims = typeof record.claims === 'string' ? record.claims : '';
|
|
86
|
-
const signatureOut = typeof record.signature === 'string' ? record.signature : '';
|
|
87
|
-
return { claims: certClaims, signature: signatureOut };
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Verify and decode a BLS guarantee certificate.
|
|
91
|
-
*
|
|
92
|
-
* Decodes the ABI-encoded claims and validates the domain separator against
|
|
93
|
-
* the on-chain configuration. For V2 certificates, the active V2 domain is
|
|
94
|
-
* fetched from the contract and verified to be enabled.
|
|
95
|
-
*
|
|
96
|
-
* @param cert - BLS certificate (hex-encoded claims + hex-encoded signature).
|
|
97
|
-
* @returns Decoded {@link PaymentGuaranteeClaims}, including validation policy for V2.
|
|
98
|
-
* @throws {@link VerificationError} on domain mismatch, invalid length, or disabled version.
|
|
99
|
-
*/
|
|
100
|
-
async verifyPaymentGuarantee(cert) {
|
|
101
|
-
const claims = (0, guarantee_1.decodeGuaranteeClaims)(cert.claims);
|
|
102
|
-
let expectedDomain;
|
|
103
|
-
if (claims.version === 2) {
|
|
104
|
-
const { domainSeparator, enabled } = await this.client.gateway.getGuaranteeVersionConfig(2);
|
|
105
|
-
if (!enabled) {
|
|
106
|
-
throw new errors_1.VerificationError('guarantee version 2 is not enabled on-chain');
|
|
107
|
-
}
|
|
108
|
-
expectedDomain = domainSeparator;
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
expectedDomain = this.guaranteeDomain;
|
|
112
|
-
}
|
|
113
|
-
const domainHex = expectedDomain.startsWith('0x')
|
|
114
|
-
? expectedDomain.slice(2)
|
|
115
|
-
: Buffer.from(expectedDomain).toString('hex');
|
|
116
|
-
if (domainHex.length !== 64) {
|
|
117
|
-
throw new errors_1.VerificationError(`guarantee domain separator has invalid length: expected 32 bytes, got ${domainHex.length / 2}`);
|
|
118
|
-
}
|
|
119
|
-
const claimsHex = Buffer.from(claims.domain).toString('hex');
|
|
120
|
-
if (claimsHex !== domainHex) {
|
|
121
|
-
throw new errors_1.VerificationError('guarantee domain mismatch');
|
|
122
|
-
}
|
|
123
|
-
return claims;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Claim payment on-chain by submitting a verified BLS certificate.
|
|
127
|
-
*
|
|
128
|
-
* Verifies the certificate first (see {@link verifyPaymentGuarantee}), then
|
|
129
|
-
* converts the BLS signature into the G2 coordinate words expected by the
|
|
130
|
-
* contract and submits the `remunerate` transaction.
|
|
131
|
-
*
|
|
132
|
-
* Requires the optional `@noble/curves` package for BLS point decompression.
|
|
133
|
-
*
|
|
134
|
-
* @param cert - BLS certificate to settle.
|
|
135
|
-
* @param waitOptions - Optional timeout/polling overrides for receipt polling.
|
|
136
|
-
* @throws {@link VerificationError} if the certificate is invalid or `claims`/`signature`
|
|
137
|
-
* are not hex strings.
|
|
138
|
-
* @throws {@link ContractError} if the contract call fails.
|
|
139
|
-
*/
|
|
140
|
-
async remunerate(cert, waitOptions) {
|
|
141
|
-
await this.verifyPaymentGuarantee(cert);
|
|
142
|
-
const describeValue = (value) => {
|
|
143
|
-
if (value === null)
|
|
144
|
-
return 'null';
|
|
145
|
-
if (value === undefined)
|
|
146
|
-
return 'undefined';
|
|
147
|
-
if (Array.isArray(value))
|
|
148
|
-
return `array(len=${value.length})`;
|
|
149
|
-
if (typeof value === 'object') {
|
|
150
|
-
const keys = Object.keys(value);
|
|
151
|
-
return `object(keys=${keys.slice(0, 6).join(',')}${keys.length > 6 ? ',...' : ''})`;
|
|
152
|
-
}
|
|
153
|
-
return typeof value;
|
|
154
|
-
};
|
|
155
|
-
if (debug_1.DEBUG_CERTS) {
|
|
156
|
-
const preview = typeof cert.signature === 'string' ? cert.signature.slice(0, 12) : '';
|
|
157
|
-
console.log(` debug remunerate: signature=${describeValue(cert.signature)} ${preview}`);
|
|
158
|
-
}
|
|
159
|
-
if (typeof cert.claims !== 'string') {
|
|
160
|
-
throw new errors_1.VerificationError(`certificate.claims must be a hex string, got ${describeValue(cert.claims)}`);
|
|
161
|
-
}
|
|
162
|
-
if (typeof cert.signature !== 'string') {
|
|
163
|
-
throw new errors_1.VerificationError(`certificate.signature must be a hex string, got ${describeValue(cert.signature)}`);
|
|
164
|
-
}
|
|
165
|
-
const sigWords = await (0, bls_1.signatureToWordsAsync)(cert.signature);
|
|
166
|
-
const claimsBytes = Buffer.from(cert.claims.replace(/^0x/, ''), 'hex');
|
|
167
|
-
if (waitOptions) {
|
|
168
|
-
return this.client.gateway.remunerate(claimsBytes, sigWords, waitOptions);
|
|
169
|
-
}
|
|
170
|
-
return this.client.gateway.remunerate(claimsBytes, sigWords);
|
|
171
|
-
}
|
|
172
|
-
/** List all tabs for this recipient that have been settled on-chain. */
|
|
173
|
-
async listSettledTabs() {
|
|
174
|
-
const tabs = await this.client.rpc.listSettledTabs(this.recipientAddress);
|
|
175
|
-
return tabs.map((t) => models_1.TabInfo.fromRpc(t));
|
|
176
|
-
}
|
|
177
|
-
/** List all tabs with outstanding (un-remunerated) guarantees for this recipient. */
|
|
178
|
-
async listPendingRemunerations() {
|
|
179
|
-
const items = await this.client.rpc.listPendingRemunerations(this.recipientAddress);
|
|
180
|
-
return items.map((item) => models_1.PendingRemunerationInfo.fromRpc(item));
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Fetch a single tab by ID.
|
|
184
|
-
*
|
|
185
|
-
* @param tabId - Tab identifier.
|
|
186
|
-
* @returns The tab, or `null` if not found.
|
|
187
|
-
*/
|
|
188
|
-
async getTab(tabId) {
|
|
189
|
-
const result = await this.client.rpc.getTab(tabId);
|
|
190
|
-
return result ? models_1.TabInfo.fromRpc(result) : null;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* List all tabs belonging to this recipient.
|
|
194
|
-
*
|
|
195
|
-
* @param settlementStatuses - Optional filter on settlement status (e.g. `['PENDING']`).
|
|
196
|
-
*/
|
|
197
|
-
async listRecipientTabs(settlementStatuses) {
|
|
198
|
-
const tabs = await this.client.rpc.listRecipientTabs(this.recipientAddress, settlementStatuses);
|
|
199
|
-
return tabs.map((t) => models_1.TabInfo.fromRpc(t));
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* List all guarantee requests associated with a tab.
|
|
203
|
-
*
|
|
204
|
-
* @param tabId - Tab identifier.
|
|
205
|
-
*/
|
|
206
|
-
async getTabGuarantees(tabId) {
|
|
207
|
-
const guarantees = await this.client.rpc.getTabGuarantees(tabId);
|
|
208
|
-
return guarantees.map((g) => models_1.GuaranteeInfo.fromRpc(g));
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Fetch the most recent guarantee for a tab.
|
|
212
|
-
*
|
|
213
|
-
* @param tabId - Tab identifier.
|
|
214
|
-
* @returns The latest {@link GuaranteeInfo}, or `null` if none exists.
|
|
215
|
-
*/
|
|
216
|
-
async getLatestGuarantee(tabId) {
|
|
217
|
-
const result = await this.client.rpc.getLatestGuarantee(tabId);
|
|
218
|
-
return result ? models_1.GuaranteeInfo.fromRpc(result) : null;
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* Fetch a specific guarantee by tab ID and request ID.
|
|
222
|
-
*
|
|
223
|
-
* @param tabId - Tab identifier.
|
|
224
|
-
* @param reqId - Guarantee request identifier.
|
|
225
|
-
* @returns The {@link GuaranteeInfo}, or `null` if not found.
|
|
226
|
-
*/
|
|
227
|
-
async getGuarantee(tabId, reqId) {
|
|
228
|
-
const result = await this.client.rpc.getGuarantee(tabId, reqId);
|
|
229
|
-
return result ? models_1.GuaranteeInfo.fromRpc(result) : null;
|
|
230
|
-
}
|
|
231
|
-
/** List all on-chain payments received by this recipient. */
|
|
232
|
-
async listRecipientPayments() {
|
|
233
|
-
const payments = await this.client.rpc.listRecipientPayments(this.recipientAddress);
|
|
234
|
-
return payments.map((p) => models_1.RecipientPaymentInfo.fromRpc(p));
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* List collateral deposit/withdrawal events associated with a tab.
|
|
238
|
-
*
|
|
239
|
-
* @param tabId - Tab identifier.
|
|
240
|
-
*/
|
|
241
|
-
async getCollateralEventsForTab(tabId) {
|
|
242
|
-
const events = await this.client.rpc.getCollateralEventsForTab(tabId);
|
|
243
|
-
return events.map((ev) => models_1.CollateralEventInfo.fromRpc(ev));
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Fetch the collateral balance a user has locked for a specific asset.
|
|
247
|
-
*
|
|
248
|
-
* @param userAddress - Address of the payer.
|
|
249
|
-
* @param assetAddress - ERC20 token address, or zero address for ETH.
|
|
250
|
-
* @returns Balance info, or `null` if no record exists.
|
|
251
|
-
*/
|
|
252
|
-
async getUserAssetBalance(userAddress, assetAddress) {
|
|
253
|
-
const balance = await this.client.rpc.getUserAssetBalance(userAddress, assetAddress);
|
|
254
|
-
return balance ? models_1.AssetBalanceInfo.fromRpc(balance) : null;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
exports.RecipientClient = RecipientClient;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { TabPaymentStatus } from '../models';
|
|
2
|
-
export declare const isNumericLike: (value: unknown) => value is number | bigint | string;
|
|
3
|
-
export type RpcTabStatus = {
|
|
4
|
-
paid?: number | bigint | string;
|
|
5
|
-
paidAmount?: number | bigint | string;
|
|
6
|
-
remunerated?: boolean;
|
|
7
|
-
paidOut?: boolean;
|
|
8
|
-
asset?: string;
|
|
9
|
-
assetAddress?: string;
|
|
10
|
-
};
|
|
11
|
-
export declare function tabStatusFromRpc(status: RpcTabStatus): TabPaymentStatus;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isNumericLike = void 0;
|
|
4
|
-
exports.tabStatusFromRpc = tabStatusFromRpc;
|
|
5
|
-
const utils_1 = require("../utils");
|
|
6
|
-
const isNumericLike = (value) => typeof value === 'number' || typeof value === 'bigint' || typeof value === 'string';
|
|
7
|
-
exports.isNumericLike = isNumericLike;
|
|
8
|
-
function tabStatusFromRpc(status) {
|
|
9
|
-
const paid = status.paid !== undefined ? status.paid : (status.paidAmount ?? 0);
|
|
10
|
-
const remunerated = status.remunerated ?? status.paidOut ?? false;
|
|
11
|
-
const asset = status.asset ?? status.assetAddress ?? '';
|
|
12
|
-
return {
|
|
13
|
-
paid: (0, utils_1.parseU256)(paid),
|
|
14
|
-
remunerated: Boolean(remunerated),
|
|
15
|
-
asset,
|
|
16
|
-
};
|
|
17
|
-
}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { PaymentGuaranteeRequestClaims, PaymentGuaranteeRequestClaimsV2, PaymentSignature, SigningScheme, TabInfo, TabPaymentStatus, UserInfo } from '../models';
|
|
2
|
-
import type { TxReceiptWaitOptions } from '../contract';
|
|
3
|
-
import type { Client } from './index';
|
|
4
|
-
/** Payer-side operations: collateral management, payment signing, withdrawals. */
|
|
5
|
-
export declare class UserClient {
|
|
6
|
-
private client;
|
|
7
|
-
constructor(client: Client);
|
|
8
|
-
/** 32-byte V1 guarantee domain separator (hex-prefixed). */
|
|
9
|
-
get guaranteeDomain(): string;
|
|
10
|
-
/**
|
|
11
|
-
* Approve the Core4Mica contract to spend an ERC20 token on your behalf.
|
|
12
|
-
* Call this before {@link deposit} for ERC20 deposits.
|
|
13
|
-
*
|
|
14
|
-
* @param token - ERC20 token contract address.
|
|
15
|
-
* @param amount - Amount to approve (in token base units).
|
|
16
|
-
* @param waitOptions - Optional timeout/polling overrides for receipt polling.
|
|
17
|
-
*/
|
|
18
|
-
approveErc20(token: string, amount: number | bigint | string, waitOptions?: TxReceiptWaitOptions): Promise<import("viem").TransactionReceipt | undefined>;
|
|
19
|
-
/**
|
|
20
|
-
* Deposit collateral into the Core4Mica contract.
|
|
21
|
-
*
|
|
22
|
-
* @param amount - Amount to deposit (in wei for ETH, base units for ERC20).
|
|
23
|
-
* @param erc20Token - ERC20 token address. Omit to deposit ETH.
|
|
24
|
-
* Call {@link approveErc20} first when depositing ERC20.
|
|
25
|
-
* @param waitOptions - Optional timeout/polling overrides.
|
|
26
|
-
*/
|
|
27
|
-
deposit(amount: number | bigint | string, erc20Token?: string, waitOptions?: TxReceiptWaitOptions): Promise<import("viem").TransactionReceipt>;
|
|
28
|
-
/**
|
|
29
|
-
* Fetch all asset positions for the current signer.
|
|
30
|
-
*
|
|
31
|
-
* @returns Array of positions — one entry per deposited asset, including
|
|
32
|
-
* locked collateral and any pending withdrawal.
|
|
33
|
-
* @throws {@link ContractError} if the contract call fails.
|
|
34
|
-
*/
|
|
35
|
-
getUser(opts?: {
|
|
36
|
-
blockNumber?: bigint;
|
|
37
|
-
}): Promise<UserInfo[]>;
|
|
38
|
-
/**
|
|
39
|
-
* Query the on-chain payment status of a tab.
|
|
40
|
-
*
|
|
41
|
-
* @param tabId - Tab identifier.
|
|
42
|
-
* @returns `{ paid, remunerated, asset }` — cumulative amount paid, whether
|
|
43
|
-
* it has been remunerated on-chain, and the asset address.
|
|
44
|
-
*/
|
|
45
|
-
getTabPaymentStatus(tabId: number | bigint): Promise<TabPaymentStatus>;
|
|
46
|
-
/**
|
|
47
|
-
* Sign a payment guarantee request with the configured signer.
|
|
48
|
-
*
|
|
49
|
-
* @param claims - V1 or V2 payment claims. Build V1 with
|
|
50
|
-
* {@link PaymentGuaranteeRequestClaims.new}; build V2 with
|
|
51
|
-
* {@link PaymentGuaranteeRequestClaimsV2} (requires validation policy fields).
|
|
52
|
-
* @param scheme - Signing scheme. Defaults to `EIP712`.
|
|
53
|
-
* Use `EIP191` for wallets that do not support typed data.
|
|
54
|
-
* @returns 65-byte ECDSA signature plus the scheme used.
|
|
55
|
-
* @throws {@link SigningError} if the signer address does not match `claims.userAddress`
|
|
56
|
-
* or the signing scheme is not supported by the account.
|
|
57
|
-
*/
|
|
58
|
-
signPayment(claims: PaymentGuaranteeRequestClaims | PaymentGuaranteeRequestClaimsV2, scheme?: SigningScheme): Promise<PaymentSignature>;
|
|
59
|
-
/**
|
|
60
|
-
* List all tabs where the current signer is the payer.
|
|
61
|
-
*
|
|
62
|
-
* @param settlementStatuses - Optional filter on settlement status (e.g. `['pending']`).
|
|
63
|
-
* @returns Array of tabs belonging to the current signer.
|
|
64
|
-
*/
|
|
65
|
-
listTabs(settlementStatuses?: string[]): Promise<TabInfo[]>;
|
|
66
|
-
/**
|
|
67
|
-
* Pay a tab on-chain. Automatically resolves the recipient, asset, and amount
|
|
68
|
-
* from the tab and its latest guarantee.
|
|
69
|
-
*
|
|
70
|
-
* @param tabId - Tab identifier.
|
|
71
|
-
* @param waitOptions - Optional timeout/polling overrides.
|
|
72
|
-
* @throws if the tab is not found or has no guarantee.
|
|
73
|
-
*/
|
|
74
|
-
payTab(tabId: number | bigint, waitOptions?: TxReceiptWaitOptions): Promise<unknown>;
|
|
75
|
-
/**
|
|
76
|
-
* Pay a tab on-chain with explicit parameters.
|
|
77
|
-
*
|
|
78
|
-
* @param tabId - Tab identifier.
|
|
79
|
-
* @param reqId - Request ID from the latest guarantee (used for ETH payment memo).
|
|
80
|
-
* @param amount - Amount to pay (in token base units / wei).
|
|
81
|
-
* @param recipientAddress - Address of the recipient.
|
|
82
|
-
* @param erc20Token - ERC20 token address. Omit to pay in ETH.
|
|
83
|
-
* @param waitOptions - Optional timeout/polling overrides.
|
|
84
|
-
*/
|
|
85
|
-
payTab(tabId: number | bigint, reqId: number | bigint, amount: number | bigint | string, recipientAddress: string, erc20Token?: string, waitOptions?: TxReceiptWaitOptions): Promise<unknown>;
|
|
86
|
-
/**
|
|
87
|
-
* Initiate a collateral withdrawal request. The withdrawal is subject to an
|
|
88
|
-
* on-chain timelock before it can be finalised.
|
|
89
|
-
*
|
|
90
|
-
* @param amount - Amount to withdraw (base units / wei).
|
|
91
|
-
* @param erc20Token - ERC20 token address. Omit to withdraw ETH.
|
|
92
|
-
* @param waitOptions - Optional timeout/polling overrides.
|
|
93
|
-
*/
|
|
94
|
-
requestWithdrawal(amount: number | bigint | string, erc20Token?: string, waitOptions?: TxReceiptWaitOptions): Promise<import("viem").TransactionReceipt>;
|
|
95
|
-
/**
|
|
96
|
-
* Cancel a pending withdrawal request before the timelock expires.
|
|
97
|
-
*
|
|
98
|
-
* @param erc20Token - ERC20 token address. Omit to cancel an ETH withdrawal.
|
|
99
|
-
* @param waitOptions - Optional timeout/polling overrides.
|
|
100
|
-
*/
|
|
101
|
-
cancelWithdrawal(erc20Token?: string, waitOptions?: TxReceiptWaitOptions): Promise<import("viem").TransactionReceipt>;
|
|
102
|
-
/**
|
|
103
|
-
* Finalise a withdrawal after the timelock has elapsed.
|
|
104
|
-
*
|
|
105
|
-
* @param erc20Token - ERC20 token address. Omit to finalise an ETH withdrawal.
|
|
106
|
-
* @param waitOptions - Optional timeout/polling overrides.
|
|
107
|
-
*/
|
|
108
|
-
finalizeWithdrawal(erc20Token?: string, waitOptions?: TxReceiptWaitOptions): Promise<import("viem").TransactionReceipt>;
|
|
109
|
-
}
|
package/dist/src/client/user.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UserClient = void 0;
|
|
4
|
-
const models_1 = require("../models");
|
|
5
|
-
const shared_1 = require("./shared");
|
|
6
|
-
const utils_1 = require("../utils");
|
|
7
|
-
/** Payer-side operations: collateral management, payment signing, withdrawals. */
|
|
8
|
-
class UserClient {
|
|
9
|
-
client;
|
|
10
|
-
constructor(client) {
|
|
11
|
-
this.client = client;
|
|
12
|
-
}
|
|
13
|
-
/** 32-byte V1 guarantee domain separator (hex-prefixed). */
|
|
14
|
-
get guaranteeDomain() {
|
|
15
|
-
return this.client.guaranteeDomain;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Approve the Core4Mica contract to spend an ERC20 token on your behalf.
|
|
19
|
-
* Call this before {@link deposit} for ERC20 deposits.
|
|
20
|
-
*
|
|
21
|
-
* @param token - ERC20 token contract address.
|
|
22
|
-
* @param amount - Amount to approve (in token base units).
|
|
23
|
-
* @param waitOptions - Optional timeout/polling overrides for receipt polling.
|
|
24
|
-
*/
|
|
25
|
-
async approveErc20(token, amount, waitOptions) {
|
|
26
|
-
return this.client.gateway.approveErc20(token, amount, waitOptions);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Deposit collateral into the Core4Mica contract.
|
|
30
|
-
*
|
|
31
|
-
* @param amount - Amount to deposit (in wei for ETH, base units for ERC20).
|
|
32
|
-
* @param erc20Token - ERC20 token address. Omit to deposit ETH.
|
|
33
|
-
* Call {@link approveErc20} first when depositing ERC20.
|
|
34
|
-
* @param waitOptions - Optional timeout/polling overrides.
|
|
35
|
-
*/
|
|
36
|
-
async deposit(amount, erc20Token, waitOptions) {
|
|
37
|
-
return this.client.gateway.deposit(amount, erc20Token, waitOptions);
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Fetch all asset positions for the current signer.
|
|
41
|
-
*
|
|
42
|
-
* @returns Array of positions — one entry per deposited asset, including
|
|
43
|
-
* locked collateral and any pending withdrawal.
|
|
44
|
-
* @throws {@link ContractError} if the contract call fails.
|
|
45
|
-
*/
|
|
46
|
-
async getUser(opts) {
|
|
47
|
-
const assets = await this.client.gateway.getUserAssets(opts);
|
|
48
|
-
return assets.map((a) => ({
|
|
49
|
-
asset: a.asset,
|
|
50
|
-
collateral: (0, utils_1.parseU256)(a.collateral),
|
|
51
|
-
withdrawalRequestAmount: (0, utils_1.parseU256)(a.withdrawalRequestAmount),
|
|
52
|
-
withdrawalRequestTimestamp: Number(a.withdrawalRequestTimestamp),
|
|
53
|
-
}));
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Query the on-chain payment status of a tab.
|
|
57
|
-
*
|
|
58
|
-
* @param tabId - Tab identifier.
|
|
59
|
-
* @returns `{ paid, remunerated, asset }` — cumulative amount paid, whether
|
|
60
|
-
* it has been remunerated on-chain, and the asset address.
|
|
61
|
-
*/
|
|
62
|
-
async getTabPaymentStatus(tabId) {
|
|
63
|
-
const status = await this.client.gateway.getPaymentStatus(tabId);
|
|
64
|
-
return (0, shared_1.tabStatusFromRpc)(status);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Sign a payment guarantee request with the configured signer.
|
|
68
|
-
*
|
|
69
|
-
* @param claims - V1 or V2 payment claims. Build V1 with
|
|
70
|
-
* {@link PaymentGuaranteeRequestClaims.new}; build V2 with
|
|
71
|
-
* {@link PaymentGuaranteeRequestClaimsV2} (requires validation policy fields).
|
|
72
|
-
* @param scheme - Signing scheme. Defaults to `EIP712`.
|
|
73
|
-
* Use `EIP191` for wallets that do not support typed data.
|
|
74
|
-
* @returns 65-byte ECDSA signature plus the scheme used.
|
|
75
|
-
* @throws {@link SigningError} if the signer address does not match `claims.userAddress`
|
|
76
|
-
* or the signing scheme is not supported by the account.
|
|
77
|
-
*/
|
|
78
|
-
async signPayment(claims, scheme = models_1.SigningScheme.EIP712) {
|
|
79
|
-
return this.client.signer.signRequest(this.client.params, claims, scheme);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* List all tabs where the current signer is the payer.
|
|
83
|
-
*
|
|
84
|
-
* @param settlementStatuses - Optional filter on settlement status (e.g. `['pending']`).
|
|
85
|
-
* @returns Array of tabs belonging to the current signer.
|
|
86
|
-
*/
|
|
87
|
-
async listTabs(settlementStatuses) {
|
|
88
|
-
const myAddress = (0, utils_1.normalizeAddress)(this.client.signer.signer.address);
|
|
89
|
-
const raw = await this.client.rpc.listUserTabs(myAddress, settlementStatuses);
|
|
90
|
-
return raw.map((t) => models_1.TabInfo.fromRpc(t));
|
|
91
|
-
}
|
|
92
|
-
async payTab(tabId, reqIdOrWaitOptions, amount, recipientAddress, erc20Token, waitOptions) {
|
|
93
|
-
// Simple overload: auto-resolve from tab + latest guarantee
|
|
94
|
-
if (reqIdOrWaitOptions === undefined ||
|
|
95
|
-
(typeof reqIdOrWaitOptions === 'object' &&
|
|
96
|
-
reqIdOrWaitOptions !== null &&
|
|
97
|
-
!('valueOf' in reqIdOrWaitOptions && typeof reqIdOrWaitOptions.valueOf() === 'bigint'))) {
|
|
98
|
-
const opts = reqIdOrWaitOptions;
|
|
99
|
-
const tab = await this.client.recipient.getTab(tabId);
|
|
100
|
-
if (!tab)
|
|
101
|
-
throw new Error(`Tab ${tabId} not found`);
|
|
102
|
-
const guarantee = await this.client.recipient.getLatestGuarantee(tabId);
|
|
103
|
-
if (!guarantee)
|
|
104
|
-
throw new Error(`Tab ${tabId} has no guarantee`);
|
|
105
|
-
const isEth = tab.assetAddress === '0x0000000000000000000000000000000000000000';
|
|
106
|
-
return isEth
|
|
107
|
-
? this.client.gateway.payTabEth(tabId, guarantee.reqId, guarantee.amount, tab.recipientAddress, opts)
|
|
108
|
-
: this.client.gateway.payTabErc20(tabId, guarantee.amount, tab.assetAddress, tab.recipientAddress, opts);
|
|
109
|
-
}
|
|
110
|
-
// Explicit overload
|
|
111
|
-
const reqId = reqIdOrWaitOptions;
|
|
112
|
-
if (erc20Token) {
|
|
113
|
-
return this.client.gateway.payTabErc20(tabId, amount, erc20Token, recipientAddress, waitOptions);
|
|
114
|
-
}
|
|
115
|
-
return this.client.gateway.payTabEth(tabId, reqId, amount, recipientAddress, waitOptions);
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Initiate a collateral withdrawal request. The withdrawal is subject to an
|
|
119
|
-
* on-chain timelock before it can be finalised.
|
|
120
|
-
*
|
|
121
|
-
* @param amount - Amount to withdraw (base units / wei).
|
|
122
|
-
* @param erc20Token - ERC20 token address. Omit to withdraw ETH.
|
|
123
|
-
* @param waitOptions - Optional timeout/polling overrides.
|
|
124
|
-
*/
|
|
125
|
-
async requestWithdrawal(amount, erc20Token, waitOptions) {
|
|
126
|
-
return this.client.gateway.requestWithdrawal(amount, erc20Token, waitOptions);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Cancel a pending withdrawal request before the timelock expires.
|
|
130
|
-
*
|
|
131
|
-
* @param erc20Token - ERC20 token address. Omit to cancel an ETH withdrawal.
|
|
132
|
-
* @param waitOptions - Optional timeout/polling overrides.
|
|
133
|
-
*/
|
|
134
|
-
async cancelWithdrawal(erc20Token, waitOptions) {
|
|
135
|
-
return this.client.gateway.cancelWithdrawal(erc20Token, waitOptions);
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Finalise a withdrawal after the timelock has elapsed.
|
|
139
|
-
*
|
|
140
|
-
* @param erc20Token - ERC20 token address. Omit to finalise an ETH withdrawal.
|
|
141
|
-
* @param waitOptions - Optional timeout/polling overrides.
|
|
142
|
-
*/
|
|
143
|
-
async finalizeWithdrawal(erc20Token, waitOptions) {
|
|
144
|
-
return this.client.gateway.finalizeWithdrawal(erc20Token, waitOptions);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
exports.UserClient = UserClient;
|
package/dist/src/config.d.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { Account } from 'viem/accounts';
|
|
2
|
-
/** Validated configuration used to construct a {@link Client}. Produced by {@link ConfigBuilder.build}. */
|
|
3
|
-
export interface Config {
|
|
4
|
-
/** URL of the 4Mica core RPC service. */
|
|
5
|
-
rpcUrl: string;
|
|
6
|
-
/** viem `Account` used to sign payments and authenticate requests. */
|
|
7
|
-
signer: Account;
|
|
8
|
-
/** Override for the Ethereum HTTP RPC URL (defaults to the value returned by the core service). */
|
|
9
|
-
ethereumHttpRpcUrl?: string;
|
|
10
|
-
/** Override for the Core4Mica contract address (defaults to the value returned by the core service). */
|
|
11
|
-
contractAddress?: string;
|
|
12
|
-
/** API key for admin-scoped RPC endpoints. */
|
|
13
|
-
adminApiKey?: string;
|
|
14
|
-
/** Static bearer token for authenticated RPC calls. Mutually exclusive with SIWE auth. */
|
|
15
|
-
bearerToken?: string;
|
|
16
|
-
/** URL of the SIWE authentication endpoint. Defaults to `rpcUrl` when auth is enabled. */
|
|
17
|
-
authUrl?: string;
|
|
18
|
-
/** Seconds before token expiry at which the auth session proactively refreshes. Defaults to 60. */
|
|
19
|
-
authRefreshMarginSecs?: number;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Fluent builder for {@link Config}.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```ts
|
|
26
|
-
* const cfg = new ConfigBuilder()
|
|
27
|
-
* .walletPrivateKey('0x...')
|
|
28
|
-
* .build();
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* All fields can also be supplied from environment variables via {@link fromEnv}.
|
|
32
|
-
*/
|
|
33
|
-
export declare class ConfigBuilder {
|
|
34
|
-
private _rpcUrl;
|
|
35
|
-
private _walletPrivateKey;
|
|
36
|
-
private _signer;
|
|
37
|
-
private _ethereumHttpRpcUrl?;
|
|
38
|
-
private _contractAddress?;
|
|
39
|
-
private _adminApiKey?;
|
|
40
|
-
private _bearerToken?;
|
|
41
|
-
private _authEnabled;
|
|
42
|
-
private _authUrl?;
|
|
43
|
-
private _authRefreshMarginSecs?;
|
|
44
|
-
/** Set the 4Mica core RPC URL directly. Use {@link network} to select a hosted network by name instead. Defaults to `https://ethereum.sepolia.api.4mica.xyz/`. */
|
|
45
|
-
rpcUrl(value: string): ConfigBuilder;
|
|
46
|
-
/**
|
|
47
|
-
* Select a hosted 4Mica network by shorthand or CAIP-2 identifier.
|
|
48
|
-
* Resolves to the corresponding core API URL.
|
|
49
|
-
* Mutually exclusive with {@link rpcUrl} — last call wins.
|
|
50
|
-
*
|
|
51
|
-
* Supported values: `"base"` / `"eip155:8453"`, `"base-sepolia"` / `"eip155:84532"`,
|
|
52
|
-
* `"ethereum-sepolia"` / `"eip155:11155111"`.
|
|
53
|
-
*
|
|
54
|
-
* @throws {@link ConfigError} if the network is not recognised.
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
* ```ts
|
|
58
|
-
* new ConfigBuilder().network("base").walletPrivateKey("0x...").build();
|
|
59
|
-
* new ConfigBuilder().network("eip155:8453").walletPrivateKey("0x...").build();
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
network(value: string): ConfigBuilder;
|
|
63
|
-
/** Set the wallet private key (hex string). Mutually exclusive with {@link signer}. */
|
|
64
|
-
walletPrivateKey(value: string): ConfigBuilder;
|
|
65
|
-
/** Set a pre-built viem `Account` directly. Mutually exclusive with {@link walletPrivateKey}. */
|
|
66
|
-
signer(value: Account): ConfigBuilder;
|
|
67
|
-
/** Override the Ethereum HTTP RPC URL used for on-chain calls. */
|
|
68
|
-
ethereumHttpRpcUrl(value: string): ConfigBuilder;
|
|
69
|
-
/** Override the Core4Mica contract address. */
|
|
70
|
-
contractAddress(value: string): ConfigBuilder;
|
|
71
|
-
/** Set an admin API key for privileged RPC endpoints. */
|
|
72
|
-
adminApiKey(value: string): ConfigBuilder;
|
|
73
|
-
/** Set a static bearer token for authenticated RPC calls. Disables SIWE auth. */
|
|
74
|
-
bearerToken(value: string): ConfigBuilder;
|
|
75
|
-
/** Enable SIWE authentication using the default RPC URL as the auth endpoint. Auth is enabled by default; this is a no-op unless you previously called a method that disabled it. */
|
|
76
|
-
enableAuth(): ConfigBuilder;
|
|
77
|
-
/** Set a custom SIWE authentication endpoint and enable auth. */
|
|
78
|
-
authUrl(value: string): ConfigBuilder;
|
|
79
|
-
/** Set the number of seconds before token expiry at which the session proactively refreshes. Enables auth. */
|
|
80
|
-
authRefreshMarginSecs(value: number): ConfigBuilder;
|
|
81
|
-
/**
|
|
82
|
-
* Load configuration from environment variables.
|
|
83
|
-
*
|
|
84
|
-
* Recognised variables:
|
|
85
|
-
* - `4MICA_NETWORK` — shorthand or CAIP-2 id (e.g. `base`); takes precedence over `4MICA_RPC_URL`
|
|
86
|
-
* - `4MICA_RPC_URL`
|
|
87
|
-
* - `4MICA_WALLET_PRIVATE_KEY`
|
|
88
|
-
* - `4MICA_ETHEREUM_HTTP_RPC_URL`
|
|
89
|
-
* - `4MICA_CONTRACT_ADDRESS`
|
|
90
|
-
* - `4MICA_ADMIN_API_KEY`
|
|
91
|
-
* - `4MICA_BEARER_TOKEN`
|
|
92
|
-
* - `4MICA_AUTH_URL`
|
|
93
|
-
* - `4MICA_AUTH_REFRESH_MARGIN_SECS`
|
|
94
|
-
*/
|
|
95
|
-
fromEnv(): ConfigBuilder;
|
|
96
|
-
/**
|
|
97
|
-
* Validate all settings and return an immutable {@link Config}.
|
|
98
|
-
*
|
|
99
|
-
* @throws {@link ConfigError} if required fields are missing, URLs are invalid,
|
|
100
|
-
* or the auth refresh margin is not a finite non-negative number.
|
|
101
|
-
*/
|
|
102
|
-
build(): Config;
|
|
103
|
-
}
|