@rhea-finance/cross-chain-aggregation-dex 2.0.4 → 2.0.5

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/dist/index.js CHANGED
@@ -97,14 +97,33 @@ var ApiClient = class {
97
97
  retryableOperation: true,
98
98
  query: {
99
99
  sender: params.sender,
100
+ mode: params.mode,
100
101
  pageNumber: params.pageNumber,
101
102
  pageSize: params.pageSize
102
- }
103
+ },
104
+ ...params.mode === "confidential" && params.walletToken ? { authenticationToken: params.walletToken } : {}
105
+ });
106
+ }
107
+ createHistoryAuthChallenge(body, options = {}) {
108
+ return this.request("/api/swap/history/auth/challenge", "history", {
109
+ ...options,
110
+ method: "POST",
111
+ body
112
+ });
113
+ }
114
+ verifyHistoryAuthChallenge(body, options = {}) {
115
+ return this.request("/api/swap/history/auth/verify", "history", {
116
+ ...options,
117
+ method: "POST",
118
+ body
103
119
  });
104
120
  }
105
121
  async request(path, stage, options) {
106
122
  const url = this.buildUrl(path, options.query);
107
- const headers = await this.buildHeaders(options.idempotencyKey);
123
+ const headers = await this.buildHeaders(
124
+ options.idempotencyKey,
125
+ options.authenticationToken
126
+ );
108
127
  const retry = this.retryConfig();
109
128
  let attempt = 1;
110
129
  for (; ; ) {
@@ -256,12 +275,13 @@ var ApiClient = class {
256
275
  const suffix = search.toString();
257
276
  return suffix ? `${this.baseUrl}${path}?${suffix}` : `${this.baseUrl}${path}`;
258
277
  }
259
- async buildHeaders(idempotencyKey) {
278
+ async buildHeaders(idempotencyKey, authenticationToken) {
260
279
  const configured = typeof this.config.headers === "function" ? await this.config.headers() : this.config.headers ?? {};
261
280
  const token = this.config.getAccessToken ? await this.config.getAccessToken() : this.config.apiKey;
262
281
  return {
263
282
  "Content-Type": "application/json",
264
283
  ...token ? { Authorization: `Bearer ${token}` } : {},
284
+ ...authenticationToken ? { Authentication: `Bearer ${authenticationToken}` } : {},
265
285
  ...idempotencyKey ? { "Idempotency-Key": idempotencyKey } : {},
266
286
  ...configured
267
287
  };
@@ -943,6 +963,7 @@ function serializeQuoteRequest(request) {
943
963
  amountIn: request.amountIn,
944
964
  slippage: request.slippageBps,
945
965
  quoteWaitingTimeMs: request.quoteWaitingTimeMs ?? DEFAULT_QUOTE_WAITING_TIME_MS,
966
+ ...request.confidentiality ? { confidentiality: request.confidentiality } : {},
946
967
  sender: request.sender.trim(),
947
968
  ...request.recipient?.trim() ? { recipient: request.recipient.trim() } : {}
948
969
  };
@@ -1215,6 +1236,112 @@ function normalizeTimestamp(value) {
1215
1236
  return Number.isNaN(date.getTime()) ? void 0 : date.toISOString();
1216
1237
  }
1217
1238
 
1239
+ // src/mca/collateral.ts
1240
+ function resolveMcaWithdrawPolicy(input) {
1241
+ const decreaseCollateral = resolveMcaRequiredCollateralDecrease({
1242
+ amountBurrow: input.amountBurrow,
1243
+ suppliedBalance: input.suppliedBalance
1244
+ });
1245
+ const available = parseDecimal(input.availableBalance, "availableBalance");
1246
+ const amount = parseDecimal(input.amountIn, "amountIn");
1247
+ return {
1248
+ ...decreaseCollateral,
1249
+ withdrawAll: input.isMax || available.digits > 0n && isAtLeastWithdrawAllThreshold(amount, available)
1250
+ };
1251
+ }
1252
+ function resolveMcaRequiredCollateralDecrease(input) {
1253
+ const amount = parseBurrowDecimal(input.amountBurrow, "amountBurrow");
1254
+ const supplied = parseBurrowDecimal(
1255
+ input.suppliedBalance,
1256
+ "suppliedBalance"
1257
+ );
1258
+ const [amountScaled, suppliedScaled] = alignScale(amount, supplied);
1259
+ const decreaseScaled = amountScaled > suppliedScaled ? amountScaled - suppliedScaled : 0n;
1260
+ return resolveParsedMcaDecreaseCollateral({
1261
+ digits: decreaseScaled,
1262
+ scale: Math.max(amount.scale, supplied.scale)
1263
+ });
1264
+ }
1265
+ function resolveMcaDecreaseCollateral(decreaseAmountBurrow, field = "decreaseAmountBurrow") {
1266
+ const parsed = parseBurrowDecimal(decreaseAmountBurrow, field);
1267
+ return resolveParsedMcaDecreaseCollateral(parsed);
1268
+ }
1269
+ function resolveParsedMcaDecreaseCollateral(parsed) {
1270
+ const needDecrease = parsed.digits > 0n;
1271
+ return {
1272
+ needDecrease,
1273
+ decreaseAmountBurrow: needDecrease ? formatDecimal(parsed) : "0"
1274
+ };
1275
+ }
1276
+ function isAtLeastWithdrawAllThreshold(amount, available) {
1277
+ const [amountScaled, availableScaled] = alignScale(amount, available);
1278
+ return amountScaled * 1000000n >= availableScaled * 999999n;
1279
+ }
1280
+ function alignScale(a, b) {
1281
+ const scale = Math.max(a.scale, b.scale);
1282
+ return [
1283
+ a.digits * pow10(scale - a.scale),
1284
+ b.digits * pow10(scale - b.scale)
1285
+ ];
1286
+ }
1287
+ function parseDecimal(value, field) {
1288
+ const trimmed = value.trim();
1289
+ if (!/^(?:0|[1-9]\d*)(?:\.\d+)?$/.test(trimmed)) {
1290
+ throw new SwapSdkError(
1291
+ "INVALID_REQUEST",
1292
+ "quote",
1293
+ `${field} must be a non-negative decimal string`
1294
+ );
1295
+ }
1296
+ const [integer = "0", fraction = ""] = trimmed.split(".");
1297
+ return {
1298
+ digits: BigInt(`${integer}${fraction}`),
1299
+ scale: fraction.length
1300
+ };
1301
+ }
1302
+ function parseBurrowDecimal(value, field) {
1303
+ const trimmed = value.trim();
1304
+ const match = /^(?:([0-9]+)(?:\.([0-9]*))?|\.([0-9]+))(?:[eE]([+-]?[0-9]+))?$/.exec(
1305
+ trimmed
1306
+ );
1307
+ if (!match) {
1308
+ throw new SwapSdkError(
1309
+ "INVALID_REQUEST",
1310
+ "quote",
1311
+ `${field} must be a non-negative decimal string`
1312
+ );
1313
+ }
1314
+ const integer = match[1] ?? "0";
1315
+ const fraction = match[2] ?? match[3] ?? "";
1316
+ const exponent = Number(match[4] ?? "0");
1317
+ if (!Number.isSafeInteger(exponent) || Math.abs(exponent) > 1e5) {
1318
+ throw new SwapSdkError(
1319
+ "INVALID_REQUEST",
1320
+ "quote",
1321
+ `${field} exponent is out of range`
1322
+ );
1323
+ }
1324
+ let digits = BigInt(`${integer}${fraction}` || "0");
1325
+ let scale = fraction.length - exponent;
1326
+ if (scale < 0) {
1327
+ digits *= pow10(-scale);
1328
+ scale = 0;
1329
+ }
1330
+ return { digits, scale };
1331
+ }
1332
+ function formatDecimal(value) {
1333
+ if (value.digits === 0n) return "0";
1334
+ if (value.scale === 0) return value.digits.toString();
1335
+ const padded = value.digits.toString().padStart(value.scale + 1, "0");
1336
+ const splitAt = padded.length - value.scale;
1337
+ const integer = padded.slice(0, splitAt);
1338
+ const fraction = padded.slice(splitAt).replace(/0+$/, "");
1339
+ return fraction ? `${integer}.${fraction}` : integer;
1340
+ }
1341
+ function pow10(exponent) {
1342
+ return 10n ** BigInt(exponent);
1343
+ }
1344
+
1218
1345
  // src/mca/quote.ts
1219
1346
  function serializeMcaQuoteRequest(request, signer) {
1220
1347
  const mcaAccountId = request.mcaAccountId.trim();
@@ -1230,13 +1357,10 @@ function serializeMcaQuoteRequest(request, signer) {
1230
1357
  if (!identityKey) {
1231
1358
  throw invalidRequest("signer identityKey is required");
1232
1359
  }
1233
- if (request.flow === "withdraw" && !/^(?:0|[1-9]\d*)(?:\.\d+)?$/.test(
1234
- request.collateral.decreaseAmountBurrow.trim()
1235
- )) {
1236
- throw invalidRequest(
1237
- "collateral.decreaseAmountBurrow must be a non-negative decimal string"
1238
- );
1239
- }
1360
+ const decreaseCollateral = request.flow === "withdraw" ? resolveMcaDecreaseCollateral(
1361
+ request.collateral.decreaseAmountBurrow,
1362
+ "collateral.decreaseAmountBurrow"
1363
+ ) : void 0;
1240
1364
  const mca = {
1241
1365
  flow: request.flow,
1242
1366
  mcaAccountId,
@@ -1245,8 +1369,8 @@ function serializeMcaQuoteRequest(request, signer) {
1245
1369
  identityKey
1246
1370
  },
1247
1371
  ...request.flow === "deposit" ? { useAsCollateral: request.collateral.useAsCollateral } : {
1248
- needDecreaseCollateral: request.collateral.needDecrease,
1249
- decreaseCollateralAmountBurrow: request.collateral.decreaseAmountBurrow,
1372
+ needDecreaseCollateral: decreaseCollateral.needDecrease,
1373
+ decreaseCollateralAmountBurrow: decreaseCollateral.decreaseAmountBurrow,
1250
1374
  ...request.collateral.withdrawAll ? { withdrawAll: true } : {}
1251
1375
  },
1252
1376
  ...request.recipientMsgSignatures ? { recipientMsgSignatures: [...request.recipientMsgSignatures] } : {},
@@ -1901,7 +2025,8 @@ var McaSwapService = class {
1901
2025
  is_cross_chain: true,
1902
2026
  tx_type: "mca-withdraw-relayer",
1903
2027
  multi_addr: input.quote.mcaAccountId,
1904
- swapId: orderId
2028
+ swapId: orderId,
2029
+ ...request.confidentiality ? { confidentiality: request.confidentiality } : {}
1905
2030
  };
1906
2031
  this.relayerReports.set(executionId, reportRequest);
1907
2032
  const result = {
@@ -2298,10 +2423,30 @@ var SwapClient = class {
2298
2423
  getHistoryRaw(params, options = {}) {
2299
2424
  return this.api.getHistory(params, options);
2300
2425
  }
2426
+ createHistoryAuthChallenge(request, options = {}) {
2427
+ return this.api.createHistoryAuthChallenge(request, options);
2428
+ }
2429
+ verifyHistoryAuthChallenge(request, options = {}) {
2430
+ return this.api.verifyHistoryAuthChallenge(request, options);
2431
+ }
2432
+ async authorizeConfidentialHistory(request, signChallenge, options = {}) {
2433
+ const challenge = await this.createHistoryAuthChallenge(request, options);
2434
+ assertHistoryChallengeMatchesRequest(challenge, request);
2435
+ const proof = await signChallenge(challenge);
2436
+ const verifyRequest = {
2437
+ challengeId: challenge.challengeId,
2438
+ proof
2439
+ };
2440
+ const token = await this.verifyHistoryAuthChallenge(verifyRequest, options);
2441
+ assertHistoryTokenMatchesChallenge(token, challenge);
2442
+ return token;
2443
+ }
2301
2444
  async getHistory(request, options = {}) {
2302
2445
  const raw = await this.api.getHistory(
2303
2446
  {
2304
2447
  sender: request.sender,
2448
+ ...request.mode ? { mode: request.mode } : {},
2449
+ ...request.walletToken ? { walletToken: request.walletToken } : {},
2305
2450
  ...request.page !== void 0 ? { pageNumber: request.page } : {},
2306
2451
  ...request.pageSize !== void 0 ? { pageSize: request.pageSize } : {}
2307
2452
  },
@@ -2352,10 +2497,39 @@ var SwapClient = class {
2352
2497
  router: build.router,
2353
2498
  tx_type: reportContext?.txType ?? (build.isCrossChain ? "cross-chain" : "same-chain"),
2354
2499
  ...reportContext?.multiAddr ? { multi_addr: reportContext.multiAddr } : {},
2355
- ...reportContext?.swapId ?? result.orderId ? { swapId: reportContext?.swapId ?? result.orderId } : {}
2500
+ ...reportContext?.swapId ?? result.orderId ? { swapId: reportContext?.swapId ?? result.orderId } : {},
2501
+ ...request.confidentiality ? { confidentiality: request.confidentiality } : {}
2356
2502
  };
2357
2503
  }
2358
2504
  };
2505
+ function assertHistoryChallengeMatchesRequest(challenge, request) {
2506
+ const expectedMca = request.mcaAccountId?.trim();
2507
+ const principalMatches = expectedMca ? challenge.principalType === "mca" && challenge.mcaAccountId?.toLowerCase() === expectedMca.toLowerCase() : challenge.principalType === "wallet" && !challenge.mcaAccountId;
2508
+ const identityMatches = request.identityKey ? normalizeHistoryIdentity(challenge.chainFamily, challenge.identityKey) === normalizeHistoryIdentity(challenge.chainFamily, request.identityKey) : true;
2509
+ if (challenge.chainFamily !== request.chainFamily || challenge.chainId !== request.chainId || normalizeHistoryAddress(challenge.chainFamily, challenge.walletAddress) !== normalizeHistoryAddress(request.chainFamily, request.walletAddress) || !identityMatches || !principalMatches || !challenge.queryAddress) {
2510
+ throw new SwapSdkError(
2511
+ "INVALID_API_RESPONSE",
2512
+ "history",
2513
+ "Confidential history challenge does not match the requested wallet or MCA"
2514
+ );
2515
+ }
2516
+ }
2517
+ function assertHistoryTokenMatchesChallenge(token, challenge) {
2518
+ const principalMatches = token.principalType === challenge.principalType && (challenge.principalType === "mca" ? token.mcaAccountId?.toLowerCase() === challenge.mcaAccountId?.toLowerCase() : !token.mcaAccountId);
2519
+ if (!principalMatches || token.queryAddress !== challenge.queryAddress) {
2520
+ throw new SwapSdkError(
2521
+ "INVALID_API_RESPONSE",
2522
+ "history",
2523
+ "Confidential history authorization returned a different principal"
2524
+ );
2525
+ }
2526
+ }
2527
+ function normalizeHistoryAddress(chain, value) {
2528
+ return chain === "evm" || chain === "aptos" || chain === "sui" ? value.toLowerCase() : value;
2529
+ }
2530
+ function normalizeHistoryIdentity(chain, value) {
2531
+ return chain === "evm" || chain === "aptos" || chain === "sui" || chain === "btc" || chain === "zcash" ? value.toLowerCase().replace(/^0x/, "") : value;
2532
+ }
2359
2533
  function isMcaQuoteRequest(request) {
2360
2534
  if (!("flow" in request) || !("mcaAccountId" in request)) return false;
2361
2535
  const flow = Reflect.get(request, "flow");
@@ -2402,48 +2576,6 @@ function delay(ms, signal) {
2402
2576
  });
2403
2577
  }
2404
2578
 
2405
- // src/mca/collateral.ts
2406
- function resolveMcaWithdrawPolicy(input) {
2407
- const collateral = parseDecimal(input.collateralBalance, "collateralBalance");
2408
- const available = parseDecimal(input.availableBalance, "availableBalance");
2409
- const amount = parseDecimal(input.amountIn, "amountIn");
2410
- const needDecrease = collateral.digits > 0n;
2411
- return {
2412
- needDecrease,
2413
- decreaseAmountBurrow: needDecrease ? input.collateralBalance.trim() : "0",
2414
- withdrawAll: input.isMax || available.digits > 0n && isAtLeastWithdrawAllThreshold(amount, available)
2415
- };
2416
- }
2417
- function isAtLeastWithdrawAllThreshold(amount, available) {
2418
- const [amountScaled, availableScaled] = alignScale(amount, available);
2419
- return amountScaled * 1000000n >= availableScaled * 999999n;
2420
- }
2421
- function alignScale(a, b) {
2422
- const scale = Math.max(a.scale, b.scale);
2423
- return [
2424
- a.digits * pow10(scale - a.scale),
2425
- b.digits * pow10(scale - b.scale)
2426
- ];
2427
- }
2428
- function parseDecimal(value, field) {
2429
- const trimmed = value.trim();
2430
- if (!/^(?:0|[1-9]\d*)(?:\.\d+)?$/.test(trimmed)) {
2431
- throw new SwapSdkError(
2432
- "INVALID_REQUEST",
2433
- "quote",
2434
- `${field} must be a non-negative decimal string`
2435
- );
2436
- }
2437
- const [integer = "0", fraction = ""] = trimmed.split(".");
2438
- return {
2439
- digits: BigInt(`${integer}${fraction}`),
2440
- scale: fraction.length
2441
- };
2442
- }
2443
- function pow10(exponent) {
2444
- return 10n ** BigInt(exponent);
2445
- }
2446
-
2447
2579
  exports.ApiClient = ApiClient;
2448
2580
  exports.DEFAULT_MCA_SIGNER_PRIORITY = DEFAULT_MCA_SIGNER_PRIORITY;
2449
2581
  exports.ExecutorRegistry = ExecutorRegistry;
@@ -2468,6 +2600,8 @@ exports.normalizeMcaQuote = normalizeMcaQuote;
2468
2600
  exports.normalizeOrderStatus = normalizeOrderStatus;
2469
2601
  exports.normalizeQuote = normalizeQuote;
2470
2602
  exports.parseUnits = parseUnits;
2603
+ exports.resolveMcaDecreaseCollateral = resolveMcaDecreaseCollateral;
2604
+ exports.resolveMcaRequiredCollateralDecrease = resolveMcaRequiredCollateralDecrease;
2471
2605
  exports.resolveMcaWithdrawPolicy = resolveMcaWithdrawPolicy;
2472
2606
  exports.selectMcaSigner = selectMcaSigner;
2473
2607
  exports.serializeMcaQuoteRequest = serializeMcaQuoteRequest;