Add acctmgr.Address to look up any known address.

Use where possible to shorten things.
This commit is contained in:
Owain G. Ainsworth 2014-04-17 23:00:52 +01:00
parent e39fa32487
commit 49adb8312f
2 changed files with 17 additions and 28 deletions

View file

@ -484,6 +484,17 @@ func (am *AccountManager) MarkAddressForAccount(address btcutil.Address,
} }
} }
// Address looks up an address if it is known to wallet at all.
func (am *AccountManager) Address(addr btcutil.Address) (wallet.WalletAddress,
error) {
a, err := am.AccountByAddress(addr)
if err != nil {
return nil, err
}
return a.Address(addr)
}
// AllAccounts returns a slice of all managed accounts. // AllAccounts returns a slice of all managed accounts.
func (am *AccountManager) AllAccounts() []*Account { func (am *AccountManager) AllAccounts() []*Account {
respChan := make(chan []*Account) respChan := make(chan []*Account)

View file

@ -286,7 +286,7 @@ func makeMultiSigScript(keys []string, nRequired int) ([]byte, *btcjson.Error) {
case *btcutil.AddressPubKey: case *btcutil.AddressPubKey:
keysesPrecious[i] = addr keysesPrecious[i] = addr
case *btcutil.AddressPubKeyHash: case *btcutil.AddressPubKeyHash:
act, err := AcctMgr.AccountByAddress(addr) ainfo, err := AcctMgr.Address(addr)
if err != nil { if err != nil {
return nil, &btcjson.Error{ return nil, &btcjson.Error{
Code: btcjson.ErrParse.Code, Code: btcjson.ErrParse.Code,
@ -294,13 +294,6 @@ func makeMultiSigScript(keys []string, nRequired int) ([]byte, *btcjson.Error) {
} }
} }
ainfo, err := act.Address(addr)
if err != nil {
return nil, &btcjson.Error{
Code: btcjson.ErrParse.Code,
Message: err.Error(),
}
}
apkinfo := ainfo.(wallet.PubKeyAddress) apkinfo := ainfo.(wallet.PubKeyAddress)
// This will be an addresspubkey // This will be an addresspubkey
@ -1641,19 +1634,11 @@ func SignMessage(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
} }
} }
a, err := AcctMgr.AccountByAddress(addr) ainfo, err := AcctMgr.Address(addr)
if err != nil { if err != nil {
return nil, &btcjson.ErrInvalidAddressOrKey return nil, &btcjson.ErrInvalidAddressOrKey
} }
ainfo, err := a.Address(addr)
if err != nil {
return nil, &btcjson.Error{
Code: btcjson.ErrWallet.Code,
Message: err.Error(),
}
}
pka := ainfo.(wallet.PubKeyAddress) pka := ainfo.(wallet.PubKeyAddress)
privkey, err := pka.PrivKey() privkey, err := pka.PrivKey()
if err != nil { if err != nil {
@ -1761,8 +1746,9 @@ func ValidateAddress(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
result.Address = addr.EncodeAddress() result.Address = addr.EncodeAddress()
result.IsValid = true result.IsValid = true
account, err := AcctMgr.AccountByAddress(addr) // We can't use AcctMgr.Address() here since we also need the account
if err == nil { // name.
if account, err := AcctMgr.AccountByAddress(addr); err == nil {
// we ignore these errors because if this call passes this can't // we ignore these errors because if this call passes this can't
// realistically fail. // realistically fail.
ainfo, _ := account.Address(addr) ainfo, _ := account.Address(addr)
@ -1813,19 +1799,11 @@ func VerifyMessage(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
} }
// First check we know about the address and get the keys. // First check we know about the address and get the keys.
a, err := AcctMgr.AccountByAddress(addr) ainfo, err := AcctMgr.Address(addr)
if err != nil { if err != nil {
return nil, &btcjson.ErrInvalidAddressOrKey return nil, &btcjson.ErrInvalidAddressOrKey
} }
ainfo, err := a.Address(addr)
if err != nil {
return nil, &btcjson.Error{
Code: btcjson.ErrWallet.Code,
Message: err.Error(),
}
}
pka := ainfo.(wallet.PubKeyAddress) pka := ainfo.(wallet.PubKeyAddress)
privkey, err := pka.PrivKey() privkey, err := pka.PrivKey()
if err != nil { if err != nil {