Skip to content

Commit

Permalink
WalletLink: use getRootKeyForWallet to check if wallet is linked to a…
Browse files Browse the repository at this point in the history
…ny root (#1106)

User A links wallet X.
User B tries to link wallet X.

Previously, this client check would pass b/c it's comparing X to root
key B, but X is linked to A, and then the transaction would go on to
revert.

Now the client check will fail b/c X is already linked, so no
transaction attempt will be made.
  • Loading branch information
salzbrenner authored Oct 14, 2024
1 parent 03cf3fe commit 339c62a
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions packages/web3/src/v3/WalletLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,27 @@ export class WalletLink {
}
}

private async assertNotAlreadyLinked(rootKey: ethers.Signer, wallet: ethers.Signer | Address) {
const rootKeyAddress = await rootKey.getAddress()
public async isLinked(walletAddress: string): Promise<boolean> {
const rootKeyAddress = await this.walletLinkShim.read.getRootKeyForWallet(walletAddress)

return rootKeyAddress !== INVALID_ADDRESS
}

private async assertNotLinked(wallet: ethers.Signer | Address) {
const walletAddress = typeof wallet === 'string' ? wallet : await wallet.getAddress()
const isLinkedAlready = await this.walletLinkShim.read.checkIfLinked(
rootKeyAddress,
walletAddress,
)

if (isLinkedAlready) {
if (await this.isLinked(walletAddress)) {
throw new WalletAlreadyLinkedError()
}

return { rootKeyAddress, walletAddress }
return { walletAddress }
}

private async assertAlreadyLinked(rootKey: ethers.Signer, walletAddress: string) {
const rootKeyAddress = await rootKey.getAddress()
const isLinkedAlready = await this.walletLinkShim.read.checkIfLinked(
rootKeyAddress,
walletAddress,
)
if (!isLinkedAlready) {
private async assertLinked(walletAddress: string) {
if (!(await this.isLinked(walletAddress))) {
throw new WalletNotLinkedError()
}
return { rootKeyAddress, walletAddress }
return { walletAddress }
}

private generateRootKeySignatureForWallet({
Expand Down Expand Up @@ -111,7 +107,8 @@ export class WalletLink {
rootKey: ethers.Signer,
wallet: ethers.Signer | Address,
) {
const { rootKeyAddress, walletAddress } = await this.assertNotAlreadyLinked(rootKey, wallet)
const { walletAddress } = await this.assertNotLinked(wallet)
const rootKeyAddress = await rootKey.getAddress()

const nonce = await this.walletLinkShim.read.getLatestNonceForRootKey(rootKeyAddress)
const rootKeySignature = await this.generateRootKeySignatureForCallerData({
Expand All @@ -134,7 +131,8 @@ export class WalletLink {
rootKey: ethers.Signer,
wallet: ethers.Signer,
) {
const { rootKeyAddress, walletAddress } = await this.assertNotAlreadyLinked(rootKey, wallet)
const { walletAddress } = await this.assertNotLinked(wallet)
const rootKeyAddress = await rootKey.getAddress()

const nonce = await this.walletLinkShim.read.getLatestNonceForRootKey(rootKeyAddress)

Expand Down Expand Up @@ -259,7 +257,8 @@ export class WalletLink {
}

private async generateRemoveLinkData(rootKey: ethers.Signer, walletAddress: string) {
const { rootKeyAddress } = await this.assertAlreadyLinked(rootKey, walletAddress)
await this.assertLinked(walletAddress)
const rootKeyAddress = await rootKey.getAddress()
const nonce = await this.walletLinkShim.read.getLatestNonceForRootKey(rootKeyAddress)
const { domain, types, value } = createEip712LinkedWalletdData({
domain: this.eip712Domain,
Expand Down

0 comments on commit 339c62a

Please sign in to comment.