Nimiq for Gleam

Package Version Hex Docs

Various modules and helpers for working with Nimiq primitives in the Gleam programming language or as a CLI.

Installation

gleam add nimiq_gleam

Address module

import nimiq_gleam/account/address

pub fn main() {
  let formatted_addr =
    address.from_hex("0000000000000000000000000000000000000000")
    |> address.to_user_friendly_address()
  // = "NQ07 0000 0000 0000 0000 0000 0000 0000 0000"
}

Key modules

import nimiq_gleam/account/address
import nimiq_gleam/key/ed25519/private_key as private_key
import nimiq_gleam/key/ed25519/public_key as ed25519_public_key
import nimiq_gleam/key/public_key

// Generate a new private key
let private = private_key.generate()
// Print the private key in hex format
io.println(private_key.to_hex(private))
// Derive its public key
let public = ed25519_public_key.derive_key(private)
// Compute its address
let address = public_key.to_address(public)
// Print the address in user-friendly format
io.println(address.to_user_friendly_address(address))

Transaction modules

import gleam/option.{None}
import nimiq_gleam/account/address
import nimiq_gleam/coin.{Coin}
import nimiq_gleam/key/ed25519/private_key as private_key
import nimiq_gleam/key/ed25519/public_key as ed25519_public_key
import nimiq_gleam/key/ed25519/signature as ed25519_signature
import nimiq_gleam/key/public_key.{EdDsaPublicKey}
import nimiq_gleam/key/signature.{EdDsaSignature}
import nimiq_gleam/transaction/enums.{TestAlbatrossNetwork}
import nimiq_gleam/transaction/signature_proof
import nimiq_gleam/transaction/transaction

let tx =
  transaction.new_basic(
    case // Sender address
      address.from_user_friendly_address(
        "NQ17 D2ES UBTP N14D RG4E 2KBK 217A 2GH2 NNY1",
      )
    {
      Ok(addr) -> addr
      Error(_) -> panic as "Invalid sender address"
    },
    case // Recipient address
      address.from_user_friendly_address(
        "NQ34 248H 248H 248H 248H 248H 248H 248H 248H",
      )
    {
      Ok(addr) -> addr
      Error(_) -> panic as "Invalid recipient address"
    },
    Coin(100_000_000), // Value in luna
    Coin(138), // Fee in luna
    100_000, // Validity start height
    TestAlbatrossNetwork, // Network ID
    None, // Flags
  )

// Construct signature proof
let private_key = case
  private_key.from_hex(
    // Replace with your private key
    "0000000000000000000000000000000000000000000000000000000000000000",
  )
{
  Ok(pk) -> pk
  Error(_) -> panic as "Invalid private key"
}
let public_key = ed25519_public_key.derive_key(private_key)
let signature =
  ed25519_signature.create(
    private_key,
    public_key,
    transaction.serialize_content(tx),
  )
let proof = signature_proof.single_sig(
  EdDsaPublicKey(public_key),
  EdDsaSignature(signature),
)

// Add proof to transaction
let tx =
  proof
  |> signature_proof.serialize()
  |> transaction.set_proof(tx, _)

// Print the serialized transaction as a hex string
io.println(transaction.to_hex(tx))

CLI

Convert an address from any representation to user-friendly address format:

nimiq_gleam address 0000000000000000000000000000000000000000
# "NQ07 0000 0000 0000 0000 0000 0000 0000 0000"

Development

gleam format # Format the code
gleam test   # Run the tests
gleam run    # Run the cli
Search Document