Skip to content

Signup login to existing wallet #732

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
35 changes: 27 additions & 8 deletions packages/wallet/wdk/src/sequence/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ export type LoginToPasskeyArgs = {

export type LoginArgs = LoginToWalletArgs | LoginToMnemonicArgs | LoginToPasskeyArgs

export type SignupResult =
| {
type: 'signup'
wallet: Address.Address
}
| {
type: 'login'
wallet: Address.Address
requestId: string
}

export function isLoginToWalletArgs(args: LoginArgs): args is LoginToWalletArgs {
return 'wallet' in args
}
Expand Down Expand Up @@ -337,7 +348,7 @@ export class Wallets {
}

if (commitment.isSignUp) {
await this.signUp({
const result = await this.signUp({
kind: commitment.kind,
commitment,
code: args.code,
Expand All @@ -356,14 +367,14 @@ export class Wallets {
return commitment.target
}

async signUp(args: SignupArgs) {
async signUp(args: SignupArgs): Promise<SignupResult> {
const loginSigner = await this.prepareSignUp(args)

// If there is an existing wallet callback, we check if any wallet already exist for this login signer
if (this.walletSelectionUiHandler) {
const existingWallets = await State.getWalletsFor(this.shared.sequence.stateProvider, loginSigner.signer)
if (existingWallets.length > 0) {
const result = await this.walletSelectionUiHandler({
const selectedWallet = await this.walletSelectionUiHandler({
existingWallets: existingWallets.map((w) => w.wallet),
signerAddress: await loginSigner.signer.address,
context: isAuthCodePkceArgs(args)
Expand All @@ -376,9 +387,17 @@ export class Wallets {
},
})

if (result) {
// A wallet was selected, we can exit early
return
if (selectedWallet && existingWallets.some((wallet) => wallet.wallet === selectedWallet)) {
// If a wallet was selected, we login to it
const requestId = await this.login({
wallet: selectedWallet,
})

return {
type: 'login',
wallet: selectedWallet,
requestId,
}
}
}
} else {
Expand Down Expand Up @@ -459,10 +478,10 @@ export class Wallets {
useGuard: !args.noGuard,
})

return wallet.address
return { type: 'signup', wallet: wallet.address }
}

async login(args: LoginArgs): Promise<string | undefined> {
async login(args: LoginArgs): Promise<string> {
if (isLoginToWalletArgs(args)) {
const prevWallet = await this.exists(args.wallet)
if (prevWallet) {
Expand Down