Skip to content

Commit

Permalink
Used Hex for TBTC token functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Oct 17, 2023
1 parent c66dc07 commit 21c5531
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 40 deletions.
7 changes: 3 additions & 4 deletions typescript/src/lib/contracts/tbtc-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ export interface TBTCToken {
* @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.
* will be locked to. Must not be prepended with length.
* @param amount - The amount to be redeemed with the precision of the tBTC
* on-chain token contract.
* @returns Transaction hash of the approve and call transaction.
*/
requestRedemption(
walletPublicKey: string,
walletPublicKey: Hex,
mainUtxo: BitcoinUtxo,
redeemerOutputScript: string,
redeemerOutputScript: Hex,
amount: BigNumber
): Promise<Hex>
}
19 changes: 9 additions & 10 deletions typescript/src/lib/ethereum/tbtc-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ export class EthereumTBTCToken
* @see {TBTCToken#requestRedemption}
*/
async requestRedemption(
walletPublicKey: string,
walletPublicKey: Hex,
mainUtxo: BitcoinUtxo,
redeemerOutputScript: string,
redeemerOutputScript: Hex,
amount: BigNumber
): Promise<Hex> {
const redeemer = await this._instance?.signer?.getAddress()
Expand Down Expand Up @@ -103,9 +103,9 @@ export class EthereumTBTCToken

private buildRequestRedemptionData(
redeemer: EthereumAddress,
walletPublicKey: string,
walletPublicKey: Hex,
mainUtxo: BitcoinUtxo,
redeemerOutputScript: string
redeemerOutputScript: Hex
): Hex {
const {
walletPublicKeyHash,
Expand Down Expand Up @@ -133,13 +133,12 @@ export class EthereumTBTCToken
}

private buildBridgeRequestRedemptionData(
walletPublicKey: string,
walletPublicKey: Hex,
mainUtxo: BitcoinUtxo,
redeemerOutputScript: string
redeemerOutputScript: Hex
) {
const walletPublicKeyHash = BitcoinHashUtils.computeHash160(
Hex.from(walletPublicKey)
).toPrefixedString()
const walletPublicKeyHash =
BitcoinHashUtils.computeHash160(walletPublicKey).toPrefixedString()

const mainUtxoParam = {
// The Ethereum Bridge expects this hash to be in the Bitcoin internal
Expand All @@ -150,7 +149,7 @@ export class EthereumTBTCToken
}

// Convert the output script to raw bytes buffer.
const rawRedeemerOutputScript = Buffer.from(redeemerOutputScript, "hex")
const rawRedeemerOutputScript = redeemerOutputScript.toBuffer()
// Prefix the output script bytes buffer with 0x and its own length.
const prefixedRawRedeemerOutputScript = `0x${Buffer.concat([
Buffer.from([rawRedeemerOutputScript.length]),
Expand Down
4 changes: 2 additions & 2 deletions typescript/src/services/redemptions/redemptions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export class RedemptionsService {
)

const txHash = await this.tbtcContracts.tbtcToken.requestRedemption(
walletPublicKey,
Hex.from(walletPublicKey),
mainUtxo,
redeemerOutputScript,
Hex.from(redeemerOutputScript),
amount
)

Expand Down
3 changes: 2 additions & 1 deletion typescript/test/data/redemption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export const walletPrivateKey =
* Public key of the wallet in the compressed form corresponding to
* walletPrivateKey.
*/
export const walletPublicKey =
export const walletPublicKey = Hex.from(
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9"
)

/**
* P2PKH address corresponding to walletPrivateKey.
Expand Down
21 changes: 11 additions & 10 deletions typescript/test/lib/ethereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,9 @@ describe("Ethereum", () => {
vault: EthereumAddress.from(
"0x24BE35e7C04E2e0a628614Ce0Ed58805e1C894F7"
),
walletPublicKey:
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9",
walletPublicKey: Hex.from(
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9"
),
mainUtxo: {
transactionHash: BitcoinTxHash.from(
"f8eaf242a55ea15e602f9f990e33f67f99dfbe25d1802bbde63cc1caabf99668"
Expand All @@ -540,10 +541,12 @@ describe("Ethereum", () => {
redeemer: EthereumAddress.from(signer.address),
amount: BigNumber.from(10000),
redeemerOutputScript: {
unprefixed:
"0020cdbf909e935c855d3e8d1b61aeb9c5e3c03ae8021b286839b1a72f2e48fdba70",
prefixed:
"0x220020cdbf909e935c855d3e8d1b61aeb9c5e3c03ae8021b286839b1a72f2e48fdba70",
unprefixed: Hex.from(
"0020cdbf909e935c855d3e8d1b61aeb9c5e3c03ae8021b286839b1a72f2e48fdba70"
),
prefixed: Hex.from(
"220020cdbf909e935c855d3e8d1b61aeb9c5e3c03ae8021b286839b1a72f2e48fdba70"
),
},
}

Expand Down Expand Up @@ -572,13 +575,11 @@ describe("Ethereum", () => {
["address", "bytes20", "bytes32", "uint32", "uint64", "bytes"],
[
redeemer.identifierHex,
BitcoinHashUtils.computeHash160(
Hex.from(walletPublicKey)
).toPrefixedString(),
BitcoinHashUtils.computeHash160(walletPublicKey).toPrefixedString(),
mainUtxo.transactionHash.reverse().toPrefixedString(),
mainUtxo.outputIndex,
mainUtxo.value,
redeemerOutputScript.prefixed,
redeemerOutputScript.prefixed.toPrefixedString(),
]
)

Expand Down
2 changes: 1 addition & 1 deletion typescript/test/services/maintenance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2533,7 +2533,7 @@ describe("Maintenance", () => {
await maintenanceService.spv.submitRedemptionProof(
transactionHash,
mainUtxo,
walletPublicKey
walletPublicKey.toString()
)
})

Expand Down
15 changes: 7 additions & 8 deletions typescript/test/services/redemptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,20 @@ describe("Redemptions", () => {
tbtcContracts = new MockTBTCContracts()
bitcoinClient = new MockBitcoinClient()

const walletPublicKeyHash = BitcoinHashUtils.computeHash160(
Hex.from(walletPublicKey)
)
const walletPublicKeyHash =
BitcoinHashUtils.computeHash160(walletPublicKey)

// Prepare NewWalletRegisteredEvent history. Set only relevant fields.
tbtcContracts.bridge.newWalletRegisteredEvents = [
{
walletPublicKeyHash: walletPublicKeyHash,
walletPublicKeyHash,
} as NewWalletRegisteredEvent,
]

// Prepare wallet data in the Bridge. Set only relevant fields.
tbtcContracts.bridge.setWallet(walletPublicKeyHash.toPrefixedString(), {
state: WalletState.Live,
walletPublicKey: Hex.from(walletPublicKey),
walletPublicKey,
pendingRedemptionsValue: BigNumber.from(0),
mainUtxoHash: tbtcContracts.bridge.buildUtxoHash(mainUtxo),
} as Wallet)
Expand Down Expand Up @@ -119,7 +118,7 @@ describe("Redemptions", () => {
expect(tokenLog[0]).to.deep.equal({
walletPublicKey,
mainUtxo,
redeemerOutputScript: redeemerOutputScript.toString(),
redeemerOutputScript,
amount,
})
})
Expand Down Expand Up @@ -156,7 +155,7 @@ describe("Redemptions", () => {
redemptionRequest.redeemerOutputScript,
BitcoinNetwork.Testnet
),
walletPublicKey,
walletPublicKey.toString(),
"pending"
)

Expand Down Expand Up @@ -194,7 +193,7 @@ describe("Redemptions", () => {
redemptionRequest.redeemerOutputScript,
BitcoinNetwork.Testnet
),
walletPublicKey,
walletPublicKey.toString(),
"timedOut"
)

Expand Down
8 changes: 4 additions & 4 deletions typescript/test/utils/mock-tbtc-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { BitcoinUtxo } from "../../src/lib/bitcoin"
import { EthereumAddress } from "../../src"

interface RequestRedemptionLog {
walletPublicKey: string
walletPublicKey: Hex
mainUtxo: BitcoinUtxo
redeemerOutputScript: string
redeemerOutputScript: Hex
amount: BigNumber
}

Expand All @@ -23,9 +23,9 @@ export class MockTBTCToken implements TBTCToken {
}

async requestRedemption(
walletPublicKey: string,
walletPublicKey: Hex,
mainUtxo: BitcoinUtxo,
redeemerOutputScript: string,
redeemerOutputScript: Hex,
amount: BigNumber
): Promise<Hex> {
this._requestRedemptionLog.push({
Expand Down

0 comments on commit 21c5531

Please sign in to comment.