-
Notifications
You must be signed in to change notification settings - Fork 25
/
helper.go
79 lines (67 loc) · 2.58 KB
/
helper.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package addresshelper
import (
"fmt"
"github.com/btcsuite/btcd/btcutil"
btccfg "github.com/btcsuite/btcd/chaincfg"
btctxscript "github.com/btcsuite/btcd/txscript"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/dcrd/dcrutil/v4"
"github.com/decred/dcrd/txscript/v4/stdaddr"
"github.com/decred/dcrd/txscript/v4/stdscript"
ltccfg "github.com/ltcsuite/ltcd/chaincfg"
"github.com/ltcsuite/ltcd/ltcutil"
ltctxscript "github.com/ltcsuite/ltcd/txscript"
)
const scriptVersion = 0
// PkScript returns the public key payment script for the given address.
func PkScript(address string, net dcrutil.AddressParams) ([]byte, error) {
addr, err := stdaddr.DecodeAddress(address, net)
if err != nil {
return nil, fmt.Errorf("error decoding address '%s': %s", address, err.Error())
}
_, pkScript := addr.PaymentScript()
return pkScript, nil
}
// BTCPkScript returns the public key payment script for the given address.
func BTCPkScript(address string, net *btccfg.Params) ([]byte, error) {
// 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.
addr, err := btcutil.DecodeAddress(address, net)
if err != nil {
return nil, fmt.Errorf("error decoding address '%s': %s", address, err.Error())
}
// Create a public key script that pays to the address.
pkScript, err := btctxscript.PayToAddrScript(addr)
if err != nil {
return nil, err
}
return pkScript, nil
}
// LTCPkScript returns the public key payment script for the given address.
func LTCPkScript(address string, net *ltccfg.Params) ([]byte, error) {
// Parse the address to send the coins to into a ltcutil.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.
addr, err := ltcutil.DecodeAddress(address, net)
if err != nil {
return nil, fmt.Errorf("error decoding address '%s': %s", address, err.Error())
}
// Create a public key script that pays to the address.
pkScript, err := ltctxscript.PayToAddrScript(addr)
if err != nil {
return nil, err
}
return pkScript, nil
}
// PkScriptAddresses returns the addresses for the given public key script.
func PkScriptAddresses(params *chaincfg.Params, pkScript []byte) []string {
_, addresses := stdscript.ExtractAddrs(scriptVersion, pkScript, params)
encodedAddresses := make([]string, len(addresses))
for i, address := range addresses {
encodedAddresses[i] = address.String()
}
return encodedAddresses
}