@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/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/cjs/adapter.js
CHANGED
|
@@ -6,8 +6,11 @@ const sessions_idls_1 = require("@fogo/sessions-idls");
|
|
|
6
6
|
const compat_1 = require("@solana/compat");
|
|
7
7
|
const kit_1 = require("@solana/kit");
|
|
8
8
|
const web3_js_1 = require("@solana/web3.js");
|
|
9
|
+
const zod_1 = require("zod");
|
|
9
10
|
// eslint-disable-next-line unicorn/no-typeof-undefined
|
|
10
11
|
const IS_BROWSER = typeof globalThis.window !== "undefined";
|
|
12
|
+
const DEFAULT_PAYMASTER = "https://paymaster.fogo.io";
|
|
13
|
+
const DEFAULT_ADDRESS_LOOKUP_TABLE_ADDRESS = "B8cUjJMqaWWTNNSTXBmeptjWswwCH1gTSCRYv4nu7kJW";
|
|
11
14
|
var TransactionResultType;
|
|
12
15
|
(function (TransactionResultType) {
|
|
13
16
|
TransactionResultType[TransactionResultType["Success"] = 0] = "Success";
|
|
@@ -26,9 +29,10 @@ const TransactionResult = {
|
|
|
26
29
|
};
|
|
27
30
|
const createSolanaWalletAdapter = async (options) => {
|
|
28
31
|
const addressLookupTables = await getAddressLookupTables(options.connection, options.addressLookupTableAddress);
|
|
32
|
+
const sponsor = await getSponsor(options);
|
|
29
33
|
return {
|
|
30
34
|
connection: options.connection,
|
|
31
|
-
payer:
|
|
35
|
+
payer: sponsor,
|
|
32
36
|
chainId: await fetchChainId(options.connection),
|
|
33
37
|
domain: getDomain(options.domain),
|
|
34
38
|
sendTransaction: async (sessionKey, instructions) => {
|
|
@@ -36,7 +40,7 @@ const createSolanaWalletAdapter = async (options) => {
|
|
|
36
40
|
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
|
|
37
41
|
const sessionKeySigner = await (0, kit_1.createSignerFromKeyPair)(sessionKey);
|
|
38
42
|
const transaction = Array.isArray(instructions)
|
|
39
|
-
? await (0, kit_1.partiallySignTransactionMessageWithSigners)((0, kit_1.pipe)((0, kit_1.createTransactionMessage)({ version: 0 }), (tx) => (0, kit_1.setTransactionMessageFeePayer)((0, compat_1.fromLegacyPublicKey)(
|
|
43
|
+
? await (0, kit_1.partiallySignTransactionMessageWithSigners)((0, kit_1.pipe)((0, kit_1.createTransactionMessage)({ version: 0 }), (tx) => (0, kit_1.setTransactionMessageFeePayer)((0, compat_1.fromLegacyPublicKey)(sponsor), tx), (tx) => (0, kit_1.setTransactionMessageLifetimeUsingBlockhash)(latestBlockhash, tx), (tx) => (0, kit_1.appendTransactionMessageInstructions)(instructions.map((instruction) => instruction instanceof web3_js_1.TransactionInstruction
|
|
40
44
|
? (0, compat_1.fromLegacyTransactionInstruction)(instruction)
|
|
41
45
|
: instruction), tx), (tx) => (0, kit_1.compressTransactionMessageUsingAddressLookupTables)(tx, Object.fromEntries(addressLookupTables?.map((table) => [
|
|
42
46
|
(0, compat_1.fromLegacyPublicKey)(table.key),
|
|
@@ -45,9 +49,7 @@ const createSolanaWalletAdapter = async (options) => {
|
|
|
45
49
|
: await (0, kit_1.partiallySignTransaction)([sessionKey], instructions instanceof web3_js_1.VersionedTransaction
|
|
46
50
|
? (0, compat_1.fromVersionedTransaction)(instructions)
|
|
47
51
|
: instructions);
|
|
48
|
-
const signature =
|
|
49
|
-
? await options.sendToPaymaster(transaction)
|
|
50
|
-
: await sendToPaymaster(options.paymasterUrl, transaction);
|
|
52
|
+
const signature = await sendToPaymaster(options, transaction);
|
|
51
53
|
const lastValidBlockHeight = await rpc.getSlot().send();
|
|
52
54
|
const confirmationResult = await options.connection.confirmTransaction({
|
|
53
55
|
signature,
|
|
@@ -61,34 +63,43 @@ const createSolanaWalletAdapter = async (options) => {
|
|
|
61
63
|
};
|
|
62
64
|
};
|
|
63
65
|
exports.createSolanaWalletAdapter = createSolanaWalletAdapter;
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
headers: {
|
|
68
|
-
"Content-Type": "application/json",
|
|
69
|
-
},
|
|
70
|
-
body: JSON.stringify({
|
|
71
|
-
transaction: (0, kit_1.getBase64EncodedWireTransaction)(transaction),
|
|
72
|
-
}),
|
|
73
|
-
});
|
|
74
|
-
if (response.status === 200) {
|
|
75
|
-
return response.text();
|
|
66
|
+
const getSponsor = async (options) => {
|
|
67
|
+
if ("sponsor" in options) {
|
|
68
|
+
return options.sponsor;
|
|
76
69
|
}
|
|
77
70
|
else {
|
|
78
|
-
|
|
71
|
+
const response = await fetch(new URL("/api/sponsor_pubkey", options.paymaster ?? DEFAULT_PAYMASTER));
|
|
72
|
+
return new web3_js_1.PublicKey(zod_1.z.string().parse(await response.text()));
|
|
79
73
|
}
|
|
80
74
|
};
|
|
81
|
-
const
|
|
82
|
-
if (
|
|
83
|
-
return
|
|
75
|
+
const sendToPaymaster = async (options, transaction) => {
|
|
76
|
+
if ("sendToPaymaster" in options) {
|
|
77
|
+
return options.sendToPaymaster(transaction);
|
|
84
78
|
}
|
|
85
79
|
else {
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
80
|
+
const response = await fetch(new URL("/api/sponsor_and_send", options.paymaster ?? DEFAULT_PAYMASTER), {
|
|
81
|
+
method: "POST",
|
|
82
|
+
headers: {
|
|
83
|
+
"Content-Type": "application/json",
|
|
84
|
+
},
|
|
85
|
+
body: JSON.stringify({
|
|
86
|
+
transaction: (0, kit_1.getBase64EncodedWireTransaction)(transaction),
|
|
87
|
+
}),
|
|
88
|
+
});
|
|
89
|
+
if (response.status === 200) {
|
|
90
|
+
return response.text();
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
throw new PaymasterResponseError(response.status, await response.text());
|
|
94
|
+
}
|
|
90
95
|
}
|
|
91
96
|
};
|
|
97
|
+
const getAddressLookupTables = async (connection, addressLookupTableAddress = DEFAULT_ADDRESS_LOOKUP_TABLE_ADDRESS) => {
|
|
98
|
+
const addressLookupTableResult = await connection.getAddressLookupTable(new web3_js_1.PublicKey(addressLookupTableAddress));
|
|
99
|
+
return addressLookupTableResult.value
|
|
100
|
+
? [addressLookupTableResult.value]
|
|
101
|
+
: undefined;
|
|
102
|
+
};
|
|
92
103
|
const fetchChainId = async (connection) => {
|
|
93
104
|
const chainIdProgram = new sessions_idls_1.ChainIdProgram(new anchor_1.AnchorProvider(connection, { publicKey: new web3_js_1.Keypair().publicKey }, {})); // We mock the wallet because we don't need to sign anything
|
|
94
105
|
const { chainIdAccount: chainIdAddress } = await chainIdProgram.methods
|
package/cjs/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import type { TransactionError } from "@solana/web3.js";
|
|
1
|
+
import type { TransactionError, Connection } from "@solana/web3.js";
|
|
2
2
|
import { PublicKey } from "@solana/web3.js";
|
|
3
|
+
import BN from "bn.js";
|
|
4
|
+
import { z } from "zod";
|
|
3
5
|
import type { SessionAdapter, TransactionResult } from "./adapter.js";
|
|
4
6
|
export { type SessionAdapter, type TransactionResult, TransactionResultType, createSolanaWalletAdapter, } from "./adapter.js";
|
|
5
7
|
type EstablishSessionOptions = {
|
|
@@ -7,19 +9,261 @@ type EstablishSessionOptions = {
|
|
|
7
9
|
walletPublicKey: PublicKey;
|
|
8
10
|
signMessage: (message: Uint8Array) => Promise<Uint8Array>;
|
|
9
11
|
expires: Date;
|
|
10
|
-
limits: Map<PublicKey, bigint>;
|
|
11
12
|
extra?: string | undefined;
|
|
12
|
-
}
|
|
13
|
+
} & ({
|
|
14
|
+
limits?: Map<PublicKey, bigint>;
|
|
15
|
+
unlimited?: false;
|
|
16
|
+
} | {
|
|
17
|
+
unlimited: true;
|
|
18
|
+
});
|
|
13
19
|
export declare const establishSession: (options: EstablishSessionOptions) => Promise<EstablishSessionResult>;
|
|
14
20
|
export declare const replaceSession: (options: {
|
|
15
21
|
adapter: SessionAdapter;
|
|
16
22
|
session: Session;
|
|
17
23
|
signMessage: (message: Uint8Array) => Promise<Uint8Array>;
|
|
18
24
|
expires: Date;
|
|
19
|
-
limits: Map<PublicKey, bigint>;
|
|
20
25
|
extra?: string | undefined;
|
|
21
|
-
}
|
|
22
|
-
|
|
26
|
+
} & ({
|
|
27
|
+
limits?: Map<PublicKey, bigint>;
|
|
28
|
+
unlimited?: false;
|
|
29
|
+
} | {
|
|
30
|
+
unlimited: true;
|
|
31
|
+
})) => Promise<EstablishSessionResult>;
|
|
32
|
+
export declare const reestablishSession: (adapter: SessionAdapter, walletPublicKey: PublicKey, sessionKey: CryptoKeyPair) => Promise<Session | undefined>;
|
|
33
|
+
export declare const getSessionAccount: (connection: Connection, sessionPublicKey: PublicKey) => Promise<{
|
|
34
|
+
authorizedPrograms: {
|
|
35
|
+
type: AuthorizedProgramsType.All;
|
|
36
|
+
} | {
|
|
37
|
+
type: AuthorizedProgramsType.Specific;
|
|
38
|
+
programs: {
|
|
39
|
+
programId: PublicKey;
|
|
40
|
+
signerPda: PublicKey;
|
|
41
|
+
}[];
|
|
42
|
+
};
|
|
43
|
+
authorizedTokens: AuthorizedTokens;
|
|
44
|
+
expiration: Date;
|
|
45
|
+
extra: unknown;
|
|
46
|
+
major: number;
|
|
47
|
+
minor: number;
|
|
48
|
+
user: PublicKey;
|
|
49
|
+
} | undefined>;
|
|
50
|
+
declare const sessionInfoSchema: z.ZodEffects<z.ZodObject<{
|
|
51
|
+
session_info: z.ZodObject<{
|
|
52
|
+
authorized_programs: z.ZodUnion<[z.ZodObject<{
|
|
53
|
+
Specific: z.ZodObject<{
|
|
54
|
+
0: z.ZodArray<z.ZodObject<{
|
|
55
|
+
program_id: z.ZodType<PublicKey, z.ZodTypeDef, PublicKey>;
|
|
56
|
+
signer_pda: z.ZodType<PublicKey, z.ZodTypeDef, PublicKey>;
|
|
57
|
+
}, "strip", z.ZodTypeAny, {
|
|
58
|
+
program_id: PublicKey;
|
|
59
|
+
signer_pda: PublicKey;
|
|
60
|
+
}, {
|
|
61
|
+
program_id: PublicKey;
|
|
62
|
+
signer_pda: PublicKey;
|
|
63
|
+
}>, "many">;
|
|
64
|
+
}, "strip", z.ZodTypeAny, {
|
|
65
|
+
0: {
|
|
66
|
+
program_id: PublicKey;
|
|
67
|
+
signer_pda: PublicKey;
|
|
68
|
+
}[];
|
|
69
|
+
}, {
|
|
70
|
+
0: {
|
|
71
|
+
program_id: PublicKey;
|
|
72
|
+
signer_pda: PublicKey;
|
|
73
|
+
}[];
|
|
74
|
+
}>;
|
|
75
|
+
}, "strip", z.ZodTypeAny, {
|
|
76
|
+
Specific: {
|
|
77
|
+
0: {
|
|
78
|
+
program_id: PublicKey;
|
|
79
|
+
signer_pda: PublicKey;
|
|
80
|
+
}[];
|
|
81
|
+
};
|
|
82
|
+
}, {
|
|
83
|
+
Specific: {
|
|
84
|
+
0: {
|
|
85
|
+
program_id: PublicKey;
|
|
86
|
+
signer_pda: PublicKey;
|
|
87
|
+
}[];
|
|
88
|
+
};
|
|
89
|
+
}>, z.ZodObject<{
|
|
90
|
+
All: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
|
|
91
|
+
}, "strip", z.ZodTypeAny, {
|
|
92
|
+
All: {};
|
|
93
|
+
}, {
|
|
94
|
+
All: {};
|
|
95
|
+
}>]>;
|
|
96
|
+
authorized_tokens: z.ZodUnion<[z.ZodObject<{
|
|
97
|
+
Specific: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
|
|
98
|
+
}, "strip", z.ZodTypeAny, {
|
|
99
|
+
Specific: {};
|
|
100
|
+
}, {
|
|
101
|
+
Specific: {};
|
|
102
|
+
}>, z.ZodObject<{
|
|
103
|
+
All: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
|
|
104
|
+
}, "strip", z.ZodTypeAny, {
|
|
105
|
+
All: {};
|
|
106
|
+
}, {
|
|
107
|
+
All: {};
|
|
108
|
+
}>]>;
|
|
109
|
+
expiration: z.ZodType<BN, z.ZodTypeDef, BN>;
|
|
110
|
+
extra: z.ZodObject<{
|
|
111
|
+
0: z.ZodUnknown;
|
|
112
|
+
}, "strip", z.ZodTypeAny, {
|
|
113
|
+
0?: unknown;
|
|
114
|
+
}, {
|
|
115
|
+
0?: unknown;
|
|
116
|
+
}>;
|
|
117
|
+
major: z.ZodNumber;
|
|
118
|
+
minor: z.ZodNumber;
|
|
119
|
+
user: z.ZodType<PublicKey, z.ZodTypeDef, PublicKey>;
|
|
120
|
+
}, "strip", z.ZodTypeAny, {
|
|
121
|
+
authorized_programs: {
|
|
122
|
+
Specific: {
|
|
123
|
+
0: {
|
|
124
|
+
program_id: PublicKey;
|
|
125
|
+
signer_pda: PublicKey;
|
|
126
|
+
}[];
|
|
127
|
+
};
|
|
128
|
+
} | {
|
|
129
|
+
All: {};
|
|
130
|
+
};
|
|
131
|
+
authorized_tokens: {
|
|
132
|
+
Specific: {};
|
|
133
|
+
} | {
|
|
134
|
+
All: {};
|
|
135
|
+
};
|
|
136
|
+
expiration: BN;
|
|
137
|
+
extra: {
|
|
138
|
+
0?: unknown;
|
|
139
|
+
};
|
|
140
|
+
major: number;
|
|
141
|
+
minor: number;
|
|
142
|
+
user: PublicKey;
|
|
143
|
+
}, {
|
|
144
|
+
authorized_programs: {
|
|
145
|
+
Specific: {
|
|
146
|
+
0: {
|
|
147
|
+
program_id: PublicKey;
|
|
148
|
+
signer_pda: PublicKey;
|
|
149
|
+
}[];
|
|
150
|
+
};
|
|
151
|
+
} | {
|
|
152
|
+
All: {};
|
|
153
|
+
};
|
|
154
|
+
authorized_tokens: {
|
|
155
|
+
Specific: {};
|
|
156
|
+
} | {
|
|
157
|
+
All: {};
|
|
158
|
+
};
|
|
159
|
+
expiration: BN;
|
|
160
|
+
extra: {
|
|
161
|
+
0?: unknown;
|
|
162
|
+
};
|
|
163
|
+
major: number;
|
|
164
|
+
minor: number;
|
|
165
|
+
user: PublicKey;
|
|
166
|
+
}>;
|
|
167
|
+
}, "strip", z.ZodTypeAny, {
|
|
168
|
+
session_info: {
|
|
169
|
+
authorized_programs: {
|
|
170
|
+
Specific: {
|
|
171
|
+
0: {
|
|
172
|
+
program_id: PublicKey;
|
|
173
|
+
signer_pda: PublicKey;
|
|
174
|
+
}[];
|
|
175
|
+
};
|
|
176
|
+
} | {
|
|
177
|
+
All: {};
|
|
178
|
+
};
|
|
179
|
+
authorized_tokens: {
|
|
180
|
+
Specific: {};
|
|
181
|
+
} | {
|
|
182
|
+
All: {};
|
|
183
|
+
};
|
|
184
|
+
expiration: BN;
|
|
185
|
+
extra: {
|
|
186
|
+
0?: unknown;
|
|
187
|
+
};
|
|
188
|
+
major: number;
|
|
189
|
+
minor: number;
|
|
190
|
+
user: PublicKey;
|
|
191
|
+
};
|
|
192
|
+
}, {
|
|
193
|
+
session_info: {
|
|
194
|
+
authorized_programs: {
|
|
195
|
+
Specific: {
|
|
196
|
+
0: {
|
|
197
|
+
program_id: PublicKey;
|
|
198
|
+
signer_pda: PublicKey;
|
|
199
|
+
}[];
|
|
200
|
+
};
|
|
201
|
+
} | {
|
|
202
|
+
All: {};
|
|
203
|
+
};
|
|
204
|
+
authorized_tokens: {
|
|
205
|
+
Specific: {};
|
|
206
|
+
} | {
|
|
207
|
+
All: {};
|
|
208
|
+
};
|
|
209
|
+
expiration: BN;
|
|
210
|
+
extra: {
|
|
211
|
+
0?: unknown;
|
|
212
|
+
};
|
|
213
|
+
major: number;
|
|
214
|
+
minor: number;
|
|
215
|
+
user: PublicKey;
|
|
216
|
+
};
|
|
217
|
+
}>, {
|
|
218
|
+
authorizedPrograms: {
|
|
219
|
+
type: AuthorizedProgramsType.All;
|
|
220
|
+
} | {
|
|
221
|
+
type: AuthorizedProgramsType.Specific;
|
|
222
|
+
programs: {
|
|
223
|
+
programId: PublicKey;
|
|
224
|
+
signerPda: PublicKey;
|
|
225
|
+
}[];
|
|
226
|
+
};
|
|
227
|
+
authorizedTokens: AuthorizedTokens;
|
|
228
|
+
expiration: Date;
|
|
229
|
+
extra: unknown;
|
|
230
|
+
major: number;
|
|
231
|
+
minor: number;
|
|
232
|
+
user: PublicKey;
|
|
233
|
+
}, {
|
|
234
|
+
session_info: {
|
|
235
|
+
authorized_programs: {
|
|
236
|
+
Specific: {
|
|
237
|
+
0: {
|
|
238
|
+
program_id: PublicKey;
|
|
239
|
+
signer_pda: PublicKey;
|
|
240
|
+
}[];
|
|
241
|
+
};
|
|
242
|
+
} | {
|
|
243
|
+
All: {};
|
|
244
|
+
};
|
|
245
|
+
authorized_tokens: {
|
|
246
|
+
Specific: {};
|
|
247
|
+
} | {
|
|
248
|
+
All: {};
|
|
249
|
+
};
|
|
250
|
+
expiration: BN;
|
|
251
|
+
extra: {
|
|
252
|
+
0?: unknown;
|
|
253
|
+
};
|
|
254
|
+
major: number;
|
|
255
|
+
minor: number;
|
|
256
|
+
user: PublicKey;
|
|
257
|
+
};
|
|
258
|
+
}>;
|
|
259
|
+
export declare enum AuthorizedProgramsType {
|
|
260
|
+
All = 0,
|
|
261
|
+
Specific = 1
|
|
262
|
+
}
|
|
263
|
+
export declare enum AuthorizedTokens {
|
|
264
|
+
All = 0,
|
|
265
|
+
Specific = 1
|
|
266
|
+
}
|
|
23
267
|
export declare const getDomainRecordAddress: (domain: string) => PublicKey;
|
|
24
268
|
export declare enum SessionResultType {
|
|
25
269
|
Success = 0,
|
|
@@ -44,4 +288,5 @@ export type Session = {
|
|
|
44
288
|
walletPublicKey: PublicKey;
|
|
45
289
|
payer: PublicKey;
|
|
46
290
|
sendTransaction: (instructions: Parameters<SessionAdapter["sendTransaction"]>[1]) => Promise<TransactionResult>;
|
|
291
|
+
sessionInfo: z.infer<typeof sessionInfoSchema>;
|
|
47
292
|
};
|