Skip to content

Commit

Permalink
Update requestRedemption fn in threshold-ts lib
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
r-czajkowski committed Jun 14, 2023
1 parent 8512568 commit c56ad55
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/threshold-ts/tbtc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import {
ZERO,
isPublicKeyHashTypeAddress,
isSameETHAddress,
isPayToScriptHashTypeAddress,
} from "../utils"
import {
Client,
computeHash160,
createOutputScriptFromAddress,
decodeBitcoinAddress,
TransactionHash,
UnspentTransactionOutput,
Expand Down Expand Up @@ -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<string>
}
Expand Down Expand Up @@ -667,14 +669,24 @@ export class TBTC implements ITBTC {
redeemer: string,
walletPublicKey: string,
mainUtxo: UnspentTransactionOutput,
redeemerOutputScript: string,
btcAddress: string,
amount: BigNumberish
): Promise<string> => {
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,
Expand Down

0 comments on commit c56ad55

Please sign in to comment.