def test_create_and_sign(self):
        collateral_pub = '038ae57bd0fa5b45640e771614ec571c7326a2266c78bb444f1971c85188411ba1'  # XahPxwmCuKjPq69hzVxP18V1eASwDWbUrn
        delegate_pub = '02526201c87c1b4630aabbd04572eec3e2545e442503e57e60880fafcc1f684dbc'  # Xx2nSdhaT7c9SREKBPAgzpkhu518XFgkgh
        protocol_version = 70103

        ip = '0.0.0.0'
        port = 20000
        addr = NetworkAddress(ip=ip, port=port)

        vin = {
            'prevout_hash': '00' * 32,
            'prevout_n': 0,
            'scriptSig': '',
            'sequence': 0xffffffff
        }

        last_ping = MasternodePing(vin=vin, block_hash='ff' * 32)

        announce = MasternodeAnnounce(vin=vin,
                                      addr=addr,
                                      collateral_key=collateral_pub,
                                      delegate_key=delegate_pub,
                                      protocol_version=protocol_version,
                                      last_ping=last_ping)

        collateral_wif = 'XJqCcyfnLYK4Y7ZDVjLrgPnsrq2cWMF6MX9cyhKgfMajwqrCwZaS'
        delegate_wif = 'XCbhXBc2N9q8kxqBF41rSuLWVpVVbDm7P1oPv9GxcrS9QXYBWZkB'
        announce.last_ping.sign(delegate_wif, bfh(delegate_pub), 1461858375)
        sig = announce.sign(collateral_wif, 1461858375)

        address = 'XahPxwmCuKjPq69hzVxP18V1eASwDWbUrn'
        self.assertTrue(announce.verify(address))
        self.assertTrue(
            ecc.verify_message_with_address(address, sig,
                                            announce.serialize_for_sig()))
    def test_verify(self):
        announce = MasternodeAnnounce.deserialize(raw_announce)
        message = announce.serialize_for_sig()

        pk = bitcoin.public_key_to_p2pkh(bfh(announce.collateral_key))
        self.assertTrue(announce.verify())

        raw = '7ca6564432d0e0920b811887e1f9077a92924c83564e6ea8ea874fc8843ccd2b0000000000ffffffff00000000000000000000ffffc0a801014e1f410411e2638aeb4584ff2e027b6ee20e05655ff05583185b1d87188185d6955534fe02ad35caabb5e6e9ce8747ba73fdccccd2369feb9a6f2b0bdee93378e7c8f1c0410411e2638aeb4584ff2e027b6ee20e05655ff05583185b1d87188185d6955534fe02ad35caabb5e6e9ce8747ba73fdccccd2369feb9a6f2b0bdee93378e7c8f1c0411bab132617d8e6a0e3b5434c91a5a64ff13a9cfadc6c178a47b87691f13a26e7440c08660e488ddf927bba1bf04c1ec196370452a30fd3381ea8ba27d627f9d4468be80e5700000000d71101007ca6564432d0e0920b811887e1f9077a92924c83564e6ea8ea874fc8843ccd2b0000000000ffffffffd75eb4fa0cb71dd2e99d7b242784a5601c5c86d7c1cf0362a3391575070000008be80e5700000000411b6d5985008e0821c936fafc192f31963141ae2fab837e84bb9f12422711c1952d5750f9a781c89117a6f4576edc1149a1bf211e7151c5c88cf3252e2d83cb154a0000000000000000'
        announce = MasternodeAnnounce.deserialize(raw)
        msg = announce.serialize_for_sig()

        pk = bitcoin.public_key_to_p2pkh(bfh(announce.collateral_key))
        self.assertTrue(announce.verify(pk))
    def test_serialization(self):
        announce = MasternodeAnnounce.deserialize(raw_announce)

        self.assertEqual(
            'e23d7d08e8a5b72313f2b0cd9f5ff14647f3055538c4761d4e928662670ad5f3',
            announce.vin['prevout_hash'])
        self.assertEqual(0, announce.vin['prevout_n'])
        self.assertEqual('', announce.vin['scriptSig'])
        self.assertEqual(0xffffffff, announce.vin['sequence'])

        self.assertEqual('45.32.125.85:20000', str(announce.addr))

        self.assertEqual(
            '039c8cef4a10b7246796563b4ae7614aaafcd61550b38acc296085dc1e942f52bc',
            announce.collateral_key)
        self.assertEqual(
            '0446eac61b70401b6ccf63d4bacf16991180c2b9cc25138fa710da62449659ca0b1ce362931b951e87c818170e927a422992a3dd32aad321e9b6d91430a4ffaf87',
            announce.delegate_key)
        self.assertEqual(
            'H6vjQtcmZ4riE5xeVocIAxEfnOAMDfi0jU06p+8MlflWeSO7U9zns6HqaUERq7+Vb4T/FxmSLgjb0FMPyiPOFEQ=',
            base64.b64encode(announce.sig).decode('utf-8'))
        self.assertEqual(1563376416, announce.sig_time)
        self.assertEqual(70214, announce.protocol_version)

        #self.assertEqual('e23d7d08e8a5b72313f2b0cd9f5ff14647f3055538c4761d4e928662670ad5f3', announce.last_ping.vin['prevout_hash'])
        #self.assertEqual(0, announce.last_ping.vin['prevout_n'])
        #self.assertEqual('', announce.last_ping.vin['scriptSig'])
        #self.assertEqual(0xffffffff, announce.last_ping.vin['sequence'])
        #self.assertEqual('1791b63bba79ad9211f6aca8d7dfa2aee7ca1ee7e11942e6f687a53ca42e72e8', announce.last_ping.block_hash)
        #self.assertEqual(1460397824, announce.last_ping.sig_time)
        #self.assertEqual('G1xOVjKTYrg9/LuqcDl8jEnLfmnt+FsOgjLSeYtxluxwUDYGndyREm35jbT2dVsuwBV3ykF9+qLpCgOCvWZ+QQ0=', base64.b64encode(announce.last_ping.sig).decode('utf-8'))

        self.assertEqual(raw_announce, announce.serialize())
    def save_current_masternode(self, as_new=False):
        """Save the masternode that is being viewed.
        If as_new is True, a new masternode will be created.
        """
        delegate_privkey = str(self.masternode_editor.delegate_key_edit.text())
        if not delegate_privkey:
            QMessageBox.warning(self, _('Warning'),
                                _('Delegate private key is empty.'))
            return

        try:
            delegate_pubkey = self.manager.import_masternode_delegate(
                delegate_privkey)
        except Exception:
            # Show an error if the private key is invalid and not an empty string.
            if delegate_privkey:
                QMessageBox.warning(
                    self, _('Warning'),
                    _('Ignoring invalid delegate private key.'))
            delegate_pubkey = ''

        alias = str(self.masternode_editor.alias_edit.text())
        # Construct a new masternode.
        if as_new:
            kwargs = self.masternode_editor.get_masternode_args()
            kwargs['delegate_key'] = delegate_pubkey
            del kwargs['vin']
            self.mapper.revert()
            self.masternodes_widget.add_masternode(
                MasternodeAnnounce(**kwargs))
        else:
            self.mapper.submit()
        self.manager.save()
        self.masternodes_widget.select_masternode(alias)
    def test_hash(self):
        announce = MasternodeAnnounce.deserialize(raw_announce)

        expected_hash = 'a8a3dc1782191f28f613c8971709a57ee58a4d0d7a11138804f89a0b088d67d1'
        msg = announce.serialize_for_sig()

        h = sha256d(ecc.msg_magic(msg))
        h = bitcoin.hash_encode(h)
        self.assertEqual(expected_hash, h)
    def test_hash(self):
        announce = MasternodeAnnounce.deserialize(raw_announce_70210)

        expected_hash = '5f69e59f5ea327be16e649fb6c72ed02e39ef9dae8ecb27d222419e94dcd89b7'
        msg = announce.serialize_for_sig()

        h = sha256d(ecc.msg_magic(msg))
        h = bitcoin.hash_encode(h)
        self.assertEqual(expected_hash, h)
    def __init__(self, manager, parent):
        super(MasternodeDialog, self).__init__(parent)
        Logger.__init__(self)
        self.gui = parent
        self.manager = manager
        self.setWindowTitle(_('Masternode Manager'))

        self.waiting_dialog = None
        self.create_layout()
        # Create a default masternode if none are present.
        if len(self.manager.masternodes) == 0:
            self.masternodes_widget.add_masternode(
                MasternodeAnnounce(alias='default'), save=False)
        self.masternodes_widget.view.selectRow(0)
    def test_serialize_protocol_version_70210(self):
        announce = MasternodeAnnounce.deserialize(raw_announce_70210)
        msg = announce.serialize_for_sig()
        expected = to_bytes(''.join([
            '45.32.125.85:20000',
            '1563376416',
            bitcoin.hash_encode(
                bitcoin.hash_160(
                    bfh('039c8cef4a10b7246796563b4ae7614aaafcd61550b38acc296085dc1e942f52bc'
                        ))),
            bitcoin.hash_encode(
                bitcoin.hash_160(
                    bfh('0446eac61b70401b6ccf63d4bacf16991180c2b9cc25138fa710da62449659ca0b1ce362931b951e87c818170e927a422992a3dd32aad321e9b6d91430a4ffaf87'
                        ))),
            '70214',
        ]))
        print('7' * 50, expected)
        print('8' * 50, msg)

        self.assertEqual(expected, msg)
    def test_serialize_protocol_version_70201(self):
        raw = '08108933d948aed6a107cd01e7862ed61ef9bf14e87da0a14e8d17791e9f9c570100000000ffffffff00000000000000000000ffff7f0000014e1f210269e1abb1ffe231ea045068272a06f0fae231d11b11a54225867d89267faa4e23210269e1abb1ffe231ea045068272a06f0fae231d11b11a54225867d89267faa4e234120b8bc547ce2471125eddfdfd5af30ea1e892e750acfe2896b241097b7f21442a61da073d47c885535769bf215eb3e97eca692d868db1bfb9dee469a1ece5acb92a1945457000000003912010008108933d948aed6a107cd01e7862ed61ef9bf14e87da0a14e8d17791e9f9c570100000000ffffffffefc894d8431c1774a19aeb732ea7fc56925b740ed80486f30424109a05000000a1945457000000004120818f17742e6644359c8b9a91e56b595615bd2c593de713304435dcfd07ceb6a815559fd3b2f05f531d9b9918b22b8748491c3f36cb25e8397ff950f74030444f0000000000000000'
        announce = MasternodeAnnounce.deserialize(raw)
        announce.sig_time = 1465161129
        msg = announce.serialize_for_sig()
        expected = to_bytes(''.join([
            '127.0.0.1:19999',
            '1465161129',
            bitcoin.hash_encode(
                bitcoin.hash_160(
                    bfh('0269e1abb1ffe231ea045068272a06f0fae231d11b11a54225867d89267faa4e23'
                        ))),
            bitcoin.hash_encode(
                bitcoin.hash_160(
                    bfh('0269e1abb1ffe231ea045068272a06f0fae231d11b11a54225867d89267faa4e23'
                        ))),
            '70201',
        ]))
        print('7' * 50, expected)
        print('8' * 50, msg)

        self.assertEqual(expected, msg)
    def test_serialization(self):
        announce = MasternodeAnnounce.deserialize(raw_announce_70210)

        self.assertEqual(
            '54db7eb00977d96b8f0a78fec4689f0a2d9ac0792676790a32265d4b0c82a3d0',
            announce.vin['prevout_hash'])
        self.assertEqual(1, announce.vin['prevout_n'])
        self.assertEqual('', announce.vin['scriptSig'])
        self.assertEqual(0xffffffff, announce.vin['sequence'])

        self.assertEqual('178.151.192.107:19999', str(announce.addr))

        self.assertEqual(
            '0221088c51bef8c9c891b385fa1e8a78b016f01db41741aea7e43e67a7415ab7be',
            announce.collateral_key)
        self.assertEqual(
            '042379a871a10ae6bf06e756262f69d7f0ce9b8b562f223bde964db573fb7d0f1e219c246a4b3a6133c5cec136d83f4049df51321ba2cb01d676cc3982c7e24d1f',
            announce.delegate_key)
        self.assertEqual(
            'ICj1/mZo5pqUMlYFghCA9BeTAsDpu4fnVzUnPmMHRTSUPtSbNPeUokmH1Q4hfp9JCOt+RypYFc1QlCuaa5P/hWc=',
            base64.b64encode(announce.sig).decode('utf-8'))
        self.assertEqual(1530021571, announce.sig_time)
        self.assertEqual(70210, announce.protocol_version)

        self.assertEqual(
            '54db7eb00977d96b8f0a78fec4689f0a2d9ac0792676790a32265d4b0c82a3d0',
            announce.last_ping.vin['prevout_hash'])
        self.assertEqual(1, announce.last_ping.vin['prevout_n'])
        self.assertEqual('', announce.last_ping.vin['scriptSig'])
        self.assertEqual(0xffffffff, announce.last_ping.vin['sequence'])
        self.assertEqual(
            '00000000004eeaf1ee1a0b0bd957ed3d905052efad13d05e3f6aa756ceb2cbd8',
            announce.last_ping.block_hash)
        self.assertEqual(1530021570, announce.last_ping.sig_time)
        self.assertEqual(
            'HEajdRdRbdZNWe47RIvIr4mT8p3Q4Zg2YCEuhk4owmLPXyIiOmBP/FA8Q8m/dwv0SO/ibAB3ORlE49ls3Cri9gk=',
            base64.b64encode(announce.last_ping.sig).decode('utf-8'))

        self.assertEqual(raw_announce_70210, announce.serialize())
 def test_get_hash(self):
     announce = MasternodeAnnounce.deserialize(raw_announce)
     expected = 'ef3fe643adc638044b32879bc644a0fb1ebe6ca75281368184c891da9c07986b'
     self.assertEqual(expected, announce.get_hash())
    def test_verify(self):
        announce = MasternodeAnnounce.deserialize(raw_announce_70210)
        message = announce.serialize_for_sig()

        pk = bitcoin.public_key_to_p2pkh(bfh(announce.collateral_key))
        self.assertTrue(announce.verify())
 def test_get_hash(self):
     announce = MasternodeAnnounce.deserialize(raw_announce_70210)
     expected = '154f19205294c3c1077d0f34473673998b6f6139209110705cee10d66e7d73af'
     self.assertEqual(expected, announce.get_hash())