Skip to content

Instantly share code, notes, and snippets.

@crokkon
Last active January 4, 2019 20:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save crokkon/17e5be82d9ef073bcee80a461a11dea9 to your computer and use it in GitHub Desktop.
set_authorities.py
from beem import Steem
from beem.account import Account
from beem.amount import Amount
from argparse import ArgumentParser
from beembase import operations
from beem.transactionbuilder import TransactionBuilder
import json
parser = ArgumentParser()
parser.add_argument('--account', type=str, required=True)
parser.add_argument('--to', type=str, required=True)
parser.add_argument('--amount', type=float, required=True)
parser.add_argument('--asset', type=str, required=True)
parser.add_argument('--memo', type=str, default="")
args = parser.parse_args()
op = operations.Transfer(**{
'from': args.account,
'to': args.to,
'amount': Amount(args.amount, args.asset),
'memo': args.memo
})
stm = Steem(expiration=3600)
tb = TransactionBuilder(steem_instance=stm)
tb.appendOps([op])
print("'%s'" % (json.dumps(tb.json())))
from beem.account import Account
from beembase import operations
from beem.transactionbuilder import TransactionBuilder
from beemgraphenebase.account import PrivateKey, PublicKey, PasswordKey
from argparse import ArgumentParser
from getpass import getpass
import json
def passwordkey_to_key(passwordkey, account, role, prefix="STM"):
""" Get a private key from an input that may be a key or a master
password.
:param str passwordkey: Key or master password to check/transform.
:param str account: Account name the key is for.
:param str role: Role of the key (posting, active, owner, memo)
:param str prefix: Network prefix (default: "STM")
:return private key as a string
"""
try:
# check if passwordkey is a valid private key
PrivateKey(passwordkey, prefix=prefix)
return passwordkey
except ValueError:
# PrivateKey() failed, provided string is not a private key
# -> treat it as a master password
pk = PasswordKey(account, passwordkey, role=role,
prefix=prefix)
return str(pk.get_private())
def valid_key(key, prefix="STM"):
""" Check if an input string is a valid public key
:param str key: input string
:param str prefix: network prefix, default: "STM"
:return True if key is a valid pubkey, else False
"""
key_validity_check = PublicKey(key, prefix=prefix)
return format(key_validity_check, prefix) == key
parser = ArgumentParser()
parser.add_argument('--key-weight', action='append',nargs=2,
metavar=('key', 'weight'))
parser.add_argument('--account-weight', action='append',nargs=2,
metavar=('account', 'weight'))
parser.add_argument('--threshold', type=int, required=True)
parser.add_argument('--account', type=str, required=True)
parser.add_argument('--role', type=str, required=True)
args = parser.parse_args()
acc = Account(args.account)
# inputs sanity checks
if args.role not in ['posting', 'active']:
print("ERROR: invalid role. Valid values are 'posting' and 'active'")
exit(-1)
if not args.key_weight and not args.account_weight:
print("ERROR: need at least one of --key_weight or --account_weight")
exit(-1)
try:
threshold = int(args.threshold)
except ValueError:
print("ERROR: %s is not a valid threshold!" % (args.threshold))
authority = {'account_auths': [], 'key_auths': [], 'address_auths':
[], 'weight_threshold': threshold, 'prefix':
acc.steem.prefix}
for key, weight in args.key_weight or []:
if not valid_key(key):
print("ERROR: %s is not a valid public key!" % (key))
exit(-1)
try:
weight = int(weight)
except ValueError:
print("ERROR: %s is not a valid weight!" % (weight))
exit(-1)
if weight <=0:
print("ERROR: weight must not be negative!")
exit(-1)
authority['key_auths'].append([key, weight])
for account, weight in args.account_weight or []:
try:
weight = int(weight)
except ValueError:
print("ERROR: %s is not a valid weight!" % (weight))
exit(-1)
if weight <=0:
print("ERROR: weight must not be negative!")
exit(-1)
authority['account_auths'].append([account, weight])
if args.role == 'posting':
posting = authority
else:
posting = acc['posting']
if args.role == 'active':
active = authority
else:
active = acc['active']
op = operations.Account_update(**{
"account": acc["name"],
'active': active,
'posting': posting,
'memo_key': acc['memo_key'],
"json_metadata": acc['json_metadata'],
"prefix": acc.steem.prefix})
print("Will set the following authorities:")
print(json.dumps(op.json(), indent=2))
pwdkey = getpass("Please enter the owner key or master password for "
"@%s: " % (acc['name']))
owner_wif = passwordkey_to_key(pwdkey, acc['name'], 'owner',
prefix=acc.steem.prefix)
# Send the account_update operation to the blockchain
tb = TransactionBuilder(steem_instance=acc.steem)
tb.appendOps([op])
tb.appendWif(owner_wif)
tb.sign()
tx = tb.broadcast()
print(tx)
import sys
import json
from beem import Steem
from beem.transactionbuilder import TransactionBuilder
from getpass import getpass
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('--trx', type=str, required=True)
parser.add_argument('--append-signature', action="store_true",
default=False)
parser.add_argument('--broadcast', action="store_true", default=False)
args = parser.parse_args()
op = json.loads(args.trx)
tb = TransactionBuilder(tx=op)
if args.append_signature:
wif = getpass("Enter private key:")
tb.appendWif(wif)
tb.sign(reconstruct_tx=False)
if args.broadcast:
tx = tb.broadcast()
print("SUCCESS: broadcasted '%s'" % (json.dumps(tx, indent=2)))
else:
print("'%s'" % (json.dumps(tb.json())))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment