Skip to content

Instantly share code, notes, and snippets.

@AbhayAysola
Last active April 11, 2021 06:35
Show Gist options
  • Save AbhayAysola/c86555372cc2c61b01757f6aacb8c234 to your computer and use it in GitHub Desktop.
Save AbhayAysola/c86555372cc2c61b01757f6aacb8c234 to your computer and use it in GitHub Desktop.
import os
import json
import requests
from pprint import pprint
from operator import itemgetter
from hashlib import sha3_256 as sha3
from nacl.encoding import HexEncoder
from nacl.signing import SigningKey, VerifyKey
# pip install pynacl requests
SIGNING_KEY = 'testingkey'
NUMBER_OF_ACCOUNTS = 10
AMOUNT_TO_KEEP = 100000
#--------Do not change code below this line--------#
signing_key = SigningKey(SIGNING_KEY.encode('utf-8'), encoder=HexEncoder)
account_number = signing_key.verify_key.encode(encoder=HexEncoder).decode('utf-8')
bank_config = requests.get('http://13.57.215.62/config?format=json').json()
balance_lock = requests.get(f"{bank_config['primary_validator']['protocol']}://{bank_config['primary_validator']['ip_address']}:{bank_config['primary_validator']['port'] or 0}/accounts/{account_number}/balance_lock?format=json").json()['balance_lock']
balance = requests.get(f"{bank_config['primary_validator']['protocol']}://{bank_config['primary_validator']['ip_address']}:{bank_config['primary_validator']['port'] or 0}/accounts/{account_number}/balance?format=json").json()['balance']
amount = (balance - AMOUNT_TO_KEEP - int(bank_config['primary_validator']['default_transaction_fee']) - int(bank_config['default_transaction_fee'])) / NUMBER_OF_ACCOUNTS
txs = [
{
'amount': int(bank_config['default_transaction_fee']),
'fee': 'BANK',
'recipient': bank_config['account_number'],
},
{
'amount': int(bank_config['primary_validator']['default_transaction_fee']),
'fee': 'PRIMARY_VALIDATOR',
'recipient': bank_config['primary_validator']['account_number'],
}
]
accounts = []
for i in range(NUMBER_OF_ACCOUNTS):
accounts.append(SigningKey.generate())
for account in accounts:
txs.append({'amount': int(amount), 'recipient': account.verify_key.encode(encoder=HexEncoder).decode('utf-8')})
f = open("signingkey.txt", "a")
for account in accounts:
print('signing key:',account.encode(encoder=HexEncoder).decode('utf-8'), 'account number:',account.verify_key.encode(encoder=HexEncoder).decode('utf-8'))
f.write(f"signing_key:{account.encode(encoder=HexEncoder).decode('utf-8')}; account_number:{account.verify_key.encode(encoder=HexEncoder).decode('utf-8')}\n")
f.close()
message = {
'balance_key': balance_lock,
'txs': sorted(txs, key=itemgetter('recipient'))
}
signature = signing_key.sign(json.dumps(message, separators=(',', ':'), sort_keys=True).encode('utf-8')).signature.hex()
block = {
'account_number': account_number,
'message': message,
'signature': signature
}
pprint(block)
# headers = {
# 'Connection': 'keep-alive',
# 'Accept': 'application/json, text/plain, */*',
# 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) TNBAccountManager/1.0.0-alpha.43 Chrome/83.0.4103.122 Electron/9.4.0 Safari/537.36',
# 'Content-Type': 'application/json',
# 'Accept-Language': 'en-US'
# }
# r = requests.request("POST", 'http://13.57.215.62/blocks', headers=headers, data=block)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment