The current code for handling signing standard scripts resides in txscript and depends on parsing and creating standard scripts. This poses a problem for future work which intends to split the standard script handling from the consensus critical code since that code will also need to depend on txscript and therefore would result in a circular dependency. In order to pave the way for splitting the standard script handling without running into the aforementioned issue, this moves all of the signing code in the txscript package to a new subpackage named sign. As an aside, the signing code really never fit very well in the txscript package anyway and it only exists there because it was not possible to parse scripts outside of the package back when the original code was implemented. However, that limitation no longer exists. It should also be noted that this only does the minimum work necessary to move the code and does not update it otherwise since future work plans to replace it with a much more robust architecture that properly handles things such as different script versions and non-standard scripts which the current code does not handle.
84 lines
2.4 KiB
Go
84 lines
2.4 KiB
Go
// Copyright (c) 2014-2016 The btcsuite developers
|
|
// Copyright (c) 2015-2021 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package txscript_test
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
|
|
"github.com/decred/dcrd/chaincfg/v3"
|
|
"github.com/decred/dcrd/txscript/v4"
|
|
"github.com/decred/dcrd/txscript/v4/stdaddr"
|
|
)
|
|
|
|
const (
|
|
// noTreasury signifies the treasury agenda should be treated as though
|
|
// it is inactive. It is used to increase the readability of the
|
|
// tests.
|
|
noTreasury = false
|
|
)
|
|
|
|
// This example demonstrates extracting information from a standard public key
|
|
// script.
|
|
func ExampleExtractPkScriptAddrs() {
|
|
// Start with a standard pay-to-pubkey-hash script.
|
|
const scriptVersion = 0
|
|
scriptHex := "76a914128004ff2fcaf13b2b91eb654b1dc2b674f7ec6188ac"
|
|
script, err := hex.DecodeString(scriptHex)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
// Extract and print details from the script.
|
|
mainNetParams := chaincfg.MainNetParams()
|
|
scriptClass, addresses, reqSigs, err := txscript.ExtractPkScriptAddrs(
|
|
scriptVersion, script, mainNetParams, noTreasury)
|
|
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: [DsSej1qR3Fyc8kV176DCh9n9cY9nqf9Quxk]
|
|
// Required Signatures: 1
|
|
}
|
|
|
|
// This example demonstrates creating a script tokenizer instance and using it
|
|
// to count the number of opcodes a script contains.
|
|
func ExampleScriptTokenizer() {
|
|
// Create a script to use in the example. Ordinarily this would come from
|
|
// some other source.
|
|
hash160 := stdaddr.Hash160([]byte("example"))
|
|
script, err := txscript.NewScriptBuilder().AddOp(txscript.OP_DUP).
|
|
AddOp(txscript.OP_HASH160).AddData(hash160).
|
|
AddOp(txscript.OP_EQUALVERIFY).AddOp(txscript.OP_CHECKSIG).Script()
|
|
if err != nil {
|
|
fmt.Printf("failed to build script: %v\n", err)
|
|
return
|
|
}
|
|
|
|
// Create a tokenizer to iterate the script and count the number of opcodes.
|
|
const scriptVersion = 0
|
|
var numOpcodes int
|
|
tokenizer := txscript.MakeScriptTokenizer(scriptVersion, script)
|
|
for tokenizer.Next() {
|
|
numOpcodes++
|
|
}
|
|
if tokenizer.Err() != nil {
|
|
fmt.Printf("script failed to parse: %v\n", err)
|
|
} else {
|
|
fmt.Printf("script contains %d opcode(s)\n", numOpcodes)
|
|
}
|
|
|
|
// Output:
|
|
// script contains 5 opcode(s)
|
|
}
|