spacemeshlib is a typescript library for the Spacemesh network.
npm install @andreivcodes/spacemeshlib
yarn add @andreivcodes/spacemeshlib
.wasm
fileed25519.wasm
through a TypeScript version of wasm_exex.js
createClients(NETWORK_URL, 443, true)
or derivePublicKey(SEED, 0)
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)
})
Typedoc generated docs are available here.
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
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.
Tests | Skipped | Failures | Errors | Time |
---|---|---|---|---|
19 | 0 :zzz: | 0 :x: | 0 :fire: | 14.666s :stopwatch: |
Lines | Statements | Branches | Functions |
---|---|---|---|
94.41% (186/197) | 62.5% (20/32) | 81.94% (59/72) |