Hive Developer logo

Hive Developer Portal

PY: Password Key Change

How to change your accounts password and keys

Full, runnable src of Password Key Change can be downloaded as part of: tutorials/python (or download just this tutorial: devportal-master-tutorials-python-34_password_key_change.zip).

In this tutorial we will explain and show you how to change your account password and keys on the Hive blockchain using the beem library.

Intro

The beem library has a built-in function to update your account details on the blockchain. We are using the Account_update operation and TransactionBuilder to make these changes. We first get the existing keys from your account then recreate these from your new password. Once these have been created using your new password we commit them to the blockchain.

Steps

  1. App setup - Library install and import. Connection to production
  2. User input - Input user and limit parameters
  3. Connect to the blockchain - Connect to the blockchain using the parameters collected from the user
  4. Configure new keys - Setup the new json object that will have the new keys derived from your new password
  5. Commit changes to blockchain - Commit the account update to the blockchain

1. App setup

In this tutorial we use the following packages:

We import the libraries and get parameters from the user.

import getpass
import json
from beem import Hive
from beem.account import Account
from beemgraphenebase.account import PasswordKey, PrivateKey
from beem.transactionbuilder import TransactionBuilder
from beembase.operations import Account_update

2. User input

You will first be asked for the account that we will be modifying the password for. You will then be prompted to enter your existing password as well as your new password that we will update your account with.

account = input('Account: ')
old_password = getpass.getpass('Current password: ')
new_password = getpass.getpass('New password: ')

if getpass.getpass('Confirm New password: ') != new_password:
  print('New password did not confirm.')
  exit()

3. Connect to the blockchain

From the parameters that have been collected we will generate the private key for the account and connect to the Hive blockchain.

wif_old_owner_key = str(
  PasswordKey(account, old_password, "owner").get_private_key()
)

client = Hive('http://127.0.0.1:8090', keys=[wif_old_owner_key])

account = Account(account, blockchain_instance=client)

4. Configure new keys

We will now generate new keys for each role using the new password as well as re-create the json that will be committed to the Hive blockchain. We generate new keys using the new password for each of these roles.

new_public_keys = {}

for role in ["owner", "active", "posting", "memo"]:
  private_key = PasswordKey(account.name, new_password, role).get_private_key()
  new_public_keys[role] = str(private_key.pubkey)

new_data = {
  "account": account.name,
  "json_metadata": json.dumps(account.json_metadata),
  "owner": {
    "key_auths": [
      [new_public_keys["owner"], 1]
    ],
    "account_auths": account['owner']['account_auths'],
    "weight_threshold": 1
  },
  "active": {
    "key_auths": [
      [new_public_keys["active"], 1]
    ],
    "account_auths": account['active']['account_auths'],
    "weight_threshold": 1
  },
  "posting": {
    "key_auths": [
      [new_public_keys["posting"], 1]
    ],
    "account_auths": account['posting']['account_auths'],
    "weight_threshold": 1
  },
  "memo_key": new_public_keys["memo"]
}

print("New data:")
print(new_data)

5. Commit changes to blockchain

The tx.appendOps(Account_update(**new_data)) call creates the operation that will be committed to the blockchain using the new json object we have created.

tx = TransactionBuilder(blockchain_instance=client)
tx.appendOps(Account_update(**new_data))

tx.appendWif(wif_old_owner_key)
signed_tx = tx.sign()
broadcast_tx = tx.broadcast(trx_id=True)

print("Account updated successfully: " + str(broadcast_tx))

If you update your password and attempt to update it again to quickly you will receive the following error.

Assert Exception:_db.head_block_time() - account_auth.last_owner_update > HIVE_OWNER_UPDATE_LIMIT: Owner authority can only be updated once an hour.

You will need to wait at least an hour before attempting this again.

To Run the tutorial

Before running this tutorial, launch your local testnet, with port 8090 mapped locally to the docker container:

docker run -d -p 8090:8090 inertia/tintoy:latest

For details on running a local testnet, see: Setting Up a Testnet

  1. review dev requirements
  2. git clone https://gitlab.syncad.com/hive/devportal.git
  3. cd devportal/tutorials/python/34_password_key_change
  4. pip install -r requirements.txt
  5. python index.py
  6. After a few moments, you should see a prompt for input in terminal screen.