# Manage Accounts

On an OCAP-powered blockchain, an account is fundamentally a state associated with an address, controlled by a pair of cryptographic keys (a public key and a private key). A key concept to understand is that accounts are created implicitly. An account officially exists on-chain as soon as it receives its first incoming transaction, such as a token or asset transfer. There is no explicit `createAccount` transaction.

This guide focuses on a critical account management operation: migrating an account to a new set of keys. This is an essential security feature that allows you to change the ownership of an account without altering its address, balance, or assets.


## Migrate an Account to New Keys

The `migrateAccount` function allows you to associate an existing account address with a new public key. This is useful in several scenarios, such as:

* **Key Compromise**: If you suspect your private key has been exposed, you can migrate the account to a new, secure key pair to prevent unauthorized access.
* **Upgrading Security**: You might want to migrate from an older key type to a newer, more secure one.

This operation effectively transfers control of the account while preserving its history and state on the blockchain.

### Parameters

<x-field-group>
  <x-field data-name="from" data-type="WalletObject" data-required="true">
    <x-field-desc markdown>The wallet object representing the current account that is being migrated. This wallet must sign the transaction to prove ownership.</x-field-desc>
  </x-field>
  <x-field data-name="to" data-type="WalletObject" data-required="true">
    <x-field-desc markdown>The new wallet object to which control will be transferred. The public key from this wallet will become the new authority for the account address.</x-field-desc>
  </x-field>
</x-field-group>

### Returns

The `migrateAccount` method returns a Promise that resolves to the transaction hash as a string.

### Example

Here's how to migrate an account from an old wallet to a new one.

```javascript Migrating an Account icon=logos:javascript
import Client from '@ocap/client';
import { fromRandom } from '@ocap/wallet';

const client = new Client('https://beta.abtnetwork.io/api');

// Assume oldWallet is the currently authorized wallet for the account
const oldWallet = fromRandom(); 

// Create a new wallet with a new set of keys
const newWallet = fromRandom();

async function migrate() {
  try {
    // Before migrating, you would typically fund the oldWallet's address.
    // For this example, we'll assume it has enough funds to pay for the transaction fee.
    // You can get test tokens from https://faucet.abtnetwork.io/
    console.log(`Migrating account from ${oldWallet.address} to new keys associated with ${newWallet.address}`);

    const hash = await client.migrateAccount({
      from: oldWallet,
      to: newWallet,
    });

    console.log('Account migration transaction sent successfully!');
    console.log('Transaction Hash:', hash);
    console.log(`View transaction at: https://beta.abtnetwork.io/explorer/txs/${hash}`);

  } catch (err) {
    console.error('Error migrating account:', err);
  }
}

migrate();
```

---

Now that you know how to secure your account by migrating keys, you might want to learn how to grant limited permissions to other accounts without sharing your primary keys. See the [Delegate Permissions](./how-to-guides-delegate-permissions.md) guide for more details.
