From 2c4e3262cd54b809800b5bda04e454fa1d2a60c4 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Wed, 25 Oct 2023 16:43:28 +0200 Subject: [PATCH] Verified Bitcoin recovery address type --- .../src/services/deposits/deposits-service.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/typescript/src/services/deposits/deposits-service.ts b/typescript/src/services/deposits/deposits-service.ts index f123b6876..1c1591afc 100644 --- a/typescript/src/services/deposits/deposits-service.ts +++ b/typescript/src/services/deposits/deposits-service.ts @@ -8,6 +8,7 @@ import { BitcoinClient, BitcoinHashUtils, BitcoinLocktimeUtils, + BitcoinScriptUtils, } from "../../lib/bitcoin" import { Hex } from "../../lib/utils" import { Deposit } from "./deposit" @@ -80,9 +81,17 @@ export class DepositsService { const bitcoinNetwork = await this.bitcoinClient.getNetwork() - // TODO: Only P2(W)PKH addresses can be used for recovery. The below conversion - // function ensures that but, it would be good to check it here as well - // in case the converter implementation changes. + const recoveryOutputScript = BitcoinAddressConverter.addressToOutputScript( + bitcoinRecoveryAddress, + bitcoinNetwork + ) + if ( + !BitcoinScriptUtils.isP2PKHScript(recoveryOutputScript) && + !BitcoinScriptUtils.isP2WPKHScript(recoveryOutputScript) + ) { + throw new Error("Bitcoin recovery address must be P2PKH or P2WPKH") + } + const refundPublicKeyHash = BitcoinAddressConverter.addressToPublicKeyHash( bitcoinRecoveryAddress, bitcoinNetwork