@4mica/sdk 1.2.17 → 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 -151
- 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 -261
- package/dist/src/models.js +0 -360
- 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
package/dist/src/bls.js
DELETED
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.signatureToWords = signatureToWords;
|
|
37
|
-
exports.signatureToWordsAsync = signatureToWordsAsync;
|
|
38
|
-
const viem_1 = require("viem");
|
|
39
|
-
const debug_1 = require("./debug");
|
|
40
|
-
const errors_1 = require("./errors");
|
|
41
|
-
let curvesCache = null;
|
|
42
|
-
let curvesPromise = null;
|
|
43
|
-
function splitFp(value) {
|
|
44
|
-
const be48 = value.toString(16).padStart(96, '0');
|
|
45
|
-
const bytes = (0, viem_1.toBytes)(`0x${be48}`);
|
|
46
|
-
const hi = new Uint8Array(32);
|
|
47
|
-
hi.set(bytes.slice(0, 16), 16);
|
|
48
|
-
const lo = bytes.slice(16);
|
|
49
|
-
return [hi, lo];
|
|
50
|
-
}
|
|
51
|
-
const normalizeBlsImportError = (err) => {
|
|
52
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
53
|
-
if (message.includes('ERR_PACKAGE_PATH_NOT_EXPORTED')) {
|
|
54
|
-
return new errors_1.VerificationError('BLS decoding requires @noble/curves; use the .js subpath (bls12-381.js) or update the SDK.');
|
|
55
|
-
}
|
|
56
|
-
if (message.includes('ERR_REQUIRE_ESM') || message.includes('require() of ES Module')) {
|
|
57
|
-
return new errors_1.VerificationError('BLS decoding requires @noble/curves; ESM install detected. Use signatureToWordsAsync or run in CJS.');
|
|
58
|
-
}
|
|
59
|
-
return new errors_1.VerificationError('BLS decoding requires @noble/curves; install it to enable remuneration');
|
|
60
|
-
};
|
|
61
|
-
const loadCurvesSync = () => {
|
|
62
|
-
if (curvesCache)
|
|
63
|
-
return curvesCache;
|
|
64
|
-
try {
|
|
65
|
-
// Lazy require to keep the dependency optional.
|
|
66
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
67
|
-
curvesCache = require('@noble/curves/bls12-381');
|
|
68
|
-
return curvesCache;
|
|
69
|
-
}
|
|
70
|
-
catch {
|
|
71
|
-
try {
|
|
72
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
73
|
-
curvesCache = require('@noble/curves/bls12-381.js');
|
|
74
|
-
return curvesCache;
|
|
75
|
-
}
|
|
76
|
-
catch (err2) {
|
|
77
|
-
throw normalizeBlsImportError(err2);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
const isBlsModule = (mod) => mod !== null &&
|
|
82
|
-
typeof mod === 'object' &&
|
|
83
|
-
'bls12_381' in mod &&
|
|
84
|
-
typeof mod.bls12_381 === 'object';
|
|
85
|
-
const loadCurvesAsync = async () => {
|
|
86
|
-
if (curvesCache)
|
|
87
|
-
return curvesCache;
|
|
88
|
-
if (curvesPromise)
|
|
89
|
-
return curvesPromise;
|
|
90
|
-
curvesPromise = (async () => {
|
|
91
|
-
try {
|
|
92
|
-
// Try CJS first.
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
94
|
-
return require('@noble/curves/bls12-381');
|
|
95
|
-
}
|
|
96
|
-
catch {
|
|
97
|
-
const mod = await Promise.resolve().then(() => __importStar(require('@noble/curves/bls12-381.js')));
|
|
98
|
-
if (!isBlsModule(mod)) {
|
|
99
|
-
throw new errors_1.VerificationError('BLS decoding: unexpected module shape from @noble/curves');
|
|
100
|
-
}
|
|
101
|
-
return mod;
|
|
102
|
-
}
|
|
103
|
-
})();
|
|
104
|
-
curvesCache = await curvesPromise;
|
|
105
|
-
return curvesCache;
|
|
106
|
-
};
|
|
107
|
-
const normalizeSignature = (input) => {
|
|
108
|
-
if (debug_1.DEBUG_BLS) {
|
|
109
|
-
const type = input && typeof input === 'object'
|
|
110
|
-
? `object(keys=${Object.keys(input)
|
|
111
|
-
.slice(0, 6)
|
|
112
|
-
.join(',')})`
|
|
113
|
-
: typeof input;
|
|
114
|
-
console.log(` debug bls: normalizeSignature input=${type}`);
|
|
115
|
-
}
|
|
116
|
-
if (typeof input === 'string') {
|
|
117
|
-
const raw = input.startsWith('0x') ? input.slice(2) : input;
|
|
118
|
-
const bytes = (0, viem_1.toBytes)(`0x${raw}`);
|
|
119
|
-
return { hex: raw, bytes };
|
|
120
|
-
}
|
|
121
|
-
if (input instanceof Uint8Array) {
|
|
122
|
-
const hex = Buffer.from(input).toString('hex');
|
|
123
|
-
return { hex, bytes: input };
|
|
124
|
-
}
|
|
125
|
-
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(input)) {
|
|
126
|
-
const hex = input.toString('hex');
|
|
127
|
-
return { hex, bytes: new Uint8Array(input) };
|
|
128
|
-
}
|
|
129
|
-
if (input instanceof ArrayBuffer) {
|
|
130
|
-
const bytes = new Uint8Array(input);
|
|
131
|
-
const hex = Buffer.from(bytes).toString('hex');
|
|
132
|
-
return { hex, bytes };
|
|
133
|
-
}
|
|
134
|
-
if (ArrayBuffer.isView(input)) {
|
|
135
|
-
const bytes = new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
136
|
-
const hex = Buffer.from(bytes).toString('hex');
|
|
137
|
-
return { hex, bytes };
|
|
138
|
-
}
|
|
139
|
-
if (Array.isArray(input)) {
|
|
140
|
-
const bytes = Uint8Array.from(input);
|
|
141
|
-
const hex = Buffer.from(bytes).toString('hex');
|
|
142
|
-
return { hex, bytes };
|
|
143
|
-
}
|
|
144
|
-
if (input && typeof input === 'object') {
|
|
145
|
-
const record = input;
|
|
146
|
-
if (Array.isArray(record.data)) {
|
|
147
|
-
const arr = record.data;
|
|
148
|
-
if (arr.some((b) => typeof b !== 'number' || b < 0 || b > 255 || !Number.isInteger(b))) {
|
|
149
|
-
throw new errors_1.VerificationError('signature data array contains invalid byte values');
|
|
150
|
-
}
|
|
151
|
-
const bytes = Uint8Array.from(arr);
|
|
152
|
-
const hex = Buffer.from(bytes).toString('hex');
|
|
153
|
-
return { hex, bytes };
|
|
154
|
-
}
|
|
155
|
-
if ('bytes' in record) {
|
|
156
|
-
return normalizeSignature(record.bytes);
|
|
157
|
-
}
|
|
158
|
-
if ('signature' in record) {
|
|
159
|
-
return normalizeSignature(record.signature);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
const label = input && typeof input === 'object'
|
|
163
|
-
? `object(keys=${Object.keys(input)
|
|
164
|
-
.slice(0, 6)
|
|
165
|
-
.join(',')})`
|
|
166
|
-
: typeof input;
|
|
167
|
-
throw new errors_1.VerificationError(`expected signature hex string or bytes, got ${label}`);
|
|
168
|
-
};
|
|
169
|
-
const signatureToWordsWith = (curves, signatureHex) => {
|
|
170
|
-
const toBigint = (field) => {
|
|
171
|
-
if (typeof field === 'bigint' || typeof field === 'number' || typeof field === 'string') {
|
|
172
|
-
return BigInt(field);
|
|
173
|
-
}
|
|
174
|
-
if (field && typeof field === 'object' && 'value' in field && field.value !== undefined) {
|
|
175
|
-
const value = field.value;
|
|
176
|
-
if (typeof value === 'bigint' || typeof value === 'number' || typeof value === 'string') {
|
|
177
|
-
return BigInt(value);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
throw new errors_1.VerificationError('invalid BLS field element');
|
|
181
|
-
};
|
|
182
|
-
const readFp2 = (value) => {
|
|
183
|
-
if (!value || typeof value !== 'object') {
|
|
184
|
-
throw new errors_1.VerificationError('invalid BLS field element');
|
|
185
|
-
}
|
|
186
|
-
const record = value;
|
|
187
|
-
if (Array.isArray(record.c) && record.c.length >= 2) {
|
|
188
|
-
return [record.c[0], record.c[1]];
|
|
189
|
-
}
|
|
190
|
-
if ('c0' in record && 'c1' in record) {
|
|
191
|
-
return [record.c0, record.c1];
|
|
192
|
-
}
|
|
193
|
-
if (Array.isArray(record.coeffs)) {
|
|
194
|
-
const coeffs = record.coeffs;
|
|
195
|
-
if (coeffs.length >= 2)
|
|
196
|
-
return [coeffs[0], coeffs[1]];
|
|
197
|
-
}
|
|
198
|
-
throw new errors_1.VerificationError('invalid BLS field element');
|
|
199
|
-
};
|
|
200
|
-
try {
|
|
201
|
-
const sig = normalizeSignature(signatureHex);
|
|
202
|
-
const g2 = curves.bls12_381.G2;
|
|
203
|
-
const pointCtor = g2.Point ?? g2.ProjectivePoint;
|
|
204
|
-
if (!pointCtor?.fromHex) {
|
|
205
|
-
throw new errors_1.VerificationError('unsupported @noble/curves BLS export');
|
|
206
|
-
}
|
|
207
|
-
let point;
|
|
208
|
-
try {
|
|
209
|
-
point = pointCtor.fromHex(sig.bytes);
|
|
210
|
-
}
|
|
211
|
-
catch (err) {
|
|
212
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
213
|
-
if (message.includes('hex string expected')) {
|
|
214
|
-
point = pointCtor.fromHex(sig.hex);
|
|
215
|
-
}
|
|
216
|
-
else {
|
|
217
|
-
throw err;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
const affine = point.toAffine();
|
|
221
|
-
const [x0, x1] = readFp2(affine.x);
|
|
222
|
-
const [y0, y1] = readFp2(affine.y);
|
|
223
|
-
const coords = [x0, x1, y0, y1].map((fp) => toBigint(fp));
|
|
224
|
-
const words = [];
|
|
225
|
-
coords.forEach((coord) => {
|
|
226
|
-
const [hi, lo] = splitFp(coord);
|
|
227
|
-
words.push(hi, lo);
|
|
228
|
-
});
|
|
229
|
-
return words;
|
|
230
|
-
}
|
|
231
|
-
catch (err) {
|
|
232
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
233
|
-
throw new errors_1.VerificationError(`invalid BLS signature: ${message}`);
|
|
234
|
-
}
|
|
235
|
-
};
|
|
236
|
-
/**
|
|
237
|
-
* Expand a compressed G2 signature into the tuple expected by the contract.
|
|
238
|
-
* This mirrors the Python helper and requires the optional `@noble/curves` dependency.
|
|
239
|
-
*/
|
|
240
|
-
function signatureToWords(signatureHex) {
|
|
241
|
-
const curves = loadCurvesSync();
|
|
242
|
-
return signatureToWordsWith(curves, signatureHex);
|
|
243
|
-
}
|
|
244
|
-
async function signatureToWordsAsync(signatureHex) {
|
|
245
|
-
const curves = await loadCurvesAsync();
|
|
246
|
-
return signatureToWordsWith(curves, signatureHex);
|
|
247
|
-
}
|
package/dist/src/chain.d.ts
DELETED
package/dist/src/chain.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getChain = getChain;
|
|
4
|
-
const chains_1 = require("viem/chains");
|
|
5
|
-
const CHAINS = {
|
|
6
|
-
1: chains_1.mainnet,
|
|
7
|
-
11155111: chains_1.sepolia,
|
|
8
|
-
8453: chains_1.base,
|
|
9
|
-
84532: chains_1.baseSepolia,
|
|
10
|
-
137: chains_1.polygon,
|
|
11
|
-
80002: chains_1.polygonAmoy,
|
|
12
|
-
};
|
|
13
|
-
function getChain(chainId, rpcUrl) {
|
|
14
|
-
const chain = CHAINS[chainId];
|
|
15
|
-
if (chain)
|
|
16
|
-
return chain;
|
|
17
|
-
return {
|
|
18
|
-
id: chainId,
|
|
19
|
-
name: `local-${chainId}`,
|
|
20
|
-
nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
|
|
21
|
-
rpcUrls: {
|
|
22
|
-
default: { http: [rpcUrl] },
|
|
23
|
-
public: { http: [rpcUrl] },
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import type { AuthTokens } from '../auth';
|
|
2
|
-
import { Config } from '../config';
|
|
3
|
-
import { ContractGateway } from '../contract';
|
|
4
|
-
import { RpcProxy } from '../rpc';
|
|
5
|
-
import { CorePublicParameters } from '../models';
|
|
6
|
-
import { PaymentSigner } from '../signing';
|
|
7
|
-
import { RecipientClient } from './recipient';
|
|
8
|
-
import { UserClient } from './user';
|
|
9
|
-
/**
|
|
10
|
-
* Top-level SDK client. Holds a live connection to the 4Mica core RPC and the
|
|
11
|
-
* on-chain Core4Mica contract. Obtain an instance via {@link Client.new}.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* const cfg = new ConfigBuilder().walletPrivateKey("0x...").build();
|
|
16
|
-
* const client = await Client.new(cfg);
|
|
17
|
-
* try {
|
|
18
|
-
* // client.user – payer-side operations
|
|
19
|
-
* // client.recipient – recipient-side operations
|
|
20
|
-
* } finally {
|
|
21
|
-
* await client.aclose();
|
|
22
|
-
* }
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
export declare class Client {
|
|
26
|
-
/** Low-level RPC proxy to the 4Mica core service. */
|
|
27
|
-
readonly rpc: RpcProxy;
|
|
28
|
-
/** Chain and contract parameters fetched from the core service at startup. */
|
|
29
|
-
readonly params: CorePublicParameters;
|
|
30
|
-
/** viem-backed gateway for on-chain calls (deposit, remunerate, …). */
|
|
31
|
-
readonly gateway: ContractGateway;
|
|
32
|
-
/** 32-byte domain separator used to verify V1 BLS guarantee certificates. */
|
|
33
|
-
readonly guaranteeDomain: string;
|
|
34
|
-
/** Payer-side operations: deposit, sign, withdraw. */
|
|
35
|
-
readonly user: UserClient;
|
|
36
|
-
/** Recipient-side operations: tabs, guarantees, remuneration. */
|
|
37
|
-
readonly recipient: RecipientClient;
|
|
38
|
-
/** Payment signing wrapper around the configured viem Account. */
|
|
39
|
-
readonly signer: PaymentSigner;
|
|
40
|
-
private authSession?;
|
|
41
|
-
private constructor();
|
|
42
|
-
/**
|
|
43
|
-
* Create and fully initialise a Client.
|
|
44
|
-
*
|
|
45
|
-
* Fetches public parameters from the core service, validates that the
|
|
46
|
-
* Ethereum RPC is on the expected chain, and sets up SIWE auth if configured.
|
|
47
|
-
*
|
|
48
|
-
* @param cfg - Validated configuration produced by {@link ConfigBuilder.build}.
|
|
49
|
-
* @throws {@link ConfigError} if the configuration is invalid.
|
|
50
|
-
* @throws {@link RpcError} if the core service is unreachable.
|
|
51
|
-
* @throws {@link ContractError} if the Ethereum RPC returns the wrong chain ID.
|
|
52
|
-
*/
|
|
53
|
-
static new(cfg: Config): Promise<Client>;
|
|
54
|
-
private static buildGateway;
|
|
55
|
-
/**
|
|
56
|
-
* Release client resources. Safe to call multiple times.
|
|
57
|
-
* Use in a `finally` block to ensure cleanup after use.
|
|
58
|
-
*/
|
|
59
|
-
aclose(): Promise<void>;
|
|
60
|
-
/**
|
|
61
|
-
* Perform an explicit SIWE login and return the resulting tokens.
|
|
62
|
-
*
|
|
63
|
-
* Not required for normal operation — the first authenticated RPC call
|
|
64
|
-
* triggers auth automatically. Call this to pre-warm the session.
|
|
65
|
-
*
|
|
66
|
-
* @throws {@link AuthMissingConfigError} if auth was not enabled in the config.
|
|
67
|
-
*/
|
|
68
|
-
login(): Promise<AuthTokens>;
|
|
69
|
-
}
|
|
70
|
-
export { UserClient } from './user';
|
|
71
|
-
export { RecipientClient } from './recipient';
|
package/dist/src/client/index.js
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RecipientClient = exports.UserClient = exports.Client = void 0;
|
|
4
|
-
const auth_1 = require("../auth");
|
|
5
|
-
const contract_1 = require("../contract");
|
|
6
|
-
const errors_1 = require("../errors");
|
|
7
|
-
const rpc_1 = require("../rpc");
|
|
8
|
-
const networks_1 = require("../networks");
|
|
9
|
-
const signing_1 = require("../signing");
|
|
10
|
-
const recipient_1 = require("./recipient");
|
|
11
|
-
const user_1 = require("./user");
|
|
12
|
-
/**
|
|
13
|
-
* Top-level SDK client. Holds a live connection to the 4Mica core RPC and the
|
|
14
|
-
* on-chain Core4Mica contract. Obtain an instance via {@link Client.new}.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* const cfg = new ConfigBuilder().walletPrivateKey("0x...").build();
|
|
19
|
-
* const client = await Client.new(cfg);
|
|
20
|
-
* try {
|
|
21
|
-
* // client.user – payer-side operations
|
|
22
|
-
* // client.recipient – recipient-side operations
|
|
23
|
-
* } finally {
|
|
24
|
-
* await client.aclose();
|
|
25
|
-
* }
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
class Client {
|
|
29
|
-
/** Low-level RPC proxy to the 4Mica core service. */
|
|
30
|
-
rpc;
|
|
31
|
-
/** Chain and contract parameters fetched from the core service at startup. */
|
|
32
|
-
params;
|
|
33
|
-
/** viem-backed gateway for on-chain calls (deposit, remunerate, …). */
|
|
34
|
-
gateway;
|
|
35
|
-
/** 32-byte domain separator used to verify V1 BLS guarantee certificates. */
|
|
36
|
-
guaranteeDomain;
|
|
37
|
-
/** Payer-side operations: deposit, sign, withdraw. */
|
|
38
|
-
user;
|
|
39
|
-
/** Recipient-side operations: tabs, guarantees, remuneration. */
|
|
40
|
-
recipient;
|
|
41
|
-
/** Payment signing wrapper around the configured viem Account. */
|
|
42
|
-
signer;
|
|
43
|
-
authSession;
|
|
44
|
-
constructor(rpc, params, gateway, guaranteeDomain, signer, authSession) {
|
|
45
|
-
this.rpc = rpc;
|
|
46
|
-
this.params = params;
|
|
47
|
-
this.gateway = gateway;
|
|
48
|
-
this.guaranteeDomain = guaranteeDomain;
|
|
49
|
-
this.signer = signer;
|
|
50
|
-
this.authSession = authSession;
|
|
51
|
-
this.user = new user_1.UserClient(this);
|
|
52
|
-
this.recipient = new recipient_1.RecipientClient(this);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Create and fully initialise a Client.
|
|
56
|
-
*
|
|
57
|
-
* Fetches public parameters from the core service, validates that the
|
|
58
|
-
* Ethereum RPC is on the expected chain, and sets up SIWE auth if configured.
|
|
59
|
-
*
|
|
60
|
-
* @param cfg - Validated configuration produced by {@link ConfigBuilder.build}.
|
|
61
|
-
* @throws {@link ConfigError} if the configuration is invalid.
|
|
62
|
-
* @throws {@link RpcError} if the core service is unreachable.
|
|
63
|
-
* @throws {@link ContractError} if the Ethereum RPC returns the wrong chain ID.
|
|
64
|
-
*/
|
|
65
|
-
static async new(cfg) {
|
|
66
|
-
const rpc = new rpc_1.RpcProxy(cfg.rpcUrl, cfg.adminApiKey);
|
|
67
|
-
const params = await rpc.getPublicParams();
|
|
68
|
-
const gateway = await Client.buildGateway(cfg, params);
|
|
69
|
-
const guaranteeDomain = await gateway.getGuaranteeDomain();
|
|
70
|
-
const signer = new signing_1.PaymentSigner(cfg.signer);
|
|
71
|
-
const authEnabled = cfg.authUrl !== undefined || cfg.authRefreshMarginSecs !== undefined;
|
|
72
|
-
const authSession = cfg.bearerToken || !authEnabled
|
|
73
|
-
? undefined
|
|
74
|
-
: new auth_1.AuthSession({
|
|
75
|
-
authUrl: cfg.authUrl ?? cfg.rpcUrl,
|
|
76
|
-
signer: cfg.signer,
|
|
77
|
-
refreshMarginSecs: cfg.authRefreshMarginSecs ?? 60,
|
|
78
|
-
});
|
|
79
|
-
if (cfg.bearerToken) {
|
|
80
|
-
rpc.withBearerToken(cfg.bearerToken);
|
|
81
|
-
}
|
|
82
|
-
else if (authSession) {
|
|
83
|
-
rpc.withTokenProvider(() => authSession.accessToken());
|
|
84
|
-
}
|
|
85
|
-
return new Client(rpc, params, gateway, guaranteeDomain, signer, authSession);
|
|
86
|
-
}
|
|
87
|
-
static async buildGateway(cfg, params) {
|
|
88
|
-
const ethRpcUrl = cfg.ethereumHttpRpcUrl ??
|
|
89
|
-
params.ethereumHttpRpcUrl ??
|
|
90
|
-
(0, networks_1.resolvePublicRpcUrl)(`eip155:${params.chainId}`);
|
|
91
|
-
const contractAddress = cfg.contractAddress ?? params.contractAddress;
|
|
92
|
-
return contract_1.ContractGateway.create(ethRpcUrl, cfg.signer, contractAddress, params.chainId);
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Release client resources. Safe to call multiple times.
|
|
96
|
-
* Use in a `finally` block to ensure cleanup after use.
|
|
97
|
-
*/
|
|
98
|
-
async aclose() {
|
|
99
|
-
await this.rpc.aclose();
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Perform an explicit SIWE login and return the resulting tokens.
|
|
103
|
-
*
|
|
104
|
-
* Not required for normal operation — the first authenticated RPC call
|
|
105
|
-
* triggers auth automatically. Call this to pre-warm the session.
|
|
106
|
-
*
|
|
107
|
-
* @throws {@link AuthMissingConfigError} if auth was not enabled in the config.
|
|
108
|
-
*/
|
|
109
|
-
async login() {
|
|
110
|
-
if (!this.authSession) {
|
|
111
|
-
throw new errors_1.AuthMissingConfigError('auth is not enabled');
|
|
112
|
-
}
|
|
113
|
-
return this.authSession.login();
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
exports.Client = Client;
|
|
117
|
-
var user_2 = require("./user");
|
|
118
|
-
Object.defineProperty(exports, "UserClient", { enumerable: true, get: function () { return user_2.UserClient; } });
|
|
119
|
-
var recipient_2 = require("./recipient");
|
|
120
|
-
Object.defineProperty(exports, "RecipientClient", { enumerable: true, get: function () { return recipient_2.RecipientClient; } });
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { AssetBalanceInfo, BLSCert, CollateralEventInfo, GuaranteeInfo, PaymentGuaranteeClaims, PaymentGuaranteeRequestClaims, PaymentGuaranteeRequestClaimsV2, PendingRemunerationInfo, RecipientPaymentInfo, SigningScheme, TabInfo, TabPaymentStatus } from '../models';
|
|
2
|
-
import type { TxReceiptWaitOptions } from '../contract';
|
|
3
|
-
import type { Client } from './index';
|
|
4
|
-
/** Recipient-side operations: tab management, guarantee issuance, remuneration. */
|
|
5
|
-
export declare class RecipientClient {
|
|
6
|
-
private client;
|
|
7
|
-
constructor(client: Client);
|
|
8
|
-
private get recipientAddress();
|
|
9
|
-
get guaranteeDomain(): string;
|
|
10
|
-
/**
|
|
11
|
-
* Create a payment tab via the core RPC.
|
|
12
|
-
*
|
|
13
|
-
* @param userAddress - Address of the payer.
|
|
14
|
-
* @param recipientAddress - Address of the recipient.
|
|
15
|
-
* @param erc20Token - ERC20 token address for the tab, or `null`/`undefined` for ETH.
|
|
16
|
-
* @param ttl - Optional time-to-live in seconds.
|
|
17
|
-
* @param guaranteeVersion - Guarantee version for the version-scoped tab identity.
|
|
18
|
-
* @returns `{ tabId, assetAddress, nextReqId }` — the tab ID, the asset address as stored
|
|
19
|
-
* by the core (use this for all subsequent claims), and the first request ID to use.
|
|
20
|
-
* @throws {@link RpcError} if the request fails.
|
|
21
|
-
*/
|
|
22
|
-
createTab(userAddress: string, recipientAddress: string, erc20Token: string | undefined | null, ttl?: number | null, guaranteeVersion?: number): Promise<{
|
|
23
|
-
tabId: bigint;
|
|
24
|
-
assetAddress: string;
|
|
25
|
-
nextReqId: bigint;
|
|
26
|
-
}>;
|
|
27
|
-
/**
|
|
28
|
-
* Query the on-chain payment status of a tab.
|
|
29
|
-
*
|
|
30
|
-
* @param tabId - Tab identifier.
|
|
31
|
-
* @returns `{ paid, remunerated, asset }`.
|
|
32
|
-
*/
|
|
33
|
-
getTabPaymentStatus(tabId: number | bigint): Promise<TabPaymentStatus>;
|
|
34
|
-
/**
|
|
35
|
-
* Issue a BLS-signed payment guarantee certificate via the core RPC.
|
|
36
|
-
*
|
|
37
|
-
* The returned {@link BLSCert} can be stored and later passed to
|
|
38
|
-
* {@link remunerate} to claim the payment on-chain.
|
|
39
|
-
*
|
|
40
|
-
* @param claims - Signed payment claims (V1 or V2).
|
|
41
|
-
* @param signature - ECDSA signature hex string from the payer.
|
|
42
|
-
* @param scheme - Signing scheme used to produce the signature.
|
|
43
|
-
* @returns BLS certificate with ABI-encoded claims and BLS signature.
|
|
44
|
-
* @throws {@link RpcError} if the core service rejects the request.
|
|
45
|
-
*/
|
|
46
|
-
issuePaymentGuarantee(claims: PaymentGuaranteeRequestClaims | PaymentGuaranteeRequestClaimsV2, signature: string, scheme: SigningScheme): Promise<BLSCert>;
|
|
47
|
-
/**
|
|
48
|
-
* Verify and decode a BLS guarantee certificate.
|
|
49
|
-
*
|
|
50
|
-
* Decodes the ABI-encoded claims and validates the domain separator against
|
|
51
|
-
* the on-chain configuration. For V2 certificates, the active V2 domain is
|
|
52
|
-
* fetched from the contract and verified to be enabled.
|
|
53
|
-
*
|
|
54
|
-
* @param cert - BLS certificate (hex-encoded claims + hex-encoded signature).
|
|
55
|
-
* @returns Decoded {@link PaymentGuaranteeClaims}, including validation policy for V2.
|
|
56
|
-
* @throws {@link VerificationError} on domain mismatch, invalid length, or disabled version.
|
|
57
|
-
*/
|
|
58
|
-
verifyPaymentGuarantee(cert: BLSCert): Promise<PaymentGuaranteeClaims>;
|
|
59
|
-
/**
|
|
60
|
-
* Claim payment on-chain by submitting a verified BLS certificate.
|
|
61
|
-
*
|
|
62
|
-
* Verifies the certificate first (see {@link verifyPaymentGuarantee}), then
|
|
63
|
-
* converts the BLS signature into the G2 coordinate words expected by the
|
|
64
|
-
* contract and submits the `remunerate` transaction.
|
|
65
|
-
*
|
|
66
|
-
* Requires the optional `@noble/curves` package for BLS point decompression.
|
|
67
|
-
*
|
|
68
|
-
* @param cert - BLS certificate to settle.
|
|
69
|
-
* @param waitOptions - Optional timeout/polling overrides for receipt polling.
|
|
70
|
-
* @throws {@link VerificationError} if the certificate is invalid or `claims`/`signature`
|
|
71
|
-
* are not hex strings.
|
|
72
|
-
* @throws {@link ContractError} if the contract call fails.
|
|
73
|
-
*/
|
|
74
|
-
remunerate(cert: BLSCert, waitOptions?: TxReceiptWaitOptions): Promise<import("viem").TransactionReceipt>;
|
|
75
|
-
/** List all tabs for this recipient that have been settled on-chain. */
|
|
76
|
-
listSettledTabs(): Promise<TabInfo[]>;
|
|
77
|
-
/** List all tabs with outstanding (un-remunerated) guarantees for this recipient. */
|
|
78
|
-
listPendingRemunerations(): Promise<PendingRemunerationInfo[]>;
|
|
79
|
-
/**
|
|
80
|
-
* Fetch a single tab by ID.
|
|
81
|
-
*
|
|
82
|
-
* @param tabId - Tab identifier.
|
|
83
|
-
* @returns The tab, or `null` if not found.
|
|
84
|
-
*/
|
|
85
|
-
getTab(tabId: number | bigint): Promise<TabInfo | null>;
|
|
86
|
-
/**
|
|
87
|
-
* List all tabs belonging to this recipient.
|
|
88
|
-
*
|
|
89
|
-
* @param settlementStatuses - Optional filter on settlement status (e.g. `['PENDING']`).
|
|
90
|
-
*/
|
|
91
|
-
listRecipientTabs(settlementStatuses?: string[]): Promise<TabInfo[]>;
|
|
92
|
-
/**
|
|
93
|
-
* List all guarantee requests associated with a tab.
|
|
94
|
-
*
|
|
95
|
-
* @param tabId - Tab identifier.
|
|
96
|
-
*/
|
|
97
|
-
getTabGuarantees(tabId: number | bigint): Promise<GuaranteeInfo[]>;
|
|
98
|
-
/**
|
|
99
|
-
* Fetch the most recent guarantee for a tab.
|
|
100
|
-
*
|
|
101
|
-
* @param tabId - Tab identifier.
|
|
102
|
-
* @returns The latest {@link GuaranteeInfo}, or `null` if none exists.
|
|
103
|
-
*/
|
|
104
|
-
getLatestGuarantee(tabId: number | bigint): Promise<GuaranteeInfo | null>;
|
|
105
|
-
/**
|
|
106
|
-
* Fetch a specific guarantee by tab ID and request ID.
|
|
107
|
-
*
|
|
108
|
-
* @param tabId - Tab identifier.
|
|
109
|
-
* @param reqId - Guarantee request identifier.
|
|
110
|
-
* @returns The {@link GuaranteeInfo}, or `null` if not found.
|
|
111
|
-
*/
|
|
112
|
-
getGuarantee(tabId: number | bigint, reqId: number | bigint): Promise<GuaranteeInfo | null>;
|
|
113
|
-
/** List all on-chain payments received by this recipient. */
|
|
114
|
-
listRecipientPayments(): Promise<RecipientPaymentInfo[]>;
|
|
115
|
-
/**
|
|
116
|
-
* List collateral deposit/withdrawal events associated with a tab.
|
|
117
|
-
*
|
|
118
|
-
* @param tabId - Tab identifier.
|
|
119
|
-
*/
|
|
120
|
-
getCollateralEventsForTab(tabId: number | bigint): Promise<CollateralEventInfo[]>;
|
|
121
|
-
/**
|
|
122
|
-
* Fetch the collateral balance a user has locked for a specific asset.
|
|
123
|
-
*
|
|
124
|
-
* @param userAddress - Address of the payer.
|
|
125
|
-
* @param assetAddress - ERC20 token address, or zero address for ETH.
|
|
126
|
-
* @returns Balance info, or `null` if no record exists.
|
|
127
|
-
*/
|
|
128
|
-
getUserAssetBalance(userAddress: string, assetAddress: string): Promise<AssetBalanceInfo | null>;
|
|
129
|
-
}
|