@fogo/sessions-sdk 0.0.16 → 0.0.17

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.
@@ -1,7 +1,7 @@
1
1
  import type { Transaction, Instruction, TransactionWithLifetime } from "@solana/kit";
2
2
  import type { Connection, TransactionError } from "@solana/web3.js";
3
3
  import { PublicKey, TransactionInstruction, VersionedTransaction } from "@solana/web3.js";
4
- export type SessionAdapter = {
4
+ export type SessionContext = {
5
5
  chainId: string;
6
6
  connection: Connection;
7
7
  payer: PublicKey;
@@ -24,14 +24,17 @@ declare const TransactionResult: {
24
24
  };
25
25
  };
26
26
  export type TransactionResult = ReturnType<(typeof TransactionResult)[keyof typeof TransactionResult]>;
27
- export declare const createSolanaWalletAdapter: (options: {
27
+ export declare const createSessionContext: (options: {
28
28
  connection: Connection;
29
29
  addressLookupTableAddress?: string | undefined;
30
30
  domain?: string | undefined;
31
31
  } & ({
32
32
  paymaster?: string | URL | undefined;
33
+ sendToPaymaster?: undefined;
34
+ sponsor?: undefined;
33
35
  } | {
36
+ paymaster?: undefined;
34
37
  sendToPaymaster: (transaction: Transaction) => Promise<TransactionResult>;
35
38
  sponsor: PublicKey;
36
- })) => Promise<SessionAdapter>;
39
+ })) => Promise<SessionContext>;
37
40
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createSolanaWalletAdapter = exports.TransactionResultType = void 0;
3
+ exports.createSessionContext = exports.TransactionResultType = void 0;
4
4
  const anchor_1 = require("@coral-xyz/anchor");
5
5
  const sessions_idls_1 = require("@fogo/sessions-idls");
6
6
  const compat_1 = require("@solana/compat");
@@ -27,7 +27,7 @@ const TransactionResult = {
27
27
  error,
28
28
  }),
29
29
  };
30
- const createSolanaWalletAdapter = async (options) => {
30
+ const createSessionContext = async (options) => {
31
31
  const addressLookupTables = await getAddressLookupTables(options.connection, options.addressLookupTableAddress);
32
32
  const domain = getDomain(options.domain);
33
33
  const sponsor = await getSponsor(options, domain);
@@ -43,7 +43,7 @@ const createSolanaWalletAdapter = async (options) => {
43
43
  },
44
44
  };
45
45
  };
46
- exports.createSolanaWalletAdapter = createSolanaWalletAdapter;
46
+ exports.createSessionContext = createSessionContext;
47
47
  const buildTransaction = async (latestBlockhash, sessionKey, sponsor, instructions, addressLookupTables) => {
48
48
  const sessionKeySigner = sessionKey
49
49
  ? await (0, kit_1.createSignerFromKeyPair)(sessionKey)
@@ -68,10 +68,7 @@ const buildTransaction = async (latestBlockhash, sessionKey, sponsor, instructio
68
68
  }
69
69
  };
70
70
  const getSponsor = async (options, domain) => {
71
- if ("sponsor" in options) {
72
- return options.sponsor;
73
- }
74
- else {
71
+ if (options.sponsor === undefined) {
75
72
  const url = new URL("/api/sponsor_pubkey", options.paymaster ?? DEFAULT_PAYMASTER);
76
73
  url.searchParams.set("domain", domain);
77
74
  const response = await fetch(url);
@@ -82,6 +79,9 @@ const getSponsor = async (options, domain) => {
82
79
  throw new PaymasterResponseError(response.status, await response.text());
83
80
  }
84
81
  }
82
+ else {
83
+ return options.sponsor;
84
+ }
85
85
  };
86
86
  const sponsorAndSendResponseSchema = zod_1.z
87
87
  .discriminatedUnion("type", [
@@ -103,10 +103,7 @@ const sponsorAndSendResponseSchema = zod_1.z
103
103
  : TransactionResult.Failed(data.signature, data.error);
104
104
  });
105
105
  const sendToPaymaster = async (options, transaction, domain) => {
106
- if ("sendToPaymaster" in options) {
107
- return options.sendToPaymaster(transaction);
108
- }
109
- else {
106
+ if (options.sendToPaymaster === undefined) {
110
107
  const url = new URL("/api/sponsor_and_send", options.paymaster ?? DEFAULT_PAYMASTER);
111
108
  url.searchParams.set("domain", domain);
112
109
  const response = await fetch(url, {
@@ -125,6 +122,9 @@ const sendToPaymaster = async (options, transaction, domain) => {
125
122
  throw new PaymasterResponseError(response.status, await response.text());
126
123
  }
127
124
  }
125
+ else {
126
+ return options.sendToPaymaster(transaction);
127
+ }
128
128
  };
129
129
  const getAddressLookupTables = async (connection, addressLookupTableAddress = DEFAULT_ADDRESS_LOOKUP_TABLE_ADDRESS) => {
130
130
  const addressLookupTableResult = await connection.getAddressLookupTable(new web3_js_1.PublicKey(addressLookupTableAddress));
package/cjs/index.d.ts CHANGED
@@ -2,10 +2,10 @@ import type { Connection, TransactionError } from "@solana/web3.js";
2
2
  import { PublicKey } from "@solana/web3.js";
3
3
  import BN from "bn.js";
4
4
  import { z } from "zod";
5
- import type { SessionAdapter, TransactionResult } from "./adapter.js";
6
- export { createSolanaWalletAdapter, TransactionResultType, type SessionAdapter, type TransactionResult, } from "./adapter.js";
5
+ import type { SessionContext, TransactionResult } from "./context.js";
6
+ export { createSessionContext, TransactionResultType, type SessionContext, type TransactionResult, } from "./context.js";
7
7
  type EstablishSessionOptions = {
8
- adapter: SessionAdapter;
8
+ context: SessionContext;
9
9
  walletPublicKey: PublicKey;
10
10
  signMessage: (message: Uint8Array) => Promise<Uint8Array>;
11
11
  expires: Date;
@@ -19,7 +19,7 @@ type EstablishSessionOptions = {
19
19
  });
20
20
  export declare const establishSession: (options: EstablishSessionOptions) => Promise<EstablishSessionResult>;
21
21
  export declare const replaceSession: (options: {
22
- adapter: SessionAdapter;
22
+ context: SessionContext;
23
23
  session: Session;
24
24
  signMessage: (message: Uint8Array) => Promise<Uint8Array>;
25
25
  expires: Date;
@@ -31,10 +31,10 @@ export declare const replaceSession: (options: {
31
31
  unlimited: true;
32
32
  })) => Promise<EstablishSessionResult>;
33
33
  export declare const revokeSession: (options: {
34
- adapter: SessionAdapter;
34
+ context: SessionContext;
35
35
  session: Session;
36
36
  }) => Promise<TransactionResult | undefined>;
37
- export declare const reestablishSession: (adapter: SessionAdapter, walletPublicKey: PublicKey, sessionKey: CryptoKeyPair) => Promise<Session | undefined>;
37
+ export declare const reestablishSession: (context: SessionContext, walletPublicKey: PublicKey, sessionKey: CryptoKeyPair) => Promise<Session | undefined>;
38
38
  export declare const getSessionAccount: (connection: Connection, sessionPublicKey: PublicKey) => Promise<{
39
39
  authorizedPrograms: {
40
40
  type: AuthorizedProgramsType.All;
@@ -1302,11 +1302,11 @@ export type Session = {
1302
1302
  sessionKey: CryptoKeyPair;
1303
1303
  walletPublicKey: PublicKey;
1304
1304
  payer: PublicKey;
1305
- sendTransaction: (instructions: Parameters<SessionAdapter["sendTransaction"]>[1]) => Promise<TransactionResult>;
1305
+ sendTransaction: (instructions: Parameters<SessionContext["sendTransaction"]>[1]) => Promise<TransactionResult>;
1306
1306
  sessionInfo: NonNullable<z.infer<typeof sessionInfoSchema>>;
1307
1307
  };
1308
1308
  type SendTransferOptions = {
1309
- adapter: SessionAdapter;
1309
+ context: SessionContext;
1310
1310
  walletPublicKey: PublicKey;
1311
1311
  signMessage: (message: Uint8Array) => Promise<Uint8Array>;
1312
1312
  mint: PublicKey;
package/cjs/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.verifyLogInToken = exports.createLogInToken = exports.sendTransfer = exports.SessionResultType = exports.getDomainRecordAddress = exports.AuthorizedTokens = exports.AuthorizedProgramsType = exports.getSessionAccount = exports.reestablishSession = exports.revokeSession = exports.replaceSession = exports.establishSession = exports.TransactionResultType = exports.createSolanaWalletAdapter = void 0;
6
+ exports.verifyLogInToken = exports.createLogInToken = exports.sendTransfer = exports.SessionResultType = exports.getDomainRecordAddress = exports.AuthorizedTokens = exports.AuthorizedProgramsType = exports.getSessionAccount = exports.reestablishSession = exports.revokeSession = exports.replaceSession = exports.establishSession = exports.TransactionResultType = exports.createSessionContext = void 0;
7
7
  const anchor_1 = require("@coral-xyz/anchor");
8
8
  const sessions_idls_1 = require("@fogo/sessions-idls");
9
9
  const mpl_token_metadata_1 = require("@metaplex-foundation/mpl-token-metadata");
@@ -17,11 +17,11 @@ const web3_js_1 = require("@solana/web3.js");
17
17
  const bn_js_1 = __importDefault(require("bn.js"));
18
18
  const bs58_1 = __importDefault(require("bs58"));
19
19
  const zod_1 = require("zod");
20
- const adapter_js_1 = require("./adapter.js");
20
+ const context_js_1 = require("./context.js");
21
21
  const crypto_js_1 = require("./crypto.js");
22
- var adapter_js_2 = require("./adapter.js");
23
- Object.defineProperty(exports, "createSolanaWalletAdapter", { enumerable: true, get: function () { return adapter_js_2.createSolanaWalletAdapter; } });
24
- Object.defineProperty(exports, "TransactionResultType", { enumerable: true, get: function () { return adapter_js_2.TransactionResultType; } });
22
+ var context_js_2 = require("./context.js");
23
+ Object.defineProperty(exports, "createSessionContext", { enumerable: true, get: function () { return context_js_2.createSessionContext; } });
24
+ Object.defineProperty(exports, "TransactionResultType", { enumerable: true, get: function () { return context_js_2.TransactionResultType; } });
25
25
  const MESSAGE_HEADER = `Fogo Sessions:
26
26
  Signing this intent will allow this app to interact with your on-chain balances. Please make sure you trust this app and the domain in the message matches the domain of the current web application.
27
27
  `;
@@ -44,7 +44,7 @@ const establishSession = async (options) => {
44
44
  else {
45
45
  const filteredLimits = new Map(options.limits?.entries().filter(([, amount]) => amount > 0n));
46
46
  const tokenInfo = filteredLimits.size > 0
47
- ? await getTokenInfo(options.adapter, filteredLimits)
47
+ ? await getTokenInfo(options.context, filteredLimits)
48
48
  : [];
49
49
  const [intentInstruction, startSessionInstruction] = await Promise.all([
50
50
  buildIntentInstruction(options, sessionKey, tokenInfo),
@@ -59,15 +59,15 @@ const establishSession = async (options) => {
59
59
  };
60
60
  exports.establishSession = establishSession;
61
61
  const sendSessionEstablishTransaction = async (options, sessionKey, instructions) => {
62
- const result = await options.adapter.sendTransaction(sessionKey, instructions);
62
+ const result = await options.context.sendTransaction(sessionKey, instructions);
63
63
  switch (result.type) {
64
- case adapter_js_1.TransactionResultType.Success: {
65
- const session = await createSession(options.adapter, options.walletPublicKey, sessionKey);
64
+ case context_js_1.TransactionResultType.Success: {
65
+ const session = await createSession(options.context, options.walletPublicKey, sessionKey);
66
66
  return session
67
67
  ? EstablishSessionResult.Success(result.signature, session)
68
68
  : EstablishSessionResult.Failed(result.signature, new Error("Transaction succeeded, but the session was not created"));
69
69
  }
70
- case adapter_js_1.TransactionResultType.Failed: {
70
+ case context_js_1.TransactionResultType.Failed: {
71
71
  return EstablishSessionResult.Failed(result.signature, result.error);
72
72
  }
73
73
  }
@@ -79,14 +79,14 @@ const replaceSession = async (options) => (0, exports.establishSession)({
79
79
  exports.replaceSession = replaceSession;
80
80
  const revokeSession = async (options) => {
81
81
  if (options.session.sessionInfo.minor >= 2) {
82
- const instruction = await new sessions_idls_1.SessionManagerProgram(new anchor_1.AnchorProvider(options.adapter.connection, {}, {})).methods
82
+ const instruction = await new sessions_idls_1.SessionManagerProgram(new anchor_1.AnchorProvider(options.context.connection, {}, {})).methods
83
83
  .revokeSession()
84
84
  .accounts({
85
85
  sponsor: options.session.sessionInfo.sponsor,
86
86
  session: options.session.sessionPublicKey,
87
87
  })
88
88
  .instruction();
89
- return options.adapter.sendTransaction(options.session.sessionKey, [
89
+ return options.context.sendTransaction(options.session.sessionKey, [
90
90
  instruction,
91
91
  ]);
92
92
  }
@@ -95,7 +95,7 @@ const revokeSession = async (options) => {
95
95
  }
96
96
  };
97
97
  exports.revokeSession = revokeSession;
98
- const reestablishSession = async (adapter, walletPublicKey, sessionKey) => createSession(adapter, walletPublicKey, sessionKey);
98
+ const reestablishSession = async (context, walletPublicKey, sessionKey) => createSession(context, walletPublicKey, sessionKey);
99
99
  exports.reestablishSession = reestablishSession;
100
100
  const getSessionAccount = async (connection, sessionPublicKey) => {
101
101
  const result = await connection.getAccountInfo(sessionPublicKey, "confirmed");
@@ -104,17 +104,17 @@ const getSessionAccount = async (connection, sessionPublicKey) => {
104
104
  : sessionInfoSchema.parse(new anchor_1.BorshAccountsCoder(sessions_idls_1.SessionManagerIdl).decode("Session", result.data));
105
105
  };
106
106
  exports.getSessionAccount = getSessionAccount;
107
- const createSession = async (adapter, walletPublicKey, sessionKey) => {
107
+ const createSession = async (context, walletPublicKey, sessionKey) => {
108
108
  const sessionPublicKey = new web3_js_1.PublicKey(await (0, kit_1.getAddressFromPublicKey)(sessionKey.publicKey));
109
- const sessionInfo = await (0, exports.getSessionAccount)(adapter.connection, sessionPublicKey);
109
+ const sessionInfo = await (0, exports.getSessionAccount)(context.connection, sessionPublicKey);
110
110
  return sessionInfo === undefined
111
111
  ? undefined
112
112
  : {
113
113
  sessionPublicKey,
114
114
  walletPublicKey,
115
115
  sessionKey,
116
- payer: adapter.payer,
117
- sendTransaction: (instructions) => adapter.sendTransaction(sessionKey, instructions),
116
+ payer: context.payer,
117
+ sendTransaction: (instructions) => context.sendTransaction(sessionKey, instructions),
118
118
  sessionInfo,
119
119
  };
120
120
  };
@@ -299,13 +299,13 @@ const SymbolOrMint = {
299
299
  mint,
300
300
  }),
301
301
  };
302
- const getTokenInfo = async (adapter, limits) => {
303
- const umi = (0, umi_bundle_defaults_1.createUmi)(adapter.connection.rpcEndpoint);
302
+ const getTokenInfo = async (context, limits) => {
303
+ const umi = (0, umi_bundle_defaults_1.createUmi)(context.connection.rpcEndpoint);
304
304
  return Promise.all(limits.entries().map(async ([mint, amount]) => {
305
305
  const metaplexMint = (0, umi_1.publicKey)(mint.toBase58());
306
306
  const metadataAddress = (0, mpl_token_metadata_1.findMetadataPda)(umi, { mint: metaplexMint })[0];
307
307
  const [mintInfo, metadata] = await Promise.all([
308
- (0, spl_token_1.getMint)(adapter.connection, mint),
308
+ (0, spl_token_1.getMint)(context.connection, mint),
309
309
  (0, mpl_token_metadata_1.safeFetchMetadata)(umi, metadataAddress),
310
310
  ]);
311
311
  return {
@@ -351,8 +351,8 @@ const addOffchainMessagePrefixToMessageIfNeeded = async (walletPublicKey, signat
351
351
  };
352
352
  const buildIntentInstruction = async (options, sessionKey, tokens) => {
353
353
  const message = await buildMessage({
354
- chainId: options.adapter.chainId,
355
- domain: options.adapter.domain,
354
+ chainId: options.context.chainId,
355
+ domain: options.context.domain,
356
356
  sessionKey,
357
357
  expires: options.expires,
358
358
  tokens,
@@ -417,19 +417,19 @@ const amountToString = (amount, decimals) => {
417
417
  ...(decimalTruncated === "" ? [] : [".", decimalTruncated]),
418
418
  ].join("");
419
419
  };
420
- const buildCreateAssociatedTokenAccountInstructions = (options, tokens) => tokens.map(({ mint }) => (0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(options.adapter.payer, (0, spl_token_1.getAssociatedTokenAddressSync)(mint, options.walletPublicKey), options.walletPublicKey, mint));
420
+ const buildCreateAssociatedTokenAccountInstructions = (options, tokens) => tokens.map(({ mint }) => (0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(options.context.payer, (0, spl_token_1.getAssociatedTokenAddressSync)(mint, options.walletPublicKey), options.walletPublicKey, mint));
421
421
  const getDomainRecordAddress = (domain) => {
422
422
  const hash = (0, sha2_1.sha256)(domain);
423
423
  return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("domain-record"), hash], new web3_js_1.PublicKey(sessions_idls_1.DomainRegistryIdl.address))[0];
424
424
  };
425
425
  exports.getDomainRecordAddress = getDomainRecordAddress;
426
426
  const buildStartSessionInstruction = async (options, sessionKey, tokens) => {
427
- const instruction = new sessions_idls_1.SessionManagerProgram(new anchor_1.AnchorProvider(options.adapter.connection, {}, {})).methods
427
+ const instruction = new sessions_idls_1.SessionManagerProgram(new anchor_1.AnchorProvider(options.context.connection, {}, {})).methods
428
428
  .startSession()
429
429
  .accounts({
430
- sponsor: options.adapter.payer,
430
+ sponsor: options.context.payer,
431
431
  session: await (0, kit_1.getAddressFromPublicKey)(sessionKey.publicKey),
432
- domainRegistry: (0, exports.getDomainRecordAddress)(options.adapter.domain),
432
+ domainRegistry: (0, exports.getDomainRecordAddress)(options.context.domain),
433
433
  });
434
434
  return tokens === undefined
435
435
  ? instruction.instruction()
@@ -480,14 +480,14 @@ Signing this intent will transfer the tokens as described below.
480
480
  const sendTransfer = async (options) => {
481
481
  const sourceAta = (0, spl_token_1.getAssociatedTokenAddressSync)(options.mint, options.walletPublicKey);
482
482
  const destinationAta = (0, spl_token_1.getAssociatedTokenAddressSync)(options.mint, options.recipient);
483
- const program = new sessions_idls_1.IntentTransferProgram(new anchor_1.AnchorProvider(options.adapter.connection, {}, {}));
484
- const umi = (0, umi_bundle_defaults_1.createUmi)(options.adapter.connection.rpcEndpoint);
483
+ const program = new sessions_idls_1.IntentTransferProgram(new anchor_1.AnchorProvider(options.context.connection, {}, {}));
484
+ const umi = (0, umi_bundle_defaults_1.createUmi)(options.context.connection.rpcEndpoint);
485
485
  const metaplexMint = (0, umi_1.publicKey)(options.mint.toBase58());
486
486
  const metadataAddress = (0, mpl_token_metadata_1.findMetadataPda)(umi, { mint: metaplexMint })[0];
487
487
  const metadata = await (0, mpl_token_metadata_1.safeFetchMetadata)(umi, metadataAddress);
488
488
  const symbol = metadata?.symbol ?? undefined;
489
- return options.adapter.sendTransaction(undefined, [
490
- (0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(options.adapter.payer, destinationAta, options.recipient, options.mint),
489
+ return options.context.sendTransaction(undefined, [
490
+ (0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(options.context.payer, destinationAta, options.recipient, options.mint),
491
491
  await buildTransferIntentInstruction(program, options, symbol),
492
492
  await program.methods
493
493
  .sendTokens()
@@ -495,7 +495,7 @@ const sendTransfer = async (options) => {
495
495
  destination: destinationAta,
496
496
  mint: options.mint,
497
497
  source: sourceAta,
498
- sponsor: options.adapter.payer,
498
+ sponsor: options.context.payer,
499
499
  // eslint-disable-next-line unicorn/no-null
500
500
  metadata: symbol === undefined ? null : new web3_js_1.PublicKey(metadataAddress),
501
501
  })
@@ -506,13 +506,13 @@ exports.sendTransfer = sendTransfer;
506
506
  const buildTransferIntentInstruction = async (program, options, symbol) => {
507
507
  const [nonce, { decimals }] = await Promise.all([
508
508
  getNonce(program, options.walletPublicKey),
509
- (0, spl_token_1.getMint)(options.adapter.connection, options.mint),
509
+ (0, spl_token_1.getMint)(options.context.connection, options.mint),
510
510
  ]);
511
511
  const message = new TextEncoder().encode([
512
512
  TRANSFER_MESSAGE_HEADER,
513
513
  serializeKV({
514
514
  version: `${CURRENT_INTENT_TRANSFER_MAJOR}.${CURRENT_INTENT_TRANSFER_MINOR}`,
515
- chain_id: options.adapter.chainId,
515
+ chain_id: options.context.chainId,
516
516
  token: symbol ?? options.mint.toBase58(),
517
517
  amount: amountToString(options.amount, decimals),
518
518
  recipient: options.recipient.toBase58(),
@@ -1,7 +1,7 @@
1
1
  import type { Transaction, Instruction, TransactionWithLifetime } from "@solana/kit";
2
2
  import type { Connection, TransactionError } from "@solana/web3.js";
3
3
  import { PublicKey, TransactionInstruction, VersionedTransaction } from "@solana/web3.js";
4
- export type SessionAdapter = {
4
+ export type SessionContext = {
5
5
  chainId: string;
6
6
  connection: Connection;
7
7
  payer: PublicKey;
@@ -24,14 +24,17 @@ declare const TransactionResult: {
24
24
  };
25
25
  };
26
26
  export type TransactionResult = ReturnType<(typeof TransactionResult)[keyof typeof TransactionResult]>;
27
- export declare const createSolanaWalletAdapter: (options: {
27
+ export declare const createSessionContext: (options: {
28
28
  connection: Connection;
29
29
  addressLookupTableAddress?: string | undefined;
30
30
  domain?: string | undefined;
31
31
  } & ({
32
32
  paymaster?: string | URL | undefined;
33
+ sendToPaymaster?: undefined;
34
+ sponsor?: undefined;
33
35
  } | {
36
+ paymaster?: undefined;
34
37
  sendToPaymaster: (transaction: Transaction) => Promise<TransactionResult>;
35
38
  sponsor: PublicKey;
36
- })) => Promise<SessionAdapter>;
39
+ })) => Promise<SessionContext>;
37
40
  export {};
@@ -24,7 +24,7 @@ const TransactionResult = {
24
24
  error,
25
25
  }),
26
26
  };
27
- export const createSolanaWalletAdapter = async (options) => {
27
+ export const createSessionContext = async (options) => {
28
28
  const addressLookupTables = await getAddressLookupTables(options.connection, options.addressLookupTableAddress);
29
29
  const domain = getDomain(options.domain);
30
30
  const sponsor = await getSponsor(options, domain);
@@ -64,10 +64,7 @@ const buildTransaction = async (latestBlockhash, sessionKey, sponsor, instructio
64
64
  }
65
65
  };
66
66
  const getSponsor = async (options, domain) => {
67
- if ("sponsor" in options) {
68
- return options.sponsor;
69
- }
70
- else {
67
+ if (options.sponsor === undefined) {
71
68
  const url = new URL("/api/sponsor_pubkey", options.paymaster ?? DEFAULT_PAYMASTER);
72
69
  url.searchParams.set("domain", domain);
73
70
  const response = await fetch(url);
@@ -78,6 +75,9 @@ const getSponsor = async (options, domain) => {
78
75
  throw new PaymasterResponseError(response.status, await response.text());
79
76
  }
80
77
  }
78
+ else {
79
+ return options.sponsor;
80
+ }
81
81
  };
82
82
  const sponsorAndSendResponseSchema = z
83
83
  .discriminatedUnion("type", [
@@ -99,10 +99,7 @@ const sponsorAndSendResponseSchema = z
99
99
  : TransactionResult.Failed(data.signature, data.error);
100
100
  });
101
101
  const sendToPaymaster = async (options, transaction, domain) => {
102
- if ("sendToPaymaster" in options) {
103
- return options.sendToPaymaster(transaction);
104
- }
105
- else {
102
+ if (options.sendToPaymaster === undefined) {
106
103
  const url = new URL("/api/sponsor_and_send", options.paymaster ?? DEFAULT_PAYMASTER);
107
104
  url.searchParams.set("domain", domain);
108
105
  const response = await fetch(url, {
@@ -121,6 +118,9 @@ const sendToPaymaster = async (options, transaction, domain) => {
121
118
  throw new PaymasterResponseError(response.status, await response.text());
122
119
  }
123
120
  }
121
+ else {
122
+ return options.sendToPaymaster(transaction);
123
+ }
124
124
  };
125
125
  const getAddressLookupTables = async (connection, addressLookupTableAddress = DEFAULT_ADDRESS_LOOKUP_TABLE_ADDRESS) => {
126
126
  const addressLookupTableResult = await connection.getAddressLookupTable(new PublicKey(addressLookupTableAddress));
package/esm/index.d.ts CHANGED
@@ -2,10 +2,10 @@ import type { Connection, TransactionError } from "@solana/web3.js";
2
2
  import { PublicKey } from "@solana/web3.js";
3
3
  import BN from "bn.js";
4
4
  import { z } from "zod";
5
- import type { SessionAdapter, TransactionResult } from "./adapter.js";
6
- export { createSolanaWalletAdapter, TransactionResultType, type SessionAdapter, type TransactionResult, } from "./adapter.js";
5
+ import type { SessionContext, TransactionResult } from "./context.js";
6
+ export { createSessionContext, TransactionResultType, type SessionContext, type TransactionResult, } from "./context.js";
7
7
  type EstablishSessionOptions = {
8
- adapter: SessionAdapter;
8
+ context: SessionContext;
9
9
  walletPublicKey: PublicKey;
10
10
  signMessage: (message: Uint8Array) => Promise<Uint8Array>;
11
11
  expires: Date;
@@ -19,7 +19,7 @@ type EstablishSessionOptions = {
19
19
  });
20
20
  export declare const establishSession: (options: EstablishSessionOptions) => Promise<EstablishSessionResult>;
21
21
  export declare const replaceSession: (options: {
22
- adapter: SessionAdapter;
22
+ context: SessionContext;
23
23
  session: Session;
24
24
  signMessage: (message: Uint8Array) => Promise<Uint8Array>;
25
25
  expires: Date;
@@ -31,10 +31,10 @@ export declare const replaceSession: (options: {
31
31
  unlimited: true;
32
32
  })) => Promise<EstablishSessionResult>;
33
33
  export declare const revokeSession: (options: {
34
- adapter: SessionAdapter;
34
+ context: SessionContext;
35
35
  session: Session;
36
36
  }) => Promise<TransactionResult | undefined>;
37
- export declare const reestablishSession: (adapter: SessionAdapter, walletPublicKey: PublicKey, sessionKey: CryptoKeyPair) => Promise<Session | undefined>;
37
+ export declare const reestablishSession: (context: SessionContext, walletPublicKey: PublicKey, sessionKey: CryptoKeyPair) => Promise<Session | undefined>;
38
38
  export declare const getSessionAccount: (connection: Connection, sessionPublicKey: PublicKey) => Promise<{
39
39
  authorizedPrograms: {
40
40
  type: AuthorizedProgramsType.All;
@@ -1302,11 +1302,11 @@ export type Session = {
1302
1302
  sessionKey: CryptoKeyPair;
1303
1303
  walletPublicKey: PublicKey;
1304
1304
  payer: PublicKey;
1305
- sendTransaction: (instructions: Parameters<SessionAdapter["sendTransaction"]>[1]) => Promise<TransactionResult>;
1305
+ sendTransaction: (instructions: Parameters<SessionContext["sendTransaction"]>[1]) => Promise<TransactionResult>;
1306
1306
  sessionInfo: NonNullable<z.infer<typeof sessionInfoSchema>>;
1307
1307
  };
1308
1308
  type SendTransferOptions = {
1309
- adapter: SessionAdapter;
1309
+ context: SessionContext;
1310
1310
  walletPublicKey: PublicKey;
1311
1311
  signMessage: (message: Uint8Array) => Promise<Uint8Array>;
1312
1312
  mint: PublicKey;
package/esm/index.js CHANGED
@@ -11,9 +11,9 @@ import { Ed25519Program, PublicKey } from "@solana/web3.js";
11
11
  import BN from "bn.js";
12
12
  import bs58 from "bs58";
13
13
  import { z } from "zod";
14
- import { TransactionResultType } from "./adapter.js";
14
+ import { TransactionResultType } from "./context.js";
15
15
  import { importKey, signMessageWithKey, verifyMessageWithKey, } from "./crypto.js";
16
- export { createSolanaWalletAdapter, TransactionResultType, } from "./adapter.js";
16
+ export { createSessionContext, TransactionResultType, } from "./context.js";
17
17
  const MESSAGE_HEADER = `Fogo Sessions:
18
18
  Signing this intent will allow this app to interact with your on-chain balances. Please make sure you trust this app and the domain in the message matches the domain of the current web application.
19
19
  `;
@@ -36,7 +36,7 @@ export const establishSession = async (options) => {
36
36
  else {
37
37
  const filteredLimits = new Map(options.limits?.entries().filter(([, amount]) => amount > 0n));
38
38
  const tokenInfo = filteredLimits.size > 0
39
- ? await getTokenInfo(options.adapter, filteredLimits)
39
+ ? await getTokenInfo(options.context, filteredLimits)
40
40
  : [];
41
41
  const [intentInstruction, startSessionInstruction] = await Promise.all([
42
42
  buildIntentInstruction(options, sessionKey, tokenInfo),
@@ -50,10 +50,10 @@ export const establishSession = async (options) => {
50
50
  }
51
51
  };
52
52
  const sendSessionEstablishTransaction = async (options, sessionKey, instructions) => {
53
- const result = await options.adapter.sendTransaction(sessionKey, instructions);
53
+ const result = await options.context.sendTransaction(sessionKey, instructions);
54
54
  switch (result.type) {
55
55
  case TransactionResultType.Success: {
56
- const session = await createSession(options.adapter, options.walletPublicKey, sessionKey);
56
+ const session = await createSession(options.context, options.walletPublicKey, sessionKey);
57
57
  return session
58
58
  ? EstablishSessionResult.Success(result.signature, session)
59
59
  : EstablishSessionResult.Failed(result.signature, new Error("Transaction succeeded, but the session was not created"));
@@ -69,14 +69,14 @@ export const replaceSession = async (options) => establishSession({
69
69
  });
70
70
  export const revokeSession = async (options) => {
71
71
  if (options.session.sessionInfo.minor >= 2) {
72
- const instruction = await new SessionManagerProgram(new AnchorProvider(options.adapter.connection, {}, {})).methods
72
+ const instruction = await new SessionManagerProgram(new AnchorProvider(options.context.connection, {}, {})).methods
73
73
  .revokeSession()
74
74
  .accounts({
75
75
  sponsor: options.session.sessionInfo.sponsor,
76
76
  session: options.session.sessionPublicKey,
77
77
  })
78
78
  .instruction();
79
- return options.adapter.sendTransaction(options.session.sessionKey, [
79
+ return options.context.sendTransaction(options.session.sessionKey, [
80
80
  instruction,
81
81
  ]);
82
82
  }
@@ -84,24 +84,24 @@ export const revokeSession = async (options) => {
84
84
  return;
85
85
  }
86
86
  };
87
- export const reestablishSession = async (adapter, walletPublicKey, sessionKey) => createSession(adapter, walletPublicKey, sessionKey);
87
+ export const reestablishSession = async (context, walletPublicKey, sessionKey) => createSession(context, walletPublicKey, sessionKey);
88
88
  export const getSessionAccount = async (connection, sessionPublicKey) => {
89
89
  const result = await connection.getAccountInfo(sessionPublicKey, "confirmed");
90
90
  return result === null
91
91
  ? undefined
92
92
  : sessionInfoSchema.parse(new BorshAccountsCoder(SessionManagerIdl).decode("Session", result.data));
93
93
  };
94
- const createSession = async (adapter, walletPublicKey, sessionKey) => {
94
+ const createSession = async (context, walletPublicKey, sessionKey) => {
95
95
  const sessionPublicKey = new PublicKey(await getAddressFromPublicKey(sessionKey.publicKey));
96
- const sessionInfo = await getSessionAccount(adapter.connection, sessionPublicKey);
96
+ const sessionInfo = await getSessionAccount(context.connection, sessionPublicKey);
97
97
  return sessionInfo === undefined
98
98
  ? undefined
99
99
  : {
100
100
  sessionPublicKey,
101
101
  walletPublicKey,
102
102
  sessionKey,
103
- payer: adapter.payer,
104
- sendTransaction: (instructions) => adapter.sendTransaction(sessionKey, instructions),
103
+ payer: context.payer,
104
+ sendTransaction: (instructions) => context.sendTransaction(sessionKey, instructions),
105
105
  sessionInfo,
106
106
  };
107
107
  };
@@ -286,13 +286,13 @@ const SymbolOrMint = {
286
286
  mint,
287
287
  }),
288
288
  };
289
- const getTokenInfo = async (adapter, limits) => {
290
- const umi = createUmi(adapter.connection.rpcEndpoint);
289
+ const getTokenInfo = async (context, limits) => {
290
+ const umi = createUmi(context.connection.rpcEndpoint);
291
291
  return Promise.all(limits.entries().map(async ([mint, amount]) => {
292
292
  const metaplexMint = metaplexPublicKey(mint.toBase58());
293
293
  const metadataAddress = findMetadataPda(umi, { mint: metaplexMint })[0];
294
294
  const [mintInfo, metadata] = await Promise.all([
295
- getMint(adapter.connection, mint),
295
+ getMint(context.connection, mint),
296
296
  safeFetchMetadata(umi, metadataAddress),
297
297
  ]);
298
298
  return {
@@ -338,8 +338,8 @@ const addOffchainMessagePrefixToMessageIfNeeded = async (walletPublicKey, signat
338
338
  };
339
339
  const buildIntentInstruction = async (options, sessionKey, tokens) => {
340
340
  const message = await buildMessage({
341
- chainId: options.adapter.chainId,
342
- domain: options.adapter.domain,
341
+ chainId: options.context.chainId,
342
+ domain: options.context.domain,
343
343
  sessionKey,
344
344
  expires: options.expires,
345
345
  tokens,
@@ -404,18 +404,18 @@ const amountToString = (amount, decimals) => {
404
404
  ...(decimalTruncated === "" ? [] : [".", decimalTruncated]),
405
405
  ].join("");
406
406
  };
407
- const buildCreateAssociatedTokenAccountInstructions = (options, tokens) => tokens.map(({ mint }) => createAssociatedTokenAccountIdempotentInstruction(options.adapter.payer, getAssociatedTokenAddressSync(mint, options.walletPublicKey), options.walletPublicKey, mint));
407
+ const buildCreateAssociatedTokenAccountInstructions = (options, tokens) => tokens.map(({ mint }) => createAssociatedTokenAccountIdempotentInstruction(options.context.payer, getAssociatedTokenAddressSync(mint, options.walletPublicKey), options.walletPublicKey, mint));
408
408
  export const getDomainRecordAddress = (domain) => {
409
409
  const hash = sha256(domain);
410
410
  return PublicKey.findProgramAddressSync([Buffer.from("domain-record"), hash], new PublicKey(DomainRegistryIdl.address))[0];
411
411
  };
412
412
  const buildStartSessionInstruction = async (options, sessionKey, tokens) => {
413
- const instruction = new SessionManagerProgram(new AnchorProvider(options.adapter.connection, {}, {})).methods
413
+ const instruction = new SessionManagerProgram(new AnchorProvider(options.context.connection, {}, {})).methods
414
414
  .startSession()
415
415
  .accounts({
416
- sponsor: options.adapter.payer,
416
+ sponsor: options.context.payer,
417
417
  session: await getAddressFromPublicKey(sessionKey.publicKey),
418
- domainRegistry: getDomainRecordAddress(options.adapter.domain),
418
+ domainRegistry: getDomainRecordAddress(options.context.domain),
419
419
  });
420
420
  return tokens === undefined
421
421
  ? instruction.instruction()
@@ -466,14 +466,14 @@ Signing this intent will transfer the tokens as described below.
466
466
  export const sendTransfer = async (options) => {
467
467
  const sourceAta = getAssociatedTokenAddressSync(options.mint, options.walletPublicKey);
468
468
  const destinationAta = getAssociatedTokenAddressSync(options.mint, options.recipient);
469
- const program = new IntentTransferProgram(new AnchorProvider(options.adapter.connection, {}, {}));
470
- const umi = createUmi(options.adapter.connection.rpcEndpoint);
469
+ const program = new IntentTransferProgram(new AnchorProvider(options.context.connection, {}, {}));
470
+ const umi = createUmi(options.context.connection.rpcEndpoint);
471
471
  const metaplexMint = metaplexPublicKey(options.mint.toBase58());
472
472
  const metadataAddress = findMetadataPda(umi, { mint: metaplexMint })[0];
473
473
  const metadata = await safeFetchMetadata(umi, metadataAddress);
474
474
  const symbol = metadata?.symbol ?? undefined;
475
- return options.adapter.sendTransaction(undefined, [
476
- createAssociatedTokenAccountIdempotentInstruction(options.adapter.payer, destinationAta, options.recipient, options.mint),
475
+ return options.context.sendTransaction(undefined, [
476
+ createAssociatedTokenAccountIdempotentInstruction(options.context.payer, destinationAta, options.recipient, options.mint),
477
477
  await buildTransferIntentInstruction(program, options, symbol),
478
478
  await program.methods
479
479
  .sendTokens()
@@ -481,7 +481,7 @@ export const sendTransfer = async (options) => {
481
481
  destination: destinationAta,
482
482
  mint: options.mint,
483
483
  source: sourceAta,
484
- sponsor: options.adapter.payer,
484
+ sponsor: options.context.payer,
485
485
  // eslint-disable-next-line unicorn/no-null
486
486
  metadata: symbol === undefined ? null : new PublicKey(metadataAddress),
487
487
  })
@@ -491,13 +491,13 @@ export const sendTransfer = async (options) => {
491
491
  const buildTransferIntentInstruction = async (program, options, symbol) => {
492
492
  const [nonce, { decimals }] = await Promise.all([
493
493
  getNonce(program, options.walletPublicKey),
494
- getMint(options.adapter.connection, options.mint),
494
+ getMint(options.context.connection, options.mint),
495
495
  ]);
496
496
  const message = new TextEncoder().encode([
497
497
  TRANSFER_MESSAGE_HEADER,
498
498
  serializeKV({
499
499
  version: `${CURRENT_INTENT_TRANSFER_MAJOR}.${CURRENT_INTENT_TRANSFER_MINOR}`,
500
- chain_id: options.adapter.chainId,
500
+ chain_id: options.context.chainId,
501
501
  token: symbol ?? options.mint.toBase58(),
502
502
  amount: amountToString(options.amount, decimals),
503
503
  recipient: options.recipient.toBase58(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fogo/sessions-sdk",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "A set of utilities for integrating with Fogo sessions",
5
5
  "keywords": [
6
6
  "fogo",
@@ -39,6 +39,6 @@
39
39
  "bn.js": "^5.1.2",
40
40
  "bs58": "^6.0.0",
41
41
  "zod": "^3.25.62",
42
- "@fogo/sessions-idls": "^0.0.6"
42
+ "@fogo/sessions-idls": "^0.0.7"
43
43
  }
44
44
  }