@ziongateway/sdk 0.1.4 → 0.1.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.
@@ -34,6 +34,13 @@ export interface ZionAgentConfig {
34
34
  * @default false
35
35
  */
36
36
  debug?: boolean;
37
+ /**
38
+ * Optional Kora configuration for gasless transactions.
39
+ * If not provided, Zion SDK will automatically use default Kora URLs based on network.
40
+ */
41
+ koraConfig?: {
42
+ rpcUrl: string;
43
+ };
37
44
  }
38
45
  /**
39
46
  * Options for the fetch request (similar to standard fetch options).
@@ -115,6 +122,7 @@ export declare class ZionAgent {
115
122
  private connection;
116
123
  private network;
117
124
  private debug;
125
+ private koraClient?;
118
126
  /**
119
127
  * Create a new ZionAgent instance.
120
128
  *
@@ -165,7 +173,11 @@ export declare class ZionAgent {
165
173
  */
166
174
  private parsePaymentRequirements;
167
175
  /**
168
- * Execute the payment transaction.
176
+ * Execute Gasless Payment (Kora)
177
+ */
178
+ private executeGaslessPayment;
179
+ /**
180
+ * Execute the payment transaction (Standard SOL Gas).
169
181
  */
170
182
  private executePayment;
171
183
  /**
package/dist/ZionAgent.js CHANGED
@@ -9,6 +9,7 @@ const spl_token_1 = require("@solana/spl-token");
9
9
  const bs58_1 = __importDefault(require("bs58"));
10
10
  const index_1 = require("./index");
11
11
  const Universal_1 = require("./Universal");
12
+ const kora_1 = require("@solana/kora");
12
13
  /**
13
14
  * Custom error class for ZionAgent payment errors.
14
15
  */
@@ -69,6 +70,26 @@ class ZionAgent {
69
70
  : "https://api.mainnet-beta.solana.com";
70
71
  this.connection = new web3_js_1.Connection(config.rpcUrl || defaultRpc, "confirmed");
71
72
  this.debug = config.debug || false;
73
+ // Kora Configuration (Automatic)
74
+ let koraUrl = config.koraConfig?.rpcUrl;
75
+ // Auto-configure Kora URL if not provided
76
+ if (!koraUrl) {
77
+ if (this.network === "solana-devnet") {
78
+ koraUrl = "https://api.devnet.solana.com";
79
+ }
80
+ else {
81
+ koraUrl = "https://zion-kora-server-production.up.railway.app";
82
+ }
83
+ }
84
+ if (this.debug)
85
+ console.log(`[ZionAgent] Using Kora URL: ${koraUrl}`);
86
+ try {
87
+ this.koraClient = new kora_1.KoraClient({ rpcUrl: koraUrl });
88
+ }
89
+ catch (e) {
90
+ if (this.debug)
91
+ console.warn("[ZionAgent] Kora initialization failed, falling back to standard transactions", e);
92
+ }
72
93
  if (this.debug) {
73
94
  console.log(`[ZionAgent] Initialized with wallet: ${this.wallet.publicKey.toBase58()}`);
74
95
  console.log(`[ZionAgent] Network: ${this.network}`);
@@ -150,7 +171,30 @@ class ZionAgent {
150
171
  console.log(`[ZionAgent] Payment requirements:`, requirements);
151
172
  }
152
173
  // Execute payment
153
- const { signature, paymentHeader } = await this.executePayment(requirements);
174
+ let signature;
175
+ let paymentHeader;
176
+ try {
177
+ // Attempt Gasless with Kora first
178
+ if (this.koraClient) {
179
+ const result = await this.executeGaslessPayment(requirements);
180
+ signature = result.signature;
181
+ paymentHeader = result.paymentHeader;
182
+ }
183
+ else {
184
+ // Fallback to standard
185
+ const result = await this.executePayment(requirements);
186
+ signature = result.signature;
187
+ paymentHeader = result.paymentHeader;
188
+ }
189
+ }
190
+ catch (err) {
191
+ if (this.debug)
192
+ console.warn("[ZionAgent] Gasless Payment failed, attempting fallback to standard", err);
193
+ // Fallback to standard transaction if gasless fails
194
+ const result = await this.executePayment(requirements);
195
+ signature = result.signature;
196
+ paymentHeader = result.paymentHeader;
197
+ }
154
198
  if (this.debug) {
155
199
  console.log(`[ZionAgent] Payment successful! Signature: ${signature}`);
156
200
  }
@@ -198,7 +242,71 @@ class ZionAgent {
198
242
  };
199
243
  }
200
244
  /**
201
- * Execute the payment transaction.
245
+ * Execute Gasless Payment (Kora)
246
+ */
247
+ async executeGaslessPayment(requirements) {
248
+ if (!this.koraClient)
249
+ throw new Error("Kora client not initialized");
250
+ if (this.debug)
251
+ console.log("[ZionAgent] Attempting Gasless Transaction via Kora...");
252
+ const assetMint = new web3_js_1.PublicKey(requirements.asset);
253
+ // 1. Get payer signer from Kora (this is the account paying gas)
254
+ // Note: getPayerSigner returns public keys, we just need the address to set as fee payer
255
+ const { signer_address: feePayerAddress } = await this.koraClient.getPayerSigner();
256
+ const feePayer = new web3_js_1.PublicKey(feePayerAddress);
257
+ // 2. Build instructions
258
+ const instructions = await index_1.PaymentBuilder.createAtomicSplitInstructions({
259
+ sender: this.wallet.publicKey.toBase58(),
260
+ recipient: requirements.recipient,
261
+ treasury: requirements.treasury,
262
+ amount: requirements.amount,
263
+ asset: requirements.asset,
264
+ connection: this.connection // Uses main RPC for looking up ATAs
265
+ });
266
+ // 3. Build Transaction
267
+ const { blockhash } = await this.connection.getLatestBlockhash();
268
+ const tx = new web3_js_1.Transaction();
269
+ tx.recentBlockhash = blockhash;
270
+ tx.feePayer = feePayer;
271
+ tx.add(...instructions);
272
+ // 4. Sign (Partial) - Only user signs, Kora will sign later
273
+ tx.partialSign(this.wallet);
274
+ // 5. Serialize
275
+ // requireAllSignatures: false because Kora's signature is missing
276
+ const serializedTx = tx.serialize({ requireAllSignatures: false });
277
+ const base64Tx = serializedTx.toString("base64");
278
+ // 6. Send to Kora
279
+ const response = await this.koraClient.signAndSendTransaction({
280
+ transaction: base64Tx
281
+ });
282
+ let signature = response.signature;
283
+ // Fallback: If signature is missing (some Kora versions), extract from signed_transaction
284
+ if (!signature && response.signed_transaction) {
285
+ const signedTxBuf = Buffer.from(response.signed_transaction, "base64");
286
+ const signedTx = web3_js_1.Transaction.from(signedTxBuf);
287
+ if (signedTx.signature) {
288
+ signature = bs58_1.default.encode(signedTx.signature);
289
+ }
290
+ }
291
+ if (this.debug)
292
+ console.log(`[ZionAgent] Kora Transaction Sent: ${signature}`);
293
+ // Kora usually waits for confirmation, but we can double check
294
+ await this.connection.confirmTransaction(signature, "confirmed");
295
+ // Create header (Standard x402 header)
296
+ const paymentHeader = index_1.PaymentBuilder.createHeader({
297
+ signature,
298
+ sender: this.wallet.publicKey.toBase58(),
299
+ recipient: requirements.recipient,
300
+ amount: requirements.amount,
301
+ asset: requirements.asset,
302
+ timestamp: Math.floor(Date.now() / 1000),
303
+ nonce: this.generateNonce(),
304
+ network: requirements.network || this.network
305
+ });
306
+ return { signature, paymentHeader };
307
+ }
308
+ /**
309
+ * Execute the payment transaction (Standard SOL Gas).
202
310
  */
203
311
  async executePayment(requirements) {
204
312
  const assetMint = new web3_js_1.PublicKey(requirements.asset);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ziongateway/sdk",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "TypeScript SDK for Zion x402 Facilitator",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,6 +21,7 @@
21
21
  "author": "Zion",
22
22
  "license": "ISC",
23
23
  "dependencies": {
24
+ "@solana/kora": "^0.1.1",
24
25
  "@solana/spl-token": "^0.4.14",
25
26
  "@solana/web3.js": "^1.98.4",
26
27
  "axios": "^1.6.0",