Skip to content

Commit e53285a

Browse files
committed
BitcoinJsLibSwapProvider: Fix refund
1 parent 86d2586 commit e53285a

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/providers/bitcoin/BitcoinJsLibSwapProvider.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

test/integration/swap/singleChain.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function testBitcoinBalance (chain) {
170170
describe('Swap Single Chain Flow', function () {
171171
this.timeout(config.timeout)
172172

173-
describe.only('Bitcoin - Ledger', () => {
173+
describe('Bitcoin - Ledger', () => {
174174
mineBitcoinBlocks()
175175
testSingle(chains.bitcoinWithLedger)
176176
})

0 commit comments

Comments
 (0)