예제 #1
0
def genAddress():
    # Initialise bitcoin.py
    setup('mainnet')

    priv = PrivateKey()
    wif = priv.to_wif(compressed=True)

    return wif
def main():
    # always remember to setup the network
    setup('mainnet')

    # create a private key (deterministically)
    priv = PrivateKey(secret_exponent=1)

    # compressed is the default
    print("\nPrivate key WIF:", priv.to_wif(compressed=True))

    # could also instantiate from existing WIF key
    #priv = PrivateKey.from_wif('KwDiBf89qGgbjEhKnhxjUh7LrciVRzI3qYjgd9m7Rfu73SvHnOwn')

    # get the public key
    pub = priv.get_public_key()

    # compressed is the default
    print("Public key:", pub.to_hex(compressed=True))

    # get address from public key
    address = pub.get_address()

    # print the address and hash160 - default is compressed address
    print("Address:", address.to_string())
    print("Hash160:", address.to_hash160())

    print("\n--------------------------------------\n")

    # sign a message with the private key and verify it
    message = "The test!"
    signature = priv.sign_message(message)
    print("The message to sign:", message)
    print("The signature is:", signature)

    if PublicKey.verify_message(address.to_string(), signature, message):
        print("The signature is valid!")
    else:
        print("The signature is NOT valid!")
    def tx_encf(self, vk_p, ct_c, user_conf_file_path):
        user_conf_file = open(user_conf_file_path)
        conf = json.load(user_conf_file)
        self.challenge_len = conf['CHA_LEN']
        self.response_len = conf['RES_LEN']

        #--------------------import the private key of the user to wallet and fund it----------------
        sk_u = conf['SK']
        sk_u = PrivateKey(secret_exponent=int(sk_u, 16))
        cmd = os.popen(zcash_cli + " " + conf_file + ' importprivkey ' +
                       sk_u.to_wif() + ' \"label\" false')
        addr_u = cmd.read()
        print("\nuser's address: ", addr_u)

        # ---------------------setup a change shielded address for the user----------------------------
        cmd = os.popen(zcash_cli + " " + conf_file +
                       ' z_getnewaddress ')  # create a new shielded address
        self.public_address_user_shielded = cmd.read()
        #self.public_address_user_shielded ="ztestsapling13y3ujgw6swemw4lhlk9786g3wngfgqmxjdy7f0d5wg6jz0yvmntnk5u4agvxt3zptght5rftmtj"
        print("change shielded address: ", self.public_address_user_shielded)

        cmd = os.popen(
            zcash_cli + " " + conf_file + ' z_exportkey ' +
            self.public_address_user_shielded)  # export the private key
        sk_change_shielded = cmd.read()
        print("paying shielded private key: ", sk_change_shielded)
        #---------------------setup the tx to send-----------------------------
        tx = ' \'[{\"address\": \"' + addr_u.strip() + '\", \"amount\":0.82},' + \
                '{\"address\": \"' + public_address_user_shielded.strip() + '\", \"amount\":0},' + \
                '{ \"address\": \"' + public_address_decoder_shielded.strip() + '\", \"amount\":0.01, \"memo\": \"' \
                    + ct_c + self.public_address_user_shielded.encode().hex() + '\"}]\''

        cmd = zcash_cli + ' ' + conf_file + ' z_sendmany ' + addr_u.strip(
        ) + tx + ' 1 0'
        tx_operation_id = os.popen(cmd).read()
        print("tx1 operation id: ", tx_operation_id)
        return tx_operation_id
예제 #4
0
# // DOCUMENTATION //
# python bitcoin-utils:  https://github.com/karask/python-bitcoin-utils

# Import modules
from bitcoinutils.setup import setup
from bitcoinutils.keys import PrivateKey

# 1. Setup the network.You can use either the mainnet or the testnet.
setup('testnet')

# 2. Create a private key. Do you all have the same private key?
#     private keys are from definition random numbers
priv = PrivateKey(secret_exponent=1)

# 3. What is the WIF (wallet import format) format (uncompressed) of the private key?
print("\nPrivate key WIF:", priv.to_wif(compressed=False))

# 4. What is the WIF format (compressed) of the private key?
print("\nPrivate key WIF:", priv.to_wif(compressed=True))

# 5. What is the corresponding public key? In uncompressed and compressed forms.
#    public keys are generated from private keys using ECDSA cryptographic function
pub = priv.get_public_key()

print("Public key:", pub.to_hex(compressed=False))
print("Public key:", pub.to_hex(compressed=True))

# 6. What is the corresponding address? Get address from public key
address = pub.get_address()
print("Address:", address.to_string())
# Get help for a class / method / function from bitcoinutils module using help() or ctrl + click
help(PrivateKey)
help(Script)


# // LAB: Relative Timelock Example //

# 0. What is the type of network you working on? (mainnet / testnet)
#    Always remember to setup the network before anything else.
setup('testnet')


# 1. Create a new private key
#    Use secret_exponent parameter to get a non random private key
priv = PrivateKey(secret_exponent=1)
priv.to_wif()
priv.to_bytes()  # 32 bytes = 32 * 8 bits = 256 bits

# 2a. Get the corresponding public Key
pub = priv.get_public_key()
pub.to_hex()
pub.to_hash160()
pub.to_bytes()

# 2b. Get the address that corresponds to that public key.
#     The string representation of this address starts with m on n, because it is a Bitcoin testnet address
address = priv.get_public_key().get_address()
address.to_string()
address.to_hash160()

address._is_address_valid(address.to_string())   # True
예제 #6
0
 def test_exponent_creation(self):
     p = PrivateKey(secret_exponent=1)
     self.assertEqual(p.to_bytes(), self.key_bytes)
     self.assertEqual(p.to_wif(compressed=False), self.key_wif)
     self.assertEqual(p.to_wif(), self.key_wifc)
예제 #7
0
 def test_wif_creation(self):
     p = PrivateKey(self.key_wifc)
     self.assertEqual(p.to_bytes(), self.key_bytes)
     self.assertEqual(p.to_wif(compressed=False), self.key_wif)