• Stars
    star
    110
  • Rank 316,770 (Top 7 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Official TypeScript packages for interacting with the Helium blockchain

Helium JS SDK

Build Status Coverage Status Renovate

Please view the Documentation for usage and examples.

⚠️ These libraries are currently in active development and are provided as-is. Helium makes no claims or guarantees about the correctness, reliability or security of this code. PRs welcome, see CONTRIBUTING.

This SDK is a collection of TypeScrypt libraries for interacting with the Helium blockchain. For additional documentation about the Helium network, visit the Developer Site.

Package NPM Version What it's for
@helium/crypto npm Cryptography utilities including keypairs, mnemonics and base58-check encoding
@helium/crypto-react-native npm Cryptography utilities following the same interface as @helium/crypto but for React Native
@helium/transactions npm Construct and serialize transaction primitives from their protobuf definitions
@helium/proto npm Protobuf definitions for Helium transactions
@helium/proto-ble npm Protobuf definitions for Helium Hotspot ble transactions
@helium/http npm An HTTP client for the blockchain REST API
@helium/currency npm Utilities for representing amounts of the different currencies supported by Helium
@helium/onboarding npm An HTTP client for interfacing with an Onboarding Server
@helium/address npm Utilities for Helium Addresses
@helium/wallet-link npm Utilities for linking a 3rd party app to the Helium Wallet

Installation

Each package can be installed independently depending on what utility you need. For example:

$ yarn add @helium/crypto @helium/transactions @helium/http
# or
$ npm install @helium/crypto @helium/transactions @helium/http

Usage

The following examples demonstrate some of the more common use cases and show how these packages can be used in combination to accomplish common tasks.

Creating and submitting a payment transaction

A payment from an owned keypair initialized with a 12 word mnemonic to an address specified by its base58 representation. The transaction is serialized to binary and submitted to the blockchain API.

import { Keypair, Address } from '@helium/crypto'
import { PaymentV1, Transaction } from '@helium/transactions'
import { Client } from '@helium/http'

const client = new Client()

// the transactions library needs to be configured
// with the latest chain vars in order to calcluate fees
const vars = await client.vars.get()
Transaction.config(vars)

// initialize an owned keypair from a 12 word mnemonic
const bob = await Keypair.fromWords(['one', 'two', ..., 'twelve'])

// initialize an address from a b58 string
const alice = Address.fromB58('148d8KTRcKA5JKPekBcKFd4KfvprvFRpjGtivhtmRmnZ8MFYnP3')

// get the speculative nonce for the keypair
const account = await client.accounts.get(bob.address.b58)

// construct a payment txn
const paymentTxn = new PaymentV1({
  payer: bob.address,
  payee: alice,
  amount: 10,
  nonce: account.speculativeNonce + 1,
})

// an appropriate transaction fee is calculated at initialization
console.log('transaction fee is:', paymentTxn.fee)

// sign the payment txn with bob's keypair
const signedPaymentTxn = await paymentTxn.sign({ payer: bob })

// submit the serialized txn to the Blockchain HTTP API
client.transactions.submit(signedPaymentTxn.toString())

Creating an advanced payment transaction

PaymentV2 transactions allow for specifying multiple recipients in the same transaction.

import { Keypair, Address } from '@helium/crypto'
import { PaymentV2, Transaction } from '@helium/transactions'
import { Client } from '@helium/http'

const client = new Client()

// the transactions library needs to be configured
// with the latest chain vars in order to calcluate fees
const vars = await client.vars.get()
Transaction.config(vars)

// initialize an owned keypair from a 12 word mnemonic
const bob = await Keypair.fromWords(['one', 'two', ..., 'twelve'])

// get the speculative nonce for the keypair
const account = await client.accounts.get(bob.address.b58)

// initialize recipient addresses from b58 strings
const alice = Address.fromB58('148d8KTRcKA5JKPekBcKFd4KfvprvFRpjGtivhtmRmnZ8MFYnP3')
const charlie = Address.fromB58('13JoEpkGQUd8bzn2BquFZe1CbmfzhL4cYpEohWH71yxy7cEY59Z')

// construct a PaymentV2 txn
const paymentTxn = new PaymentV2({
  payer: bob.address,
  payments: [
    {
      payee: alice,
      amount: 20,
    },
    {
      payee: charlie,
      amount: 10,
    },
  ],
  nonce: account.speculativeNonce + 1,
})

// an appropriate transaction fee is calculated at initialization
console.log('transaction fee is:', paymentTxn.fee)

// sign the payment txn with bob's keypair
const signedPaymentTxn = await paymentTxn.sign({ payer: bob })

// submit the serialized txn to the Blockchain HTTP API
client.transactions.submit(signedPaymentTxn.toString())

Sending an Account's full balance

Sending the maximum amount from an account (leaving a 0 HNT balance) requires taking into account the transaction fee. All fees are denominated in Data Credits (DC), which is equal to $0.00001 USD, meaning 35,000 DC equals $0.35 USD. DC are obtained by burning HNT, permanently removing it from circulation. If you do not already have DC in your account, the appropriate amount of HNT will be burned to cover the fee.

The general formula is: amountToSend = balance - feeInHNT

The packages in helium-js provide utility functions to calculate the above:

import { Keypair, Address } from '@helium/crypto'
import { PaymentV2, Transaction } from '@helium/transactions'
import { Client } from '@helium/http'
import { Balance, CurrencyType } from '@helium/currency'

const client = new Client()

// the transactions library needs to be configured
// with the latest chain vars in order to calcluate fees
const vars = await client.vars.get()
Transaction.config(vars)

// assuming bob has a balance of 100 HNT
const bob = await Keypair.fromWords(['one', 'two', ..., 'twelve'])

// initialize an address from a b58 string
const alice = Address.fromB58('148d8KTRcKA5JKPekBcKFd4KfvprvFRpjGtivhtmRmnZ8MFYnP3')

// get the speculative nonce for the keypair
const account = await client.accounts.get(bob.address.b58)

// construct a PaymentV2 txn for the purpose
// of calculating the fee
const paymentTxnForFee = new PaymentV2({
  payer: bob.address,
  payments: [
    {
      payee: alice,
      amount: account.balance.integerBalance,
    },
  ],
  nonce: account.speculativeNonce + 1,
})

// calculate max sendable amount
const feeInDC = new Balance(paymentTxnForFee.fee, CurrencyType.dataCredit)
const oracle = await client.oracle.getCurrentPrice()
const feeInHNT = feeInDC.toNetworkTokens(oracle.price)
const amountToSend = account.balance.minus(feeInHNT).integerBalance

// construct a PaymentV2 txn to sign
const paymentTxnForFee = new PaymentV2({
  payer: bob.address,
  payments: [
    {
      payee: alice,
      amount: amountToSend,
    },
  ],
  nonce: account.speculativeNonce + 1,
})

// sign the payment txn with bob's keypair
const signedPaymentTxn = await paymentTxn.sign({ payer: bob })

// submit the serialized txn to the Blockchain HTTP API
client.transactions.submit(signedPaymentTxn.toString())

⚠️ Note that oracle prices change over time. It's possible for a transaction to fail if the oracle price changes in between the time the transaction is constructed and when it is absorbed by the consensus group. The API exposes what the next oracle price will be at https://api.helium.io /v1/oracle/predictions. See https://developer.helium.com/blockchain/api/oracle for more details. To avoid failed transactions, it may be worth querying both the oracle predictions, and the current oracle value, and taking the greatest of those values.

Sending payment from multi-signature wallet

Sending a payment from a multi-signature wallet requires collecting the minimum number of required signatures from the addresses associated with the multi-signature address in question. Below is an example of creating and signing a 1 of 2 multi-signature address transaction.

import { Keypair, MultisigSignature } from '@helium/crypto'
import Address, { MultisigAddress } from '@helium/address'
import { PaymentV2, Transaction } from '@helium/transactions'
import { Client } from '@helium/http'

const client = new Client()

// the transactions library needs to be configured
// with the latest chain vars in order to calcluate fees
const vars = await client.vars.get()
Transaction.config(vars)

// initialize a keypair available for signing
const bob = await Keypair.fromWords(['one', 'two', ..., 'twelve'])

// initialize an address from a b58 string
const alice = Address.fromB58('148d8KTRcKA5JKPekBcKFd4KfvprvFRpjGtivhtmRmnZ8MFYnP3')

// initialize multisig address with the full set of addreses and required number of signatures
const multisigAddress = await MultisigAddress.create([bob.address, alice], 1)

// get the speculative nonce for the multisig keypair
const account = await client.accounts.get(multisigAddress.b58)

// create random payee address
const payeeAddress = Address.fromB58('13dSybmfNofup3rBGat2poGfuab4BhYZNKUJFczSi4jcwLmoXvD')

// construct a PaymentV2 txn to sign
const paymentTxn = new PaymentV2({
  payer: multisigAddress,
  payments: [
    {
      payee: payeeAddress,
      amount: amountToSend,
    },
  ],
  nonce: account.speculativeNonce + 1,
})

// Create signatures payload, a map of address to signature, and finally a KeySignature list
const bobSignature = (await paymentTxn.sign({ payer: bob })).signature || new Uint8Array()
const signatureMap = new Map([[bob.address, await bob.sign(paymentTxn.serialize())]])
const signatures = KeySignature.fromMap([bob.address, aliceAddress], signatureMap)

// Construct multisig signature using the address, the full set of all addresses, and the required signatures
const multisigSig = new MultisigSignature([bob.address, aliceAddress], signatures)

// Update signature on payment trasnaction
await paymentTxn.sign({payer: multisigSig})

// submit the serialized txn to the Blockchain HTTP API
client.transactions.submit(paymentTxn.toString())

More Repositories

1

miner

Miner for the helium blockchain
Erlang
609
star
2

HIP

Helium Improvement Proposals
Jupyter Notebook
582
star
3

gateway-rs

The Helium Gateway
Rust
280
star
4

helium-wallet-rs

Rust implementation of a helium wallet CLI
Rust
258
star
5

blockchain-core

Erlang
214
star
6

denylist

Shell
201
star
7

plumtree

Epidemic Broadcast Trees
Erlang
193
star
8

hotspot-app

Helium hotspot app for iOS and Android
TypeScript
187
star
9

explorer

A Helium Blockchain Explorer
JavaScript
124
star
10

docs

Helium Documentation
MDX
120
star
11

helium-program-library

Helium programs to run on the Solana blockchain
TypeScript
118
star
12

erlang-libp2p

An Erlang implementation of libp2p swarms
Erlang
111
star
13

console

A management console to onboard and manage devices running on the Helium blockchain network.
JavaScript
104
star
14

router

router combines a LoRaWAN Network Server with an API for console, and provides a proxy to the Helium blockchain
Erlang
69
star
15

mappers

Mappers Frontend and API
Elixir
68
star
16

erlang-hbbft

Erlang implementation of HoneyBadgerBFT
Erlang
67
star
17

blockchain-etl

Blockchain follower that follows and stores the Helium blockchain
Erlang
64
star
18

longfi-arduino

C++
62
star
19

wallet-app

TypeScript
59
star
20

console-decoders

Payload decoder functions for console.
JavaScript
59
star
21

blockchain-node

A Heilum Blockchain Node
Erlang
58
star
22

network-explorer

TypeScript
57
star
23

angry-purple-tiger

animal-based hash digests for humans
JavaScript
48
star
24

blockchain-http

An http API for the helium blockchain database
Erlang
47
star
25

erlang-dkg

Distributed key generation for Erlang (using pairing based cryptography)
Erlang
41
star
26

erlang-h3

Erlang binding for Uber's H3 spatial coordinate library
Erlang
41
star
27

maker-starter-app

TypeScript
37
star
28

gateway-config

The Helium configuration application. Enables configuring the Helium Hotspot over Bluetooth
Erlang
37
star
29

helium-ledger-app

The official Helium app for the Ledger Nano S
C
32
star
30

helium-vote

TypeScript
30
star
31

helium-ledger-cli

Rust
26
star
32

virtual-lorawan-device

A utility that attaches to a Semtech UDP Host and pretends to be a LoRaWAN Device
Rust
23
star
33

proto

Rust
22
star
34

whitepaper

The Helium Whitepaper
TeX
19
star
35

oracles

Oracles for Helium subDAOs
Rust
19
star
36

gateway-mfr-rs

Rust
18
star
37

angry-purple-tiger-rs

animal-based hash digests for humans.. in rust
Rust
18
star
38

merkerl

A Merkle Tree implementation in Erlang
Erlang
18
star
39

rosetta-helium

Rosetta implementation for helium
Go
18
star
40

longfi-platformio

C++
16
star
41

packet-purchaser

Erlang
16
star
42

relcast

Relcast library
Erlang
15
star
43

gateway_mfr

Erlang Manufacturing Suppport Code
Erlang
15
star
44

erlang-lorawan

Erlang
15
star
45

cargo-elixir

JavaScript
15
star
46

ecc508

Library to communicate with the Microchip cryptoauthentication device
Erlang
14
star
47

lorawan-sniffer

Rust
14
star
48

helium-api-rs

A Rust library for accessing Helium API servers
Rust
14
star
49

erlang-multihash

Erlang implementation of multihash
Erlang
13
star
50

react-native-helium

TypeScript
13
star
51

onboarding-server

JavaScript
13
star
52

erlang-tpke

Threshold encryption
Erlang
11
star
53

helium-console-cli

A command line interface for interacting with Helium Console API
Rust
11
star
54

ebus

An Erlang binding to libdbus
Erlang
11
star
55

libp2p-crypto

Erlang cypto library used by the libp2p system
Erlang
11
star
56

psql-migration

A SQL migration script for Erlang
Erlang
10
star
57

kdtree

Simple kdtree library in erlang
Erlang
10
star
58

semtech-udp

Rust
9
star
59

ecc608-linux-rs

A linux rust library for the i2c ecc508 and ecc608 family of crypto chips
Rust
9
star
60

modular-governance

A modular set of governance contracts on solana
JavaScript
9
star
61

helium-packet-router

Erlang
9
star
62

erlang-erasure

Simple Erlang binding for Jerasure's Reed-Solomon erasure encoding/decoding
C
9
star
63

longfi-core

Platform agnostic implementation of core LongFi primitives.
C
9
star
64

erlang-splicer

Splice 2 sockets together in Erlang
Erlang
9
star
65

stm32-lora-disco-rs

Unofficial Rust Board Support Crate for B-L072Z-LRWAN1
GDB
8
star
66

longfi-erlang

LongFi core bindings for Erlang
C
8
star
67

ecc_compact

Unpatented ECC point compression for NIST p-256 public keys
C
8
star
68

xorf-generator

public key filter tool
Rust
8
star
69

erlang-multiaddr

Erlang implementation of multiaddr
Erlang
8
star
70

longfi-arduino-legacy

C
7
star
71

vincenty

Vincenty's formulae implementation in Erlang
Rust
7
star
72

erlang-pbc

Pairwise crypto
C
7
star
73

erlang-ubx

Erlang support for speaking to ublox gps modules using the ubx protocol
Erlang
6
star
74

lorawan-h3

LoRaWAN regions represented as H3 polyfills
Rust
6
star
75

account-compression-anchor-gen

Anchor generated CPI clients for account compression and bubblegum
Rust
6
star
76

explorer-api

JavaScript
6
star
77

ebus-gatt

A bluetooth gatt server implementation using ebus
Erlang
6
star
78

cortex-mpu

Cortex-M MPU library
Rust
6
star
79

packet-forwarder-test

Rust
6
star
80

erlang-tc

Erlang NIF for threshold_crypto
Erlang
5
star
81

helium-crypto-rs

Rust
5
star
82

well-known

Well known addresses, OUIs, etc. for the Helium network
5
star
83

gwmp-mux

Multiplexer for Semtech's GWMP over UDP
Rust
5
star
84

erl_angry_purple_tiger

Erlang port of angry_purple_tiger
Erlang
5
star
85

erlang-sss

Erlang binding for Shamir Secret Sharing
Erlang
5
star
86

sibyl

Erlang
4
star
87

miner-test

Helium miner testing utilities
Erlang
4
star
88

cream

An Erlang cache backed by the performant Moka library
Erlang
4
star
89

connman

Connection Manager
C
4
star
90

erlang-stats

A NIF wrapper around kthohr/stats
Makefile
4
star
91

ebus-connman

An erlang connman client using ebus
Erlang
4
star
92

helium-foundation-k8s

Helium foundation k8s defs
4
star
93

helium-data

Utilities for processing helium data
Rust
4
star
94

ipython-target-modeling

Testing targeting models
Jupyter Notebook
3
star
95

module-sdk

Libraries and example applications for developing Helium embedded Applications
C
3
star
96

wallet-issues-tracker

Issue tracker for the Helium Wallet app
3
star
97

longfi-st-hal

Helium LongFi Examples Using ST HAL
C
3
star
98

etl-extract

Extracts ETL data that can be used for display or further analysis
Rust
3
star
99

elixir-reed-solomon-erasure

Reed solomon erasure coding NIFs for Elixir
Elixir
3
star
100

intercept

Erlang
3
star