Ejemplo n.º 1
0
    def test_blind(self):
        from bitcoincash.wallet import CBitcoinSecret
        # signer
        privkey = secrets.token_bytes(32)
        pubkey = CBitcoinSecret.from_secret_bytes(privkey).pub
        signer = schnorr.BlindSigner()
        R = signer.get_R()

        # requester
        message_hash = secrets.token_bytes(32)
        requester = schnorr.BlindSignatureRequest(pubkey, R, message_hash)

        # create and send request
        e_request = requester.get_request()
        s_response = signer.sign(privkey, e_request)

        # finalize and unblind the signature
        signature = requester.finalize(s_response)
        self.assertTrue(schnorr.verify(pubkey, signature, message_hash))

        # try bastardizing the s response by adding 1 to last byte
        s_bad = bytearray(s_response)
        s_bad[-1] = (s_bad[-1] + 1) % 256
        with self.assertRaises(RuntimeError):
            signature = requester.finalize(s_bad)
Ejemplo n.º 2
0
    def test_sign_message_simple(self):
        key = CBitcoinSecret(
            "L4vB5fomsK8L95wQ7GFzvErYGht49JsCPJyJMHpB4xGM6xgi2jvG")
        address = "1F26pNMrywyZJdr22jErtKcjF8R3Ttt55G"
        message = address

        message = BitcoinMessage(message)
        signature = SignMessage(key, message)

        self.assertTrue(signature)
        self.assertTrue(VerifyMessage(tocashaddr(address), message, signature))
Ejemplo n.º 3
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))
Ejemplo n.º 4
0
    def test_sign_message_vectors(self):
        for vector in load_test_vectors('signmessage.json'):
            key = CBitcoinSecret(vector['wif'])
            message = BitcoinMessage(vector['address'])

            signature = SignMessage(key, message)

            self.assertTrue(signature,
                            "Failed to sign for [%s]" % vector['address'])
            self.assertTrue(
                VerifyMessage(tocashaddr(vector['address']), message,
                              vector['signature']),
                "Failed to verify signature for [%s]" % vector['address'])
Ejemplo n.º 5
0
def make_address_from_passphrase(passphrase, compressed=True, as_str=True):
    """
    Create a Bitcoin address from a passphrase. The passphrase is hashed and
    then used as the secret bytes to construct the CBitcoinSecret.
    """
    if not isinstance(passphrase, bytes):
        passphrase = bytes(passphrase, "utf-8")
    passphrasehash = hashlib.sha256(passphrase).digest()
    private_key = CBitcoinSecret.from_secret_bytes(passphrasehash, compressed=compressed)
    address = P2PKHBitcoinAddress.from_pubkey(private_key.pub)
    if as_str:
        return str(address)
    else:
        return address
Ejemplo n.º 6
0
    def dumpprivkey(self, addr):
        """Return the private key matching an address
        """
        r = self._call('dumpprivkey', str(addr))

        return CBitcoinSecret(r)
Ejemplo n.º 7
0
def sign_message(key, msg):
    secret = CBitcoinSecret(key)
    message = BitcoinMessage(msg)
    return SignMessage(secret, message)
Ejemplo n.º 8
0
    sys.stderr.write('Sorry, Python 3.x required by this example.\n')
    sys.exit(1)

import hashlib

from bitcoincash import SelectParams
from bitcoincash.core import b2x, lx, COIN, COutPoint, CMutableTxOut, CMutableTxIn, CMutableTransaction, Hash160
from bitcoincash.core.script import CScript, OP_DUP, OP_HASH160, OP_EQUALVERIFY, OP_CHECKSIG, SignatureHash, SIGHASH_ALL
from bitcoincash.core.scripteval import VerifyScript, SCRIPT_VERIFY_P2SH
from bitcoincash.wallet import CBitcoinAddress, CBitcoinSecret

SelectParams('mainnet')

# Create the (in)famous correct brainwallet secret key.
h = hashlib.sha256(b'correct horse battery staple').digest()
seckey = CBitcoinSecret.from_secret_bytes(h)

# Create a redeemScript. Similar to a scriptPubKey the redeemScript must be
# satisfied for the funds to be spent.
txin_redeemScript = CScript([seckey.pub, OP_CHECKSIG])
print(b2x(txin_redeemScript))

# Create the magic P2SH scriptPubKey format from that redeemScript. You should
# look at the CScript.to_p2sh_scriptPubKey() function in bitcoincash.core.script to
# understand what's happening, as well as read BIP16:
# https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki
txin_scriptPubKey = txin_redeemScript.to_p2sh_scriptPubKey()

# Convert the P2SH scriptPubKey to a base58 Bitcoin address and print it.
# You'll need to send some funds to it to create a txout to spend.
txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey)