Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

waas: support getIdToken intent #548

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/waas/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Observer, SequenceWaaSBase } from './base'
import { Account, IdentityType, IntentDataOpenSession, IntentDataSendTransaction } from './clients/intent.gen'
import {
Account,
IdentityType,
IntentDataOpenSession,
IntentDataSendTransaction,
IntentResponseIdToken
} from './clients/intent.gen'
import { newSessionFromSessionId } from './session'
import { LocalStore, Store, StoreObj } from './store'
import {
Expand All @@ -17,6 +23,7 @@ import {
isCloseSessionResponse,
isFeeOptionsResponse,
isFinishValidateSessionResponse,
isGetIdTokenResponse,
isGetSessionResponse,
isInitiateAuthResponse,
isIntentTimeError,
Expand Down Expand Up @@ -564,6 +571,17 @@ export class SequenceWaaS {
await this.sendIntent(intent)
}

async getIdToken(args?: { nonce?: string }): Promise<IntentResponseIdToken> {
const intent = await this.waas.getIdToken({ nonce: args?.nonce })
const res = await this.sendIntent(intent)

if (!isGetIdTokenResponse(res)) {
throw new Error(`Invalid response: ${JSON.stringify(res)}`)
}

return res.data
}

async useIdentifier<T extends CommonAuthArgs>(args: T): Promise<T & { identifier: string }> {
if (args.identifier) {
return args as T & { identifier: string }
Expand Down
16 changes: 16 additions & 0 deletions packages/waas/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
combineTransactionIntents,
feeOptions,
finishValidateSession,
getIdToken,
getSession,
getTransactionReceipt,
GetTransactionReceiptArgs,
Expand Down Expand Up @@ -573,6 +574,21 @@ export class SequenceWaaSBase {
return this.signIntent(intent)
}

async getIdToken({ nonce }: { nonce?: string }) {
const sessionId = await this.sessionId.get()
if (!sessionId) {
throw new Error('session not open')
}

const intent = getIdToken({
wallet: await this.getWalletAddress(),
lifespan: DEFAULT_LIFESPAN,
sessionId,
nonce
})
return this.signIntent(intent)
}

async batch(intents: Intent<IntentDataSendTransaction>[]): Promise<SignedIntent<IntentDataSendTransaction>> {
const combined = combineTransactionIntents(intents)
return this.signIntent(combined)
Expand Down
11 changes: 11 additions & 0 deletions packages/waas/src/intents/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IntentResponseAuthInitiated,
IntentResponseCode,
IntentResponseGetSession,
IntentResponseIdToken,
IntentResponseValidationFinished,
IntentResponseValidationStarted
} from '../clients/intent.gen'