2.1.0 • Published 1 year ago

@andreivcodes/spacemeshlib v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

spacemeshlib

spacemeshlib is a typescript library for the Spacemesh network.

Installation

npm install @andreivcodes/spacemeshlib
yarn add @andreivcodes/spacemeshlib

Features

  • Imports Spacemesh API, builds it to typescript using ts-proto
  • Imports a modified fork of ed25519-WASM, and builds a .wasm file
  • Exports grpc clients using nice-grpc
  • Exports functions of ed25519.wasm through a TypeScript version of wasm_exex.js
  • Abstracts away a lot of the boilerplate of grpc and wasm.
  • Exports easy to work with functions as createClients(NETWORK_URL, 443, true) or derivePublicKey(SEED, 0)

Usage

import {
  toHexString,
  derivePrivateKey,
  derivePublicKey,
  getAccountBalance,
  getAccountNonce,
  submitTransaction,
  createClients,
  SubmitTransactionResponse,
} from '@andreivcodes/spacemeshlib'

const NETWORK_URL = 'https://api-devnet226.spacemesh.io/'

const SEED: string = process.env.SEEDPHRASE!

const RECIPIENT: string = '0x38DB093Ce43Fe3dB88D89568bAAeB68A6b5E74a6'.slice(2)

//loads public key from seed
const pk = (await derivePublicKey(SEED, 0)) as Uint8Array

//connects to network and returns nice-grpc clients
createClients(NETWORK_URL, 443, true)

//gets account nonce and balance
const accountNonce = await getAccountNonce(pk)
const accountBalance = await getAccountBalance(pk)

if (accountBalance < amount) {
  console.log('I am out of funds :(')
  return
}

//changing state of the mesh requires a private key, so we load that
//same as we did with public key, but using derivePrivateKey
const pk = (await derivePrivateKey(SEED, 0)) as Uint8Array

//submit a transaction
submitTransaction({
  accountNonce: accountNonce,
  receiver: RECIPIENT
  gasLimit: 1,
  fee: 1,
  amount: 100,
  secretKey: sk,
})
  //and get a response back
  .then((response: SubmitTransactionResponse) => {
    console.log(`💸 just transferred funds to ${RECIPIENT}.`)
    console.log(`Tx ID: 0x${toHexString(response.txstate?.id?.id!)}`)
  })
  .catch((err: any) => {
    console.log(`could not transfer :( submitTransaction failed`)
    console.log(err)
  })

Docs

Typedoc generated docs are available here.

Build

Requirements

  • protobuf
  • golang

yarn

Installs all dependencies


yarn build:node

Cleans old files, generates new index files, lints and builds using current protoc and wasm files

ESM build is stored in /dist/esm

CommonJS build is stored in /dist/cjs

Types are stored in /dist/types


yarn build:wasm

compiles a new src/wasm/ed25519.wasm from /src/wasm/ed25519-WASM/

go1.19.1 is required!


yarn build:protoc

Compiles proto files from /src/proto/api and stores result in /src/proto/dist

protobuf is required!


yarn build:all

build:node, build:wasm and build:protoc.

Builds everything.

protobuf and go1.19.1 are required!


yarn gen-index

Generates index.js for exports automatically using ctix


yarn proto:watch

Compiles proto files on any file change


yarn lint

Runs ESLint


yarn prettier

Runs prettier


yarn docs

Generates docs using Typedoc and stores results in /docs

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests and docs as appropriate.

License

MIT

Tests

CodeQL

TestsSkippedFailuresErrorsTime
190 :zzz:0 :x:0 :fire:14.666s :stopwatch:
LinesStatementsBranchesFunctions
94.41% (186/197)62.5% (20/32)81.94% (59/72)
2.1.0

1 year ago

2.0.2

1 year ago

1.0.42

1 year ago

1.0.41

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.40

1 year ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

10.0.17

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago