@zyfai/sdk 0.1.14 → 0.1.16

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 CHANGED
@@ -40,8 +40,7 @@ pnpm add @zyfai/sdk viem
40
40
  import { ZyfaiSDK } from "@zyfai/sdk";
41
41
 
42
42
  const sdk = new ZyfaiSDK({
43
- apiKey: "your-execution-api-key", // Execution API (transactions, Safe deployment)
44
- dataApiKey: "your-data-api-key", // Data API (analytics, earnings, opportunities)
43
+ apiKey: "your-api-key", // API key for both Execution API and Data API
45
44
  bundlerApiKey: "your-bundler-api-key", // Required for Safe deployment
46
45
  environment: "production", // or 'staging' (default: 'production')
47
46
  });
@@ -49,12 +48,11 @@ const sdk = new ZyfaiSDK({
49
48
 
50
49
  **Configuration Options:**
51
50
 
52
- | Option | Required | Description |
53
- | --------------- | -------- | ----------------------------------------------------------------------------------------------- |
54
- | `apiKey` | Yes | API key for Execution API (Safe deployment, transactions, session keys) |
55
- | `dataApiKey` | No | API key for Data API (earnings, opportunities, analytics). Defaults to `apiKey` if not provided |
56
- | `bundlerApiKey` | No\* | Pimlico API key for Safe deployment (\*required for `deploySafe`) |
57
- | `environment` | No | `"production"` or `"staging"` (default: `"production"`) |
51
+ | Option | Required | Description |
52
+ | --------------- | -------- | ---------------------------------------------------------------------------------------------------- |
53
+ | `apiKey` | Yes | API key for both Execution API and Data API (Safe deployment, transactions, session keys, analytics) |
54
+ | `bundlerApiKey` | No\* | Pimlico API key for Safe deployment (\*required for `deploySafe`) |
55
+ | `environment` | No | `"production"` or `"staging"` (default: `"production"`) |
58
56
 
59
57
  **API Endpoints by Environment:**
60
58
 
@@ -786,12 +784,8 @@ try {
786
784
  For running the examples, set up the following environment variables:
787
785
 
788
786
  ```bash
789
- # Required: Execution API key (Safe deployment, transactions, session keys)
790
- ZYFAI_API_KEY=your-execution-api-key
791
-
792
- # Optional: Data API key (earnings, opportunities, analytics)
793
- # Falls back to ZYFAI_API_KEY if not provided
794
- ZYFAI_DATA_API_KEY=your-data-api-key
787
+ # Required: API key for both Execution API and Data API
788
+ ZYFAI_API_KEY=your-api-key
795
789
 
796
790
  # Required for Safe deployment: Bundler API key (e.g., Pimlico)
797
791
  BUNDLER_API_KEY=your-pimlico-api-key
package/dist/index.d.mts CHANGED
@@ -7,10 +7,8 @@ type Address = `0x${string}`;
7
7
  type Hex = `0x${string}`;
8
8
  type Environment = "staging" | "production";
9
9
  interface SDKConfig {
10
- /** API key for the Execution API */
10
+ /** API key for both Execution API and Data API */
11
11
  apiKey: string;
12
- /** API key for the Data API - defaults to apiKey if not provided */
13
- dataApiKey?: string;
14
12
  /** Environment: 'staging' or 'production' (default: 'production') */
15
13
  environment?: Environment;
16
14
  /** Bundler API key for Safe deployment (e.g., Pimlico) */
package/dist/index.d.ts CHANGED
@@ -7,10 +7,8 @@ type Address = `0x${string}`;
7
7
  type Hex = `0x${string}`;
8
8
  type Environment = "staging" | "production";
9
9
  interface SDKConfig {
10
- /** API key for the Execution API */
10
+ /** API key for both Execution API and Data API */
11
11
  apiKey: string;
12
- /** API key for the Data API - defaults to apiKey if not provided */
13
- dataApiKey?: string;
14
12
  /** Environment: 'staging' or 'production' (default: 'production') */
15
13
  environment?: Environment;
16
14
  /** Bundler API key for Safe deployment (e.g., Pimlico) */
package/dist/index.js CHANGED
@@ -102,14 +102,12 @@ var HttpClient = class {
102
102
  /**
103
103
  * Create HTTP client for both Execution API and Data API
104
104
  *
105
- * @param apiKey - API key for Execution API
105
+ * @param apiKey - API key for both Execution API and Data API
106
106
  * @param environment - 'staging' or 'production'
107
- * @param dataApiKey - API key for Data API - defaults to apiKey
108
107
  */
109
- constructor(apiKey, environment = "production", dataApiKey) {
108
+ constructor(apiKey, environment = "production") {
110
109
  this.authToken = null;
111
110
  this.apiKey = apiKey;
112
- this.dataApiKey = dataApiKey || apiKey;
113
111
  this.environment = environment;
114
112
  const endpoint = API_ENDPOINTS[environment];
115
113
  const parsedUrl = new URL(endpoint);
@@ -128,7 +126,7 @@ var HttpClient = class {
128
126
  baseURL: `${dataEndpoint}${DATA_API_VERSION}`,
129
127
  headers: {
130
128
  "Content-Type": "application/json",
131
- "X-API-Key": this.dataApiKey
129
+ "X-API-Key": this.apiKey
132
130
  },
133
131
  timeout: 3e4
134
132
  });
@@ -227,7 +225,7 @@ var HttpClient = class {
227
225
  const fullUrl = `${DATA_API_ENDPOINTS[this.environment]}${path}`;
228
226
  const headers = {
229
227
  "Content-Type": "application/json",
230
- "X-API-Key": this.dataApiKey,
228
+ "X-API-Key": this.apiKey,
231
229
  ...config?.headers
232
230
  };
233
231
  if (this.authToken) {
@@ -243,7 +241,7 @@ var HttpClient = class {
243
241
  setupDataInterceptors() {
244
242
  this.dataClient.interceptors.request.use(
245
243
  (config) => {
246
- config.headers["X-API-Key"] = this.dataApiKey;
244
+ config.headers["X-API-Key"] = this.apiKey;
247
245
  if (this.authToken) {
248
246
  config.headers["Authorization"] = `Bearer ${this.authToken}`;
249
247
  }
@@ -476,10 +474,30 @@ var getSafeAccount = async (config) => {
476
474
  threshold: 1
477
475
  });
478
476
  const saltHex = (0, import_viem3.fromHex)((0, import_viem3.toHex)(effectiveSalt), "bigint");
477
+ const signer = {
478
+ ...owner.account,
479
+ address: signerAddress,
480
+ // Override with the signer address at top level
481
+ signMessage: async (message) => {
482
+ if (typeof message === "string") {
483
+ return await owner.signMessage({
484
+ account: owner.account,
485
+ message
486
+ });
487
+ } else {
488
+ return await owner.signMessage({
489
+ account: owner.account,
490
+ message: {
491
+ raw: message.raw
492
+ }
493
+ });
494
+ }
495
+ }
496
+ };
479
497
  const safeAccount = await (0, import_accounts.toSafeSmartAccount)({
480
498
  client: publicClient,
481
- owners: [owner],
482
- // Pass the owner object with address and signMessage capability
499
+ owners: [signer],
500
+ // Pass the signer object with address at top level and signMessage capability
483
501
  version: "1.4.1",
484
502
  entryPoint: {
485
503
  address: import_account_abstraction.entryPoint07Address,
@@ -612,7 +630,7 @@ var deploySafeAccount = async (config) => {
612
630
  );
613
631
  }
614
632
  };
615
- var signSessionKey = async (config, sessions, allPublicClients) => {
633
+ var signSessionKey = async (config, sessions, allPublicClients, signingParams) => {
616
634
  const { owner, publicClient } = config;
617
635
  if (!owner || !owner.account) {
618
636
  throw new Error("Wallet not connected. Please connect your wallet first.");
@@ -640,8 +658,9 @@ var signSessionKey = async (config, sessions, allPublicClients) => {
640
658
  sessions,
641
659
  account,
642
660
  clients,
643
- permitGenericPolicy: true,
644
- sessionNonces
661
+ permitGenericPolicy: signingParams?.permitGenericPolicy ?? true,
662
+ sessionNonces,
663
+ ignoreSecurityAttestations: signingParams?.ignoreSecurityAttestations ?? false
645
664
  });
646
665
  const signature = await owner.signMessage({
647
666
  account: owner.account,
@@ -670,12 +689,12 @@ var ZyfaiSDK = class {
670
689
  // Store reference to current provider for event handling
671
690
  this.currentChainId = null;
672
691
  const sdkConfig = typeof config === "string" ? { apiKey: config } : config;
673
- const { apiKey, dataApiKey, environment, bundlerApiKey } = sdkConfig;
692
+ const { apiKey, environment, bundlerApiKey } = sdkConfig;
674
693
  if (!apiKey) {
675
694
  throw new Error("API key is required");
676
695
  }
677
696
  this.environment = environment || "production";
678
- this.httpClient = new HttpClient(apiKey, this.environment, dataApiKey);
697
+ this.httpClient = new HttpClient(apiKey, this.environment);
679
698
  this.bundlerApiKey = bundlerApiKey;
680
699
  }
681
700
  /**
@@ -1153,9 +1172,10 @@ var ZyfaiSDK = class {
1153
1172
  alreadyActive: true
1154
1173
  };
1155
1174
  }
1156
- const sessionConfig = await this.httpClient.get(
1175
+ const sessionConfigResponse = await this.httpClient.get(
1157
1176
  ENDPOINTS.SESSION_KEYS_CONFIG
1158
1177
  );
1178
+ const sessionConfig = Array.isArray(sessionConfigResponse) ? sessionConfigResponse : sessionConfigResponse.sessions;
1159
1179
  if (!sessionConfig || sessionConfig.length === 0) {
1160
1180
  throw new Error("No session configuration available from API");
1161
1181
  }
@@ -1163,16 +1183,36 @@ var ZyfaiSDK = class {
1163
1183
  ...session,
1164
1184
  chainId: BigInt(session.chainId)
1165
1185
  }));
1186
+ const DEFAULT_ACTION_TARGET = "0x0000000000000000000000000000000000000001";
1187
+ const DEFAULT_ACTION_SELECTOR = "0x00000001";
1188
+ const permitGenericPolicy = sessionConfig.some(
1189
+ (session) => session.actions?.some(
1190
+ (action) => action.actionTarget === DEFAULT_ACTION_TARGET && action.actionTargetSelector === DEFAULT_ACTION_SELECTOR
1191
+ )
1192
+ );
1193
+ const chainConfig = getChainConfig(chainId);
1194
+ const accountType = await getAccountType(
1195
+ userAddress,
1196
+ chainConfig.publicClient
1197
+ );
1198
+ const signingParams = {
1199
+ permitGenericPolicy,
1200
+ ignoreSecurityAttestations: accountType === "Safe"
1201
+ };
1166
1202
  const signatureResult = await this.signSessionKey(
1167
1203
  userAddress,
1168
1204
  chainId,
1169
- sessions
1205
+ sessions,
1206
+ signingParams
1170
1207
  );
1171
1208
  if (!signatureResult.signature) {
1172
1209
  throw new Error("Failed to obtain session key signature");
1173
1210
  }
1174
1211
  await this.updateUserProtocols(chainId);
1212
+ const signer = sessions[0].sessionValidator;
1213
+ console.log("Session validator:", signer);
1175
1214
  const activation = await this.activateSessionKey(
1215
+ signer,
1176
1216
  signatureResult.signature,
1177
1217
  signatureResult.sessionNonces
1178
1218
  );
@@ -1192,7 +1232,7 @@ var ZyfaiSDK = class {
1192
1232
  * Internal method to sign session key
1193
1233
  * @private
1194
1234
  */
1195
- async signSessionKey(userAddress, chainId, sessions) {
1235
+ async signSessionKey(userAddress, chainId, sessions, signingParams) {
1196
1236
  try {
1197
1237
  if (!userAddress) {
1198
1238
  throw new Error("User address is required");
@@ -1227,7 +1267,8 @@ var ZyfaiSDK = class {
1227
1267
  environment: this.environment
1228
1268
  },
1229
1269
  sessions,
1230
- allPublicClients
1270
+ allPublicClients,
1271
+ signingParams
1231
1272
  );
1232
1273
  const safeAddress = await getDeterministicSafeAddress({
1233
1274
  owner: walletClient,
@@ -1274,9 +1315,10 @@ var ZyfaiSDK = class {
1274
1315
  /**
1275
1316
  * Activate session key via ZyFAI API
1276
1317
  */
1277
- async activateSessionKey(signature, sessionNonces) {
1318
+ async activateSessionKey(signer, signature, sessionNonces) {
1278
1319
  const nonces = this.normalizeSessionNonces(sessionNonces);
1279
1320
  const payload = {
1321
+ signer,
1280
1322
  hash: signature,
1281
1323
  nonces
1282
1324
  };
package/dist/index.mjs CHANGED
@@ -63,14 +63,12 @@ var HttpClient = class {
63
63
  /**
64
64
  * Create HTTP client for both Execution API and Data API
65
65
  *
66
- * @param apiKey - API key for Execution API
66
+ * @param apiKey - API key for both Execution API and Data API
67
67
  * @param environment - 'staging' or 'production'
68
- * @param dataApiKey - API key for Data API - defaults to apiKey
69
68
  */
70
- constructor(apiKey, environment = "production", dataApiKey) {
69
+ constructor(apiKey, environment = "production") {
71
70
  this.authToken = null;
72
71
  this.apiKey = apiKey;
73
- this.dataApiKey = dataApiKey || apiKey;
74
72
  this.environment = environment;
75
73
  const endpoint = API_ENDPOINTS[environment];
76
74
  const parsedUrl = new URL(endpoint);
@@ -89,7 +87,7 @@ var HttpClient = class {
89
87
  baseURL: `${dataEndpoint}${DATA_API_VERSION}`,
90
88
  headers: {
91
89
  "Content-Type": "application/json",
92
- "X-API-Key": this.dataApiKey
90
+ "X-API-Key": this.apiKey
93
91
  },
94
92
  timeout: 3e4
95
93
  });
@@ -188,7 +186,7 @@ var HttpClient = class {
188
186
  const fullUrl = `${DATA_API_ENDPOINTS[this.environment]}${path}`;
189
187
  const headers = {
190
188
  "Content-Type": "application/json",
191
- "X-API-Key": this.dataApiKey,
189
+ "X-API-Key": this.apiKey,
192
190
  ...config?.headers
193
191
  };
194
192
  if (this.authToken) {
@@ -204,7 +202,7 @@ var HttpClient = class {
204
202
  setupDataInterceptors() {
205
203
  this.dataClient.interceptors.request.use(
206
204
  (config) => {
207
- config.headers["X-API-Key"] = this.dataApiKey;
205
+ config.headers["X-API-Key"] = this.apiKey;
208
206
  if (this.authToken) {
209
207
  config.headers["Authorization"] = `Bearer ${this.authToken}`;
210
208
  }
@@ -456,10 +454,30 @@ var getSafeAccount = async (config) => {
456
454
  threshold: 1
457
455
  });
458
456
  const saltHex = fromHex(toHex(effectiveSalt), "bigint");
457
+ const signer = {
458
+ ...owner.account,
459
+ address: signerAddress,
460
+ // Override with the signer address at top level
461
+ signMessage: async (message) => {
462
+ if (typeof message === "string") {
463
+ return await owner.signMessage({
464
+ account: owner.account,
465
+ message
466
+ });
467
+ } else {
468
+ return await owner.signMessage({
469
+ account: owner.account,
470
+ message: {
471
+ raw: message.raw
472
+ }
473
+ });
474
+ }
475
+ }
476
+ };
459
477
  const safeAccount = await toSafeSmartAccount({
460
478
  client: publicClient,
461
- owners: [owner],
462
- // Pass the owner object with address and signMessage capability
479
+ owners: [signer],
480
+ // Pass the signer object with address at top level and signMessage capability
463
481
  version: "1.4.1",
464
482
  entryPoint: {
465
483
  address: entryPoint07Address,
@@ -592,7 +610,7 @@ var deploySafeAccount = async (config) => {
592
610
  );
593
611
  }
594
612
  };
595
- var signSessionKey = async (config, sessions, allPublicClients) => {
613
+ var signSessionKey = async (config, sessions, allPublicClients, signingParams) => {
596
614
  const { owner, publicClient } = config;
597
615
  if (!owner || !owner.account) {
598
616
  throw new Error("Wallet not connected. Please connect your wallet first.");
@@ -620,8 +638,9 @@ var signSessionKey = async (config, sessions, allPublicClients) => {
620
638
  sessions,
621
639
  account,
622
640
  clients,
623
- permitGenericPolicy: true,
624
- sessionNonces
641
+ permitGenericPolicy: signingParams?.permitGenericPolicy ?? true,
642
+ sessionNonces,
643
+ ignoreSecurityAttestations: signingParams?.ignoreSecurityAttestations ?? false
625
644
  });
626
645
  const signature = await owner.signMessage({
627
646
  account: owner.account,
@@ -650,12 +669,12 @@ var ZyfaiSDK = class {
650
669
  // Store reference to current provider for event handling
651
670
  this.currentChainId = null;
652
671
  const sdkConfig = typeof config === "string" ? { apiKey: config } : config;
653
- const { apiKey, dataApiKey, environment, bundlerApiKey } = sdkConfig;
672
+ const { apiKey, environment, bundlerApiKey } = sdkConfig;
654
673
  if (!apiKey) {
655
674
  throw new Error("API key is required");
656
675
  }
657
676
  this.environment = environment || "production";
658
- this.httpClient = new HttpClient(apiKey, this.environment, dataApiKey);
677
+ this.httpClient = new HttpClient(apiKey, this.environment);
659
678
  this.bundlerApiKey = bundlerApiKey;
660
679
  }
661
680
  /**
@@ -1133,9 +1152,10 @@ var ZyfaiSDK = class {
1133
1152
  alreadyActive: true
1134
1153
  };
1135
1154
  }
1136
- const sessionConfig = await this.httpClient.get(
1155
+ const sessionConfigResponse = await this.httpClient.get(
1137
1156
  ENDPOINTS.SESSION_KEYS_CONFIG
1138
1157
  );
1158
+ const sessionConfig = Array.isArray(sessionConfigResponse) ? sessionConfigResponse : sessionConfigResponse.sessions;
1139
1159
  if (!sessionConfig || sessionConfig.length === 0) {
1140
1160
  throw new Error("No session configuration available from API");
1141
1161
  }
@@ -1143,16 +1163,36 @@ var ZyfaiSDK = class {
1143
1163
  ...session,
1144
1164
  chainId: BigInt(session.chainId)
1145
1165
  }));
1166
+ const DEFAULT_ACTION_TARGET = "0x0000000000000000000000000000000000000001";
1167
+ const DEFAULT_ACTION_SELECTOR = "0x00000001";
1168
+ const permitGenericPolicy = sessionConfig.some(
1169
+ (session) => session.actions?.some(
1170
+ (action) => action.actionTarget === DEFAULT_ACTION_TARGET && action.actionTargetSelector === DEFAULT_ACTION_SELECTOR
1171
+ )
1172
+ );
1173
+ const chainConfig = getChainConfig(chainId);
1174
+ const accountType = await getAccountType(
1175
+ userAddress,
1176
+ chainConfig.publicClient
1177
+ );
1178
+ const signingParams = {
1179
+ permitGenericPolicy,
1180
+ ignoreSecurityAttestations: accountType === "Safe"
1181
+ };
1146
1182
  const signatureResult = await this.signSessionKey(
1147
1183
  userAddress,
1148
1184
  chainId,
1149
- sessions
1185
+ sessions,
1186
+ signingParams
1150
1187
  );
1151
1188
  if (!signatureResult.signature) {
1152
1189
  throw new Error("Failed to obtain session key signature");
1153
1190
  }
1154
1191
  await this.updateUserProtocols(chainId);
1192
+ const signer = sessions[0].sessionValidator;
1193
+ console.log("Session validator:", signer);
1155
1194
  const activation = await this.activateSessionKey(
1195
+ signer,
1156
1196
  signatureResult.signature,
1157
1197
  signatureResult.sessionNonces
1158
1198
  );
@@ -1172,7 +1212,7 @@ var ZyfaiSDK = class {
1172
1212
  * Internal method to sign session key
1173
1213
  * @private
1174
1214
  */
1175
- async signSessionKey(userAddress, chainId, sessions) {
1215
+ async signSessionKey(userAddress, chainId, sessions, signingParams) {
1176
1216
  try {
1177
1217
  if (!userAddress) {
1178
1218
  throw new Error("User address is required");
@@ -1207,7 +1247,8 @@ var ZyfaiSDK = class {
1207
1247
  environment: this.environment
1208
1248
  },
1209
1249
  sessions,
1210
- allPublicClients
1250
+ allPublicClients,
1251
+ signingParams
1211
1252
  );
1212
1253
  const safeAddress = await getDeterministicSafeAddress({
1213
1254
  owner: walletClient,
@@ -1254,9 +1295,10 @@ var ZyfaiSDK = class {
1254
1295
  /**
1255
1296
  * Activate session key via ZyFAI API
1256
1297
  */
1257
- async activateSessionKey(signature, sessionNonces) {
1298
+ async activateSessionKey(signer, signature, sessionNonces) {
1258
1299
  const nonces = this.normalizeSessionNonces(sessionNonces);
1259
1300
  const payload = {
1301
+ signer,
1260
1302
  hash: signature,
1261
1303
  nonces
1262
1304
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zyfai/sdk",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "description": "TypeScript SDK for ZyFAI Yield Optimization Engine - Deploy Safe smart wallets, manage session keys, and interact with DeFi protocols",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",