def transaction_to_block(transaction, miner, blockchain): miner = PrivateKey(miner).public_key().to_bytes() block = Block(blockchain.get_last_block(), [transaction], miner_pub_key=miner) block.mine(blockchain.get_difficult()) return block
def test_transaction(sender_key: int, receiver_key: int, amount: float): receiver_pub_key = PrivateKey(receiver_key).public_key().to_bytes() transaction = Transaction(outputs=[(receiver_pub_key, amount)]) wallet = Wallet(sender_key) signed_transaction = wallet.sign(transaction) assert signed_transaction.is_signed_correctly() assert signed_transaction.is_equal(signed_transaction)
def generate_keys(): p = generate_prime(1000, 2000) q = generate_prime(2000, 3000) assert p != q n = p * q phi = (p - 1) * (q - 1) e = 101 d = 1 while (d * e) % phi != 1: d += 1 return PublicKey(e, n), PrivateKey(d, n)
def construct_simple_transaction(wifkey, output_addr, locktime, prevout_txid, prevout_index, prevout_value): ''' Construct a Bitcoin Cash one-input / one-output transaction. wifkey (str) : private key (Wallet Import Format) output_addr (str) : recipient address (legacy or cash format) prevout_txid (str) : previous output transaction id prevout_index (int) : index of the output in the previous transaction prevout_value (int) : previous output value in satoshis''' # Private key prvkey = PrivateKey.from_wif(wifkey) # Public key and address (Public Key Hash) pubkey = PublicKey.from_prvkey(prvkey) input_address = Address.from_pubkey(pubkey) # Output address output_address = Address.from_string(output_addr) # Creation of the transaction txin = {} txin['address'] = input_address txin['txid'] = prevout_txid txin['index'] = prevout_index txin['value'] = prevout_value txin['sequence'] = Constants.SEQUENCE_NUMBER txin['pubkeys'] = [pubkey] txin['nsigs'] = 1 tx = Transaction.from_inputs([txin], locktime) txsize = (tx.estimate_size() + 32 + 2 * (output_address.kind == Constants.CASH_P2PKH)) fee = Constants.FEE_RATE * txsize txout = {} txout['address'] = output_address txout['value'] = prevout_value - fee tx.add_output(txout) prvkeys = [prvkey] tx.sign(prvkeys) rawtx = tx.serialize() fee = tx.get_fee() print("Input address", input_address.to_cash()) print("Output address", output_address.to_cash()) print("Amount sent (sat)", prevout_value - fee) print("Fee (sat)", fee) print() if tx.iscomplete: return rawtx, tx.txid(), fee else: return None
def generate_keys(): p = gen_prime(200, 300) q = gen_prime(300, 400) n = p * q e = 101 # Здесь должен быть обратный эл-т в кольце по модулю! d = 1 while (e * d) % n != 1: d += 1 return PublicKey(e, n), PrivateKey(d, n)
def add_transaction(): transaction_dict = request.json form = TransactionForm(**transaction_dict) if form.validate(): wallet = Wallet(key) transaction = Transaction(outputs=[(PrivateKey( transaction_dict['receiver']).public_key().to_bytes(), transaction_dict['amount'])]) signed_transaction = wallet.sign(transaction) transactionPool.receive_transaction(blockchain, signed_transaction) return '' return '', 403
class Wallet: def __init__(self, private_key_number): self.private_key = PrivateKey(private_key_number) def get_public_key(self): return self.private_key.public_key() def get_public_key_in_bytes(self): return self.get_public_key().to_bytes() def sign(self, transaction): signed_transaction = deepcopy(transaction) if signed_transaction.input is None: signed_transaction.input = self.get_public_key_in_bytes() signature = self.private_key.sign(bytes(signed_transaction)) signed_transaction.signature = signature return signed_transaction def get_balance(self, blockchain): return blockchain.balances[self.get_public_key_in_bytes()]
def get_prvkeys(self, branch, index): ''' branch (int): external (0) or internal (1) ''' assert branch in (0, 1) if self.is_watching_only(): raise KeyStoreError("watching-only keystore") if isinstance(index, int): index = [index] branch_xprv, _ = private_derivation(self.get_account_xprv(), "", "/{:d}".format(branch)) prvkeys = [] for i in index: xprv, _ = private_derivation(branch_xprv, "", "/{:d}".format(i)) prvkey, _, _, _, _ = decode_xkey(xprv) prvkeys.append(PrivateKey.from_hex(prvkey)) return prvkeys
def blockchain(): blockchain = Blockchain() private_keys = [43, 53, 63] public_keys = mapv(lambda priv: PrivateKey(priv).public_key().to_bytes(), private_keys) genesis_block = GenesisBlock(miner_pub_key=public_keys[0]) genesis_block.mine(blockchain.get_difficult()) blockchain.add(genesis_block) miners = [public_keys[1], public_keys[2]] outputs_array = [[(public_keys[0], 1), (public_keys[1], 2)], [(public_keys[2], 10), (public_keys[1], 1)]] for key, miner, outputs in zip(private_keys, miners, outputs_array): transaction = Wallet(key).sign(Transaction(outputs)) block = Block(blockchain.get_last_block(), [transaction], miner_pub_key=miner) block.mine(blockchain.get_difficult()) blockchain.add(block) return blockchain
# Save, load, and generate keys parser.add_argument("--keyfile", "-k", help="Generate and save keys to filename provided") parser.add_argument("--public", "-pu", help="Provide a public key file for encryption") parser.add_argument("--private", "-pr", help="Provide a private key file for decryption") args = parser.parse_args() rsa = RSA() # Encrypt and possibly save a message if args.message and args.public: message = args.message public = PublicKey.load(args.public) filename = args.save encrypt_message(rsa, message, public, filename) # Decrypt a message in a file elif args.load and args.private: filename = args.load private = PrivateKey.load(args.private) decrypt_file(rsa, filename, private) # Generate and save keys to file elif args.keyfile: filename = args.keyfile gen_keys(rsa, filename)
import sys if sys.version_info < (3, 5): sys.exit("Error: Must be using Python 3.5 or higher") print() ''' My addresses ''' print("ADDRESSES") print() # Claim private key # mainnet: Kz2GqdB5f5JWEYn1znS8bk2aUqg4J8WXycs6uEnwCUobRsib6N2R # testnet: cQPGJYAw68zmPzFHPCFFy4Xe74yTxacE3f1a1fFShbTbgckLz6dn # Claim address # testnet: bchtest:qrtppnj5yk7ur6u2t0tagesvlyhycddgfq630lqqkh # n12q2oecWxTmcaMmMShnqvtC4FL59LCaBE (legacy) claim_secret = 0x53a0e9e7894f4f77fba3cc62d9dcb82d61d085c34f7fa2a712e26d62ae4f42a3 claim_prvkey = PrivateKey(claim_secret) print("Claim address") print(" privkey", claim_prvkey) claim_pubkey = PublicKey.from_prvkey(claim_prvkey) claim_address = Address.from_pubkey(claim_pubkey) print(" address (cash)", claim_address.to_full_cash()) print(" address (legacy)", claim_address.to_legacy()) print(" address (hex)", claim_address.h.hex()) print() # Refund private key # mainnet: L1MK6tz8mD9WRnr9RqsbDg9BYktUkUwd6ZWQRHgdHU1m6VbUPkdz # testnet: cRiJZoyzCGqmbEKQpFgiazeFAzBtQw3KAbesXi98nafmMEgYAMDg # Refund address # testnet: bchtest:qrref365h428zh4zvwpnnfzlp2tq4w9t8qnh0pzm5q # myiEzYohqurvpghWrCaaafz5orGQinfEC9
def __init__(self, private_key_number): self.private_key = PrivateKey(private_key_number)
txin['index'] = 0 txin['value'] = 175352 txout1 = {} txout1['type'] = 'p2sh' txout1['address'] = p2sh_addr txout1['value'] = txin['value'] // 2 - 200 txout2 = {} txout2['type'] = 'p2pkh' txout2['address'] = output_addr txout2['value'] = txin['value'] // 2 - 200 tx = Transaction(2, [txin], [txout1, txout2], 0) prvkeys = [[ PrivateKey.from_wif(wifkeys_multisig[2]), PrivateKey.from_wif(wifkeys_multisig[0]) ]] tx.sign(prvkeys, alg="schnorr") txb = tx.serialize() txid = tx.txid().hex() fee = tx.get_fee() print("Transaction BCH de {} vers {} et {}".format( p2sh_addr.to_cash(), txout1['address'].to_cash(), txout2['address'].to_cash())) print("size", len(txb)) print("fee (satcashes)", fee) print(txb.hex()) print("id:", txid)
import requests from datetime import datetime from block import Transaction, Block from crypto import PublicKey, PrivateKey, sign_message b_url = 'http://localhost:5002' private_key = PrivateKey(3, 37) public_key = PublicKey(3, 37) address = '123' def add_transaction(to, amount): t = Transaction( address, to, amount, '', public_key.dumps(), ) t.sign = sign_message(private_key.d, private_key.n, t.get_message()) r = requests.post(b_url + '/add_transaction', json=t.to_dict()) print(r.text) def get_transactions(): r = requests.get(b_url + '/transaction_pool') return r.json()
## Bitcoin multisig the hard way - https://www.soroushjp.com/2014/12/20/bitcoin-multisig-the-hard-way-understanding-raw-multisignature-bitcoin-transactions/ #wifkeys = ["5JruagvxNLXTnkksyLMfgFgf3CagJ3Ekxu5oGxpTm5mPfTAPez3", #"5JX3qAwDEEaapvLXRfbXRMSiyRgRSW9WjgxeyJQWwBugbudCwsk", #"5JjHVMwJdjPEPQhq34WMUhzLcEd4SD7HgZktEh8WHstWcCLRceV"] # Sorted public keys involved in the multisig address pubkeys = [PublicKey.from_prvkey(wk).to_ser() for wk in wifkeys_multisig] # Number of signatures required to unlock the multisig address nsigs = 2 redeem_script = multisig_locking_script(pubkeys, nsigs) p2sh_addr = Address.from_script(redeem_script) # Transaction 1: p2pkh -> p2sh prvkey1 = PrivateKey.from_wif(wifkey1) pubkey1 = PublicKey.from_prvkey(prvkey1) input_address = Address.from_pubkey(pubkey1.to_ser()).to_string() output_address = p2sh_addr.to_cash() prevout_txid = "10e7ee10ecab3d16fcba5160792733dc2eeeb7270389d304832da3c9f5d31ef5" prevout_index = 1 prevout_value = 80000 # previous output value locktime = 537937 # Creation of the transaction txin = {} txin['address'] = Address.from_pubkey( bytes.fromhex(pubkey1.to_ser(strtype=True))) txin['txid'] = prevout_txid txin['index'] = prevout_index
def get_balance(addr): blocks = get_blocks() transactions = sum((x.transactions for x in blocks), []) amount = 0 for transaction in transactions: if transaction.from_addr == addr: amount -= transaction.amount if transaction.to_addr == addr: amount += transaction.amount return amount f = open('wallet.json', 'r') wallet = json.loads(f.read()) f.close() public_key = PublicKey.loads(wallet['public_key']) private_key = PrivateKey.loads(wallet['private_key']) address = wallet['address'] print(add_transaction('0' * 64, 1, address, public_key, private_key)) print(get_transactions()) print(mine()) print(get_transactions()) # get_blocks() print(get_balance(address))
hash_redeem_script = sha256(redeem_script) print(hash_redeem_script.hex(), "=?", redeem_script_hash.hex(), ("Oui" if hash_redeem_script == redeem_script_hash else "Non")) redeem_script_2 = bytes.fromhex( "6321035ddbc3ec6a9459ab05e20af1451d80deff37941095b2003ecc27c0235a8dc4d067029000b2752102d7c52ff0e21c77a848fe5b2eb1cd68a1ad4d84dfeacd2c1ec141d66f4364772868ac" ) hash_redeem_script_2 = sha256(redeem_script_2) witprog = hash_redeem_script_2 p2wsh_addr_2 = SegWitAddr.encode(SegWitAddr.SEGWIT_HRP, witver, witprog) print(p2wsh_addr_2) print() # BTC transaction wifkey = "KxDEKVUyDvbZj2sCoiKjpPKHuz7Vcei7t4PL1wiapTnQTAgLdNrb" prvkey = PrivateKey.from_wif(wifkey) pubkey = PublicKey.from_prvkey(prvkey) address = Address.from_pubkey(pubkey) # 1QKcNhS3VmDfbaAoX8gZpKuM3Xr9gZ8Pxo print() print("BTC legacy transaction") print("Keys", prvkey, pubkey) print("Address", address.to_legacy()) output_address = Address.from_string("37KwYRZUteFURrfYiKDg21dGWvf8K1wcNm") print("Output address", output_address.to_legacy()) # Inputs txins = [] txin1 = {} txin1['address'] = address txin1['type'] = 'p2pkh'
def to_out(out): return [PrivateKey(out[0]).public_key().to_bytes(), out[1]]
def create_app(my_ip: str = 'localhost:5000', key: int = 1, servers: List[str] = []): app = Flask(__name__) servers = deepcopy(servers) blockchain = Blockchain() transactionPool = TransactionPool() miner_key = PrivateKey(key).public_key().to_bytes() def send_my_ip(): server_data = servers + [my_ip] for server in servers: requests.post(f'http://{server}/receiveips', \ data=dumps(server_data)) def send_blockchain(): for server in servers: blockArray = BlockArray(blockchain) blockchain_bytes = b64encode(pickle.dumps(blockArray)) requests.post(f'http://{server}/receiveblockchain', \ data=blockchain_bytes) send_my_ip() @app.route('/blockchain') def blockchain_web(): return jsonify(blockchain.to_dict()) @app.route('/addtransaction', methods=['POST']) def add_transaction(): transaction_dict = request.json form = TransactionForm(**transaction_dict) if form.validate(): wallet = Wallet(key) transaction = Transaction(outputs=[(PrivateKey( transaction_dict['receiver']).public_key().to_bytes(), transaction_dict['amount'])]) signed_transaction = wallet.sign(transaction) transactionPool.receive_transaction(blockchain, signed_transaction) return '' return '', 403 @app.route('/mine') def mine(): if len(blockchain) == 0: genesis_block = GenesisBlock(miner_pub_key=miner_key) genesis_block.mine(blockchain.get_difficult()) blockchain.add(genesis_block) else: block = Block(previous_block=blockchain.chain[-1], miner_pub_key=miner_key, \ transactions=transactionPool.get_best_transactions(blockchain,1)) block.mine(blockchain.get_difficult()) blockchain.add(block) send_blockchain() return '' @app.route('/receiveblockchain', methods=['POST']) def receive_blockchain(): blockArray = pickle.loads(b64decode(request.data)) if isinstance(blockArray, BlockArray): len_before = len(blockchain) blockchain.substitute(blockArray) len_after = len(blockchain) if len_after > len_before: send_blockchain() return '' return '', 403 @app.route('/receiveips', methods=['POST']) def receive_ips(): ips = loads(request.data.decode('utf-8')) if isinstance(ips, list): for ip in ips: if ip not in servers and ip != my_ip: servers.append(ip) return '' @app.route('/getips') def get_ips(): return jsonify(servers) return app