Skip to content

Commit

Permalink
Remove the usage of @keep-network/tbtc-v2
Browse files Browse the repository at this point in the history
Remove all the import of `@keep-netowrk/tbtc-v2` accross the whole dApp. The
only place left with such import are tests, which also should be addressed after
we updated the lib.
  • Loading branch information
michalsmiarowski committed Nov 13, 2023
1 parent dd2bfdb commit 1ce6350
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 63 deletions.
19 changes: 2 additions & 17 deletions src/components/Modal/TbtcMintingConfirmationModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@ import { Skeleton } from "@chakra-ui/react"
import MintingTransactionDetails from "../../../pages/tBTC/Bridge/components/MintingTransactionDetails"
import { MintingStep } from "../../../types/tbtc"
import { useThreshold } from "../../../contexts/ThresholdContext"
import { DepositScriptParameters } from "@keep-network/tbtc-v2.ts/dist/src/deposit"
import {
decodeBitcoinAddress,
UnspentTransactionOutput,
} from "@keep-network/tbtc-v2.ts/dist/src/bitcoin"
import { useRevealDepositTransaction } from "../../../hooks/tbtc"
import { BigNumber } from "ethers"
import { getChainIdentifier } from "../../../threshold-ts/utils"
import { InlineTokenBalance } from "../../TokenBalance"
import { BridgeContractLink } from "../../tBTC"
import { BitcoinUtxo } from "tbtc-sdk-v2"
Expand All @@ -37,17 +31,8 @@ const TbtcMintingConfirmationModal: FC<TbtcMintingConfirmationModalProps> = ({
utxo,
closeModal,
}) => {
const {
updateState,
tBTCMintAmount,
btcRecoveryAddress,
ethAddress,
refundLocktime,
walletPublicKeyHash,
blindingFactor,
mintingFee,
thresholdNetworkFee,
} = useTbtcState()
const { updateState, tBTCMintAmount, mintingFee, thresholdNetworkFee } =
useTbtcState()
const threshold = useThreshold()

const onSuccessfulDepositReveal = () => {
Expand Down
27 changes: 16 additions & 11 deletions src/components/Modal/TbtcRecoveryFileModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ import InfoBox from "../../InfoBox"
import { BaseModalProps } from "../../../types"
import btcJsonFile from "../../../static/images/tbtc-json-file.png"
import withBaseModal from "../withBaseModal"
import { DepositScriptParameters } from "@keep-network/tbtc-v2.ts/dist/src/deposit"
import { downloadFile } from "../../../web3/utils"
import { getChainIdentifier } from "../../../threshold-ts/utils"
import { BridgeContractLink } from "../../tBTC"
import { useTbtcState } from "../../../hooks/useTbtcState"
import { ChainIdentifier } from "tbtc-sdk-v2"

export type RecoveryJsonFileData = {
depositor: ChainIdentifier
blindingFactor: string
walletPublicKeyHash: string
refundPublicKeyHash: string
refundLocktime: string
btcRecoveryAddress: string
}

const TbtcRecoveryFileModalModal: FC<
BaseModalProps & {
Expand Down Expand Up @@ -47,21 +56,17 @@ const TbtcRecoveryFileModalModal: FC<
closeModal()
}

const depositScriptParameters: DepositScriptParameters = {
depositor: getChainIdentifier(ethAddress),
blindingFactor,
walletPublicKeyHash,
refundPublicKeyHash,
refundLocktime,
}

const handleDownloadClick = () => {
const date = new Date().toISOString().split("T")[0]

const fileName = `${ethAddress}_${btcDepositAddress}_${date}`

const finalData = {
...depositScriptParameters,
const finalData: RecoveryJsonFileData = {
depositor: getChainIdentifier(ethAddress),
blindingFactor,
walletPublicKeyHash,
refundPublicKeyHash,
refundLocktime,
btcRecoveryAddress: btcRecoveryAddress,
}
downloadFile(JSON.stringify(finalData), fileName, "text/json")
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/tbtc/useSubscribeToRedemptionsCompletedEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Hex } from "@keep-network/tbtc-v2.ts"
// TODO: Refactor this so that it's only imported in our threshold-ts lib
import { Hex } from "tbtc-sdk-v2"
import { Event } from "ethers"
import { useSubscribeToContractEvent } from "../../web3/hooks"
import { useBridgeContract } from "./useBridgeContract"
Expand Down
7 changes: 3 additions & 4 deletions src/pages/tBTC/Bridge/Minting/InitiateMinting.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { FC } from "react"
import { Alert, AlertIcon, BodyMd, Button } from "@threshold-network/components"
import { BodyMd, Button } from "@threshold-network/components"
import { BridgeProcessCardTitle } from "../components/BridgeProcessCardTitle"
import { MintingStep } from "../../../../types/tbtc"
import { BridgeProcessCardSubTitle } from "../components/BridgeProcessCardSubTitle"
import { AlertDescription } from "@chakra-ui/react"
import { useModal } from "../../../../hooks/useModal"
import { ModalType } from "../../../../enums"
import withOnlyConnectedWallet from "../../../../components/withOnlyConnectedWallet"
import { UnspentTransactionOutput } from "@keep-network/tbtc-v2.ts/dist/src/bitcoin"
import { BitcoinUtxo } from "tbtc-sdk-v2"

const InitiateMintingComponent: FC<{
utxo: UnspentTransactionOutput
utxo: BitcoinUtxo
onPreviousStepClick: (previosuStep: MintingStep) => void
}> = ({ utxo, onPreviousStepClick }) => {
const { openModal } = useModal()
Expand Down
4 changes: 2 additions & 2 deletions src/pages/tBTC/Bridge/ResumeDeposit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@threshold-network/components"
import { useWeb3React } from "@web3-react/core"
import { FormikErrors, FormikProps, withFormik } from "formik"
import { DepositScriptParameters } from "@keep-network/tbtc-v2.ts/dist/src/deposit"
import { RecoveryJsonFileData } from "../../../components/Modal/TbtcRecoveryFileModal"
import { useNavigate } from "react-router-dom"
import { PageComponent } from "../../../types"
import { BridgeProcessCardTitle } from "./components/BridgeProcessCardTitle"
Expand Down Expand Up @@ -147,7 +147,7 @@ const ResumeDepositForm: FC<FormikProps<FormValues>> = (props) => {
)
}

type DepositDetails = DepositScriptParameters & { btcRecoveryAddress: string }
type DepositDetails = RecoveryJsonFileData

type FormValues = {
depositParameters: DepositDetails | null
Expand Down
24 changes: 8 additions & 16 deletions src/threshold-ts/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import {
Client,
UnspentTransactionOutput,
} from "@keep-network/tbtc-v2.ts/dist/src/bitcoin"
import { BitcoinNetwork } from "@keep-network/tbtc-v2.ts/dist/src/bitcoin-network"
import {
ClientOptions,
Credentials,
} from "@keep-network/tbtc-v2.ts/dist/src/electrum"
ElectrumClientOptions,
ElectrumCredentials,
BitcoinClient,
BitcoinNetwork,
} from "tbtc-sdk-v2"
import { providers, Signer } from "ethers"
import { BitcoinClient } from "tbtc-sdk-v2"

export interface EthereumConfig {
providerOrSigner: providers.Provider | Signer
Expand All @@ -17,9 +13,9 @@ export interface EthereumConfig {
account?: string
}

export type BitcoinClientCredentials = Credentials
export type BitcoinClientCredentials = ElectrumCredentials

export type BitcoinClientOptions = ClientOptions
export type BitcoinClientOptions = ElectrumClientOptions

export interface BitcoinConfig {
/**
Expand All @@ -32,7 +28,7 @@ export interface BitcoinConfig {
/**
* If we want to mock client then we should pass the mock here
*/
client?: BitcoinClient | Client
client?: BitcoinClient
// TODO: Remove deprecated `Client` fallback when the migration will be fully completed

/**
Expand All @@ -52,7 +48,3 @@ export interface ThresholdConfig {
}

export { BitcoinNetwork }

export type UnspentTransactionOutputPlainObject = {
[key in keyof UnspentTransactionOutput]: string
}
2 changes: 1 addition & 1 deletion src/threshold-ts/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
getAddress as ethersGetAddress,
} from "@ethersproject/address"
import { AddressZero } from "@ethersproject/constants"
import { EthereumAddress } from "@keep-network/tbtc-v2.ts"
import { EthereumAddress } from "tbtc-sdk-v2"

export const unprefixedAndUncheckedAddress = (address: string): string => {
const prefix = address.substring(0, 2)
Expand Down
38 changes: 30 additions & 8 deletions src/threshold-ts/utils/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { BitcoinNetwork } from "@keep-network/tbtc-v2.ts"
import { TransactionHash } from "@keep-network/tbtc-v2.ts/dist/src/bitcoin"
export {
computeHash160,
createOutputScriptFromAddress,
createAddressFromOutputScript,
} from "@keep-network/tbtc-v2.ts/dist/src/bitcoin"
import { BitcoinNetwork } from "tbtc-sdk-v2"
// export {
// createOutputScriptFromAddress as createOutputScriptFromAddress2,
// createAddressFromOutputScript as createAddressFromOutputScript2,
// } from "@keep-network/tbtc-v2.ts/dist/src/bitcoin"
import {
AddressType,
getAddressInfo,
Network,
validate,
} from "bitcoin-address-validation"
import { BitcoinTxHash, Hex } from "tbtc-sdk-v2"
import {
BitcoinTxHash,
Hex,
BitcoinHashUtils,
BitcoinAddressConverter,
} from "tbtc-sdk-v2"

export const BITCOIN_PRECISION = 8

Expand Down Expand Up @@ -57,3 +60,22 @@ export const prependScriptPubKeyByLength = (scriptPubKey: string) => {
rawRedeemerOutputScript,
]).toString("hex")}`
}

export const computeHash160 = (text: string) => {
return BitcoinHashUtils.computeHash160(Hex.from(text))
}

// TODO: Make sure those two return the same value as the one used with old sdk.
export const createOutputScriptFromAddress = (
address: string,
bitcoinNetwork: BitcoinNetwork
) => {
return BitcoinAddressConverter.addressToOutputScript(address, bitcoinNetwork)
}

export const createAddressFromOutputScript = (
script: Hex,
bitcoinNetwork: BitcoinNetwork
) => {
return BitcoinAddressConverter.outputScriptToAddress(script, bitcoinNetwork)
}
5 changes: 2 additions & 3 deletions src/types/tbtc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { UnspentTransactionOutput } from "@keep-network/tbtc-v2.ts/dist/src/bitcoin"
import { UpdateStateActionPayload } from "./state"
import { FetchingState } from "."
import { BridgeActivity, BridgeProcess } from "../threshold-ts/tbtc"
export type { UnspentTransactionOutputPlainObject } from "../threshold-ts/types"
import { BitcoinUtxo } from "tbtc-sdk-v2"

export interface TbtcState {
mintingStep: MintingStep
Expand All @@ -13,7 +12,7 @@ export interface TbtcState {
refundLocktime: string
blindingFactor: string
walletPublicKeyHash: string
utxo: UnspentTransactionOutput
utxo: BitcoinUtxo
txConfirmations: number
nextBridgeCrossingInUnix?: number
depositRevealedTxHash?: string
Expand Down

0 comments on commit 1ce6350

Please sign in to comment.