/
address.go
59 lines (51 loc) · 1.7 KB
/
address.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
// Copyright (c) 2015-2016 The btcsuite developers
// Copyright (c) 2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package cfgutil
import (
"decred.org/dcrwallet/v2/errors"
"github.com/decred/dcrd/txscript/v4/stdaddr"
)
// AddressFlag contains a stdaddr.Address and implements the flags.Marshaler and
// Unmarshaler interfaces so it can be used as a config struct field.
type AddressFlag struct {
str string
}
// NewAddressFlag creates an AddressFlag with a default stdaddr.Address.
func NewAddressFlag() *AddressFlag {
return new(AddressFlag)
}
// MarshalFlag satisfies the flags.Marshaler interface.
func (a *AddressFlag) MarshalFlag() (string, error) {
return a.str, nil
}
// UnmarshalFlag satisfies the flags.Unmarshaler interface.
func (a *AddressFlag) UnmarshalFlag(addr string) error {
a.str = addr
return nil
}
// Address decodes the address flag for the network described by params.
// If the flag is the empty string, this returns a nil address.
func (a *AddressFlag) Address(params stdaddr.AddressParams) (stdaddr.Address, error) {
if a.str == "" {
return nil, nil
}
return stdaddr.DecodeAddress(a.str, params)
}
// StakeAddress decodes the address flag for the network described by
// params as a stake address.
// If the flag is the empty string, this returns a nil address.
func (a *AddressFlag) StakeAddress(params stdaddr.AddressParams) (stdaddr.StakeAddress, error) {
addr, err := a.Address(params)
if err != nil {
return nil, err
}
if addr == nil {
return nil, nil
}
if saddr, ok := addr.(stdaddr.StakeAddress); ok {
return saddr, nil
}
return nil, errors.Errorf("address is not suitable for stake usage")
}