def _convenience_build_standard_transaction(self): """These transactions get POSTed directly to masternodes by TAU wallet software""" STU = (ED25519Wallet.new()) DAVIS = (ED25519Wallet.new()) DENTON = (ED25519Wallet.new()) FALCON = (ED25519Wallet.new()) KNOWN_ADRS = (STU, DAVIS, DENTON, FALCON) amount = 10 tx = StandardTransactionBuilder.create_tx(STU[0], STU[1], DAVIS[1], amount) return tx
def test_verify_wrong_ms(self): # Test merkle tree validation returns false for incorrect verifying (public) key msg = b'this is a pretend merkle tree hash' timestamp = 'now' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) ms = MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk) sk1, vk1 = ED25519Wallet.new() self.assertFalse(ms.verify(msg, vk1))
def test_invalid_sender_wrong_sender(self): """ Tests that an error is raised during creation if an invalid sender field is passed in. A sender should be a 64 character hex string verifying key. """ # Test an error is thrown when MerkleSignature created with a sender that is not the correct public key msg = b'this is a pretend merkle tree hash' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) timestamp = 'now' vk_bad = ED25519Wallet.new()[1] # different verifying (public) key bad_public_key = MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk_bad) self.assertRaises(Exception, bad_public_key)
def random_envelope(): sk, vk = ED25519Wallet.new() tx = StandardTransactionBuilder.random_tx() sender = 'me' return Envelope.create_from_message(message=tx, signing_key=sk, sender_id=sender)
def test_invalid_signature_bad_length(self): # Test signature incorrect length (but valid hex) sk, vk = ED25519Wallet.new() timestamp = 'now' sig = ''.join(('A' for _ in range(100))) wrong_len = MerkleSignature.create(sig_hex=sig, timestamp=timestamp, sender=vk, validate=False) self.assertRaises(Exception, wrong_len.validate)
def test_valid_sender(self): # Confirm no error when correct public key is used msg = b'this is a pretend merkle tree hash' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) timestamp = 'now' MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk) # no error thrown
def test_validate_catches_bad_sig(self): # test that validate is called by default and throws an exception with bad sig sk, vk = ED25519Wallet.new() sig = ''.join(('X' for _ in range(128))) timestamp = 'now' self.assertRaises(Exception, MerkleSignature.create, sig_hex=sig, timestamp=timestamp, sender=vk)
def test_create_with_envelope(self): """ Tests creating a message with an envelope produces an object with the expected properties """ sk, vk = ED25519Wallet.new() tx = StandardTransactionBuilder.random_tx() sender = 'me' env = Envelope.create_from_message(message=tx, signing_key=sk) cmd = ReactorCommand.create_cmd('some_cls', 'some_func', envelope=env) self.assertTrue(ReactorCommand.envelope, env)
def test_deserialization_valid_json(self): # Test valid json throws no errors msg = b'this is a pretend merkle tree hash' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) d = { MerkleSignature.SIG: signature, MerkleSignature.TS: 'now', MerkleSignature.SENDER: vk } binary = json.dumps(d).encode() MerkleSignature.from_bytes(binary)
def test_deserialization_bad_sk_hex(self): # Test valid json but signature (private key) not proper hex msg = b'this is a pretend merkle tree hash' sk, vk = ED25519Wallet.new() sig = ''.join(('Z' for _ in range(128))) d = { MerkleSignature.SIG: sig, MerkleSignature.TS: 'now', MerkleSignature.SENDER: vk } binary = json.dumps(d).encode() self.assertRaises(Exception, MerkleSignature.from_bytes, binary)
def test_invalid_timestamp(self): """ Test that if the timestamp field is not formatted as expected an error will be thrown """ msg = b'this is a pretend merkle tree hash' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) timestamp = 99 self.assertRaises( TypeError, MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk))
def run_mgmt(): from cilantro.logger import get_logger from cilantro import Constants from cilantro.db import DB, DB_NAME from cilantro.utils.test import MPComposer from cilantro.protocol.wallets import ED25519Wallet import os, time, asyncio log = get_logger(__name__) sk = Constants.Testnet.Masternodes[0]['sk'] vk = Constants.Protocol.Wallets.get_vk(sk) s,v = ED25519Wallet.new() mpc = MPComposer(name='mgmt', sk=s) mpc.add_sub(filter='a', vk=vk)
def _create_merkle_sig(self, msg: bytes): """ Helper method to create a MerkleSignature and wallet keys :return: A tuple container (MerkleSignature, signing_key, verifying_key) """ assert type(msg) == bytes, "Must pass in bytes" sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) ms = MerkleSignature.create(sig_hex=signature, timestamp=TIMESTAMP, sender=vk) return ms, sk, vk
def test_verify_valid_ms(self): """ Tests that MerkleSignature.verify(...) returns true given a proper msg and vk """ # Test merkle tree verify() validates correct verifying (public) keys msg = b'this is a pretend merkle tree hash' timestamp = 'now' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) ms = MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk) self.assertTrue(ms.verify(msg))
def run_mgmt(): from cilantro.logger import get_logger from cilantro import Constants from cilantro.db import DB, DB_NAME from cilantro.utils.test import MPComposer from cilantro.protocol.wallets import ED25519Wallet import os, time, asyncio log = get_logger("MANAGEMENT NODE") sk = Constants.Testnet.Masternode.Sk vk = Constants.Protocol.Wallets.get_vk(sk) s, v = ED25519Wallet.new() mpc = MPComposer(name='mgmt', sk=s) log.critical("trying to look at vk: {}".format(vk)) mpc.add_sub(filter='a', url='tcp://{}:33333'.format(vk))
def test_invalid_sender_bad_hash(self): # Test an error is thrown when created with a sender of not valid hash msg = b'this is a pretend merkle tree hash' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) timestamp = 'now' vk_bad_hash = ''.join( 'Z' for _ in range(64)) # verifying (public) key with bad hash self.assertRaises( Exception, MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk_bad_hash, validate=False))
def test_valid_creation(self): """ Tests that a MerkleSignature created with some argument has the expected properties """ msg = b'this is a pretend merkle tree hash' timestamp = 'now' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) ms = MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk) self.assertEqual(ms.signature, signature) self.assertEqual(ms.timestamp, timestamp) self.assertEqual(ms.sender, vk)
def test_invalid_signature_nonhex(self): """ Tests that an error is raised during validation if an invalid signature is passed (nonhex, or length != 128) """ msg = b'this is a pretend merkle tree hash' timestamp = 'now' sk, vk = ED25519Wallet.new() # Test nonhex signature (but valid length) sig = ''.join(('X' for _ in range(128))) nonhex = MerkleSignature.create(sig_hex=sig, timestamp=timestamp, sender=vk, validate=False) self.assertRaises(Exception, nonhex.validate)
def build_test_merkle_sig(msg: bytes = b'some default payload', sk=None, vk=None) -> MerkleSignature: """ Builds a 'test' merkle signature. Used exclusively for unit tests :return: """ import time if not sk: sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) return MerkleSignature.create(sig_hex=signature, timestamp=str(time.time()), sender=vk)
def test_serialization(self): """ Tests that a created block data reply successfully serializes and deserializes. The deserialized object should have the same properties as the original one before it was serialized. """ msg = b'this is a pretend merkle tree hash' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) timestamp = 'now' valid_merkle_sig = MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk) valid_merkle_sig_serialized = valid_merkle_sig.serialize() clone = MerkleSignature.from_bytes(valid_merkle_sig_serialized) self.assertEqual(valid_merkle_sig.signature, clone.signature) self.assertEqual(valid_merkle_sig.timestamp, clone.timestamp) self.assertEqual(valid_merkle_sig.sender, clone.sender)
def random_envelope(sk=None, tx=None): sk = sk or ED25519Wallet.new()[0] tx = tx or random_msg() return Envelope.create_from_message(message=tx, signing_key=sk)
import sys import time PORT_START = 4650 NUM_DELEGATES = 4 MULTI_PROC = True # NOTE: True is not working atm (reactor wont get notify_ready() for some reason) URL_BASE = 'ipc://127.0.0.1' delegates = [] log = get_logger("DelegateFactory") log.info("MAIN THREAD") # Delegates and wallets for n in range(NUM_DELEGATES): sk, vk = ED25519Wallet.new() port = PORT_START + n url = "{}:{}".format(URL_BASE, port) # delegates.append({'url': 'ipc://127.0.0.1', 'verifying_key': vk, 'signing_key': sk, 'port': port}) delegates.append({'url': url, 'verifying_key': vk, 'signing_key': sk}) # public_delegate_list = [{'url': d['url'], 'verifying_key': d['verifying_key']} for d in delegates] public_delegates = {d['url']: d['verifying_key'] for d in delegates} def signal_handler(signal, frame): log.debug('You pressed Ctrl+C!') [d.subscriber_process.terminate() for d in delegate_objects] sys.exit(0)
AKA: m, h(m), s(h(m)) for all delegates 1. test that we can make a merkle tree and sign it pretty quickly (DONE) 2. test that we can make a merkle tree on a whole bunch of nodes and sign it between each other ''' # create real transactions and a real merkle tree true_txs = [secrets.token_bytes(64) for i in range(100)] m = MerkleTree(true_txs) # create fake transactions and a fake merkle tree fake_txs = [secrets.token_bytes(64) for i in range(100)] n = MerkleTree(fake_txs) # generate the delegates with new wallets delegates = [ED25519Wallet.new() for i in range(64)] connection_list = ['inproc://{}'.format(k[1]) for k in delegates] def print_stuff(): pprint(connection_list) print('\n===MERKLE TREE HASHED===') h = hashlib.sha3_256() [h.update(mm) for mm in m.nodes] print(h.digest().hex()) print('\n===ENTIRE MERKLE TREE===') [print(mm.hex()) for mm in m.nodes]
from cilantro.protocol.proofs import POW from cilantro.protocol.interpreters import TestNetInterpreter from cilantro.protocol.wallets import ED25519Wallet ''' Steps: 1. create a ton of wallets 2. create a ton of random transaction for those wallets 3. interpret those transaction and verify them 4. put the db output stack in a list 5. figure it out from there. ''' import random import redis NUM_WALLETS = 100 wallets = [ED25519Wallet.new() for x in range(NUM_WALLETS)] txs = [] interpreter = TestNetInterpreter(proof_system=POW) setup_queries = {} def generate_random_std_transaction(): from_wallet, to_wallet = random.sample(wallets, 2) amount = str(random.randint(1, 1000)) tx = TestNetTransaction.standard_tx(from_wallet[1], to_wallet[1], amount) transaction_builder = TestNetTransaction(ED25519Wallet, POW)