@fogo/sessions-sdk 0.1.0 → 0.1.1
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/connection.d.ts +2 -5
- package/cjs/connection.js +74 -18
- package/cjs/context.d.ts +5 -5
- package/cjs/context.js +1 -1
- package/cjs/index.d.ts +148 -146
- package/cjs/index.js +23 -21
- package/cjs/mints.d.ts +5 -0
- package/cjs/mints.js +9 -0
- package/cjs/network.d.ts +4 -0
- package/cjs/network.js +8 -0
- package/esm/connection.d.ts +2 -5
- package/esm/connection.js +68 -12
- package/esm/context.d.ts +5 -5
- package/esm/context.js +2 -2
- package/esm/index.d.ts +148 -146
- package/esm/index.js +12 -13
- package/esm/mints.d.ts +5 -0
- package/esm/mints.js +6 -0
- package/esm/network.d.ts +4 -0
- package/esm/network.js +5 -0
- package/package.json +1 -1
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.bridgeIn = exports.bridgeOut = exports.sendNativeTransfer = exports.sendTransfer = exports.getBridgeOutFee = exports.getTransferFee = exports.SessionResultType = exports.getDomainRecordAddress = exports.AuthorizedTokens = exports.AuthorizedProgramsType = exports.getSessionAccount = exports.reestablishSession = exports.revokeSession = exports.replaceSession = exports.establishSession = exports.
|
|
6
|
+
exports.verifyLogInToken = exports.createLogInToken = exports.bridgeIn = exports.bridgeOut = exports.sendNativeTransfer = exports.sendTransfer = exports.getBridgeOutFee = exports.getTransferFee = exports.SessionResultType = exports.getDomainRecordAddress = exports.AuthorizedTokens = exports.AuthorizedProgramsType = exports.getSessionAccount = exports.reestablishSession = exports.revokeSession = exports.replaceSession = exports.establishSession = exports.Network = exports.createSystemProgramSessionWrapInstruction = exports.createSessionWrapInstructions = exports.createSessionUnwrapInstruction = exports.createSessionContext = exports.TransactionResultType = exports.createSessionConnection = 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");
|
|
@@ -27,12 +27,19 @@ const connection_js_1 = require("./connection.js");
|
|
|
27
27
|
const context_js_1 = require("./context.js");
|
|
28
28
|
const crypto_js_1 = require("./crypto.js");
|
|
29
29
|
const instructions_js_1 = require("./instructions.js");
|
|
30
|
+
const mints_js_1 = require("./mints.js");
|
|
31
|
+
const network_js_1 = require("./network.js");
|
|
30
32
|
var connection_js_2 = require("./connection.js");
|
|
31
33
|
Object.defineProperty(exports, "createSessionConnection", { enumerable: true, get: function () { return connection_js_2.createSessionConnection; } });
|
|
32
|
-
Object.defineProperty(exports, "Network", { enumerable: true, get: function () { return connection_js_2.Network; } });
|
|
33
34
|
Object.defineProperty(exports, "TransactionResultType", { enumerable: true, get: function () { return connection_js_2.TransactionResultType; } });
|
|
34
35
|
var context_js_2 = require("./context.js");
|
|
35
36
|
Object.defineProperty(exports, "createSessionContext", { enumerable: true, get: function () { return context_js_2.createSessionContext; } });
|
|
37
|
+
var instructions_js_2 = require("./instructions.js");
|
|
38
|
+
Object.defineProperty(exports, "createSessionUnwrapInstruction", { enumerable: true, get: function () { return instructions_js_2.createSessionUnwrapInstruction; } });
|
|
39
|
+
Object.defineProperty(exports, "createSessionWrapInstructions", { enumerable: true, get: function () { return instructions_js_2.createSessionWrapInstructions; } });
|
|
40
|
+
Object.defineProperty(exports, "createSystemProgramSessionWrapInstruction", { enumerable: true, get: function () { return instructions_js_2.createSystemProgramSessionWrapInstruction; } });
|
|
41
|
+
var network_js_2 = require("./network.js");
|
|
42
|
+
Object.defineProperty(exports, "Network", { enumerable: true, get: function () { return network_js_2.Network; } });
|
|
36
43
|
const MESSAGE_HEADER = `Fogo Sessions:
|
|
37
44
|
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.
|
|
38
45
|
`;
|
|
@@ -45,8 +52,8 @@ const CURRENT_INTENT_TRANSFER_MINOR = "2";
|
|
|
45
52
|
const CURRENT_BRIDGE_OUT_MAJOR = "0";
|
|
46
53
|
const CURRENT_BRIDGE_OUT_MINOR = "2";
|
|
47
54
|
const SESSION_ESTABLISHMENT_LOOKUP_TABLE_ADDRESS = {
|
|
48
|
-
[
|
|
49
|
-
[
|
|
55
|
+
[network_js_1.Network.Testnet]: "B8cUjJMqaWWTNNSTXBmeptjWswwCH1gTSCRYv4nu7kJW",
|
|
56
|
+
[network_js_1.Network.Mainnet]: undefined,
|
|
50
57
|
};
|
|
51
58
|
const establishSession = async (options) => {
|
|
52
59
|
const sessionKey = options.createUnsafeExtractableSessionKey
|
|
@@ -72,7 +79,7 @@ const establishSession = async (options) => {
|
|
|
72
79
|
};
|
|
73
80
|
exports.establishSession = establishSession;
|
|
74
81
|
const sendSessionEstablishTransaction = async (options, sessionKey, instructions, sessionEstablishmentLookupTable) => {
|
|
75
|
-
const result = await options.context.sendTransaction(sessionKey, instructions, {
|
|
82
|
+
const result = await options.context.sendTransaction(sessionKey, instructions, options.walletPublicKey, {
|
|
76
83
|
variation: "Session Establishment",
|
|
77
84
|
addressLookupTable: sessionEstablishmentLookupTable ??
|
|
78
85
|
SESSION_ESTABLISHMENT_LOOKUP_TABLE_ADDRESS[options.context.network],
|
|
@@ -103,7 +110,7 @@ const revokeSession = async (options) => {
|
|
|
103
110
|
session: options.session.sessionPublicKey,
|
|
104
111
|
})
|
|
105
112
|
.instruction();
|
|
106
|
-
return options.context.sendTransaction(options.session.sessionKey, [instruction], {
|
|
113
|
+
return options.context.sendTransaction(options.session.sessionKey, [instruction], options.session.walletPublicKey, {
|
|
107
114
|
variation: "Session Revocation",
|
|
108
115
|
});
|
|
109
116
|
}
|
|
@@ -136,7 +143,7 @@ const createSession = async (context, walletPublicKey, sessionKey) => {
|
|
|
136
143
|
getSessionUnwrapInstructions: () => [
|
|
137
144
|
(0, instructions_js_1.createSessionUnwrapInstruction)(sessionPublicKey, walletPublicKey),
|
|
138
145
|
],
|
|
139
|
-
sendTransaction: (instructions, extraConfig) => context.sendTransaction(sessionKey, instructions, extraConfig),
|
|
146
|
+
sendTransaction: (instructions, extraConfig) => context.sendTransaction(sessionKey, instructions, walletPublicKey, extraConfig),
|
|
140
147
|
sessionInfo,
|
|
141
148
|
};
|
|
142
149
|
};
|
|
@@ -443,11 +450,11 @@ const getDomainRecordAddress = (domain) => {
|
|
|
443
450
|
};
|
|
444
451
|
exports.getDomainRecordAddress = getDomainRecordAddress;
|
|
445
452
|
const BRIDGING_ADDRESS_LOOKUP_TABLE = {
|
|
446
|
-
[
|
|
453
|
+
[network_js_1.Network.Testnet]: {
|
|
447
454
|
// USDC
|
|
448
455
|
ELNbJ1RtERV2fjtuZjbTscDekWhVzkQ1LjmiPsxp5uND: "4FCi6LptexBdZtaePsoCMeb1XpCijxnWu96g5LsSb6WP",
|
|
449
456
|
},
|
|
450
|
-
[
|
|
457
|
+
[network_js_1.Network.Mainnet]: {
|
|
451
458
|
// USDC
|
|
452
459
|
uSd2czE61Evaf76RNbq4KPpXnkiL3irdzgLFUMe3NoG: "7hmMz3nZDnPJfksLuPotKmUBAFDneM2D9wWg3R1VcKSv",
|
|
453
460
|
},
|
|
@@ -503,11 +510,6 @@ const EstablishSessionResult = {
|
|
|
503
510
|
error,
|
|
504
511
|
}),
|
|
505
512
|
};
|
|
506
|
-
const USDC_MINT = {
|
|
507
|
-
[connection_js_1.Network.Mainnet]: "uSd2czE61Evaf76RNbq4KPpXnkiL3irdzgLFUMe3NoG",
|
|
508
|
-
[connection_js_1.Network.Testnet]: "ELNbJ1RtERV2fjtuZjbTscDekWhVzkQ1LjmiPsxp5uND",
|
|
509
|
-
};
|
|
510
|
-
const USDC_DECIMALS = 6;
|
|
511
513
|
const getTransferFee = async (context) => {
|
|
512
514
|
const { fee, ...config } = await getFee(context);
|
|
513
515
|
return {
|
|
@@ -527,7 +529,7 @@ exports.getBridgeOutFee = getBridgeOutFee;
|
|
|
527
529
|
const getFee = async (context) => {
|
|
528
530
|
const program = new sessions_idls_1.IntentTransferProgram(new anchor_1.AnchorProvider(context.connection, {}, {}));
|
|
529
531
|
const umi = (0, umi_bundle_defaults_1.createUmi)(context.connection.rpcEndpoint);
|
|
530
|
-
const usdcMintAddress = USDC_MINT[context.network];
|
|
532
|
+
const usdcMintAddress = mints_js_1.USDC_MINT[context.network];
|
|
531
533
|
const usdcMint = new web3_js_1.PublicKey(usdcMintAddress);
|
|
532
534
|
const [feeConfigPda] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("fee_config"), usdcMint.toBytes()], program.programId);
|
|
533
535
|
const feeConfig = await program.account.feeConfig.fetch(feeConfigPda);
|
|
@@ -537,7 +539,7 @@ const getFee = async (context) => {
|
|
|
537
539
|
})[0],
|
|
538
540
|
mint: usdcMint,
|
|
539
541
|
symbolOrMint: "USDC.s",
|
|
540
|
-
decimals: USDC_DECIMALS,
|
|
542
|
+
decimals: mints_js_1.USDC_DECIMALS,
|
|
541
543
|
fee: {
|
|
542
544
|
intrachainTransfer: BigInt(feeConfig.intrachainTransferFee.toString()),
|
|
543
545
|
bridgeTransfer: BigInt(feeConfig.bridgeTransferFee.toString()),
|
|
@@ -572,7 +574,7 @@ const sendTransfer = async (options) => {
|
|
|
572
574
|
symbol === undefined ? null : new web3_js_1.PublicKey(metadataAddress),
|
|
573
575
|
})
|
|
574
576
|
.instruction(),
|
|
575
|
-
], {
|
|
577
|
+
], options.walletPublicKey, {
|
|
576
578
|
variation: "Intent Transfer",
|
|
577
579
|
paymasterDomain: context_js_1.SESSIONS_INTERNAL_PAYMASTER_DOMAIN,
|
|
578
580
|
});
|
|
@@ -609,7 +611,7 @@ const sendNativeTransfer = async (options) => {
|
|
|
609
611
|
sponsor: options.context.internalPayer,
|
|
610
612
|
})
|
|
611
613
|
.instruction(),
|
|
612
|
-
], {
|
|
614
|
+
], options.walletPublicKey, {
|
|
613
615
|
variation: "Intent Transfer",
|
|
614
616
|
paymasterDomain: context_js_1.SESSIONS_INTERNAL_PAYMASTER_DOMAIN,
|
|
615
617
|
});
|
|
@@ -673,7 +675,7 @@ const bridgeOut = async (options) => {
|
|
|
673
675
|
return options.context.sendTransaction(options.sessionKey, [
|
|
674
676
|
web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: BRIDGE_OUT_CUS }),
|
|
675
677
|
...instructions,
|
|
676
|
-
], {
|
|
678
|
+
], options.walletPublicKey, {
|
|
677
679
|
variation: "Intent NTT Bridge",
|
|
678
680
|
paymasterDomain: context_js_1.SESSIONS_INTERNAL_PAYMASTER_DOMAIN,
|
|
679
681
|
extraSigners: [outboxItem],
|
|
@@ -830,8 +832,8 @@ const buildWormholeTransfer = async (options, connection) => {
|
|
|
830
832
|
}
|
|
831
833
|
};
|
|
832
834
|
const NETWORK_TO_WORMHOLE_NETWORK = {
|
|
833
|
-
[
|
|
834
|
-
[
|
|
835
|
+
[network_js_1.Network.Mainnet]: "Mainnet",
|
|
836
|
+
[network_js_1.Network.Testnet]: "Testnet",
|
|
835
837
|
};
|
|
836
838
|
const getNonce = async (program, walletPublicKey, nonceType) => {
|
|
837
839
|
const [noncePda] = await (0, kit_1.getProgramDerivedAddress)({
|
package/cjs/mints.d.ts
ADDED
package/cjs/mints.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.USDC_DECIMALS = exports.USDC_MINT = void 0;
|
|
4
|
+
const network_js_1 = require("./network.js");
|
|
5
|
+
exports.USDC_MINT = {
|
|
6
|
+
[network_js_1.Network.Mainnet]: "uSd2czE61Evaf76RNbq4KPpXnkiL3irdzgLFUMe3NoG",
|
|
7
|
+
[network_js_1.Network.Testnet]: "ELNbJ1RtERV2fjtuZjbTscDekWhVzkQ1LjmiPsxp5uND",
|
|
8
|
+
};
|
|
9
|
+
exports.USDC_DECIMALS = 6;
|
package/cjs/network.d.ts
ADDED
package/cjs/network.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Network = void 0;
|
|
4
|
+
var Network;
|
|
5
|
+
(function (Network) {
|
|
6
|
+
Network[Network["Testnet"] = 0] = "Testnet";
|
|
7
|
+
Network[Network["Mainnet"] = 1] = "Mainnet";
|
|
8
|
+
})(Network || (exports.Network = Network = {}));
|
package/esm/connection.d.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import type { GetLatestBlockhashApi, Instruction, Rpc, Transaction, TransactionWithLifetime } from "@solana/kit";
|
|
2
2
|
import type { TransactionError } from "@solana/web3.js";
|
|
3
3
|
import { Keypair, PublicKey, TransactionInstruction, VersionedTransaction, Connection as Web3Connection } from "@solana/web3.js";
|
|
4
|
-
|
|
5
|
-
Testnet = 0,
|
|
6
|
-
Mainnet = 1
|
|
7
|
-
}
|
|
4
|
+
import { Network } from "./network.js";
|
|
8
5
|
export declare enum TransactionResultType {
|
|
9
6
|
Success = 0,
|
|
10
7
|
Failed = 1
|
|
@@ -37,7 +34,7 @@ export declare const createSessionConnection: (options: {
|
|
|
37
34
|
connection: Web3Connection;
|
|
38
35
|
network: Network;
|
|
39
36
|
getSolanaConnection: () => Promise<Web3Connection>;
|
|
40
|
-
sendToPaymaster: (domain: string, sessionKey: CryptoKeyPair | undefined, instructions: TransactionOrInstructions, extraConfig?: SendTransactionOptions) => Promise<TransactionResult>;
|
|
37
|
+
sendToPaymaster: (domain: string, sessionKey: CryptoKeyPair | undefined, instructions: TransactionOrInstructions, walletPublicKey: PublicKey, extraConfig?: SendTransactionOptions) => Promise<TransactionResult>;
|
|
41
38
|
getSponsor: (domain: string) => Promise<PublicKey>;
|
|
42
39
|
};
|
|
43
40
|
export type TransactionOrInstructions = (TransactionInstruction | Instruction)[] | VersionedTransaction | (Transaction & TransactionWithLifetime);
|
package/esm/connection.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
import { AnchorProvider, BN, Wallet } from "@coral-xyz/anchor";
|
|
2
|
+
import { TollboothIdl, TollboothProgram } from "@fogo/sessions-idls";
|
|
3
|
+
import { sha256 } from "@noble/hashes/sha2";
|
|
1
4
|
import { fromLegacyKeypair, fromLegacyPublicKey, fromLegacyTransactionInstruction, fromVersionedTransaction, } from "@solana/compat";
|
|
2
|
-
import { addSignersToTransactionMessage, appendTransactionMessageInstructions, compressTransactionMessageUsingAddressLookupTables, createSignerFromKeyPair, createSolanaRpc, createTransactionMessage, getBase64EncodedWireTransaction, partiallySignTransaction, partiallySignTransactionMessageWithSigners, pipe, setTransactionMessageFeePayer, setTransactionMessageLifetimeUsingBlockhash, } from "@solana/kit";
|
|
5
|
+
import { addSignersToTransactionMessage, appendTransactionMessageInstructions, compressTransactionMessageUsingAddressLookupTables, createSignerFromKeyPair, createSolanaRpc, createTransactionMessage, getAddressFromPublicKey, getBase64EncodedWireTransaction, partiallySignTransaction, partiallySignTransactionMessageWithSigners, pipe, setTransactionMessageFeePayer, setTransactionMessageLifetimeUsingBlockhash, } from "@solana/kit";
|
|
6
|
+
import { getAssociatedTokenAddressSync } from "@solana/spl-token";
|
|
3
7
|
import { Keypair, PublicKey, TransactionInstruction, VersionedTransaction, Connection as Web3Connection, } from "@solana/web3.js";
|
|
4
8
|
import { z } from "zod";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Network[Network["Testnet"] = 0] = "Testnet";
|
|
8
|
-
Network[Network["Mainnet"] = 1] = "Mainnet";
|
|
9
|
-
})(Network || (Network = {}));
|
|
9
|
+
import { USDC_MINT } from "./mints.js";
|
|
10
|
+
import { Network } from "./network.js";
|
|
10
11
|
const DEFAULT_RPC = {
|
|
11
12
|
[Network.Testnet]: "https://testnet.fogo.io",
|
|
12
13
|
[Network.Mainnet]: "https://mainnet.fogo.io",
|
|
@@ -42,7 +43,7 @@ export const createSessionConnection = (options) => {
|
|
|
42
43
|
connection,
|
|
43
44
|
network: options.network,
|
|
44
45
|
getSolanaConnection: createSolanaConnectionGetter(options.network),
|
|
45
|
-
sendToPaymaster: async (domain, sessionKey, instructions, extraConfig) => sendToPaymaster({ ...options, rpc, connection, addressLookupTableCache, sponsorCache }, domain, sessionKey, instructions, extraConfig),
|
|
46
|
+
sendToPaymaster: async (domain, sessionKey, instructions, walletPublicKey, extraConfig) => sendToPaymaster({ ...options, rpc, connection, addressLookupTableCache, sponsorCache }, domain, sessionKey, instructions, walletPublicKey, extraConfig),
|
|
46
47
|
getSponsor: (domain) => getSponsor(options, sponsorCache, domain),
|
|
47
48
|
};
|
|
48
49
|
};
|
|
@@ -68,10 +69,10 @@ const NETWORK_TO_QUERY_PARAM = {
|
|
|
68
69
|
[Network.Mainnet]: "mainnet",
|
|
69
70
|
[Network.Testnet]: "testnet",
|
|
70
71
|
};
|
|
71
|
-
const sendToPaymaster = async (connection, domain, sessionKey, instructions, extraConfig) => {
|
|
72
|
+
const sendToPaymaster = async (connection, domain, sessionKey, instructions, walletPublicKey, extraConfig) => {
|
|
72
73
|
const signerKeys = await getSignerKeys(sessionKey, extraConfig?.extraSigners);
|
|
73
74
|
const transaction = Array.isArray(instructions)
|
|
74
|
-
? await buildTransaction(connection, domain, signerKeys, instructions, extraConfig)
|
|
75
|
+
? await buildTransaction(connection, domain, sessionKey, signerKeys, instructions, walletPublicKey, extraConfig)
|
|
75
76
|
: await addSignaturesToExistingTransaction(instructions, signerKeys);
|
|
76
77
|
if (connection.sendToPaymaster === undefined) {
|
|
77
78
|
const url = new URL("/api/sponsor_and_send", connection.paymaster ?? DEFAULT_PAYMASTER[connection.network]);
|
|
@@ -99,8 +100,9 @@ const sendToPaymaster = async (connection, domain, sessionKey, instructions, ext
|
|
|
99
100
|
return connection.sendToPaymaster(transaction);
|
|
100
101
|
}
|
|
101
102
|
};
|
|
102
|
-
const buildTransaction = async (connection, domain, signerKeys, instructions, extraConfig) => {
|
|
103
|
-
const
|
|
103
|
+
const buildTransaction = async (connection, domain, sessionKey, signerKeys, instructions, walletPublicKey, extraConfig) => {
|
|
104
|
+
const feeMint = new PublicKey(USDC_MINT[connection.network]); // TODO: make this configurable
|
|
105
|
+
const [{ value: latestBlockhash }, sponsor, addressLookupTable, signers, feeAmount, sessionKeyAddress,] = await Promise.all([
|
|
104
106
|
connection.rpc.getLatestBlockhash().send(),
|
|
105
107
|
connection.sponsor === undefined
|
|
106
108
|
? getSponsor(connection, connection.sponsorCache, domain)
|
|
@@ -109,15 +111,51 @@ const buildTransaction = async (connection, domain, signerKeys, instructions, ex
|
|
|
109
111
|
? Promise.resolve(undefined)
|
|
110
112
|
: getAddressLookupTable(connection.connection, connection.addressLookupTableCache, extraConfig.addressLookupTable),
|
|
111
113
|
Promise.all(signerKeys.map((signer) => createSignerFromKeyPair(signer))),
|
|
114
|
+
getFee(connection, domain, extraConfig?.variation, feeMint),
|
|
115
|
+
sessionKey === undefined
|
|
116
|
+
? Promise.resolve(undefined)
|
|
117
|
+
: getAddressFromPublicKey(sessionKey.publicKey),
|
|
112
118
|
]);
|
|
119
|
+
const tollboothInstruction = await buildTollboothInstructionIfNeeded({
|
|
120
|
+
sessionKeyAddress,
|
|
121
|
+
walletPublicKey,
|
|
122
|
+
domain,
|
|
123
|
+
feeMint,
|
|
124
|
+
feeAmount,
|
|
125
|
+
});
|
|
113
126
|
return partiallySignTransactionMessageWithSigners(pipe(createTransactionMessage({ version: 0 }), (tx) => setTransactionMessageFeePayer(fromLegacyPublicKey(sponsor), tx), (tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx), (tx) => appendTransactionMessageInstructions(instructions.map((instruction) => instruction instanceof TransactionInstruction
|
|
114
127
|
? fromLegacyTransactionInstruction(instruction)
|
|
115
|
-
: instruction), tx), (tx) =>
|
|
128
|
+
: instruction), tx), (tx) => tollboothInstruction === undefined
|
|
129
|
+
? tx
|
|
130
|
+
: appendTransactionMessageInstructions([tollboothInstruction], tx), (tx) => addressLookupTable === undefined
|
|
116
131
|
? tx
|
|
117
132
|
: compressTransactionMessageUsingAddressLookupTables(tx, {
|
|
118
133
|
[fromLegacyPublicKey(addressLookupTable.key)]: addressLookupTable.state.addresses.map((address) => fromLegacyPublicKey(address)),
|
|
119
134
|
}), (tx) => addSignersToTransactionMessage(signers, tx)));
|
|
120
135
|
};
|
|
136
|
+
const getDomainTollRecipientAddress = (domain) => {
|
|
137
|
+
const hash = sha256(domain);
|
|
138
|
+
return PublicKey.findProgramAddressSync([Buffer.from("toll_recipient"), Buffer.from([0]), hash], new PublicKey(TollboothIdl.address))[0];
|
|
139
|
+
};
|
|
140
|
+
const buildTollboothInstructionIfNeeded = async ({ sessionKeyAddress, walletPublicKey, domain, feeMint, feeAmount, }) => {
|
|
141
|
+
if (feeAmount.gt(new BN(0)) && sessionKeyAddress !== undefined) {
|
|
142
|
+
const userTokenAccount = getAssociatedTokenAddressSync(feeMint, walletPublicKey);
|
|
143
|
+
const recipient = getDomainTollRecipientAddress(domain);
|
|
144
|
+
const instruction = await new TollboothProgram(new AnchorProvider({}, {})).methods
|
|
145
|
+
.payToll(feeAmount, 0)
|
|
146
|
+
.accounts({
|
|
147
|
+
session: new PublicKey(sessionKeyAddress),
|
|
148
|
+
source: userTokenAccount,
|
|
149
|
+
destination: getAssociatedTokenAddressSync(feeMint, recipient, true),
|
|
150
|
+
mint: feeMint,
|
|
151
|
+
})
|
|
152
|
+
.instruction();
|
|
153
|
+
return fromLegacyTransactionInstruction(instruction);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
121
159
|
const addSignaturesToExistingTransaction = (transaction, signerKeys) => partiallySignTransaction(signerKeys, transaction instanceof VersionedTransaction
|
|
122
160
|
? fromVersionedTransaction(transaction) // VersionedTransaction has a lifetime so it's fine to cast it so we can call partiallySignTransaction
|
|
123
161
|
: transaction);
|
|
@@ -169,6 +207,24 @@ const getSponsor = async (options, sponsorCache, domain) => {
|
|
|
169
207
|
return value;
|
|
170
208
|
}
|
|
171
209
|
};
|
|
210
|
+
const getFee = async (options, domain, variation, mint) => {
|
|
211
|
+
if (variation) {
|
|
212
|
+
const url = new URL("/api/fee", options.paymaster ?? DEFAULT_PAYMASTER[options.network]);
|
|
213
|
+
url.searchParams.set("domain", domain);
|
|
214
|
+
url.searchParams.set("variation", variation);
|
|
215
|
+
url.searchParams.set("mint", mint.toBase58());
|
|
216
|
+
const response = await fetch(url);
|
|
217
|
+
if (response.status === 200) {
|
|
218
|
+
return new BN(await response.text());
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
throw new PaymasterResponseError(response.status, await response.text());
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
return new BN(0);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
172
228
|
const getAddressLookupTable = async (connection, addressLookupTableCache, addressLookupTableAddress) => {
|
|
173
229
|
const value = addressLookupTableCache.get(addressLookupTableAddress);
|
|
174
230
|
if (value === undefined) {
|
package/esm/context.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Connection as Web3Connection } from "@solana/web3.js";
|
|
1
|
+
import { type PublicKey, Connection as Web3Connection } from "@solana/web3.js";
|
|
2
2
|
import type { Connection, SendTransactionOptions as SendTransactionBaseOptions, TransactionOrInstructions } from "./connection.js";
|
|
3
3
|
export declare const SESSIONS_INTERNAL_PAYMASTER_DOMAIN = "sessions";
|
|
4
4
|
export declare const createSessionContext: (options: {
|
|
@@ -8,13 +8,13 @@ export declare const createSessionContext: (options: {
|
|
|
8
8
|
}) => Promise<{
|
|
9
9
|
chainId: string;
|
|
10
10
|
domain: string;
|
|
11
|
-
payer:
|
|
12
|
-
internalPayer:
|
|
11
|
+
payer: PublicKey;
|
|
12
|
+
internalPayer: PublicKey;
|
|
13
13
|
getSolanaConnection: () => Promise<Web3Connection>;
|
|
14
14
|
connection: Web3Connection;
|
|
15
15
|
rpc: import("@solana/kit").Rpc<import("@solana/kit").RequestAirdropApi & import("@solana/kit").GetAccountInfoApi & import("@solana/kit").GetBalanceApi & import("@solana/kit").GetBlockApi & import("@solana/kit").GetBlockCommitmentApi & import("@solana/kit").GetBlockHeightApi & import("@solana/kit").GetBlockProductionApi & import("@solana/kit").GetBlocksApi & import("@solana/kit").GetBlocksWithLimitApi & import("@solana/kit").GetBlockTimeApi & import("@solana/kit").GetClusterNodesApi & import("@solana/kit").GetEpochInfoApi & import("@solana/kit").GetEpochScheduleApi & import("@solana/kit").GetFeeForMessageApi & import("@solana/kit").GetFirstAvailableBlockApi & import("@solana/kit").GetGenesisHashApi & import("@solana/kit").GetHealthApi & import("@solana/kit").GetHighestSnapshotSlotApi & import("@solana/kit").GetIdentityApi & import("@solana/kit").GetInflationGovernorApi & import("@solana/kit").GetInflationRateApi & import("@solana/kit").GetInflationRewardApi & import("@solana/kit").GetLargestAccountsApi & import("@solana/kit").GetLatestBlockhashApi & import("@solana/kit").GetLeaderScheduleApi & import("@solana/kit").GetMaxRetransmitSlotApi & import("@solana/kit").GetMaxShredInsertSlotApi & import("@solana/kit").GetMinimumBalanceForRentExemptionApi & import("@solana/kit").GetMultipleAccountsApi & import("@solana/kit").GetProgramAccountsApi & import("@solana/kit").GetRecentPerformanceSamplesApi & import("@solana/kit").GetRecentPrioritizationFeesApi & import("@solana/kit").GetSignaturesForAddressApi & import("@solana/kit").GetSignatureStatusesApi & import("@solana/kit").GetSlotApi & import("@solana/kit").GetSlotLeaderApi & import("@solana/kit").GetSlotLeadersApi & import("@solana/kit").GetStakeMinimumDelegationApi & import("@solana/kit").GetSupplyApi & import("@solana/kit").GetTokenAccountBalanceApi & import("@solana/kit").GetTokenAccountsByDelegateApi & import("@solana/kit").GetTokenAccountsByOwnerApi & import("@solana/kit").GetTokenLargestAccountsApi & import("@solana/kit").GetTokenSupplyApi & import("@solana/kit").GetTransactionApi & import("@solana/kit").GetTransactionCountApi & import("@solana/kit").GetVersionApi & import("@solana/kit").GetVoteAccountsApi & import("@solana/kit").IsBlockhashValidApi & import("@solana/kit").MinimumLedgerSlotApi & import("@solana/kit").SendTransactionApi & import("@solana/kit").SimulateTransactionApi>;
|
|
16
|
-
network: import("./
|
|
17
|
-
sendTransaction: (sessionKey: CryptoKeyPair | undefined, instructions: TransactionOrInstructions, sendTxOptions?: SendTransactionOptions) => Promise<import("./connection.js").TransactionResult>;
|
|
16
|
+
network: import("./network.js").Network;
|
|
17
|
+
sendTransaction: (sessionKey: CryptoKeyPair | undefined, instructions: TransactionOrInstructions, walletPublicKey: PublicKey, sendTxOptions?: SendTransactionOptions) => Promise<import("./connection.js").TransactionResult>;
|
|
18
18
|
}>;
|
|
19
19
|
export type SendTransactionOptions = SendTransactionBaseOptions & {
|
|
20
20
|
paymasterDomain?: string | undefined;
|
package/esm/context.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AnchorProvider } from "@coral-xyz/anchor";
|
|
2
2
|
import { ChainIdProgram } from "@fogo/sessions-idls";
|
|
3
|
-
import { Keypair, Connection as Web3Connection } from "@solana/web3.js";
|
|
3
|
+
import { Keypair, Connection as Web3Connection, } from "@solana/web3.js";
|
|
4
4
|
// eslint-disable-next-line unicorn/no-typeof-undefined
|
|
5
5
|
const IS_BROWSER = typeof globalThis.window !== "undefined";
|
|
6
6
|
export const SESSIONS_INTERNAL_PAYMASTER_DOMAIN = "sessions";
|
|
@@ -19,7 +19,7 @@ export const createSessionContext = async (options) => {
|
|
|
19
19
|
connection: options.connection.connection,
|
|
20
20
|
rpc: options.connection.rpc,
|
|
21
21
|
network: options.connection.network,
|
|
22
|
-
sendTransaction: (sessionKey, instructions, sendTxOptions) => options.connection.sendToPaymaster(sendTxOptions?.paymasterDomain ?? domain, sessionKey, instructions, {
|
|
22
|
+
sendTransaction: (sessionKey, instructions, walletPublicKey, sendTxOptions) => options.connection.sendToPaymaster(sendTxOptions?.paymasterDomain ?? domain, sessionKey, instructions, walletPublicKey, {
|
|
23
23
|
...sendTxOptions,
|
|
24
24
|
addressLookupTable: sendTxOptions?.addressLookupTable ??
|
|
25
25
|
options.defaultAddressLookupTableAddress,
|