wallet_signmessagewithaddress.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2016-present The Bitcoin Core developers
   3  # Distributed under the MIT software license, see the accompanying
   4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  """Test Wallet commands for signing and verifying messages."""
   6  
   7  from test_framework.test_framework import BitcoinTestFramework
   8  from test_framework.util import (
   9      assert_raises_rpc_error,
  10  )
  11  
  12  class SignMessagesWithAddressTest(BitcoinTestFramework):
  13      def set_test_params(self):
  14          self.setup_clean_chain = True
  15          self.num_nodes = 1
  16          self.extra_args = [["-addresstype=legacy"]]
  17  
  18      def skip_test_if_missing_module(self):
  19          self.skip_if_no_wallet()
  20  
  21      def run_test(self):
  22          message = 'This is just a test message'
  23  
  24          self.log.info('test signing with an address with wallet')
  25          address = self.nodes[0].getnewaddress()
  26          signature = self.nodes[0].signmessage(address, message)
  27          assert self.nodes[0].verifymessage(address, signature, message)
  28  
  29          self.log.info('test verifying with another address should not work')
  30          other_address = self.nodes[0].getnewaddress()
  31          other_signature = self.nodes[0].signmessage(other_address, message)
  32          assert not self.nodes[0].verifymessage(other_address, signature, message)
  33          assert not self.nodes[0].verifymessage(address, other_signature, message)
  34  
  35          self.log.info('test parameter validity and error codes')
  36          # signmessage has two required parameters
  37          for num_params in [0, 1, 3, 4, 5]:
  38              param_list = ["dummy"]*num_params
  39              assert_raises_rpc_error(-1, "signmessage", self.nodes[0].signmessage, *param_list)
  40          # invalid key or address provided
  41          assert_raises_rpc_error(-5, "Invalid address", self.nodes[0].signmessage, "invalid_addr", message)
  42  
  43  
  44  if __name__ == '__main__':
  45      SignMessagesWithAddressTest(__file__).main()
  46