Set account field in listtransactions result.

This field is only set for non-"send" categories since the wallet does
not track a "from account" like Core's wallet does.

Fixes #353.
This commit is contained in:
Josh Rickmar 2016-02-01 18:05:05 -05:00
parent cc97e063b8
commit b480a0a09d
2 changed files with 23 additions and 11 deletions

View file

@ -760,7 +760,8 @@ func (t relevantTx) notificationCmds(w *wallet.Wallet) []interface{} {
return nil return nil
} }
ltr := wallet.ListTransactions(details, syncBlock.Height, w.ChainParams()) ltr := wallet.ListTransactions(details, w.Manager, syncBlock.Height,
w.ChainParams())
ntfns := make([]interface{}, len(ltr)) ntfns := make([]interface{}, len(ltr))
for i := range ntfns { for i := range ntfns {
ntfns[i] = btcjson.NewNewTxNtfn(ltr[i].Account, ltr[i]) ntfns[i] = btcjson.NewNewTxNtfn(ltr[i].Account, ltr[i])

View file

@ -975,9 +975,10 @@ func RecvCategory(details *wtxmgr.TxDetails, syncHeight int32) CreditCategory {
// ListTransactions creates a object that may be marshalled to a response result // ListTransactions creates a object that may be marshalled to a response result
// for a listtransactions RPC. // for a listtransactions RPC.
// //
// TODO: This should be moved out of this package into the main package's // TODO: This should be moved to the legacyrpc package.
// rpcserver.go, along with everything that requires this. func ListTransactions(details *wtxmgr.TxDetails, addrMgr *waddrmgr.Manager,
func ListTransactions(details *wtxmgr.TxDetails, syncHeight int32, net *chaincfg.Params) []btcjson.ListTransactionsResult { syncHeight int32, net *chaincfg.Params) []btcjson.ListTransactionsResult {
var ( var (
blockHashStr string blockHashStr string
blockTime int64 blockTime int64
@ -1034,19 +1035,28 @@ outputs:
} }
var address string var address string
var accountName string
_, addrs, _, _ := txscript.ExtractPkScriptAddrs(output.PkScript, net) _, addrs, _, _ := txscript.ExtractPkScriptAddrs(output.PkScript, net)
if len(addrs) == 1 { if len(addrs) == 1 {
address = addrs[0].EncodeAddress() addr := addrs[0]
address = addr.EncodeAddress()
account, err := addrMgr.AddrAccount(addrs[0])
if err == nil {
accountName, err = addrMgr.AccountName(account)
if err != nil {
accountName = ""
}
}
} }
amountF64 := btcutil.Amount(output.Value).ToBTC() amountF64 := btcutil.Amount(output.Value).ToBTC()
result := btcjson.ListTransactionsResult{ result := btcjson.ListTransactionsResult{
// Fields left zeroed: // Fields left zeroed:
// InvolvesWatchOnly // InvolvesWatchOnly
// Account
// BlockIndex // BlockIndex
// //
// Fields set below: // Fields set below:
// Account (only for non-"send" categories)
// Category // Category
// Amount // Amount
// Fee // Fee
@ -1079,6 +1089,7 @@ outputs:
results = append(results, result) results = append(results, result)
} }
if isCredit { if isCredit {
result.Account = accountName
result.Category = recvCat result.Category = recvCat
result.Amount = amountF64 result.Amount = amountF64
result.Fee = nil result.Fee = nil
@ -1095,8 +1106,8 @@ func (w *Wallet) ListSinceBlock(start, end, syncHeight int32) ([]btcjson.ListTra
txList := []btcjson.ListTransactionsResult{} txList := []btcjson.ListTransactionsResult{}
err := w.TxStore.RangeTransactions(start, end, func(details []wtxmgr.TxDetails) (bool, error) { err := w.TxStore.RangeTransactions(start, end, func(details []wtxmgr.TxDetails) (bool, error) {
for _, detail := range details { for _, detail := range details {
jsonResults := ListTransactions(&detail, syncHeight, jsonResults := ListTransactions(&detail, w.Manager,
w.chainParams) syncHeight, w.chainParams)
txList = append(txList, jsonResults...) txList = append(txList, jsonResults...)
} }
return false, nil return false, nil
@ -1138,7 +1149,7 @@ func (w *Wallet) ListTransactions(from, count int) ([]btcjson.ListTransactionsRe
} }
jsonResults := ListTransactions(&details[i], jsonResults := ListTransactions(&details[i],
syncBlock.Height, w.chainParams) w.Manager, syncBlock.Height, w.chainParams)
txList = append(txList, jsonResults...) txList = append(txList, jsonResults...)
} }
@ -1181,7 +1192,7 @@ func (w *Wallet) ListAddressTransactions(pkHashes map[string]struct{}) (
continue continue
} }
jsonResults := ListTransactions(detail, jsonResults := ListTransactions(detail, w.Manager,
syncBlock.Height, w.chainParams) syncBlock.Height, w.chainParams)
if err != nil { if err != nil {
return false, err return false, err
@ -1214,7 +1225,7 @@ func (w *Wallet) ListAllTransactions() ([]btcjson.ListTransactionsResult, error)
// unsorted, but it will process mined transactions in the // unsorted, but it will process mined transactions in the
// reverse order they were marked mined. // reverse order they were marked mined.
for i := len(details) - 1; i >= 0; i-- { for i := len(details) - 1; i >= 0; i-- {
jsonResults := ListTransactions(&details[i], jsonResults := ListTransactions(&details[i], w.Manager,
syncBlock.Height, w.chainParams) syncBlock.Height, w.chainParams)
txList = append(txList, jsonResults...) txList = append(txList, jsonResults...)
} }