How to revoke a function-call access key?

Viewed 112

"Access keys are stored as account_id,public_key in a trie state." how do I revoke an function-call access key from the blockchain?

2 Answers

You can remove an access key by sending a DeleteKey transaction. For more details on transactions, please checkout this page.

Here is how you would delete an access key for example.testnet:

const { KeyPair, keyStore, connect } = require("near-api-js");

const CREDENTIALS_DIR = "~/.near-credentials";
const ACCOUNT_ID = "example.testnet";
const PUBLIC_KEY = "8hSHprDq2StXwMtNd43wDTXQYsjXcD4MJTXQYsjXcc";
const keyStore = new keyStores.UnencryptedFileSystemKeyStore(CREDENTIALS_DIR);

const config = {
  keyStore,
  networkId: "testnet",
  nodeUrl: "https://rpc.testnet.near.org",
};

deleteAccessKey(ACCOUNT_ID, PUBLIC_KEY);

async function deleteAccessKey(accountId, publicKey) {
  const near = await connect(config);
  const account = await near.account(accountId);
  await account.deleteKey(publicKey);
}

You will need to make sure you have credentials for your account stored locally to complete this function. Do this by running the following near-cli command:

near login

For more info on rotating access keys, check out this doc:

https://docs.near.org/docs/api/naj-cookbook#access-key-rotation

Related