Skip to content

Latest commit

 

History

History
221 lines (189 loc) · 6.66 KB

update-an-account.md

File metadata and controls

221 lines (189 loc) · 6.66 KB

Update an Account

AccountUpdateTransaction() updates/changes the properties for an existing account. Any null field is ignored (left unchanged). Any of the following properties can be updated for an account:

  • Key(s)
  • Auto Renew Period
  • Expiration Time
  • Send Record Threshold
  • Receive Record Threshold
  • Proxy Account

{% hint style="danger" %} The account must be signed by the old key(s) and new key(s) when updating the keys of an account. {% endhint %}

Constructor Description
AccountUpdateTransaction() Initializes the AccountUpdateTransaction object
new AccountUpdateTransaction()

Basic

Method Type Description
setAccountId(<accountId>) AccountId

The ID of the account to update in this transaction

default: None

setKey(<publicKey>) Ed25519 The public key to set the account to

setAutoRenewPeriod

(<autoRenewPeriod>)

Duration

The period of time in which the account will auto-renew in seconds. Duration type is in seconds. For example, one hour would result in the input value of 3600 seconds

default: None

setExpirationTime

(<expirationTime>)

Instant

The new expiration time to extend to.

default: None

setSendRecordThreshold

(<sendRecordThreshold>)

Hbar/long Set the threshold for generating records when sending currency, in tinybar.

setReceiveRecordThreshold

(<receiveRecordThreshold)

Hbar/long

Creates a record for any transaction that deposits more than x value of tinybars.

default: None

setProxyAccount(<accountId>) AccountId

ID of account to which this account should be proxy staked.

default: None

Example:

{% tabs %} {% tab title="Java" %}

Transaction transaction = new AccountUpdateTransaction()
     .setAccountId(accountId)
     .setKey(newKey.getPublicKey())
     // Sign with the previous key and the new key
     .build(client)
     .sign(originalKey)
     .sign(newKey);

System.out.println("transaction ID: " + transaction.id);

transaction.execute(client);
// (important!) wait for the transaction to complete by querying the receipt
transaction.getReceipt(client);

// Now we fetch the account information to check if the key was changed
System.out.println(" :: getAccount and check our current key");

AccountInfo info = client.getAccount(accountId);

System.out.println("key = " + info.key);

{% endtab %}

{% tab title="JavaScript" %}

const { Client, Ed25519PrivateKey, AccountCreateTransaction, AccountUpdateTransaction, AccountInfoQuery, AccountId} = require("@hashgraph/sdk");
require("dotenv").config();

async function main() {
    const operatorPrivateKey = process.env.OPERATOR_KEY;
    const operatorAccount = process.env.OPERATOR_ID;

    if (operatorPrivateKey == null || operatorAccount == null) {
        throw new Error("environment variables OPERATOR_KEY and OPERATOR_ID must be present");
    }

    const client = new Client({
        network: { "0.testnet.hedera.com:50211": "0.0.3" },
        operator: {
            account: operatorAccount,
            privateKey: operatorPrivateKey
        }
    });

    // First, we create a new account so we don't affect our account

    const privateKey = await Ed25519PrivateKey.generate();
    const publicKey = await privateKey.publicKey;

    console.log(`private = ${privateKey.toString()}`);
    console.log(`public = ${privateKey.publicKey.toString()}`);

    const transactionId = await new AccountCreateTransaction()
        .setKey(publicKey)
        .setInitialBalance(0)
        .execute(client);

    // Get the account ID for the new account
    const transactionReceipt = await transactionId.getReceipt(client);
    const accountId = transactionReceipt.getAccountId();
    console.log(`account = ${accountId}`);

    // Create new keys
    const newPrivateKey = await Ed25519PrivateKey.generate();
    const newPublicKey = newPrivateKey.publicKey;

    console.log(`:: update keys of account ${accountId}`)
    console.log(`setKey = ${newPublicKey}`)

    //Update the key on the account
    const updateTransaction = await new AccountUpdateTransaction()
        .setAccountId(accountId)
        .setKey(newPublicKey) // The new public key to update the account with
        .build(client)
        .sign(privateKey) // Sign with the original private key on account
        .sign(newPrivateKey) // Sign with new private key on the account
        .execute(client);

    console.log(`transactionId = ${updateTransaction}`)

    // (important!) wait for the transaction to complete by querying the receipt
    await updateTransaction.getReceipt(client);

    console.log(`:: get account info and check our current key`);

   // Now we fetch the account information to check if the key was changed
    const acctInfo = await new AccountInfoQuery()
        .setAccountId(accountId)
        .execute(client); 

    console.log(`key = ${acctInfo.key}`)
}
main();

{% endtab %} {% endtabs %}