Code Monkey home page Code Monkey logo

nanohakase's Introduction

Nanohakase

Nanohakase is a python library for the Nano cryptocurrency. It aims to be the simplest Nano library out there, and is a self fork of Bananopie (Nanopie was taken already).

Installation

pip install nanohakase

Nanohakase is on pypi.

If you have trouble installing nanohakase because of the ed25519-blake2b dependency, try this: (Linux Debian/Ubuntu example)

sudo apt-get install python3-dev

or it's equivalent for your OS.

Quick Start

First, start with a RPC class, for read only

from nanohakase import *
rpc = RPC("https://proxy.nanos.cc/proxy")

#check current blockcount
print(rpc.get_block_count()["count"])

#get last 10 transactions
print(rpc.get_account_history("nano_3346kkobb11qqpo17imgiybmwrgibr7yi34mwn5j6uywyke8f7fnfp94uyps", count=10)["history"])

#check balance
print(raw_to_whole(int(rpc.get_account_balance("nano_3346kkobb11qqpo17imgiybmwrgibr7yi34mwn5j6uywyke8f7fnfp94uyps")["balance"])))

For sending/receiving transactions, use a Wallet.

from nanohakase import RPC, Wallet
rpc = RPC("https://app.natrium.io/api")

my_account = Wallet(rpc, seed="seed here", index=0)

#or generate a new one
my_new_account = Wallet(rpc, index=0)

print(my_new_account.seed)

#get address of self
print(my_account.get_address())

#get balance of self
print(my_account.get_balance())

#send 1 nano to the faucet development fund
print(my_account.send("nano_3pdripjhteyymwjnaspc5nd96gyxgcdxcskiwwwoqxttnrncrxi974riid94", "1"))

#receive funds
my_account.receive_all()

#change rep
my_account.change_rep("nano_1kd4h9nqaxengni43xy9775gcag8ptw8ddjifnm77qes1efuoqikoqy5sjq3")

#change seed index
my_account.index = 2

Utility functions are also provided.

import nanohakase

#whole to raw nano
print(nanohakase.whole_to_raw("492.2"))

#raw to whole nano
print(nanohakase.raw_to_whole(1900000000000000000000000000))

Documentation

Also see the Nano RPC docs for information on what rpc call wrapper functions return.

RPC (Class)

Parameters:

  • rpc_url (str): IP or URL of node
  • auth (str or bool, Default: False): Optional HTTP Authorization header

Sample:

rpc = RPC("https://proxy.nanos.cc/proxy")

Properties:

  • rpc_url (str): IP or URL of node
  • auth (str or bool): Optional HTTP Authorization header

Methods:

call (Function)

RPC call. Intended for internal use, but useful for RPC calls that aren't directly implemented.

Parameters:

  • payload (dictionary): Payload to send to node

Sample:

rpc.call({"action": "block_count"})

Returns: Response of RPC call (JSON dictionary)

get_block_count (Function)

Get network block count.

Parameters None

Returns: See Nano RPC Docs

get_block_info (Function)

Get block info for hash.

Parameters

  • block (st): Block hash

Returns: See Nano RPC Docs

get_blocks (Function)

Get blocks.

Parameters

  • blocks (str list): List of block hashes to get information on

Returns: See Nano RPC Docs

get_blocks_info (Function)

Get blocks, with more detailed information.

Parameters

  • blocks (str list): List of block hashes to get information on

Returns: See Nano RPC Docs

get_representatives (Function)

Get list of network representatives and their weight

Parameters None

Returns: See Nano RPC Docs

get_representatives_online (Function)

Get list of network representatives that have recently voted

Parameters None

Returns: See Nano RPC Docs

get_account_history (Function)

Get account history (confirmed and received transaction list)

Parameters

  • account (str): Address of account
  • count (int, Default: -1): Optional parameter to specify amount of transactions to return. -1 means all, or at least as much as the node will allow

Returns: See Nano RPC Docs

get_account_info (Function)

Get account information, like height, frontier, balance, etc

Parameters

  • account (str): Address of account

Returns: See Nano RPC Docs

get_account_balance (Function)

Get account balance

Parameters

  • account (str): Address of account

Returns: See Nano RPC Docs

get_account_representative (Function)

Get account representative

Parameters

  • account (str): Address of account

Returns: See Nano RPC Docs

get_accounts_representatives (Function)

Get representatives of accounts

Parameters

  • account (str list): List of addresses

Returns: See Nano RPC Docs

get_account_weight (Function)

Get delegated weight of representative

Parameters

  • account (str): Address of representative

Returns: See Nano RPC Docs

get_receivable (Function)

Get receivable transactions for account

Parameters

  • account (str): Address of representative
  • count (int, Default: 20): Optional parameter to specify max amount of receivable transactions to return
  • thereshold (int or bool, Default: False): Optional parameter to filter out any receivable transactions with value less than the thereshold

Returns: See Nano RPC Docs

Wallet (class)

Parameters:

  • rpc (RPC): A RPC class
  • seed (str or bool, Default: False): 64 character hex seed, if False, will generate a seed by itself. Private keys are derived from the seed.
  • index (int, Default: 0): Optional parameter that is the index of the seed. Any number from 0 to 4294967295. Each index of the seed is a different private key, and so different address.

Sample:

my_wallet = Wallet(RPC("https://proxy.nanos.cc/proxy"), "seed here", 0)

Properties:

  • rpc_url (str): IP or URL of node
  • seed (str): Nano seed (64 character hex string)
  • index (int): Seed index. Change this property to change the wallet seed index.

Methods

send_process (Function)

Internal use function to send a process RPC call

Parameters

  • block (dictionary): block content
  • subtype (str): Send, receive, or change

Returns See Nano RPC Docs

send (Function)

High level function to send Nano

Parameters

  • to (str): Address to send to
  • amount (str): Amount of Nano to send (in whole, not raw)
  • work (str or bool, Default: False): Leave it as False to ask node to generate work (passes do_work). Put in a work string if work generated locally

Sample:

my_wallet = Wallet(RPC("https://app.natrium.io/api"), "seed here", 0)
my_account.send("nano_3pdripjhteyymwjnaspc5nd96gyxgcdxcskiwwwoqxttnrncrxi974riid94", "1")

Returns See Nano RPC Docs

receive_specific (Function)

Receive a specific block

Parameters

  • hash (str): Block hash to receive
  • work (str or bool, Default: False): Leave it as False to ask node to generate work (passes do_work). Put in a work string if work generated locally

Returns See Nano RPC Docs

receive_all (Function)

Receive all (technically, 20) receivable transactions

Parameters None

Sample:

my_wallet = Wallet(RPC("https://proxy.nanos.cc/proxy"), "seed here", 0)
my_account.receive_all()

Returns Nothing

change_rep (Function)

Change account representative

Parameters

  • new_representative (str): Representative Nano address to change to
  • work (str or bool, Default: False): Leave it as False to ask node to generate work (passes do_work). Put in a work string if work generated locally

Sample:

my_wallet = Wallet(RPC("https://proxy.nanos.cc/proxy"), "seed here", 0)
my_account.change_rep("nano_1kd4h9nqaxengni43xy9775gcag8ptw8ddjifnm77qes1efuoqikoqy5sjq3")

Returns See Nano RPC Docs

get_address (Function)

Get address at current index of current seed

Parameters None

Returns str, Nano address

get_balance (Function)

Double wrapped function to get balance of self (see RPC's get_account_balance)

get_receivable (Function)

Double wrapped function to get receivable blocks (see RPC's get_receivable)

get_representative (Function)

Double wrapped function to get representative of self (see RPC's get_account_representative)

get_account_info (Function)

Double wrapped function to get account info of self (see RPC's get_account_info)

generate_seed (static Function)

Generate a random seed using os.urandom

Parameters None

Sample:

print(Wallet.generate_seed())

Returns 64 character hex seed

Util

Properties

  • NANO_DECIMALS (int): Amount of decimals that Nano has (30)
  • PREAMBLE (str): Hex string to prepend when signing

Methods

encode_base32, decode_base32, bytes_to_hex, hex_to_bytes, random_bytes, get_private_key_from_seed, get_public_key_from_private_key, get_address_from_public_key, get_public_key_from_address, hash_block, sign are internal use Functions that are currently undocumented. Look at /nanohakase/util.py to see what they do.

whole_to_raw (Function)

Converts whole Nano to raw Nano

Parameters

  • whole (str): Whole amount of Nano

Returns int, that is raw amount of Nano

raw_to_whole (Function)

Converts raw Nano to whole Nano (Cuts off at 2 decimal places)

Parameters

  • raw (int): Raw amount of Nano

Returns int, that is whole amount of Nano

nanohakase's People

Contributors

arostekmu avatar stjet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nanohakase's Issues

Sending does not work

Hey, I'm having an issue gettin the library to send XNO. It used to work, but since a few months it's returning a non-hexadecimal number error:

nrpc = nanoRPC("https://app.natrium.io/api")
nano_key = nanoWallet(nrpc, seed="XXXXX", index=0)
print(nano_key.get_balance())
print(nano_key.send("nano_3pdripjhteyymwjnaspc5nd96gyxgcdxcskiwwwoqxttnrncrxi974riid94", "0.001"))
{'balance': '2594130159959955286961', 'pending': '0', 'receivable': '0'}
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/debian/.local/lib/python3.9/site-packages/gunicorn/__main__.py", line 7, in <module>
    run()
  File "/home/debian/.local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/home/debian/.local/lib/python3.9/site-packages/gunicorn/app/base.py", line 236, in run
    super().run()
  File "/home/debian/.local/lib/python3.9/site-packages/gunicorn/app/base.py", line 72, in run
    Arbiter(self).run()
  File "/home/debian/.local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 58, in __init__
    self.setup(app)
  File "/home/debian/.local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 118, in setup
    self.app.wsgi()
  File "/home/debian/.local/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/home/debian/.local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
    return self.load_wsgiapp()
  File "/home/debian/.local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/debian/.local/lib/python3.9/site-packages/gunicorn/util.py", line 371, in import_app
    mod = importlib.import_module(module)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/debian/duino-coin-master-server/app.py", line 562, in <module>
    print(nano_key.send("nano_3pdripjhteyymwjnaspc5nd96gyxgcdxcskiwwwoqxttnrncrxi974riid94", "0.001"))
  File "/home/debian/.local/lib/python3.9/site-packages/nanohakase/wallet.py", line 50, in send
    block_hash = hash_block(block)
  File "/home/debian/.local/lib/python3.9/site-packages/nanohakase/util.py", line 110, in hash_block
    blake_obj.update(hex_to_bytes(padded_balance))
  File "/home/debian/.local/lib/python3.9/site-packages/nanohakase/util.py", line 65, in hex_to_bytes
    return bytes.fromhex(hex)
ValueError: non-hexadecimal number found in fromhex() arg at position 8

I believe this is a bug in the library, as it used to work fine and my code hasn't changed. Can you maybe take a look at it in your free time?

Update from upstream bananopie

Update nanohakase from the upstream bananopie repo. Unfortunately I didn't make this a fork so it's gonna be a bit tricky.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.