python开发steem机器人,通过json文件管理beempy创建的steem账号密码

By @dappcoder2/18/2020hive-180932

steem使用的是分级授权,因此一个steem账号,具有4个密码:"owner", "active", "posting", "memo".

这些密码都很长,靠人脑记住,是不太现实.只能靠文件存储,让机器人自己读写.

上次已经示范了如何通过beempy,现在就让这个创建过程使用json文件管理.

beem.png

json文件存储的格式(/bots.json):

{
    "nodes": [
        "https://anyx.io",
        "https://api.steemit.com"
    ],
    "steems": {
        "steemaccount": {
        }
    }
}

这里就示范beempy创建新号存入json文件(create_claimed_account_beem.py):

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 用消耗RC领取的pending claimed accounts,创建 steem 新号
# Export the key of the specified account from the local Wallet
# python create_claimed_account_beem.py creator account

import sys
import getopt
import time
import json
# from beem import Steem
from beem.account import Account
from beem.instance import shared_steem_instance
from beemgraphenebase.account import BrainKey, PasswordKey

# 必须有创建者 creator 和新号 account
try:
    creator = sys.argv[1]
    account = sys.argv[2]
except Exception as e:
    sys.exit()
print('creator:', creator)
print('account:', account)

stm = shared_steem_instance()
# 解锁本地钱包(操作钱包时,需要解锁,否则不用)
# 输入本地钱包密码或在代码中直接赋值
# Enter the local wallet password or set value directly in the code
password = ''  # 'walletkey'
if not password:
    password = input('local wallet password : ')
stm.wallet.unlock(password)

# 本地存储 steem 账号信息的文件
accounts_file = '/bots.json'

try:
    # 创建者账号
    creatorA = Account(creator, steem_instance=stm)
    accountA = Account(account, steem_instance=stm)
except Exception as e:
    print('account can be created:', account)

# 生成一套新密码
bk = BrainKey()
brainkey = bk.get_brainkey()
print('brain key:', str(brainkey))
prikey = str(bk.get_private())
print('private key:', str(prikey))
pubkey = format(bk.get_public(), "STM")
print('public key:', str(pubkey))

# 创建新号:本地存储 owner(默认不存储)
tx = stm.create_claimed_account(
    account, creator=creatorA, password=prikey, store_owner_key=True)
print('create_claimed_account:', json.dumps(tx, indent=4))

# 获取整套密钥
posting_key = PasswordKey(account, prikey, role="posting", prefix=stm.prefix)
active_key = PasswordKey(account, prikey, role="active", prefix=stm.prefix)
memo_key = PasswordKey(account, prikey, role="memo", prefix=stm.prefix)
owner_key = PasswordKey(account, prikey, role="owner", prefix=stm.prefix)
# 提取整套密钥中的公钥
# active_pubkey = active_key.get_public_key()
# owner_pubkey = owner_key.get_public_key()
# posting_pubkey = posting_key.get_public_key()
memo_pubkey = memo_key.get_public_key()
# 提取整套密钥中的私钥
active_privkey = active_key.get_private_key()
posting_privkey = posting_key.get_private_key()
owner_privkey = owner_key.get_private_key()
memo_privkey = memo_key.get_private_key()

print('posting_privkey:', str(posting_privkey))
print('active_privkey:', str(active_privkey))
print('owner_privkey:', str(owner_privkey))
print('memo_privkey:', str(memo_privkey))

with open(accounts_file, 'r', encoding='utf-8') as f:
    # 直接用load()传入文件对象,将文件中字符串内容转为json字典
    accounts_dict = json.load(f)
    # print('old.accounts_dict:', accounts_dict)
    accounts_dict['steems'][account] = {
        "name": account,  # steem 账号名称
        "email": "sample@,pwd",  # steem 注册 email
        "mobile": "sample9,from",  # steem 注册 手机号
        "keys": {
            "brain": brainkey,  # steem 账号的脑密码
            "private": prikey,  # steem 账号当前的私钥
            "public": pubkey,  # steem 账号当前的公钥
            "history": [   # steem 账号的私钥的修改记录
                prikey,
                "sample2",
                "sample3"
            ],
            "posting": str(posting_privkey),  # steem 账号的 posting 私钥
            "active": str(active_privkey),  # steem 账号的 active 私钥
            "owner": str(owner_privkey),  # steem 账号的 owner 私钥
            "memo": str(memo_privkey),  # steem 账号的 memo 私钥,用于解密memo
            "memo_key": str(memo_pubkey)  # steem 账号的 memo 公钥,用于加密memo
        }
    }
with open(accounts_file, 'w', encoding='utf-8') as f:
    # 用dump()传入字典和文件对象,将json字典转为字符串,存入文件
    json.dump(accounts_dict, f, indent=4, ensure_ascii=False)

执行程序:python create_claimed_account_beem dappcoder newaccddd
执行结果,和预期一致:

167

comments