|
22 | 22 | import sys
|
23 | 23 |
|
24 | 24 | from .authproxy import JSONRPCException
|
| 25 | +from .descriptors import descsum_create |
25 | 26 | from .util import (
|
26 | 27 | MAX_NODES,
|
27 | 28 | append_config,
|
@@ -170,10 +171,10 @@ def __del__(self):
|
170 | 171 | def __getattr__(self, name):
|
171 | 172 | """Dispatches any unrecognised messages to the RPC connection or a CLI instance."""
|
172 | 173 | if self.use_cli:
|
173 |
| - return getattr(self.cli, name) |
| 174 | + return getattr(RPCOverloadWrapper(self.cli, True), name) |
174 | 175 | else:
|
175 | 176 | assert self.rpc_connected and self.rpc is not None, self._node_msg("Error: no RPC connection")
|
176 |
| - return getattr(self.rpc, name) |
| 177 | + return getattr(RPCOverloadWrapper(self.rpc), name) |
177 | 178 |
|
178 | 179 | def start(self, extra_args=None, *, cwd=None, stdout=None, stderr=None, **kwargs):
|
179 | 180 | """Start the node."""
|
@@ -265,11 +266,11 @@ def generate(self, nblocks, maxtries=1000000):
|
265 | 266 |
|
266 | 267 | def get_wallet_rpc(self, wallet_name):
|
267 | 268 | if self.use_cli:
|
268 |
| - return self.cli("-rpcwallet={}".format(wallet_name)) |
| 269 | + return RPCOverloadWrapper(self.cli("-rpcwallet={}".format(wallet_name)), True) |
269 | 270 | else:
|
270 | 271 | assert self.rpc_connected and self.rpc, self._node_msg("RPC not connected")
|
271 | 272 | wallet_path = "wallet/{}".format(urllib.parse.quote(wallet_name))
|
272 |
| - return self.rpc / wallet_path |
| 273 | + return RPCOverloadWrapper(self.rpc / wallet_path) |
273 | 274 |
|
274 | 275 | def stop_node(self, expected_stderr='', wait=0):
|
275 | 276 | """Stop the node."""
|
@@ -595,3 +596,103 @@ def send_cli(self, command=None, *args, **kwargs):
|
595 | 596 | return json.loads(cli_stdout, parse_float=decimal.Decimal)
|
596 | 597 | except json.JSONDecodeError:
|
597 | 598 | return cli_stdout.rstrip("\n")
|
| 599 | + |
| 600 | +class RPCOverloadWrapper(): |
| 601 | + def __init__(self, rpc, cli=False): |
| 602 | + self.rpc = rpc |
| 603 | + self.is_cli = cli |
| 604 | + |
| 605 | + def __getattr__(self, name): |
| 606 | + return getattr(self.rpc, name) |
| 607 | + |
| 608 | + def importprivkey(self, privkey, label=None, rescan=None): |
| 609 | + wallet_info = self.getwalletinfo() |
| 610 | + if self.is_cli: |
| 611 | + if label is None: |
| 612 | + label = 'null' |
| 613 | + if rescan is None: |
| 614 | + rescan = 'null' |
| 615 | + if 'descriptors' not in wallet_info or ('descriptors' in wallet_info and not wallet_info['descriptors']): |
| 616 | + return self.__getattr__('importprivkey')(privkey, label, rescan) |
| 617 | + desc = descsum_create('combo(' + privkey + ')') |
| 618 | + req = [{ |
| 619 | + 'desc': desc, |
| 620 | + 'timestamp': 0 if rescan else 'now', |
| 621 | + 'label': label if label else '' |
| 622 | + }] |
| 623 | + import_res = self.importdescriptors(req) |
| 624 | + if not import_res[0]['success']: |
| 625 | + raise JSONRPCException(import_res[0]['error']) |
| 626 | + |
| 627 | + def addmultisigaddress(self, nrequired, keys, label=None, address_type=None): |
| 628 | + wallet_info = self.getwalletinfo() |
| 629 | + if self.is_cli: |
| 630 | + if label is None: |
| 631 | + label = 'null' |
| 632 | + if address_type is None: |
| 633 | + address_type = 'null' |
| 634 | + if 'descriptors' not in wallet_info or ('descriptors' in wallet_info and not wallet_info['descriptors']): |
| 635 | + return self.__getattr__('addmultisigaddress')(nrequired, keys, label, address_type) |
| 636 | + cms = self.createmultisig(nrequired, keys, address_type) |
| 637 | + req = [{ |
| 638 | + 'desc': cms['descriptor'], |
| 639 | + 'timestamp': 0, |
| 640 | + 'label': label if label else '' |
| 641 | + }] |
| 642 | + import_res = self.importdescriptors(req) |
| 643 | + if not import_res[0]['success']: |
| 644 | + raise JSONRPCException(import_res[0]['error']) |
| 645 | + return cms |
| 646 | + |
| 647 | + def importpubkey(self, pubkey, label=None, rescan=None): |
| 648 | + wallet_info = self.getwalletinfo() |
| 649 | + if self.is_cli: |
| 650 | + if label is None: |
| 651 | + label = 'null' |
| 652 | + if rescan is None: |
| 653 | + rescan = 'null' |
| 654 | + if 'descriptors' not in wallet_info or ('descriptors' in wallet_info and not wallet_info['descriptors']): |
| 655 | + return self.__getattr__('importpubkey')(pubkey, label, rescan) |
| 656 | + desc = descsum_create('combo(' + pubkey + ')') |
| 657 | + req = [{ |
| 658 | + 'desc': desc, |
| 659 | + 'timestamp': 0 if rescan else 'now', |
| 660 | + 'label': label if label else '' |
| 661 | + }] |
| 662 | + import_res = self.importdescriptors(req) |
| 663 | + if not import_res[0]['success']: |
| 664 | + raise JSONRPCException(import_res[0]['error']) |
| 665 | + |
| 666 | + def importaddress(self, address, label=None, rescan=None, p2sh=None): |
| 667 | + wallet_info = self.getwalletinfo() |
| 668 | + if self.is_cli: |
| 669 | + if label is None: |
| 670 | + label = 'null' |
| 671 | + if rescan is None: |
| 672 | + rescan = 'null' |
| 673 | + if p2sh is None: |
| 674 | + p2sh = 'null' |
| 675 | + if 'descriptors' not in wallet_info or ('descriptors' in wallet_info and not wallet_info['descriptors']): |
| 676 | + return self.__getattr__('importaddress')(address, label, rescan, p2sh) |
| 677 | + is_hex = False |
| 678 | + try: |
| 679 | + int(address ,16) |
| 680 | + is_hex = True |
| 681 | + desc = descsum_create('raw(' + address + ')') |
| 682 | + except: |
| 683 | + desc = descsum_create('addr(' + address + ')') |
| 684 | + reqs = [{ |
| 685 | + 'desc': desc, |
| 686 | + 'timestamp': 0 if rescan else 'now', |
| 687 | + 'label': label if label else '' |
| 688 | + }] |
| 689 | + if is_hex and p2sh: |
| 690 | + reqs.append({ |
| 691 | + 'desc': descsum_create('p2sh(raw(' + address + '))'), |
| 692 | + 'timestamp': 0 if rescan else 'now', |
| 693 | + 'label': label if label else '' |
| 694 | + }) |
| 695 | + import_res = self.importdescriptors(reqs) |
| 696 | + for res in import_res: |
| 697 | + if not res['success']: |
| 698 | + raise JSONRPCException(res['error']) |
0 commit comments