From c56ad55d03231e68aa94847cf38c76452caa9fed Mon Sep 17 00:00:00 2001 From: Rafal Czajkowski Date: Wed, 14 Jun 2023 09:58:12 +0200 Subject: [PATCH] Update `requestRedemption` fn in threshold-ts lib In the dapp in redemption flow the user needs to provide the BTC address that the redeemed funds will be locked to but the `requestRedemption` fn from `tbtc-v2.ts` lib expects the output script. Here we allow to pass the BTC address to our implementation of the `requestRedemption` fn and we convert the BTC address into otput script under the hood using the `createOutputScriptFromAddress` helper function from the `tbtc-v2.ts` lib. --- src/threshold-ts/tbtc/index.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/threshold-ts/tbtc/index.ts b/src/threshold-ts/tbtc/index.ts index 01b99d335..689d4b3c7 100644 --- a/src/threshold-ts/tbtc/index.ts +++ b/src/threshold-ts/tbtc/index.ts @@ -17,10 +17,12 @@ import { ZERO, isPublicKeyHashTypeAddress, isSameETHAddress, + isPayToScriptHashTypeAddress, } from "../utils" import { Client, computeHash160, + createOutputScriptFromAddress, decodeBitcoinAddress, TransactionHash, UnspentTransactionOutput, @@ -209,16 +211,16 @@ export interface ITBTC { * the compressed form (33 bytes long with 02 or 03 prefix). * @param mainUtxo The main UTXO of the wallet. Must match the main UTXO * held by the on-chain Bridge contract. - * @param redeemerOutputScript - The output script that the redeemed funds - * will be locked to. Must be un-prefixed and not prepended with length. - * @param amount The amount to be redeemed in satoshis. + * @param btcAddress - The Bitcoin address that the redeemed funds + * will be locked to. + * @param amount The amount to be redeemed in tBTC token unit. * @returns Transaction hash of the request redemption transaction. */ requestRedemption( redeemer: string, walletPublicKey: string, mainUtxo: UnspentTransactionOutput, - redeemerOutputScript: string, + btcAddress: string, amount: BigNumberish ): Promise } @@ -667,14 +669,24 @@ export class TBTC implements ITBTC { redeemer: string, walletPublicKey: string, mainUtxo: UnspentTransactionOutput, - redeemerOutputScript: string, + btcAddress: string, amount: BigNumberish ): Promise => { + if ( + !isValidBtcAddress(btcAddress, this.bitcoinNetwork) || + (!isPublicKeyHashTypeAddress(btcAddress) && + !isPayToScriptHashTypeAddress(btcAddress)) + ) { + throw new Error( + "Unsupported BTC address! Supported type addresses are: P2PKH, P2WPKH, P2SH, P2WSH." + ) + } + const tx = await requestRedemption( getChainIdentifier(redeemer), walletPublicKey, mainUtxo, - redeemerOutputScript, + createOutputScriptFromAddress(btcAddress), BigNumber.from(amount), getChainIdentifier(this._tbtcVault.address), this._bridge,