Skip to content

Commit

Permalink
Used Hex for walletPublicKeyHash and redeemerOutputScript
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Oct 17, 2023
1 parent 023170f commit ce3dcff
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 45 deletions.
11 changes: 5 additions & 6 deletions typescript/src/lib/contracts/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,10 @@ export interface RedemptionRequest {
redeemer: ChainIdentifier

/**
* The output script the redeemed Bitcoin funds are locked to. It is un-prefixed
* and is not prepended with length.
* The output script the redeemed Bitcoin funds are locked to. It is not
* prepended with length.
*/
redeemerOutputScript: string
redeemerOutputScript: Hex

/**
* The amount of Bitcoins in satoshis that is requested to be redeemed.
Expand Down Expand Up @@ -334,10 +334,9 @@ export type RedemptionRequestedEvent = Omit<
"requestedAt"
> & {
/**
* Public key hash of the wallet that is meant to handle the redemption. Must
* be an unprefixed hex string (without 0x prefix).
* Public key hash of the wallet that is meant to handle the redemption.
*/
walletPublicKeyHash: string
walletPublicKeyHash: Hex
} & ChainEvent

/* eslint-disable no-unused-vars */
Expand Down
6 changes: 3 additions & 3 deletions typescript/src/lib/ethereum/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export class EthereumBridge
): RedemptionRequest {
return {
redeemer: EthereumAddress.from(request.redeemer),
redeemerOutputScript: redeemerOutputScript.toString(),
redeemerOutputScript: redeemerOutputScript,
requestedAmount: BigNumber.from(request.requestedAmount),
treasuryFee: BigNumber.from(request.treasuryFee),
txMaxFee: BigNumber.from(request.txMaxFee),
Expand Down Expand Up @@ -653,9 +653,9 @@ export class EthereumBridge
blockNumber: BigNumber.from(event.blockNumber).toNumber(),
blockHash: Hex.from(event.blockHash),
transactionHash: Hex.from(event.transactionHash),
walletPublicKeyHash: Hex.from(event.args!.walletPubKeyHash).toString(),
walletPublicKeyHash: Hex.from(event.args!.walletPubKeyHash),
redeemer: EthereumAddress.from(event.args!.redeemer),
redeemerOutputScript: redeemerOutputScript,
redeemerOutputScript: Hex.from(redeemerOutputScript),
requestedAmount: BigNumber.from(event.args!.requestedAmount),
treasuryFee: BigNumber.from(event.args!.treasuryFee),
txMaxFee: BigNumber.from(event.args!.txMaxFee),
Expand Down
4 changes: 2 additions & 2 deletions typescript/src/services/maintenance/wallet-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ class Redemption {

redemptionRequests.push({
...redemptionRequest,
redeemerOutputScript: redeemerOutputScript,
redeemerOutputScript: Hex.from(redeemerOutputScript),
})
}

Expand Down Expand Up @@ -710,7 +710,7 @@ class Redemption {
txTotalFee = txTotalFee.add(request.txMaxFee)

psbt.addOutput({
script: Buffer.from(request.redeemerOutputScript, "hex"),
script: request.redeemerOutputScript.toBuffer(),
value: outputValue.toNumber(),
})
}
Expand Down
53 changes: 36 additions & 17 deletions typescript/test/data/redemption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ export const singleP2PKHRedemptionWithWitnessChange: RedemptionTestData = {
"82883a4c7a8dd73ef165deb402d432613615ced4"
),
// script for testnet P2PKH address mmTeMR8RKu6QzMGTG4ipA71uewm3EuJng5
redeemerOutputScript:
"76a9144130879211c54df460e484ddf9aac009cb38ee7488ac",
redeemerOutputScript: Hex.from(
"76a9144130879211c54df460e484ddf9aac009cb38ee7488ac"
),
requestedAmount: BigNumber.from(10000),
treasuryFee: BigNumber.from(1000),
txMaxFee: BigNumber.from(1600),
Expand Down Expand Up @@ -141,7 +142,9 @@ export const singleP2WPKHRedemptionWithWitnessChange: RedemptionTestData = {
"82883a4c7a8dd73ef165deb402d432613615ced4"
),
// script for testnet P2WPKH address tb1qgycg0ys3c4xlgc8ysnwln2kqp89n3mn5ts7z3l
redeemerOutputScript: "00144130879211c54df460e484ddf9aac009cb38ee74",
redeemerOutputScript: Hex.from(
"00144130879211c54df460e484ddf9aac009cb38ee74"
),
requestedAmount: BigNumber.from(15000),
treasuryFee: BigNumber.from(1100),
txMaxFee: BigNumber.from(1700),
Expand Down Expand Up @@ -197,7 +200,9 @@ export const singleP2SHRedemptionWithWitnessChange: RedemptionTestData = {
"82883a4c7a8dd73ef165deb402d432613615ced4"
),
// script for testnet P2SH address 2Mxy76sc1qAxiJ1fXMXDXqHvVcPLh6Lf12C
redeemerOutputScript: "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87",
redeemerOutputScript: Hex.from(
"a9143ec459d0f3c29286ae5df5fcc421e2786024277e87"
),
requestedAmount: BigNumber.from(13000),
treasuryFee: BigNumber.from(800),
txMaxFee: BigNumber.from(1700),
Expand Down Expand Up @@ -253,8 +258,9 @@ export const singleP2WSHRedemptionWithWitnessChange: RedemptionTestData = {
"82883a4c7a8dd73ef165deb402d432613615ced4"
),
// script for testnet P2WSH address tb1qs63s8nwjut4tr5t8nudgzwp4m3dpkefjzpmumn90pruce0cye2tq2jkq0y
redeemerOutputScript:
"002086a303cdd2e2eab1d1679f1a813835dc5a1b65321077cdccaf08f98cbf04ca96",
redeemerOutputScript: Hex.from(
"002086a303cdd2e2eab1d1679f1a813835dc5a1b65321077cdccaf08f98cbf04ca96"
),
requestedAmount: BigNumber.from(18000),
treasuryFee: BigNumber.from(1000),
txMaxFee: BigNumber.from(1400),
Expand Down Expand Up @@ -310,8 +316,9 @@ export const multipleRedemptionsWithWitnessChange: RedemptionTestData = {
"82883a4c7a8dd73ef165deb402d432613615ced4"
),
// script for testnet P2PKH address mmTeMR8RKu6QzMGTG4ipA71uewm3EuJng5
redeemerOutputScript:
"76a9144130879211c54df460e484ddf9aac009cb38ee7488ac",
redeemerOutputScript: Hex.from(
"76a9144130879211c54df460e484ddf9aac009cb38ee7488ac"
),
requestedAmount: BigNumber.from(18000),
treasuryFee: BigNumber.from(1000),
txMaxFee: BigNumber.from(1100),
Expand All @@ -326,7 +333,9 @@ export const multipleRedemptionsWithWitnessChange: RedemptionTestData = {
"82883a4c7a8dd73ef165deb402d432613615ced4"
),
// script for testnet P2WPKH address tb1qgycg0ys3c4xlgc8ysnwln2kqp89n3mn5ts7z3l
redeemerOutputScript: "00144130879211c54df460e484ddf9aac009cb38ee74",
redeemerOutputScript: Hex.from(
"00144130879211c54df460e484ddf9aac009cb38ee74"
),
requestedAmount: BigNumber.from(13000),
treasuryFee: BigNumber.from(800),
txMaxFee: BigNumber.from(900),
Expand All @@ -341,7 +350,9 @@ export const multipleRedemptionsWithWitnessChange: RedemptionTestData = {
"82883a4c7a8dd73ef165deb402d432613615ced4"
),
// script for testnet P2SH address 2Mxy76sc1qAxiJ1fXMXDXqHvVcPLh6Lf12C
redeemerOutputScript: "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87",
redeemerOutputScript: Hex.from(
"a9143ec459d0f3c29286ae5df5fcc421e2786024277e87"
),
requestedAmount: BigNumber.from(12000),
treasuryFee: BigNumber.from(1100),
txMaxFee: BigNumber.from(1000),
Expand All @@ -356,8 +367,9 @@ export const multipleRedemptionsWithWitnessChange: RedemptionTestData = {
"82883a4c7a8dd73ef165deb402d432613615ced4"
),
// script for testnet P2WSH address tb1qs63s8nwjut4tr5t8nudgzwp4m3dpkefjzpmumn90pruce0cye2tq2jkq0y
redeemerOutputScript:
"002086a303cdd2e2eab1d1679f1a813835dc5a1b65321077cdccaf08f98cbf04ca96",
redeemerOutputScript: Hex.from(
"002086a303cdd2e2eab1d1679f1a813835dc5a1b65321077cdccaf08f98cbf04ca96"
),
requestedAmount: BigNumber.from(15000),
treasuryFee: BigNumber.from(700),
txMaxFee: BigNumber.from(1400),
Expand Down Expand Up @@ -416,8 +428,9 @@ export const multipleRedemptionsWithoutChange: RedemptionTestData = {
"82883a4c7a8dd73ef165deb402d432613615ced4"
),
// script for testnet P2PKH address mmTeMR8RKu6QzMGTG4ipA71uewm3EuJng5
redeemerOutputScript:
"76a9144130879211c54df460e484ddf9aac009cb38ee7488ac",
redeemerOutputScript: Hex.from(
"76a9144130879211c54df460e484ddf9aac009cb38ee7488ac"
),
requestedAmount: BigNumber.from(6000),
treasuryFee: BigNumber.from(0),
txMaxFee: BigNumber.from(800),
Expand All @@ -432,7 +445,9 @@ export const multipleRedemptionsWithoutChange: RedemptionTestData = {
"82883a4c7a8dd73ef165deb402d432613615ced4"
),
// script for testnet P2WPKH address tb1qf0ulldawp79s7knz9v254j5zjyn0demfx2d0xx
redeemerOutputScript: "00144bf9ffb7ae0f8b0f5a622b154aca829126f6e769",
redeemerOutputScript: Hex.from(
"00144bf9ffb7ae0f8b0f5a622b154aca829126f6e769"
),
requestedAmount: BigNumber.from(4000),
treasuryFee: BigNumber.from(0),
txMaxFee: BigNumber.from(900),
Expand Down Expand Up @@ -491,7 +506,9 @@ export const singleP2SHRedemptionWithNonWitnessChange: RedemptionTestData = {
"82883a4c7a8dd73ef165deb402d432613615ced4"
),
// script for testnet P2SH address 2Mxy76sc1qAxiJ1fXMXDXqHvVcPLh6Lf12C
redeemerOutputScript: "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87",
redeemerOutputScript: Hex.from(
"a9143ec459d0f3c29286ae5df5fcc421e2786024277e87"
),
requestedAmount: BigNumber.from(12000),
treasuryFee: BigNumber.from(1000),
txMaxFee: BigNumber.from(1200),
Expand Down Expand Up @@ -902,7 +919,9 @@ export const findWalletForRedemptionData: {
"0xeb9af8E66869902476347a4eFe59a527a57240ED"
),
// script for testnet P2PKH address mjc2zGWypwpNyDi4ZxGbBNnUA84bfgiwYc
redeemerOutputScript: "76a9142cd680318747b720d67bf4246eb7403b476adb3488ac",
redeemerOutputScript: Hex.from(
"76a9142cd680318747b720d67bf4246eb7403b476adb3488ac"
),
requestedAmount: BigNumber.from(1000000),
treasuryFee: BigNumber.from(20000),
txMaxFee: BigNumber.from(20000),
Expand Down
10 changes: 6 additions & 4 deletions typescript/test/lib/ethereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ describe("Ethereum", () => {
redeemer: EthereumAddress.from(
"f39fd6e51aad88f6f4ce6ab8827279cfffb92266"
),
redeemerOutputScript:
"a9143ec459d0f3c29286ae5df5fcc421e2786024277e87",
redeemerOutputScript: Hex.from(
"a9143ec459d0f3c29286ae5df5fcc421e2786024277e87"
),
requestedAmount: BigNumber.from(10000),
treasuryFee: BigNumber.from(100),
txMaxFee: BigNumber.from(50),
Expand Down Expand Up @@ -124,8 +125,9 @@ describe("Ethereum", () => {
redeemer: EthereumAddress.from(
"f39fd6e51aad88f6f4ce6ab8827279cfffb92266"
),
redeemerOutputScript:
"a9143ec459d0f3c29286ae5df5fcc421e2786024277e87",
redeemerOutputScript: Hex.from(
"a9143ec459d0f3c29286ae5df5fcc421e2786024277e87"
),
requestedAmount: BigNumber.from(10000),
treasuryFee: BigNumber.from(100),
txMaxFee: BigNumber.from(50),
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 @@ -1450,7 +1450,7 @@ describe("Maintenance", () => {
it("should revert", async () => {
const redeemerOutputScripts = data.pendingRedemptions.map(
(redemption) =>
redemption.pendingRedemption.redeemerOutputScript
redemption.pendingRedemption.redeemerOutputScript.toString()
)

const walletTx = new WalletTx(
Expand Down
22 changes: 11 additions & 11 deletions typescript/test/services/redemptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe("Redemptions", () => {

await redemptionsService.requestRedemption(
BitcoinAddressConverter.outputScriptToAddress(
Hex.from(redeemerOutputScript),
redeemerOutputScript,
BitcoinNetwork.Testnet
),
amount
Expand All @@ -119,7 +119,7 @@ describe("Redemptions", () => {
expect(tokenLog[0]).to.deep.equal({
walletPublicKey,
mainUtxo,
redeemerOutputScript,
redeemerOutputScript: redeemerOutputScript.toString(),
amount,
})
})
Expand Down Expand Up @@ -153,7 +153,7 @@ describe("Redemptions", () => {
const actualRedemptionRequest =
await redemptionsService.getRedemptionRequests(
BitcoinAddressConverter.outputScriptToAddress(
Hex.from(redemptionRequest.redeemerOutputScript),
redemptionRequest.redeemerOutputScript,
BitcoinNetwork.Testnet
),
walletPublicKey,
Expand Down Expand Up @@ -191,7 +191,7 @@ describe("Redemptions", () => {
const actualRedemptionRequest =
await redemptionsService.getRedemptionRequests(
BitcoinAddressConverter.outputScriptToAddress(
Hex.from(redemptionRequest.redeemerOutputScript),
redemptionRequest.redeemerOutputScript,
BitcoinNetwork.Testnet
),
walletPublicKey,
Expand Down Expand Up @@ -377,7 +377,7 @@ describe("Redemptions", () => {

const key = MockBridge.buildRedemptionKey(
walletPublicKeyHash.toString(),
redeemerOutputScript
redeemerOutputScript.toString()
)

pendingRedemptions.set(
Expand All @@ -387,7 +387,7 @@ describe("Redemptions", () => {
tbtcContracts.bridge.setPendingRedemptions(pendingRedemptions)

result = await redemptionsService.findWalletForRedemption(
redeemerOutputScript,
redeemerOutputScript.toString(),
amount
)
})
Expand Down Expand Up @@ -466,12 +466,12 @@ describe("Redemptions", () => {

const pendingRedemption1 = MockBridge.buildRedemptionKey(
walletPublicKeyHash.toString(),
redeemerOutputScript
redeemerOutputScript.toString()
)

const pendingRedemption2 = MockBridge.buildRedemptionKey(
findWalletForRedemptionData.liveWallet.event.walletPublicKeyHash.toString(),
redeemerOutputScript
redeemerOutputScript.toString()
)

pendingRedemptions.set(
Expand All @@ -489,7 +489,7 @@ describe("Redemptions", () => {
it("should throw an error", async () => {
await expect(
redemptionsService.findWalletForRedemption(
redeemerOutputScript,
redeemerOutputScript.toString(),
amount
)
).to.be.rejectedWith(
Expand Down Expand Up @@ -825,8 +825,8 @@ export async function runRedemptionScenario(
)
)

const redeemerOutputScripts = data.pendingRedemptions.map(
(redemption) => redemption.pendingRedemption.redeemerOutputScript
const redeemerOutputScripts = data.pendingRedemptions.map((redemption) =>
redemption.pendingRedemption.redeemerOutputScript.toString()
)

const walletTx = new WalletTx(tbtcContracts, bitcoinClient, data.witness)
Expand Down
2 changes: 1 addition & 1 deletion typescript/test/utils/mock-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export class MockBridge implements Bridge {
? (redemptionsMap.get(redemptionKey) as RedemptionRequest)
: {
redeemer: EthereumAddress.from(constants.AddressZero),
redeemerOutputScript: "",
redeemerOutputScript: Hex.from(""),
requestedAmount: BigNumber.from(0),
treasuryFee: BigNumber.from(0),
txMaxFee: BigNumber.from(0),
Expand Down

0 comments on commit ce3dcff

Please sign in to comment.