decred.org/dcrwallet/v3@v3.1.0/wallet/udb/address.go (about) 1 // Copyright (c) 2014 The btcsuite developers 2 // Copyright (c) 2015-2018 The Decred developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package udb 7 8 import ( 9 "github.com/decred/dcrd/dcrutil/v4" 10 "github.com/decred/dcrd/txscript/v4/stdaddr" 11 ) 12 13 // ManagedAddress is an interface that provides acces to information regarding 14 // an address managed by an address manager. Concrete implementations of this 15 // type may provide further fields to provide information specific to that type 16 // of address. 17 type ManagedAddress interface { 18 // Account returns the account the address is associated with. 19 Account() uint32 20 21 // Address returns a stdaddr.Address for the backing address. 22 Address() stdaddr.Address 23 24 // AddrHash returns the key or script hash related to the address 25 AddrHash() []byte 26 27 // Imported returns true if the backing address was imported instead 28 // of being part of an address chain. 29 Imported() bool 30 31 // Internal returns true if the backing address was created for internal 32 // use such as a change output of a transaction. 33 Internal() bool 34 35 // Multisig returns true if the backing address was created for multisig 36 // use. 37 Multisig() bool 38 } 39 40 // ManagedPubKeyAddress extends ManagedAddress and additionally provides the 41 // public and private keys for pubkey-based addresses. 42 type ManagedPubKeyAddress interface { 43 ManagedAddress 44 45 // PubKey returns the public key associated with the address. 46 PubKey() []byte 47 48 // Index returns the child number used to derive this public key address 49 Index() uint32 50 } 51 52 // ManagedScriptAddress extends ManagedAddress and represents a pay-to-script-hash 53 // style of addresses. 54 type ManagedScriptAddress interface { 55 ManagedAddress 56 57 // RedeemScript returns the redeem script and script version. 58 RedeemScript() (uint16, []byte) 59 } 60 61 // managedAddress represents a public key address. It also may or may not have 62 // the private key associated with the public key. 63 type managedAddress struct { 64 manager *Manager 65 account uint32 66 address *stdaddr.AddressPubKeyHashEcdsaSecp256k1V0 67 imported bool 68 internal bool 69 multisig bool 70 pubKey []byte 71 index uint32 72 } 73 74 // Enforce managedAddress satisfies the ManagedPubKeyAddress interface. 75 var _ ManagedPubKeyAddress = (*managedAddress)(nil) 76 77 // Account returns the account number the address is associated with. 78 // 79 // This is part of the ManagedAddress interface implementation. 80 func (a *managedAddress) Account() uint32 { 81 return a.account 82 } 83 84 // Address returns the stdaddr.Address which represents the managed address. 85 // This will be a pay-to-pubkey-hash address. 86 // 87 // This is part of the ManagedAddress interface implementation. 88 func (a *managedAddress) Address() stdaddr.Address { 89 return a.address 90 } 91 92 // AddrHash returns the public key hash for the address. 93 // 94 // This is part of the ManagedAddress interface implementation. 95 func (a *managedAddress) AddrHash() []byte { 96 return a.address.Hash160()[:] 97 } 98 99 // Imported returns true if the address was imported instead of being part of an 100 // address chain. 101 // 102 // This is part of the ManagedAddress interface implementation. 103 func (a *managedAddress) Imported() bool { 104 return a.imported 105 } 106 107 // Internal returns true if the address was created for internal use such as a 108 // change output of a transaction. 109 // 110 // This is part of the ManagedAddress interface implementation. 111 func (a *managedAddress) Internal() bool { 112 return a.internal 113 } 114 115 // Multisig returns true if the address was created for multisig use. 116 // 117 // This is part of the ManagedAddress interface implementation. 118 func (a *managedAddress) Multisig() bool { 119 return a.multisig 120 } 121 122 // PubKey returns the public key associated with the address. 123 // 124 // This is part of the ManagedPubKeyAddress interface implementation. 125 func (a *managedAddress) PubKey() []byte { 126 return a.pubKey 127 } 128 129 // Index returns the child number used to derive this key. 130 // 131 // This is part of the ManagedPubKeyAddress interface implementation. 132 func (a *managedAddress) Index() uint32 { 133 return a.index 134 } 135 136 // newManagedAddressWithoutPrivKey returns a new managed address based on the 137 // passed account, public key, and whether or not the public key should be 138 // compressed. 139 func newManagedAddressWithoutPrivKey(m *Manager, account uint32, pubKey []byte) (*managedAddress, error) { 140 // Create a pay-to-pubkey-hash address from the public key. 141 pubKeyHash := dcrutil.Hash160(pubKey) 142 address, err := stdaddr.NewAddressPubKeyHashEcdsaSecp256k1V0(pubKeyHash, m.chainParams) 143 if err != nil { 144 return nil, err 145 } 146 147 return &managedAddress{ 148 manager: m, 149 address: address, 150 account: account, 151 imported: false, 152 internal: false, 153 multisig: false, 154 pubKey: pubKey, 155 }, nil 156 } 157 158 // scriptAddress represents a pay-to-script-hash address. 159 type scriptAddress struct { 160 manager *Manager 161 account uint32 162 address *stdaddr.AddressScriptHashV0 163 redeemScript []byte 164 } 165 166 // Enforce scriptAddress satisfies the ManagedScriptAddress interface. 167 var _ ManagedScriptAddress = (*scriptAddress)(nil) 168 169 // Account returns the account the address is associated with. This will always 170 // be the ImportedAddrAccount constant for script addresses. 171 // 172 // This is part of the ManagedAddress interface implementation. 173 func (a *scriptAddress) Account() uint32 { 174 return a.account 175 } 176 177 // Address returns the stdaddr.Address which represents the managed address. 178 // This will be a pay-to-script-hash address. 179 // 180 // This is part of the ManagedAddress interface implementation. 181 func (a *scriptAddress) Address() stdaddr.Address { 182 return a.address 183 } 184 185 // AddrHash returns the script hash for the address. 186 // 187 // This is part of the ManagedAddress interface implementation. 188 // 189 // This is part of the ManagedAddress interface implementation. 190 func (a *scriptAddress) AddrHash() []byte { 191 return a.address.Hash160()[:] 192 } 193 194 // Imported always returns true since script addresses are always imported 195 // addresses and not part of any chain. 196 // 197 // This is part of the ManagedAddress interface implementation. 198 func (a *scriptAddress) Imported() bool { 199 return true 200 } 201 202 // Internal always returns false since script addresses are always imported 203 // addresses and not part of any chain in order to be for internal use. 204 // 205 // This is part of the ManagedAddress interface implementation. 206 func (a *scriptAddress) Internal() bool { 207 return false 208 } 209 210 // Multisig always returns false since script addresses are always imported 211 // addresses and not part of any chain in order to be for multisig use. 212 // 213 // This is part of the ManagedAddress interface implementation. 214 func (a *scriptAddress) Multisig() bool { 215 return false 216 } 217 218 func (a *scriptAddress) RedeemScript() (version uint16, script []byte) { 219 return 0, a.redeemScript 220 } 221 222 // newScriptAddress initializes and returns a new pay-to-script-hash address. 223 func newScriptAddress(m *Manager, account uint32, scriptHash, redeemScript []byte) (*scriptAddress, error) { 224 address, err := stdaddr.NewAddressScriptHashV0FromHash(scriptHash, 225 m.chainParams) 226 if err != nil { 227 return nil, err 228 } 229 230 return &scriptAddress{ 231 manager: m, 232 account: account, 233 address: address, 234 redeemScript: redeemScript, 235 }, nil 236 }