Skip to content

Instantly share code, notes, and snippets.

@davecgh
Last active September 20, 2018 19:49
Show Gist options
  • Save davecgh/312ba783d627007cd5e4 to your computer and use it in GitHub Desktop.
Save davecgh/312ba783d627007cd5e4 to your computer and use it in GitHub Desktop.
P2SH Pubkey
package main
import (
"encoding/hex"
"fmt"
"github.com/btcsuite/btcutil"
. "github.com/btcsuite/btcd/txscript"
)
func main() {
// Using pubkey and signature from redeeming tx 32c28bc00a42aad56388389299de4adce429c549c8d70200692063be5506d5d1:0
// which references tx e5779b9e78f9650debc2893fd9636d827b26b4ddfa6a8172fe8708c924f5c39d:0
pubKeyHex := "048b65a0e6bb200e6dac05e74281b1ab9a41e80006d6b12d8521e09981da97dd96ac72d24d1a7ded9493a9fc20fdb4a714808f0b680f1f1d93527748b5e3f629ff"
pubKey, _ := hex.DecodeString(pubKeyHex)
sigHex := "30440220717655c3f0dad0b527275d99cb6831cf3efab9a75db8e14ca10f1804d3ccaeab02203414de200eabc6b9fada87a1a222f91a7ae7361542c6c91e06791e182c3cf0ce01"
sig, _ := hex.DecodeString(sigHex)
// 1) Generate the serialized pay script
payScript, _ := NewScriptBuilder().AddData(pubKey).AddOp(OP_CHECKSIG).Script()
payScriptDisasm, _ := DisasmString(payScript)
fmt.Printf("payScript hex: %x\npayScript asm: %s\n", payScript, payScriptDisasm)
// 2) Calculate the hash160 to obtain the script hash
payScriptHash := btcutil.Hash160(payScript)
fmt.Printf("payScript hash: %x\n", payScriptHash)
// 3) Create the spending transaction's output script
outScript, _ := NewScriptBuilder().AddOp(OP_HASH160).AddData(payScriptHash).AddOp(OP_EQUAL).Script()
outScriptDisasm, _ := DisasmString(outScript)
fmt.Printf("outScript hex: %x\noutScript asm: %s\n", outScript, outScriptDisasm)
// 4) Create the redeeming transaction's input script
inScript, _ := NewScriptBuilder().AddData(sig).AddData(payScript).Script()
inScriptDisasm, _ := DisasmString(inScript)
fmt.Printf("inScript hex: %x\ninScript asm: %s\n", inScript, inScriptDisasm)
}
Output:
payScript hex: 41048b65a0e6bb200e6dac05e74281b1ab9a41e80006d6b12d8521e09981da97dd96ac72d24d1a7ded9493a9fc20fdb4a714808f0b680f1f1d93527748b5e3f629ffac
payScript asm: 048b65a0e6bb200e6dac05e74281b1ab9a41e80006d6b12d8521e09981da97dd96ac72d24d1a7ded9493a9fc20fdb4a714808f0b680f1f1d93527748b5e3f629ff OP_CHECKSIG
payScript hash: da6e6a632d96dc5530d7b3c9f3017725d023093e
outScript hex: a914da6e6a632d96dc5530d7b3c9f3017725d023093e87
outScript asm: OP_HASH160 da6e6a632d96dc5530d7b3c9f3017725d023093e OP_EQUAL
inScript hex: 4730440220717655c3f0dad0b527275d99cb6831cf3efab9a75db8e14ca10f1804d3ccaeab02203414de200eabc6b9fada87a1a222f91a7ae7361542c6c91e06791e182c3cf0ce014341048b65a0e6bb200e6dac05e74281b1ab9a41e80006d6b12d8521e09981da97dd96ac72d24d1a7ded9493a9fc20fdb4a714808f0b680f1f1d93527748b5e3f629ffac
inScript asm: 30440220717655c3f0dad0b527275d99cb6831cf3efab9a75db8e14ca10f1804d3ccaeab02203414de200eabc6b9fada87a1a222f91a7ae7361542c6c91e06791e182c3cf0ce01 41048b65a0e6bb200e6dac05e74281b1ab9a41e80006d6b12d8521e09981da97dd96ac72d24d1a7ded9493a9fc20fdb4a714808f0b680f1f1d93527748b5e3f629ffac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment