Skip to content

Commit f0b272c

Browse files
committed
refactor(auth): parse siwe fields with siwemessage
1 parent 7f3667a commit f0b272c

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

packages/auth/src/lib/AuthManager/authAdapters/getPkpAuthContextFromPreGeneratedAdapter.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
parseLitResource,
66
} from '@lit-protocol/auth-helpers';
77
import { RESOLVED_AUTH_CONTEXT_PREFIX } from '@lit-protocol/auth-helpers';
8+
import { SiweMessage } from 'siwe';
89
import {
910
AUTH_METHOD_TYPE,
1011
AUTH_METHOD_TYPE_VALUES,
@@ -292,29 +293,24 @@ function extractAuthConfigFromDelegationAuthSig(delegationAuthSig: AuthSig): {
292293
} {
293294
const siweMessage = delegationAuthSig.signedMessage;
294295

295-
// const parsedSiweMessage = new SiweMessage(siweMessage);
296-
297-
// Extract domain
298-
const domainMatch = siweMessage.match(/^([^\s]+) wants you to sign in/m);
299-
const domain = domainMatch ? domainMatch[1] : undefined;
296+
const parsedSiweMessage = new SiweMessage(siweMessage);
297+
const domain = parsedSiweMessage.domain;
300298

301299
// Extract statement
302-
const statementMatch = siweMessage.match(/^(.*?)(?:\n\nURI:|$)/m);
303-
const statement = statementMatch
304-
? statementMatch[1].split('\n').slice(2).join('\n').trim()
305-
: undefined;
300+
const statement = parsedSiweMessage.statement;
306301

307302
// Extract expiration
308-
const expirationMatch = siweMessage.match(/^Expiration Time: (.*)$/m);
309-
const expiration = expirationMatch ? expirationMatch[1].trim() : undefined;
303+
const expiration = parsedSiweMessage.expirationTime;
310304

311-
const resourceMatches = [...siweMessage.matchAll(/-\s*(urn:recap:[^\s]+)/g)];
305+
const resourceMatches = parsedSiweMessage.resources ?? [];
312306

313307
const resources: LitResourceAbilityRequest[] = [];
314308
let derivedAuthData: AuthData | undefined;
315309

316-
for (const match of resourceMatches) {
317-
const urn = match[1];
310+
for (const urn of resourceMatches) {
311+
if (!urn.startsWith('urn:recap:')) {
312+
continue;
313+
}
318314
const { requests, derivedAuthData: candidateAuthData } =
319315
decodeRecapResource(urn);
320316

0 commit comments

Comments
 (0)