dHealth chain utility is a development library for performing operations and writing applications
on dHealth network using Typescript and JavaScript.
Submit an Issue
.
Resources
.
News
- Install Node.js which includes Node Package Manager
npm i dhealth-utils
yarn add dhealth-utils
// Import util classes
import {...} from 'dhealth-utils';
// Import sdk classes
import {...} from '@dhealth/sdk';
// Import util classes
const dhealth_utils = require('dhealth-utils');
// Import sdk classes
const {...} = require('@dhealth/sdk');
Generate HD wallet mnemonic
const mnemonic = AccountUtils.generateHDWalletMnemonic();
const mnemonic = dhealth_utils.AccountUtils.generateHDWalletMnemonic();
Get HD wallet from mnemonic
const wallet = AccountUtils.getHDWalletFromMnemonic('second snow city expect toward flash lava have pulp attack health timber');
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
const masterAccountPK = wallet.getAccountPrivateKey();
Get default account private key from HD wallet
const defaultAccountPK = wallet.getChildAccountPrivateKey();
Derive specific child-path private key from HD wallet
const childAccount = wallet.getChildAccountPrivateKey('m/44\'/4343\'/0\'/0\'/0\'', NetworkType.TEST_NET);
Create SimpleWallet on account private key
import {NetworkType, Password, SimpleWallet} from '@dhealth/sdk';
const privateKey = 'your-private-key';
const simpleWallet = SimpleWallet.createFromPrivateKey('name', new Password('37777777'), privateKey, NetworkType.TEST_NET);
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);
Create a new account
const acc = AccountUtil.generateAccount(NetworkType.TEST_NET);
const acc = dhealth_utils.AccountUtil.generateAccount(dhealth_utils.NetworkType.TEST_NET);
Get Account info
const accInfo = await AccountUtil.getAccountInfo('TA4J3PTVAHIVWDG3G7DOH3BAW7HWSKIQJWHIBNY');
const accInfo = await dhealth_utils.AccountUtil.getAccountInfo('TA4J3PTVAHIVWDG3G7DOH3BAW7HWSKIQJWHIBNY');
Get Address from public key
const address = AccountUtil.getWalletAddressFromPublicKey('414C930BB85456B6A3D03EEA025532F6D54F3A763612072895FC5808ED9367FD', NetworkType.TEST_NET);
const address = dhealth_utils.AccountUtil.getWalletAddressFromPublicKey('414C930BB85456B6A3D03EEA025532F6D54F3A763612072895FC5808ED9367FD', dhealth_utils.NetworkType.TEST_NET);
Get genesis block
const genesisBlock = await BlockchainUtil.getGenesisBlock(NetworkType.TEST_NET);
const genesisBlock = await dhealth_utils.BlockchainUtil.getGenesisBlock(NetworkType.TEST_NET);
Get latest block
const block = await BlockchainUtil.getLatestBlock(NetworkType.TEST_NET);
console.log(block);
const block = await dhealth_utils.BlockchainUtil.getLatestBlock(dhealth_utils.NetworkType.TEST_NET);
console.log(block);
Get network timestamp of a block
// 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);
// 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);
Create mosaic
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
)
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
const nodeUrl = 'https://api-01.dhealth.dev:3001';
const mosaicName = 'dhealth.dhp';
const mosaicIdHex = await MosaicUtil.getMosaicIdFromNamespace(nodeUrl, mosaicName);
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
const mosaicIdHex = '5A4935C1D66E6AC4';
const mosaicInfo = await MosaicUtil.getMosaicInfo(NetworkType.TEST_NET, mosaicIdHex);
const mosaicIdHex = '5A4935C1D66E6AC4';
const mosaicInfo = await dhealth_utils.MosaicUtil.getMosaicInfo(dhealth_utils.NetworkType.TEST_NET, mosaicIdHex);
Get an available node from network
const node = await NetworkUtil.getNodeFromNetwork(NetworkType.TEST_NET);
const node = await dhealth_utils.NetworkUtil.getNodeFromNetwork(NetworkType.TEST_NET);
Check if a node is up
const isNodeUp = await NetworkUtil.nodeIsUp('https://api-01.dhealth.dev:3001');
const isNodeUp = await dhealth_utils.NetworkUtil.nodeIsUp('https://api-01.dhealth.dev:3001');
Get network type from a wallet address
const networkType = NetworkUtil.getNetworkTypeFromAddress('TA4J3PTVAHIVWDG3G7DOH3BAW7HWSKIQJWHIBNY');
const networkType = dhealth_utils.NetworkUtil.getNetworkTypeFromAddress('TA4J3PTVAHIVWDG3G7DOH3BAW7HWSKIQJWHIBNY');
Get all incoming transactions
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
);
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
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
);
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 |
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
);
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
const transaction = incomingTxs[0];
const timestamp = await TransactionUtil.getTimestampFromTransaction(transaction);
const transaction = incomingTxs[0];
const timestamp = await dhealth_utils.TransactionUtil.getTimestampFromTransaction(transaction);
Create a transfer transaction
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
);
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
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);
});
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);
});
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);
});
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)
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);
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);
Join the conversation and help the community.