Skip to content

signum-network/signumjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

signumjs

The Signum Network SDK for Javascript (written in Typescript)

Build Quality Gate Status Maintainability Rating Vulnerabilities Coverage npm jsDelivr Rate this package Featured on Openbase

@signumjs is a modern SDK written in Typescript providing common functionalities for browsers and nodejs to interact with the Signum Network blockchain, an advanced community-driven blockchain technology.


Best way to start is with the extensive Online Documentation


Packages

The SDK is separated in the following packages

  • @signumjs/core The main package providing an extensive API for blockchain interaction
  • @signumjs/contracts A package providing Signum relevant functions for smart contracts
  • @signumjs/crypto A package providing Signum relevant crypto functions
  • @signumjs/util A package providing useful functions, e.g. common conversion functions
  • @signumjs/http A package providing a simplified Http layer, with consistent response types, and exception handling
  • @signumjs/monitor A package providing a class to execute recurring async operations with de/serialization feature, good for listening to blockchain transactions
  • @signumjs/wallets This package provides the communication with SIP22 compatible deeplinkable
  • @signumjs/standards This package provides the communication with SIP22 compatible deeplinkable

Installation

@signumjs aims modern browsers and nodejs >= v14, but can also be used as bundled JavaScript using <script>

Using with NodeJS and/or modern web frameworks

Install using npm:

npm install @signumjs/core
npm install @signumjs/contracts (optional)
npm install @signumjs/crypto (optional)
npm install @signumjs/util (optional)
npm install @signumjs/http (optional)
npm install @signumjs/monitor (optional)
npm install @signumjs/wallets (optional)
npm install @signumjs/standards (optional)

or using yarn:

yarn add @signumjs/core
yarn add @signumjs/contracts (optional)
yarn add @signumjs/crypto (optional)
yarn add @signumjs/util (optional)
yarn add @signumjs/http (optional)
yarn add @signumjs/monitor (optional)
yarn add @signumjs/wallets (optional)
yarn add @signumjs/standards (optional)

Using in classic <script>

Each package is available as bundled standalone library using UMD. This way SignumJS can be used also within <script>-Tags. This might be useful for Wordpress and/or other PHP applications.

Just import one of the packages using the HTML <script> tag.

<script src='https://cdn.jsdelivr.net/npm/@signumjs/core/dist/signumjs.min.js'></script>

<script src='https://cdn.jsdelivr.net/npm/@signumjs/contracts/dist/signumjs.contracts.min.js'></script>

<script src='https://cdn.jsdelivr.net/npm/@signumjs/crypto/dist/signumjs.crypto.min.js'></script>

<script src='https://cdn.jsdelivr.net/npm/@signumjs/http/dist/signumjs.http.min.js'></script>

<script src='https://cdn.jsdelivr.net/npm/@signumjs/util/dist/signumjs.util.min.js'></script>

<script src='https://cdn.jsdelivr.net/npm/@signumjs/monitor/dist/signumjs.monitor.min.js'></script>

<script src='https://cdn.jsdelivr.net/npm/@signumjs/wallets/dist/signumjs.wallets.min.js'></script>

<script src='https://cdn.jsdelivr.net/npm/@signumjs/standards/dist/signumjs.standards.min.js'></script>

Due to the way a package is imported following global variables are provided

Package Variable
core sig$
contracts sig$contracts
crypto sig$crypto
http sig$http
monitor sig$monitor
util sig$util
wallets sig$wallets
wallets sig$standards

Examples:

// using core
const ledger = sig$.LedgerClientFactory.create({
    nodeHost: "http://testnet.signum.network",
});

ledger.network.getBlockchainStatus().then(console.log);
// using contracts
const dataView = new sig$contracts.ContractDataView(contract)
console.log(dataView.getVariable(2))
// using crypto
console.log(sig$crypto.hashSHA256("test"))
// using util
const value = sig$util.Amount.fromSigna("1000")
// using http
const client = new sig$http.HttpClientFactory.createHttpClient('https://jsonplaceholder.typicode.com/');
client.get('/todos/1').then(console.log)
// using wallets
const wallet = new sig$wallets.GenericExtensionWallet()
const connection = await wallet.connect();
const subscription = connection.listen({
    onAccountChanged: (accountId, publicKey) => { /*...*/ }
})
// ...
subscription.unlisten()
// using standards - depends on ledger 
const ledger = sig$.LedgerClientFactory.create({
    nodeHost: "http://europe3.testnet.signum.network",
});

// create Descriptor data object
const descriptorData = sig$standards.DescriptorDataBuilder
    .create('ohager')
    .setType('hum')
    .setBackground('QmUFc4dyX7TJn5dPxp8CrcDeedoV18owTBUWApYMuF6Koc', 'image/jpeg')
    .setAvatar('QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR', 'image/gif')
    .setSocialMediaLinks(['https://somelink.com'])
    .setDescription('Just a humble dev...')
    .setHomePage('https://digital-independence.dev')
    .build();

// updates account descriptor
const client = new sig$standards.DescriptorDataClient(ledger)
const transaction = await client.setAccountDescriptor({
    descriptorData,
    senderPublicKey: '497d559d18d989b8....ed2716a4b2121902',
    senderPrivateKey: '**********************************'
});
 // using monitor

// A method that checks if an account exists
// > IMPORTANT: Do not use closures, when you need to serialize the monitor
import {LedgerClientFactory} from './ledgerClientFactory';

async function tryFetchAccount() {
    try {
        const ledger = LedgerClientFactory.create({nodeHost: 'https://europe3.testnet.signum.network/'})
        const {account} = await ledger.account.getAccount('1234')
        return account;
    } catch (e) {
        // ignore error
        return null;
    }
}

// A comparing function to check if a certain condition for the returned data from fetch function
// is true. If it's true the monitor stops
function checkIfAccountExists(account) {
    return account !== null;
}

// Create your monitor
const monitor = new Monitor<Account>({
    asyncFetcherFn: tryFetchAccount,
    compareFn: checkIfAccountExists,
    intervalSecs: 10, // polling interval in seconds
    key: 'monitor-account',
    timeoutSecs: 2 * 240 // when reached timeout the monitor stops
})
    .onFulfilled(() => {
        // called when `checkIfAccountExists` returns true
        console.log('Yay, account active');
    })
    .onTimeout(() => {
        // called when `timeoutSecs` is reached
        console.log('Hmm, something went wrong');
    }).start();

Usage

The following example shows how to interact with the blockchain, i.e. getting the balance of a specific account

ES6/NodeJS style

In a separate file, preferably index.js or main.js write your entry point like this:

import {LedgerClientFactory, ApiSettings} from '@signumjs/core'
import {Amount} from '@signumjs/util'

// this self-executing file makes turns this file into a starting point of your app
(async () => {
    try {
        const ledger = LedgerClientFactory.createClient({nodeHost: 'https://europe3.testnet.signum.network'});
        const {balanceNQT} = await ledger.account.getAccountBalance('13036514135565182944')
        console.log(`Account Balance: ${Amount.fromPlanck(balanceNQT).toString()}`)
    } catch (e) { // e is of type HttpError (as part of @signumjs/http)
        console.error(`Whooops, something went wrong: ${e.message}`)
    }
})()

<script> style

const ledger = sig$.LedgerClientFactory.create({nodeHost: 'https://europe3.testnet.signum.network'});
ledger.account.getAccountBalance('13036514135565182944')
    .then(balance => {
        console.log(`Account Balance: ${sig$util.Amount.fromPlanck(balance.balanceNQT).toString()}`)

    })
    .catch(e => { // e is of type HttpError (as part of @signumjs/http)
        console.error(`Whooops, something went wrong: ${e.message}`)
    })

Development

Contributors are warmly welcome. To start your local build just hit

npm install

That's it!

Building the packages

The SDK is using Lerna to manage all subpackages in a developer friendlier way:

npm run build

Running Tests

  1. Single test run npm run test
  2. Run in watch mode npm run test:watch
  3. Run end-to-end test npm run test:e2e | Keep in mind that these tests are slow as they run against true servers. And therefore, it cannot be guaranteed that all E2E tests always work

Publish

To publish all packages (using lerna and same version strategy) just run

npm run publish

Note: Only with a valid npm OTP token