diff --git a/packages/waas/src/auth.ts b/packages/waas/src/auth.ts index a17a51ec7..ddd80091f 100644 --- a/packages/waas/src/auth.ts +++ b/packages/waas/src/auth.ts @@ -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 { @@ -17,6 +23,7 @@ import { isCloseSessionResponse, isFeeOptionsResponse, isFinishValidateSessionResponse, + isGetIdTokenResponse, isGetSessionResponse, isInitiateAuthResponse, isIntentTimeError, @@ -564,6 +571,17 @@ export class SequenceWaaS { await this.sendIntent(intent) } + async getIdToken(args?: { nonce?: string }): Promise { + 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(args: T): Promise { if (args.identifier) { return args as T & { identifier: string } diff --git a/packages/waas/src/base.ts b/packages/waas/src/base.ts index e71c6aa9c..de6fa2139 100644 --- a/packages/waas/src/base.ts +++ b/packages/waas/src/base.ts @@ -4,6 +4,7 @@ import { combineTransactionIntents, feeOptions, finishValidateSession, + getIdToken, getSession, getTransactionReceipt, GetTransactionReceiptArgs, @@ -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[]): Promise> { const combined = combineTransactionIntents(intents) return this.signIntent(combined) diff --git a/packages/waas/src/intents/responses.ts b/packages/waas/src/intents/responses.ts index dec23c8a2..faa72f452 100644 --- a/packages/waas/src/intents/responses.ts +++ b/packages/waas/src/intents/responses.ts @@ -6,6 +6,7 @@ import { IntentResponseAuthInitiated, IntentResponseCode, IntentResponseGetSession, + IntentResponseIdToken, IntentResponseValidationFinished, IntentResponseValidationStarted } from '../clients/intent.gen' @@ -128,6 +129,7 @@ export type FinishValidateSessionResponse = Response export type LinkAccountResponse = Response export type ListAccountsResponse = Response +export type IdTokenResponse = Response export function isInitiateAuthResponse(receipt: any): receipt is InitiateAuthResponse { return ( @@ -277,3 +279,12 @@ export function isIntentTimeError(error: any): error is WebrpcEndpointError { error.cause?.endsWith('intent is invalid: intent issued in the future')) ) } + +export function isGetIdTokenResponse(receipt: any): receipt is IdTokenResponse { + return ( + typeof receipt === 'object' && + receipt.code === IntentResponseCode.idToken && + typeof receipt.data === 'object' && + typeof receipt.data.idToken === 'string' + ) +} diff --git a/packages/waas/src/intents/session.ts b/packages/waas/src/intents/session.ts index 8f45eda47..74a114580 100644 --- a/packages/waas/src/intents/session.ts +++ b/packages/waas/src/intents/session.ts @@ -8,6 +8,7 @@ import { IntentDataValidateSession, IntentDataSessionAuthProof, IntentDataInitiateAuth, + IntentDataGetIdToken, IntentName } from '../clients/intent.gen' @@ -62,3 +63,9 @@ export type SessionAuthProof = BaseArgs & IntentDataSessionAuthProof export function sessionAuthProof({ lifespan, ...data }: SessionAuthProof): Intent { return makeIntent(IntentName.sessionAuthProof, lifespan, data) } + +export type GetIdTokenArgs = BaseArgs & IntentDataGetIdToken + +export function getIdToken({ lifespan, ...data }: GetIdTokenArgs): Intent { + return makeIntent(IntentName.getIdToken, lifespan, data) +}