def test_new_auth_key_from_public_key():
    key_bytes = bytes.fromhex("447fc3be296803c2303951c7816624c7566730a5cc6860a4a1bd3c04731569f5")
    public_key = Ed25519PublicKey.from_public_bytes(key_bytes)
    auth_key = AuthKey.from_public_key(public_key)
    assert auth_key.hex() == "459c77a38803bd53f3adee52703810e3a74fd7c46952c497e75afb0a7932586d"
    assert auth_key.prefix().hex() == "459c77a38803bd53f3adee52703810e3"
    assert auth_key.account_address().to_hex() == "a74fd7c46952c497e75afb0a7932586d"
def main():
    # connect to testnet
    client = testnet.create_client()

    # generate private key
    private_key = Ed25519PrivateKey.generate()
    # generate auth key
    auth_key = AuthKey.from_public_key(private_key.public_key())
    print(
        f"Generated address: {utils.account_address_hex(auth_key.account_address())}"
    )

    # create new account
    faucet = testnet.Faucet(client)
    testnet.Faucet.mint(faucet, auth_key.hex(), 100_000_000, CURRENCY)

    # get account events key
    account = client.get_account(auth_key.account_address())
    events_key = account.received_events_key

    # start minter to demonstrates events creation
    start_minter(client, auth_key)

    # demonstrates events subscription
    subscribe(client, events_key)
def main():
    # generate private key
    private_key = Ed25519PrivateKey.generate()
    # generate auth key
    auth_key = AuthKey.from_public_key(private_key.public_key())

    print(
        f"Generated address: {utils.account_address_hex(auth_key.account_address())}"
    )
    print(f"Auth Key (HEX): {auth_key.hex()}")
    print(
        f"Public key (HEX): {utils.public_key_bytes(private_key.public_key()).hex()}"
    )
Exemple #4
0
def main():
    # connect to testnet
    client = testnet.create_client()

    # generate private key for new account
    private_key = Ed25519PrivateKey.generate()

    # generate auth key for new account
    auth_key = AuthKey.from_public_key(private_key.public_key())
    print(
        f"Generated address: {utils.account_address_hex(auth_key.account_address())}"
    )

    # use mint to create new account
    faucet = testnet.Faucet(client)
    testnet.Faucet.mint(faucet, auth_key.hex(), 192000000, "XUS")
def main():
    # connect to testnet
    client = testnet.create_client()

    # generate private key
    private_key = Ed25519PrivateKey.generate()
    # generate auth key
    auth_key = AuthKey.from_public_key(private_key.public_key())
    print(
        f"Generated address: {utils.account_address_hex(auth_key.account_address())}"
    )

    # create account
    faucet = testnet.Faucet(client)
    testnet.Faucet.mint(faucet, auth_key.hex(), 100_000_000, "XUS")

    # get account information
    account = client.get_account(auth_key.account_address())

    print("Account info:")
    print(account)
Exemple #6
0
def main():
    # generate private key
    private_key = Ed25519PrivateKey.generate()
    # generate auth key
    auth_key = AuthKey.from_public_key(private_key.public_key())

    # create intent identifier
    account_identifier = identifier.encode_account(
        utils.account_address_hex(auth_key.account_address()), None,
        identifier.TDM)
    encoded_intent_identifier = identifier.encode_intent(
        account_identifier, "XUS", 10000000)
    print(f"Encoded IntentIdentifier: {encoded_intent_identifier}")

    # deserialize IntentIdentifier
    intent_identifier = identifier.decode_intent(encoded_intent_identifier,
                                                 identifier.TDM)

    print(
        f"Account (HEX) from intent: {utils.account_address_hex(intent_identifier.account_address)}"
    )
    print(f"Amount from intent: {intent_identifier.amount}")
    print(f"Currency from intent: {intent_identifier.currency_code}")
Exemple #7
0
 def auth_key(self) -> AuthKey:
     return AuthKey.from_public_key(
         Ed25519PublicKey.from_public_bytes(self.public_key_bytes()))
    private_key = args.private_key
    auth_key = args.auth_key
    refill_amount_human = args.amount
    threshold_amount_human = args.threshold
    currencies = args.currencies
    network_chainid = args.network_chainid
    network_json_rpc_url = args.network_json_rpc_url
    sleep_interval = args.sleep_interval

if auth_key is None:
    print("Must get an authentication key to watch!")
    sys.exit(1)

diem_client = jsonrpc.Client(network_json_rpc_url)

watched_account_auth_key = AuthKey(bytes.fromhex(auth_key))
watched_account_addr = watched_account_auth_key.account_address()
watched_account_addr_hex = utils.account_address_hex(watched_account_addr)

refill_amount = int(refill_amount_human) * 1_000_000  # to microdiem
threshold_amount = int(threshold_amount_human) * 1_000_000  # to microdiem
sleep_interval = float(sleep_interval)

while True:
    print(f'current datetime: {datetime.now().isoformat().replace("T", " ")}')

    for currency in currencies:
        currency_balance = next(
            b.amount
            for b in diem_client.get_account(watched_account_addr).balances)
        print(f"{currency} balance is {currency_balance} ", )
def main():
    print("#1 connect to testnet")
    client = testnet.create_client()

    print("#2 Generate Keys")
    # generate private key for sender account
    sender_private_key = Ed25519PrivateKey.generate()
    # generate auth key for sender account
    sender_auth_key = AuthKey.from_public_key(sender_private_key.public_key())
    print(
        f"Generated sender address: {utils.account_address_hex(sender_auth_key.account_address())}"
    )

    print("#3 Create account")
    faucet = testnet.Faucet(client)
    testnet.Faucet.mint(faucet, sender_auth_key.hex(), 100000000, CURRENCY)

    print("#4 Get account information")
    sender_account = client.get_account(sender_auth_key.account_address())

    events_key = sender_account.received_events_key
    print("#5 Start event listener")
    get_events_example.subscribe(client, events_key)

    print("#6 Add money to account")
    faucet = testnet.Faucet(client)
    testnet.Faucet.mint(faucet, sender_auth_key.hex(), 10000000, CURRENCY)

    print("#7 Generate Keys")
    # generate private key for receiver account
    receiver_private_key = Ed25519PrivateKey.generate()
    # generate auth key for receiver account
    receiver_auth_key = AuthKey.from_public_key(
        receiver_private_key.public_key())
    print(
        f"Generated receiver address: {utils.account_address_hex(receiver_auth_key.account_address())}"
    )

    print("#8 Create second account")
    faucet = testnet.Faucet(client)
    testnet.Faucet.mint(faucet, receiver_auth_key.hex(), 1000000, CURRENCY)

    print("#9 Generate IntentIdentifier")
    account_identifier = identifier.encode_account(
        utils.account_address_hex(receiver_auth_key.account_address()), None,
        identifier.TDM)
    encoded_intent_identifier = identifier.encode_intent(
        account_identifier, CURRENCY, 10000000)
    print(f"Encoded IntentIdentifier: {encoded_intent_identifier}")

    print("#10 Deserialize IntentIdentifier")
    intent_identifier = identifier.decode_intent(encoded_intent_identifier,
                                                 identifier.TDM)
    print(
        f"Account (HEX) from intent: {utils.account_address_hex(intent_identifier.account_address)}"
    )
    print(f"Amount from intent: {intent_identifier.amount}")
    print(f"Currency from intent: {intent_identifier.currency_code}")

    print("#11 Peer 2 peer transaction")
    # create script
    script = stdlib.encode_peer_to_peer_with_metadata_script(
        currency=utils.currency_code(intent_identifier.currency_code),
        payee=intent_identifier.account_address,
        amount=intent_identifier.amount,
        metadata=b'',  # no requirement for metadata and metadata signature
        metadata_signature=b'',
    )
    # create transaction
    raw_transaction = diem_types.RawTransaction(
        sender=sender_auth_key.account_address(),
        sequence_number=sender_account.sequence_number,
        payload=diem_types.TransactionPayload__Script(script),
        max_gas_amount=1_000_000,
        gas_unit_price=0,
        gas_currency_code=CURRENCY,
        expiration_timestamp_secs=int(time.time()) + 30,
        chain_id=testnet.CHAIN_ID,
    )
    # sign transaction
    signature = sender_private_key.sign(
        utils.raw_transaction_signing_msg(raw_transaction))
    public_key_bytes = utils.public_key_bytes(sender_private_key.public_key())

    signed_txn = utils.create_signed_transaction(raw_transaction,
                                                 public_key_bytes, signature)
    # submit transaction
    client.submit(signed_txn)
    # wait for transaction
    client.wait_for_transaction(signed_txn)
Exemple #10
0
def main():
    # connect to testnet
    client = testnet.create_client()

    # generate private key for sender account
    sender_private_key = Ed25519PrivateKey.generate()
    # generate auth key for sender account
    sender_auth_key = AuthKey.from_public_key(sender_private_key.public_key())
    print(
        f"Generated sender address: {utils.account_address_hex(sender_auth_key.account_address())}"
    )

    # create sender account
    faucet = testnet.Faucet(client)
    testnet.Faucet.mint(faucet, sender_auth_key.hex(), 100000000, "XUS")

    # get sender account
    sender_account = client.get_account(sender_auth_key.account_address())

    # generate private key for receiver account
    receiver_private_key = Ed25519PrivateKey.generate()
    # generate auth key for receiver account
    receiver_auth_key = AuthKey.from_public_key(
        receiver_private_key.public_key())
    print(
        f"Generated receiver address: {utils.account_address_hex(receiver_auth_key.account_address())}"
    )

    # create receiver account
    faucet = testnet.Faucet(client)
    testnet.Faucet.mint(faucet, receiver_auth_key.hex(), 10000000, CURRENCY)

    # create script
    script = stdlib.encode_peer_to_peer_with_metadata_script(
        currency=utils.currency_code(CURRENCY),
        payee=receiver_auth_key.account_address(),
        amount=10000000,
        metadata=b'',  # no requirement for metadata and metadata signature
        metadata_signature=b'',
    )
    # create transaction
    raw_transaction = diem_types.RawTransaction(
        sender=sender_auth_key.account_address(),
        sequence_number=sender_account.sequence_number,
        payload=diem_types.TransactionPayload__Script(script),
        max_gas_amount=1_000_000,
        gas_unit_price=0,
        gas_currency_code=CURRENCY,
        expiration_timestamp_secs=int(time.time()) + 30,
        chain_id=testnet.CHAIN_ID,
    )
    # sign transaction
    signature = sender_private_key.sign(
        utils.raw_transaction_signing_msg(raw_transaction))
    public_key_bytes = utils.public_key_bytes(sender_private_key.public_key())

    signed_txn = utils.create_signed_transaction(raw_transaction,
                                                 public_key_bytes, signature)
    # submit transaction
    client.submit(signed_txn)
    # wait for transaction
    client.wait_for_transaction(signed_txn)