Crea Developer logo

Crea 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 the PY tutorials repository.

In this tutorial we will explain and show you how to change your account password and keys on the Crea blockchain using the crea class found within the crea-python library.

Intro

The Crea python library has a built-in function to update your account details on the blockchain. We are using the AccountUpdate and commit.finalizeOp to make these changes. The AccountUpdate function creates the operation that we will be committing to the blockchain using the commit.finalizeOp function. 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. The initial parameters we need to complete this operation are:

  1. account - The user account that we will be changing
  2. old_password - Your existing password for the account we are changing
  3. new_password - The new password we will be updating your account with

Caution: This functionality does not work on the TestNet so we will be modifying the live 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 2 package:

We import the libraries and get parameters from the user.

import crea
import creabase
from creabase.account import PasswordKey
from creabase.account import PrivateKey
from creabase import operations

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 = input('Current password: ')
new_password = input('New password: ')

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 Crea blockchain.

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

client = crea.Crea(keys=[old_owner_key])

4. Configure new keys

We will now generate new keys for each role using the new password as well as create the json that will be committed to the Crea 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, new_password, role).get_private_key()
    new_public_keys[role] = str(private_key.pubkey)

new_data = {
    "account": account,
    "json_metadata": {},
    "owner": {
        "key_auths": [
            [new_public_keys["owner"], 1]
        ],
        "account_auths": [],
        "weight_threshold": 1
    },
    "active": {
        "key_auths": [
            [new_public_keys["active"], 1]
        ],
        "account_auths": [],
        "weight_threshold": 1
    },
    "posting": {
        "key_auths": [
            [new_public_keys["posting"], 1]
        ],
        "account_auths": [],
        "weight_threshold": 1
    },
    "memo_key": new_public_keys["memo"]
}

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

5. Commit changes to blockchain

The operations.AccountUpdate(**new_data) creates the operation that will be committed to the blockchain using the new json object we have created.

Once we commit the changes to the blockchain using client.commit.finalizeOp the changes are committed and the password is updated.

op = operations.AccountUpdate(**new_data)

result = client.commit.finalizeOp(op, account, "owner")
print("Result:")
print(result)

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 > CREA_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.

That’s it!

To Run the tutorial

  1. review dev requirements
  2. clone this repo
  3. cd tutorials/33_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.