Skip to content

dHealth-Symbol-Lab/dhealth-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dHealth-utils - The dHealth chain utility.

dhealth-logo
dHealth chain utility is a development library for performing operations and writing applications
on dHealth network using Typescript and JavaScript.

www.dhealth.network

Submit an Issue . Resources . News

GitHub package.json version   npm   CircleCI
Coveralls  


Installation

Prerequisites

npm

npm i dhealth-utils

yarn

yarn add dhealth-utils

Import

Typescript

// Import util classes
import {...} from 'dhealth-utils';

// Import sdk classes
import {...} from '@dhealth/sdk';

Javascript

// Import util classes
const dhealth_utils = require('dhealth-utils');

// Import sdk classes
const {...} = require('@dhealth/sdk');

Usage

HD Wallet

Generate HD wallet mnemonic

Typescript

const mnemonic = AccountUtils.generateHDWalletMnemonic();

Javascript

const mnemonic = dhealth_utils.AccountUtils.generateHDWalletMnemonic();
Get HD wallet from mnemonic

Typescript

const wallet = AccountUtils.getHDWalletFromMnemonic('second snow city expect toward flash lava have pulp attack health timber');

Javascript

const wallet = dhealth_utils.AccountUtils.getHDWalletFromMnemonic('second snow city expect toward flash lava have pulp attack health timber');
Get master account private key from HD wallet

Typescript & Javascript

const masterAccountPK = wallet.getAccountPrivateKey();
Get default account private key from HD wallet

Typescript & Javascript

const defaultAccountPK = wallet.getChildAccountPrivateKey();
Derive specific child-path private key from HD wallet

Typescript & Javascript

const childAccount = wallet.getChildAccountPrivateKey('m/44\'/4343\'/0\'/0\'/0\'', NetworkType.TEST_NET);
Create SimpleWallet on account private key

Typescript

import {NetworkType, Password, SimpleWallet} from '@dhealth/sdk';

const privateKey = 'your-private-key';
const simpleWallet = SimpleWallet.createFromPrivateKey('name', new Password('37777777'), privateKey, NetworkType.TEST_NET);

Javascript

const {NetworkType, Password, SimpleWallet} = require('@dhealth/sdk');

const privateKey = 'your-private-key';
const simpleWallet = dhealth_utils.SimpleWallet.createFromPrivateKey('name', new dhealth_utils.Password('37777777'), privateKey, dhealth_utils.NetworkType.TEST_NET);

Account

Create a new account

Typescript

const acc = AccountUtil.generateAccount(NetworkType.TEST_NET);

Javascript

const acc = dhealth_utils.AccountUtil.generateAccount(dhealth_utils.NetworkType.TEST_NET);
Get Account info

Typescript

const accInfo = await AccountUtil.getAccountInfo('TA4J3PTVAHIVWDG3G7DOH3BAW7HWSKIQJWHIBNY');

Javascript

const accInfo = await dhealth_utils.AccountUtil.getAccountInfo('TA4J3PTVAHIVWDG3G7DOH3BAW7HWSKIQJWHIBNY');
Get Address from public key

Typescript

const address = AccountUtil.getWalletAddressFromPublicKey('414C930BB85456B6A3D03EEA025532F6D54F3A763612072895FC5808ED9367FD', NetworkType.TEST_NET);

Javascript

const address = dhealth_utils.AccountUtil.getWalletAddressFromPublicKey('414C930BB85456B6A3D03EEA025532F6D54F3A763612072895FC5808ED9367FD', dhealth_utils.NetworkType.TEST_NET);

Blockchain

Get genesis block

Typescript

const genesisBlock = await BlockchainUtil.getGenesisBlock(NetworkType.TEST_NET);

Javascript

const genesisBlock = await dhealth_utils.BlockchainUtil.getGenesisBlock(NetworkType.TEST_NET);
Get latest block
Typescript
const block = await BlockchainUtil.getLatestBlock(NetworkType.TEST_NET);

console.log(block);

Javascript

const block = await dhealth_utils.BlockchainUtil.getLatestBlock(dhealth_utils.NetworkType.TEST_NET);

console.log(block);
Get network timestamp of a block

Typescript

// block number to get timestamp
const height = 1;

// block instance
const block = await BlockchainUtil.getBlockByHeightUInt64(
    NetworkType.TEST_NET,
    height
);

// block timestamp in normal (dHealth network has a separate timestamp)
const timestampUInt64 = block.timestamp;

// get timestamp of block
const networkTimestamp = NetworkUtil.getNetworkTimestampFromUInt64(
    NetworkType.TEST_NET, timestampUInt64
);

// print result
console.log(networkTimestamp);

Javascript

// block number to get timestamp
const height = 1;

// block instance
const block = await dhealth_utils.BlockchainUtil.getBlockByHeightUInt64(
    dhealth_utils.NetworkType.TEST_NET,
    height
);

// block timestamp in normal (dHealth network has a separate timestamp)
const timestampUInt64 = block.timestamp;

// get timestamp of block
const networkTimestamp = dhealth_utils.NetworkUtil.getNetworkTimestampFromUInt64(
    dhealth_utils.NetworkType.TEST_NET, timestampUInt64
);

// print result
console.log(networkTimestamp);

Mosaic

Create mosaic

Typescript

const network = NetworkType.TEST_NET;
const privateKey = '09E8303C4D6ECB45F8431A1C27380CB91C941F595A2E5AA6384C73F3AD907126';
const durationBlock = 0;
const isSupplyMutable = false;
const isTransferable = true;
const isRestrictable = false;
const divisibility = 6;
const supply = 100000000;

const result = await MosaicUtil.createMosaic(
    network,
    privateKey,
    durationBlock,
    isSupplyMutable,
    isTransferable,
    isRestrictable,
    divisibility,
    supply
)

Javascript

const network = NetworkType.TEST_NET;
const privateKey = '09E8303C4D6ECB45F8431A1C27380CB91C941F595A2E5AA6384C73F3AD907126';
const durationBlock = 0;
const isSupplyMutable = false;
const isTransferable = true;
const isRestrictable = false;
const divisibility = 6;
const supply = 100000000;

const result = await dhealth_utils.MosaicUtil.createMosaic(
    network,
    privateKey,
    durationBlock,
    isSupplyMutable,
    isTransferable,
    isRestrictable,
    divisibility,
    supply
)
Get mosaic ID from name

Typescript

const nodeUrl = 'https://api-01.dhealth.dev:3001';
const mosaicName = 'dhealth.dhp';
const mosaicIdHex = await MosaicUtil.getMosaicIdFromNamespace(nodeUrl, mosaicName);

Javascript

const nodeUrl = 'https://api-01.dhealth.dev:3001';
const mosaicName = 'dhealth.dhp';
const mosaicIdHex = await dhealth_utils.MosaicUtil.getMosaicIdFromNamespace(nodeUrl, mosaicName);
Get mosaic info from ID

Typescript

const mosaicIdHex = '5A4935C1D66E6AC4';
const mosaicInfo = await MosaicUtil.getMosaicInfo(NetworkType.TEST_NET, mosaicIdHex);

Javascript

const mosaicIdHex = '5A4935C1D66E6AC4';
const mosaicInfo = await dhealth_utils.MosaicUtil.getMosaicInfo(dhealth_utils.NetworkType.TEST_NET, mosaicIdHex);

Network

Get an available node from network

Typescript

const node = await NetworkUtil.getNodeFromNetwork(NetworkType.TEST_NET);

Javascript

const node = await dhealth_utils.NetworkUtil.getNodeFromNetwork(NetworkType.TEST_NET);
Check if a node is up

Typescript

const isNodeUp = await NetworkUtil.nodeIsUp('https://api-01.dhealth.dev:3001');

Javascript

const isNodeUp = await dhealth_utils.NetworkUtil.nodeIsUp('https://api-01.dhealth.dev:3001');
Get network type from a wallet address

Typescript

const networkType = NetworkUtil.getNetworkTypeFromAddress('TA4J3PTVAHIVWDG3G7DOH3BAW7HWSKIQJWHIBNY');

Javascript

const networkType = dhealth_utils.NetworkUtil.getNetworkTypeFromAddress('TA4J3PTVAHIVWDG3G7DOH3BAW7HWSKIQJWHIBNY');

Transaction

Get all incoming transactions

Typescript

const address = 'TBEFN3SSXFFEIUOJQLXSZBRJGN56G4XHW647OQQ';
const transactionGroup = TransactionGroup.Confirmed;
const pageNumber = 1;
const pageSize = 100;
// optional - can leave null
const mosaicIdHex = '5A4935C1D66E6AC4';
const imcomingTxs = await TransactionUtil.getIncomingTransactions(
    address,
    transactionGroup,
    pageNumber,
    pageSize,
    mosaicIdHex
);

Javascript

const address = 'TBEFN3SSXFFEIUOJQLXSZBRJGN56G4XHW647OQQ';
const transactionGroup = dhealth_utils.TransactionGroup.Confirmed;
const pageNumber = 1;
const pageSize = 100;
// optional - can leave null
const mosaicIdHex = '5A4935C1D66E6AC4';
const incomingTxs = await dhealth_utils.TransactionUtil.getIncomingTransactions(
    address,
    transactionGroup,
    pageNumber,
    pageSize,
    mosaicIdHex
);
Get all outgoing transactions

Typescript

const address = 'TBEFN3SSXFFEIUOJQLXSZBRJGN56G4XHW647OQQ';
const transactionGroup = TransactionGroup.Confirmed;
const pageNumber = 1;
const pageSize = 100;
// optional - can leave null
const mosaicIdHex = '5A4935C1D66E6AC4';

const outgoingTxs = await TransactionUtil.getOutgoingTransactions(
    address,
    transactionGroup,
    pageNumber,
    pageSize,
    mosaicIdHex
);

Javascript

const address = 'TBEFN3SSXFFEIUOJQLXSZBRJGN56G4XHW647OQQ';
const transactionGroup = dhealth_utils.TransactionGroup.Confirmed;
const pageNumber = 1;
const pageSize = 100;
// optional - can leave null
const mosaicIdHex = '5A4935C1D66E6AC4';

const outgoingTxs = await dhealth_utils.TransactionUtil.getOutgoingTransactions(
    address,
    transactionGroup,
    pageNumber,
    pageSize,
    mosaicIdHex
);
Get transactions with custom search criteria

Find out more about: TransactionSearchCriteria.

Properties
address embedded fromHeight fromTransferAmount group height
offset order pageNumber pageSize recipientAddress signerPublicKey
toHeight toTransferAmount transferMosaicId type

Typescript

const networkType = NetworkType.TEST_NET;

// You can modify/add more fields based on your needs
const searchCriteria = {
    recipientAddress: address,
    group: group,
    pageNumber: pageNumber,
    pageSize: pageSize,
    mosaicIdHex: mosaicIdHex
}

const txs = await TransactionUtil.getTransactions(
    networkType,
    searchCriteria
);

Javascript

const networkType = dhealth_utils.NetworkType.TEST_NET;

// You can modify/add more fields based on your needs
const searchCriteria = {
    recipientAddress: address,
    group: group,
    pageNumber: pageNumber,
    pageSize: pageSize,
    mosaicIdHex: mosaicIdHex
}

const txs = await dhealth_utils.TransactionUtil.getTransactions(
    networkType,
    searchCriteria
);
Get timestamp of a transaction

Typescript

const transaction = incomingTxs[0];
const timestamp = await TransactionUtil.getTimestampFromTransaction(transaction);

Javascript

const transaction = incomingTxs[0];
const timestamp = await dhealth_utils.TransactionUtil.getTimestampFromTransaction(transaction);
Create a transfer transaction

Typescript

const transactionCreationParams = {
  networkType: NetworkType.TEST_NET,
  maxFee: 100000, // 0.1 dhp - 1 million basic units equal 1 dhp
  recipientAddress: 'TBEFN3SSXFFEIUOJQLXSZBRJGN56G4XHW647OQQ',
  mosaicDetails: [{mosaicId: '5A4935C1D66E6AC4', amount: 100000}],
  plainMessage: `test create transfer tx - ${new Date().getTime()}`
}

const result = await TransactionUtil.createTransaction(
  TransferTransaction,
  transactionCreationParams
);

Javascript

const transactionCreationParams = {
  networkType: NetworkType.TEST_NET,
  maxFee: 100000, // 0.1 dhp - 1 million basic units equal 1 dhp
  recipientAddress: 'TBEFN3SSXFFEIUOJQLXSZBRJGN56G4XHW647OQQ',
  mosaicDetails: [{mosaicId: '5A4935C1D66E6AC4', amount: 100000}],
  plainMessage: `test create transfer tx - ${new Date().getTime()}`
}

const result = await dhealth_utils.TransactionUtil.createTransaction(
  TransferTransaction,
  transactionCreationParams
);
Create and announce a transfer transaction

Typescript - with mosaic ID

const privateKey = '008D53A06B75DAB055034F436B85DFA77E027A8485B16C6604C35A1D2483254B';
const transactionCreationParams = {
  networkType: NetworkType.TEST_NET,
  maxFee: 100000, // 0.1 dhp - 1 million basic units equal 1 dhp
  recipientAddress: 'TBEFN3SSXFFEIUOJQLXSZBRJGN56G4XHW647OQQ',
  mosaicDetails: [{mosaicId: '5A4935C1D66E6AC4', amount: 100000}],
  plainMessage: `test create transfer tx - ${new Date().getTime()}`
}

const result = await TransactionUtil.createAndAnnounceTransaction(
  TransferTransaction,
  transactionCreationParams,
  privateKey
).catch(err => {
  console.log(err);
});

Typescript - with namespace name

const privateKey = '008D53A06B75DAB055034F436B85DFA77E027A8485B16C6604C35A1D2483254B';
const transactionCreationParams = {
  networkType: NetworkType.TEST_NET,
  maxFee: 100000, // 0.1 dhp - 1 million basic units equal 1 dhp
  recipientAddress: 'TBEFN3SSXFFEIUOJQLXSZBRJGN56G4XHW647OQQ',
  mosaicDetails: [{namespaceName: 'dhealth.dhp', amount: 100000}],
  plainMessage: `test create transfer tx - ${new Date().getTime()}`
}

const result = await TransactionUtil.createAndAnnounceTransaction(
  TransferTransaction,
  transactionCreationParams,
  privateKey
).catch(err => {
  console.log(err);
});

Javascript - with mosaic ID

const privateKey = '008D53A06B75DAB055034F436B85DFA77E027A8485B16C6604C35A1D2483254B';
const transactionCreationParams = {
  networkType: NetworkType.TEST_NET,
  maxFee: 100000, // 0.1 dhp - 1 million basic units equal 1 dhp
  recipientAddress: 'TBEFN3SSXFFEIUOJQLXSZBRJGN56G4XHW647OQQ',
  mosaicDetails: [{mosaicId: '5A4935C1D66E6AC4', amount: 100000}],
  plainMessage: `test create transfer tx - ${new Date().getTime()}`
}

const result = await dhealth_utils.TransactionUtil.createAndAnnounceTransaction(
  TransferTransaction,
  transactionCreationParams,
  privateKey
).catch(err => {
  console.log(err);
});

Javascript - with namespace name

const privateKey = '008D53A06B75DAB055034F436B85DFA77E027A8485B16C6604C35A1D2483254B';
const transactionCreationParams = {
  networkType: NetworkType.TEST_NET,
  maxFee: 100000, // 0.1 dhp - 1 million basic units equal 1 dhp
  recipientAddress: 'TBEFN3SSXFFEIUOJQLXSZBRJGN56G4XHW647OQQ',
  mosaicDetails: [{namespaceName: 'dhealth.dhp', amount: 100000}],
  plainMessage: `test create transfer tx - ${new Date().getTime()}`
}

const result = await dhealth_utils.TransactionUtil.createAndAnnounceTransaction(
  TransferTransaction,
  transactionCreationParams,
  privateKey
).catch(err => {
  console.log(err);
});
Create a transfer URI (to be imported in wallet)

Typescript

import { NetworkType, Mosaic, MosaicId, UInt64 } from '@dhealth/sdk';
...
const transaction = await TransactionUtil.createTransferTransaction(
  NetworkType.TEST_NET,
  'TBEFN3SSXFFEIUOJQLXSZBRJGN56G4XHW647OQQ',
  [new Mosaic(new MosaicId('5A4935C1D66E6AC4'), UInt64.fromUint(1000000))],
  'test',
  10000
);
const txURI = TransactionUtil.createTransactionURI(transaction);
console.log('txURI: ', txURI);

Javascript

const { NetworkType, Mosaic, MosaicId, UInt64 } = require('@dhealth/sdk');
...
const transaction = await dhealth_utils.TransactionUtil.createTransferTransaction(
  NetworkType.TEST_NET,
  'TBEFN3SSXFFEIUOJQLXSZBRJGN56G4XHW647OQQ',
  [new Mosaic(new MosaicId('5A4935C1D66E6AC4'), UInt64.fromUint(1000000))],
  'test',
  10000
);
const txURI = dhealth_utils.TransactionUtil.createTransactionURI(transaction);
console.log('txURI: ', txURI);

Community

Join the conversation and help the community.

Twitter Follow

Facebook Love Angular badge