2

I was looking to create custom P2SH addresses from redeemScripts ( they start with 3...) using bictore-lib.

If anyone can direct me to the right code in their module, I can't find it.

1 Answer 1

2

Here is the reference for creating a P2SH multisig address https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.js#L44

However your question states "custom" P2SH so I think you might be asking about something more advanced, in which case I would suggest reviewing bitcoin.script.compile, the CLTV address for example:

let bitcoin = require("bitcoinjs-lib");
let bip65 = require('bip65');

function cltvCheckSigOutput (aQ, bQ, lockTime) {
    return bitcoin.script.compile([
      bitcoin.opcodes.OP_IF,
      bitcoin.script.number.encode(lockTime),
      bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
      bitcoin.opcodes.OP_DROP,

      bitcoin.opcodes.OP_ELSE,
      bQ.publicKey,
      bitcoin.opcodes.OP_CHECKSIGVERIFY,
      bitcoin.opcodes.OP_ENDIF,

      aQ.publicKey,
      bitcoin.opcodes.OP_CHECKSIG
    ])
  }     

  function utcNow () {
    return Math.floor(Date.now() / 1000)
  }

let NETWORK = bitcoin.networks.testnet;  
let alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', NETWORK); 
let bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', NETWORK)

//set lock time for 3 hours from now
let lockTime = bip65.encode({ utc: utcNow() + (3600 * 3) });

let redeemScript = cltvCheckSigOutput(alice, bob, lockTime);
let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript));
let address = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK);

console.log(address);
4
  • Thanks. I am actually looking for a JS script in bitcoinjs-lib or bitcore-lib to create NON MULTISIG, so a normal P2SH address from a redeemScript. Or even from a customString for that matter.
    – Robert
    Jun 19, 2018 at 15:36
  • To be clear, in this article it mentions about a "special bitcoin address which are essentially Base58Check encodings of the 20 byte hash of the redeem script.". How are these "special addresses" created? But not the multisig ones, just an address which is the hash of a redeemScript. medium.com/@ismailakkila/…. @m1xolyd1an
    – Robert
    Jun 19, 2018 at 16:02
  • Maybe this is what what I am looking for. github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/…
    – Robert
    Jun 19, 2018 at 16:15
  • Essentially it is just bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript)); where redeemScript is equal to an OP code script array bitcoin.script.compile([bitcoin.opcodes.OP_IF, bitcoin.script.number.encode(lockTime),...])
    – m1xolyd1an
    Jun 20, 2018 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.