@sip-protocol/sdk 0.2.6 → 0.2.7
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/browser.js +117 -13
- package/dist/browser.mjs +1 -1
- package/dist/chunk-5BAS4D44.mjs +10283 -0
- package/dist/index.js +117 -13
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
- package/src/adapters/near-intents.ts +27 -1
- package/src/validation.ts +126 -0
package/dist/browser.js
CHANGED
|
@@ -645,6 +645,91 @@ function validateScalar(value, field) {
|
|
|
645
645
|
);
|
|
646
646
|
}
|
|
647
647
|
}
|
|
648
|
+
function isValidEvmAddress(address) {
|
|
649
|
+
if (typeof address !== "string") return false;
|
|
650
|
+
return /^0x[0-9a-fA-F]{40}$/.test(address);
|
|
651
|
+
}
|
|
652
|
+
function isValidSolanaAddressFormat(address) {
|
|
653
|
+
if (typeof address !== "string") return false;
|
|
654
|
+
return /^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(address);
|
|
655
|
+
}
|
|
656
|
+
function isValidNearAddressFormat(address) {
|
|
657
|
+
if (typeof address !== "string") return false;
|
|
658
|
+
if (/^[0-9a-f]{64}$/.test(address)) return true;
|
|
659
|
+
if (address.length < 2 || address.length > 64) return false;
|
|
660
|
+
if (!/^[a-z0-9]([a-z0-9._-]*[a-z0-9])?$/.test(address)) return false;
|
|
661
|
+
if (address.includes("..")) return false;
|
|
662
|
+
return true;
|
|
663
|
+
}
|
|
664
|
+
function getChainAddressType(chain) {
|
|
665
|
+
switch (chain) {
|
|
666
|
+
case "ethereum":
|
|
667
|
+
case "polygon":
|
|
668
|
+
case "arbitrum":
|
|
669
|
+
case "optimism":
|
|
670
|
+
case "base":
|
|
671
|
+
return "evm";
|
|
672
|
+
case "solana":
|
|
673
|
+
return "solana";
|
|
674
|
+
case "near":
|
|
675
|
+
return "near";
|
|
676
|
+
case "zcash":
|
|
677
|
+
return "zcash";
|
|
678
|
+
default:
|
|
679
|
+
return "unknown";
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
function validateAddressForChain(address, chain, field = "address") {
|
|
683
|
+
const addressType = getChainAddressType(chain);
|
|
684
|
+
switch (addressType) {
|
|
685
|
+
case "evm":
|
|
686
|
+
if (!isValidEvmAddress(address)) {
|
|
687
|
+
throw new ValidationError(
|
|
688
|
+
`Invalid address format for ${chain}. Expected EVM address (0x + 40 hex chars), got: ${address.slice(0, 20)}...`,
|
|
689
|
+
field,
|
|
690
|
+
{ chain, expectedFormat: "0x...", receivedFormat: address.startsWith("0x") ? "hex but wrong length" : "not hex" }
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
break;
|
|
694
|
+
case "solana":
|
|
695
|
+
if (!isValidSolanaAddressFormat(address)) {
|
|
696
|
+
throw new ValidationError(
|
|
697
|
+
`Invalid address format for ${chain}. Expected Solana address (base58, 32-44 chars), got: ${address.slice(0, 20)}...`,
|
|
698
|
+
field,
|
|
699
|
+
{ chain, expectedFormat: "base58", receivedFormat: address.startsWith("0x") ? "looks like EVM" : "unknown" }
|
|
700
|
+
);
|
|
701
|
+
}
|
|
702
|
+
break;
|
|
703
|
+
case "near":
|
|
704
|
+
if (!isValidNearAddressFormat(address)) {
|
|
705
|
+
throw new ValidationError(
|
|
706
|
+
`Invalid address format for ${chain}. Expected NEAR account ID (named or implicit), got: ${address.slice(0, 20)}...`,
|
|
707
|
+
field,
|
|
708
|
+
{ chain, expectedFormat: "alice.near or 64 hex chars" }
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
break;
|
|
712
|
+
case "zcash":
|
|
713
|
+
if (!address || address.length === 0) {
|
|
714
|
+
throw new ValidationError(
|
|
715
|
+
`Invalid address format for ${chain}. Expected Zcash address.`,
|
|
716
|
+
field,
|
|
717
|
+
{ chain }
|
|
718
|
+
);
|
|
719
|
+
}
|
|
720
|
+
break;
|
|
721
|
+
default:
|
|
722
|
+
break;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
function isAddressValidForChain(address, chain) {
|
|
726
|
+
try {
|
|
727
|
+
validateAddressForChain(address, chain);
|
|
728
|
+
return true;
|
|
729
|
+
} catch {
|
|
730
|
+
return false;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
648
733
|
|
|
649
734
|
// src/secure-memory.ts
|
|
650
735
|
var import_utils = require("@noble/hashes/utils");
|
|
@@ -2384,8 +2469,10 @@ var DEFAULT_ASSET_MAPPINGS = {
|
|
|
2384
2469
|
"arbitrum:ETH": "nep141:arb.omft.near",
|
|
2385
2470
|
// Base assets
|
|
2386
2471
|
"base:ETH": "nep141:base.omft.near",
|
|
2387
|
-
// Polygon assets
|
|
2388
|
-
"polygon:MATIC": "nep141:matic.omft.near"
|
|
2472
|
+
// Polygon assets (support both MATIC and POL symbols)
|
|
2473
|
+
"polygon:MATIC": "nep141:matic.omft.near",
|
|
2474
|
+
"polygon:POL": "nep141:matic.omft.near"
|
|
2475
|
+
// POL is the rebranded MATIC
|
|
2389
2476
|
};
|
|
2390
2477
|
var CHAIN_BLOCKCHAIN_MAP = {
|
|
2391
2478
|
near: "near",
|
|
@@ -2433,6 +2520,23 @@ var NEARIntentsAdapter = class {
|
|
|
2433
2520
|
*/
|
|
2434
2521
|
async prepareSwap(request, recipientMetaAddress, senderAddress) {
|
|
2435
2522
|
this.validateRequest(request);
|
|
2523
|
+
const inputChain = request.inputAsset.chain;
|
|
2524
|
+
if (senderAddress) {
|
|
2525
|
+
if (!isAddressValidForChain(senderAddress, inputChain)) {
|
|
2526
|
+
const inputChainType = getChainAddressType(inputChain);
|
|
2527
|
+
const senderFormat = senderAddress.startsWith("0x") ? "EVM" : /^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(senderAddress) ? "Solana" : /^[0-9a-f]{64}$/.test(senderAddress) || /^[a-z0-9._-]+$/.test(senderAddress) ? "NEAR" : "unknown";
|
|
2528
|
+
throw new ValidationError(
|
|
2529
|
+
`Wallet address format doesn't match input chain. You're swapping FROM ${inputChain} (${inputChainType} format) but your connected wallet uses ${senderFormat} format. Please connect a wallet that matches the source chain (${inputChain}).`,
|
|
2530
|
+
"senderAddress",
|
|
2531
|
+
{
|
|
2532
|
+
inputChain,
|
|
2533
|
+
expectedFormat: inputChainType,
|
|
2534
|
+
receivedFormat: senderFormat,
|
|
2535
|
+
hint: `For ${inputChain} swaps, connect a ${inputChainType === "evm" ? "MetaMask or EVM" : inputChainType} wallet.`
|
|
2536
|
+
}
|
|
2537
|
+
);
|
|
2538
|
+
}
|
|
2539
|
+
}
|
|
2436
2540
|
let recipientAddress;
|
|
2437
2541
|
let refundAddress = senderAddress;
|
|
2438
2542
|
let stealthData;
|
|
@@ -2497,22 +2601,22 @@ var NEARIntentsAdapter = class {
|
|
|
2497
2601
|
);
|
|
2498
2602
|
}
|
|
2499
2603
|
if (!senderAddress) {
|
|
2500
|
-
const
|
|
2501
|
-
const inputChainType = CHAIN_BLOCKCHAIN_MAP[
|
|
2502
|
-
if (isEd25519Chain(
|
|
2604
|
+
const inputChain2 = request.inputAsset.chain;
|
|
2605
|
+
const inputChainType = CHAIN_BLOCKCHAIN_MAP[inputChain2];
|
|
2606
|
+
if (isEd25519Chain(inputChain2)) {
|
|
2503
2607
|
const inputKeyBytes = (metaAddr.spendingKey.length - 2) / 2;
|
|
2504
2608
|
if (inputKeyBytes === 32) {
|
|
2505
2609
|
const refundStealth = generateEd25519StealthAddress(metaAddr);
|
|
2506
|
-
if (
|
|
2610
|
+
if (inputChain2 === "solana") {
|
|
2507
2611
|
refundAddress = ed25519PublicKeyToSolanaAddress(refundStealth.stealthAddress.address);
|
|
2508
|
-
} else if (
|
|
2612
|
+
} else if (inputChain2 === "near") {
|
|
2509
2613
|
refundAddress = ed25519PublicKeyToNearAddress(refundStealth.stealthAddress.address);
|
|
2510
2614
|
}
|
|
2511
2615
|
} else {
|
|
2512
2616
|
throw new ValidationError(
|
|
2513
|
-
`Cross-curve refunds not supported: input chain ${
|
|
2617
|
+
`Cross-curve refunds not supported: input chain ${inputChain2} requires ed25519 but meta-address uses secp256k1. Please provide a senderAddress for refunds, or use matching curves for input/output chains.`,
|
|
2514
2618
|
"senderAddress",
|
|
2515
|
-
{ inputChain, inputChainType, metaAddressCurve: "secp256k1" }
|
|
2619
|
+
{ inputChain: inputChain2, inputChainType, metaAddressCurve: "secp256k1" }
|
|
2516
2620
|
);
|
|
2517
2621
|
}
|
|
2518
2622
|
} else if (inputChainType === "evm") {
|
|
@@ -2522,16 +2626,16 @@ var NEARIntentsAdapter = class {
|
|
|
2522
2626
|
refundAddress = publicKeyToEthAddress(refundStealth.stealthAddress.address);
|
|
2523
2627
|
} else {
|
|
2524
2628
|
throw new ValidationError(
|
|
2525
|
-
`Cross-curve refunds not supported: input chain ${
|
|
2629
|
+
`Cross-curve refunds not supported: input chain ${inputChain2} requires secp256k1 but meta-address uses ed25519. Please provide a senderAddress for refunds, or use matching curves for input/output chains.`,
|
|
2526
2630
|
"senderAddress",
|
|
2527
|
-
{ inputChain, inputChainType, metaAddressCurve: "ed25519" }
|
|
2631
|
+
{ inputChain: inputChain2, inputChainType, metaAddressCurve: "ed25519" }
|
|
2528
2632
|
);
|
|
2529
2633
|
}
|
|
2530
2634
|
} else {
|
|
2531
2635
|
throw new ValidationError(
|
|
2532
|
-
`senderAddress is required for refunds on ${
|
|
2636
|
+
`senderAddress is required for refunds on ${inputChain2}. Automatic refund address generation is only supported for EVM, Solana, and NEAR chains.`,
|
|
2533
2637
|
"senderAddress",
|
|
2534
|
-
{ inputChain, inputChainType }
|
|
2638
|
+
{ inputChain: inputChain2, inputChainType }
|
|
2535
2639
|
);
|
|
2536
2640
|
}
|
|
2537
2641
|
}
|