コード例 #1
0
def pubkey_to_p2pkh_script(pub, require_compressed=False):
    """
    Given a pubkey in bytes, return a CScript
    representing the corresponding pay-to-pubkey-hash
    scriptPubKey.
    """
    return P2PKHCoinAddress.from_pubkey(pub).to_scriptPubKey()
コード例 #2
0
ファイル: test_wallet.py プロジェクト: wiz/python-elementstx
def get_unconfidential_address_samples(pub1, pub2):
    return AddressSamples(
        p2pkh=P2PKHCoinAddress.from_pubkey(pub1),
        p2wpkh=P2WPKHCoinAddress.from_pubkey(pub1),
        p2sh=P2SHCoinAddress.from_redeemScript(
            CScript(b'\xa9' + Hash160(pub1) + b'\x87')),
        p2wsh=P2WSHCoinAddress.from_redeemScript(
            CScript(b'\xa9' + Hash160(pub1) + b'\x87')),
        conf_p2pkh=P2PKHCoinConfidentialAddress.from_unconfidential(
            P2PKHCoinAddress.from_pubkey(pub1), pub2),
        conf_p2wpkh=P2WPKHCoinConfidentialAddress.from_unconfidential(
            P2WPKHCoinAddress.from_pubkey(pub1), pub2),
        conf_p2sh=P2SHCoinConfidentialAddress.from_unconfidential(
            P2SHCoinAddress.from_redeemScript(
                CScript(b'\xa9' + Hash160(pub1) + b'\x87')), pub2),
        conf_p2wsh=P2WSHCoinConfidentialAddress.from_unconfidential(
            P2WSHCoinAddress.from_redeemScript(
                CScript(b'\xa9' + Hash160(pub1) + b'\x87')), pub2))
コード例 #3
0
 async def async_mainnet() -> None:
     select_chain_params('bitcoin/mainnet')
     await wait_async('testnet')
     a = P2PKHCoinAddress.from_pubkey(pub)
     assert CBase58Data(str(a))[0] == 0
     check_core_modules()
     ready('mainnet')
     finish('mainnet')
     self.assertEqual(get_current_chain_params().NAME, 'bitcoin')
コード例 #4
0
 def derive_address(self):
     bip_keypath = HDKeyPath(self.key_path[:BIP_KEYPATH_INDEX])
     if bip_keypath == bip_44_keypath():
         self._address = P2PKHAddress.from_pubkey(self.pub_key)
     elif bip_keypath == bip_84_keypath():
         self._address = P2WPKHAddress.from_pubkey(self.pub_key)
     elif bip_keypath == bip_49_keypath():
         self._address = WalletAddress.make_wrapped_segwit_address(
             self.pub_key)
     else:
         raise Exception
コード例 #5
0
def print_verbose(signature, key, msg):
    secret = CCoinKey(key)
    address = P2PKHCoinAddress.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_sighash(self) -> None:
        spent_amount = 1100
        pub = CKey.from_secret_bytes(os.urandom(32)).pub
        spk_legacy = P2PKHCoinAddress.from_pubkey(pub).to_scriptPubKey()
        spk_segwit = P2WPKHCoinAddress.from_pubkey(pub).to_scriptPubKey()

        tx = CTransaction([
            CTxIn(
                COutPoint(b'\x00' * 32, 0), CScript([]), nSequence=0xFFFFFFFF)
        ], [CTxOut(1000, spk_legacy)],
                          nLockTime=0,
                          nVersion=1)

        # no exceptions should be raised with these two calls
        spk_legacy.sighash(tx,
                           0,
                           SIGHASH_ALL,
                           amount=spent_amount,
                           sigversion=SIGVERSION_WITNESS_V0)
        spk_segwit.sighash(tx,
                           0,
                           SIGHASH_ALL,
                           amount=spent_amount,
                           sigversion=SIGVERSION_WITNESS_V0)

        with self.assertRaises(ValueError):
            # unknown sigversion
            spk_segwit.sighash(tx,
                               0,
                               SIGHASH_ALL,
                               amount=spent_amount,
                               sigversion=SIGVERSION_WITNESS_V0 +
                               1)  # type: ignore

        assert spk_segwit.is_witness_scriptpubkey()
        with self.assertRaises(ValueError):
            # incorect sigversion for segwit
            spk_segwit.sighash(tx,
                               0,
                               SIGHASH_ALL,
                               amount=spent_amount,
                               sigversion=SIGVERSION_BASE)

        with self.assertRaises(ValueError):
            # inIdx > len(tx.vin) - non-raw sighash function should raise
            # ValueError (raw_sighash can return HASH_ONE)
            spk_legacy.sighash(tx,
                               10,
                               SIGHASH_ALL,
                               amount=spent_amount,
                               sigversion=SIGVERSION_BASE)
コード例 #7
0
 def test_get_output_size(self) -> None:
     pub = CPubKey(x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71'))
     a0 = P2PKHCoinAddress.from_pubkey(pub)
     self.assertEqual(P2PKHCoinAddress.get_output_size(), 34)
     self.assertEqual(a0.get_output_size(), 34)
     a1 = P2WPKHCoinAddress.from_pubkey(pub)
     self.assertEqual(P2WPKHCoinAddress.get_output_size(), 31)
     self.assertEqual(a1.get_output_size(), 31)
     a2 = P2SHCoinAddress.from_redeemScript(
         CScript(b'\xa9' + Hash160(pub) + b'\x87'))
     self.assertEqual(P2SHCoinAddress.get_output_size(), 32)
     self.assertEqual(a2.get_output_size(), 32)
     a3 = P2WSHCoinAddress.from_redeemScript(
         CScript(b'\xa9' + Hash160(pub) + b'\x87'))
     self.assertEqual(P2WSHCoinAddress.get_output_size(), 43)
     self.assertEqual(a3.get_output_size(), 43)
コード例 #8
0
def VerifyMessage(address: P2PKHCoinAddress,
                  message: 'BitcoinMessage',
                  sig: Union[str, bytes],
                  validate_base64: bool = True) -> bool:

    if isinstance(sig, bytes):
        sig_b64 = sig.decode('ascii')
    else:
        sig_b64 = sig

    sig_bytes = base64.b64decode(sig_b64, validate=validate_base64)
    hash = message.GetHash()

    pubkey = CPubKey.recover_compact(hash, sig_bytes)

    if pubkey is None:
        return False

    return str(P2PKHCoinAddress.from_pubkey(pubkey)) == str(address)