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