@owney/sdk 0.6.7-beta.3 → 0.6.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/index.cjs +64 -97
- package/dist/index.d.cts +0 -7
- package/dist/index.d.ts +0 -7
- package/dist/index.js +64 -97
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -535,8 +535,7 @@ var storage = () => {
|
|
|
535
535
|
return null;
|
|
536
536
|
}
|
|
537
537
|
};
|
|
538
|
-
var buildKey = (address) => `${KEY_PREFIX}:${address.toLowerCase()}`;
|
|
539
|
-
var legacyKeyPrefix = (address) => `${KEY_PREFIX}:${address.toLowerCase()}:`;
|
|
538
|
+
var buildKey = (address, chainId) => `${KEY_PREFIX}:${address.toLowerCase()}:${chainId}`;
|
|
540
539
|
var isJwtExpired = (token) => {
|
|
541
540
|
const parts = token.split(".");
|
|
542
541
|
if (parts.length !== 3) return false;
|
|
@@ -553,37 +552,9 @@ var isJwtExpired = (token) => {
|
|
|
553
552
|
};
|
|
554
553
|
var memorySessions = /* @__PURE__ */ new Map();
|
|
555
554
|
var isFreshSession = (session) => !!session && !!session.token && !isJwtExpired(session.token);
|
|
556
|
-
var
|
|
557
|
-
if (!store) return null;
|
|
558
|
-
const prefix = legacyKeyPrefix(address);
|
|
559
|
-
const legacyKeys = [];
|
|
560
|
-
try {
|
|
561
|
-
for (let i = 0; i < store.length; i++) {
|
|
562
|
-
const key = store.key(i);
|
|
563
|
-
if (key?.startsWith(prefix)) legacyKeys.push(key);
|
|
564
|
-
}
|
|
565
|
-
} catch {
|
|
566
|
-
return null;
|
|
567
|
-
}
|
|
568
|
-
let adopted = null;
|
|
569
|
-
for (const key of legacyKeys) {
|
|
570
|
-
if (!adopted) {
|
|
571
|
-
try {
|
|
572
|
-
const parsed = JSON.parse(store.getItem(key) ?? "");
|
|
573
|
-
if (isFreshSession(parsed)) adopted = parsed;
|
|
574
|
-
} catch {
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
try {
|
|
578
|
-
store.removeItem(key);
|
|
579
|
-
} catch {
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
return adopted;
|
|
583
|
-
};
|
|
584
|
-
var readSession = (address, _chainId) => {
|
|
555
|
+
var readSession = (address, chainId) => {
|
|
585
556
|
if (typeof window === "undefined") return null;
|
|
586
|
-
const key = buildKey(address);
|
|
557
|
+
const key = buildKey(address, chainId);
|
|
587
558
|
const store = storage();
|
|
588
559
|
let raw = null;
|
|
589
560
|
try {
|
|
@@ -604,19 +575,14 @@ var readSession = (address, _chainId) => {
|
|
|
604
575
|
}
|
|
605
576
|
return null;
|
|
606
577
|
}
|
|
607
|
-
const legacy = readLegacySession(store, address);
|
|
608
|
-
if (legacy) {
|
|
609
|
-
writeSession(address, _chainId, legacy);
|
|
610
|
-
return legacy;
|
|
611
|
-
}
|
|
612
578
|
const cached = memorySessions.get(key);
|
|
613
579
|
if (isFreshSession(cached)) return cached;
|
|
614
580
|
if (cached) memorySessions.delete(key);
|
|
615
581
|
return null;
|
|
616
582
|
};
|
|
617
|
-
var writeSession = (address,
|
|
583
|
+
var writeSession = (address, chainId, session) => {
|
|
618
584
|
if (typeof window === "undefined") return;
|
|
619
|
-
const key = buildKey(address);
|
|
585
|
+
const key = buildKey(address, chainId);
|
|
620
586
|
memorySessions.set(key, session);
|
|
621
587
|
const store = storage();
|
|
622
588
|
try {
|
|
@@ -624,15 +590,14 @@ var writeSession = (address, _chainId, session) => {
|
|
|
624
590
|
} catch {
|
|
625
591
|
}
|
|
626
592
|
};
|
|
627
|
-
var clearSession = (address,
|
|
628
|
-
const key = buildKey(address);
|
|
593
|
+
var clearSession = (address, chainId) => {
|
|
594
|
+
const key = buildKey(address, chainId);
|
|
629
595
|
memorySessions.delete(key);
|
|
630
596
|
const store = storage();
|
|
631
597
|
try {
|
|
632
598
|
store?.removeItem(key);
|
|
633
599
|
} catch {
|
|
634
600
|
}
|
|
635
|
-
readLegacySession(store, address);
|
|
636
601
|
};
|
|
637
602
|
|
|
638
603
|
// src/agents/zyfai/autoSelect.ts
|
|
@@ -648,6 +613,18 @@ function buildAutoSelectUpdate(current, asset, chainId) {
|
|
|
648
613
|
};
|
|
649
614
|
}
|
|
650
615
|
|
|
616
|
+
// src/agents/zyfai/protocol-merge.ts
|
|
617
|
+
function buildProtocolMergeUpdate(available, current, asset) {
|
|
618
|
+
const enabled = current.protocols ?? [];
|
|
619
|
+
const enabledSet = new Set(enabled);
|
|
620
|
+
const missing = available.filter((p) => !enabledSet.has(p.id));
|
|
621
|
+
if (missing.length === 0) return null;
|
|
622
|
+
return {
|
|
623
|
+
asset,
|
|
624
|
+
protocols: [.../* @__PURE__ */ new Set([...enabled, ...missing.map((p) => p.id)])]
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
|
|
651
628
|
// src/agents/zyfai/zyfai.agent.ts
|
|
652
629
|
var DEFAULT_ZYFAI_RPC_URLS = {
|
|
653
630
|
8453: "https://base-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
|
|
@@ -709,15 +686,11 @@ var ZyfaiAgent = class {
|
|
|
709
686
|
// successful decode is cached forever; failures are NOT cached so a transient
|
|
710
687
|
// RPC error retries on the next fetch.
|
|
711
688
|
withdrawAmountCache = /* @__PURE__ */ new Map();
|
|
712
|
-
constructor(apiKey, rpcUrls
|
|
689
|
+
constructor(apiKey, rpcUrls) {
|
|
713
690
|
this.rpcUrls = rpcUrls ?? DEFAULT_ZYFAI_RPC_URLS;
|
|
714
691
|
this.sdk = new import_sdk.ZyfaiSDK({
|
|
715
692
|
apiKey,
|
|
716
|
-
rpcUrls: this.rpcUrls
|
|
717
|
-
// Forwarded to /auth/login by the SDK's own SIWE handshake, so
|
|
718
|
-
// attribution survives without the AppKit SIWX config (which carried it
|
|
719
|
-
// via `createOwneySIWX({ referralSource })` when apps used the modal).
|
|
720
|
-
referralSource
|
|
693
|
+
rpcUrls: this.rpcUrls
|
|
721
694
|
});
|
|
722
695
|
this.installAuthCache();
|
|
723
696
|
}
|
|
@@ -956,6 +929,34 @@ var ZyfaiAgent = class {
|
|
|
956
929
|
}
|
|
957
930
|
}
|
|
958
931
|
}
|
|
932
|
+
/**
|
|
933
|
+
* Sync newly-available protocols onto an existing user's profile at wallet
|
|
934
|
+
* connection. Zyfai's own protocol sync (`updateUserProtocols`) runs only when
|
|
935
|
+
* a session key is created, so existing users — who already have one — never
|
|
936
|
+
* pick up protocols Zyfai adds later. This closes that gap idempotently:
|
|
937
|
+
* it diffs available-vs-enabled by protocol id and writes the full union
|
|
938
|
+
* back, or does nothing when none are missing.
|
|
939
|
+
*
|
|
940
|
+
* Non-fatal: a sync failure must never block activation. Only "USDC" / "WETH"
|
|
941
|
+
* are modeled per-asset by the Zyfai profile API, so other assets are skipped.
|
|
942
|
+
*/
|
|
943
|
+
async ensureProtocolsSynced(chainId, asset) {
|
|
944
|
+
if (asset !== "USDC" && asset !== "WETH") return;
|
|
945
|
+
try {
|
|
946
|
+
const [current, { protocols: available }] = await Promise.all([
|
|
947
|
+
this.sdk.getUserDetails(asset),
|
|
948
|
+
this.sdk.getAvailableProtocols(chainId)
|
|
949
|
+
]);
|
|
950
|
+
const update = buildProtocolMergeUpdate(available, current, asset);
|
|
951
|
+
if (!update) return;
|
|
952
|
+
await this.sdk.updateUserProfile(update);
|
|
953
|
+
} catch (error) {
|
|
954
|
+
console.warn(
|
|
955
|
+
"[zyfai] ensureProtocolsSynced failed (non-fatal):",
|
|
956
|
+
error instanceof Error ? error.message : String(error)
|
|
957
|
+
);
|
|
958
|
+
}
|
|
959
|
+
}
|
|
959
960
|
/** Connect + resolve smart wallet address for read operations. */
|
|
960
961
|
async resolveSmartWallet(state, chainId) {
|
|
961
962
|
await this.connectAuth(state, chainId);
|
|
@@ -987,6 +988,11 @@ var ZyfaiAgent = class {
|
|
|
987
988
|
async activateAgent(state, chainId) {
|
|
988
989
|
try {
|
|
989
990
|
await this.connectAuth(state, chainId);
|
|
991
|
+
const validChainId = isValidChainId(chainId);
|
|
992
|
+
await Promise.all([
|
|
993
|
+
this.ensureProtocolsSynced(validChainId, "USDC"),
|
|
994
|
+
this.ensureProtocolsSynced(validChainId, "WETH")
|
|
995
|
+
]);
|
|
990
996
|
} catch (error) {
|
|
991
997
|
throw error;
|
|
992
998
|
}
|
|
@@ -1681,7 +1687,6 @@ var OwneySDK = class {
|
|
|
1681
1687
|
apiKey;
|
|
1682
1688
|
zyfaiRpcUrls;
|
|
1683
1689
|
routingApiBaseUrl;
|
|
1684
|
-
referralSource;
|
|
1685
1690
|
cachedSponsoredCallback = null;
|
|
1686
1691
|
cachedWethSponsoredCallback = null;
|
|
1687
1692
|
initializingAgentsPromise = null;
|
|
@@ -1689,7 +1694,6 @@ var OwneySDK = class {
|
|
|
1689
1694
|
this.apiKey = config.apiKey;
|
|
1690
1695
|
this.zyfaiRpcUrls = config.zyfaiRpcUrls;
|
|
1691
1696
|
this.routingApiBaseUrl = config.routingApiBaseUrl;
|
|
1692
|
-
this.referralSource = config.referralSource;
|
|
1693
1697
|
}
|
|
1694
1698
|
/**
|
|
1695
1699
|
* Establish connection state for all subsequent SDK calls.
|
|
@@ -1873,7 +1877,7 @@ var OwneySDK = class {
|
|
|
1873
1877
|
}
|
|
1874
1878
|
createAgent(agentId, key) {
|
|
1875
1879
|
if (agentId === "zyfai") {
|
|
1876
|
-
return new ZyfaiAgent(key, this.zyfaiRpcUrls
|
|
1880
|
+
return new ZyfaiAgent(key, this.zyfaiRpcUrls);
|
|
1877
1881
|
}
|
|
1878
1882
|
return null;
|
|
1879
1883
|
}
|
|
@@ -2759,39 +2763,11 @@ var storage2 = () => {
|
|
|
2759
2763
|
return null;
|
|
2760
2764
|
}
|
|
2761
2765
|
};
|
|
2762
|
-
var buildKey2 = (address) => `${KEY_PREFIX2}:${address.toLowerCase()}`;
|
|
2763
|
-
var legacyKeyPrefix2 = (address) => `${KEY_PREFIX2}:${address.toLowerCase()}:`;
|
|
2766
|
+
var buildKey2 = (address, chainId) => `${KEY_PREFIX2}:${address.toLowerCase()}:${chainId}`;
|
|
2764
2767
|
var memorySiwxSessions = /* @__PURE__ */ new Map();
|
|
2765
|
-
var readLegacySiwxSession = (store, address) => {
|
|
2766
|
-
if (!store) return null;
|
|
2767
|
-
const prefix = legacyKeyPrefix2(address);
|
|
2768
|
-
const legacyKeys = [];
|
|
2769
|
-
try {
|
|
2770
|
-
for (let i = 0; i < store.length; i++) {
|
|
2771
|
-
const key = store.key(i);
|
|
2772
|
-
if (key?.startsWith(prefix)) legacyKeys.push(key);
|
|
2773
|
-
}
|
|
2774
|
-
} catch {
|
|
2775
|
-
return null;
|
|
2776
|
-
}
|
|
2777
|
-
let adopted = null;
|
|
2778
|
-
for (const key of legacyKeys) {
|
|
2779
|
-
if (!adopted) {
|
|
2780
|
-
try {
|
|
2781
|
-
adopted = JSON.parse(store.getItem(key) ?? "");
|
|
2782
|
-
} catch {
|
|
2783
|
-
}
|
|
2784
|
-
}
|
|
2785
|
-
try {
|
|
2786
|
-
store.removeItem(key);
|
|
2787
|
-
} catch {
|
|
2788
|
-
}
|
|
2789
|
-
}
|
|
2790
|
-
return adopted;
|
|
2791
|
-
};
|
|
2792
2768
|
var readSiwxSession = (address, chainId) => {
|
|
2793
2769
|
if (typeof window === "undefined") return null;
|
|
2794
|
-
const key = buildKey2(address);
|
|
2770
|
+
const key = buildKey2(address, chainId);
|
|
2795
2771
|
const store = storage2();
|
|
2796
2772
|
let raw = null;
|
|
2797
2773
|
try {
|
|
@@ -2811,16 +2787,11 @@ var readSiwxSession = (address, chainId) => {
|
|
|
2811
2787
|
return null;
|
|
2812
2788
|
}
|
|
2813
2789
|
}
|
|
2814
|
-
const legacy = readLegacySiwxSession(store, address);
|
|
2815
|
-
if (legacy) {
|
|
2816
|
-
writeSiwxSession(address, chainId, legacy);
|
|
2817
|
-
return legacy;
|
|
2818
|
-
}
|
|
2819
2790
|
return memorySiwxSessions.get(key) ?? null;
|
|
2820
2791
|
};
|
|
2821
|
-
var writeSiwxSession = (address,
|
|
2792
|
+
var writeSiwxSession = (address, chainId, session) => {
|
|
2822
2793
|
if (typeof window === "undefined") return;
|
|
2823
|
-
const key = buildKey2(address);
|
|
2794
|
+
const key = buildKey2(address, chainId);
|
|
2824
2795
|
memorySiwxSessions.set(key, session);
|
|
2825
2796
|
const store = storage2();
|
|
2826
2797
|
try {
|
|
@@ -2828,15 +2799,14 @@ var writeSiwxSession = (address, _chainId, session) => {
|
|
|
2828
2799
|
} catch {
|
|
2829
2800
|
}
|
|
2830
2801
|
};
|
|
2831
|
-
var clearSiwxSession = (address,
|
|
2832
|
-
const key = buildKey2(address);
|
|
2802
|
+
var clearSiwxSession = (address, chainId) => {
|
|
2803
|
+
const key = buildKey2(address, chainId);
|
|
2833
2804
|
memorySiwxSessions.delete(key);
|
|
2834
2805
|
const store = storage2();
|
|
2835
2806
|
try {
|
|
2836
2807
|
store?.removeItem(key);
|
|
2837
2808
|
} catch {
|
|
2838
2809
|
}
|
|
2839
|
-
readLegacySiwxSession(store, address);
|
|
2840
2810
|
};
|
|
2841
2811
|
|
|
2842
2812
|
// src/agents/zyfai/zyfai.siwx.ts
|
|
@@ -2900,12 +2870,9 @@ function buildSIWXConfig(deps) {
|
|
|
2900
2870
|
}
|
|
2901
2871
|
if (!stored) return [];
|
|
2902
2872
|
const session = {
|
|
2903
|
-
// `stored.data` is
|
|
2904
|
-
//
|
|
2905
|
-
|
|
2906
|
-
// signature is valid for every chain, and AppKit would otherwise treat
|
|
2907
|
-
// the session as not covering the active network and re-prompt.
|
|
2908
|
-
data: { ...stored.data, chainId },
|
|
2873
|
+
// `stored.data.chainId` is a plain string in the cache (kept AppKit-free);
|
|
2874
|
+
// it always holds a valid CaipNetworkId here, so narrow at the boundary.
|
|
2875
|
+
data: stored.data,
|
|
2909
2876
|
message: stored.message,
|
|
2910
2877
|
signature: stored.signature
|
|
2911
2878
|
};
|
package/dist/index.d.cts
CHANGED
|
@@ -15,12 +15,6 @@ interface OwneySDKConfig {
|
|
|
15
15
|
* e.g. "http://localhost:3000", when testing sponsor changes.
|
|
16
16
|
*/
|
|
17
17
|
routingApiBaseUrl?: string;
|
|
18
|
-
/**
|
|
19
|
-
* Optional attribution tag forwarded to Zyfai's /auth/login when the SDK
|
|
20
|
-
* authenticates (e.g. "delos"). Replaces the equivalent option on
|
|
21
|
-
* `createOwneySIWX` for apps that don't use the AppKit SIWX modal.
|
|
22
|
-
*/
|
|
23
|
-
referralSource?: string;
|
|
24
18
|
}
|
|
25
19
|
interface ConnectionState {
|
|
26
20
|
provider: any;
|
|
@@ -319,7 +313,6 @@ declare class OwneySDK {
|
|
|
319
313
|
private apiKey;
|
|
320
314
|
private zyfaiRpcUrls?;
|
|
321
315
|
private routingApiBaseUrl?;
|
|
322
|
-
private referralSource?;
|
|
323
316
|
private cachedSponsoredCallback;
|
|
324
317
|
private cachedWethSponsoredCallback;
|
|
325
318
|
private initializingAgentsPromise;
|
package/dist/index.d.ts
CHANGED
|
@@ -15,12 +15,6 @@ interface OwneySDKConfig {
|
|
|
15
15
|
* e.g. "http://localhost:3000", when testing sponsor changes.
|
|
16
16
|
*/
|
|
17
17
|
routingApiBaseUrl?: string;
|
|
18
|
-
/**
|
|
19
|
-
* Optional attribution tag forwarded to Zyfai's /auth/login when the SDK
|
|
20
|
-
* authenticates (e.g. "delos"). Replaces the equivalent option on
|
|
21
|
-
* `createOwneySIWX` for apps that don't use the AppKit SIWX modal.
|
|
22
|
-
*/
|
|
23
|
-
referralSource?: string;
|
|
24
18
|
}
|
|
25
19
|
interface ConnectionState {
|
|
26
20
|
provider: any;
|
|
@@ -319,7 +313,6 @@ declare class OwneySDK {
|
|
|
319
313
|
private apiKey;
|
|
320
314
|
private zyfaiRpcUrls?;
|
|
321
315
|
private routingApiBaseUrl?;
|
|
322
|
-
private referralSource?;
|
|
323
316
|
private cachedSponsoredCallback;
|
|
324
317
|
private cachedWethSponsoredCallback;
|
|
325
318
|
private initializingAgentsPromise;
|
package/dist/index.js
CHANGED
|
@@ -503,8 +503,7 @@ var storage = () => {
|
|
|
503
503
|
return null;
|
|
504
504
|
}
|
|
505
505
|
};
|
|
506
|
-
var buildKey = (address) => `${KEY_PREFIX}:${address.toLowerCase()}`;
|
|
507
|
-
var legacyKeyPrefix = (address) => `${KEY_PREFIX}:${address.toLowerCase()}:`;
|
|
506
|
+
var buildKey = (address, chainId) => `${KEY_PREFIX}:${address.toLowerCase()}:${chainId}`;
|
|
508
507
|
var isJwtExpired = (token) => {
|
|
509
508
|
const parts = token.split(".");
|
|
510
509
|
if (parts.length !== 3) return false;
|
|
@@ -521,37 +520,9 @@ var isJwtExpired = (token) => {
|
|
|
521
520
|
};
|
|
522
521
|
var memorySessions = /* @__PURE__ */ new Map();
|
|
523
522
|
var isFreshSession = (session) => !!session && !!session.token && !isJwtExpired(session.token);
|
|
524
|
-
var
|
|
525
|
-
if (!store) return null;
|
|
526
|
-
const prefix = legacyKeyPrefix(address);
|
|
527
|
-
const legacyKeys = [];
|
|
528
|
-
try {
|
|
529
|
-
for (let i = 0; i < store.length; i++) {
|
|
530
|
-
const key = store.key(i);
|
|
531
|
-
if (key?.startsWith(prefix)) legacyKeys.push(key);
|
|
532
|
-
}
|
|
533
|
-
} catch {
|
|
534
|
-
return null;
|
|
535
|
-
}
|
|
536
|
-
let adopted = null;
|
|
537
|
-
for (const key of legacyKeys) {
|
|
538
|
-
if (!adopted) {
|
|
539
|
-
try {
|
|
540
|
-
const parsed = JSON.parse(store.getItem(key) ?? "");
|
|
541
|
-
if (isFreshSession(parsed)) adopted = parsed;
|
|
542
|
-
} catch {
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
try {
|
|
546
|
-
store.removeItem(key);
|
|
547
|
-
} catch {
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
return adopted;
|
|
551
|
-
};
|
|
552
|
-
var readSession = (address, _chainId) => {
|
|
523
|
+
var readSession = (address, chainId) => {
|
|
553
524
|
if (typeof window === "undefined") return null;
|
|
554
|
-
const key = buildKey(address);
|
|
525
|
+
const key = buildKey(address, chainId);
|
|
555
526
|
const store = storage();
|
|
556
527
|
let raw = null;
|
|
557
528
|
try {
|
|
@@ -572,19 +543,14 @@ var readSession = (address, _chainId) => {
|
|
|
572
543
|
}
|
|
573
544
|
return null;
|
|
574
545
|
}
|
|
575
|
-
const legacy = readLegacySession(store, address);
|
|
576
|
-
if (legacy) {
|
|
577
|
-
writeSession(address, _chainId, legacy);
|
|
578
|
-
return legacy;
|
|
579
|
-
}
|
|
580
546
|
const cached = memorySessions.get(key);
|
|
581
547
|
if (isFreshSession(cached)) return cached;
|
|
582
548
|
if (cached) memorySessions.delete(key);
|
|
583
549
|
return null;
|
|
584
550
|
};
|
|
585
|
-
var writeSession = (address,
|
|
551
|
+
var writeSession = (address, chainId, session) => {
|
|
586
552
|
if (typeof window === "undefined") return;
|
|
587
|
-
const key = buildKey(address);
|
|
553
|
+
const key = buildKey(address, chainId);
|
|
588
554
|
memorySessions.set(key, session);
|
|
589
555
|
const store = storage();
|
|
590
556
|
try {
|
|
@@ -592,15 +558,14 @@ var writeSession = (address, _chainId, session) => {
|
|
|
592
558
|
} catch {
|
|
593
559
|
}
|
|
594
560
|
};
|
|
595
|
-
var clearSession = (address,
|
|
596
|
-
const key = buildKey(address);
|
|
561
|
+
var clearSession = (address, chainId) => {
|
|
562
|
+
const key = buildKey(address, chainId);
|
|
597
563
|
memorySessions.delete(key);
|
|
598
564
|
const store = storage();
|
|
599
565
|
try {
|
|
600
566
|
store?.removeItem(key);
|
|
601
567
|
} catch {
|
|
602
568
|
}
|
|
603
|
-
readLegacySession(store, address);
|
|
604
569
|
};
|
|
605
570
|
|
|
606
571
|
// src/agents/zyfai/autoSelect.ts
|
|
@@ -616,6 +581,18 @@ function buildAutoSelectUpdate(current, asset, chainId) {
|
|
|
616
581
|
};
|
|
617
582
|
}
|
|
618
583
|
|
|
584
|
+
// src/agents/zyfai/protocol-merge.ts
|
|
585
|
+
function buildProtocolMergeUpdate(available, current, asset) {
|
|
586
|
+
const enabled = current.protocols ?? [];
|
|
587
|
+
const enabledSet = new Set(enabled);
|
|
588
|
+
const missing = available.filter((p) => !enabledSet.has(p.id));
|
|
589
|
+
if (missing.length === 0) return null;
|
|
590
|
+
return {
|
|
591
|
+
asset,
|
|
592
|
+
protocols: [.../* @__PURE__ */ new Set([...enabled, ...missing.map((p) => p.id)])]
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
|
|
619
596
|
// src/agents/zyfai/zyfai.agent.ts
|
|
620
597
|
var DEFAULT_ZYFAI_RPC_URLS = {
|
|
621
598
|
8453: "https://base-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
|
|
@@ -677,15 +654,11 @@ var ZyfaiAgent = class {
|
|
|
677
654
|
// successful decode is cached forever; failures are NOT cached so a transient
|
|
678
655
|
// RPC error retries on the next fetch.
|
|
679
656
|
withdrawAmountCache = /* @__PURE__ */ new Map();
|
|
680
|
-
constructor(apiKey, rpcUrls
|
|
657
|
+
constructor(apiKey, rpcUrls) {
|
|
681
658
|
this.rpcUrls = rpcUrls ?? DEFAULT_ZYFAI_RPC_URLS;
|
|
682
659
|
this.sdk = new ZyfaiSDK({
|
|
683
660
|
apiKey,
|
|
684
|
-
rpcUrls: this.rpcUrls
|
|
685
|
-
// Forwarded to /auth/login by the SDK's own SIWE handshake, so
|
|
686
|
-
// attribution survives without the AppKit SIWX config (which carried it
|
|
687
|
-
// via `createOwneySIWX({ referralSource })` when apps used the modal).
|
|
688
|
-
referralSource
|
|
661
|
+
rpcUrls: this.rpcUrls
|
|
689
662
|
});
|
|
690
663
|
this.installAuthCache();
|
|
691
664
|
}
|
|
@@ -924,6 +897,34 @@ var ZyfaiAgent = class {
|
|
|
924
897
|
}
|
|
925
898
|
}
|
|
926
899
|
}
|
|
900
|
+
/**
|
|
901
|
+
* Sync newly-available protocols onto an existing user's profile at wallet
|
|
902
|
+
* connection. Zyfai's own protocol sync (`updateUserProtocols`) runs only when
|
|
903
|
+
* a session key is created, so existing users — who already have one — never
|
|
904
|
+
* pick up protocols Zyfai adds later. This closes that gap idempotently:
|
|
905
|
+
* it diffs available-vs-enabled by protocol id and writes the full union
|
|
906
|
+
* back, or does nothing when none are missing.
|
|
907
|
+
*
|
|
908
|
+
* Non-fatal: a sync failure must never block activation. Only "USDC" / "WETH"
|
|
909
|
+
* are modeled per-asset by the Zyfai profile API, so other assets are skipped.
|
|
910
|
+
*/
|
|
911
|
+
async ensureProtocolsSynced(chainId, asset) {
|
|
912
|
+
if (asset !== "USDC" && asset !== "WETH") return;
|
|
913
|
+
try {
|
|
914
|
+
const [current, { protocols: available }] = await Promise.all([
|
|
915
|
+
this.sdk.getUserDetails(asset),
|
|
916
|
+
this.sdk.getAvailableProtocols(chainId)
|
|
917
|
+
]);
|
|
918
|
+
const update = buildProtocolMergeUpdate(available, current, asset);
|
|
919
|
+
if (!update) return;
|
|
920
|
+
await this.sdk.updateUserProfile(update);
|
|
921
|
+
} catch (error) {
|
|
922
|
+
console.warn(
|
|
923
|
+
"[zyfai] ensureProtocolsSynced failed (non-fatal):",
|
|
924
|
+
error instanceof Error ? error.message : String(error)
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
}
|
|
927
928
|
/** Connect + resolve smart wallet address for read operations. */
|
|
928
929
|
async resolveSmartWallet(state, chainId) {
|
|
929
930
|
await this.connectAuth(state, chainId);
|
|
@@ -955,6 +956,11 @@ var ZyfaiAgent = class {
|
|
|
955
956
|
async activateAgent(state, chainId) {
|
|
956
957
|
try {
|
|
957
958
|
await this.connectAuth(state, chainId);
|
|
959
|
+
const validChainId = isValidChainId(chainId);
|
|
960
|
+
await Promise.all([
|
|
961
|
+
this.ensureProtocolsSynced(validChainId, "USDC"),
|
|
962
|
+
this.ensureProtocolsSynced(validChainId, "WETH")
|
|
963
|
+
]);
|
|
958
964
|
} catch (error) {
|
|
959
965
|
throw error;
|
|
960
966
|
}
|
|
@@ -1653,7 +1659,6 @@ var OwneySDK = class {
|
|
|
1653
1659
|
apiKey;
|
|
1654
1660
|
zyfaiRpcUrls;
|
|
1655
1661
|
routingApiBaseUrl;
|
|
1656
|
-
referralSource;
|
|
1657
1662
|
cachedSponsoredCallback = null;
|
|
1658
1663
|
cachedWethSponsoredCallback = null;
|
|
1659
1664
|
initializingAgentsPromise = null;
|
|
@@ -1661,7 +1666,6 @@ var OwneySDK = class {
|
|
|
1661
1666
|
this.apiKey = config.apiKey;
|
|
1662
1667
|
this.zyfaiRpcUrls = config.zyfaiRpcUrls;
|
|
1663
1668
|
this.routingApiBaseUrl = config.routingApiBaseUrl;
|
|
1664
|
-
this.referralSource = config.referralSource;
|
|
1665
1669
|
}
|
|
1666
1670
|
/**
|
|
1667
1671
|
* Establish connection state for all subsequent SDK calls.
|
|
@@ -1845,7 +1849,7 @@ var OwneySDK = class {
|
|
|
1845
1849
|
}
|
|
1846
1850
|
createAgent(agentId, key) {
|
|
1847
1851
|
if (agentId === "zyfai") {
|
|
1848
|
-
return new ZyfaiAgent(key, this.zyfaiRpcUrls
|
|
1852
|
+
return new ZyfaiAgent(key, this.zyfaiRpcUrls);
|
|
1849
1853
|
}
|
|
1850
1854
|
return null;
|
|
1851
1855
|
}
|
|
@@ -2731,39 +2735,11 @@ var storage2 = () => {
|
|
|
2731
2735
|
return null;
|
|
2732
2736
|
}
|
|
2733
2737
|
};
|
|
2734
|
-
var buildKey2 = (address) => `${KEY_PREFIX2}:${address.toLowerCase()}`;
|
|
2735
|
-
var legacyKeyPrefix2 = (address) => `${KEY_PREFIX2}:${address.toLowerCase()}:`;
|
|
2738
|
+
var buildKey2 = (address, chainId) => `${KEY_PREFIX2}:${address.toLowerCase()}:${chainId}`;
|
|
2736
2739
|
var memorySiwxSessions = /* @__PURE__ */ new Map();
|
|
2737
|
-
var readLegacySiwxSession = (store, address) => {
|
|
2738
|
-
if (!store) return null;
|
|
2739
|
-
const prefix = legacyKeyPrefix2(address);
|
|
2740
|
-
const legacyKeys = [];
|
|
2741
|
-
try {
|
|
2742
|
-
for (let i = 0; i < store.length; i++) {
|
|
2743
|
-
const key = store.key(i);
|
|
2744
|
-
if (key?.startsWith(prefix)) legacyKeys.push(key);
|
|
2745
|
-
}
|
|
2746
|
-
} catch {
|
|
2747
|
-
return null;
|
|
2748
|
-
}
|
|
2749
|
-
let adopted = null;
|
|
2750
|
-
for (const key of legacyKeys) {
|
|
2751
|
-
if (!adopted) {
|
|
2752
|
-
try {
|
|
2753
|
-
adopted = JSON.parse(store.getItem(key) ?? "");
|
|
2754
|
-
} catch {
|
|
2755
|
-
}
|
|
2756
|
-
}
|
|
2757
|
-
try {
|
|
2758
|
-
store.removeItem(key);
|
|
2759
|
-
} catch {
|
|
2760
|
-
}
|
|
2761
|
-
}
|
|
2762
|
-
return adopted;
|
|
2763
|
-
};
|
|
2764
2740
|
var readSiwxSession = (address, chainId) => {
|
|
2765
2741
|
if (typeof window === "undefined") return null;
|
|
2766
|
-
const key = buildKey2(address);
|
|
2742
|
+
const key = buildKey2(address, chainId);
|
|
2767
2743
|
const store = storage2();
|
|
2768
2744
|
let raw = null;
|
|
2769
2745
|
try {
|
|
@@ -2783,16 +2759,11 @@ var readSiwxSession = (address, chainId) => {
|
|
|
2783
2759
|
return null;
|
|
2784
2760
|
}
|
|
2785
2761
|
}
|
|
2786
|
-
const legacy = readLegacySiwxSession(store, address);
|
|
2787
|
-
if (legacy) {
|
|
2788
|
-
writeSiwxSession(address, chainId, legacy);
|
|
2789
|
-
return legacy;
|
|
2790
|
-
}
|
|
2791
2762
|
return memorySiwxSessions.get(key) ?? null;
|
|
2792
2763
|
};
|
|
2793
|
-
var writeSiwxSession = (address,
|
|
2764
|
+
var writeSiwxSession = (address, chainId, session) => {
|
|
2794
2765
|
if (typeof window === "undefined") return;
|
|
2795
|
-
const key = buildKey2(address);
|
|
2766
|
+
const key = buildKey2(address, chainId);
|
|
2796
2767
|
memorySiwxSessions.set(key, session);
|
|
2797
2768
|
const store = storage2();
|
|
2798
2769
|
try {
|
|
@@ -2800,15 +2771,14 @@ var writeSiwxSession = (address, _chainId, session) => {
|
|
|
2800
2771
|
} catch {
|
|
2801
2772
|
}
|
|
2802
2773
|
};
|
|
2803
|
-
var clearSiwxSession = (address,
|
|
2804
|
-
const key = buildKey2(address);
|
|
2774
|
+
var clearSiwxSession = (address, chainId) => {
|
|
2775
|
+
const key = buildKey2(address, chainId);
|
|
2805
2776
|
memorySiwxSessions.delete(key);
|
|
2806
2777
|
const store = storage2();
|
|
2807
2778
|
try {
|
|
2808
2779
|
store?.removeItem(key);
|
|
2809
2780
|
} catch {
|
|
2810
2781
|
}
|
|
2811
|
-
readLegacySiwxSession(store, address);
|
|
2812
2782
|
};
|
|
2813
2783
|
|
|
2814
2784
|
// src/agents/zyfai/zyfai.siwx.ts
|
|
@@ -2872,12 +2842,9 @@ function buildSIWXConfig(deps) {
|
|
|
2872
2842
|
}
|
|
2873
2843
|
if (!stored) return [];
|
|
2874
2844
|
const session = {
|
|
2875
|
-
// `stored.data` is
|
|
2876
|
-
//
|
|
2877
|
-
|
|
2878
|
-
// signature is valid for every chain, and AppKit would otherwise treat
|
|
2879
|
-
// the session as not covering the active network and re-prompt.
|
|
2880
|
-
data: { ...stored.data, chainId },
|
|
2845
|
+
// `stored.data.chainId` is a plain string in the cache (kept AppKit-free);
|
|
2846
|
+
// it always holds a valid CaipNetworkId here, so narrow at the boundary.
|
|
2847
|
+
data: stored.data,
|
|
2881
2848
|
message: stored.message,
|
|
2882
2849
|
signature: stored.signature
|
|
2883
2850
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@owney/sdk",
|
|
3
|
-
"version": "0.6.7
|
|
3
|
+
"version": "0.6.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"test:watch": "vitest"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@zyfai/sdk": "0.2.
|
|
26
|
+
"@zyfai/sdk": "^0.2.41",
|
|
27
27
|
"siwe": "^3.0.0",
|
|
28
28
|
"viem": "^2.48.1"
|
|
29
29
|
},
|