2.1.4 • Published 2 years ago

mui-metablockchain-sdk v2.1.4

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

MUI Metablockchain SDK

Integration Test npm version

JS SDK to interface with MetaMUI Metablockchain node.

Install

npm install mui-metablockchain-sdk

Usage

Network options

The best practice is to first create a websocket connection to the network you intend to connect with and then pass this object to all function calls. The available network options are local, dev, testnet, mainnet If no network is specified, the functions default to either dev or local. In future when multiple nodes are being hosted, the user will have to option to pass url of the node itself.

Example

  • Create a DID and write on chain
const { connection, did, config } = require('mui-metablockchain-sdk');
const NETWORK = "dev";

async function create_new_did(){

const mnemonic = "//Bob";
const new_did = "sample";

// create the DID object
let did_obj = await did.generateDID(mnemonic, new_did, "");

//Use the validator account to sign did operation
const keyring = await config.initKeyring();
const sig_key_pair = await keyring.addFromUri('//Alice');

let provider = await connection.buildConnection(NETWORK);
let tx_hash = await did.storeDIDOnChain(did_obj, sig_key_pair, provider);

return tx_hash;
}

Test

npm test

Modules

  • Connection
  • Balance
  • Collective
  • DID
  • token
  • Transactions
  • VC
  • Utils
  • Schema
  • SSID VC

Connection Utilities

Functions

buildConnection(network, ignoreCache)

Return an APIPromise object

Kind: global function

ParamTypeDefaultDescription
networkStringlocalMetaMUI network provider to connect
ignoreCacheBoolenfalse(optional) (default=true) Note : setting the ignoreCache value to true will create a new ws ws conection on every call

Balance Utilities

Functions

getBalance(did, api) ⇒ String

Get account balance(Highest Form) based on the did supplied.

Kind: global function
Returns: String - Balance In Highest Form

ParamTypeDefaultDescription
didStringIdentifier of the user
apiApiPromsefalseApi Object from Build Connection

Example

await getBalanceFromDID(did, api)

subscribeToBalanceChanges(identifier, callback, api)

Listen to balance changes for a DID and execute the callback.

Kind: global function

ParamTypeDefaultDescription
identifierStringDID
callbackfunctionCb function to execute with new balance in Highest Form
apiApiPromisefalseApi Object from Build Connection

Collective Utilities

Functions

setMembers(newMembers, prime, oldCount, signingKeypair) ⇒ String

Set Members and prime of collective pallet

Kind: global function
Returns: String - Hash

ParamTypeDescription
newMembersArray.<String>Array of Did
primeStringDid of Prime
oldCountNumberOld members count
signingKeypairKeyPairKey pair of Sender

propose(threshold, proposal, lengthCount, signingKeypair)

To create a proposal

Kind: global function

ParamTypeDescription
thresholdNumberThreshold to successfull execution
proposalCallCall to propose
lengthCountNumberLength of call
signingKeypairKeyPairKey pair of sender

execute(proposal, lengthCount, signingKeypair)

To Execute a call

Kind: global function

ParamTypeDescription
proposalCallCall to propose
lengthCountNumberLength of Call
signingKeypairKeyPairKey pair of sender

vote(proposalHash, index, approve, signingKeypair)

Vote on a proposal

Kind: global function

ParamTypeDescription
proposalHashStringHash of proposal
indexNumberProposal index
approveBooleanTrue/false
signingKeypairKeyPairKey pair of sender

close(proposalHash, index, proposalWeightBond, lengthCount, signingKeypair)

Close a proposal manually, executes call if yes votes is greater than or equal to threshold

Kind: global function

ParamTypeDescription
proposalHashStringHash
indexNumberProposal index
proposalWeightBondBooleanWeight
lengthCountNumberLength
signingKeypairKeyPairKey pair of sender

disapproveProposal(proposalHash, signingKeypair)

Disapprove proposal

Kind: global function

ParamTypeDescription
proposalHashStringHash
signingKeypairKeyPairKey pair of sender

getMembers(api)

Get Members of Council

Kind: global function

ParamTypeDefaultDescription
apiBooleanfalseNetwork Provider

getPrime(api)

Get Prime of Council

Kind: global function

ParamTypeDefaultDescription
apiBooleanfalseNetwork Provider

getProposals(api)

Get All Proposals

Kind: global function

ParamTypeDefaultDescription
apiBooleanfalseNetwork Provider

getProposalOf(proposalHash, api)

Get Proposal of given hash

Kind: global function

ParamTypeDefaultDescription
proposalHashHashHash of proposal
apiBooleanfalseNetwork Provider

getVotes(proposalHash, api)

Get Votes of given proposal hash

Kind: global function

ParamTypeDefaultDescription
proposalHashHashHash of proposal
apiBooleanfalseNetwork Provider

getProposalCount(api)

Get Total proposals count

Kind: global function

ParamTypeDefaultDescription
apiBooleanfalseNetwork Provider

DID Utilties

Functions

generateMnemonic() ⇒ String

Generate Mnemonic

Kind: global function
Returns: String - Mnemonic

generateDID(mnemonic, identifier, metadata) ⇒ Object

Generate did object to be stored in blockchain.

Kind: global function
Returns: Object - Object containing did structure

ParamType
mnemonicString
identifierString
metadataString

storeDIDOnChain(DID, signingKeypair, api) ⇒ String

Store the generated DID object in blockchain

Kind: global function
Returns: String - txnId Txnid for storage operation.

ParamTypeDefault
DIDObject
signingKeypairObject
apiApiPromisefalse

getDIDDetails(identifier) ⇒ JSON

Get did information from accountID

Kind: global function

ParamTypeDescription
identifierStringDID Identifier

resolveDIDToAccount(identifier, api, blockNumber) ⇒ String

Get the accountId for a given DID

Kind: global function

ParamTypeDefault
identifierString
apiApiPromisefalse
blockNumberNumber

resolveAccountIdToDid(accountId, api) ⇒ String | Boolean

Get the DID associated to given accountID

Kind: global function
Returns: String | Boolean - (false if not found)

ParamTypeDefaultDescription
accountIdString(hex/base64 version works)
apiApiPromisefalse

updateDidKey(identifier, newKey, signingKeypair, api)

This function will rotate the keys assiged to a DID It should only be called by validator accounts, else will fail

Kind: global function

ParamTypeDescription
identifierString
newKeyUint8Array
signingKeypairKeyringObj// of a validator account
apiApiPromise

convertFixedSizeHex(data, size) ⇒ String

Convert to hex but return fixed size always, mimics substrate storage

Kind: global function

ParamTypeDefault
dataString
sizeInt64

sanitiseDid(did) ⇒ String

Checks if the given did is in hex format or not & converts it into valid hex format.

Note: This util function is needed since dependant module wont convert the utf did to hex anymore

Kind: global function
Returns: String - Hex did

ParamType
didString

isDidValidator(identifier, api) ⇒ Boolean

Check if the user is an approved validator

Kind: global function

ParamTypeDefault
identifierString
apiApiPromisefalse

getDidKeyHistory(identifier, api) ⇒ Array

Fetch the history of rotated keys for the specified DID

Kind: global function

ParamTypeDefault
identifierString
apiApiPromisefalse

updateMetadata(identifier, metadata, signingKeypair, api)

Kind: global function

ParamTypeDefaultDescription
identifierString
metadataString
signingKeypairKeyringObjof a validator account
apiApiPromisefalse

Token Utilities

Functions

issueToken(vcId, totalIssuanceAmt, senderAccountKeyPair, api) ⇒ hexString

Issue new token from given vc Id. Amount is in lowest form here but everywhere else it's in highest form

Kind: global function

ParamTypeDefaultDescription
vcIdString
totalIssuanceAmtStringAmount is in lowest form
senderAccountKeyPairKeyPair
apiAPIPromisefalse

transferToken(recipentDid, currencyCode, tokenAmount, senderAccountKeyPair, api) ⇒ hexString

Transfer token with given token_id to the recipent_did

Kind: global function

ParamTypeDefaultDescription
recipentDidString
currencyCodeString
tokenAmountStringIn Highest Form
senderAccountKeyPairKeyPair
apiAPIPromisefalse

transferTokenWithMemo(recipentDid, currencyCode, tokenAmount, memo, senderAccountKeyPair, api) ⇒ hexString

Transfer token with given token_id to the recipent_did with an additional memo

Kind: global function

ParamTypeDefaultDescription
recipentDidString
currencyCodeString
tokenAmountStringIn Highest Form
memoString
senderAccountKeyPairKeyPair
apiAPIPromisefalse

transferAll(recipentDid, currencyCode, senderAccountKeyPair, api) ⇒ hexString

Transfer all token with given vc_id to the recipent_did

Kind: global function

ParamTypeDefault
recipentDidString
currencyCodeString
senderAccountKeyPairKeyPair
apiAPIPromisefalse

slashToken(vcId, senderAccountKeyPair, api) ⇒ hexString

Slash token from given currency

Kind: global function

ParamTypeDefault
vcIdString
senderAccountKeyPairKeyPair
apiAPIPromisefalse

mintToken(vcId, senderAccountKeyPair, api) ⇒ hexString

Mint token to given currency

Kind: global function

ParamTypeDefault
vcIdString
senderAccountKeyPairKeyPair
apiAPIPromisefalse

getTokenBalance(did, currencyCode, api) ⇒ String

Get the token balance for a given token for given did

Kind: global function
Returns: String - Balance In Highest Form

ParamTypeDefault
didString
currencyCodeString
apiApiPromisefalse

getDetailedTokenBalance(did, currencyCode, api) ⇒ Object

Get the detailed token balance for a given token for given did

Kind: global function
Returns: Object - In Highest Form

ParamTypeDefault
didString
currencyCodeString
apiApiPromisefalse

getTokenList(api) ⇒ Array

Get the list of all active tokens in metablockchain network

Kind: global function
Returns: Array - { id: '1', name: 'XYZ' }, { id: '2', name: 'ABC' }

ParamTypeDefault
apiApiPromisefalse

getTokenData(currencyCode, api) ⇒ Object

Get the token by currency id in metablockchain network

Kind: global function

ParamTypeDefault
currencyCodeString
apiApiPromisefalse

getTokenTotalSupply(currencyCode, api) ⇒ String

Get the total issuance amount for given currency id

Kind: global function
Returns: String - TotalSupply In Highest Form

ParamTypeDefault
currencyCodeString
apiApiPromisefalse

getLocks(currencyCode, api) ⇒ Object

Get the lock for given currency id

Kind: global function

ParamTypeDefault
currencyCodeString
apiApiPromisefalse

getTokenIssuer(currencyCode, api) ⇒ String

Get the issuer for given token code

Kind: global function

ParamTypeDefault
currencyCodeString
apiApiPromisefalse

withdrawTreasuryReserve(destination, from, amount, senderAccountKeyPair, api) ⇒ String

Function to withdraw the treasury reserve amount locked at the time of token creation. Only a validator can call this operation succesfully.

Kind: global function
Returns: String - transaction_hex_id

ParamTypeDefaultDescription
destinationString(DID)
fromString(DID)
amountString(MUI amount)
senderAccountKeyPairKeyPair
apiApiPromisefalse

transferTokenWithVC(vcId, receiverDID, senderAccountKeyPair, api) ⇒ hexString

Transfer token of given currency to given Did from Currency owner account

Kind: global function

ParamTypeDefault
vcIdString
receiverDIDString
senderAccountKeyPairKeyPair
apiAPIPromisefalse

setBalance(dest, currencyCode, amount, senderAccountKeyPair, api) ⇒ hexString

Set balance of did with given token_id Can be called only token owner

Kind: global function

ParamTypeDefaultDescription
destString
currencyCodeString
amountStringIn Highest Form
senderAccountKeyPairKeyPair
apiAPIPromisefalse

sanitiseCCode(currency_code) ⇒ String

Checks if the given currency_code is in hex format or not & converts it into valid hex format.

Note: This util function is needed since dependant module wont convert the utf did to hex anymore

Kind: global function
Returns: String - Hex currency_code

ParamType
currency_codeString

getFormattedTokenAmount(currency_code, tokenAmount, provider) ⇒ Number

Formats the given amount into lowest form based on the decimals supported by given token.

Kind: global function
Returns: Number - tokenAmount in lowest form else error

ParamTypeDescription
currency_codeStringin the sanitised form
tokenAmountStringin the highest form
providerAPIPromise

removeToken(currencyCode, vcId, clearAccounts, signingKeypair, identity) ⇒ String

Remove token

Kind: global function
Returns: String - Hash

ParamTypeDefault
currencyCodeString
vcIdString
clearAccountsBool
signingKeypairKeyPair
identityString

Transaction Utilities

Functions

sendTransaction(senderAccountKeyPair, receiverDID, amount, api, nonce) ⇒ Uint8Array

The function will perform a metamui transfer operation from the account of senderAccount to the receiverDID. Note : balanceCheck has not been included in the checks since sender not having balance is handled in extrinsic, check test/transaction.js

Kind: global function

ParamTypeDefaultDescription
senderAccountKeyPairKeyPair
receiverDIDString
amountStringIn Lowest Form
apiAPIPromisefalse(optional)
nonceint(optional)

transfer(senderAccountKeyPair, receiverDID, amount, memo, api, nonce) ⇒ Uint8Array

This function is similar to sendTransaction except that it provides the user to add the memo to transfer functionality.

Kind: global function

ParamTypeDefaultDescription
senderAccountKeyPairKeyPair
receiverDIDString
amountStringIn Lowest Form
memoString
apiAPIPromisefalse(optional)
nonceint(optional)

Verifiable Credential (VC) Utilities

Functions

createTokenVC(TokenVC) ⇒ String

Encodes Token VC and pads with appropriate bytes

Kind: global function
Returns: String - Token VC Hex String

ParamTypeDescription
TokenVCObject
TokenVC.tokenNameString
TokenVC.reservableBalanceStringIn Highest Form
TokenVC.decimalString
TokenVC.currencyCodeString

createMintSlashVC(vcProperty) ⇒ String

Encodes Token VC and pads with appropriate bytes

Kind: global function
Returns: String - Token VC Hex String

ParamTypeDescription
vcPropertyObject
vcProperty.vcIdString
vcProperty.currencyCodeString
vcProperty.amountStringIn Highest Form

createTokenTransferVC(vcProperty) ⇒ String

Encodes Token VC and pads with appropriate bytes

Kind: global function
Returns: String - Token VC Hex String

ParamTypeDescription
vcPropertyObject
vcProperty.vcIdString
vcProperty.currencyCodeString
vcProperty.amountStringIn Highest Form

createGenericVC(vcProperty) ⇒ String

Encodes Generic VC and pads with appropriate bytes

Kind: global function
Returns: String - Token VC Hex String

ParamType
vcPropertyObject
vcProperty.cidString

generateVC(vcProperty, owner, issuers, vcType, sigKeypair) ⇒ String

Create VC

Kind: global function
Returns: String - VC Hex String

ParamTypeDescription
vcPropertyObject
ownerStringDid
issuersArray.<String>Array of Did
vcTypeStringTokenVC, MintTokens, SlashTokens, TokenTransferVC, GenericVC
sigKeypairKeyPairOwner Key Ring pair

approveVC(vcID, signingKeyPair, api) ⇒ String

Approve VC

Kind: global function
Returns: String - Transaction hash or Error

ParamTypeDescription
vcIDObjectvc_id of VC to be approved
signingKeyPairKeyPairIssuer Key Ring pair
apiAPIPromise

storeVC(vcHex, senderAccountKeyPair, api) ⇒ hexString

Store vc hex

Kind: global function

ParamTypeDefault
vcHexString
senderAccountKeyPairKeyPair
apiAPIPromisefalse

updateStatus(vcId, vcStatus, senderAccountKeyPair, api) ⇒ hexString

Update Status

Kind: global function

ParamTypeDefault
vcIdString
vcStatusString
senderAccountKeyPairKeyPair
apiAPIPromisefalse

getVCs(vcId, api) ⇒ String

Get VCs by VC id

Kind: global function
Returns: String - (false if not found)

ParamTypeDefaultDescription
vcIdString(hex/base64 version works)
apiApiPromisefalse

getVCIdsByDID(did, api) ⇒ String

Get VC Ids by did

Kind: global function
Returns: String - (false if not found)

ParamTypeDefaultDescription
didString(hex/base64 version works)
apiApiPromisefalse

getDIDByVCId(vcId, api) ⇒ String

Get DID by VC Id

Kind: global function
Returns: String - (false if not found)

ParamTypeDefaultDescription
vcIdString(hex/base64 version works)
apiApiPromisefalse

getVCHistoryByVCId(vcId, api) ⇒ String

Get VC history by VC Id

Kind: global function
Returns: String - (false if not found)

ParamTypeDefaultDescription
vcIdString(hex/base64 version works)
apiApiPromisefalse

getVCApprovers(vcId, api) ⇒ Array.<Did>

Get approved issuers of the VC

Kind: global function
Returns: Array.<Did> - approved issuer list

ParamTypeDefaultDescription
vcIdString(hex/base64 version works)
apiApiPromisefalse

getGenericVCDataByCId(vcId, api) ⇒ JSON

Get Generic vc data

Kind: global function
Returns: JSON - Generic VC data

ParamType
vcIdString
apiApiPromise

getGenericVCData(vcId, api) ⇒ JSON

Get Generic vc data

Kind: global function
Returns: JSON - Generic VC data

ParamTypeDefault
vcIdString
apiApiPromisefalse

verifyGenericVC(vcId, data, api) ⇒ Boolean

Verify Generic Vc data

Kind: global function
Returns: Boolean - true if verified

ParamTypeDefault
vcIdString
dataObject
apiApiPromisefalse

createVC(properties_json, schema_hash) ⇒ JSON

The function returns the VC in the expected format, the verifier and signature fields are left blank to be filled by signing function

Kind: global function

ParamType
properties_jsonJSON
schema_hashHex

signVC(vcJson, verifierDid, signingKeyPair) ⇒ JSON

Sign a VC using the given verifier_pvkey

Kind: global function

ParamType
vcJsonJSON
verifierDidString
signingKeyPairKeyPair

verifyVC(vcJson) ⇒ Boolean

Verify if the signature/verifier DID is valid and matches the given data in vc_json

Kind: global function
Returns: Boolean - true if valid VC

ParamType
vcJsonJSON

Other Utilities

Functions

bytesToHex(inputBytes)

Kind: global function

ParamTypeDescription
inputBytesBytesu8[]

hexToBytes(inputString)

Kind: global function

ParamType
inputStringString

base58ToBytes(bs58string)

Kind: global function

ParamType
bs58stringBase58

stringToBytes(inputString)

Kind: global function

ParamType
inputStringString

hexToString(hexString)

Kind: global function

ParamType
hexStringHex

vcHexToVcId(hexString)

Kind: global function

ParamType
hexStringHex

encodeData(data, typeKey) ⇒ String

Encodes object/ string of given type to hex

Kind: global function
Returns: String - Encoded Hex

ParamTypeDescription
dataObject | StringObject to be encoded
typeKeyStringKey from METABLOCKCHAIN_TYPES which represents type of data

decodeHex(hexValue, typeKey) ⇒ Object | String

Decodes hex of given type to it's corresponding object/value

Kind: global function
Returns: Object | String - Decoded Object/String

ParamTypeDescription
hexValueStringHex String to be decoded
typeKeyStringKey from METABLOCKCHAIN_TYPES which represents type of data

isUpperAndValid(str) ⇒

Checks if str is upper and only contains characters

Kind: global function
Returns: bool

Param
str

tidy(s) ⇒ Object | String

regex to remove unwanted hex bytes

Kind: global function
Returns: Object | String - Decoded tidy Object/String

ParamTypeDescription
sStringHex String to make tidy

getVCS(hexValue, typeKey) ⇒ Object | String

function that decodes hex of createTokenVC

Kind: global function
Returns: Object | String - Decoded Object/String

ParamTypeDescription
hexValueStringHex String to be decoded
typeKeyStringKey from METABLOCKCHAIN_TYPES which represents type of data

decodeVC(hexValue, typeKey) ⇒ Object | String

function that decodes hex of createVC where type is TokenVC to it's corresponding object/value

Kind: global function
Returns: Object | String - Decoded Object/String

ParamTypeDescription
hexValueStringHex String to be decoded
typeKeyStringKey from METABLOCKCHAIN_TYPES which represents type of data

sortObjectByKeys(unorderedObj) ⇒ Object

Sort object by keys

Kind: global function
Returns: Object - ordered object by key

ParamTypeDescription
unorderedObjObjectunordered object

generateObjectHash(unordered) ⇒ Object

generate blake hash of js object

Kind: global function
Returns: Object - ordered object by key

ParamTypeDescription
unorderedObjectunordered object

Schema

Functions

createNewSchema(schema_properties) ⇒ JSON

Create a new schema with the properties provided in the schema_properties json The stringified schema and its hash will be returned as required by the extrinsic.

Kind: global function

ParamType
schema_propertiesJSON

storeSchemaOnChain(schema, signingKeypair)

Write a new schema to the chain using the account provided This extrinsic can only be called by the accounts in the validator_set pallet

Kind: global function

ParamType
schemaJSON
signingKeypairString

doesSchemaExist(schemaHash) ⇒ Boolean

The function will returns the Boolean value based on valid/invalid schemaHash.

Kind: global function
Returns: Boolean - Will return true, if valid schemaHash

ParamType
schemaHashHex

SSID VC

Functions

createSsidVC(properties_json) ⇒ JSON

The function returns the VC in the expected format, the signature field is left blank to be filled by signing function

Kind: global function

ParamType
properties_jsonJSON

signSsidVC(vcJson, signerKeyPair) ⇒ JSON

Sign a VC using the given verifier_pvkey

Kind: global function

ParamType
vcJsonJSON
signerKeyPairKeyPair

verifySsidVC(vcJson) ⇒ Boolean

Verify if the signature is valid and matches the given public_key in ssid_vc

Kind: global function
Returns: Boolean - true if valid SSID_VC

ParamType
vcJsonJSON
2.1.4

2 years ago

2.1.3

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago