gonano

module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2024 License: MIT

README

Gonano

==========

Gonano is primarily a package written in Go for creating and managing Nano wallets, with the ability to both send and receive amounts. Gonano also comprises a wallet package which handles key management and an rpc package for communicating with Nano nodes.

This package is a fork of https://github.com/ekipma/gonano which removes hardware ledger and gpu pow support for better compatibility. It also adds mnemonic import and fixes other issues.

Install

go get github.com/fairking/gonano
wallet package

func NewWallet(seed []byte) (w *Wallet, err error)
func NewMnemonicWallet(mnemonic) (w *Wallet, err error)
func NewBip39Wallet(mnemonic, password string) (w *Wallet, err error)
func NewLedgerWallet() (w *Wallet, err error)

Main entrypoint to the package. The first function creates a wallet using a traditional seed, the second uses a menemonic, the third uses a BIP39 mnemonic and passphrase.

func (w *Wallet) ScanForAccounts() (err error)

Scans the wallet for non-empty accounts, including not yet opened accounts with receivable amounts.

func (w *Wallet) NewAccount(index *uint32) (a *Account, err error)

Creates a new account within the wallet. If index is non-nil, derives the account from the seed with the given index.

func (w *Wallet) GetAccount(address string) *Account
func (w *Wallet) GetAccounts() (accounts []*Account)

Gets the account with the given address, or all the accounts known to the wallet.

func (w *Wallet) ReceiveReceivables() (err error)

Receives all receivable amounts to the wallet.

func (a *Account) Address() string

Get the address of the account.

func (a *Account) Index() uint32

Get the derivation index of the account.

func (a *Account) Balance() (balance, receivable *big.Int, err error)

Get the owned balance and receivable balance of the account. Amounts are in raws.

func (a *Account) Send(account string, amount *big.Int) (hash rpc.BlockHash, err error)

Send amount Nano from this to another account. The block hash is returned.

func (a *Account) ReceiveReceivables() (err error)

Receives all receivable amounts to the account.

func (a *Account) ChangeRep(representative string) (hash rpc.BlockHash, err error)

Change the representative of the account.

util package

func NanoAmountFromString(s string) (n NanoAmount, err error)
func (n NanoAmount) String() string

Convert the string representation of an amount to a NanoAmount. A NanoAmount has a Raw field containing the amount in raws. A NanoAmount can also be converted back to a string.

func AddressToPubkey(address string) (pubkey []byte, err error)
func PubkeyToAddress(pubkey []byte) (address string, err error)

AddressToPubkey converts nano address to a public key. PubkeyToAddress converts public key to a nano address.

rpc package

Create an RPC client with Nano API URL (eg. https://app.natrium.io/api).

rpcClient := rpc.Client{URL: "..."}

Not all RPC methods are supported by some of the nodes. Please seek an additional info on their official websites. The following methods are available:

func (c *Client) AccountBalance(account string) (balance, receivable *RawAmount, err error)
func (c *Client) AccountBlockCount(account string) (blockCount uint64, err error)
func (c *Client) AccountHistory(account string, count int64, head BlockHash) (history []AccountHistory, previous BlockHash, err error)
func (c *Client) AccountHistoryRaw(account string, count int64, head BlockHash) (history []AccountHistoryRaw, previous BlockHash, err error)
func (c *Client) AccountInfo(account string) (info AccountInfo, err error)
func (c *Client) AccountRepresentative(account string) (representative string, err error)
func (c *Client) AccountWeight(account string) (weight *RawAmount, err error)
func (c *Client) AccountsBalances(accounts []string) (balances map[string]*AccountBalance, err error)
func (c *Client) AccountsFrontiers(accounts []string) (frontiers map[string]BlockHash, err error)
func (c *Client) AccountsReceivable(accounts []string, count int64) (receivable map[string]HashToReceivableMap, err error)
func (c *Client) AvailableSupply() (available *RawAmount, err error)
func (c *Client) BlockAccount(hash BlockHash) (account string, err error)
func (c *Client) BlockConfirm(hash BlockHash) (started bool, err error)
func (c *Client) BlockCount() (cemented, count, unchecked uint64, err error)
func (c *Client) BlockInfo(hash BlockHash) (info BlockInfo, err error)
func (c *Client) Blocks(hashes []BlockHash) (blocks map[string]*Block, err error)
func (c *Client) BlocksInfo(hashes []BlockHash) (blocks map[string]*BlockInfo, err error)
func (c *Client) Chain(block BlockHash, count int64) (blocks []BlockHash, err error)
func (c *Client) Delegators(account string) (delegators map[string]*RawAmount, err error)
func (c *Client) DelegatorsCount(account string) (count uint64, err error)
func (c *Client) FrontierCount() (count uint64, err error)
func (c *Client) Frontiers(account string, count int64) (frontiers map[string]BlockHash, err error)
func (c *Client) Ledger(account string, count int64) (accounts map[string]AccountInfo, err error)
func (c *Client) Process(block *Block, subtype string) (hash BlockHash, err error)
func (c *Client) Representatives(count int64) (representatives map[string]*RawAmount, err error)
func (c *Client) RepresentativesOnline() (representatives map[string]Representative, err error)
func (c *Client) Republish(hash BlockHash, count, sources, destinations int64) (blocks []BlockHash, err error)
func (c *Client) Successors(block BlockHash, count int64) (blocks []BlockHash, err error)
func (c *Client) WorkCancel(hash BlockHash) (err error)
func (c *Client) WorkGenerate(hash BlockHash, difficulty HexData) (work, difficulty2 HexData, multiplier float64, err error)
func (c *Client) WorkValidate(hash BlockHash, work HexData) (validAll, validReceive bool, difficulty HexData, multiplier float64, err error)
Example

Check nano wallet balance:

// Import a new wallet from mnemonic
var w, _ = wallet.NewMnemonicWallet("law icon child link you detect exchange annual act equal dwarf sponsor away width illness renew detect team divert intact fault doll smooth mail")

// Apply the client url
w.RPC = rpc.Client{URL: "https://rainstorm.city/api"}

// Create an account with index 0
var ind = uint32(0)
var a, e1 = w.NewAccount(&ind)
if e1 != nil {
    panic(e1)
}
assert.Equal(t, "nano_3ngori18ug4jxtgpxwnpqmnz5buhyc11nwt99rnkxuohyf88x8qj1i5iomim", a.Address())

// Check the balance of the account
var b, _, e2 = a.Balance()
if e2 != nil {
    panic(e2)
}
var nanoAmount = util.NanoAmount{Raw: b}
assert.Equal(t, "0.1", nanoAmount.String())

Directories

Path Synopsis
ed25519
Package ed25519 implements the Ed25519 signature algorithm.
Package ed25519 implements the Ed25519 signature algorithm.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL