Пример #1
0
def execute(args: Namespace, wallet_passwd: str) -> None:
    from hathor.crypto.util import get_private_key_bytes, get_public_key_bytes_compressed
    from hathor.wallet import Wallet
    from hathor.wallet.util import generate_multisig_address, generate_multisig_redeem_script

    if (args.pubkey_count
            and args.pubkey_count > 16) or args.signatures_required > 16:
        print(
            'Error: maximum number of public keys or signatures required is 16'
        )
        return

    if not args.pubkey_count and not args.public_keys:
        print('Error: you must give at least pubkey_count or public_keys')
        return

    if args.dir:
        wallet = Wallet(directory=args.dir)
    else:
        wallet = Wallet()

    wallet.unlock(wallet_passwd.encode())

    if args.public_keys:
        public_keys_hex = args.public_keys.split(',')
        public_bytes = [bytes.fromhex(pkh) for pkh in public_keys_hex]
    else:
        # If not public keys as parameter, we need to create them
        public_bytes = []

        for i in range(args.pubkey_count):
            addr = wallet.get_unused_address()
            key = wallet.keys[addr]
            pk = key.get_private_key(wallet_passwd.encode())
            public_key_bytes = get_public_key_bytes_compressed(pk.public_key())
            public_bytes.append(public_key_bytes)
            print('------------------\n')
            print('Key {}\n'.format(i + 1))
            print('Private key: {}\n'.format(
                get_private_key_bytes(
                    pk,
                    encryption_algorithm=serialization.BestAvailableEncryption(
                        wallet_passwd.encode())).hex()))
            print('Public key: {}\n'.format(public_key_bytes.hex()))
            print('Address: {}\n'.format(addr))

    # Then we create the redeem script
    redeem_script = generate_multisig_redeem_script(args.signatures_required,
                                                    public_bytes)

    print('------------------\n')
    print('Redeem script:', redeem_script.hex())
    print('\n')

    # Then we created the multisig address
    address = generate_multisig_address(redeem_script)

    print('------------------\n')
    print('MultiSig address:', address)
    print('------------------\n\n')
Пример #2
0
    def create(cls, password: Optional[bytes]) -> 'KeyPair':
        """
        :raises WalletLocked: wallet password was not provided
        """
        if not password:
            raise WalletLocked

        new_key = ec.generate_private_key(ec.SECP256K1(), default_backend())
        private_key_bytes = get_private_key_bytes(new_key,
                                                  encryption_algorithm=serialization.BestAvailableEncryption(password))
        address = get_address_b58_from_public_key(new_key.public_key())
        return cls(private_key_bytes=private_key_bytes, address=address, used=False)
Пример #3
0
def main():
    from hathor.cli.util import create_parser
    from hathor.crypto.util import get_hash160, get_private_key_bytes, get_public_key_bytes_compressed

    parser = create_parser()

    parser.add_argument('filepath', help='Create a new private key in the given file')
    args = parser.parse_args()

    new_key = ec.generate_private_key(ec.SECP256K1(), default_backend())
    private_key_bytes = get_private_key_bytes(new_key)
    with open(args.filepath, 'w') as key_file:
        key_file.write(base64.b64encode(private_key_bytes).decode('utf-8'))
        print('key created!')
    public_key_bytes = get_public_key_bytes_compressed(new_key.public_key())
    print('base64 pubkey hash:', base64.b64encode(get_hash160(public_key_bytes)).decode('utf-8'))
Пример #4
0
    def test_wallet_create_transaction(self):
        genesis_private_key_bytes = get_private_key_bytes(
            self.genesis_private_key,
            encryption_algorithm=serialization.BestAvailableEncryption(
                PASSWORD))
        genesis_address = get_address_b58_from_public_key(
            self.genesis_public_key)
        # create wallet with genesis block key
        key_pair = KeyPair(private_key_bytes=genesis_private_key_bytes,
                           address=genesis_address,
                           used=True)
        keys = {}
        keys[key_pair.address] = key_pair
        w = Wallet(keys=keys, directory=self.directory)
        w.unlock(PASSWORD)
        genesis_blocks = [
            tx for tx in get_genesis_transactions(None) if tx.is_block
        ]
        genesis_block = genesis_blocks[0]
        genesis_value = sum([output.value for output in genesis_block.outputs])

        # wallet will receive genesis block and store in unspent_tx
        w.on_new_tx(genesis_block)
        for index in range(len(genesis_block.outputs)):
            utxo = w.unspent_txs[settings.HATHOR_TOKEN_UID].get(
                (genesis_block.hash, index))
            self.assertIsNotNone(utxo)
        self.assertEqual(w.balance[settings.HATHOR_TOKEN_UID],
                         WalletBalance(0, genesis_value))

        # create transaction spending this value, but sending to same wallet
        new_address = w.get_unused_address()
        out = WalletOutputInfo(decode_address(new_address), 100, timelock=None)
        tx1 = w.prepare_transaction_compute_inputs(Transaction, outputs=[out])
        tx1.storage = self.storage
        tx1.update_hash()
        self.storage.save_transaction(tx1)
        w.on_new_tx(tx1)
        self.assertEqual(len(w.spent_txs), 1)
        self.assertEqual(w.balance[settings.HATHOR_TOKEN_UID],
                         WalletBalance(0, genesis_value))

        # pass inputs and outputs to prepare_transaction, but not the input keys
        # spend output last transaction
        input_info = WalletInputInfo(tx1.hash, 1, None)
        new_address = w.get_unused_address()
        key2 = w.keys[new_address]
        out = WalletOutputInfo(decode_address(key2.address),
                               100,
                               timelock=None)
        tx2 = w.prepare_transaction_incomplete_inputs(Transaction,
                                                      inputs=[input_info],
                                                      outputs=[out],
                                                      tx_storage=self.storage)
        tx2.storage = self.storage
        tx2.update_hash()
        self.storage.save_transaction(tx2)
        w.on_new_tx(tx2)
        self.assertEqual(len(w.spent_txs), 2)
        self.assertEqual(w.balance[settings.HATHOR_TOKEN_UID],
                         WalletBalance(0, genesis_value))

        # test keypair exception
        with self.assertRaises(WalletLocked):
            key_pair.get_private_key(None)
Пример #5
0
 def test_privkey_serialization(self):
     private_key_bytes = get_private_key_bytes(self.private_key)
     self.assertEqual(
         self.private_key.private_numbers(),
         get_private_key_from_bytes(private_key_bytes).private_numbers())