Ejemplo n.º 1
0
 async def test_private_key_validation(self):
     with self.assertRaisesRegex(TypeError,
                                 'private key must be raw bytes'):
         PrivateKey(None, None, b'abcd' * 8, 0, 255)
     with self.assertRaisesRegex(ValueError,
                                 'private key must be 32 bytes'):
         PrivateKey(None, b'abcd', b'abcd' * 8, 0, 255)
     private_key = PrivateKey(
         ledger_class({
             'db': ledger_class.database_class(':memory:'),
             'headers': ledger_class.headers_class(':memory:'),
         }),
         unhexlify(
             '2423f3dc6087d9683f73a684935abc0ccd8bc26370588f56653128c6a6f0bf7c'
         ), b'abcd' * 8, 0, 1)
     ec_point = private_key.ec_point()
     self.assertEqual(
         ec_point[0],
         30487144161998778625547553412379759661411261804838752332906558028921886299019
     )
     self.assertEqual(
         ec_point[1],
         86198965946979720220333266272536217633917099472454294641561154971209433250106
     )
     self.assertEqual(private_key.address(),
                      '1GVM5dEhThbiyCZ9gqBZBv6p9whga7MTXo')
     with self.assertRaisesRegex(ValueError,
                                 'invalid BIP32 private key child number'):
         private_key.child(-1)
     self.assertIsInstance(private_key.child(PrivateKey.HARDENED),
                           PrivateKey)
Ejemplo n.º 2
0
 def setUp(self):
     self.ledger = ledger_class({
         'db':
         ledger_class.database_class(':memory:'),
         'headers':
         ledger_class.headers_class(':memory:'),
     })
Ejemplo n.º 3
0
 async def asyncSetUp(self):
     self.ledger = ledger_class({
         'db': ledger_class.database_class(':memory:'),
         'headers': ledger_class.headers_class(':memory:'),
     })
     self.wallet = Wallet()
     await self.ledger.db.open()
Ejemplo n.º 4
0
 async def asyncSetUp(self):
     self.ledger = ledger_class({
         'db': ledger_class.database_class(':memory:'),
         'headers': ledger_class.headers_class(':memory:'),
     })
     await self.ledger.db.open()
     self.account = self.ledger.account_class.generate(self.ledger, Wallet(), "torba")
Ejemplo n.º 5
0
 async def asyncSetUp(self):
     self.ledger = MainNetLedger({
         'db':
         MainNetLedger.database_class(':memory:'),
         'headers':
         MainNetLedger.headers_class(':memory:')
     })
     await self.ledger.db.open()
Ejemplo n.º 6
0
    async def test_reset_on_version_change(self):
        self.ledger = ledger_class({
            'db':
            ledger_class.database_class(self.path),
            'headers':
            ledger_class.headers_class(':memory:'),
        })

        # initial open, pre-version enabled db
        self.ledger.db.SCHEMA_VERSION = None
        self.assertListEqual(self.get_tables(), [])
        await self.ledger.db.open()
        self.assertEqual(
            self.get_tables(),
            ['account_address', 'pubkey_address', 'tx', 'txi', 'txo'])
        self.assertListEqual(self.get_addresses(), [])
        self.add_address('address1')
        await self.ledger.db.close()

        # initial open after version enabled
        self.ledger.db.SCHEMA_VERSION = '1.0'
        await self.ledger.db.open()
        self.assertEqual(self.get_version(), '1.0')
        self.assertListEqual(self.get_tables(), [
            'account_address', 'pubkey_address', 'tx', 'txi', 'txo', 'version'
        ])
        self.assertListEqual(self.get_addresses(),
                             [])  # address1 deleted during version upgrade
        self.add_address('address2')
        await self.ledger.db.close()

        # nothing changes
        self.assertEqual(self.get_version(), '1.0')
        self.assertListEqual(self.get_tables(), [
            'account_address', 'pubkey_address', 'tx', 'txi', 'txo', 'version'
        ])
        await self.ledger.db.open()
        self.assertEqual(self.get_version(), '1.0')
        self.assertListEqual(self.get_tables(), [
            'account_address', 'pubkey_address', 'tx', 'txi', 'txo', 'version'
        ])
        self.assertListEqual(self.get_addresses(), ['address2'])
        await self.ledger.db.close()

        # upgrade version, database reset
        self.ledger.db.SCHEMA_VERSION = '1.1'
        self.ledger.db.CREATE_TABLES_QUERY += """
        create table if not exists foo (bar text);
        """
        await self.ledger.db.open()
        self.assertEqual(self.get_version(), '1.1')
        self.assertListEqual(self.get_tables(), [
            'account_address', 'foo', 'pubkey_address', 'tx', 'txi', 'txo',
            'version'
        ])
        self.assertListEqual(self.get_addresses(), [])  # all tables got reset
        await self.ledger.db.close()
Ejemplo n.º 7
0
 def setUp(self):
     super().setUp()
     self.ledger = MainNetLedger({
         'db':
         MainNetLedger.database_class(':memory:'),
         'headers':
         MainNetLedger.headers_class(':memory:')
     })
     return self.ledger.db.open()
Ejemplo n.º 8
0
 async def test_from_extended_keys(self):
     ledger = ledger_class({
         'db':
         ledger_class.database_class(':memory:'),
         'headers':
         ledger_class.headers_class(':memory:'),
     })
     self.assertIsInstance(
         from_extended_key_string(
             ledger,
             'xprv9s21ZrQH143K2dyhK7SevfRG72bYDRNv25yKPWWm6dqApNxm1Zb1m5gGcBWYfbsPjTr2v5joit8Af2Zp5P'
             '6yz3jMbycrLrRMpeAJxR8qDg8',
         ), PrivateKey)
     self.assertIsInstance(
         from_extended_key_string(
             ledger,
             'xpub661MyMwAqRbcF84AR8yfHoMzf4S2ct6mPJtvBtvNeyN9hBHuZ6uGJszkTSn5fQUCdz3XU17eBzFeAUwV6f'
             'iW44g14WF52fYC5J483wqQ5ZP',
         ), PubKey)
Ejemplo n.º 9
0
 async def test_private_key_derivation(self):
     private_key = PrivateKey(
         ledger_class({
             'db': ledger_class.database_class(':memory:'),
             'headers': ledger_class.headers_class(':memory:'),
         }),
         unhexlify(
             '2423f3dc6087d9683f73a684935abc0ccd8bc26370588f56653128c6a6f0bf7c'
         ), b'abcd' * 8, 0, 1)
     for i in range(20):
         new_privkey = private_key.child(i)
         self.assertIsInstance(new_privkey, PrivateKey)
         self.assertEqual(hexlify(new_privkey.private_key_bytes),
                          expected_privkeys[i])
     for i in range(PrivateKey.HARDENED + 1, private_key.HARDENED + 20):
         new_privkey = private_key.child(i)
         self.assertIsInstance(new_privkey, PrivateKey)
         self.assertEqual(
             hexlify(new_privkey.private_key_bytes),
             expected_hardened_privkeys[i - 1 - PrivateKey.HARDENED])
Ejemplo n.º 10
0
    def setUp(self):
        self.ledger = ledger_class({
            'db':
            ledger_class.database_class(':memory:'),
            'headers':
            ledger_class.headers_class(':memory:'),
        })
        yield self.ledger.db.open()
        self.account = self.ledger.account_class.from_dict(
            self.ledger, Wallet(), {
                "seed":
                "carbon smart garage balance margin twelve chest sword "
                "toast envelope bottom stomach absent"
            })

        addresses = yield self.account.ensure_address_gap()
        self.pubkey_hash = [
            self.ledger.address_to_hash160(a) for a in addresses
        ]
        self.hash_cycler = cycle(self.pubkey_hash)