dhealth-utils
v1.1.19
Published
Utility library for dHealth
Downloads
10
Readme
Installation
Prerequisites
- Install Node.js which includes Node Package Manager
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
Typescript
const mnemonic = AccountUtils.generateHDWalletMnemonic();
Javascript
const mnemonic = dhealth_utils.AccountUtils.generateHDWalletMnemonic();
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');
Typescript & Javascript
const masterAccountPK = wallet.getAccountPrivateKey();
Typescript & Javascript
const defaultAccountPK = wallet.getChildAccountPrivateKey();
Typescript & Javascript
const childAccount = wallet.getChildAccountPrivateKey('m/44\'/4343\'/0\'/0\'/0\'', NetworkType.TEST_NET);
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
Typescript
const acc = AccountUtil.generateAccount(NetworkType.TEST_NET);
Javascript
const acc = dhealth_utils.AccountUtil.generateAccount(dhealth_utils.NetworkType.TEST_NET);
Typescript
const accInfo = await AccountUtil.getAccountInfo('TA4J3PTVAHIVWDG3G7DOH3BAW7HWSKIQJWHIBNY');
Javascript
const accInfo = await dhealth_utils.AccountUtil.getAccountInfo('TA4J3PTVAHIVWDG3G7DOH3BAW7HWSKIQJWHIBNY');
Typescript
const address = AccountUtil.getWalletAddressFromPublicKey('414C930BB85456B6A3D03EEA025532F6D54F3A763612072895FC5808ED9367FD', NetworkType.TEST_NET);
Javascript
const address = dhealth_utils.AccountUtil.getWalletAddressFromPublicKey('414C930BB85456B6A3D03EEA025532F6D54F3A763612072895FC5808ED9367FD', dhealth_utils.NetworkType.TEST_NET);
Blockchain
Typescript
const genesisBlock = await BlockchainUtil.getGenesisBlock(NetworkType.TEST_NET);
Javascript
const genesisBlock = await dhealth_utils.BlockchainUtil.getGenesisBlock(NetworkType.TEST_NET);
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);
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
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
)
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);
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
Typescript
const node = await NetworkUtil.getNodeFromNetwork(NetworkType.TEST_NET);
Javascript
const node = await dhealth_utils.NetworkUtil.getNodeFromNetwork(NetworkType.TEST_NET);
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');
Typescript
const networkType = NetworkUtil.getNetworkTypeFromAddress('TA4J3PTVAHIVWDG3G7DOH3BAW7HWSKIQJWHIBNY');
Javascript
const networkType = dhealth_utils.NetworkUtil.getNetworkTypeFromAddress('TA4J3PTVAHIVWDG3G7DOH3BAW7HWSKIQJWHIBNY');
Transaction
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
);
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
);
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
);
Typescript
const transaction = incomingTxs[0];
const timestamp = await TransactionUtil.getTimestampFromTransaction(transaction);
Javascript
const transaction = incomingTxs[0];
const timestamp = await dhealth_utils.TransactionUtil.getTimestampFromTransaction(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
);
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);
});
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.