def sign_message_with_transaction(transaction, key): """ Signs a transaction message or transaction :param transaction dict: :param key str: A signing key returns message, txnid tuple: The first 16 characters of a sha256 hexdigest. """ transaction['Nonce'] = time.time() pub = pybitcointools.encode_pubkey(pybitcointools.privkey_to_pubkey(key), "hex") transaction["public_key"] = pub sig = pybitcointools.ecdsa_sign( dict2cbor(transaction), key ) transaction['Signature'] = sig txnid = hashlib.sha256(transaction['Signature']).hexdigest()[:16] message = { 'Transaction': transaction, '__TYPE__': "/mktplace.transactions.MarketPlace/Transaction", '__NONCE__': time.time(), } cbor_serialized_mess = dict2cbor(message) signature = pybitcointools.ecdsa_sign( cbor_serialized_mess, key ) message['__SIGNATURE__'] = signature return message, txnid
def _sign_message_with_transaction(transaction, message_type, key): """ Signs a transaction message or transaction :param transaction (dict): :param key (str): A signing key returns message, txnid (tuple): The first 16 characters of a sha256 hexdigest. """ transaction['Nonce'] = time.time() pub = pybitcointools.encode_pubkey(pybitcointools.privkey_to_pubkey(key), "hex") transaction["public_key"] = pub sig = pybitcointools.ecdsa_sign(_dict2cbor(transaction), key) transaction['Signature'] = sig txnid = hashlib.sha256(transaction['Signature']).hexdigest()[:16] message = { 'Transaction': transaction, '__TYPE__': message_type, '__NONCE__': time.time(), } cbor_serialized_message = _dict2cbor(message) signature = pybitcointools.ecdsa_sign(cbor_serialized_message, key) message['__SIGNATURE__'] = signature return message, txnid
def do_generate(args): private_key = pybitcointools.random_key() public_key = pybitcointools.encode_pubkey( pybitcointools.privkey_to_pubkey(private_key), "hex") words = generate_word_list(args.pool_size) batches = [] start = time.time() total_txn_count = 0 for i in xrange(0, args.count): txns = [] for _ in xrange(0, random.randint(1, args.batch_max_size)): txn = create_intkey_transaction( verb=random.choice(['set', 'inc', 'dec']), name=random.choice(words), value=random.randint(0, 100000), private_key=private_key, public_key=public_key) total_txn_count += 1 txns.append(txn) batch = create_batch( transactions=txns, private_key=private_key, public_key=public_key) batches.append(batch) if i % 100 == 0 and i != 0: stop = time.time() txn_count = 0 for batch in batches[-100:]: txn_count += len(batch.transactions) fmt = 'batches {}, batch/sec: {:.2f}, txns: {}, txns/sec: {:.2f}' print fmt.format( str(i), 100 / (stop - start), str(total_txn_count), txn_count / (stop - start)) start = stop batch_list = batch_pb2.BatchList(batches=batches) print "Writing to {}...".format(args.output) with open(args.output, "w") as fd: fd.write(batch_list.SerializeToString())
def test_is_valid_pub_key(self): pubkey = pybitcointools.privkey_to_pubkey("5KQ4iQQGgbQX9MmfiPUwwHBL1R" "GPa86NwFbqrWoodjuzruqFVDd") pub = pybitcointools.encode_pubkey(pubkey, "hex") minfo = {'Nonce': 100, 'public_key': pub, 'TransactionType': '/Transaction', 'Dependencies': []} sig = pybitcointools.ecdsa_sign( signed_object.dict2cbor(minfo), "5KQ4iQQGgbQX9MmfiPUwwHBL1RGPa86NwFbqrWoodjuzruqFVDd" ) # Create valid transaction minfo["Signature"] = sig temp = Transaction(minfo) self.assertTrue(temp.is_valid("unused")) # Change transaction after it was signed minfo["Nonce"] = time.time() temp = Transaction(minfo) self.assertFalse(temp.is_valid("unused"))
def test_is_valid_pub_key(self): pubkey = pybitcointools.privkey_to_pubkey("5KQ4iQQGgbQX9MmfiPUwwHBL1R" "GPa86NwFbqrWoodjuzruqFVDd") pub = pybitcointools.encode_pubkey(pubkey, "hex") minfo = { 'Nonce': 100, 'public_key': pub, 'TransactionType': '/Transaction', 'Dependencies': [] } sig = pybitcointools.ecdsa_sign( signed_object.dict2cbor(minfo), "5KQ4iQQGgbQX9MmfiPUwwHBL1RGPa86NwFbqrWoodjuzruqFVDd") # Create valid transaction minfo["Signature"] = sig temp = Transaction(minfo) self.assertTrue(temp.is_valid("unused")) # Change transaction after it was signed minfo["Nonce"] = time.time() temp = Transaction(minfo) self.assertFalse(temp.is_valid("unused"))
def do_generate(args): private_key = pybitcointools.random_key() public_key = pybitcointools.encode_pubkey( pybitcointools.privkey_to_pubkey(private_key), "hex") words = generate_word_list(args.pool_size) txns = [] batches = [] bytecode = "" with open(args.contract, "rb") as fd: byte = fd.readline() while byte != "": bytecode += byte byte = fd.readline() byte_addr = get_address("jvm_sc", bytecode) txn = create_jvm_sc_transaction(verb='store', private_key=private_key, public_key=public_key, bytecode=bytecode, methods=["set", "inc", "dec"]) txns.append(txn) keys = [] addresses = [] for i in range(20): if len(txns) < 10: key = random.choice(words) keys.append(key) value = str(random.randint(0, 1000)) key_addr = get_address("intkey", key) addresses.append(key_addr) txn = create_jvm_sc_transaction(verb='run', private_key=private_key, public_key=public_key, byte_addr=byte_addr, method="set", parameters=[ "key," + key, "value," + value, "&check," + key_addr ], addresses=addresses) txns.append(txn) addresses = [] else: batch = create_batch(txns, private_key, public_key) batches.append(batch) txns = [] for i in range(20): if len(txns) < 10: key = random.choice(keys) key_addr = get_address("intkey", key) addresses.append(key_addr) txn = create_jvm_sc_transaction( verb='run', private_key=private_key, public_key=public_key, byte_addr=byte_addr, method=random.choice(["inc", "dec"]), parameters=["key," + key, "&value," + key_addr, "diff,2"], addresses=addresses) txns.append(txn) addresses = [] else: batch = create_batch(txns, private_key, public_key) batches.append(batch) txns = [] batch_list = batch_pb2.BatchList(batches=batches) print "Writing to {}...".format(args.output) with open(args.output, "w") as fd: fd.write(batch_list.SerializeToString())
# It requires less computation and less space in the blockchain. # THE UNCOMPRESSED KEY # # Although the private key in this demonstration was statically defined, we will generate # the public key on the fly. We'll use pybitcointools library by Vitalik. # # Sorry guys but we won't discuss in this demo how Elliptic Curve Cryptography works, nor # how to generate an ECDSA public key. Although, you are invited to take a look at how # pybitcointools generates it. # # The following is the uncompressed 65 bytes public key, in hex encoding # Why 65 bytes? The actual ECDSA public key is 64 bytes, but bitcoin protocol adds a prefix # to indicate if its uncompressed or compressed. In this case a "04" prefix. # That extra "04" is 1 byte. public_key_version = pybitcointools.privkey_to_pubkey(private_key_hex) # This will generate the hashed uncompressed public key, # that is, the "bitcoin address" that you share with others. # Again, the uncompressed format is not recommended anymore. # # The sharable bitcoin address: public_address_uncompressed = utils.public_address(public_key_version) # THE COMPRESSED KEY # Compressed keys only specify the x coordinate plus an 1 byte # flag indicating which side of the symmetrical curve the point is on, which allows y to be derived. # In order to indicate that key is compressed, "02" or "03" prefix is added to the encoded hex value. # The "02" and "03" is determined based on the the elliptic curve equation result. # # Public key with compression (32 bytes + 1 byte prefix = 33 bytes), in hex encoding
def do_generate(args): private_key = pybitcointools.random_key() public_key = pybitcointools.encode_pubkey( pybitcointools.privkey_to_pubkey(private_key), "hex") words = generate_word_list(args.pool_size) txns = [] batches = [] bytecode = "" with open(args.contract, "rb") as fd: byte = fd.readline() while byte != "": bytecode += byte byte = fd.readline() byte_addr = get_address("jvm_sc", bytecode) txn = create_jvm_sc_transaction( verb='store', private_key=private_key, public_key=public_key, bytecode=bytecode, methods=["set", "inc", "dec"]) txns.append(txn) keys = [] addresses = [] for i in range(20): if len(txns) < 10: key = random.choice(words) keys.append(key) value = str(random.randint(0, 1000)) key_addr = get_address("intkey", key) addresses.append(key_addr) txn = create_jvm_sc_transaction( verb='run', private_key=private_key, public_key=public_key, byte_addr=byte_addr, method="set", parameters=["key," + key, "value," + value, "&check," + key_addr], addresses=addresses) txns.append(txn) addresses = [] else: batch = create_batch(txns, private_key, public_key) batches.append(batch) txns = [] for i in range(20): if len(txns) < 10: key = random.choice(keys) key_addr = get_address("intkey", key) addresses.append(key_addr) txn = create_jvm_sc_transaction( verb='run', private_key=private_key, public_key=public_key, byte_addr=byte_addr, method=random.choice(["inc", "dec"]), parameters=["key," + key, "&value," + key_addr, "diff,2"], addresses=addresses) txns.append(txn) addresses = [] else: batch = create_batch(txns, private_key, public_key) batches.append(batch) txns = [] batch_list = batch_pb2.BatchList(batches=batches) print "Writing to {}...".format(args.output) with open(args.output, "w") as fd: fd.write(batch_list.SerializeToString())