def test_pubkey_serialization(self): # pylint: disable=protected-access priv = signer.generate_privkey() pub = signer.generate_pubkey(priv) raw_pub = signer._decode_pubkey(pub, 'hex') pub2 = signer._encode_pubkey(raw_pub, 'hex') self.assertTrue(str(pub) == str(pub2))
def test_basic_ops(self): msg = 'this is a message' priv = signer.generate_privkey() pub = signer.generate_pubkey(priv) sig = signer.sign(msg, priv) ver = signer.verify(msg, sig, pub) self.assertTrue(ver)
def _read_signing_keys(key_filename): """Reads the given file as a WIF formatted key. Args: key_filename: The filename where the key is stored. If None, defaults to the default key for the current user. Returns: tuple (str, str): the public and private key pair Raises: CliException: If unable to read the file. """ filename = key_filename if filename is None: filename = os.path.join(os.path.expanduser('~'), '.sawtooth', 'keys', getpass.getuser() + '.wif') try: with open(filename, 'r') as key_file: wif_key = key_file.read().strip() signing_key = signing.encode_privkey( signing.decode_privkey(wif_key, 'wif'), 'hex') pubkey = signing.encode_pubkey( signing.generate_pubkey(signing_key), 'hex') return pubkey, signing_key except IOError as e: raise CliException('Unable to read key file: {}'.format(str(e)))
def _do_config_set(args): """Executes the 'set' subcommand. Given a key file, and a series of key/value pairs, it generates batches of sawtooth_config transactions in a BatchList instance, and stores it in a file. """ settings = [s.split('=', 1) for s in args.setting] with open(args.key, 'r') as key_file: wif_key = key_file.read().strip() signing_key = signing.encode_privkey( signing.decode_privkey(wif_key, 'wif'), 'hex') pubkey = signing.encode_pubkey(signing.generate_pubkey(signing_key), 'hex') txns = [ _create_config_txn(pubkey, signing_key, setting) for setting in settings ] txn_ids = [txn.header_signature for txn in txns] batch_header = BatchHeader(signer_pubkey=pubkey, transaction_ids=txn_ids).SerializeToString() batch = Batch(header=batch_header, header_signature=signing.sign(batch_header, signing_key), transactions=txns) batch_list = BatchList(batches=[batch]).SerializeToString() try: with open(args.output, 'wb') as batch_file: batch_file.write(batch_list) except: raise CliException('Unable to write to {}'.format(args.output))
def do_keygen(args): if args.key_name is not None: key_name = args.key_name else: key_name = getpass.getuser() if args.key_dir is not None: key_dir = args.key_dir if not os.path.exists(key_dir): raise CliException('no such directory: {}'.format(key_dir)) else: key_dir = os.path.join(os.path.expanduser('~'), '.sawtooth', 'keys') if not os.path.exists(key_dir): if not args.quiet: print('creating key directory: {}'.format(key_dir)) try: os.makedirs(key_dir) except IOError as e: raise CliException('IOError: {}'.format(str(e))) wif_filename = os.path.join(key_dir, key_name + '.wif') addr_filename = os.path.join(key_dir, key_name + '.addr') if not args.force: file_exists = False for filename in [wif_filename, addr_filename]: if os.path.exists(filename): file_exists = True print('file exists: {}'.format(filename), file=sys.stderr) if file_exists: raise CliException( 'files exist, rerun with --force to overwrite existing files') privkey = signing.generate_privkey() encoded = signing.encode_privkey(privkey) pubkey = signing.generate_pubkey(privkey) addr = signing.generate_identifier(pubkey) try: wif_exists = os.path.exists(wif_filename) with open(wif_filename, 'w') as wif_fd: if not args.quiet: if wif_exists: print('overwriting file: {}'.format(wif_filename)) else: print('writing file: {}'.format(wif_filename)) wif_fd.write(encoded) wif_fd.write('\n') addr_exists = os.path.exists(addr_filename) with open(addr_filename, 'w') as addr_fd: if not args.quiet: if addr_exists: print('overwriting file: {}'.format(addr_filename)) else: print('writing file: {}'.format(addr_filename)) addr_fd.write(addr) addr_fd.write('\n') except IOError as ioe: raise CliException('IOError: {}'.format(str(ioe)))
def _read_signing_keys(key_filename): """Reads the given file as a WIF formatted key. Args: key_filename: The filename where the key is stored. Returns: tuple (str, str): the public and private key pair Raises: CliException: If unable to read the file. """ filename = key_filename try: with open(filename, 'r') as key_file: wif_key = key_file.read().strip() signing_key = signing.encode_privkey( signing.decode_privkey(wif_key, 'wif'), 'hex') pubkey = signing.encode_pubkey( signing.generate_pubkey(signing_key), 'hex') return pubkey, signing_key except IOError as e: raise CliException('Unable to read key file: {}'.format(str(e)))
def do_populate(args, batches, words): private_key = signing.generate_privkey() public_key = signing.generate_pubkey(private_key) total_txn_count = 0 txns = [] for i in range(0, len(words)): name = list(words)[i] txn = create_intkey_transaction(verb='set', name=name, value=random.randint(9000, 100000), deps=[], private_key=private_key, public_key=public_key) total_txn_count += 1 txns.append(txn) # Establish the signature of the txn associated with the word # so we can create good dependencies later words[name] = txn.header_signature batch = create_batch(transactions=txns, private_key=private_key, public_key=public_key) batches.append(batch)
def do_register_init(args, config): priv_key = signing.generate_privkey() pub_key = signing.generate_pubkey(priv_key) user_public_key = args.user_public_key user_name = args.user_name email_address = args.email_address authorized = args.authorized role = args.role cmd = "user list-user" cmd = shlex.split(cmd) process = subprocess.Popen(cmd, stdout=subprocess.PIPE) process.wait() output = '' for line in process.stdout: output += line.decode("utf-8").strip() if output == "[]" and role == "admin" and len(user_public_key) == 66: url = config.get('DEFAULT', 'url') client = UserBatch(base_url=url) response = client.register_user(user_public_key, user_name, email_address, authorized, role, priv_key, pub_key) print_msg(response) else: print(ret_access_denied__msg('Invalid operation.'))
def do_populate(args): private_key = signing.generate_privkey() public_key = signing.generate_pubkey(private_key) words = generate_word_list(args.pool_size) batches = [] total_txn_count = 0 txns = [] for i in range(0, len(words)): txn = create_intkey_transaction(verb='set', name=words[i], value=random.randint(9000, 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) batch_list = batch_pb2.BatchList(batches=batches) print("Writing to {}...".format(args.output)) with open(args.output, "wb") as fd: fd.write(batch_list.SerializeToString())
def __init__(self): self.block_sender = MockBlockSender() self.batch_sender = MockBatchSender() self.block_store = BlockStore(DictDatabase()) self.block_cache = BlockCache(self.block_store) self.state_db = {} # add the mock reference to the consensus self.state_db[_setting_address('sawtooth.consensus.algorithm')] = \ _setting_entry('sawtooth.consensus.algorithm', 'test_journal.mock_consensus') self.state_view_factory = MockStateViewFactory(self.state_db) self.signing_key = signing.generate_privkey() self.public_key = signing.encode_pubkey( signing.generate_pubkey(self.signing_key), "hex") self.identity_signing_key = signing.generate_privkey() self.genesis_block = self._generate_genesis_block() self.set_chain_head(self.genesis_block) self.block_publisher = BlockPublisher( transaction_executor=MockTransactionExecutor(), block_cache=self.block_cache, state_view_factory=self.state_view_factory, block_sender=self.block_sender, batch_sender=self.block_sender, squash_handler=None, chain_head=self.genesis_block, identity_signing_key=self.identity_signing_key, data_dir=None)
def _create_key(self, key_name='validator.wif'): privkey = signing.generate_privkey() wif_file = os.path.join(self._temp_dir, key_name) with open(wif_file, 'w') as wif_fd: wif_fd.write(signing.encode_privkey(privkey)) return signing.generate_pubkey(privkey)
def __init__(self, base_url, keyfile, wait=None): """ Member variables: _base_url _private_key _public_key _transaction_family _payload_encoding _family_version _wait """ self._base_url = base_url try: with open(keyfile) as fd: self._private_key = fd.read().strip() fd.close() except: raise IOError("Failed to read keys.") self._public_key = signing.generate_pubkey(self._private_key) self._transaction_family = "battleship" self._payload_encoding = "json-utf8" self._family_version = "1.0" self._wait = wait
def __init__(self): self._report_private_key = \ signing.encode_privkey( signing.decode_privkey(self.__REPORT_PRIVATE_KEY_WIF, 'wif'), 'hex') self._report_public_key = signing.generate_pubkey( self._report_private_key)
def test_invalid_signature(self): msg = "This is a message" priv = signer.generate_privkey() priv2 = signer.generate_privkey() sig = signer.sign(msg, priv) pub = signer.generate_pubkey(priv2) ver = signer.verify(msg, sig, pub) self.assertFalse(ver)
def __init__(self, test_name, tester): super().__init__(test_name) self.tester = tester self.private_key = signing.generate_privkey() self.public_key = signing.encode_pubkey( signing.generate_pubkey(self.private_key), "hex") self.factory = ValidatorRegistryMessageFactory( private=self.private_key, public=self.public_key)
def __init__(self, delegate, args): super(IntKeyWorkload, self).__init__(delegate, args) self._streams = [] self._pending_batches = {} self._lock = threading.Lock() self._delegate = delegate self._deps = {} self._private_key = signing.generate_privkey() self._public_key = signing.generate_pubkey(self._private_key)
def do_keygen(args): """Executes the key generation operation, given the parsed arguments. Args: args (:obj:`Namespace`): The parsed args. """ if args.key_name is not None: key_name = args.key_name else: key_name = 'validator' key_dir = get_key_dir() if not os.path.exists(key_dir): raise CliException("Key directory does not exist: {}".format(key_dir)) wif_filename = os.path.join(key_dir, key_name + '.wif') addr_filename = os.path.join(key_dir, key_name + '.addr') if not args.force: file_exists = False for filename in [wif_filename, addr_filename]: if os.path.exists(filename): file_exists = True print('file exists: {}'.format(filename), file=sys.stderr) if file_exists: raise CliException( 'files exist, rerun with --force to overwrite existing files') privkey = signing.generate_privkey() encoded = signing.encode_privkey(privkey) pubkey = signing.generate_pubkey(privkey) addr = signing.generate_identifier(pubkey) try: wif_exists = os.path.exists(wif_filename) with open(wif_filename, 'w') as wif_fd: if not args.quiet: if wif_exists: print('overwriting file: {}'.format(wif_filename)) else: print('writing file: {}'.format(wif_filename)) wif_fd.write(encoded) wif_fd.write('\n') addr_exists = os.path.exists(addr_filename) with open(addr_filename, 'w') as addr_fd: if not args.quiet: if addr_exists: print('overwriting file: {}'.format(addr_filename)) else: print('writing file: {}'.format(addr_filename)) addr_fd.write(addr) addr_fd.write('\n') except IOError as ioe: raise CliException('IOError: {}'.format(str(ioe)))
def get_keys(): # Get public and private key privkey = signing.generate_privkey() pubkey = signing.generate_pubkey(privkey) userKeyJSON = "{}" keys = json.loads(userKeyJSON) keys["public_key"] = pubkey keys["private_key"] = privkey userKeyJSON = json.dumps(keys) return userKeyJSON
def __init__(self, base_url, keyfile): self._base_url = base_url try: with open(keyfile) as fd: self._private_key = fd.read().strip() fd.close() except: raise IOError("Failed to read keys.") self._public_key = signing.generate_pubkey(self._private_key)
def __init__(self, base_url, keyfile): self._base_url = base_url try: with open(keyfile) as fd: self._private_key = fd.read().strip() fd.close() except: raise IOError("Failed to read keys.") self._public_key = signing.generate_pubkey(self._private_key)
def test_set_status(self): """Tests that set_status() has the correct behavior. Basically: 1. Adds a batch which has two transactions. 2. Calls next_transaction() to get the first Transaction. 3. Calls next_transaction() to verify that it returns None. 4. Calls set_status() to mark the first transaction applied. 5. Calls next_transaction() to get the second Transaction. Step 3 returns None because the first transaction hasn't been marked as applied, and the SerialScheduler will only return one not-applied Transaction at a time. Step 5 is expected to return the second Transaction, not None, since the first Transaction was marked as applied in the previous step. """ private_key = signing.generate_privkey() public_key = signing.generate_pubkey(private_key) context_manager = ContextManager(dict_database.DictDatabase()) squash_handler = context_manager.get_squash_handler() first_state_root = context_manager.get_first_root() scheduler = SerialScheduler(squash_handler, first_state_root) txns = [] for name in ['a', 'b']: txn = create_transaction(name=name, private_key=private_key, public_key=public_key) txns.append(txn) batch = create_batch(transactions=txns, private_key=private_key, public_key=public_key) scheduler.add_batch(batch) scheduled_txn_info = scheduler.next_transaction() self.assertIsNotNone(scheduled_txn_info) self.assertEquals('a', scheduled_txn_info.txn.payload.decode()) self.assertIsNone(scheduler.next_transaction()) scheduler.set_transaction_execution_result( scheduled_txn_info.txn.header_signature, is_valid=False, context_id=None) scheduled_txn_info = scheduler.next_transaction() self.assertIsNotNone(scheduled_txn_info) self.assertEquals('b', scheduled_txn_info.txn.payload.decode())
def _generate_genesis_block(self): """ Returns a blocker wrapper with the basics of the block header in place """ genesis_header = block_pb2.BlockHeader( block_num=0, previous_block_id=NULL_BLOCK_IDENTIFIER, signer_pubkey=signing.encode_pubkey( signing.generate_pubkey(self._identity_priv_key), "hex")) return BlockBuilder(genesis_header)
def __init__(self, identity_signing_key, batch_sender): """Initialize the BatchPublisher. :param identity_signing_key: the validator's signing key. :param batch_sender: interface to an object that will post the built batch to the network. """ self.identity_signing_key = identity_signing_key self._batch_sender = batch_sender self._identity_signing_key = identity_signing_key self._identity_public_key = signing.encode_pubkey( signing.generate_pubkey(self._identity_signing_key), "hex")
def setUp(self): self.block_store = BlockStore({}) self.gossip = MockGossip() self.completer = Completer(self.block_store, self.gossip) self.completer._on_block_received = self._on_block_received self.completer._on_batch_received = self._on_batch_received self.private_key = signing.generate_privkey() self.public_key = signing.encode_pubkey( signing.generate_pubkey(self.private_key), "hex") self.blocks = [] self.batches = []
def test_transaction_order(self): """Tests the that transactions are returned in order added. Adds three batches with varying number of transactions, then tests that they are returned in the appropriate order when using an iterator. This test also creates a second iterator and verifies that both iterators return the same transactions. This test also finalizes the scheduler and verifies that StopIteration is thrown by the iterator. """ private_key = signing.generate_privkey() public_key = signing.generate_pubkey(private_key) context_manager = ContextManager(dict_database.DictDatabase()) squash_handler = context_manager.get_squash_handler() first_state_root = context_manager.get_first_root() scheduler = SerialScheduler(squash_handler, first_state_root) txns = [] for names in [['a', 'b', 'c'], ['d', 'e'], ['f', 'g', 'h', 'i']]: batch_txns = [] for name in names: txn = create_transaction(name=name, private_key=private_key, public_key=public_key) batch_txns.append(txn) txns.append(txn) batch = create_batch(transactions=batch_txns, private_key=private_key, public_key=public_key) scheduler.add_batch(batch) scheduler.finalize() iterable1 = iter(scheduler) iterable2 = iter(scheduler) for txn in txns: scheduled_txn_info = next(iterable1) self.assertEqual(scheduled_txn_info, next(iterable2)) self.assertIsNotNone(scheduled_txn_info) self.assertEquals(txn.payload, scheduled_txn_info.txn.payload) scheduler.set_transaction_execution_result(txn.header_signature, False, None) with self.assertRaises(StopIteration): next(iterable1)
def __init__(self, test_name, tester): super().__init__(test_name) self.tester = tester self.private_key = signing.generate_privkey() self.public_key = signing.encode_pubkey( signing.generate_pubkey(self.private_key), "hex") self.factory = ValidatorRegistryMessageFactory( private=self.private_key, public=self.public_key) self._report_private_key = \ signing.encode_privkey( signing.decode_privkey( '5Jz5Kaiy3kCiHE537uXcQnJuiNJshf2bZZn43CrALMGoCd3zRuo', 'wif'), 'hex')
def _sign_block(self, block): """ The block should be complete and the final signature from the publishing validator (this validator) needs to be added. """ public_key = signing.encode_pubkey( signing.generate_pubkey(self._identity_priv_key), "hex") block.block_header.signer_pubkey = public_key block_header = block.block_header header_bytes = block_header.SerializeToString() signature = signing.sign(header_bytes, self._identity_priv_key) block.set_signature(signature) return block
def __init__(self, base_url, keyfile): self._base_url = base_url try: with open(keyfile) as fd: self._private_key = fd.read().strip() fd.close() except FileNotFoundError: raise XoException( 'Could not find private key file {}; ' 'try running `sawtooth keygen`'.format(keyfile)) except OSError: raise XoException("Failed to read keys.") self._public_key = signing.generate_pubkey(self._private_key)
def __init__(self, base_url, store_name=None, name='SawtoothClient', txntype_name=None, msgtype_name=None, keystring=None, keyfile=None, disable_client_validation=False): self._base_url = base_url self._message_type = msgtype_name self._transaction_type = txntype_name # an explicit store name takes precedence over a store name # implied by the transaction type self._store_name = None if store_name is not None: self._store_name = store_name.strip('/') elif txntype_name is not None: self._store_name = txntype_name.strip('/') self._communication = _Communication(base_url) self._last_transaction = None self._signing_key = None self._identifier = None self._update_batch = None self._disable_client_validation = disable_client_validation if keystring: LOGGER.debug("set signing key from string\n%s", keystring) self._signing_key = signing.encode_privkey( signing.decode_privkey(keystring, 'wif'), 'hex') elif keyfile: LOGGER.debug("set signing key from file %s", keyfile) try: self._signing_key = signing.encode_privkey( signing.decode_privkey( open(keyfile, "r").read().strip(), 'wif'), 'hex') except IOError as ex: raise ClientException( "Failed to load key file: {}".format(str(ex))) if self._signing_key is not None: self._identifier = signing.generate_identifier( signing.generate_pubkey(self._signing_key))
def test_completion_on_last_result(self): """Tests the that the schedule is not marked complete until the last result is set. Adds three batches with varying number of transactions, then tests that they are returned in the appropriate order when using an iterator. Test that the value of `complete` is false until the last value. This test also finalizes the scheduler and verifies that StopIteration is thrown by the iterator, and the complete is true in the at the end. """ private_key = signing.generate_privkey() public_key = signing.generate_pubkey(private_key) txns = [] for names in [['a', 'b', 'c'], ['d', 'e'], ['f', 'g', 'h', 'i']]: batch_txns = [] for name in names: txn = create_transaction(name=name, private_key=private_key, public_key=public_key) batch_txns.append(txn) txns.append(txn) batch = create_batch(transactions=batch_txns, private_key=private_key, public_key=public_key) self.scheduler.add_batch(batch) self.scheduler.finalize() iterable1 = iter(self.scheduler) for txn in txns: scheduled_txn_info = next(iterable1) self.assertFalse(self.scheduler.complete(block=False)) self.scheduler.set_transaction_execution_result( txn.header_signature, False, None) self.assertTrue(self.scheduler.complete(block=False)) with self.assertRaises(StopIteration): next(iterable1)
def __init__(self, transaction_executor, block_cache, state_view_factory, block_sender, batch_sender, squash_handler, chain_head, identity_signing_key, data_dir): """ Initialize the BlockPublisher object Args: transaction_executor (:obj:`TransactionExecutor`): A TransactionExecutor instance. block_cache (:obj:`BlockCache`): A BlockCache instance. state_view_factory (:obj:`StateViewFactory`): StateViewFactory for read-only state views. block_sender (:obj:`BlockSender`): The BlockSender instance. batch_sender (:obj:`BatchSender`): The BatchSender instance. squash_handler (function): Squash handler function for merging contexts. chain_head (:obj:`BlockWrapper`): The initial chain head. identity_signing_key (str): Private key for signing blocks """ self._lock = RLock() self._candidate_block = None # the next block in potentia self._consensus = None self._block_cache = block_cache self._state_view_factory = state_view_factory self._transaction_executor = transaction_executor self._block_sender = block_sender self._batch_publisher = BatchPublisher(identity_signing_key, batch_sender) self._pending_batches = [] # batches we are waiting for validation, # arranged in the order of batches received. self._committed_txn_cache = TransactionCache( self._block_cache.block_store) # Look-up cache for transactions that are committed in the current # chain. Cache is used here so that we can support opportunistically # building on top of a block we published. As well as hold the state # of the transactions already added to the candidate block. self._scheduler = None self._chain_head = chain_head # block (BlockWrapper) self._squash_handler = squash_handler self._identity_signing_key = identity_signing_key self._identity_public_key = signing.encode_pubkey( signing.generate_pubkey(self._identity_signing_key), "hex") self._data_dir = data_dir
def do_init(args, config): username = config.get('DEFAULT', 'username') if args.username is not None: username = args.username url = config.get('DEFAULT', 'url') if args.url is not None: url = args.url config.set('DEFAULT', 'username', username) print("set username: {}".format(username)) config.set('DEFAULT', 'url', url) print("set url: {}".format(url)) save_config(config) priv_filename = config.get('DEFAULT', 'key_file') if priv_filename.endswith(".priv"): addr_filename = priv_filename[0:-len(".priv")] + ".addr" else: addr_filename = priv_filename + ".addr" if not os.path.exists(priv_filename): try: if not os.path.exists(os.path.dirname(priv_filename)): os.makedirs(os.path.dirname(priv_filename)) privkey = signing.generate_privkey() pubkey = signing.generate_pubkey(privkey) addr = signing.generate_identifier(pubkey) with open(priv_filename, "w") as priv_fd: print("writing file: {}".format(priv_filename)) priv_fd.write(privkey) priv_fd.write("\n") with open(addr_filename, "w") as addr_fd: print("writing file: {}".format(addr_filename)) addr_fd.write(addr) addr_fd.write("\n") except IOError as ioe: raise SupplyChainException("IOError: {}".format(str(ioe)))