2016-08-08 21:04:33 +02:00
|
|
|
// Copyright (c) 2014-2016 The btcsuite developers
|
2014-07-08 21:40:32 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-30 07:03:01 +01:00
|
|
|
package txscript_test
|
2014-07-08 21:40:32 +02:00
|
|
|
|
|
|
|
import (
|
2014-07-08 21:59:57 +02:00
|
|
|
"encoding/hex"
|
2014-07-08 21:40:32 +02:00
|
|
|
"fmt"
|
|
|
|
|
2021-10-15 07:45:32 +02:00
|
|
|
"github.com/lbryio/lbcd/btcec"
|
|
|
|
"github.com/lbryio/lbcd/chaincfg"
|
|
|
|
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
|
|
|
"github.com/lbryio/lbcd/txscript"
|
|
|
|
"github.com/lbryio/lbcd/wire"
|
|
|
|
btcutil "github.com/lbryio/lbcutil"
|
2014-07-08 21:40:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// This example demonstrates creating a script which pays to a bitcoin address.
|
|
|
|
// It also prints the created script hex and uses the DisasmString function to
|
|
|
|
// display the disassembled script.
|
|
|
|
func ExamplePayToAddrScript() {
|
|
|
|
// Parse the address to send the coins to into a btcutil.Address
|
|
|
|
// which is useful to ensure the accuracy of the address and determine
|
|
|
|
// the address type. It is also required for the upcoming call to
|
|
|
|
// PayToAddrScript.
|
2021-08-19 20:41:48 +02:00
|
|
|
addressStr := "bHW58d37s1hBjj3wPBkn5zpCX3F8ZW3uWf"
|
2015-02-06 06:18:27 +01:00
|
|
|
address, err := btcutil.DecodeAddress(addressStr, &chaincfg.MainNetParams)
|
2014-07-08 21:40:32 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a public key script that pays to the address.
|
2015-01-30 07:03:01 +01:00
|
|
|
script, err := txscript.PayToAddrScript(address)
|
2014-07-08 21:40:32 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("Script Hex: %x\n", script)
|
|
|
|
|
2015-01-30 07:03:01 +01:00
|
|
|
disasm, err := txscript.DisasmString(script)
|
2014-07-08 21:40:32 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("Script Disassembly:", disasm)
|
|
|
|
|
|
|
|
// Output:
|
2021-08-19 20:41:48 +02:00
|
|
|
// Script Hex: 76a914345991dbf57bfb014b87006acdfafbfc5fe8292f88ac
|
|
|
|
// Script Disassembly: OP_DUP OP_HASH160 345991dbf57bfb014b87006acdfafbfc5fe8292f OP_EQUALVERIFY OP_CHECKSIG
|
2014-07-08 21:40:32 +02:00
|
|
|
}
|
2014-07-08 21:59:57 +02:00
|
|
|
|
|
|
|
// This example demonstrates extracting information from a standard public key
|
|
|
|
// script.
|
|
|
|
func ExampleExtractPkScriptAddrs() {
|
|
|
|
// Start with a standard pay-to-pubkey-hash script.
|
|
|
|
scriptHex := "76a914128004ff2fcaf13b2b91eb654b1dc2b674f7ec6188ac"
|
|
|
|
script, err := hex.DecodeString(scriptHex)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract and print details from the script.
|
2015-01-30 07:03:01 +01:00
|
|
|
scriptClass, addresses, reqSigs, err := txscript.ExtractPkScriptAddrs(
|
2015-02-06 06:18:27 +01:00
|
|
|
script, &chaincfg.MainNetParams)
|
2014-07-08 21:59:57 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("Script Class:", scriptClass)
|
|
|
|
fmt.Println("Addresses:", addresses)
|
|
|
|
fmt.Println("Required Signatures:", reqSigs)
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// Script Class: pubkeyhash
|
2021-08-19 20:41:48 +02:00
|
|
|
// Addresses: [bER6Ddq6YfRKDJDmmdeaqrP8XHmDJcYSJQ]
|
2014-07-08 21:59:57 +02:00
|
|
|
// Required Signatures: 1
|
|
|
|
}
|
2015-02-09 17:11:55 +01:00
|
|
|
|
|
|
|
// This example demonstrates manually creating and signing a redeem transaction.
|
|
|
|
func ExampleSignTxOutput() {
|
|
|
|
// Ordinarily the private key would come from whatever storage mechanism
|
|
|
|
// is being used, but for this example just hard code it.
|
|
|
|
privKeyBytes, err := hex.DecodeString("22a47fa09a223f2aa079edf85a7c2" +
|
|
|
|
"d4f8720ee63e502ee2869afab7de234b80c")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
privKey, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), privKeyBytes)
|
|
|
|
pubKeyHash := btcutil.Hash160(pubKey.SerializeCompressed())
|
|
|
|
addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash,
|
|
|
|
&chaincfg.MainNetParams)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// For this example, create a fake transaction that represents what
|
|
|
|
// would ordinarily be the real transaction that is being spent. It
|
|
|
|
// contains a single output that pays to address in the amount of 1 BTC.
|
2016-10-27 04:09:19 +02:00
|
|
|
originTx := wire.NewMsgTx(wire.TxVersion)
|
2016-08-08 21:04:33 +02:00
|
|
|
prevOut := wire.NewOutPoint(&chainhash.Hash{}, ^uint32(0))
|
2016-10-19 02:32:50 +02:00
|
|
|
txIn := wire.NewTxIn(prevOut, []byte{txscript.OP_0, txscript.OP_0}, nil)
|
2015-02-09 17:11:55 +01:00
|
|
|
originTx.AddTxIn(txIn)
|
|
|
|
pkScript, err := txscript.PayToAddrScript(addr)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
txOut := wire.NewTxOut(100000000, pkScript)
|
|
|
|
originTx.AddTxOut(txOut)
|
2016-08-08 21:04:33 +02:00
|
|
|
originTxHash := originTx.TxHash()
|
2015-02-09 17:11:55 +01:00
|
|
|
|
|
|
|
// Create the transaction to redeem the fake transaction.
|
2016-10-27 04:09:19 +02:00
|
|
|
redeemTx := wire.NewMsgTx(wire.TxVersion)
|
2015-02-09 17:11:55 +01:00
|
|
|
|
|
|
|
// Add the input(s) the redeeming transaction will spend. There is no
|
|
|
|
// signature script at this point since it hasn't been created or signed
|
|
|
|
// yet, hence nil is provided for it.
|
|
|
|
prevOut = wire.NewOutPoint(&originTxHash, 0)
|
2016-10-19 02:32:50 +02:00
|
|
|
txIn = wire.NewTxIn(prevOut, nil, nil)
|
2015-02-09 17:11:55 +01:00
|
|
|
redeemTx.AddTxIn(txIn)
|
|
|
|
|
|
|
|
// Ordinarily this would contain that actual destination of the funds,
|
|
|
|
// but for this example don't bother.
|
|
|
|
txOut = wire.NewTxOut(0, nil)
|
|
|
|
redeemTx.AddTxOut(txOut)
|
|
|
|
|
|
|
|
// Sign the redeeming transaction.
|
|
|
|
lookupKey := func(a btcutil.Address) (*btcec.PrivateKey, bool, error) {
|
|
|
|
// Ordinarily this function would involve looking up the private
|
|
|
|
// key for the provided address, but since the only thing being
|
|
|
|
// signed in this example uses the address associated with the
|
|
|
|
// private key from above, simply return it with the compressed
|
|
|
|
// flag set since the address is using the associated compressed
|
|
|
|
// public key.
|
|
|
|
//
|
|
|
|
// NOTE: If you want to prove the code is actually signing the
|
|
|
|
// transaction properly, uncomment the following line which
|
|
|
|
// intentionally returns an invalid key to sign with, which in
|
|
|
|
// turn will result in a failure during the script execution
|
|
|
|
// when verifying the signature.
|
|
|
|
//
|
|
|
|
// privKey.D.SetInt64(12345)
|
|
|
|
//
|
|
|
|
return privKey, true, nil
|
|
|
|
}
|
|
|
|
// Notice that the script database parameter is nil here since it isn't
|
|
|
|
// used. It must be specified when pay-to-script-hash transactions are
|
|
|
|
// being signed.
|
|
|
|
sigScript, err := txscript.SignTxOutput(&chaincfg.MainNetParams,
|
|
|
|
redeemTx, 0, originTx.TxOut[0].PkScript, txscript.SigHashAll,
|
|
|
|
txscript.KeyClosure(lookupKey), nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
redeemTx.TxIn[0].SignatureScript = sigScript
|
|
|
|
|
|
|
|
// Prove that the transaction has been validly signed by executing the
|
|
|
|
// script pair.
|
2015-02-14 15:37:47 +01:00
|
|
|
flags := txscript.ScriptBip16 | txscript.ScriptVerifyDERSignatures |
|
2015-02-09 17:11:55 +01:00
|
|
|
txscript.ScriptStrictMultiSig |
|
|
|
|
txscript.ScriptDiscourageUpgradableNops
|
2015-04-20 22:28:00 +02:00
|
|
|
vm, err := txscript.NewEngine(originTx.TxOut[0].PkScript, redeemTx, 0,
|
2016-10-19 02:32:50 +02:00
|
|
|
flags, nil, nil, -1)
|
2015-02-09 17:11:55 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2015-04-20 22:28:00 +02:00
|
|
|
if err := vm.Execute(); err != nil {
|
2015-02-09 17:11:55 +01:00
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("Transaction successfully signed")
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// Transaction successfully signed
|
|
|
|
}
|