@@ -572,6 +572,52 @@ static UniValue signmessage(const JSONRPCRequest& request)
572
572
return signature;
573
573
}
574
574
575
+ static CAmount GetReceived (interfaces::Chain::Lock& locked_chain, const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
576
+ {
577
+ std::set<CTxDestination> address_set;
578
+
579
+ if (by_label) {
580
+ // Get the set of addresses assigned to label
581
+ std::string label = LabelFromValue (params[0 ]);
582
+ address_set = wallet.GetLabelAddresses (label);
583
+ } else {
584
+ // Get the address
585
+ CTxDestination dest = DecodeDestination (params[0 ].get_str ());
586
+ if (!IsValidDestination (dest)) {
587
+ throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Invalid Bitcoin address" );
588
+ }
589
+ CScript script_pub_key = GetScriptForDestination (dest);
590
+ if (!wallet.IsMine (script_pub_key)) {
591
+ throw JSONRPCError (RPC_WALLET_ERROR, " Address not found in wallet" );
592
+ }
593
+ address_set.insert (dest);
594
+ }
595
+
596
+ // Minimum confirmations
597
+ int min_depth = 1 ;
598
+ if (!params[1 ].isNull ())
599
+ min_depth = params[1 ].get_int ();
600
+
601
+ // Tally
602
+ CAmount amount = 0 ;
603
+ for (const std::pair<const uint256, CWalletTx>& wtx_pair : wallet.mapWallet ) {
604
+ const CWalletTx& wtx = wtx_pair.second ;
605
+ if (wtx.IsCoinBase () || !locked_chain.checkFinalTx (*wtx.tx ) || wtx.GetDepthInMainChain () < min_depth) {
606
+ continue ;
607
+ }
608
+
609
+ for (const CTxOut& txout : wtx.tx ->vout ) {
610
+ CTxDestination address;
611
+ if (ExtractDestination (txout.scriptPubKey , address) && wallet.IsMine (address) && address_set.count (address)) {
612
+ amount += txout.nValue ;
613
+ }
614
+ }
615
+ }
616
+
617
+ return amount;
618
+ }
619
+
620
+
575
621
static UniValue getreceivedbyaddress (const JSONRPCRequest& request)
576
622
{
577
623
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest (request);
@@ -609,36 +655,7 @@ static UniValue getreceivedbyaddress(const JSONRPCRequest& request)
609
655
auto locked_chain = pwallet->chain ().lock ();
610
656
LOCK (pwallet->cs_wallet );
611
657
612
- // Bitcoin address
613
- CTxDestination dest = DecodeDestination (request.params [0 ].get_str ());
614
- if (!IsValidDestination (dest)) {
615
- throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Invalid Bitcoin address" );
616
- }
617
- CScript scriptPubKey = GetScriptForDestination (dest);
618
- if (!pwallet->IsMine (scriptPubKey)) {
619
- throw JSONRPCError (RPC_WALLET_ERROR, " Address not found in wallet" );
620
- }
621
-
622
- // Minimum confirmations
623
- int nMinDepth = 1 ;
624
- if (!request.params [1 ].isNull ())
625
- nMinDepth = request.params [1 ].get_int ();
626
-
627
- // Tally
628
- CAmount nAmount = 0 ;
629
- for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet ) {
630
- const CWalletTx& wtx = pairWtx.second ;
631
- if (wtx.IsCoinBase () || !locked_chain->checkFinalTx (*wtx.tx )) {
632
- continue ;
633
- }
634
-
635
- for (const CTxOut& txout : wtx.tx ->vout )
636
- if (txout.scriptPubKey == scriptPubKey)
637
- if (wtx.GetDepthInMainChain () >= nMinDepth)
638
- nAmount += txout.nValue ;
639
- }
640
-
641
- return ValueFromAmount (nAmount);
658
+ return ValueFromAmount (GetReceived (*locked_chain, *pwallet, request.params , /* by_label */ false ));
642
659
}
643
660
644
661
@@ -679,34 +696,7 @@ static UniValue getreceivedbylabel(const JSONRPCRequest& request)
679
696
auto locked_chain = pwallet->chain ().lock ();
680
697
LOCK (pwallet->cs_wallet );
681
698
682
- // Minimum confirmations
683
- int nMinDepth = 1 ;
684
- if (!request.params [1 ].isNull ())
685
- nMinDepth = request.params [1 ].get_int ();
686
-
687
- // Get the set of pub keys assigned to label
688
- std::string label = LabelFromValue (request.params [0 ]);
689
- std::set<CTxDestination> setAddress = pwallet->GetLabelAddresses (label);
690
-
691
- // Tally
692
- CAmount nAmount = 0 ;
693
- for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet ) {
694
- const CWalletTx& wtx = pairWtx.second ;
695
- if (wtx.IsCoinBase () || !locked_chain->checkFinalTx (*wtx.tx )) {
696
- continue ;
697
- }
698
-
699
- for (const CTxOut& txout : wtx.tx ->vout )
700
- {
701
- CTxDestination address;
702
- if (ExtractDestination (txout.scriptPubKey , address) && pwallet->IsMine (address) && setAddress.count (address)) {
703
- if (wtx.GetDepthInMainChain () >= nMinDepth)
704
- nAmount += txout.nValue ;
705
- }
706
- }
707
- }
708
-
709
- return ValueFromAmount (nAmount);
699
+ return ValueFromAmount (GetReceived (*locked_chain, *pwallet, request.params , /* by_label */ true ));
710
700
}
711
701
712
702
0 commit comments