@fogo/sessions-sdk 0.0.6 → 0.0.8
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/adapter.d.ts +2 -2
- package/cjs/adapter.js +36 -25
- package/cjs/index.d.ts +251 -6
- package/cjs/index.js +169 -58
- package/cjs/paymaster.js +6 -10
- package/esm/adapter.d.ts +2 -2
- package/esm/adapter.js +36 -25
- package/esm/index.d.ts +251 -6
- package/esm/index.js +165 -58
- package/esm/paymaster.js +7 -11
- package/package.json +3 -2
package/cjs/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SessionResultType = exports.getDomainRecordAddress = exports.reestablishSession = exports.replaceSession = exports.establishSession = exports.createSolanaWalletAdapter = exports.TransactionResultType = void 0;
|
|
6
|
+
exports.SessionResultType = exports.getDomainRecordAddress = exports.AuthorizedTokens = exports.AuthorizedProgramsType = exports.getSessionAccount = exports.reestablishSession = exports.replaceSession = exports.establishSession = exports.createSolanaWalletAdapter = exports.TransactionResultType = void 0;
|
|
4
7
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
5
8
|
const sessions_idls_1 = require("@fogo/sessions-idls");
|
|
6
9
|
const mpl_token_metadata_1 = require("@metaplex-foundation/mpl-token-metadata");
|
|
@@ -10,6 +13,8 @@ const sha2_1 = require("@noble/hashes/sha2");
|
|
|
10
13
|
const kit_1 = require("@solana/kit");
|
|
11
14
|
const spl_token_1 = require("@solana/spl-token");
|
|
12
15
|
const web3_js_1 = require("@solana/web3.js");
|
|
16
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
17
|
+
const zod_1 = require("zod");
|
|
13
18
|
const adapter_js_1 = require("./adapter.js");
|
|
14
19
|
var adapter_js_2 = require("./adapter.js");
|
|
15
20
|
Object.defineProperty(exports, "TransactionResultType", { enumerable: true, get: function () { return adapter_js_2.TransactionResultType; } });
|
|
@@ -17,45 +22,139 @@ Object.defineProperty(exports, "createSolanaWalletAdapter", { enumerable: true,
|
|
|
17
22
|
const MESSAGE_HEADER = `Fogo Sessions:
|
|
18
23
|
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
24
|
`;
|
|
25
|
+
const UNLIMITED_TOKEN_PERMISSIONS_VALUE = "this app may spend any amount of any token";
|
|
26
|
+
const TOKENLESS_PERMISSIONS_VALUE = "this app may not spend any tokens";
|
|
20
27
|
const CURRENT_MAJOR = "0";
|
|
21
28
|
const CURRENT_MINOR = "1";
|
|
22
29
|
const establishSession = async (options) => {
|
|
23
30
|
const sessionKey = await (0, kit_1.generateKeyPair)();
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
if (options.unlimited) {
|
|
32
|
+
return sendSessionEstablishTransaction(options, sessionKey, await Promise.all([
|
|
33
|
+
buildIntentInstruction(options, sessionKey),
|
|
34
|
+
buildStartSessionInstruction(options, sessionKey),
|
|
35
|
+
]));
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
const filteredLimits = new Map(options.limits?.entries().filter(([, amount]) => amount > 0n));
|
|
39
|
+
const tokenInfo = filteredLimits.size > 0
|
|
40
|
+
? await getTokenInfo(options.adapter, filteredLimits)
|
|
41
|
+
: [];
|
|
42
|
+
const [intentInstruction, startSessionInstruction] = await Promise.all([
|
|
43
|
+
buildIntentInstruction(options, sessionKey, tokenInfo),
|
|
44
|
+
buildStartSessionInstruction(options, sessionKey, tokenInfo),
|
|
45
|
+
]);
|
|
46
|
+
return sendSessionEstablishTransaction(options, sessionKey, [
|
|
47
|
+
...buildCreateAssociatedTokenAccountInstructions(options, tokenInfo),
|
|
48
|
+
intentInstruction,
|
|
49
|
+
startSessionInstruction,
|
|
50
|
+
]);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
exports.establishSession = establishSession;
|
|
54
|
+
const sendSessionEstablishTransaction = async (options, sessionKey, instructions) => {
|
|
55
|
+
const result = await options.adapter.sendTransaction(sessionKey, instructions);
|
|
34
56
|
switch (result.type) {
|
|
35
57
|
case adapter_js_1.TransactionResultType.Success: {
|
|
36
|
-
|
|
58
|
+
const session = await createSession(options.adapter, options.walletPublicKey, sessionKey);
|
|
59
|
+
return session
|
|
60
|
+
? EstablishSessionResult.Success(result.signature, session)
|
|
61
|
+
: EstablishSessionResult.Failed(result.signature, new Error("Transaction succeeded, but the session was not created"));
|
|
37
62
|
}
|
|
38
63
|
case adapter_js_1.TransactionResultType.Failed: {
|
|
39
64
|
return EstablishSessionResult.Failed(result.signature, result.error);
|
|
40
65
|
}
|
|
41
66
|
}
|
|
42
67
|
};
|
|
43
|
-
exports.establishSession = establishSession;
|
|
44
68
|
const replaceSession = async (options) => (0, exports.establishSession)({
|
|
45
69
|
...options,
|
|
46
70
|
walletPublicKey: options.session.walletPublicKey,
|
|
47
71
|
});
|
|
48
72
|
exports.replaceSession = replaceSession;
|
|
49
|
-
// TODO we really should check to ensure the session is still valid...
|
|
50
73
|
const reestablishSession = async (adapter, walletPublicKey, sessionKey) => createSession(adapter, walletPublicKey, sessionKey);
|
|
51
74
|
exports.reestablishSession = reestablishSession;
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
75
|
+
const getSessionAccount = async (connection, sessionPublicKey) => {
|
|
76
|
+
const result = await connection.getAccountInfo(sessionPublicKey);
|
|
77
|
+
return result === null
|
|
78
|
+
? undefined
|
|
79
|
+
: sessionInfoSchema.parse(new anchor_1.BorshAccountsCoder(sessions_idls_1.SessionManagerIdl).decode("Session", result.data));
|
|
80
|
+
};
|
|
81
|
+
exports.getSessionAccount = getSessionAccount;
|
|
82
|
+
const createSession = async (adapter, walletPublicKey, sessionKey) => {
|
|
83
|
+
const sessionPublicKey = new web3_js_1.PublicKey(await (0, kit_1.getAddressFromPublicKey)(sessionKey.publicKey));
|
|
84
|
+
const sessionInfo = await (0, exports.getSessionAccount)(adapter.connection, sessionPublicKey);
|
|
85
|
+
return sessionInfo === undefined
|
|
86
|
+
? undefined
|
|
87
|
+
: {
|
|
88
|
+
sessionPublicKey,
|
|
89
|
+
walletPublicKey,
|
|
90
|
+
sessionKey,
|
|
91
|
+
payer: adapter.payer,
|
|
92
|
+
sendTransaction: (instructions) => adapter.sendTransaction(sessionKey, instructions),
|
|
93
|
+
sessionInfo,
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
const sessionInfoSchema = zod_1.z
|
|
97
|
+
.object({
|
|
98
|
+
session_info: zod_1.z.object({
|
|
99
|
+
authorized_programs: zod_1.z.union([
|
|
100
|
+
zod_1.z.object({
|
|
101
|
+
Specific: zod_1.z.object({
|
|
102
|
+
0: zod_1.z.array(zod_1.z.object({
|
|
103
|
+
program_id: zod_1.z.instanceof(web3_js_1.PublicKey),
|
|
104
|
+
signer_pda: zod_1.z.instanceof(web3_js_1.PublicKey),
|
|
105
|
+
})),
|
|
106
|
+
}),
|
|
107
|
+
}),
|
|
108
|
+
zod_1.z.object({
|
|
109
|
+
All: zod_1.z.object({}),
|
|
110
|
+
}),
|
|
111
|
+
]),
|
|
112
|
+
authorized_tokens: zod_1.z.union([
|
|
113
|
+
zod_1.z.object({ Specific: zod_1.z.object({}) }),
|
|
114
|
+
zod_1.z.object({ All: zod_1.z.object({}) }),
|
|
115
|
+
]),
|
|
116
|
+
expiration: zod_1.z.instanceof(bn_js_1.default),
|
|
117
|
+
extra: zod_1.z.object({
|
|
118
|
+
0: zod_1.z.unknown(),
|
|
119
|
+
}),
|
|
120
|
+
major: zod_1.z.number(),
|
|
121
|
+
minor: zod_1.z.number(),
|
|
122
|
+
user: zod_1.z.instanceof(web3_js_1.PublicKey),
|
|
123
|
+
}),
|
|
124
|
+
})
|
|
125
|
+
.transform(({ session_info }) => ({
|
|
126
|
+
authorizedPrograms: "All" in session_info.authorized_programs
|
|
127
|
+
? AuthorizedPrograms.All()
|
|
128
|
+
: AuthorizedPrograms.Specific(session_info.authorized_programs.Specific[0].map(({ program_id, signer_pda }) => ({
|
|
129
|
+
programId: program_id,
|
|
130
|
+
signerPda: signer_pda,
|
|
131
|
+
}))),
|
|
132
|
+
authorizedTokens: "All" in session_info.authorized_tokens
|
|
133
|
+
? AuthorizedTokens.All
|
|
134
|
+
: AuthorizedTokens.Specific,
|
|
135
|
+
expiration: new Date(Number(session_info.expiration) * 1000),
|
|
136
|
+
extra: session_info.extra[0],
|
|
137
|
+
major: session_info.major,
|
|
138
|
+
minor: session_info.minor,
|
|
139
|
+
user: session_info.user,
|
|
140
|
+
}));
|
|
141
|
+
var AuthorizedProgramsType;
|
|
142
|
+
(function (AuthorizedProgramsType) {
|
|
143
|
+
AuthorizedProgramsType[AuthorizedProgramsType["All"] = 0] = "All";
|
|
144
|
+
AuthorizedProgramsType[AuthorizedProgramsType["Specific"] = 1] = "Specific";
|
|
145
|
+
})(AuthorizedProgramsType || (exports.AuthorizedProgramsType = AuthorizedProgramsType = {}));
|
|
146
|
+
const AuthorizedPrograms = {
|
|
147
|
+
All: () => ({ type: AuthorizedProgramsType.All }),
|
|
148
|
+
Specific: (programs) => ({
|
|
149
|
+
type: AuthorizedProgramsType.Specific,
|
|
150
|
+
programs,
|
|
151
|
+
}),
|
|
152
|
+
};
|
|
153
|
+
var AuthorizedTokens;
|
|
154
|
+
(function (AuthorizedTokens) {
|
|
155
|
+
AuthorizedTokens[AuthorizedTokens["All"] = 0] = "All";
|
|
156
|
+
AuthorizedTokens[AuthorizedTokens["Specific"] = 1] = "Specific";
|
|
157
|
+
})(AuthorizedTokens || (exports.AuthorizedTokens = AuthorizedTokens = {}));
|
|
59
158
|
const SymbolOrMintType = {
|
|
60
159
|
Symbol: "Symbol",
|
|
61
160
|
Mint: "Mint",
|
|
@@ -70,13 +169,13 @@ const SymbolOrMint = {
|
|
|
70
169
|
mint,
|
|
71
170
|
}),
|
|
72
171
|
};
|
|
73
|
-
const getTokenInfo = async (
|
|
74
|
-
const umi = (0, umi_bundle_defaults_1.createUmi)(
|
|
75
|
-
return Promise.all(
|
|
172
|
+
const getTokenInfo = async (adapter, limits) => {
|
|
173
|
+
const umi = (0, umi_bundle_defaults_1.createUmi)(adapter.connection.rpcEndpoint);
|
|
174
|
+
return Promise.all(limits.entries().map(async ([mint, amount]) => {
|
|
76
175
|
const metaplexMint = (0, umi_1.publicKey)(mint.toBase58());
|
|
77
176
|
const metadataAddress = (0, mpl_token_metadata_1.findMetadataPda)(umi, { mint: metaplexMint })[0];
|
|
78
177
|
const [mintInfo, metadata] = await Promise.all([
|
|
79
|
-
(0, spl_token_1.getMint)(
|
|
178
|
+
(0, spl_token_1.getMint)(adapter.connection, mint),
|
|
80
179
|
(0, mpl_token_metadata_1.safeFetchMetadata)(umi, metadataAddress),
|
|
81
180
|
]);
|
|
82
181
|
return {
|
|
@@ -121,14 +220,21 @@ const buildMessage = async (body) => new TextEncoder().encode([
|
|
|
121
220
|
const serializeKV = (data) => Object.entries(data)
|
|
122
221
|
.map(([key, value]) => [key, ":", value.startsWith("\n") ? "" : " ", value].join(""))
|
|
123
222
|
.join("\n");
|
|
124
|
-
const serializeTokenList = (tokens) =>
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
223
|
+
const serializeTokenList = (tokens) => {
|
|
224
|
+
if (tokens === undefined) {
|
|
225
|
+
return UNLIMITED_TOKEN_PERMISSIONS_VALUE;
|
|
226
|
+
}
|
|
227
|
+
else if (tokens.length === 0) {
|
|
228
|
+
return TOKENLESS_PERMISSIONS_VALUE;
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
return tokens
|
|
232
|
+
.values()
|
|
233
|
+
.map(({ symbolOrMint, amount, decimals }) => `\n-${symbolOrMint.type === SymbolOrMintType.Symbol ? symbolOrMint.symbol : symbolOrMint.mint.toBase58()}: ${amountToString(amount, decimals)}`)
|
|
234
|
+
.toArray()
|
|
235
|
+
.join("");
|
|
236
|
+
}
|
|
237
|
+
};
|
|
132
238
|
const amountToString = (amount, decimals) => {
|
|
133
239
|
const asStr = amount.toString();
|
|
134
240
|
const whole = asStr.length > decimals ? asStr.slice(0, asStr.length - decimals) : "0";
|
|
@@ -146,35 +252,40 @@ const getDomainRecordAddress = (domain) => {
|
|
|
146
252
|
return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("domain-record"), hash], new web3_js_1.PublicKey(sessions_idls_1.DomainRegistryIdl.address))[0];
|
|
147
253
|
};
|
|
148
254
|
exports.getDomainRecordAddress = getDomainRecordAddress;
|
|
149
|
-
const buildStartSessionInstruction = async (options, sessionKey, tokens) =>
|
|
150
|
-
.
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
},
|
|
167
|
-
...(symbolOrMint.type === SymbolOrMintType.Symbol
|
|
168
|
-
? [
|
|
255
|
+
const buildStartSessionInstruction = async (options, sessionKey, tokens) => {
|
|
256
|
+
const instruction = new sessions_idls_1.SessionManagerProgram(new anchor_1.AnchorProvider(options.adapter.connection, {}, {})).methods
|
|
257
|
+
.startSession()
|
|
258
|
+
.accounts({
|
|
259
|
+
sponsor: options.adapter.payer,
|
|
260
|
+
session: await (0, kit_1.getAddressFromPublicKey)(sessionKey.publicKey),
|
|
261
|
+
domainRegistry: (0, exports.getDomainRecordAddress)(options.adapter.domain),
|
|
262
|
+
});
|
|
263
|
+
return tokens === undefined
|
|
264
|
+
? instruction.instruction()
|
|
265
|
+
: instruction
|
|
266
|
+
.remainingAccounts(tokens.flatMap(({ symbolOrMint, mint, metadataAddress }) => [
|
|
267
|
+
{
|
|
268
|
+
pubkey: (0, spl_token_1.getAssociatedTokenAddressSync)(mint, options.walletPublicKey),
|
|
269
|
+
isWritable: true,
|
|
270
|
+
isSigner: false,
|
|
271
|
+
},
|
|
169
272
|
{
|
|
170
|
-
pubkey:
|
|
273
|
+
pubkey: mint,
|
|
171
274
|
isWritable: false,
|
|
172
275
|
isSigner: false,
|
|
173
276
|
},
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
277
|
+
...(symbolOrMint.type === SymbolOrMintType.Symbol
|
|
278
|
+
? [
|
|
279
|
+
{
|
|
280
|
+
pubkey: metadataAddress,
|
|
281
|
+
isWritable: false,
|
|
282
|
+
isSigner: false,
|
|
283
|
+
},
|
|
284
|
+
]
|
|
285
|
+
: []),
|
|
286
|
+
]))
|
|
287
|
+
.instruction();
|
|
288
|
+
};
|
|
178
289
|
var SessionResultType;
|
|
179
290
|
(function (SessionResultType) {
|
|
180
291
|
SessionResultType[SessionResultType["Success"] = 0] = "Success";
|
package/cjs/paymaster.js
CHANGED
|
@@ -4,16 +4,12 @@ exports.createPaymasterEndpoint = exports.sponsorAndSend = void 0;
|
|
|
4
4
|
const compat_1 = require("@solana/compat");
|
|
5
5
|
const kit_1 = require("@solana/kit");
|
|
6
6
|
const zod_1 = require("zod");
|
|
7
|
-
const sponsorAndSend = async (rpc, sponsor, transaction) =>
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
})
|
|
14
|
-
.send();
|
|
15
|
-
return (0, kit_1.getSignatureFromTransaction)(signedTransaction);
|
|
16
|
-
};
|
|
7
|
+
const sponsorAndSend = async (rpc, sponsor, transaction) => rpc
|
|
8
|
+
.sendTransaction((0, kit_1.getBase64EncodedWireTransaction)(await (0, kit_1.signTransaction)([sponsor], transaction)), {
|
|
9
|
+
encoding: "base64",
|
|
10
|
+
skipPreflight: true,
|
|
11
|
+
})
|
|
12
|
+
.send();
|
|
17
13
|
exports.sponsorAndSend = sponsorAndSend;
|
|
18
14
|
const createPaymasterEndpoint = async (options) => {
|
|
19
15
|
const rpc = (0, kit_1.createSolanaRpc)(options.rpc);
|
package/esm/adapter.d.ts
CHANGED
|
@@ -26,12 +26,12 @@ declare const TransactionResult: {
|
|
|
26
26
|
export type TransactionResult = ReturnType<(typeof TransactionResult)[keyof typeof TransactionResult]>;
|
|
27
27
|
export declare const createSolanaWalletAdapter: (options: {
|
|
28
28
|
connection: Connection;
|
|
29
|
-
sponsor: PublicKey;
|
|
30
29
|
addressLookupTableAddress?: string | undefined;
|
|
31
30
|
domain?: string | undefined;
|
|
32
31
|
} & ({
|
|
33
|
-
|
|
32
|
+
paymaster?: string | URL | undefined;
|
|
34
33
|
} | {
|
|
35
34
|
sendToPaymaster: (transaction: Transaction) => Promise<string>;
|
|
35
|
+
sponsor: PublicKey;
|
|
36
36
|
})) => Promise<SessionAdapter>;
|
|
37
37
|
export {};
|
package/esm/adapter.js
CHANGED
|
@@ -3,8 +3,11 @@ import { ChainIdProgram } from "@fogo/sessions-idls";
|
|
|
3
3
|
import { fromLegacyPublicKey, fromLegacyTransactionInstruction, fromVersionedTransaction, } from "@solana/compat";
|
|
4
4
|
import { createTransactionMessage, setTransactionMessageFeePayer, setTransactionMessageLifetimeUsingBlockhash, appendTransactionMessageInstructions, getBase64EncodedWireTransaction, partiallySignTransactionMessageWithSigners, pipe, createSolanaRpc, addSignersToTransactionMessage, compressTransactionMessageUsingAddressLookupTables, createSignerFromKeyPair, partiallySignTransaction, } from "@solana/kit";
|
|
5
5
|
import { PublicKey, Keypair, TransactionInstruction, VersionedTransaction, } from "@solana/web3.js";
|
|
6
|
+
import { z } from "zod";
|
|
6
7
|
// eslint-disable-next-line unicorn/no-typeof-undefined
|
|
7
8
|
const IS_BROWSER = typeof globalThis.window !== "undefined";
|
|
9
|
+
const DEFAULT_PAYMASTER = "https://paymaster.fogo.io";
|
|
10
|
+
const DEFAULT_ADDRESS_LOOKUP_TABLE_ADDRESS = "B8cUjJMqaWWTNNSTXBmeptjWswwCH1gTSCRYv4nu7kJW";
|
|
8
11
|
export var TransactionResultType;
|
|
9
12
|
(function (TransactionResultType) {
|
|
10
13
|
TransactionResultType[TransactionResultType["Success"] = 0] = "Success";
|
|
@@ -23,9 +26,10 @@ const TransactionResult = {
|
|
|
23
26
|
};
|
|
24
27
|
export const createSolanaWalletAdapter = async (options) => {
|
|
25
28
|
const addressLookupTables = await getAddressLookupTables(options.connection, options.addressLookupTableAddress);
|
|
29
|
+
const sponsor = await getSponsor(options);
|
|
26
30
|
return {
|
|
27
31
|
connection: options.connection,
|
|
28
|
-
payer:
|
|
32
|
+
payer: sponsor,
|
|
29
33
|
chainId: await fetchChainId(options.connection),
|
|
30
34
|
domain: getDomain(options.domain),
|
|
31
35
|
sendTransaction: async (sessionKey, instructions) => {
|
|
@@ -33,7 +37,7 @@ export const createSolanaWalletAdapter = async (options) => {
|
|
|
33
37
|
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
|
|
34
38
|
const sessionKeySigner = await createSignerFromKeyPair(sessionKey);
|
|
35
39
|
const transaction = Array.isArray(instructions)
|
|
36
|
-
? await partiallySignTransactionMessageWithSigners(pipe(createTransactionMessage({ version: 0 }), (tx) => setTransactionMessageFeePayer(fromLegacyPublicKey(
|
|
40
|
+
? await partiallySignTransactionMessageWithSigners(pipe(createTransactionMessage({ version: 0 }), (tx) => setTransactionMessageFeePayer(fromLegacyPublicKey(sponsor), tx), (tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx), (tx) => appendTransactionMessageInstructions(instructions.map((instruction) => instruction instanceof TransactionInstruction
|
|
37
41
|
? fromLegacyTransactionInstruction(instruction)
|
|
38
42
|
: instruction), tx), (tx) => compressTransactionMessageUsingAddressLookupTables(tx, Object.fromEntries(addressLookupTables?.map((table) => [
|
|
39
43
|
fromLegacyPublicKey(table.key),
|
|
@@ -42,9 +46,7 @@ export const createSolanaWalletAdapter = async (options) => {
|
|
|
42
46
|
: await partiallySignTransaction([sessionKey], instructions instanceof VersionedTransaction
|
|
43
47
|
? fromVersionedTransaction(instructions)
|
|
44
48
|
: instructions);
|
|
45
|
-
const signature =
|
|
46
|
-
? await options.sendToPaymaster(transaction)
|
|
47
|
-
: await sendToPaymaster(options.paymasterUrl, transaction);
|
|
49
|
+
const signature = await sendToPaymaster(options, transaction);
|
|
48
50
|
const lastValidBlockHeight = await rpc.getSlot().send();
|
|
49
51
|
const confirmationResult = await options.connection.confirmTransaction({
|
|
50
52
|
signature,
|
|
@@ -57,34 +59,43 @@ export const createSolanaWalletAdapter = async (options) => {
|
|
|
57
59
|
},
|
|
58
60
|
};
|
|
59
61
|
};
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
headers: {
|
|
64
|
-
"Content-Type": "application/json",
|
|
65
|
-
},
|
|
66
|
-
body: JSON.stringify({
|
|
67
|
-
transaction: getBase64EncodedWireTransaction(transaction),
|
|
68
|
-
}),
|
|
69
|
-
});
|
|
70
|
-
if (response.status === 200) {
|
|
71
|
-
return response.text();
|
|
62
|
+
const getSponsor = async (options) => {
|
|
63
|
+
if ("sponsor" in options) {
|
|
64
|
+
return options.sponsor;
|
|
72
65
|
}
|
|
73
66
|
else {
|
|
74
|
-
|
|
67
|
+
const response = await fetch(new URL("/api/sponsor_pubkey", options.paymaster ?? DEFAULT_PAYMASTER));
|
|
68
|
+
return new PublicKey(z.string().parse(await response.text()));
|
|
75
69
|
}
|
|
76
70
|
};
|
|
77
|
-
const
|
|
78
|
-
if (
|
|
79
|
-
return
|
|
71
|
+
const sendToPaymaster = async (options, transaction) => {
|
|
72
|
+
if ("sendToPaymaster" in options) {
|
|
73
|
+
return options.sendToPaymaster(transaction);
|
|
80
74
|
}
|
|
81
75
|
else {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
76
|
+
const response = await fetch(new URL("/api/sponsor_and_send", options.paymaster ?? DEFAULT_PAYMASTER), {
|
|
77
|
+
method: "POST",
|
|
78
|
+
headers: {
|
|
79
|
+
"Content-Type": "application/json",
|
|
80
|
+
},
|
|
81
|
+
body: JSON.stringify({
|
|
82
|
+
transaction: getBase64EncodedWireTransaction(transaction),
|
|
83
|
+
}),
|
|
84
|
+
});
|
|
85
|
+
if (response.status === 200) {
|
|
86
|
+
return response.text();
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
throw new PaymasterResponseError(response.status, await response.text());
|
|
90
|
+
}
|
|
86
91
|
}
|
|
87
92
|
};
|
|
93
|
+
const getAddressLookupTables = async (connection, addressLookupTableAddress = DEFAULT_ADDRESS_LOOKUP_TABLE_ADDRESS) => {
|
|
94
|
+
const addressLookupTableResult = await connection.getAddressLookupTable(new PublicKey(addressLookupTableAddress));
|
|
95
|
+
return addressLookupTableResult.value
|
|
96
|
+
? [addressLookupTableResult.value]
|
|
97
|
+
: undefined;
|
|
98
|
+
};
|
|
88
99
|
const fetchChainId = async (connection) => {
|
|
89
100
|
const chainIdProgram = new ChainIdProgram(new AnchorProvider(connection, { publicKey: new Keypair().publicKey }, {})); // We mock the wallet because we don't need to sign anything
|
|
90
101
|
const { chainIdAccount: chainIdAddress } = await chainIdProgram.methods
|