@owney/sdk 0.5.0-beta.0 → 0.5.0

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 CHANGED
@@ -25,7 +25,8 @@ __export(src_exports, {
25
25
  InvalidHistoryCursorError: () => InvalidHistoryCursorError,
26
26
  NotConnectedError: () => NotConnectedError,
27
27
  OwneyError: () => OwneyError,
28
- OwneySDK: () => OwneySDK
28
+ OwneySDK: () => OwneySDK,
29
+ createOwneySIWX: () => createOwneySIWX
29
30
  });
30
31
  module.exports = __toCommonJS(src_exports);
31
32
 
@@ -2024,6 +2025,172 @@ var OwneySDK = class {
2024
2025
  };
2025
2026
  }
2026
2027
  };
2028
+
2029
+ // src/agents/zyfai/zyfai.siwx.ts
2030
+ var import_viem4 = require("viem");
2031
+ var import_siwe = require("siwe");
2032
+ var import_sdk2 = require("@zyfai/sdk");
2033
+
2034
+ // src/agents/zyfai/zyfai.siwx-cache.ts
2035
+ var KEY_PREFIX2 = "owney.siwx.session";
2036
+ var storage2 = () => {
2037
+ if (typeof window === "undefined")
2038
+ return null;
2039
+ try {
2040
+ return window.localStorage;
2041
+ } catch {
2042
+ return null;
2043
+ }
2044
+ };
2045
+ var buildKey2 = (address, chainId) => `${KEY_PREFIX2}:${address.toLowerCase()}:${chainId}`;
2046
+ var readSiwxSession = (address, chainId) => {
2047
+ const store = storage2();
2048
+ if (!store)
2049
+ return null;
2050
+ const raw = store.getItem(buildKey2(address, chainId));
2051
+ if (!raw)
2052
+ return null;
2053
+ try {
2054
+ return JSON.parse(raw);
2055
+ } catch {
2056
+ store.removeItem(buildKey2(address, chainId));
2057
+ return null;
2058
+ }
2059
+ };
2060
+ var writeSiwxSession = (address, chainId, session) => {
2061
+ const store = storage2();
2062
+ if (!store)
2063
+ return;
2064
+ try {
2065
+ store.setItem(buildKey2(address, chainId), JSON.stringify(session));
2066
+ } catch {
2067
+ }
2068
+ };
2069
+ var clearSiwxSession = (address, chainId) => {
2070
+ const store = storage2();
2071
+ if (!store)
2072
+ return;
2073
+ try {
2074
+ store.removeItem(buildKey2(address, chainId));
2075
+ } catch {
2076
+ }
2077
+ };
2078
+
2079
+ // src/agents/zyfai/zyfai.siwx.ts
2080
+ var STATEMENT = "Sign in with Ethereum";
2081
+ var VERSION = "1";
2082
+ var numericChainId = (caip) => {
2083
+ const segment = caip.split(":").pop();
2084
+ const id = Number(segment);
2085
+ if (!segment || !Number.isFinite(id) || id <= 0) {
2086
+ throw new Error(`Invalid CAIP chain id: "${caip}"`);
2087
+ }
2088
+ return id;
2089
+ };
2090
+ var browserDomain = () => typeof window !== "undefined" ? window.location.host : "";
2091
+ var browserUri = () => typeof window !== "undefined" ? window.location.origin : "";
2092
+ function buildSIWXConfig(deps) {
2093
+ const { post, referralSource } = deps;
2094
+ const createMessage = async (input) => {
2095
+ const { nonce } = await post("/auth/challenge", {});
2096
+ const domain = browserDomain();
2097
+ const uri = browserUri();
2098
+ const issuedAt = (/* @__PURE__ */ new Date()).toISOString();
2099
+ const chainId = input.chainId;
2100
+ const accountAddress = input.accountAddress;
2101
+ const message = {
2102
+ accountAddress,
2103
+ chainId,
2104
+ domain,
2105
+ uri,
2106
+ version: VERSION,
2107
+ nonce,
2108
+ statement: STATEMENT,
2109
+ issuedAt,
2110
+ toString() {
2111
+ return new import_siwe.SiweMessage({
2112
+ address: (0, import_viem4.getAddress)(accountAddress),
2113
+ chainId: numericChainId(chainId),
2114
+ domain,
2115
+ uri,
2116
+ statement: STATEMENT,
2117
+ nonce,
2118
+ version: VERSION,
2119
+ issuedAt
2120
+ }).prepareMessage();
2121
+ }
2122
+ };
2123
+ return message;
2124
+ };
2125
+ const getSessions = async (chainId, address) => {
2126
+ let id;
2127
+ try {
2128
+ id = numericChainId(chainId);
2129
+ } catch {
2130
+ return [];
2131
+ }
2132
+ const jwt = readSession(address, id);
2133
+ const stored = readSiwxSession(address, id);
2134
+ if (!jwt) {
2135
+ if (stored)
2136
+ clearSiwxSession(address, id);
2137
+ return [];
2138
+ }
2139
+ if (!stored)
2140
+ return [];
2141
+ const session = {
2142
+ // `stored.data.chainId` is a plain string in the cache (kept AppKit-free);
2143
+ // it always holds a valid CaipNetworkId here, so narrow at the boundary.
2144
+ data: stored.data,
2145
+ message: stored.message,
2146
+ signature: stored.signature
2147
+ };
2148
+ return [session];
2149
+ };
2150
+ const persistSession = async (session) => {
2151
+ const address = session.data.accountAddress;
2152
+ const id = numericChainId(session.data.chainId);
2153
+ const message = new import_siwe.SiweMessage(session.message);
2154
+ const login = await post("/auth/login", {
2155
+ message,
2156
+ signature: session.signature,
2157
+ referralSource
2158
+ });
2159
+ writeSession(address, id, {
2160
+ token: login.accessToken,
2161
+ userId: login.userId ?? null,
2162
+ hasActiveSessionKey: Boolean(login.hasActiveSessionKey)
2163
+ });
2164
+ const stored = {
2165
+ data: session.data,
2166
+ message: session.message,
2167
+ signature: session.signature
2168
+ };
2169
+ writeSiwxSession(address, id, stored);
2170
+ };
2171
+ const addSession = async (session) => {
2172
+ await persistSession(session);
2173
+ };
2174
+ const setSessions = async (sessions) => {
2175
+ for (const session of sessions) {
2176
+ await persistSession(session);
2177
+ }
2178
+ };
2179
+ const revokeSession = async (chainId, address) => {
2180
+ const id = numericChainId(chainId);
2181
+ clearSession(address, id);
2182
+ clearSiwxSession(address, id);
2183
+ };
2184
+ return { createMessage, getSessions, addSession, setSessions, revokeSession };
2185
+ }
2186
+ function createOwneySIWX(config) {
2187
+ const zyfai = new import_sdk2.ZyfaiSDK({ apiKey: config.apiKey });
2188
+ const http = zyfai.httpClient;
2189
+ return buildSIWXConfig({
2190
+ post: (url, data) => http.post(url, data),
2191
+ referralSource: config.referralSource
2192
+ });
2193
+ }
2027
2194
  // Annotate the CommonJS export names for ESM import in node:
2028
2195
  0 && (module.exports = {
2029
2196
  AgentChainIncompatibleError,
@@ -2031,5 +2198,6 @@ var OwneySDK = class {
2031
2198
  InvalidHistoryCursorError,
2032
2199
  NotConnectedError,
2033
2200
  OwneyError,
2034
- OwneySDK
2201
+ OwneySDK,
2202
+ createOwneySIWX
2035
2203
  });
package/dist/index.d.cts CHANGED
@@ -1,3 +1,5 @@
1
+ import { SIWXConfig } from '@reown/appkit-controllers';
2
+
1
3
  type ZyfaiRpcUrlsConfig = Partial<Record<(typeof SUPPORTED_CHAIN_IDS)[number], string>>;
2
4
  interface OwneySDKConfig {
3
5
  apiKey: string;
@@ -427,4 +429,17 @@ declare class InvalidHistoryCursorError extends Error {
427
429
  constructor(message: string);
428
430
  }
429
431
 
430
- export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentHistoryPosition, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, ApyByChainAndAsset, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, InvalidHistoryCursorError, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAgentStatus, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneySDK, OwneySDKConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions };
432
+ type OwneySIWXConfig = {
433
+ /** Owney/Zyfai API key — same value passed to `new OwneySDK({ apiKey })`. */
434
+ apiKey: string;
435
+ /** Optional attribution forwarded to /auth/login. */
436
+ referralSource?: string;
437
+ };
438
+ /**
439
+ * Public factory. Construct ONCE at `createAppKit` time and pass to
440
+ * `createAppKit({ siwx })`. Reuses Zyfai's internal httpClient so the API base
441
+ * URL and `X-API-Key` header stay a single source of truth.
442
+ */
443
+ declare function createOwneySIWX(config: OwneySIWXConfig): SIWXConfig;
444
+
445
+ export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentHistoryPosition, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, ApyByChainAndAsset, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, InvalidHistoryCursorError, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAgentStatus, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneySDK, OwneySDKConfig, OwneySIWXConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions, createOwneySIWX };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { SIWXConfig } from '@reown/appkit-controllers';
2
+
1
3
  type ZyfaiRpcUrlsConfig = Partial<Record<(typeof SUPPORTED_CHAIN_IDS)[number], string>>;
2
4
  interface OwneySDKConfig {
3
5
  apiKey: string;
@@ -427,4 +429,17 @@ declare class InvalidHistoryCursorError extends Error {
427
429
  constructor(message: string);
428
430
  }
429
431
 
430
- export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentHistoryPosition, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, ApyByChainAndAsset, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, InvalidHistoryCursorError, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAgentStatus, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneySDK, OwneySDKConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions };
432
+ type OwneySIWXConfig = {
433
+ /** Owney/Zyfai API key — same value passed to `new OwneySDK({ apiKey })`. */
434
+ apiKey: string;
435
+ /** Optional attribution forwarded to /auth/login. */
436
+ referralSource?: string;
437
+ };
438
+ /**
439
+ * Public factory. Construct ONCE at `createAppKit` time and pass to
440
+ * `createAppKit({ siwx })`. Reuses Zyfai's internal httpClient so the API base
441
+ * URL and `X-API-Key` header stay a single source of truth.
442
+ */
443
+ declare function createOwneySIWX(config: OwneySIWXConfig): SIWXConfig;
444
+
445
+ export { AccountAgentApy, AccountApyOptions, AgentApy, AgentApyDetails, AgentBalance, AgentChainIncompatibleError, AgentEarnings, AgentHistoryEntry, AgentHistoryPosition, AgentId, AgentNotFoundError, AgentSupportedAsset, AgentSupportedAssets, AgentUserProfile, AgentWithdrawResult, AgentsApyOptions, ApyByChainAndAsset, Asset, ConnectionState, DailyApyDays, DepositCallback, DepositOptions, HistoryAction, HistoryFilters, HistoryOptions, HistoryTransaction, IAgent, InvalidHistoryCursorError, NotConnectedError, OwneyAccountApy, OwneyAgentAllocation, OwneyAgentApy, OwneyAgentHistory, OwneyAgentStatus, OwneyBalances, OwneyDepositResult, OwneyEarnings, OwneyError, OwneyErrorCode, OwneyMultiDepositResult, OwneySDK, OwneySDKConfig, OwneySIWXConfig, OwneySupportedChainId, OwneySupportedChains, OwneySupportedTokens, OwneyToken, OwneyUserProfile, OwneyWithdrawResult, RebalanceLog, WithdrawOptions, createOwneySIWX };
package/dist/index.js CHANGED
@@ -1997,11 +1997,178 @@ var OwneySDK = class {
1997
1997
  };
1998
1998
  }
1999
1999
  };
2000
+
2001
+ // src/agents/zyfai/zyfai.siwx.ts
2002
+ import { getAddress } from "viem";
2003
+ import { SiweMessage } from "siwe";
2004
+ import { ZyfaiSDK as ZyfaiSDK2 } from "@zyfai/sdk";
2005
+
2006
+ // src/agents/zyfai/zyfai.siwx-cache.ts
2007
+ var KEY_PREFIX2 = "owney.siwx.session";
2008
+ var storage2 = () => {
2009
+ if (typeof window === "undefined")
2010
+ return null;
2011
+ try {
2012
+ return window.localStorage;
2013
+ } catch {
2014
+ return null;
2015
+ }
2016
+ };
2017
+ var buildKey2 = (address, chainId) => `${KEY_PREFIX2}:${address.toLowerCase()}:${chainId}`;
2018
+ var readSiwxSession = (address, chainId) => {
2019
+ const store = storage2();
2020
+ if (!store)
2021
+ return null;
2022
+ const raw = store.getItem(buildKey2(address, chainId));
2023
+ if (!raw)
2024
+ return null;
2025
+ try {
2026
+ return JSON.parse(raw);
2027
+ } catch {
2028
+ store.removeItem(buildKey2(address, chainId));
2029
+ return null;
2030
+ }
2031
+ };
2032
+ var writeSiwxSession = (address, chainId, session) => {
2033
+ const store = storage2();
2034
+ if (!store)
2035
+ return;
2036
+ try {
2037
+ store.setItem(buildKey2(address, chainId), JSON.stringify(session));
2038
+ } catch {
2039
+ }
2040
+ };
2041
+ var clearSiwxSession = (address, chainId) => {
2042
+ const store = storage2();
2043
+ if (!store)
2044
+ return;
2045
+ try {
2046
+ store.removeItem(buildKey2(address, chainId));
2047
+ } catch {
2048
+ }
2049
+ };
2050
+
2051
+ // src/agents/zyfai/zyfai.siwx.ts
2052
+ var STATEMENT = "Sign in with Ethereum";
2053
+ var VERSION = "1";
2054
+ var numericChainId = (caip) => {
2055
+ const segment = caip.split(":").pop();
2056
+ const id = Number(segment);
2057
+ if (!segment || !Number.isFinite(id) || id <= 0) {
2058
+ throw new Error(`Invalid CAIP chain id: "${caip}"`);
2059
+ }
2060
+ return id;
2061
+ };
2062
+ var browserDomain = () => typeof window !== "undefined" ? window.location.host : "";
2063
+ var browserUri = () => typeof window !== "undefined" ? window.location.origin : "";
2064
+ function buildSIWXConfig(deps) {
2065
+ const { post, referralSource } = deps;
2066
+ const createMessage = async (input) => {
2067
+ const { nonce } = await post("/auth/challenge", {});
2068
+ const domain = browserDomain();
2069
+ const uri = browserUri();
2070
+ const issuedAt = (/* @__PURE__ */ new Date()).toISOString();
2071
+ const chainId = input.chainId;
2072
+ const accountAddress = input.accountAddress;
2073
+ const message = {
2074
+ accountAddress,
2075
+ chainId,
2076
+ domain,
2077
+ uri,
2078
+ version: VERSION,
2079
+ nonce,
2080
+ statement: STATEMENT,
2081
+ issuedAt,
2082
+ toString() {
2083
+ return new SiweMessage({
2084
+ address: getAddress(accountAddress),
2085
+ chainId: numericChainId(chainId),
2086
+ domain,
2087
+ uri,
2088
+ statement: STATEMENT,
2089
+ nonce,
2090
+ version: VERSION,
2091
+ issuedAt
2092
+ }).prepareMessage();
2093
+ }
2094
+ };
2095
+ return message;
2096
+ };
2097
+ const getSessions = async (chainId, address) => {
2098
+ let id;
2099
+ try {
2100
+ id = numericChainId(chainId);
2101
+ } catch {
2102
+ return [];
2103
+ }
2104
+ const jwt = readSession(address, id);
2105
+ const stored = readSiwxSession(address, id);
2106
+ if (!jwt) {
2107
+ if (stored)
2108
+ clearSiwxSession(address, id);
2109
+ return [];
2110
+ }
2111
+ if (!stored)
2112
+ return [];
2113
+ const session = {
2114
+ // `stored.data.chainId` is a plain string in the cache (kept AppKit-free);
2115
+ // it always holds a valid CaipNetworkId here, so narrow at the boundary.
2116
+ data: stored.data,
2117
+ message: stored.message,
2118
+ signature: stored.signature
2119
+ };
2120
+ return [session];
2121
+ };
2122
+ const persistSession = async (session) => {
2123
+ const address = session.data.accountAddress;
2124
+ const id = numericChainId(session.data.chainId);
2125
+ const message = new SiweMessage(session.message);
2126
+ const login = await post("/auth/login", {
2127
+ message,
2128
+ signature: session.signature,
2129
+ referralSource
2130
+ });
2131
+ writeSession(address, id, {
2132
+ token: login.accessToken,
2133
+ userId: login.userId ?? null,
2134
+ hasActiveSessionKey: Boolean(login.hasActiveSessionKey)
2135
+ });
2136
+ const stored = {
2137
+ data: session.data,
2138
+ message: session.message,
2139
+ signature: session.signature
2140
+ };
2141
+ writeSiwxSession(address, id, stored);
2142
+ };
2143
+ const addSession = async (session) => {
2144
+ await persistSession(session);
2145
+ };
2146
+ const setSessions = async (sessions) => {
2147
+ for (const session of sessions) {
2148
+ await persistSession(session);
2149
+ }
2150
+ };
2151
+ const revokeSession = async (chainId, address) => {
2152
+ const id = numericChainId(chainId);
2153
+ clearSession(address, id);
2154
+ clearSiwxSession(address, id);
2155
+ };
2156
+ return { createMessage, getSessions, addSession, setSessions, revokeSession };
2157
+ }
2158
+ function createOwneySIWX(config) {
2159
+ const zyfai = new ZyfaiSDK2({ apiKey: config.apiKey });
2160
+ const http = zyfai.httpClient;
2161
+ return buildSIWXConfig({
2162
+ post: (url, data) => http.post(url, data),
2163
+ referralSource: config.referralSource
2164
+ });
2165
+ }
2000
2166
  export {
2001
2167
  AgentChainIncompatibleError,
2002
2168
  AgentNotFoundError,
2003
2169
  InvalidHistoryCursorError,
2004
2170
  NotConnectedError,
2005
2171
  OwneyError,
2006
- OwneySDK
2172
+ OwneySDK,
2173
+ createOwneySIWX
2007
2174
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owney/sdk",
3
- "version": "0.5.0-beta.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
@@ -24,9 +24,11 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@zyfai/sdk": "^0.2.36",
27
+ "siwe": "^3.0.0",
27
28
  "viem": "^2.48.1"
28
29
  },
29
30
  "devDependencies": {
31
+ "@reown/appkit-controllers": "^1.8.20",
30
32
  "dotenv": "^17.3.1",
31
33
  "tsup": "^7.0.0",
32
34
  "typescript": "^5.0.0",