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.
Bitcoin Stack Exchange is a question and answer site for Bitcoin users, developers, and enthusiasts. It only takes a minute to sign up.
Sign up to join this communityHere 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);
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.
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),...])
Jun 20, 2018 at 4:09