@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/signing.js
DELETED
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PaymentSigner = exports.GUARANTEE_EIP712_TYPES_V2 = exports.GUARANTEE_EIP712_TYPES = exports.computeValidationRequestHash = exports.computeValidationSubjectHash = void 0;
|
|
4
|
-
exports.validateGuaranteeTypedData = validateGuaranteeTypedData;
|
|
5
|
-
exports.validateGuaranteeSigningContext = validateGuaranteeSigningContext;
|
|
6
|
-
exports.buildGuaranteeTypedData = buildGuaranteeTypedData;
|
|
7
|
-
exports.buildGuaranteeTypedDataV2 = buildGuaranteeTypedDataV2;
|
|
8
|
-
exports.encodeGuaranteeEip191V2 = encodeGuaranteeEip191V2;
|
|
9
|
-
exports.encodeGuaranteeEip191 = encodeGuaranteeEip191;
|
|
10
|
-
const viem_1 = require("viem");
|
|
11
|
-
var validation_1 = require("./validation");
|
|
12
|
-
Object.defineProperty(exports, "computeValidationSubjectHash", { enumerable: true, get: function () { return validation_1.computeValidationSubjectHash; } });
|
|
13
|
-
Object.defineProperty(exports, "computeValidationRequestHash", { enumerable: true, get: function () { return validation_1.computeValidationRequestHash; } });
|
|
14
|
-
const errors_1 = require("./errors");
|
|
15
|
-
const models_1 = require("./models");
|
|
16
|
-
const utils_1 = require("./utils");
|
|
17
|
-
const serde_1 = require("./serde");
|
|
18
|
-
exports.GUARANTEE_EIP712_TYPES = {
|
|
19
|
-
EIP712Domain: [
|
|
20
|
-
{ name: 'name', type: 'string' },
|
|
21
|
-
{ name: 'version', type: 'string' },
|
|
22
|
-
{ name: 'chainId', type: 'uint256' },
|
|
23
|
-
],
|
|
24
|
-
SolGuaranteeRequestClaimsV1: [
|
|
25
|
-
{ name: 'user', type: 'address' },
|
|
26
|
-
{ name: 'recipient', type: 'address' },
|
|
27
|
-
{ name: 'tabId', type: 'uint256' },
|
|
28
|
-
{ name: 'reqId', type: 'uint256' },
|
|
29
|
-
{ name: 'amount', type: 'uint256' },
|
|
30
|
-
{ name: 'asset', type: 'address' },
|
|
31
|
-
{ name: 'timestamp', type: 'uint64' },
|
|
32
|
-
],
|
|
33
|
-
};
|
|
34
|
-
exports.GUARANTEE_EIP712_TYPES_V2 = {
|
|
35
|
-
EIP712Domain: [
|
|
36
|
-
{ name: 'name', type: 'string' },
|
|
37
|
-
{ name: 'version', type: 'string' },
|
|
38
|
-
{ name: 'chainId', type: 'uint256' },
|
|
39
|
-
],
|
|
40
|
-
SolGuaranteeRequestClaimsV2: [
|
|
41
|
-
{ name: 'user', type: 'address' },
|
|
42
|
-
{ name: 'recipient', type: 'address' },
|
|
43
|
-
{ name: 'tabId', type: 'uint256' },
|
|
44
|
-
{ name: 'reqId', type: 'uint256' },
|
|
45
|
-
{ name: 'amount', type: 'uint256' },
|
|
46
|
-
{ name: 'asset', type: 'address' },
|
|
47
|
-
{ name: 'timestamp', type: 'uint64' },
|
|
48
|
-
{ name: 'validationRegistryAddress', type: 'address' },
|
|
49
|
-
{ name: 'validationRequestHash', type: 'bytes32' },
|
|
50
|
-
{ name: 'validationChainId', type: 'uint256' },
|
|
51
|
-
{ name: 'validatorAddress', type: 'address' },
|
|
52
|
-
{ name: 'validatorAgentId', type: 'uint256' },
|
|
53
|
-
{ name: 'minValidationScore', type: 'uint8' },
|
|
54
|
-
{ name: 'validationSubjectHash', type: 'bytes32' },
|
|
55
|
-
{ name: 'jobHash', type: 'bytes32' },
|
|
56
|
-
{ name: 'requiredValidationTag', type: 'string' },
|
|
57
|
-
],
|
|
58
|
-
};
|
|
59
|
-
const fieldsMatch = (actual, expected) => {
|
|
60
|
-
if (!Array.isArray(actual) || actual.length !== expected.length)
|
|
61
|
-
return false;
|
|
62
|
-
const norm = (list) => list.map((item) => `${item.name}:${item.type}`).sort();
|
|
63
|
-
const actualSet = new Set(norm(actual));
|
|
64
|
-
return norm(expected).every((key) => actualSet.has(key));
|
|
65
|
-
};
|
|
66
|
-
const parseChainId = (value) => {
|
|
67
|
-
if (typeof value === 'bigint')
|
|
68
|
-
return Number(value);
|
|
69
|
-
if (typeof value === 'number')
|
|
70
|
-
return value;
|
|
71
|
-
if (typeof value === 'string' && value.trim())
|
|
72
|
-
return Number(value);
|
|
73
|
-
return Number.NaN;
|
|
74
|
-
};
|
|
75
|
-
const ensureBigIntish = (value, label) => {
|
|
76
|
-
try {
|
|
77
|
-
BigInt(value);
|
|
78
|
-
}
|
|
79
|
-
catch {
|
|
80
|
-
throw new utils_1.ValidationError(`${label} must be a numeric value`);
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
function validateGuaranteeTypedData(payload, options = {}) {
|
|
84
|
-
if (!(0, serde_1.isRecord)(payload)) {
|
|
85
|
-
throw new utils_1.ValidationError('typed data payload is required');
|
|
86
|
-
}
|
|
87
|
-
if (!(0, serde_1.isRecord)(payload.domain)) {
|
|
88
|
-
throw new utils_1.ValidationError('domain is required');
|
|
89
|
-
}
|
|
90
|
-
if (!(0, serde_1.isRecord)(payload.types)) {
|
|
91
|
-
throw new utils_1.ValidationError('types are required');
|
|
92
|
-
}
|
|
93
|
-
if (!(0, serde_1.isRecord)(payload.message)) {
|
|
94
|
-
throw new utils_1.ValidationError('message is required');
|
|
95
|
-
}
|
|
96
|
-
const types = payload.types;
|
|
97
|
-
if (!fieldsMatch(types.SolGuaranteeRequestClaimsV1, exports.GUARANTEE_EIP712_TYPES.SolGuaranteeRequestClaimsV1)) {
|
|
98
|
-
throw new utils_1.ValidationError('Unexpected struct fields for SolGuaranteeRequestClaimsV1');
|
|
99
|
-
}
|
|
100
|
-
const message = payload.message;
|
|
101
|
-
const requiredFields = ['user', 'recipient', 'tabId', 'reqId', 'amount', 'asset', 'timestamp'];
|
|
102
|
-
if (requiredFields.some((field) => !(field in message))) {
|
|
103
|
-
throw new utils_1.ValidationError('message is missing required SolGuaranteeRequestClaimsV1 fields');
|
|
104
|
-
}
|
|
105
|
-
const user = (0, utils_1.normalizeAddress)(message.user);
|
|
106
|
-
const recipient = (0, utils_1.normalizeAddress)(message.recipient);
|
|
107
|
-
(0, utils_1.normalizeAddress)(message.asset);
|
|
108
|
-
if (options.expectedSigner) {
|
|
109
|
-
if ((0, utils_1.normalizeAddress)(options.expectedSigner) !== user) {
|
|
110
|
-
throw new utils_1.ValidationError('message.user must match signer address');
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
if (options.expectedRecipient) {
|
|
114
|
-
if ((0, utils_1.normalizeAddress)(options.expectedRecipient) !== recipient) {
|
|
115
|
-
throw new utils_1.ValidationError('message.recipient must match expected recipient');
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
ensureBigIntish(message.tabId, 'tabId');
|
|
119
|
-
ensureBigIntish(message.reqId, 'reqId');
|
|
120
|
-
ensureBigIntish(message.amount, 'amount');
|
|
121
|
-
ensureBigIntish(message.timestamp, 'timestamp');
|
|
122
|
-
if (options.expectedChainId !== undefined) {
|
|
123
|
-
const domainChainId = parseChainId(payload.domain.chainId);
|
|
124
|
-
if (!Number.isFinite(domainChainId)) {
|
|
125
|
-
throw new utils_1.ValidationError('domain.chainId is required for typed data signatures');
|
|
126
|
-
}
|
|
127
|
-
if (Number(domainChainId) !== Number(options.expectedChainId)) {
|
|
128
|
-
throw new utils_1.ValidationError(`domain.chainId mismatch; expected ${options.expectedChainId}, got ${domainChainId}`);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
function validateGuaranteeSigningContext(params, claims, options = {}) {
|
|
133
|
-
if (options.signerAddress) {
|
|
134
|
-
const signer = (0, utils_1.normalizeAddress)(options.signerAddress);
|
|
135
|
-
const user = (0, utils_1.normalizeAddress)(claims.userAddress);
|
|
136
|
-
if (signer !== user) {
|
|
137
|
-
throw new utils_1.ValidationError(`address mismatch: signer ${options.signerAddress} != claims.user_address ${claims.userAddress}`);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
if (options.signerChainId !== undefined) {
|
|
141
|
-
const signerChainId = Number(options.signerChainId);
|
|
142
|
-
if (!Number.isFinite(signerChainId)) {
|
|
143
|
-
throw new utils_1.ValidationError('signer chain id is invalid');
|
|
144
|
-
}
|
|
145
|
-
if (Number(params.chainId) !== signerChainId) {
|
|
146
|
-
throw new utils_1.ValidationError(`chain id mismatch: expected ${params.chainId}, got ${signerChainId}`);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
function buildGuaranteeTypedData(params, claims) {
|
|
151
|
-
return {
|
|
152
|
-
types: exports.GUARANTEE_EIP712_TYPES,
|
|
153
|
-
primaryType: 'SolGuaranteeRequestClaimsV1',
|
|
154
|
-
domain: {
|
|
155
|
-
name: params.eip712Name,
|
|
156
|
-
version: params.eip712Version,
|
|
157
|
-
chainId: params.chainId,
|
|
158
|
-
},
|
|
159
|
-
message: {
|
|
160
|
-
user: claims.userAddress,
|
|
161
|
-
recipient: claims.recipientAddress,
|
|
162
|
-
tabId: claims.tabId,
|
|
163
|
-
reqId: claims.reqId,
|
|
164
|
-
amount: claims.amount,
|
|
165
|
-
asset: claims.assetAddress,
|
|
166
|
-
timestamp: BigInt(claims.timestamp),
|
|
167
|
-
},
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
function buildGuaranteeTypedDataV2(params, claims) {
|
|
171
|
-
return {
|
|
172
|
-
types: exports.GUARANTEE_EIP712_TYPES_V2,
|
|
173
|
-
primaryType: 'SolGuaranteeRequestClaimsV2',
|
|
174
|
-
domain: {
|
|
175
|
-
name: params.eip712Name,
|
|
176
|
-
version: params.eip712Version,
|
|
177
|
-
chainId: params.chainId,
|
|
178
|
-
},
|
|
179
|
-
message: {
|
|
180
|
-
user: claims.userAddress,
|
|
181
|
-
recipient: claims.recipientAddress,
|
|
182
|
-
tabId: claims.tabId,
|
|
183
|
-
reqId: claims.reqId,
|
|
184
|
-
amount: claims.amount,
|
|
185
|
-
asset: claims.assetAddress,
|
|
186
|
-
timestamp: BigInt(claims.timestamp),
|
|
187
|
-
validationRegistryAddress: claims.validationRegistryAddress,
|
|
188
|
-
validationRequestHash: (0, utils_1.ensureHexPrefix)(claims.validationRequestHash),
|
|
189
|
-
validationChainId: BigInt(claims.validationChainId),
|
|
190
|
-
validatorAddress: claims.validatorAddress,
|
|
191
|
-
validatorAgentId: claims.validatorAgentId,
|
|
192
|
-
minValidationScore: claims.minValidationScore,
|
|
193
|
-
validationSubjectHash: (0, utils_1.ensureHexPrefix)(claims.validationSubjectHash),
|
|
194
|
-
jobHash: (0, utils_1.ensureHexPrefix)(claims.jobHash),
|
|
195
|
-
requiredValidationTag: claims.requiredValidationTag,
|
|
196
|
-
},
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
function encodeGuaranteeEip191V2(claims) {
|
|
200
|
-
return (0, viem_1.encodeAbiParameters)([
|
|
201
|
-
{ type: 'address' },
|
|
202
|
-
{ type: 'address' },
|
|
203
|
-
{ type: 'uint256' },
|
|
204
|
-
{ type: 'uint256' },
|
|
205
|
-
{ type: 'uint256' },
|
|
206
|
-
{ type: 'address' },
|
|
207
|
-
{ type: 'uint64' },
|
|
208
|
-
{ type: 'address' },
|
|
209
|
-
{ type: 'bytes32' },
|
|
210
|
-
{ type: 'uint256' },
|
|
211
|
-
{ type: 'address' },
|
|
212
|
-
{ type: 'uint256' },
|
|
213
|
-
{ type: 'uint8' },
|
|
214
|
-
{ type: 'bytes32' },
|
|
215
|
-
{ type: 'bytes32' },
|
|
216
|
-
{ type: 'string' },
|
|
217
|
-
], [
|
|
218
|
-
claims.userAddress,
|
|
219
|
-
claims.recipientAddress,
|
|
220
|
-
claims.tabId,
|
|
221
|
-
claims.reqId,
|
|
222
|
-
claims.amount,
|
|
223
|
-
claims.assetAddress,
|
|
224
|
-
BigInt(claims.timestamp),
|
|
225
|
-
claims.validationRegistryAddress,
|
|
226
|
-
(claims.validationRequestHash.startsWith('0x')
|
|
227
|
-
? claims.validationRequestHash
|
|
228
|
-
: `0x${claims.validationRequestHash}`),
|
|
229
|
-
BigInt(claims.validationChainId),
|
|
230
|
-
claims.validatorAddress,
|
|
231
|
-
claims.validatorAgentId,
|
|
232
|
-
claims.minValidationScore,
|
|
233
|
-
(claims.validationSubjectHash.startsWith('0x')
|
|
234
|
-
? claims.validationSubjectHash
|
|
235
|
-
: `0x${claims.validationSubjectHash}`),
|
|
236
|
-
(claims.jobHash.startsWith('0x') ? claims.jobHash : `0x${claims.jobHash}`),
|
|
237
|
-
claims.requiredValidationTag,
|
|
238
|
-
]);
|
|
239
|
-
}
|
|
240
|
-
function encodeGuaranteeEip191(claims) {
|
|
241
|
-
const payload = (0, viem_1.encodeAbiParameters)([
|
|
242
|
-
{ type: 'address' },
|
|
243
|
-
{ type: 'address' },
|
|
244
|
-
{ type: 'uint256' },
|
|
245
|
-
{ type: 'uint256' },
|
|
246
|
-
{ type: 'uint256' },
|
|
247
|
-
{ type: 'address' },
|
|
248
|
-
{ type: 'uint64' },
|
|
249
|
-
], [
|
|
250
|
-
claims.userAddress,
|
|
251
|
-
claims.recipientAddress,
|
|
252
|
-
claims.tabId,
|
|
253
|
-
claims.reqId,
|
|
254
|
-
claims.amount,
|
|
255
|
-
claims.assetAddress,
|
|
256
|
-
BigInt(claims.timestamp),
|
|
257
|
-
]);
|
|
258
|
-
return payload;
|
|
259
|
-
}
|
|
260
|
-
class PaymentSigner {
|
|
261
|
-
signer;
|
|
262
|
-
constructor(signer) {
|
|
263
|
-
this.signer = signer;
|
|
264
|
-
}
|
|
265
|
-
async signRequest(params, claims, scheme = models_1.SigningScheme.EIP712) {
|
|
266
|
-
try {
|
|
267
|
-
validateGuaranteeSigningContext(params, claims, { signerAddress: this.signer.address });
|
|
268
|
-
const isV2 = claims instanceof models_1.PaymentGuaranteeRequestClaimsV2;
|
|
269
|
-
if (scheme === models_1.SigningScheme.EIP712) {
|
|
270
|
-
if (!this.signer.signTypedData) {
|
|
271
|
-
throw new errors_1.SigningError('signTypedData is not supported for this account');
|
|
272
|
-
}
|
|
273
|
-
if (isV2) {
|
|
274
|
-
const typed = buildGuaranteeTypedDataV2(params, claims);
|
|
275
|
-
const signature = await this.signer.signTypedData({
|
|
276
|
-
domain: typed.domain,
|
|
277
|
-
types: { SolGuaranteeRequestClaimsV2: typed.types.SolGuaranteeRequestClaimsV2 },
|
|
278
|
-
primaryType: typed.primaryType,
|
|
279
|
-
message: typed.message,
|
|
280
|
-
});
|
|
281
|
-
return { signature, scheme };
|
|
282
|
-
}
|
|
283
|
-
const typed = buildGuaranteeTypedData(params, claims);
|
|
284
|
-
const signature = await this.signer.signTypedData({
|
|
285
|
-
domain: typed.domain,
|
|
286
|
-
types: { SolGuaranteeRequestClaimsV1: typed.types.SolGuaranteeRequestClaimsV1 },
|
|
287
|
-
primaryType: typed.primaryType,
|
|
288
|
-
message: typed.message,
|
|
289
|
-
});
|
|
290
|
-
return { signature, scheme };
|
|
291
|
-
}
|
|
292
|
-
if (scheme === models_1.SigningScheme.EIP191) {
|
|
293
|
-
if (!this.signer.signMessage) {
|
|
294
|
-
throw new errors_1.SigningError('signMessage is not supported for this account');
|
|
295
|
-
}
|
|
296
|
-
const message = isV2 ? encodeGuaranteeEip191V2(claims) : encodeGuaranteeEip191(claims);
|
|
297
|
-
const signature = await this.signer.signMessage({ message });
|
|
298
|
-
return { signature, scheme };
|
|
299
|
-
}
|
|
300
|
-
throw new errors_1.SigningError(`unsupported signing scheme: ${scheme}`);
|
|
301
|
-
}
|
|
302
|
-
catch (err) {
|
|
303
|
-
if (err instanceof utils_1.ValidationError) {
|
|
304
|
-
throw new errors_1.SigningError(err.message);
|
|
305
|
-
}
|
|
306
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
307
|
-
throw new errors_1.SigningError(message);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
exports.PaymentSigner = PaymentSigner;
|
package/dist/src/utils.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Hex } from 'viem';
|
|
2
|
-
export declare class ValidationError extends Error {
|
|
3
|
-
constructor(message: string);
|
|
4
|
-
}
|
|
5
|
-
export declare function validateUrl(raw: string): string;
|
|
6
|
-
export declare function normalizePrivateKey(raw: string): string;
|
|
7
|
-
export declare function normalizeAddress(raw: string): string;
|
|
8
|
-
export declare function ensureHexPrefix(value: string): Hex;
|
|
9
|
-
export declare function parseU256(value: number | bigint | string): bigint;
|
|
10
|
-
export declare function serializeU256(value: number | bigint | string): string;
|
|
11
|
-
export declare function hexFromBytes(bytes: Uint8Array): Hex;
|
package/dist/src/utils.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ValidationError = void 0;
|
|
4
|
-
exports.validateUrl = validateUrl;
|
|
5
|
-
exports.normalizePrivateKey = normalizePrivateKey;
|
|
6
|
-
exports.normalizeAddress = normalizeAddress;
|
|
7
|
-
exports.ensureHexPrefix = ensureHexPrefix;
|
|
8
|
-
exports.parseU256 = parseU256;
|
|
9
|
-
exports.serializeU256 = serializeU256;
|
|
10
|
-
exports.hexFromBytes = hexFromBytes;
|
|
11
|
-
const viem_1 = require("viem");
|
|
12
|
-
class ValidationError extends Error {
|
|
13
|
-
constructor(message) {
|
|
14
|
-
super(message);
|
|
15
|
-
this.name = 'ValidationError';
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.ValidationError = ValidationError;
|
|
19
|
-
function validateUrl(raw) {
|
|
20
|
-
let url;
|
|
21
|
-
try {
|
|
22
|
-
url = new URL(raw);
|
|
23
|
-
}
|
|
24
|
-
catch {
|
|
25
|
-
throw new ValidationError(`invalid URL: ${raw}`);
|
|
26
|
-
}
|
|
27
|
-
if (!url.protocol || !url.host) {
|
|
28
|
-
throw new ValidationError(`invalid URL: ${raw}`);
|
|
29
|
-
}
|
|
30
|
-
return raw;
|
|
31
|
-
}
|
|
32
|
-
function normalizePrivateKey(raw) {
|
|
33
|
-
const key = raw.startsWith('0x') ? raw.slice(2) : raw;
|
|
34
|
-
if (!/^[0-9a-fA-F]{64}$/.test(key)) {
|
|
35
|
-
throw new ValidationError('invalid private key (expected 32 byte hex)');
|
|
36
|
-
}
|
|
37
|
-
return `0x${key.toLowerCase()}`;
|
|
38
|
-
}
|
|
39
|
-
function normalizeAddress(raw) {
|
|
40
|
-
const candidate = String(raw);
|
|
41
|
-
if ((0, viem_1.isAddress)(candidate)) {
|
|
42
|
-
return (0, viem_1.getAddress)(candidate);
|
|
43
|
-
}
|
|
44
|
-
const lower = candidate.toLowerCase();
|
|
45
|
-
if ((0, viem_1.isAddress)(lower)) {
|
|
46
|
-
return (0, viem_1.getAddress)(lower);
|
|
47
|
-
}
|
|
48
|
-
throw new ValidationError(`invalid address: ${raw}`);
|
|
49
|
-
}
|
|
50
|
-
function ensureHexPrefix(value) {
|
|
51
|
-
return (value.startsWith('0x') ? value : `0x${value}`);
|
|
52
|
-
}
|
|
53
|
-
function parseNumericString(raw) {
|
|
54
|
-
const text = raw.trim();
|
|
55
|
-
const n = BigInt(text);
|
|
56
|
-
if (n < 0n) {
|
|
57
|
-
throw new ValidationError('u256 cannot be negative');
|
|
58
|
-
}
|
|
59
|
-
return n;
|
|
60
|
-
}
|
|
61
|
-
function parseU256(value) {
|
|
62
|
-
if (typeof value === 'number') {
|
|
63
|
-
if (!Number.isFinite(value)) {
|
|
64
|
-
throw new ValidationError('invalid integer');
|
|
65
|
-
}
|
|
66
|
-
if (!Number.isSafeInteger(value)) {
|
|
67
|
-
throw new ValidationError('integer value exceeds safe precision; use bigint or hex string for large values');
|
|
68
|
-
}
|
|
69
|
-
if (value < 0) {
|
|
70
|
-
throw new ValidationError('u256 cannot be negative');
|
|
71
|
-
}
|
|
72
|
-
return BigInt(value);
|
|
73
|
-
}
|
|
74
|
-
if (typeof value === 'bigint') {
|
|
75
|
-
if (value < 0) {
|
|
76
|
-
throw new ValidationError('u256 cannot be negative');
|
|
77
|
-
}
|
|
78
|
-
return value;
|
|
79
|
-
}
|
|
80
|
-
if (typeof value === 'string') {
|
|
81
|
-
return parseNumericString(value);
|
|
82
|
-
}
|
|
83
|
-
throw new ValidationError(`unsupported numeric type: ${typeof value}`);
|
|
84
|
-
}
|
|
85
|
-
function serializeU256(value) {
|
|
86
|
-
return `0x${parseU256(value).toString(16)}`;
|
|
87
|
-
}
|
|
88
|
-
function hexFromBytes(bytes) {
|
|
89
|
-
return `0x${Buffer.from(bytes).toString('hex')}`;
|
|
90
|
-
}
|
package/dist/src/validation.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { type Hex } from 'viem';
|
|
2
|
-
import { PaymentGuaranteeRequestClaims, PaymentGuaranteeRequestClaimsV2 } from './models';
|
|
3
|
-
/** Binding domain string used as a prefix when hashing validation subjects. */
|
|
4
|
-
export declare const VALIDATION_SUBJECT_BINDING_DOMAIN = "4MICA_VALIDATION_SUBJECT_V1";
|
|
5
|
-
/** Binding domain string used as a prefix when hashing validation requests. */
|
|
6
|
-
export declare const VALIDATION_REQUEST_BINDING_DOMAIN = "4MICA_VALIDATION_REQUEST_V2";
|
|
7
|
-
/**
|
|
8
|
-
* Compute the `validationSubjectHash` for a V2 payment guarantee request.
|
|
9
|
-
*
|
|
10
|
-
* Binds the payment subject (tab, user, recipient, amount, asset, timestamp) to
|
|
11
|
-
* a keccak256 hash prefixed by {@link VALIDATION_SUBJECT_BINDING_DOMAIN} so that
|
|
12
|
-
* on-chain validators can verify which payment the validation refers to.
|
|
13
|
-
*
|
|
14
|
-
* @param claims - V1 base claims (the subject fields are the same for V1 and V2).
|
|
15
|
-
* @returns 32-byte keccak256 hash as a `0x`-prefixed hex string.
|
|
16
|
-
*/
|
|
17
|
-
export declare function computeValidationSubjectHash(claims: PaymentGuaranteeRequestClaims): Hex;
|
|
18
|
-
/**
|
|
19
|
-
* Compute the `validationRequestHash` for a V2 payment guarantee request.
|
|
20
|
-
*
|
|
21
|
-
* Binds the full validation policy (chain, registry, validator, score threshold,
|
|
22
|
-
* subject hash, required tag, job hash) to a keccak256 hash prefixed by
|
|
23
|
-
* {@link VALIDATION_REQUEST_BINDING_DOMAIN}. This hash is included in the V2 claims
|
|
24
|
-
* that are BLS-signed by the core service, allowing on-chain verification that the
|
|
25
|
-
* validation policy was not tampered with.
|
|
26
|
-
*
|
|
27
|
-
* @param claims - V2 claims with all validation policy fields populated.
|
|
28
|
-
* The `validationSubjectHash` field must already be computed via
|
|
29
|
-
* {@link computeValidationSubjectHash} before calling this function.
|
|
30
|
-
* @returns 32-byte keccak256 hash as a `0x`-prefixed hex string.
|
|
31
|
-
*/
|
|
32
|
-
export declare function computeValidationRequestHash(claims: PaymentGuaranteeRequestClaimsV2): Hex;
|
package/dist/src/validation.js
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.VALIDATION_REQUEST_BINDING_DOMAIN = exports.VALIDATION_SUBJECT_BINDING_DOMAIN = void 0;
|
|
4
|
-
exports.computeValidationSubjectHash = computeValidationSubjectHash;
|
|
5
|
-
exports.computeValidationRequestHash = computeValidationRequestHash;
|
|
6
|
-
const viem_1 = require("viem");
|
|
7
|
-
const utils_1 = require("./utils");
|
|
8
|
-
/** Binding domain string used as a prefix when hashing validation subjects. */
|
|
9
|
-
exports.VALIDATION_SUBJECT_BINDING_DOMAIN = '4MICA_VALIDATION_SUBJECT_V1';
|
|
10
|
-
/** Binding domain string used as a prefix when hashing validation requests. */
|
|
11
|
-
exports.VALIDATION_REQUEST_BINDING_DOMAIN = '4MICA_VALIDATION_REQUEST_V2';
|
|
12
|
-
/**
|
|
13
|
-
* Compute the `validationSubjectHash` for a V2 payment guarantee request.
|
|
14
|
-
*
|
|
15
|
-
* Binds the payment subject (tab, user, recipient, amount, asset, timestamp) to
|
|
16
|
-
* a keccak256 hash prefixed by {@link VALIDATION_SUBJECT_BINDING_DOMAIN} so that
|
|
17
|
-
* on-chain validators can verify which payment the validation refers to.
|
|
18
|
-
*
|
|
19
|
-
* @param claims - V1 base claims (the subject fields are the same for V1 and V2).
|
|
20
|
-
* @returns 32-byte keccak256 hash as a `0x`-prefixed hex string.
|
|
21
|
-
*/
|
|
22
|
-
function computeValidationSubjectHash(claims) {
|
|
23
|
-
const bindingDomain = (0, viem_1.keccak256)(new TextEncoder().encode(exports.VALIDATION_SUBJECT_BINDING_DOMAIN));
|
|
24
|
-
const encoded = (0, viem_1.encodeAbiParameters)([
|
|
25
|
-
{ type: 'bytes32' },
|
|
26
|
-
{ type: 'uint256' },
|
|
27
|
-
{ type: 'uint256' },
|
|
28
|
-
{ type: 'address' },
|
|
29
|
-
{ type: 'address' },
|
|
30
|
-
{ type: 'uint256' },
|
|
31
|
-
{ type: 'address' },
|
|
32
|
-
{ type: 'uint64' },
|
|
33
|
-
], [
|
|
34
|
-
bindingDomain,
|
|
35
|
-
claims.tabId,
|
|
36
|
-
claims.reqId,
|
|
37
|
-
claims.userAddress,
|
|
38
|
-
claims.recipientAddress,
|
|
39
|
-
claims.amount,
|
|
40
|
-
claims.assetAddress,
|
|
41
|
-
BigInt(claims.timestamp),
|
|
42
|
-
]);
|
|
43
|
-
return (0, viem_1.keccak256)(encoded);
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Compute the `validationRequestHash` for a V2 payment guarantee request.
|
|
47
|
-
*
|
|
48
|
-
* Binds the full validation policy (chain, registry, validator, score threshold,
|
|
49
|
-
* subject hash, required tag, job hash) to a keccak256 hash prefixed by
|
|
50
|
-
* {@link VALIDATION_REQUEST_BINDING_DOMAIN}. This hash is included in the V2 claims
|
|
51
|
-
* that are BLS-signed by the core service, allowing on-chain verification that the
|
|
52
|
-
* validation policy was not tampered with.
|
|
53
|
-
*
|
|
54
|
-
* @param claims - V2 claims with all validation policy fields populated.
|
|
55
|
-
* The `validationSubjectHash` field must already be computed via
|
|
56
|
-
* {@link computeValidationSubjectHash} before calling this function.
|
|
57
|
-
* @returns 32-byte keccak256 hash as a `0x`-prefixed hex string.
|
|
58
|
-
*/
|
|
59
|
-
function computeValidationRequestHash(claims) {
|
|
60
|
-
const bindingDomain = (0, viem_1.keccak256)(new TextEncoder().encode(exports.VALIDATION_REQUEST_BINDING_DOMAIN));
|
|
61
|
-
const tagHash = (0, viem_1.keccak256)(new TextEncoder().encode(claims.requiredValidationTag));
|
|
62
|
-
const encoded = (0, viem_1.encodeAbiParameters)([
|
|
63
|
-
{ type: 'bytes32' },
|
|
64
|
-
{ type: 'uint256' },
|
|
65
|
-
{ type: 'address' },
|
|
66
|
-
{ type: 'address' },
|
|
67
|
-
{ type: 'uint256' },
|
|
68
|
-
{ type: 'bytes32' },
|
|
69
|
-
{ type: 'uint8' },
|
|
70
|
-
{ type: 'bytes32' },
|
|
71
|
-
{ type: 'bytes32' },
|
|
72
|
-
], [
|
|
73
|
-
bindingDomain,
|
|
74
|
-
BigInt(claims.validationChainId),
|
|
75
|
-
claims.validationRegistryAddress,
|
|
76
|
-
claims.validatorAddress,
|
|
77
|
-
claims.validatorAgentId,
|
|
78
|
-
(0, utils_1.ensureHexPrefix)(claims.validationSubjectHash),
|
|
79
|
-
claims.minValidationScore,
|
|
80
|
-
tagHash,
|
|
81
|
-
(0, utils_1.ensureHexPrefix)(claims.jobHash),
|
|
82
|
-
]);
|
|
83
|
-
return (0, viem_1.keccak256)(encoded);
|
|
84
|
-
}
|
package/dist/src/wallet/cdp.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { type Account } from 'viem';
|
|
2
|
-
export interface CdpAccountConfig {
|
|
3
|
-
/** CDP API key ID from the Coinbase Developer Platform dashboard. */
|
|
4
|
-
apiKeyId: string;
|
|
5
|
-
/** CDP API key secret from the Coinbase Developer Platform dashboard. */
|
|
6
|
-
apiKeySecret: string;
|
|
7
|
-
/** CDP wallet secret — required for account creation/signing operations. */
|
|
8
|
-
walletSecret: string;
|
|
9
|
-
/** Idempotency name — getOrCreateAccount always returns the same wallet for a given name. */
|
|
10
|
-
name: string;
|
|
11
|
-
}
|
|
12
|
-
/** Creates a viem Account backed by a Coinbase CDP MPC wallet (private key never leaves CDP). */
|
|
13
|
-
export declare function createCdpAccount(config: CdpAccountConfig): Promise<Account>;
|
package/dist/src/wallet/cdp.js
DELETED
|
@@ -1,78 +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.createCdpAccount = createCdpAccount;
|
|
37
|
-
const viem_1 = require("viem");
|
|
38
|
-
const accounts_1 = require("viem/accounts");
|
|
39
|
-
/** Creates a viem Account backed by a Coinbase CDP MPC wallet (private key never leaves CDP). */
|
|
40
|
-
async function createCdpAccount(config) {
|
|
41
|
-
const { CdpClient } = await Promise.resolve().then(() => __importStar(require('@coinbase/cdp-sdk')));
|
|
42
|
-
const cdp = new CdpClient({
|
|
43
|
-
apiKeyId: config.apiKeyId,
|
|
44
|
-
apiKeySecret: config.apiKeySecret,
|
|
45
|
-
walletSecret: config.walletSecret,
|
|
46
|
-
});
|
|
47
|
-
const evmAccount = await cdp.evm.getOrCreateAccount({ name: config.name });
|
|
48
|
-
const address = evmAccount.address;
|
|
49
|
-
async function signMessage({ message }) {
|
|
50
|
-
if (typeof message === 'string') {
|
|
51
|
-
// CDP's signMessage applies the EIP-191 prefix internally.
|
|
52
|
-
const result = await cdp.evm.signMessage({ address, message });
|
|
53
|
-
return result.signature;
|
|
54
|
-
}
|
|
55
|
-
const hash = (0, viem_1.hashMessage)(message);
|
|
56
|
-
const result = await cdp.evm.signHash({ address, hash });
|
|
57
|
-
return result.signature;
|
|
58
|
-
}
|
|
59
|
-
async function signTypedData(parameters) {
|
|
60
|
-
// Always hash locally with viem (guaranteed correct EIP-712 encoding including
|
|
61
|
-
// non-standard types like uint64) and sign only the hash via CDP.
|
|
62
|
-
// CDP's native signTypedData can silently produce a wrong hash for custom types,
|
|
63
|
-
// which would pass without error but fail signature verification on the core side.
|
|
64
|
-
const hash = (0, viem_1.hashTypedData)(parameters);
|
|
65
|
-
const result = await cdp.evm.signHash({ address, hash });
|
|
66
|
-
return result.signature;
|
|
67
|
-
}
|
|
68
|
-
async function signTransaction(transaction, options) {
|
|
69
|
-
const serializer = options?.serializer ?? viem_1.serializeTransaction;
|
|
70
|
-
const hash = (0, viem_1.keccak256)(serializer(transaction));
|
|
71
|
-
const result = await cdp.evm.signHash({ address, hash });
|
|
72
|
-
const { r, s, yParity } = (0, viem_1.hexToSignature)(result.signature);
|
|
73
|
-
return serializer(transaction, { r, s, yParity });
|
|
74
|
-
}
|
|
75
|
-
// viem's signTypedData generic cannot be satisfied by a hand-rolled adapter without casting — same pattern as hdKeyToAccount.
|
|
76
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
77
|
-
return (0, accounts_1.toAccount)({ address, signMessage, signTypedData, signTransaction });
|
|
78
|
-
}
|
package/dist/src/wallet/index.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createCdpAccount = void 0;
|
|
4
|
-
var cdp_1 = require("./cdp");
|
|
5
|
-
Object.defineProperty(exports, "createCdpAccount", { enumerable: true, get: function () { return cdp_1.createCdpAccount; } });
|