@@ -57,10 +57,18 @@ export default class BitcoinJsLibSwapProvider extends Provider {
5757 }
5858
5959 async claimSwap ( initiationTxHash , recipientAddress , refundAddress , secret , expiration ) {
60- const wif = await this . getMethod ( 'dumpPrivKey' ) ( recipientAddress )
60+ return this . _redeemSwap ( initiationTxHash , recipientAddress , refundAddress , expiration , true , secret )
61+ }
62+
63+ async refundSwap ( initiationTxHash , recipientAddress , refundAddress , secretHash , expiration ) {
64+ return this . _redeemSwap ( initiationTxHash , recipientAddress , refundAddress , expiration , false , undefined , secretHash )
65+ }
66+
67+ async _redeemSwap ( initiationTxHash , recipientAddress , refundAddress , expiration , isRedeem , secret , secretHash ) {
68+ const address = isRedeem ? recipientAddress : refundAddress
69+ const wif = await this . getMethod ( 'dumpPrivKey' ) ( address )
6170 const wallet = bitcoin . ECPair . fromWIF ( wif , this . _bitcoinJsNetwork )
62- const secretHash = sha256 ( secret )
63- const script = this . createSwapScript ( recipientAddress , refundAddress , secretHash , expiration )
71+ const script = this . createSwapScript ( recipientAddress , refundAddress , secretHash || sha256 ( secret ) , expiration )
6472 const scriptPubKey = padHexStart ( script )
6573 const p2shAddress = pubKeyToAddress ( scriptPubKey , this . _network . name , 'scriptHash' )
6674 const sendScript = this . getMethod ( 'createScript' ) ( p2shAddress )
@@ -71,11 +79,10 @@ export default class BitcoinJsLibSwapProvider extends Provider {
7179 let vout = initiationTx . _raw . data . vout [ voutIndex ]
7280 const txfee = await this . getMethod ( 'calculateFee' ) ( 1 , 1 , 3 )
7381
74- secret = Buffer . from ( secret , 'hex' )
7582 vout . txid = initiationTxHash
7683 vout . vSat = vout . value * 1e8
7784 vout . script = Buffer . from ( script , 'hex' )
78- const walletRedeem = this . spendSwap ( recipientAddress , wallet , secret , true , txfee , vout , this . _bitcoinJsNetwork , expiration )
85+ const walletRedeem = this . spendSwap ( address , wallet , secret , isRedeem , txfee , vout , this . _bitcoinJsNetwork , expiration )
7986 return this . getMethod ( 'sendRawTransaction' ) ( walletRedeem )
8087 }
8188
@@ -97,7 +104,7 @@ export default class BitcoinJsLibSwapProvider extends Provider {
97104 wallet . sign ( sigHash ) . toScriptSignature ( hashType ) ,
98105 wallet . getPublicKeyBuffer ( ) ,
99106 isRedeem ,
100- secret
107+ isRedeem ? Buffer . from ( secret , 'hex' ) : undefined
101108 )
102109
103110 const redeem = bitcoin . script . scriptHash . input . encode (
@@ -107,10 +114,6 @@ export default class BitcoinJsLibSwapProvider extends Provider {
107114 return txRaw . toHex ( )
108115 }
109116
110- async refundSwap ( initiationTxHash , recipientAddress , refundAddress , secretHash , expiration ) {
111- throw new Error ( 'BitcoinJsLibSwapProvider: Refunding not implemented' )
112- }
113-
114117 doesTransactionMatchSwapParams ( transaction , value , recipientAddress , refundAddress , secretHash , expiration ) {
115118 const data = this . createSwapScript ( recipientAddress , refundAddress , secretHash , expiration )
116119 const scriptPubKey = padHexStart ( data )
0 commit comments