Skip to content

Commit

Permalink
BitcoinJsLibSwapProvider: Fix refund
Browse files Browse the repository at this point in the history
  • Loading branch information
monokh committed Mar 1, 2019
1 parent 86d2586 commit e53285a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions src/providers/bitcoin/BitcoinJsLibSwapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,18 @@ export default class BitcoinJsLibSwapProvider extends Provider {
}

async claimSwap (initiationTxHash, recipientAddress, refundAddress, secret, expiration) {
const wif = await this.getMethod('dumpPrivKey')(recipientAddress)
return this._redeemSwap(initiationTxHash, recipientAddress, refundAddress, expiration, true, secret)
}

async refundSwap (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration) {
return this._redeemSwap(initiationTxHash, recipientAddress, refundAddress, expiration, false, undefined, secretHash)
}

async _redeemSwap (initiationTxHash, recipientAddress, refundAddress, expiration, isRedeem, secret, secretHash) {
const address = isRedeem ? recipientAddress : refundAddress
const wif = await this.getMethod('dumpPrivKey')(address)
const wallet = bitcoin.ECPair.fromWIF(wif, this._bitcoinJsNetwork)
const secretHash = sha256(secret)
const script = this.createSwapScript(recipientAddress, refundAddress, secretHash, expiration)
const script = this.createSwapScript(recipientAddress, refundAddress, secretHash || sha256(secret), expiration)
const scriptPubKey = padHexStart(script)
const p2shAddress = pubKeyToAddress(scriptPubKey, this._network.name, 'scriptHash')
const sendScript = this.getMethod('createScript')(p2shAddress)
Expand All @@ -71,11 +79,10 @@ export default class BitcoinJsLibSwapProvider extends Provider {
let vout = initiationTx._raw.data.vout[voutIndex]
const txfee = await this.getMethod('calculateFee')(1, 1, 3)

secret = Buffer.from(secret, 'hex')
vout.txid = initiationTxHash
vout.vSat = vout.value * 1e8
vout.script = Buffer.from(script, 'hex')
const walletRedeem = this.spendSwap(recipientAddress, wallet, secret, true, txfee, vout, this._bitcoinJsNetwork, expiration)
const walletRedeem = this.spendSwap(address, wallet, secret, isRedeem, txfee, vout, this._bitcoinJsNetwork, expiration)
return this.getMethod('sendRawTransaction')(walletRedeem)
}

Expand All @@ -97,7 +104,7 @@ export default class BitcoinJsLibSwapProvider extends Provider {
wallet.sign(sigHash).toScriptSignature(hashType),
wallet.getPublicKeyBuffer(),
isRedeem,
secret
isRedeem ? Buffer.from(secret, 'hex') : undefined
)

const redeem = bitcoin.script.scriptHash.input.encode(
Expand All @@ -107,10 +114,6 @@ export default class BitcoinJsLibSwapProvider extends Provider {
return txRaw.toHex()
}

async refundSwap (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration) {
throw new Error('BitcoinJsLibSwapProvider: Refunding not implemented')
}

doesTransactionMatchSwapParams (transaction, value, recipientAddress, refundAddress, secretHash, expiration) {
const data = this.createSwapScript(recipientAddress, refundAddress, secretHash, expiration)
const scriptPubKey = padHexStart(data)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/swap/singleChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function testBitcoinBalance (chain) {
describe('Swap Single Chain Flow', function () {
this.timeout(config.timeout)

describe.only('Bitcoin - Ledger', () => {
describe('Bitcoin - Ledger', () => {
mineBitcoinBlocks()
testSingle(chains.bitcoinWithLedger)
})
Expand Down

0 comments on commit e53285a

Please sign in to comment.