Bitcoinjs-lib: BIP199 HTLC contract support (for AtomicSwap)

Created on 1 Dec 2017  路  4Comments  路  Source: bitcoinjs/bitcoinjs-lib

Is there an example of how to make HTLC contract?
https://github.com/bitcoin/bips/blob/master/bip-0199.mediawiki

how to / question / docs

All 4 comments

Not at this time, but I intend to add some examples soon, probably in https://github.com/bitcoinjs/bitcoinjs-lib/pull/944

PRs accepted!

It works for me! Next steps: build and try to execute redeem tx, write bitcoinjs tests

var keyPair = bitcoin.ECPair.fromWIF('cRgnQe9MUu1JznntrLaoQpB476M8PURvXVQB5R2eqms5tXnzNsrr',bitcoin.networks.testnet)
var pubKeyWi = keyPair.getPublicKeyBuffer()
document.querySelector("#myAddr").innerHTML = keyPair.getAddress();

var hodlDate = Math.floor(Date.now() / 1000) + (3600 * 48)
var hodlLockTimeBuffer = new Buffer(4)
hodlLockTimeBuffer.writeInt32LE(hodlDate | 0, 0);

var witnessScript = bitcoin.script.compile([].concat(
  OPS.OP_IF,
  OPS.OP_RIPEMD160,
  Buffer.from("db018259bba932f6ede72154601e581c5f581c76", 'hex'),
  OPS.OP_EQUALVERIFY,
  OPS.OP_DUP,
  OPS.OP_HASH160,
  pubKeyWi,
  OPS.OP_ELSE,
  hodlLockTimeBuffer,
  OPS.OP_CHECKLOCKTIMEVERIFY,
  OPS.OP_DROP,
  OPS.OP_DUP,
  OPS.OP_HASH160,
  pubKeyWi,
  OPS.OP_ENDIF,
  OPS.OP_EQUALVERIFY,
  OPS.OP_CHECKSIG,
))

var redeemScript = bitcoin.script.compile([OPS.OP_0, bitcoin.crypto.sha256(witnessScript)]);  //not sure about this
var scriptPubKey = bitcoin.script.compile([OPS.OP_HASH160, bitcoin.crypto.hash160(redeemScript), OPS.OP_EQUAL]) 
var address = bitcoin.address.fromOutputScript(scriptPubKey,bitcoin.networks.testnet)

@noxonsu I think you're lock time Buffer is wrong.
Please see https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.js#L18

Closing in favour of #944

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rbndg picture rbndg  路  3Comments

dakk picture dakk  路  3Comments

ghost picture ghost  路  3Comments

zhaozhiming picture zhaozhiming  路  3Comments

ishwarchandratiwari picture ishwarchandratiwari  路  3Comments