How to safely use your Private keys with Beem

Words
233
Reading
2 min
Listen
Play
6y

image.png

Beem is a powerful tool by holger80@holger80 that you can use for interacting with many different Graphene Chains, including Hive, Whaleshares and Smoke.

But powerful tools need to be used with caution, and this is absolutly no exception when it comes to cryptocurrency chains.

With Beem you can interact with an accounts wallet.

from beem import Hive
hive.wallet.unlock("mySecretPassword")
hive.wallet.addPrivateKey("myPrivateActiveKey")

# My totally unsecure script code to follow :(

DO NOT USE YOUR KEYS DIRECTLY!

As the example above can be seen as something very easy to make for interactive with your own wallet in Python, just imagine if your script came in other users hands? Maybe your sever got broken into, or you even accidently pushed or shared your script without thinking about your own keys being in there?

Bummer :)

HOW TO USE YOUR KEYS

Of course there are much better ways to deal with this type of sensitive information, and that is to input the keys only when needed.

from beem import Hive
from beem import exceptions
import getpass

hive_nodeList = NodeList()
hive_nodeList.update_nodes()
hive_nodes = hive_nodeList.get_hive_nodes()
hive = Hive(node=hive_nodes)

def checkHiveWallet():
    try:
        hive.wallet.getActiveKeysForAccount("myAccount")
    except exceptions.MissingKeyError:
        key = getpass.getpass('Please Supply the Hive Wallet Active Key:')
        try:
            hive.wallet.addPrivateKey(key)
        except exceptions.InvalidWifError:
            print("Invalid Key! Please try again.")
            checkHiveWallet()  

def unlockWallet():

    walletPassword = getpass.getpass('Wallet Password:')
    try:
        hive.wallet.unlock(walletPassword)
    except beemstorage.exceptions.WrongMasterPasswordException:
        print('Invalid Password, please try again!')
        unlockWallet()

if __name__ == "__main__":
    
    unlockWallet()
    checkHiveWallet()

    # ...... continue your awsome code here :)

With this little script you will

  1. Be prompted for your wallet password
  2. Make sure it's a valid password, otherwise it will ask again
  3. Check if the wallet has the Active keys for the desired account (here myAccount)
  4. Ask for the keys if they are not in the wallet already

Your keys have now been added encrypted to the Beem wallet of hive.wallet without storing them inside your code, and can be used for transfers and other actions.

Happy Developing!

How to safely use your Private keys with Beem | Ecency