2015-01-30 07:03:01 +01:00
|
|
|
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
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"
|
|
|
|
|
2015-01-30 07:03:01 +01:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
2015-01-17 00:29:53 +01:00
|
|
|
"github.com/btcsuite/btcnet"
|
2015-01-15 17:42:26 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
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.
|
|
|
|
addressStr := "12gpXQVcCL2qhTNQgyLVdCFG2Qs2px98nV"
|
|
|
|
address, err := btcutil.DecodeAddress(addressStr, &btcnet.MainNetParams)
|
|
|
|
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:
|
|
|
|
// Script Hex: 76a914128004ff2fcaf13b2b91eb654b1dc2b674f7ec6188ac
|
|
|
|
// Script Disassembly: OP_DUP OP_HASH160 128004ff2fcaf13b2b91eb654b1dc2b674f7ec61 OP_EQUALVERIFY OP_CHECKSIG
|
|
|
|
}
|
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(
|
2014-07-08 21:59:57 +02:00
|
|
|
script, &btcnet.MainNetParams)
|
|
|
|
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
|
|
|
|
// Addresses: [12gpXQVcCL2qhTNQgyLVdCFG2Qs2px98nV]
|
|
|
|
// Required Signatures: 1
|
|
|
|
}
|