diff --git a/README.md b/README.md index 09e3be59..db56a016 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,10 @@ $ go get github.com/conformal/btcscript prints the created script hex and uses the DisasmString function to display the disassembled script. +* [Extracting Details from Standard Scripts] + (http://godoc.org/github.com/conformal/btcscript#example-ExtractPkScriptAddrs) + Demonstrates extracting information from a standard public key script. + ## TODO - Increase test coverage to 100% diff --git a/example_test.go b/example_test.go index 3392cfac..933705c7 100644 --- a/example_test.go +++ b/example_test.go @@ -5,6 +5,7 @@ package btcscript_test import ( + "encoding/hex" "fmt" "github.com/conformal/btcnet" @@ -46,3 +47,31 @@ func ExamplePayToAddrScript() { // Script Hex: 76a914128004ff2fcaf13b2b91eb654b1dc2b674f7ec6188ac // Script Disassembly: OP_DUP OP_HASH160 128004ff2fcaf13b2b91eb654b1dc2b674f7ec61 OP_EQUALVERIFY OP_CHECKSIG } + +// 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. + scriptClass, addresses, reqSigs, err := btcscript.ExtractPkScriptAddrs( + 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 +}