@fogo/sessions-sdk 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/index.js CHANGED
@@ -131,10 +131,36 @@ const getSessionAccount = async (connection, sessionPublicKey) => {
131
131
  : sessionInfoSchema.parse(new anchor_1.BorshAccountsCoder(sessions_idls_1.SessionManagerIdl).decode("Session", result.data));
132
132
  };
133
133
  exports.getSessionAccount = getSessionAccount;
134
+ const getDomainRegistryAuthorizedPrograms = async (connection, domain) => {
135
+ const result = await connection.getAccountInfo((0, exports.getDomainRecordAddress)(domain), "confirmed");
136
+ if (result === null) {
137
+ return [];
138
+ }
139
+ else {
140
+ const programs = [];
141
+ for (let i = 0; i < result.data.length; i += 64) {
142
+ programs.push({
143
+ programId: new web3_js_1.PublicKey(result.data.subarray(i, i + 32)),
144
+ signerPda: new web3_js_1.PublicKey(result.data.subarray(i + 32, i + 64)),
145
+ });
146
+ }
147
+ return programs;
148
+ }
149
+ };
150
+ const authorizedProgramsMatchDomainRegistry = (sessionAuthorizedPrograms, domainAuthorizedPrograms) => {
151
+ if (sessionAuthorizedPrograms.type === AuthorizedProgramsType.All) {
152
+ return true;
153
+ }
154
+ return domainAuthorizedPrograms.every(({ programId: programIdFromRegistry }) => sessionAuthorizedPrograms.programs.some(({ programId }) => programId.equals(programIdFromRegistry)));
155
+ };
134
156
  const createSession = async (context, walletPublicKey, sessionKey) => {
135
157
  const sessionPublicKey = new web3_js_1.PublicKey(await (0, kit_1.getAddressFromPublicKey)(sessionKey.publicKey));
136
- const sessionInfo = await (0, exports.getSessionAccount)(context.connection, sessionPublicKey);
137
- return sessionInfo === undefined
158
+ const [sessionInfo, domainRegistryAuthorizedPrograms] = await Promise.all([
159
+ (0, exports.getSessionAccount)(context.connection, sessionPublicKey),
160
+ getDomainRegistryAuthorizedPrograms(context.connection, context.domain),
161
+ ]);
162
+ return sessionInfo === undefined ||
163
+ !authorizedProgramsMatchDomainRegistry(sessionInfo.authorizedPrograms, domainRegistryAuthorizedPrograms)
138
164
  ? undefined
139
165
  : {
140
166
  sessionPublicKey,
package/esm/index.js CHANGED
@@ -111,10 +111,36 @@ export const getSessionAccount = async (connection, sessionPublicKey) => {
111
111
  ? undefined
112
112
  : sessionInfoSchema.parse(new BorshAccountsCoder(SessionManagerIdl).decode("Session", result.data));
113
113
  };
114
+ const getDomainRegistryAuthorizedPrograms = async (connection, domain) => {
115
+ const result = await connection.getAccountInfo(getDomainRecordAddress(domain), "confirmed");
116
+ if (result === null) {
117
+ return [];
118
+ }
119
+ else {
120
+ const programs = [];
121
+ for (let i = 0; i < result.data.length; i += 64) {
122
+ programs.push({
123
+ programId: new PublicKey(result.data.subarray(i, i + 32)),
124
+ signerPda: new PublicKey(result.data.subarray(i + 32, i + 64)),
125
+ });
126
+ }
127
+ return programs;
128
+ }
129
+ };
130
+ const authorizedProgramsMatchDomainRegistry = (sessionAuthorizedPrograms, domainAuthorizedPrograms) => {
131
+ if (sessionAuthorizedPrograms.type === AuthorizedProgramsType.All) {
132
+ return true;
133
+ }
134
+ return domainAuthorizedPrograms.every(({ programId: programIdFromRegistry }) => sessionAuthorizedPrograms.programs.some(({ programId }) => programId.equals(programIdFromRegistry)));
135
+ };
114
136
  const createSession = async (context, walletPublicKey, sessionKey) => {
115
137
  const sessionPublicKey = new PublicKey(await getAddressFromPublicKey(sessionKey.publicKey));
116
- const sessionInfo = await getSessionAccount(context.connection, sessionPublicKey);
117
- return sessionInfo === undefined
138
+ const [sessionInfo, domainRegistryAuthorizedPrograms] = await Promise.all([
139
+ getSessionAccount(context.connection, sessionPublicKey),
140
+ getDomainRegistryAuthorizedPrograms(context.connection, context.domain),
141
+ ]);
142
+ return sessionInfo === undefined ||
143
+ !authorizedProgramsMatchDomainRegistry(sessionInfo.authorizedPrograms, domainRegistryAuthorizedPrograms)
118
144
  ? undefined
119
145
  : {
120
146
  sessionPublicKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fogo/sessions-sdk",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "A set of utilities for integrating with Fogo sessions",
5
5
  "repository": {
6
6
  "type": "git",