コード例 #1
0
    def test_from_bare_checksig_scriptPubKey(self):
        def T(hex_scriptpubkey, expected_str_address):
            scriptPubKey = CScript(x(hex_scriptpubkey))
            # test that CBitcoinAddressError is raised, we do not support
            # bare checksig
            with self.assertRaises(CBitcoinAddressError):
                P2PKHBitcoinAddress.from_scriptPubKey(scriptPubKey)

        # compressed
        T(
            '21000000000000000000000000000000000000000000000000000000000000000000ac',
            '14p5cGy5DZmtNMQwTQiytBvxMVuTmFMSyU')

        # uncompressed
        T(
            '410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ac',
            '13VmALKHkCdSN1JULkP6RqW3LcbpWvgryV')

        # non-canonical encoding
        T(
            '4c21000000000000000000000000000000000000000000000000000000000000000000ac',
            '14p5cGy5DZmtNMQwTQiytBvxMVuTmFMSyU')

        # odd-lengths are *not* accepted
        with self.assertRaises(CBitcoinAddressError):
            P2PKHBitcoinAddress.from_scriptPubKey(
                CScript(
                    x('2200000000000000000000000000000000000000000000000000000000000000000000ac'
                      )))
コード例 #2
0
    def test_from_non_canonical_scriptPubKey(self) -> None:
        def T(hex_scriptpubkey: str, expected_str_address: str) -> None:
            scriptPubKey = CScript(x(hex_scriptpubkey))
            # now test that CBitcoinAddressError is raised, non-canonical
            # pushdata is not allowed
            with self.assertRaises(CBitcoinAddressError):
                P2PKHBitcoinAddress.from_scriptPubKey(scriptPubKey)

        T('76a94c14000000000000000000000000000000000000000088ac', '1111111111111111111114oLvT2')
        T('76a94d1400000000000000000000000000000000000000000088ac', '1111111111111111111114oLvT2')
        T('76a94e14000000000000000000000000000000000000000000000088ac', '1111111111111111111114oLvT2')

        # make sure invalid scripts raise CBitcoinAddressError
        with self.assertRaises(CBitcoinAddressError):
            P2PKHBitcoinAddress.from_scriptPubKey(CScript(x('76a94c14')))
コード例 #3
0
def VerifyMessage(address, message, sig):
    sig = base64.b64decode(sig)
    hash = message.GetHash()

    pubkey = CPubKey.recover_compact(hash, sig)

    return str(P2PKHBitcoinAddress.from_pubkey(pubkey)) == str(address)
コード例 #4
0
 def __init__(self, key: CBitcoinSecret, address_type: str = "p2pkh"):
     self.key_index = None
     self.type = address_type
     self.key = key
     if address_type == "p2pkh":
         self.address = P2PKHBitcoinAddress.from_pubkey(self.key.pub)
     elif address_type == "p2sh":
         self.address = P2SHBitcoinAddress.from_redeemScript(
             CScript([self.key.pub, OP_CHECKSIG]))
     elif address_type == "p2wpkh":
         key_hash = Hash160(self.key.pub)
         script_pub_key = CScript([OP_0, key_hash])
         self.address = P2WPKHBitcoinAddress.from_scriptPubKey(
             script_pub_key)
     elif address_type == "p2wsh":
         self.witness_program = CScript([self.key.pub, OP_CHECKSIG])
         script_hash = hashlib.sha256(self.witness_program).digest()
         script_pub_key = CScript([OP_0, script_hash])
         self.address = P2WSHBitcoinAddress.from_scriptPubKey(
             script_pub_key)
     else:
         raise UnsupportedAddressTypeError()
     self.value = 0
     self.txid = None
     self.vout = None
コード例 #5
0
def print_verbose(signature, key, msg):
    secret = CBitcoinSecret(key)
    address = P2PKHBitcoinAddress.from_pubkey(secret.pub)
    message = BitcoinMessage(msg)
    print('Address: %s' % address)
    print('Message: %s' % msg)
    print('Signature: %s' % signature)
    print('Verified: %s' % VerifyMessage(address, message, signature))
    print('\nTo verify using bitcoin core:')
    print('\n`bitcoin-cli verifymessage %s \'%s\' \'%s\'`\n' % (address, signature.decode('ascii'), msg))
コード例 #6
0
    def test_from_invalid_pubkeys(self):
        """Create P2PKHBitcoinAddress's from invalid pubkeys"""

        # first test with accept_invalid=True
        def T(invalid_pubkey, expected_str_addr):
            addr = P2PKHBitcoinAddress.from_pubkey(invalid_pubkey,
                                                   accept_invalid=True)
            self.assertEqual(str(addr), expected_str_addr)

        T(x(''), '1HT7xU2Ngenf7D4yocz2SAcnNLW7rK8d4E')
        T(
            x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c72'
              ), '1L9V4NXbNtZsLjrD3nkU7gtEYLWRBWXLiZ')

        # With accept_invalid=False we should get CBitcoinAddressError's
        with self.assertRaises(CBitcoinAddressError):
            P2PKHBitcoinAddress.from_pubkey(x(''))
        with self.assertRaises(CBitcoinAddressError):
            P2PKHBitcoinAddress.from_pubkey(
                x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c72'
                  ))
        with self.assertRaises(CBitcoinAddressError):
            P2PKHBitcoinAddress.from_pubkey(
                CPubKey(
                    x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c72'
                      )))
コード例 #7
0
# There's also a corresponding x() convenience function that takes big-endian
# hex and converts it to bytes.
txid = lx('7e195aa3de827814f172c362fcf838d92ba10e3f9fdd9c3ecaf79522b311b22d')
vout = 0

# Create the txin structure, which includes the outpoint. The scriptSig
# defaults to being empty.
txin = CMutableTxIn(COutPoint(txid, vout))

# We also need the scriptPubKey of the output we're spending because
# SignatureHash() replaces the transaction scriptSig's with it.
#
# Here we'll create that scriptPubKey from scratch using the pubkey that
# corresponds to the secret key we generated above.
txin_scriptPubKey = \
    P2PKHBitcoinAddress.from_pubkey(seckey.pub).to_scriptPubKey()

# Create the txout. This time we create the scriptPubKey from a Bitcoin
# address.
txout = CMutableTxOut(
    0.001 * COIN,
    CBitcoinAddress('1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8').to_scriptPubKey())

# Create the unsigned transaction.
tx = CMutableTransaction([txin], [txout])

# Calculate the signature hash for that transaction.
sighash = SignatureHash(txin_scriptPubKey, tx, 0, SIGHASH_ALL)

# Now sign it. We have to append the type of signature we want to the end, in
# this case the usual SIGHASH_ALL.
コード例 #8
0
 def T(invalid_pubkey: bytes, expected_str_addr: str) -> None:
     addr = P2PKHBitcoinAddress.from_pubkey(invalid_pubkey, accept_invalid=True)
     self.assertEqual(str(addr), expected_str_addr)
コード例 #9
0
 def T(pubkey: bytes, expected_str_addr: str) -> None:
     addr = P2PKHBitcoinAddress.from_pubkey(pubkey)
     self.assertEqual(str(addr), expected_str_addr)
コード例 #10
0
 def T(hex_scriptpubkey: str, expected_str_address: str) -> None:
     scriptPubKey = CScript(x(hex_scriptpubkey))
     # test that CBitcoinAddressError is raised, we do not support
     # bare checksig
     with self.assertRaises(CBitcoinAddressError):
         P2PKHBitcoinAddress.from_scriptPubKey(scriptPubKey)
コード例 #11
0
 def T(hex_scriptpubkey: str, expected_str_address: str) -> None:
     scriptPubKey = CScript(x(hex_scriptpubkey))
     # now test that CBitcoinAddressError is raised, non-canonical
     # pushdata is not allowed
     with self.assertRaises(CBitcoinAddressError):
         P2PKHBitcoinAddress.from_scriptPubKey(scriptPubKey)
コード例 #12
0
 def T(pubkey, expected_str_addr):
     addr = P2PKHBitcoinAddress.from_pubkey(pubkey)
     self.assertEqual(str(addr), expected_str_addr)