Skip to content

Commit

Permalink
waas: add support for getAdopter and adoptChildWallet
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav committed Nov 14, 2024
1 parent c025506 commit a7fccb7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
25 changes: 24 additions & 1 deletion packages/waas/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ import {
SignedIntent,
SignMessageArgs,
getTimeDrift,
updateTimeDrift
updateTimeDrift,
AdoptChildWalletArgs
} from './intents'
import {
FeeOptionsResponse,
isChildWalletAdoptedResponse,
isCloseSessionResponse,
isFeeOptionsResponse,
isFinishValidateSessionResponse,
isGetAdopterResponse,
isGetIdTokenResponse,
isGetSessionResponse,
isInitiateAuthResponse,
Expand Down Expand Up @@ -738,6 +741,19 @@ export class SequenceWaaS {
return res.data
}

async getAdopter(): Promise<string> {
await this.updateTimeDrift()

const intent = await this.waas.getAdopter()
const res = await this.sendIntent(intent)

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

return res.data.adopterAddress
}

async useIdentifier<T extends CommonAuthArgs>(args: T): Promise<T & { identifier: string }> {
if (args.identifier) {
return args as T & { identifier: string }
Expand Down Expand Up @@ -844,6 +860,13 @@ export class SequenceWaaS {
return this.trySendIntent(args, intent, isFeeOptionsResponse)
}

async adoptChildWallet(args: WithSimpleNetwork<AdoptChildWalletArgs> & CommonAuthArgs) {
await this.updateTimeDrift()

const intent = await this.waas.adoptChildWallet(args)
return this.trySendIntent(args, intent, isChildWalletAdoptedResponse)
}

async networkList(): Promise<NetworkList> {
const networks: NetworkList = []
const chainList = await this.client.chainList({
Expand Down
20 changes: 20 additions & 0 deletions packages/waas/src/base.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {
adoptChildWallet,
AdoptChildWalletArgs,
changeIntentTime,
closeSession,
combineTransactionIntents,
feeOptions,
finishValidateSession,
getAdopter,
getIdToken,
getSession,
getTransactionReceipt,
Expand Down Expand Up @@ -589,6 +592,23 @@ export class SequenceWaaSBase {
return this.signIntent(intent)
}

async adoptChildWallet(args: WithSimpleNetwork<AdoptChildWalletArgs>) {
const intent = adoptChildWallet({
wallet: await this.getWalletAddress(),
...args,
lifespan: DEFAULT_LIFESPAN,
})
return this.signIntent(intent)
}

async getAdopter() {
const intent = getAdopter({
wallet: await this.getWalletAddress(),
lifespan: DEFAULT_LIFESPAN,
})
return this.signIntent(intent)
}

async batch(intents: Intent<IntentDataSendTransaction>[]): Promise<SignedIntent<IntentDataSendTransaction>> {
const combined = combineTransactionIntents(intents)
return this.signIntent(combined)
Expand Down
22 changes: 22 additions & 0 deletions packages/waas/src/intents/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
IntentDataSendTransaction,
IntentResponseAccountFederated,
IntentResponseAccountList,
IntentResponseAdopter,
IntentResponseAuthInitiated,
IntentResponseChildWalletAdopted,
IntentResponseCode,
IntentResponseGetSession,
IntentResponseIdToken,
Expand Down Expand Up @@ -130,6 +132,8 @@ export type GetSessionResponse = Response<IntentResponseCode.getSessionResponse,
export type LinkAccountResponse = Response<IntentResponseCode.accountFederated, IntentResponseAccountFederated>
export type ListAccountsResponse = Response<IntentResponseCode.accountList, IntentResponseAccountList>
export type IdTokenResponse = Response<IntentResponseCode.idToken, IntentResponseIdToken>
export type AdopterResponse = Response<IntentResponseCode.adopter, IntentResponseAdopter>
export type ChildWalletAdoptedResponse = Response<IntentResponseCode.childWalletAdopted, IntentResponseChildWalletAdopted>

export function isInitiateAuthResponse(receipt: any): receipt is InitiateAuthResponse {
return (
Expand Down Expand Up @@ -288,3 +292,21 @@ export function isGetIdTokenResponse(receipt: any): receipt is IdTokenResponse {
typeof receipt.data.idToken === 'string'
)
}

export function isGetAdopterResponse(receipt: any): receipt is AdopterResponse {
return (
typeof receipt === 'object' &&
receipt.code === IntentResponseCode.adopter &&
typeof receipt.data === 'object' &&
typeof receipt.data.adopterAddress === 'string'
)
}

export function isChildWalletAdoptedResponse(receipt: any): receipt is ChildWalletAdoptedResponse {
return (
typeof receipt === 'object' &&
receipt.code === IntentResponseCode.childWalletAdopted &&
typeof receipt.data === 'object' &&
typeof receipt.data.adopterAddress === 'string'
)
}
16 changes: 15 additions & 1 deletion packages/waas/src/intents/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
IntentDataSessionAuthProof,
IntentDataInitiateAuth,
IntentDataGetIdToken,
IntentName
IntentName,
IntentDataAdoptChildWallet,
IntentDataGetAdopter
} from '../clients/intent.gen'

interface BaseArgs {
Expand Down Expand Up @@ -69,3 +71,15 @@ export type GetIdTokenArgs = BaseArgs & IntentDataGetIdToken
export function getIdToken({ lifespan, ...data }: GetIdTokenArgs): Intent<IntentDataGetIdToken> {
return makeIntent(IntentName.getIdToken, lifespan, data)
}

export type AdoptChildWalletArgs = BaseArgs & IntentDataAdoptChildWallet

export function adoptChildWallet({ lifespan, ...data }: AdoptChildWalletArgs): Intent<IntentDataAdoptChildWallet> {
return makeIntent(IntentName.adoptChildWallet, lifespan, data)
}

export type GetAdopterArgs = BaseArgs & IntentDataGetAdopter

export function getAdopter({ lifespan, ...data }: GetAdopterArgs): Intent<IntentDataGetAdopter> {
return makeIntent(IntentName.getAdopter, lifespan, data)
}

0 comments on commit a7fccb7

Please sign in to comment.