def run(): # Create a private keys _create_fetchai_private_key(private_key_file=FETCHAI_PRIVATE_KEY_FILE_1) _create_fetchai_private_key(private_key_file=FETCHAI_PRIVATE_KEY_FILE_2) # Set up the wallets wallet_1 = Wallet({FETCHAI: FETCHAI_PRIVATE_KEY_FILE_1}) wallet_2 = Wallet({FETCHAI: FETCHAI_PRIVATE_KEY_FILE_2}) # Set up the LedgerApis ledger_apis = LedgerApis({FETCHAI: {"network": "testnet"}}, FETCHAI) # Generate some wealth _try_generate_testnet_wealth(FETCHAI, wallet_1.addresses[FETCHAI]) logger.info("Sending amount to {}".format(wallet_2.addresses.get(FETCHAI))) # Create the transaction and send it to the ledger. ledger_api = ledger_apis.apis[FETCHAI] tx_nonce = ledger_api.generate_tx_nonce( wallet_2.addresses.get(FETCHAI), wallet_1.addresses.get(FETCHAI) ) tx_digest = ledger_api.send_transaction( crypto=wallet_1.crypto_objects.get(FETCHAI), destination_address=wallet_2.addresses.get(FETCHAI), amount=1, tx_fee=1, tx_nonce=tx_nonce, ) logger.info("Transaction complete.") logger.info("The transaction digest is {}".format(tx_digest))
def run(): # Create a private key _create_fetchai_private_key() # Ensure the input and output files do not exist initially if os.path.isfile(INPUT_FILE): os.remove(INPUT_FILE) if os.path.isfile(OUTPUT_FILE): os.remove(OUTPUT_FILE) # Instantiate the builder and build the AEA # By default, the default protocol, error skill and stub connection are added builder = AEABuilder() builder.set_name("my_aea") builder.add_private_key(FETCHAI, FETCHAI_PRIVATE_KEY_FILE) builder.add_ledger_api_config(FETCHAI, {"network": "testnet"}) # Add the echo skill (assuming it is present in the local directory 'packages') builder.add_skill("./packages/fetchai/skills/echo") # Create our AEA my_aea = builder.build() # Set the AEA running in a different thread try: t = Thread(target=my_aea.start) t.start() # Wait for everything to start up time.sleep(4) # Create a message inside an envelope and get the stub connection to pass it on to the echo skill message_text = ( "my_aea,other_agent,fetchai/default:0.1.0,\x08\x01*\x07\n\x05hello," ) with open(INPUT_FILE, "w") as f: f.write(message_text) print("input message: " + message_text) # Wait for the envelope to get processed time.sleep(4) # Read the output envelope generated by the echo skill with open(OUTPUT_FILE, "r") as f: print("output message: " + f.readline()) finally: # Shut down the AEA my_aea.stop() t.join() t = None
def _verify_or_create_private_keys(aea_project_path: Path) -> None: """Verify or create private keys.""" path_to_configuration = aea_project_path / DEFAULT_AEA_CONFIG_FILE agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig) fp_read = path_to_configuration.open(mode="r", encoding="utf-8") agent_configuration = agent_loader.load(fp_read) for identifier, _value in agent_configuration.private_key_paths.read_all(): if identifier not in SUPPORTED_CRYPTOS: ValueError("Unsupported identifier in private key paths.") fetchai_private_key_path = agent_configuration.private_key_paths.read(FETCHAI) if fetchai_private_key_path is None: _create_fetchai_private_key( private_key_file=str(aea_project_path / FETCHAI_PRIVATE_KEY_FILE) ) agent_configuration.private_key_paths.update(FETCHAI, FETCHAI_PRIVATE_KEY_FILE) else: try: _try_validate_fet_private_key_path( str(aea_project_path / fetchai_private_key_path), exit_on_error=False ) except FileNotFoundError: # pragma: no cover logger.error( "File {} for private key {} not found.".format( repr(fetchai_private_key_path), FETCHAI, ) ) raise ethereum_private_key_path = agent_configuration.private_key_paths.read(ETHEREUM) if ethereum_private_key_path is None: _create_ethereum_private_key( private_key_file=str(aea_project_path / ETHEREUM_PRIVATE_KEY_FILE) ) agent_configuration.private_key_paths.update( ETHEREUM, ETHEREUM_PRIVATE_KEY_FILE ) else: try: _try_validate_ethereum_private_key_path( str(aea_project_path / ethereum_private_key_path), exit_on_error=False ) except FileNotFoundError: # pragma: no cover logger.error( "File {} for private key {} not found.".format( repr(ethereum_private_key_path), ETHEREUM, ) ) raise fp_write = path_to_configuration.open(mode="w", encoding="utf-8") agent_loader.dump(agent_configuration, fp_write)
def _verify_or_create_private_keys(ctx: Context) -> None: """ Verify or create private keys. :param ctx: Context """ path = Path(DEFAULT_AEA_CONFIG_FILE) agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig) fp = path.open(mode="r", encoding="utf-8") aea_conf = agent_loader.load(fp) for identifier, _value in aea_conf.private_key_paths.read_all(): if identifier not in SUPPORTED_CRYPTOS: ValueError("Unsupported identifier in private key paths.") fetchai_private_key_path = aea_conf.private_key_paths.read(FETCHAI) if fetchai_private_key_path is None: _create_fetchai_private_key() aea_conf.private_key_paths.update(FETCHAI, FETCHAI_PRIVATE_KEY_FILE) else: try: _try_validate_fet_private_key_path(fetchai_private_key_path) except FileNotFoundError: # pragma: no cover logger.error("File {} for private key {} not found.".format( repr(fetchai_private_key_path), FETCHAI, )) sys.exit(1) ethereum_private_key_path = aea_conf.private_key_paths.read(ETHEREUM) if ethereum_private_key_path is None: _create_ethereum_private_key() aea_conf.private_key_paths.update(ETHEREUM, ETHEREUM_PRIVATE_KEY_FILE) else: try: _try_validate_ethereum_private_key_path(ethereum_private_key_path) except FileNotFoundError: # pragma: no cover logger.error("File {} for private key {} not found.".format( repr(ethereum_private_key_path), ETHEREUM, )) sys.exit(1) # update aea config path = Path(DEFAULT_AEA_CONFIG_FILE) fp = path.open(mode="w", encoding="utf-8") agent_loader.dump(aea_conf, fp) ctx.agent_config = aea_conf
def test__create_fetchai_private_key_positive(self, *mocks): """Test _create_ethereum_private_key positive result.""" _create_fetchai_private_key()
def run(): # Create a private key _create_fetchai_private_key(private_key_file=FETCHAI_PRIVATE_KEY_FILE_1) # Instantiate the builder and build the AEA # By default, the default protocol, error skill and stub connection are added builder = AEABuilder() builder.set_name("my_aea") builder.add_private_key(FETCHAI, FETCHAI_PRIVATE_KEY_FILE_1) builder.add_ledger_api_config(FETCHAI, {"network": "testnet"}) # Create our AEA my_aea = builder.build() # Generate some wealth for the default address _try_generate_testnet_wealth(FETCHAI, my_aea.identity.address) # add a simple skill with handler skill_context = SkillContext(my_aea.context) skill_config = SkillConfig(name="simple_skill", author="fetchai", version="0.1.0") tx_handler = TransactionHandler(skill_context=skill_context, name="transaction_handler") simple_skill = Skill(skill_config, skill_context, handlers={tx_handler.name: tx_handler}) my_aea.resources.add_skill(simple_skill) # create a second identity _create_fetchai_private_key(private_key_file=FETCHAI_PRIVATE_KEY_FILE_2) counterparty_wallet = Wallet({FETCHAI: FETCHAI_PRIVATE_KEY_FILE_2}) counterparty_identity = Identity( name="counterparty_aea", addresses=counterparty_wallet.addresses, default_address_key=FETCHAI, ) # create tx message for decision maker to process fetchai_ledger_api = my_aea.context.ledger_apis.apis[FETCHAI] tx_nonce = fetchai_ledger_api.generate_tx_nonce( my_aea.identity.address, counterparty_identity.address) tx_msg = TransactionMessage( performative=TransactionMessage.Performative.PROPOSE_FOR_SETTLEMENT, skill_callback_ids=[skill_config.public_id], tx_id="transaction0", tx_sender_addr=my_aea.identity.address, tx_counterparty_addr=counterparty_identity.address, tx_amount_by_currency_id={"FET": -1}, tx_sender_fee=1, tx_counterparty_fee=0, tx_quantities_by_good_id={}, ledger_id=FETCHAI, info={"some_info_key": "some_info_value"}, tx_nonce=tx_nonce, ) my_aea.context.decision_maker_message_queue.put_nowait(tx_msg) # Set the AEA running in a different thread try: logger.info("STARTING AEA NOW!") t = Thread(target=my_aea.start) t.start() # Let it run long enough to interact with the weather station time.sleep(20) finally: # Shut down the AEA logger.info("STOPPING AEA NOW!") my_aea.stop() t.join()
def run(): # Create a private key _create_fetchai_private_key() # Set up the wallet, identity, oef connection, ledger and (empty) resources wallet = Wallet({FETCHAI: FETCHAI_PRIVATE_KEY_FILE}) identity = Identity("my_aea", address=wallet.addresses.get(FETCHAI)) oef_connection = OEFConnection(address=identity.address, oef_addr=HOST, oef_port=PORT) ledger_apis = LedgerApis({}, FETCHAI) resources = Resources() # create the AEA my_aea = AEA( identity, [oef_connection], wallet, ledger_apis, resources, # stub_connection, ) # Add the default protocol (which is part of the AEA distribution) default_protocol = Protocol.from_dir( os.path.join(AEA_DIR, "protocols", "default")) resources.add_protocol(default_protocol) # Add the oef protocol (which is a package) oef_protocol = Protocol.from_dir( os.path.join( os.getcwd(), "packages", "fetchai", "protocols", "oef_search", )) resources.add_protocol(oef_protocol) # Add the fipa protocol (which is a package) fipa_protocol = Protocol.from_dir( os.path.join( os.getcwd(), "packages", "fetchai", "protocols", "fipa", )) resources.add_protocol(fipa_protocol) # Add the error and weather_station skills error_skill = Skill.from_dir(os.path.join(AEA_DIR, "skills", "error"), my_aea.context) weather_skill = Skill.from_dir( os.path.join(ROOT_DIR, "packages", "fetchai", "skills", "weather_client"), my_aea.context, ) strategy = cast(Strategy, weather_skill.models.get("strategy")) strategy.is_ledger_tx = False strategy.max_buyer_tx_fee = 100 strategy.max_row_price = 40 for skill in [error_skill, weather_skill]: resources.add_skill(skill) # Set the AEA running in a different thread try: logger.info("STARTING AEA NOW!") t = Thread(target=my_aea.start) t.start() # Let it run long enough to interact with the weather station time.sleep(25) finally: # Shut down the AEA logger.info("STOPPING AEA NOW!") my_aea.stop() t.join()
def run(): # Create a private keys _create_fetchai_private_key(private_key_file=FETCHAI_PRIVATE_KEY_FILE_1) _create_fetchai_private_key(private_key_file=FETCHAI_PRIVATE_KEY_FILE_2) # Set up the wallets wallet_1 = Wallet({FETCHAI: FETCHAI_PRIVATE_KEY_FILE_1}) wallet_2 = Wallet({FETCHAI: FETCHAI_PRIVATE_KEY_FILE_2}) # Generate some wealth _try_generate_testnet_wealth(FETCHAI, wallet_1.addresses[FETCHAI]) # Set up the LedgerApis ledger_apis = LedgerApis({FETCHAI: {"network": "testnet"}}, FETCHAI) tx_handler = TransactionHandler(skill_context="skill_context", name="fake_skill") resources = Resources() resources.handler_registry.register( ( PublicId.from_str("fetchai/fake_skill:0.1.0"), PublicId.from_str("fetchai/internal:0.1.0"), ), tx_handler, ) stub_connection = StubConnection(input_file_path=INPUT_FILE, output_file_path=OUTPUT_FILE) identity_1 = Identity( name="my_aea", address=wallet_1.addresses.get(FETCHAI), default_address_key=FETCHAI, ) identity_2 = Identity( name="my_aea_2", address=wallet_2.addresses.get(FETCHAI), default_address_key=FETCHAI, ) # create the AEA my_aea = AEA(identity_1, [stub_connection], wallet_1, ledger_apis, resources) ledger_api = ledger_apis.apis[FETCHAI] tx_nonce = ledger_api.generate_tx_nonce(identity_1.addresses.get(FETCHAI), identity_2.addresses.get(FETCHAI)) tx_msg = TransactionMessage( performative=TransactionMessage.Performative.PROPOSE_FOR_SETTLEMENT, skill_callback_ids=[PublicId("fetchai", "fake_skill", "0.1.0")], tx_id="transaction0", tx_sender_addr=identity_1.addresses.get(FETCHAI), tx_counterparty_addr=identity_2.addresses.get(FETCHAI), tx_amount_by_currency_id={"FET": -1}, tx_sender_fee=1, tx_counterparty_fee=0, tx_quantities_by_good_id={}, ledger_id=FETCHAI, info={"some_info_key": "some_info_value"}, tx_nonce=tx_nonce, ) my_aea.context.decision_maker_message_queue.put_nowait(tx_msg) # Set the AEA running in a different thread try: logger.info("STARTING AEA NOW!") t = Thread(target=my_aea.start) t.start() # Let it run long enough to interact with the weather station time.sleep(20) finally: # Shut down the AEA logger.info("STOPPING AEA NOW!") my_aea.stop() t.join()
def run(): # Create a private key _create_fetchai_private_key() # Ensure the input and output files do not exist initially if os.path.isfile(INPUT_FILE): os.remove(INPUT_FILE) if os.path.isfile(OUTPUT_FILE): os.remove(OUTPUT_FILE) # Set up the Wallet, stub connection, ledger and (empty) resources wallet = Wallet({FETCHAI: FETCHAI_PRIVATE_KEY_FILE}) stub_connection = StubConnection( input_file_path=INPUT_FILE, output_file_path=OUTPUT_FILE ) ledger_apis = LedgerApis({"fetchai": {"network": "testnet"}}, "fetchai") resources = Resources() # Create an identity identity = Identity( name="my_aea", address=wallet.addresses.get(FETCHAI), default_address_key=FETCHAI, ) # Create our AEA my_aea = AEA(identity, [stub_connection], wallet, ledger_apis, resources) # Add the default protocol (which is part of the AEA distribution) default_protocol = Protocol.from_dir(os.path.join(AEA_DIR, "protocols", "default")) resources.add_protocol(default_protocol) # Add the error skill (from the local packages dir) and the echo skill (which is part of the AEA distribution) echo_skill = Skill.from_dir( os.path.join(ROOT_DIR, "packages", "fetchai", "skills", "echo"), my_aea.context, ) resources.add_skill(echo_skill) error_skill = Skill.from_dir( os.path.join(AEA_DIR, "skills", "error"), my_aea.context ) resources.add_skill(error_skill) # Set the AEA running in a different thread try: t = Thread(target=my_aea.start) t.start() # Wait for everything to start up time.sleep(4) # Create a message inside an envelope and get the stub connection to pass it on to the echo skill message_text = ( "my_aea,other_agent,fetchai/default:0.1.0,\x08\x01*\x07\n\x05hello" ) with open(INPUT_FILE, "w") as f: f.write(message_text) print("input message: " + message_text) # Wait for the envelope to get processed time.sleep(4) # Read the output envelope generated by the echo skill with open(OUTPUT_FILE, "r") as f: print("output message: " + f.readline()) finally: # Shut down the AEA my_aea.stop() t.join() t = None
def _verify_or_create_private_keys(ctx: Context) -> None: """ Verify or create private keys. :param ctx: Context """ path = Path(DEFAULT_AEA_CONFIG_FILE) agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig) fp = open(str(path), mode="r", encoding="utf-8") aea_conf = agent_loader.load(fp) for identifier, value in aea_conf.private_key_paths.read_all(): if identifier not in SUPPORTED_CRYPTOS: ValueError("Unsupported identifier in private key paths.") default_private_key_config = aea_conf.private_key_paths.read(DEFAULT) if default_private_key_config is None: _create_default_private_key() default_private_key_config = PrivateKeyPathConfig( DEFAULT, DEFAULT_PRIVATE_KEY_FILE) aea_conf.private_key_paths.create(default_private_key_config.ledger, default_private_key_config) else: default_private_key_config = cast(PrivateKeyPathConfig, default_private_key_config) try: _try_validate_private_key_pem_path(default_private_key_config.path) except FileNotFoundError: logger.error("File {} for private key {} not found.".format( repr(default_private_key_config.path), default_private_key_config.ledger)) sys.exit(1) fetchai_private_key_config = aea_conf.private_key_paths.read(FETCHAI) if fetchai_private_key_config is None: _create_fetchai_private_key() fetchai_private_key_config = PrivateKeyPathConfig( FETCHAI, FETCHAI_PRIVATE_KEY_FILE) aea_conf.private_key_paths.create(fetchai_private_key_config.ledger, fetchai_private_key_config) else: fetchai_private_key_config = cast(PrivateKeyPathConfig, fetchai_private_key_config) try: _try_validate_fet_private_key_path(fetchai_private_key_config.path) except FileNotFoundError: logger.error("File {} for private key {} not found.".format( repr(fetchai_private_key_config.path), fetchai_private_key_config.ledger)) sys.exit(1) ethereum_private_key_config = aea_conf.private_key_paths.read(ETHEREUM) if ethereum_private_key_config is None: _create_ethereum_private_key() ethereum_private_key_config = PrivateKeyPathConfig( ETHEREUM, ETHEREUM_PRIVATE_KEY_FILE) aea_conf.private_key_paths.create(ethereum_private_key_config.ledger, ethereum_private_key_config) else: ethereum_private_key_config = cast(PrivateKeyPathConfig, ethereum_private_key_config) try: _try_validate_ethereum_private_key_path( ethereum_private_key_config.path) except FileNotFoundError: logger.error("File {} for private key {} not found.".format( repr(ethereum_private_key_config.path), ethereum_private_key_config.ledger)) sys.exit(1) # update aea config path = Path(DEFAULT_AEA_CONFIG_FILE) fp = open(str(path), mode="w", encoding="utf-8") agent_loader.dump(aea_conf, fp) ctx.agent_config = aea_conf