Add new funcs for creating payment scripts.
This commit adds two new functions named PayToScriptHashScript and PayToAddrScript. The first one creates and returns a public-key script which pays to the provided script hash and conforms to BIP0016. The second function takes the new btcutil.Address interface type and returns an appropriate script to pay to the address type in the interface. It currently works for btcutil.AddressPubKeyHash and btcutil.AddressScriptHash.
This commit is contained in:
parent
3a8ec0078b
commit
3d60bac238
1 changed files with 44 additions and 0 deletions
44
script.go
44
script.go
|
@ -12,6 +12,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/conformal/btcec"
|
"github.com/conformal/btcec"
|
||||||
|
"github.com/conformal/btcutil"
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
"io"
|
"io"
|
||||||
|
@ -112,6 +113,10 @@ var StackErrInvalidIndex = errors.New("Invalid script index")
|
||||||
// that peforms operations other that pushing data to the stack.
|
// that peforms operations other that pushing data to the stack.
|
||||||
var StackErrNonPushOnly = errors.New("SigScript is non pushonly")
|
var StackErrNonPushOnly = errors.New("SigScript is non pushonly")
|
||||||
|
|
||||||
|
// ErrUnsupportedAddress is returned when a concrete type that implements
|
||||||
|
// a btcutil.Address is not a supported type.
|
||||||
|
var ErrUnsupportedAddress = errors.New("unsupported address type")
|
||||||
|
|
||||||
// Bip16Activation is the timestamp where BIP0016 is valid to use in the
|
// Bip16Activation is the timestamp where BIP0016 is valid to use in the
|
||||||
// blockchain. To be used to determine if BIP0016 should be called for or not.
|
// blockchain. To be used to determine if BIP0016 should be called for or not.
|
||||||
// This timestamp corresponds to Sun Apr 1 00:00:00 UTC 2012.
|
// This timestamp corresponds to Sun Apr 1 00:00:00 UTC 2012.
|
||||||
|
@ -969,6 +974,45 @@ func PayToPubKeyHashScript(pubKeyHash []byte) (pkScript []byte, err error) {
|
||||||
return unparseScript(pops)
|
return unparseScript(pops)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PayToScriptHashScript creates a new script to pay a transaction output to a
|
||||||
|
// script hash.
|
||||||
|
func PayToScriptHashScript(scriptHash []byte) (pkScript []byte, err error) {
|
||||||
|
pops := []parsedOpcode{
|
||||||
|
parsedOpcode{
|
||||||
|
opcode: opcodemap[OP_HASH160],
|
||||||
|
},
|
||||||
|
parsedOpcode{
|
||||||
|
opcode: opcodemap[OP_DATA_20],
|
||||||
|
data: scriptHash,
|
||||||
|
},
|
||||||
|
parsedOpcode{
|
||||||
|
opcode: opcodemap[OP_EQUAL],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return unparseScript(pops)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PayToAddrScript creates a new script to pay a transaction output to a the
|
||||||
|
// specified address. Currently the only supported address types are
|
||||||
|
// btcutil.AddressPubKeyHash and btcutil.AddressScriptHash.
|
||||||
|
func PayToAddrScript(addr btcutil.Address) ([]byte, error) {
|
||||||
|
switch addr := addr.(type) {
|
||||||
|
case *btcutil.AddressPubKeyHash:
|
||||||
|
if addr == nil {
|
||||||
|
return nil, ErrUnsupportedAddress
|
||||||
|
}
|
||||||
|
return PayToPubKeyHashScript(addr.ScriptAddress())
|
||||||
|
|
||||||
|
case *btcutil.AddressScriptHash:
|
||||||
|
if addr == nil {
|
||||||
|
return nil, ErrUnsupportedAddress
|
||||||
|
}
|
||||||
|
return PayToScriptHashScript(addr.ScriptAddress())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, ErrUnsupportedAddress
|
||||||
|
}
|
||||||
|
|
||||||
// SignatureScript creates an input signature script for tx to spend
|
// SignatureScript creates an input signature script for tx to spend
|
||||||
// BTC sent from a previous output to the owner of privkey. tx must
|
// BTC sent from a previous output to the owner of privkey. tx must
|
||||||
// include all transaction inputs and outputs, however txin scripts are
|
// include all transaction inputs and outputs, however txin scripts are
|
||||||
|
|
Loading…
Reference in a new issue