RaiEx v0.2.0 RaiEx.Block View Source

The block struct and associated functions.

Fields

  • type - the block type, default: “send”
  • previous - the previous block hash, e.g. 9F1D53E732E48F25F94711D5B22086778278624F715D9B2BEC8FB81134E7C904
  • destination - the destination address, e.g. xrb_34bmpi65zr967cdzy4uy4twu7mqs9nrm53r1penffmuex6ruqy8nxp7ms1h1
  • balance - the amount to send, measured in RAW
  • work - the proof of work, e.g. “266063092558d903”
  • signature - the signed block digest/hash
  • hash - the block digest/hash
  • source - the source hash for a receive block
  • representative - the representative for an open block
  • account - the account for an open block
  • state - the state of the block, can be: :unsent or :sent

Send a block

alias RaiEx.{Block, Tools}

seed = "9F1D53E732E48F25F94711D5B22086778278624F715D9B2BEC8FB81134E7C904"

# Generate a private and public keypair from a wallet seed
{priv, pub} = Tools.seed_account(seed, 0)

# Derives an "xrb_" address
address = Tools.create_account!(pub)

# Get the previous block hash
{:ok, %{"frontier" => block_hash}} = RaiEx.account_info(address)

# Somewhat counterintuitively 'balance' refers to the new balance not the
# amount to be sent
block = %Block{
  previous: block_hash,
  destination: "xrb_1aewtdjz8knar65gmu6xo5tmp7ijrur1fgtetua3mxqujh5z9m1r77fsrpqw",
  balance: 0
}

# Signs and broadcasts the block to the network
block |> Block.sign(priv, pub) |> Block.send()

Now all the funds from the first account have been transferred to:

"xrb_1aewtdjz8knar65gmu6xo5tmp7ijrur1fgtetua3mxqujh5z9m1r77fsrpqw"

Receive the most recent pending block

alias RaiEx.{Block, Tools}

seed = "9F1D53E732E48F25F94711D5B22086778278624F715D9B2BEC8FB81134E7C904"

# Generate a private and public keypair from a wallet seed
{priv, pub} = Tools.seed_account(seed, 1)

# Derives an "xrb_" account
account = Tools.create_account!(pub)

{:ok, %{"blocks" => [block_hash]}} = RaiEx.pending(account, 1)
{:ok, %{"frontier" => frontier}} = RaiEx.account_info(account)

block = %Block{
  type: "receive",
  previous: frontier,
  source: block_hash
}

block |> Block.sign(priv, pub) |> Block.process()

Open an account

alias RaiEx.{Block, Tools}

seed = "9F1D53E732E48F25F94711D5B22086778278624F715D9B2BEC8FB81134E7C904"
representative = "xrb_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4"

# Generate a private and public keypair from a wallet seed
{priv_existing, pub_existing} = Tools.seed_account(seed, 1)
{priv_new, pub_new} = Tools.seed_account(seed, 2)

existing_account = Tools.create_account!(pub_existing)
new_account = Tools.create_account!(pub_new)

{:ok, %{"frontier" => block_hash, "balance" => balance}} = RaiEx.account_info(existing_account)

# Convert to number
{balance, ""} = Integer.parse(balance)

# We need to generate a send block to the new address
block = %Block{
  previous: block_hash,
  destination: new_account,
  balance: balance
}

# Signs and broadcasts the block to the network
send_block = block |> Block.sign(priv_existing, pub_existing) |> Block.send()

# The open block
block = %Block{
  type: "open",
  account: new_account,
  source: send_block.hash,
  representative: representative
}

# Broadcast to the network
open_block = block |> Block.sign(priv_new, pub_new) |> Block.process()

Link to this section Summary

Functions

Generates a RaiEx.Block struct from a map

Opens a block

Processes the block. Automatically invokes the correct processing function

Receives a block

Sends a block

Signs the block. Automatically invokes the correct signing function

Link to this section Functions

Generates a RaiEx.Block struct from a map.

Opens a block.

Processes the block. Automatically invokes the correct processing function.

Receives a block.

Sends a block.

Link to this function sign(block, priv_key, pub_key \\ nil) View Source

Signs the block. Automatically invokes the correct signing function.

Link to this function sign_open(block, priv_key, pub_key \\ nil) View Source

Signs an open block.

Link to this function sign_recv(block, priv_key, pub_key \\ nil) View Source

Signs a receive block.

Link to this function sign_send(block, priv_key, pub_key \\ nil) View Source

Signs a send block.