def create_account(balance, block_count=1, complete=False, confirm=False): private_key = generate_seed() account_id = get_account_id(private_key=private_key) account = Account( account_id=account_id, public_key=get_account_public_key(private_key=private_key), private_key=private_key, source=AccountSource.PRIVATE_KEY, representative=get_account_id(public_key=generate_seed()) ) block_amount = int(balance / block_count) for _ in range(0, block_count): account.receive_block( pocketable_block_factory( account_id=account_id, amount=block_amount ) ) if complete: block = account.blocks[-1] block.sign(private_key=account.private_key) block.solve_work(difficulty=TEST_DIFFICULTY) if confirm: block.confirmed = True account.update_confirmed_head() return account
def add_account( server, passphrase: PassphraseOption, account_id: AccountOption(group_name="account"), public_key: PublicKeyOption(group_name="account"), private_key: PrivateKeyOption(group_name="account")): if sum([bool(account_id), bool(public_key), bool(private_key)]) != 1: raise StdioError( "one_parameter_required", "Only one parameter from 'account_id', 'public_key' or " "'private_key' is required" ) account = None if account_id: account = server.wallet.add_account_from_account_id(account_id) elif public_key: account = server.wallet.add_account_from_account_id( get_account_id(public_key=public_key) ) elif private_key: with unlock_wallet(server=server, passphrase=passphrase): account = server.wallet.add_account_from_private_key(private_key) server.save_wallet() return StdioResult({"account_id": account.account_id})
def encode_ip(ip): ip_as_bytes = bytes(map(int, ip.split('.'))) #convert to bytes ip_as_bytes += (32 - len(ip_as_bytes)) * b'\0' #padding ip_account = get_account_id( public_key=ip_as_bytes.hex(), prefix="nano_") ##convert to nano account format return ip_account
def search_for_id(phrase, only_prefix): i = random.SystemRandom().randint(0, (2**256) - 1) phrase_length = len(phrase) iterations = 0 start_time = time.time() while iterations < ITERATIONS_PER_RUN and i < 2**256: private_key = "{0:0{1}x}".format(i, 64) account_id = get_account_id(private_key=private_key) match = False if only_prefix: # Account has to start with the phrase match = account_id[5:5 + phrase_length] == phrase else: # Any place is OK match = phrase in account_id if match: # Found it! return { "found": True, "private_key": private_key, "account_id": account_id } iterations += 1 i += 1 end_time = time.time() return {"found": False, "rate": iterations / (end_time - start_time)}
def create_account(account_id=None): if not account_id: account_id = get_account_id(public_key=generate_seed()) account = Account( account_id=account_id, source=AccountSource.WATCHING) return account
def create_work_unit(account_id=None, difficulty=None): if not difficulty: difficulty = to_hex(10000, 16) block = RawBlock( block_type="state", account=(account_id or get_account_id(public_key=generate_seed())), previous=None, representative=get_account_id(public_key="0" * 64), balance=0, link=generate_seed(), difficulty=difficulty, verify=False) work_unit = WorkUnit(account_id=block.account, block_hash=block.block_hash, work_block_hash=block.work_block_hash, difficulty=block.difficulty) return work_unit
def create_link_block(account_id, amount): sending_private_key = generate_seed() sending_account_id = get_account_id(private_key=sending_private_key) block = RawBlock( block_type="state", account=sending_account_id, previous=generate_seed().upper(), representative=get_account_id(public_key=generate_seed()), balance=amount + random.randint(2**100, 2**110), link_as_account=account_id) block.sign(sending_private_key) block.solve_work(difficulty=TEST_DIFFICULTY) link_block = LinkBlock( block_data=block.to_dict(), amount=amount, timestamp=Timestamp( date=time.time(), source=TimestampSource.WALLET ) ) return link_block
def create_account(private_key=None): if not private_key: private_key = generate_seed() public_key = get_account_public_key(private_key=private_key) account_id = get_account_id(public_key=public_key) account = Account( account_id=account_id, public_key=public_key, private_key=private_key, source=AccountSource.PRIVATE_KEY) return account
def encode_ip(ip): def int_to_bytes(x: int) -> bytes: return x.to_bytes((x.bit_length() + 7) // 8, 'big') try: ip_as_bytes = int_to_bytes(int( ipaddress.IPv4Address(ip))) #convert ipv4 to bytes except: try: ip_as_bytes = int_to_bytes(int( ipaddress.IPv6Address(ip))) #convert ipv6 to bytes except: return "Invalid IP Address" ip_as_bytes += (32 - len(ip_as_bytes)) * b'\0' #padding to 32 bytes ip_account = get_account_id( public_key=ip_as_bytes.hex(), prefix="nano_") ##convert to nano account format return ip_account
def test_work_local_multiple(local_work_plugin_factory, work_unit_factory): # Enqueue multiple PoW jobs and ensure they're all completed account_ids = [ get_account_id(private_key=generate_seed()) for _ in range(0, 100) ] work_plugin = local_work_plugin_factory(threads=8) work_plugin.add_work_units_to_solve([ work_unit_factory(account_id=account_id) for account_id in account_ids ], network_difficulty=to_hex(10000, 16)) work_units = work_plugin.work_units # The underlying queue is a set, so the exact amount of results # should be 100 even if more valid PoWs were found wait_for(lambda: count_solved_units(work_units) == 100, timeout=5) for work_unit in work_units.values(): account_ids.remove(work_unit.account_id)
def create_block(prev_block, private_key, amount): account_id = get_account_id(private_key=private_key) link_block = legacy_pocketable_block_factory( account_id=account_id, amount=amount) raw_block = RawBlock( account=account_id, block_type="receive", previous=prev_block.block_hash, source=link_block.block_hash) raw_block.sign(private_key) raw_block.solve_work(difficulty=TEST_DIFFICULTY) block = Block( block_data=raw_block.to_dict(), link_block=link_block, ) return block
def add_account_from_private_key(self, private_key): """ Add a new account from a private key. If the account already exists as a watching-only account, private key will only be added instead. :param str private_key: Private key to add :raises AccountAlreadyExists: Account already exists with this private key :raises nanolib.exceptions.InvalidPrivateKey: Private key is invalid :returns: Created account :rtype: siliqua.wallet.accounts.Account """ validate_private_key(private_key) key_pair = get_account_key_pair(private_key=private_key) account_id = get_account_id(private_key=private_key) if account_id in self.account_map: # If the account is already added but only as a watching-only # address, update the Account entry account = self.account_map[account_id] if not account.private_key: account.private_key = private_key account.source = AccountSource.PRIVATE_KEY account.encrypt_secrets(secret_key=self.secret_key) return account raise AccountAlreadyExists("Account already in the wallet") account = Account(account_id=account_id, public_key=key_pair.public, private_key=private_key, source=AccountSource.PRIVATE_KEY) account.encrypt_secrets(secret_key=self.secret_key) return self.add_account(account)
import waitress app = Flask(__name__) #Check config try: validate_account_id(worker["account"]) validate_private_key(worker["private_key"]) validate_account_id(worker["representative"]) except Exception as e: print("Invalid worker_config.json settings found! Details: ") print(e) quit() #Check if key pair is valid if worker["account"] != get_account_id(private_key=worker["private_key"], prefix="nano_"): print("Invalid key pair") quit() print("Configurations okay") #Check if Node is online try: r = requests.post(worker["node"], json={"action": "version"}) except: print("Node " + worker["node"] + " Offline! Exiting") quit() else: if "node_vendor" in r.json(): print("Node (" + r.json()["node_vendor"] + ") is online on " + worker["node"]) else:
def check_key_pair(account, private_key): if account == get_account_id(private_key=private_key, prefix="nano_"): return True else: return False
def keys(self): return [ get_account_id(public_key=binascii.hexlify(key).decode(), prefix="xrb_") for key in self.data.keys() ]
def get_public_account(msg: bytes): return get_account_id(public_key=hashlib.sha256(msg).hexdigest())
def items(self): return [(get_account_id(public_key=binascii.hexlify(key).decode(), prefix="xrb_"), value) for key, value in self.data.items()]
def importConfig(): #get worker api config try: with open('config/worker_config.json') as worker_config: data = json.load(worker_config) worker = { "account": data['reward_account'].replace("xrb_", "nano_"), "private_key": data['private_key'].upper(), "public_key": nanolib.get_account_key_pair(data['private_key']).public.upper(), "representative": data['representative'].replace("xrb_", "nano_"), "default_fee": data['fee'], "node": to_url(data['node']), "worker_node": to_url(data['worker']), "max_multiplier": data['max_multiplier'], "use_dynamic_pow": data['use_dynamic_pow'], "use_dynamic_fee": data['use_dynamic_fee'], "dynamic_pow_interval": data['dynamic_pow_interval'], "show_network_difficulty": data['show_network_difficulty'], "service_listen": data['listen'], "service_port": data['port'], } worker["default_fee"] = to_raws(str( worker["default_fee"])) #convert mNano fee to raws except Exception as err: raise Exception("worker_config.json error: " + str(err)) #Get worker registration config try: with open('config/register_config.json') as register_config: data_register = json.load(register_config) register_config = { "account": data_register['registration_account'].replace("xrb_", "nano_"), "register_code": int(data_register['register_code']), "get_ip": { "ipv4": to_url(data_register['get_ip']['ipv4']), "ipv6": to_url(data_register['get_ip']['ipv6']) }, "default_ip_version": ip_version(data_register['default_ip_version']) } except Exception as err: raise Exception("worker_config.json error: " + str(err)) #Check config file try: nanolib.validate_account_id(worker["account"]) nanolib.validate_private_key(worker["private_key"]) nanolib.validate_account_id(worker["representative"]) except Exception as e: raise Exception( "Invalid config in worker_config.json found! Details: " + str(e)) #Check config file try: nanolib.validate_account_id(register_config["account"]) except Exception as e: raise Exception( "Invalid config in register_config.json found! Details: " + str(e)) #Check if key pair is valid if worker["account"] != nanolib.get_account_id( private_key=worker["private_key"], prefix="nano_"): raise Exception("Invalid key pair") return {"worker_config": worker, "register_config": register_config}
def to_dict(self): return { get_account_id(public_key=binascii.hexlify(key).decode(), prefix="xrb_"): value for key, value in self.data.items() }