Skip to content

Commit

Permalink
Improve public key fetching for closed/terminated wallets
Browse files Browse the repository at this point in the history
The current logic used to fetch wallet public keys was throwing errors while
used for `Closed` or `Terminated` wallets. This is because the `WalletRegistry`
contract does no longer hold the public key for such wallets. In effect,
the exception was propagated upstream and caused problems while searching
wallets for redemption.

Here we fix that problem by making the public key field optional and keeping
it undefined for closed/terminated wallets. That way, the client code can
detect this fact and omit closed/terminated wallets during processing.
  • Loading branch information
lukasz-zimnoch committed Apr 22, 2024
1 parent 503f8ae commit 7b4f6d6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 4 additions & 2 deletions typescript/src/lib/contracts/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,11 @@ export interface Wallet {
*/
ecdsaWalletID: Hex
/**
* Compressed public key of the ECDSA Wallet.
* Compressed public key of the ECDSA Wallet. If the wallet is Closed
* or Terminated, this field is empty as the public key is removed from the
* WalletRegistry.
*/
walletPublicKey: Hex
walletPublicKey?: Hex
/**
* Latest wallet's main UTXO hash.
*/
Expand Down
25 changes: 18 additions & 7 deletions typescript/src/lib/ethereum/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,15 +512,26 @@ export class EthereumBridge
return walletPublicKey
}

private async getWalletCompressedPublicKey(ecdsaWalletID: Hex): Promise<Hex> {
private async getWalletCompressedPublicKey(
ecdsaWalletID: Hex
): Promise<Hex | undefined> {
const walletRegistry = await this.walletRegistry()
const uncompressedPublicKey = await walletRegistry.getWalletPublicKey(
ecdsaWalletID
)

return Hex.from(
BitcoinPublicKeyUtils.compressPublicKey(uncompressedPublicKey)
)
try {
const uncompressedPublicKey = await walletRegistry.getWalletPublicKey(
ecdsaWalletID
)

return Hex.from(
BitcoinPublicKeyUtils.compressPublicKey(uncompressedPublicKey)
)
} catch (error) {
console.log(
`cannot get wallet public key for ${ecdsaWalletID}; error: ${error}`
)

return undefined
}
}

// eslint-disable-next-line valid-jsdoc
Expand Down
2 changes: 1 addition & 1 deletion typescript/src/services/redemptions/redemptions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class RedemptionsService {
await this.tbtcContracts.bridge.wallets(walletPublicKeyHash)

// Wallet must be in Live state.
if (state !== WalletState.Live) {
if (state !== WalletState.Live || !walletPublicKey) {
console.debug(
`Wallet is not in Live state ` +
`(wallet public key hash: ${walletPublicKeyHash.toString()}). ` +
Expand Down

0 comments on commit 7b4f6d6

Please sign in to comment.