-
Notifications
You must be signed in to change notification settings - Fork 14
/
paytoaddr.go
50 lines (43 loc) · 1.49 KB
/
paytoaddr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package zecutil
import (
"errors"
"fmt"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcutil"
)
func PayToAddrScript(addr btcutil.Address) ([]byte, error) {
var script []byte
var err error
script, err = txscript.PayToAddrScript(addr)
if err == nil {
return script, nil
}
const nilAddrErrStr = "unable to generate payment script for nil address"
switch addr := addr.(type) {
case *ZecAddressPubKeyHash:
if addr == nil {
return nil, errors.New(nilAddrErrStr)
}
return payToPubKeyHashScript(addr.ScriptAddress())
case *ZecAddressScriptHash:
if addr == nil {
return nil, errors.New(nilAddrErrStr)
}
return payToScriptHashScript(addr.ScriptAddress())
}
return nil, fmt.Errorf("unable to generate payment script for unsupported address type %T", addr)
}
// payToPubKeyHashScript creates a new script to pay a transaction
// output to a 20-byte pubkey hash. It is expected that the input is a valid
// hash.
func payToPubKeyHashScript(pubKeyHash []byte) ([]byte, error) {
return txscript.NewScriptBuilder().AddOp(txscript.OP_DUP).AddOp(txscript.OP_HASH160).
AddData(pubKeyHash).AddOp(txscript.OP_EQUALVERIFY).AddOp(txscript.OP_CHECKSIG).
Script()
}
// payToScriptHashScript creates a new script to pay a transaction output to a
// script hash. It is expected that the input is a valid hash.
func payToScriptHashScript(scriptHash []byte) ([]byte, error) {
return txscript.NewScriptBuilder().AddOp(txscript.OP_HASH160).AddData(scriptHash).
AddOp(txscript.OP_EQUAL).Script()
}