當前位置: 首頁>>代碼示例>>Python>>正文


Python VerifyingKey.from_string方法代碼示例

本文整理匯總了Python中ecdsa.VerifyingKey.from_string方法的典型用法代碼示例。如果您正苦於以下問題:Python VerifyingKey.from_string方法的具體用法?Python VerifyingKey.from_string怎麽用?Python VerifyingKey.from_string使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在ecdsa.VerifyingKey的用法示例。


在下文中一共展示了VerifyingKey.from_string方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: from_parent

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def from_parent(parent_key, i):
        if i & HARDENED_INDEX:
            raise ValueError("Can't generate a hardened child key from a parent public key.")

        child = hmac.new(parent_key.chain_code,
                         parent_key.compressed_key + i.to_bytes(length=4, byteorder='big'),
                         hashlib.sha512).digest()
        child_left, child_right = child[:32], child[32:]
        if int.from_bytes(child_left, 'big') >= ecdsa.generator_256.order():
            return None

        temp_pri_key = SigningKey.from_string(string=child_left, curve=curves.NIST256p)

        ki = temp_pri_key.verifying_key.pubkey.point + parent_key.key.pubkey.point
        if ki == ellipticcurve.INFINITY:
            return None

        return HDPublicKey(public_key=VerifyingKey.from_public_point(point=ki, curve=curves.NIST256p),
                           chain_code=child_right,
                           index=i,
                           depth=parent_key.depth + 1,
                           parent_fingerprint=parent_key.fingerprint) 
開發者ID:ontio,項目名稱:ontology-python-sdk,代碼行數:24,代碼來源:hd_public_key.py

示例2: ec_mult

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def ec_mult(self, his_pubkey):
        # - second call: given the pubkey of far side, calculate the shared pt on curve
        # - creates session key based on that
        from ecdsa.curves import SECP256k1
        from ecdsa import VerifyingKey
        from ecdsa.util import number_to_string

        # Validate his pubkey a little: this call will check it's on the curve.
        assert len(his_pubkey) == 64
        his_pubkey = VerifyingKey.from_string(his_pubkey, curve=SECP256k1, hashfunc=sha256)

        #print("his pubkey = %s" % b2a_hex(his_pubkey.to_string()))

        # do the D-H thing
        pt = self.my_key.privkey.secret_multiplier * his_pubkey.pubkey.point

        # final key is sha256 of that point, serialized (64 bytes).
        order = SECP256k1.order
        kk = number_to_string(pt.x(), order) + number_to_string(pt.y(), order)

        del self.my_key

        return sha256(kk).digest() 
開發者ID:bitcoin-core,項目名稱:HWI,代碼行數:25,代碼來源:client.py

示例3: mitm_verify

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def mitm_verify(self, sig, expected_xpub):
        # First try with Pycoin
        try:
            from pycoin.key.BIP32Node import BIP32Node
            from pycoin.contrib.msg_signing import verify_message
            from pycoin.encoding  import from_bytes_32
            from base64 import b64encode

            mk = BIP32Node.from_wallet_key(expected_xpub)
            return verify_message(mk, b64encode(sig), msg_hash=from_bytes_32(self.session_key))
        except ImportError:
            pass

        # If Pycoin is not available, do it using ecdsa
        from ecdsa import BadSignatureError, SECP256k1, VerifyingKey
        pubkey, chaincode = decode_xpub(expected_xpub)
        vk = VerifyingKey.from_string(get_pubkey_string(pubkey), curve=SECP256k1)
        try:
            ok = vk.verify_digest(sig[1:], self.session_key)
        except BadSignatureError:
            ok = False

        return ok 
開發者ID:bitcoin-core,項目名稱:HWI,代碼行數:25,代碼來源:client.py

示例4: validate_tx_in

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def validate_tx_in(tx_in, transaction, a_unspent_tx_outs):

    referenced_utxo = [utxo for utxo in a_unspent_tx_outs if utxo.tx_out_id == tx_in.tx_out_id and utxo.tx_out_index == tx_in.tx_out_index][0]
    if referenced_utxo == []:
        print('referenced txOut not found: ' + json.dumps(tx_in))
        return False

    address = referenced_utxo.address

    vk = VerifyingKey.from_string(bytes.fromhex(address), curve=SECP256k1)

    try:
        vk.verify(bytes.fromhex(tx_in.signature), transaction.id.encode())

    except Exception as e:
        # change the exception
        print('invalid tx_in signature: %s txId: %s address: %s' % (tx_in.signature, transaction.id, referenced_utxo.address))
        return False

    return True 
開發者ID:PacktPublishing,項目名稱:Foundations-of-Blockchain,代碼行數:22,代碼來源:transaction.py

示例5: sign_tx_in

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def sign_tx_in(transaction, tx_in_index,
               private_key, a_unspent_tx_outs):

    tx_in = transaction.tx_ins[tx_in_index]
    data_to_sign = str(transaction.id)
    referenced_utxo = find_unspent_tx_out(tx_in.tx_out_id, tx_in.tx_out_index, a_unspent_tx_outs)
    if referenced_utxo is None:
        print('could not find referenced txOut')
        # throw Error()
        return False

    referenced_address = referenced_utxo.address

    if get_public_key(private_key) != referenced_address:
        print('trying to sign an input with private' +
              ' key that does not match the address that is referenced in tx_in')
        # throw Error()
        return False

    # key = ec.keyFromPrivate(private_key, 'hex')
    sk = SigningKey.from_string(private_key, curve=SECP256k1)
    signature = binascii.b2a_hex(sk.sign(data_to_sign.encode())).decode()
    return signature 
開發者ID:PacktPublishing,項目名稱:Foundations-of-Blockchain,代碼行數:25,代碼來源:transaction.py

示例6: get_child

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def get_child(self, index, hardened=False):
        left, right = self.get_hash(index, hardened)
        point = ((left * generator_secp256k1)
                 + VerifyingKey.from_string(self.key.uncompressed[1:], curve=SECP256k1).pubkey.point)
        if point == INFINITY:
            raise ValueError('Computed point equals INFINITY')
        return ExtendedPublicKey(PublicKey.from_point(point), right, self.depth+1, self.get_fingerprint(), index, False) 
開發者ID:chainside,項目名稱:btcpy,代碼行數:9,代碼來源:hd.py

示例7: from_bytes

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def from_bytes(cls, data: bytes):
        """ Generates either a HDPublicKey from the underlying bytes.

        The serialization must conform to the description in:
        https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format
        """
        if len(data) < 78:
            raise ValueError("b must be at least 78 bytes long.")

        version = int.from_bytes(data[:4], 'big')
        depth = data[4]
        parent_fingerprint = data[5:9]
        index = int.from_bytes(data[9:13], 'big')
        chain_code = data[13:45]
        key_bytes = data[45:78]

        if version != HDPublicKey.__VERSION:
            raise ValueError('invalid HD Public Key.')

        if key_bytes[0] != 0x02 and key_bytes[0] != 0x03:
            raise ValueError("First byte of public key must be 0x02 or 0x03!")

        # The curve of points satisfying y^2 = x^3 + a*x + b (mod p).
        curve = ecdsa.curve_256
        x = util.string_to_number(key_bytes[1:])
        y = (x * x * x + curve.a() * x + curve.b()) % curve.p()
        y = numbertheory.square_root_mod_prime(y, curve.p())
        if (key_bytes[0] == 0x03 and y % 2 == 0) or (key_bytes[0] == 0x02 and y % 2 != 0):
            y = (y * -1) % curve.p()
        order = curves.NIST256p.order
        s_key = util.number_to_string(x, order) + util.number_to_string(y, order)

        public_key = VerifyingKey.from_string(string=s_key, curve=curves.NIST256p)
        rv = cls(
            public_key=public_key,
            chain_code=chain_code,
            index=index,
            depth=depth,
            parent_fingerprint=parent_fingerprint)
        return rv 
開發者ID:ontio,項目名稱:ontology-python-sdk,代碼行數:42,代碼來源:hd_public_key.py

示例8: get_compressed_pk

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def get_compressed_pk(pk):
    """
    Constructs the compressed representation of a SECP256k1 ECDSA public key form a given uncompressed key.

    :param pk: The uncompressed SECP256k1 key to be decompressed.
    :type pk: hex
    :return: The compressed SECP256k1 ECDSA key.
    :rtype: hex
    """

    ecdsa_pk = VerifyingKey.from_string(unhexlify(pk[2:]), curve=SECP256k1)
    compressed_pk = serialize_pk(ecdsa_pk)

    return compressed_pk 
開發者ID:sr-gi,項目名稱:bitcoin_tools,代碼行數:16,代碼來源:keys.py

示例9: hex2key

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def hex2key(hex_key):
        key_bytes = unhexlify(hex_key)
        if len(hex_key) == 64:
            return SigningKey.from_string(key_bytes, curve=NIST256p,
                    hashfunc=sha256)
        elif len(hex_key) == 128:
            return VerifyingKey.from_string(key_bytes, curve=NIST256p,
                    hashfunc=sha256)
        else:
            raise ValueError("Key in hex form is of the wrong length.") 
開發者ID:paxswill,項目名稱:evesrp,代碼行數:12,代碼來源:bravecore.py

示例10: get_public_key

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def get_public_key(private_key):

    sk = SigningKey.from_string(private_key
                                , curve=SECP256k1)
    vk = sk.get_verifying_key()
    return binascii.b2a_hex(vk.to_string()).decode() 
開發者ID:PacktPublishing,項目名稱:Foundations-of-Blockchain,代碼行數:8,代碼來源:transaction.py

示例11: verify

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def verify(self):

        vk = VerifyingKey.from_string(bytes.fromhex(self.public_key), curve=SECP256k1)

        try:
            vk.verify(bytes.fromhex(self.signature), SHA256.new(str(self.id).encode()).digest())

        except keys.BadSignatureError:
            print('invalid transaction signature')
            return False

        return True 
開發者ID:PacktPublishing,項目名稱:Foundations-of-Blockchain,代碼行數:14,代碼來源:digital_signature_example.py

示例12: sign

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def sign(self, private_key):

        data_to_sign = SHA256.new(str(self.id).encode()).digest()

        sk = SigningKey.from_string(bytes.fromhex(private_key), curve=SECP256k1)
        self.signature = binascii.b2a_hex(sk.sign(data_to_sign)).decode() 
開發者ID:PacktPublishing,項目名稱:Foundations-of-Blockchain,代碼行數:8,代碼來源:digital_signature_example.py

示例13: _from_wif

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def _from_wif(self, wif):
        """Creates key from WIFC or WIF format key

        Check to_wif for the detailed process. From WIF is the reverse.

        Raises
        ------
        ValueError
            if the checksum is wrong or if the WIF/WIFC is not from the
            configured network.
        """

        wif_utf = wif.encode('utf-8')

        # decode base58check get key bytes plus checksum
        data_bytes = b58decode( wif_utf )
        key_bytes = data_bytes[:-4]
        checksum = data_bytes[-4:]

        # verify key with checksum
        data_hash = hashlib.sha256(hashlib.sha256(key_bytes).digest()).digest()
        if not checksum == data_hash[0:4]:
            raise ValueError('Checksum is wrong. Possible mistype?')

        # get network prefix and check with current setup
        network_prefix = key_bytes[:1]
        if NETWORK_WIF_PREFIXES[get_network()] != network_prefix:
            raise ValueError('Using the wrong network!')

        # remove network prefix
        key_bytes = key_bytes[1:]

        # check length of bytes and if > 32 then compressed
        # use this to instantite an ecdsa key
        if len(key_bytes) > 32:
            self.key = SigningKey.from_string(key_bytes[:-1], curve=SECP256k1)
        else:
            self.key = SigningKey.from_string(key_bytes, curve=SECP256k1) 
開發者ID:karask,項目名稱:python-bitcoin-utils,代碼行數:40,代碼來源:keys.py

示例14: VerifySignature

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def VerifySignature(message, signature, public_key, unhex=True):
        """
        Verify the integrity of the message.

        Args:
            message (hexstr or str): the message to verify.
            signature (bytearray): the signature belonging to the message.
            public_key (ECPoint|bytes): the public key to use for verifying the signature. If `public_key` is of type bytes then it should be raw bytes (i.e. b'\xAA\xBB').
            unhex (bool): whether the message should be unhexlified before verifying

        Returns:
            bool: True if verification passes. False otherwise.
        """

        if type(public_key) is EllipticCurve.ECPoint:
            pubkey_x = public_key.x.value.to_bytes(32, 'big')
            pubkey_y = public_key.y.value.to_bytes(32, 'big')

            public_key = pubkey_x + pubkey_y

        if unhex:
            try:
                message = binascii.unhexlify(message)
            except binascii.Error:
                pass
        elif isinstance(message, str):
            message = message.encode('utf-8')

        if len(public_key) == 33:
            public_key = bitcoin.decompress(public_key)
            public_key = public_key[1:]

        try:
            vk = VerifyingKey.from_string(public_key, curve=NIST256p, hashfunc=hashlib.sha256)
            res = vk.verify(signature, message, hashfunc=hashlib.sha256)
            return res
        except Exception:
            pass

        return False 
開發者ID:CityOfZion,項目名稱:neo-python,代碼行數:42,代碼來源:Crypto.py

示例15: sign

# 需要導入模塊: from ecdsa import VerifyingKey [as 別名]
# 或者: from ecdsa.VerifyingKey import from_string [as 別名]
def sign(msg, privkey):
    from ecdsa import SigningKey
    if isinstance(privkey, bytes):
        privkey = SigningKey.from_string(privkey)
    return privkey.sign(msg) 
開發者ID:halilozercan,項目名稱:halocoin,代碼行數:7,代碼來源:tools.py


注:本文中的ecdsa.VerifyingKey.from_string方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。