Skip to content

Commit

Permalink
Update findWalletForRedemption method
Browse files Browse the repository at this point in the history
Adjust the code to the latest changes from `tbtc-v2.ts` lib. We must
pass redeemer output script to the `findWalletForRedemption` fn from the
`tbtc-v2.ts` lib. In the dapp we operate on the bitcoin address so we
convert this address to a script under the hood and then call the
function from `tbtc-v2.ts` lib.
  • Loading branch information
r-czajkowski committed Jul 10, 2023
1 parent 1ecefd4 commit 0f3518f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/pages/tBTC/Bridge/Unmint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ type UnmintFormValues = {

type UnmitnFormProps = {
onSubmitForm: (values: UnmintFormValues) => void
findRedemptionWallet: (amount: string) => Promise<RedemptionWalletData>
findRedemptionWallet: (
amount: string,
redeemerOutputScript: string
) => Promise<RedemptionWalletData>
} & UnmintFormBaseProps

const UnmintForm = withFormik<UnmitnFormProps, UnmintFormValues>({
Expand Down Expand Up @@ -245,7 +248,10 @@ const UnmintForm = withFormik<UnmitnFormProps, UnmintFormValues>({
try {
setSubmitting(true)

const wallet = await props.findRedemptionWallet(values.amount)
const wallet = await props.findRedemptionWallet(
values.amount,
values.btcAddress
)
setFieldValue("wallet", wallet, false)

props.onSubmitForm({ ...values, wallet })
Expand Down
46 changes: 40 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 @@ -220,7 +222,18 @@ export interface ITBTC {

findAllRevealedDeposits(depositor: string): Promise<RevealedDepositEvent[]>

findWalletForRedemption(amount: BigNumberish): Promise<RedemptionWalletData>
/**
* Finds the oldest active wallet that has enough BTC to handle a redemption
* request.
* @param amount The amount to be redeemed in tBTC token precision.
* @param btcAddress The Bitcoin address the redeemed funds are supposed to be
* locked on.
* @returns Promise with the wallet details needed to request a redemption.
*/
findWalletForRedemption(
amount: BigNumberish,
btcAddress: string
): Promise<RedemptionWalletData>
}

export class TBTC implements ITBTC {
Expand Down Expand Up @@ -654,15 +667,36 @@ export class TBTC implements ITBTC {
}

findWalletForRedemption = async (
amount: BigNumberish
amount: BigNumberish,
btcAddress: string
): Promise<RedemptionWalletData> => {
if (this._isValidBitcoinAddressForRedemption(btcAddress)) {
throw new Error(
"Unsupported BTC address! Supported type addresses are: P2PKH, P2WPKH, P2SH, P2WSH."
)
}

const { satoshis } = this._amountToSatoshi(amount)

const redeemerOutputScript =
createOutputScriptFromAddress(btcAddress).toString()

return await findWalletForRedemption(
satoshis,
redeemerOutputScript,
this.bitcoinNetwork,
this._bridge,
this._bitcoinClient,
this.bitcoinNetwork
this._bitcoinClient
)
}

private _isValidBitcoinAddressForRedemption = (
btcAddress: string
): boolean => {
return (
!isValidBtcAddress(btcAddress, this.bitcoinNetwork) ||
(!isPublicKeyHashTypeAddress(btcAddress) &&
!isPayToScriptHashTypeAddress(btcAddress))
)
}

Expand All @@ -672,8 +706,8 @@ export class TBTC implements ITBTC {
* `amount` is not divisible by SATOSHI_MULTIPLIER, the remainder is left on
* the caller's account when minting or unminting.
* @param {BigNumberish} amount Amount of tBTC to be converted.
* @return {AmountToSatoshiResult} The object that represnts convertible
* amount, remainder and amount in statoshi.
* @return {AmountToSatoshiResult} The object that represents convertible
* amount, remainder and amount in satoshi.
*/
private _amountToSatoshi = (amount: BigNumberish): AmountToSatoshiResult => {
const _amount = BigNumber.from(amount)
Expand Down

0 comments on commit 0f3518f

Please sign in to comment.