Crypto.com Chain JS library
1. Quick Guide
1.0. Installing the library ⬇️
npm install @crypto-com/chain-jslib
1.1. Working with private keys and key pairs 🔐
// Importsconst sdk = ;const HDKey = sdkHDKey;const Secp256k1KeyPair = sdkSecp256k1KeyPair;const Bytes = sdkutilsBytes; // Initializing the library configurations with TestNet configconst cro = sdk; // Generating a random HD Keylet randomHDKey = HDKey; // Import an HDKey from a previous mnemonic phraseconst importedHDKey = HDKey; // Derive a private key from an HDKey at the specified pathconst privateKey = importedHDKey; // Getting a keyPair from a private keyconst keyPair = Secp256k1KeyPair;
1.2. Generating an address 🔖
// Initializing the library configurations with TestNet configconst cro = sdk; // Import private key from hex key valuelet privKey = Bytes;// Get keyPair from the imported private keyconst importedKeyPair = Secp256k1KeyPair; // Generate address from the imported key pairlet address = importedKeyPair;console; // tcro1sxe3v6gka3u8j7d2xhl8rmfyjnmggqlh6e82hq
1.3. Build and Sign a transfer transaction ✅
// Imports const sdk = ;const HDKey = sdkHDKey;const Secp256k1KeyPair = sdkSecp256k1KeyPair;const Units = sdkUnits;const Big = sdkutilsBig; // Initialize the library configurations with TestNet configsconst cro = sdk; const importedHDKey = HDKey; // Derive a private key from an HDKey at the specified pathconst privateKey = importedHDKey; // Getting a keyPair from a private keyconst keyPair = Secp256k1KeyPair; // Init Raw transactionconst rawTx = ; const feeAmount = "6500" UnitsBASE; // Custom properties setrawTx;rawTx;rawTx;rawTx; const msgSend = fromAddress: "tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3" toAddress: "tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3" amount: "1210" UnitsBASE; const signableTx = rawTx ; const signedTx = signableTx ; console;// 0aa4010a8c010a1c2f636f736d6f732e62616e6b2e763162657461312e4d736753656e64126c0a2b7463726f313635747a63726832796c3833673871657178756567326735677a6775353779336665336b6333122b7463726f313635747a63726832796c3833673871657178756567326735677a6775353779336665336b63331a100a08626173657463726f120431323130120f48656c6c6f2054657374204d656d6f1896ef14126a0a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a2103c3d281a28592adce81bee3094f00eae26932cbc682fba239b90f47dac9fe703612040a020801180d12160a100a08626173657463726f12043635303010c08b111a40fe9b30f29bb9a83df3685f5bf8b7e6c34bae9ee8ba93115af4136289354c5bf947698ef3a3c0a1f6092ba7a2069616c436f4bcf6f3ecef11b92ad4d319ec0347 // Note that the result of signedTx.getHexEncoded() can be directly broadcasted to the network as a raw tx
2. Cosmos Protobuf Definitions
Generate Cosmos Protobuf Definitions in JavaScript
-
Download Cosmos proto definitions folder
npm run get-proto -
Generate definitions files in JavaScript
npm run define-proto
Update Supported Modules
-
To support more Cosmos modules, edit
lib/src/cosmos/v1beta1/scripts/predefine-proto.sh
and append the lines"$COSMOS_PROTO_DIR/bank/v1beta1/bank.proto" \ "$COSMOS_PROTO_DIR/bank/v1beta1/tx.proto" \
In this example it is adding
bank
module support, replace the paths with the modules and its protbuf files accordingly. -
edit
lib/src/cosmos/v1beta1/types/typeurls.ts
to add the protobuf type URLs to JS definitions mapping
3. API Documentation
The library API documentation can be generated by running
npm run docs:build
The resulting generated documentation will be created in the docs/dist
directory