@fogo/sessions-sdk 0.0.25 → 0.0.29

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/cjs/index.js CHANGED
@@ -53,7 +53,7 @@ const establishSession = async (options) => {
53
53
  : await (0, kit_1.generateKeyPair)();
54
54
  if (options.unlimited) {
55
55
  return sendSessionEstablishTransaction(options, sessionKey, await Promise.all([
56
- buildIntentInstruction(options, sessionKey),
56
+ buildStartSessionIntentInstruction(options, sessionKey),
57
57
  buildStartSessionInstruction(options, sessionKey),
58
58
  ]), options.sessionEstablishmentLookupTable);
59
59
  }
@@ -63,7 +63,7 @@ const establishSession = async (options) => {
63
63
  ? await getTokenInfo(options.context, filteredLimits)
64
64
  : [];
65
65
  const [intentInstruction, startSessionInstruction] = await Promise.all([
66
- buildIntentInstruction(options, sessionKey, tokenInfo),
66
+ buildStartSessionIntentInstruction(options, sessionKey, tokenInfo),
67
67
  buildStartSessionInstruction(options, sessionKey, tokenInfo),
68
68
  ]);
69
69
  return sendSessionEstablishTransaction(options, sessionKey, [intentInstruction, startSessionInstruction], options.sessionEstablishmentLookupTable);
@@ -134,6 +134,47 @@ const createSession = async (context, walletPublicKey, sessionKey) => {
134
134
  sessionInfo,
135
135
  };
136
136
  };
137
+ const authorizedTokensSchema = zod_1.z.union([
138
+ zod_1.z.object({
139
+ Specific: zod_1.z.object({
140
+ "0": zod_1.z.array(zod_1.z.instanceof(web3_js_1.PublicKey)),
141
+ }),
142
+ }),
143
+ zod_1.z.object({ All: zod_1.z.object({}) }),
144
+ ]);
145
+ const revokedSessionInfoSchema = zod_1.z.object({
146
+ user: zod_1.z.instanceof(web3_js_1.PublicKey),
147
+ expiration: zod_1.z.instanceof(bn_js_1.default),
148
+ authorized_tokens_with_mints: authorizedTokensSchema,
149
+ });
150
+ const activeSessionInfoSchema = zod_1.z.object({
151
+ authorized_programs: zod_1.z.union([
152
+ zod_1.z.object({
153
+ Specific: zod_1.z.object({
154
+ 0: zod_1.z.array(zod_1.z.object({
155
+ program_id: zod_1.z.instanceof(web3_js_1.PublicKey),
156
+ signer_pda: zod_1.z.instanceof(web3_js_1.PublicKey),
157
+ })),
158
+ }),
159
+ }),
160
+ zod_1.z.object({
161
+ All: zod_1.z.object({}),
162
+ }),
163
+ ]),
164
+ authorized_tokens: zod_1.z.union([
165
+ zod_1.z.object({
166
+ Specific: zod_1.z.object({
167
+ "0": zod_1.z.array(zod_1.z.instanceof(web3_js_1.PublicKey)),
168
+ }),
169
+ }),
170
+ zod_1.z.object({ All: zod_1.z.object({}) }),
171
+ ]),
172
+ expiration: zod_1.z.instanceof(bn_js_1.default),
173
+ extra: zod_1.z.object({
174
+ 0: zod_1.z.unknown(),
175
+ }),
176
+ user: zod_1.z.instanceof(web3_js_1.PublicKey),
177
+ });
137
178
  const sessionInfoSchema = zod_1.z
138
179
  .object({
139
180
  session_info: zod_1.z.union([
@@ -206,37 +247,31 @@ const sessionInfoSchema = zod_1.z
206
247
  V3: zod_1.z.object({
207
248
  "0": zod_1.z.union([
208
249
  zod_1.z.object({
209
- Revoked: zod_1.z.instanceof(bn_js_1.default),
250
+ Revoked: zod_1.z.object({
251
+ "0": revokedSessionInfoSchema,
252
+ }),
253
+ }),
254
+ zod_1.z.object({
255
+ Active: zod_1.z.object({
256
+ "0": activeSessionInfoSchema,
257
+ }),
258
+ }),
259
+ ]),
260
+ }),
261
+ }),
262
+ zod_1.z.object({
263
+ V4: zod_1.z.object({
264
+ "0": zod_1.z.union([
265
+ zod_1.z.object({
266
+ Revoked: zod_1.z.object({
267
+ "0": revokedSessionInfoSchema,
268
+ }),
210
269
  }),
211
270
  zod_1.z.object({
212
271
  Active: zod_1.z.object({
213
272
  "0": zod_1.z.object({
214
- authorized_programs: zod_1.z.union([
215
- zod_1.z.object({
216
- Specific: zod_1.z.object({
217
- 0: zod_1.z.array(zod_1.z.object({
218
- program_id: zod_1.z.instanceof(web3_js_1.PublicKey),
219
- signer_pda: zod_1.z.instanceof(web3_js_1.PublicKey),
220
- })),
221
- }),
222
- }),
223
- zod_1.z.object({
224
- All: zod_1.z.object({}),
225
- }),
226
- ]),
227
- authorized_tokens: zod_1.z.union([
228
- zod_1.z.object({
229
- Specific: zod_1.z.object({
230
- "0": zod_1.z.array(zod_1.z.instanceof(web3_js_1.PublicKey)),
231
- }),
232
- }),
233
- zod_1.z.object({ All: zod_1.z.object({}) }),
234
- ]),
235
- expiration: zod_1.z.instanceof(bn_js_1.default),
236
- extra: zod_1.z.object({
237
- 0: zod_1.z.unknown(),
238
- }),
239
- user: zod_1.z.instanceof(web3_js_1.PublicKey),
273
+ domain_hash: zod_1.z.array(zod_1.z.number()).length(32),
274
+ active_session_info: activeSessionInfoSchema,
240
275
  }),
241
276
  }),
242
277
  }),
@@ -262,6 +297,10 @@ const sessionInfoSchema = zod_1.z
262
297
  activeSessionInfo = session_info.V3["0"].Active["0"];
263
298
  minor = 3;
264
299
  }
300
+ else if ("V4" in session_info && "Active" in session_info.V4["0"]) {
301
+ activeSessionInfo = session_info.V4["0"].Active["0"].active_session_info;
302
+ minor = 4;
303
+ }
265
304
  else {
266
305
  return;
267
306
  }
@@ -335,64 +374,23 @@ const getTokenInfo = async (context, limits) => {
335
374
  };
336
375
  }));
337
376
  };
338
- const serializeU16LE = (value) => {
339
- const result = new ArrayBuffer(2);
340
- new DataView(result).setUint16(0, value, true); // littleEndian = true
341
- return new Uint8Array(result);
342
- };
343
- // Some wallets add a prefix to the messag before signing, for example Ledger through Phantom
344
- const addOffchainMessagePrefixToMessageIfNeeded = async (walletPublicKey, signature, message) => {
345
- const publicKey = await crypto.subtle.importKey("raw", walletPublicKey.toBytes(), { name: "Ed25519" }, true, ["verify"]);
346
- if (await (0, kit_1.verifySignature)(publicKey, signature, message)) {
347
- return message;
348
- }
349
- else {
350
- // Source: https://github.com/anza-xyz/solana-sdk/blob/master/offchain-message/src/lib.rs#L162
351
- const messageWithOffchainMessagePrefix = Uint8Array.from([
352
- // eslint-disable-next-line unicorn/number-literal-case
353
- 0xff,
354
- ...new TextEncoder().encode("solana offchain"),
355
- 0,
356
- 1,
357
- ...serializeU16LE(message.length),
358
- ...message,
359
- ]);
360
- if (await (0, kit_1.verifySignature)(publicKey, signature, messageWithOffchainMessagePrefix)) {
361
- return messageWithOffchainMessagePrefix;
362
- }
363
- else {
364
- throw new Error("The signature provided by the browser wallet is not valid");
365
- }
366
- }
367
- };
368
- const buildIntentInstruction = async (options, sessionKey, tokens) => {
369
- const message = await buildMessage({
370
- chainId: options.context.chainId,
371
- domain: options.context.domain,
372
- sessionKey,
373
- expires: options.expires,
374
- tokens,
375
- extra: options.extra,
376
- });
377
- const intentSignature = (0, kit_1.signatureBytes)(await options.signMessage(message));
377
+ const buildStartSessionIntentInstruction = async (options, sessionKey, tokens) => buildIntentInstruction(options, MESSAGE_HEADER, {
378
+ version: `${CURRENT_MAJOR}.${CURRENT_MINOR}`,
379
+ chain_id: options.context.chainId,
380
+ domain: options.context.domain,
381
+ expires: options.expires.toISOString(),
382
+ session_key: await (0, kit_1.getAddressFromPublicKey)(sessionKey.publicKey),
383
+ tokens: serializeTokenList(tokens),
384
+ });
385
+ const buildIntentInstruction = async (options, header, body, extra) => {
386
+ const message = new TextEncoder().encode([header, serializeKV(body), extra && serializeExtra(extra)].join("\n"));
387
+ const { signature, signedMessage } = await options.signMessage(message);
378
388
  return web3_js_1.Ed25519Program.createInstructionWithPublicKey({
379
389
  publicKey: options.walletPublicKey.toBytes(),
380
- signature: intentSignature,
381
- message: await addOffchainMessagePrefixToMessageIfNeeded(options.walletPublicKey, intentSignature, message),
390
+ signature,
391
+ message: signedMessage,
382
392
  });
383
393
  };
384
- const buildMessage = async (body) => new TextEncoder().encode([
385
- MESSAGE_HEADER,
386
- serializeKV({
387
- version: `${CURRENT_MAJOR}.${CURRENT_MINOR}`,
388
- chain_id: body.chainId,
389
- domain: body.domain,
390
- expires: body.expires.toISOString(),
391
- session_key: await (0, kit_1.getAddressFromPublicKey)(body.sessionKey.publicKey),
392
- tokens: serializeTokenList(body.tokens),
393
- }),
394
- body.extra && serializeExtra(body.extra),
395
- ].join("\n"));
396
394
  const serializeExtra = (extra) => {
397
395
  for (const [key, value] of Object.entries(extra)) {
398
396
  if (!/^[a-z]+(_[a-z0-9]+)*$/.test(key)) {
@@ -579,24 +577,15 @@ const buildTransferIntentInstruction = async (program, options, symbol, feeToken
579
577
  getNonce(program, options.walletPublicKey, NonceType.Transfer),
580
578
  (0, spl_token_1.getMint)(options.context.connection, options.mint),
581
579
  ]);
582
- const message = new TextEncoder().encode([
583
- TRANSFER_MESSAGE_HEADER,
584
- serializeKV({
585
- version: `${CURRENT_INTENT_TRANSFER_MAJOR}.${CURRENT_INTENT_TRANSFER_MINOR}`,
586
- chain_id: options.context.chainId,
587
- token: symbol ?? options.mint.toBase58(),
588
- amount: amountToString(options.amount, decimals),
589
- recipient: options.recipient.toBase58(),
590
- fee_token: feeToken,
591
- fee_amount: feeAmount,
592
- nonce: nonce === null ? "1" : nonce.nonce.add(new bn_js_1.default(1)).toString(),
593
- }),
594
- ].join("\n"));
595
- const intentSignature = (0, kit_1.signatureBytes)(await options.signMessage(message));
596
- return web3_js_1.Ed25519Program.createInstructionWithPublicKey({
597
- publicKey: options.walletPublicKey.toBytes(),
598
- signature: intentSignature,
599
- message: await addOffchainMessagePrefixToMessageIfNeeded(options.walletPublicKey, intentSignature, message),
580
+ return buildIntentInstruction(options, TRANSFER_MESSAGE_HEADER, {
581
+ version: `${CURRENT_INTENT_TRANSFER_MAJOR}.${CURRENT_INTENT_TRANSFER_MINOR}`,
582
+ chain_id: options.context.chainId,
583
+ token: symbol ?? options.mint.toBase58(),
584
+ amount: amountToString(options.amount, decimals),
585
+ recipient: options.recipient.toBase58(),
586
+ fee_token: feeToken,
587
+ fee_amount: feeAmount,
588
+ nonce: nonce === null ? "1" : nonce.nonce.add(new bn_js_1.default(1)).toString(),
600
589
  });
601
590
  };
602
591
  const BRIDGE_OUT_MESSAGE_HEADER = `Fogo Bridge Transfer:
@@ -699,25 +688,16 @@ const getNttPdas = async (options, wh, program, outboxItemPublicKey, quotePayeeA
699
688
  };
700
689
  const buildBridgeOutIntent = async (program, options, decimals, symbol, feeToken, feeAmount) => {
701
690
  const nonce = await getNonce(program, options.walletPublicKey, NonceType.Bridge);
702
- const message = new TextEncoder().encode([
703
- BRIDGE_OUT_MESSAGE_HEADER,
704
- serializeKV({
705
- version: `${CURRENT_BRIDGE_OUT_MAJOR}.${CURRENT_BRIDGE_OUT_MINOR}`,
706
- from_chain_id: options.context.chainId,
707
- to_chain_id: "solana",
708
- token: symbol ?? options.fromToken.mint.toBase58(),
709
- amount: amountToString(options.amount, decimals),
710
- recipient_address: options.walletPublicKey.toBase58(),
711
- fee_token: feeToken,
712
- fee_amount: feeAmount,
713
- nonce: nonce === null ? "1" : nonce.nonce.add(new bn_js_1.default(1)).toString(),
714
- }),
715
- ].join("\n"));
716
- const intentSignature = (0, kit_1.signatureBytes)(await options.solanaWallet.signMessage(message));
717
- return web3_js_1.Ed25519Program.createInstructionWithPublicKey({
718
- publicKey: options.walletPublicKey.toBytes(),
719
- signature: intentSignature,
720
- message: await addOffchainMessagePrefixToMessageIfNeeded(options.walletPublicKey, intentSignature, message),
691
+ return buildIntentInstruction(options, BRIDGE_OUT_MESSAGE_HEADER, {
692
+ version: `${CURRENT_BRIDGE_OUT_MAJOR}.${CURRENT_BRIDGE_OUT_MINOR}`,
693
+ from_chain_id: options.context.chainId,
694
+ to_chain_id: "solana",
695
+ token: symbol ?? options.fromToken.mint.toBase58(),
696
+ amount: amountToString(options.amount, decimals),
697
+ recipient_address: options.walletPublicKey.toBase58(),
698
+ fee_token: feeToken,
699
+ fee_amount: feeAmount,
700
+ nonce: nonce === null ? "1" : nonce.nonce.add(new bn_js_1.default(1)).toString(),
721
701
  });
722
702
  };
723
703
  const bridgeIn = async (options) => {
@@ -731,10 +711,9 @@ const bridgeIn = async (options) => {
731
711
  address: () => options.walletPublicKey.toBase58(),
732
712
  chain: () => "Solana",
733
713
  sign: (transactions) => Promise.all(transactions.map(async ({ transaction }) => {
734
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
735
- const signedTx = await options.solanaWallet.signTransaction(
714
+ const signedTx = await options.signTransaction(
736
715
  // Hooray for Wormhole's incomplete typing eh?
737
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
716
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
738
717
  transaction.transaction);
739
718
  // Hooray for Wormhole's incomplete typing eh?
740
719
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
package/esm/connection.js CHANGED
@@ -12,8 +12,8 @@ const DEFAULT_RPC = {
12
12
  [Network.Mainnet]: "https://mainnet.fogo.io",
13
13
  };
14
14
  const DEFAULT_PAYMASTER = {
15
- [Network.Testnet]: "https://testnet.fogo-paymaster.xyz",
16
- [Network.Mainnet]: "https://mainnet.fogo-paymaster.xyz",
15
+ [Network.Testnet]: "https://fogo-testnet.dourolabs-paymaster.xyz",
16
+ [Network.Mainnet]: "https://fogo-mainnet.dourolabs-paymaster.xyz",
17
17
  };
18
18
  export var TransactionResultType;
19
19
  (function (TransactionResultType) {