Bitcore

如何使用 bitcore-lib 創建 P2SH 地址

  • June 19, 2018

我正在尋找從redeemScripts(它們以 3 開頭…)使用bictore-lib.

如果有人可以將我引導到他們模組中的正確程式碼,我找不到它。

這是創建 P2SH 多重簽名地址的參考<https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.js#L44>

但是,您的問題是“自定義”P2SH,所以我認為您可能會問一些更高級的問題,在這種情況下,我建議您查看bitcoin.script.compileCLTV 地址,例如:

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);

引用自:https://bitcoin.stackexchange.com/questions/76376