def _validate_user(self, restrict=False):
        # validate user incase of update

        self.key_file = self._get_key_file()
        pub_key_file = self._get_pub_key_file()
        if os.path.isfile(pub_key_file):
            try:
                with open(pub_key_file) as fd:
                        self.pub_key_str = fd.read().strip()
            except OSError:
                    raise Exception(
                        'Failed to read public key {key} of user {user}'.format(key=pub_key_file, user=self.user)
                    )
        else:
            raise Exception('Unable to find public kye {key} of user {user}'.format(key=pub_key_file, user=self.user))

        username, tag, priv_key = self._get_user_from_block_chain()
        private_key = Secp256k1PrivateKey.from_hex(priv_key)
        public_key = Secp256k1PublicKey(private_key.secp256k1_private_key.pubkey)

        if self.pub_key_str == public_key.as_hex():
            import pdb;pdb.set_trace()
            if restrict and tag != 'admin':
                raise Exception('Only Admin type users are allowed to perform these operations')
            print('Validation successful')
            return

        raise Exception(
            'Should have correct public keyfile {file} to create user'.format(file=pub_key_file))
Beispiel #2
0
    def generate_key_pair():
        """
        Generate public and private key pair.

        Returns:
            Generated key pair in bytes.
        """
        private_key_obj = Secp256k1PrivateKey.new_random()
        public_key_obj = Secp256k1PublicKey(
            private_key_obj.secp256k1_private_key.pubkey)

        return private_key_obj.as_bytes(), public_key_obj.as_bytes()
Beispiel #3
0
    def _public_key_bytes_to_object(public_key):
        """
        Public key bytes to object.

        Args:
            public_key (bytes): secp256k1 public key in bytes

        Returns:
            public key object
        """
        return Secp256k1PublicKey(
            secp256k1.PublicKey(public_key, raw=True, ctx=__CTX__))
Beispiel #4
0
    def __init__(self, private_key=None, public_key=None):
        """
        Constructor for ``ECDSA`` key pair. If only private key available then public key will be generate from private.

        Args:
            private_key (bytes): secp256k1 private key
            public_key (bytes, optional): secp256k1 public key
        """
        if private_key and public_key:
            self._private_key = private_key
            self._public_key = public_key

            self._private_key_obj = self._private_key_bytes_to_object(
                private_key=self._private_key)
            self._public_key_obj = self._public_key_bytes_to_object(
                public_key=self._public_key)

        elif private_key:
            self._private_key = private_key

            self._private_key_obj = self._private_key_bytes_to_object(
                private_key=self._private_key)
            self._public_key_obj = Secp256k1PublicKey(
                self._private_key_obj.secp256k1_private_key.pubkey)

            self._public_key = self._public_key_obj.as_bytes()

        elif public_key:
            self._public_key = public_key
            self._public_key_obj = self._public_key_bytes_to_object(
                public_key=self._public_key)

        if private_key:
            self._private_key_hex = self._private_key.hex()
        else:
            self._private_key_hex = None

        self._public_key_hex = self._public_key.hex()

        self._address = generate_address(RemmeFamilyName.PUBLIC_KEY.value,
                                         self._public_key)
        self._key_type = KeyType.ECDSA

        super(ECDSA, self).__init__(
            private_key=private_key if private_key else None,
            public_key=self._public_key,
            private_key_hex=self._private_key_hex,
            public_key_hex=self._public_key_hex,
            key_type=self._key_type,
            address=self._address,
        )
    def __init__(self, base_url, keyfile=None):

        self._base_url = base_url
        if keyfile is None:
            self._signer = None
            return

        try:
            with open(keyfile) as fd:
                private_key_str = fd.read().strip()
        except OSError as err:
            raise Exception('Failed to read private key {}: {}'.format(keyfile, str(err)))

        try:
            self.private_key = Secp256k1PrivateKey.from_hex(private_key_str)
            self.public_key = Secp256k1PublicKey(self.private_key.secp256k1_private_key.pubkey)
        except ParseError as e:
            raise Exception('Unable to load private key: {}'.format(str(e)))

        self._signer = CryptoFactory(create_context('secp256k1')).new_signer(self.private_key)