Skip to content

Utility Methods for skc-sdk and skc-trade-sdk

This document provides an overview of utility functions available in the skc-sdk and skc-trade-sdk libraries, along with examples of how to use them.


generateKeys

Generates Ethereum and Cosmos key pairs. Returns mnemonic if no parameters are provided.

javascript
// Import generateKeys from skc-trade-sdk
import { generateKeys } from "skc-trade-sdk";

// Import generateKeys from skc-sdk
import { generateKeys } from "skc-sdk";

// Generate new keys with random mnemonic
const keys = generateKeys();

// Generate keys with specific mnemonic and path
const keysWithParams = generateKeys(
  "test test test test test test test test test test test test",
  "m/44/0"
);

generateHdAddressIndex

Maps a UUID v4 to a 32-bit integer for HD wallet derivation.

javascript
// Import generateHdAddressIndex from skc-trade-sdk
import { generateHdAddressIndex } from "skc-trade-sdk";

// Import generateHdAddressIndex from skc-sdk
import { generateHdAddressIndex } from "skc-sdk";

const addressIndex = generateHdAddressIndex(
  "555d9159-bb35-40ea-bd8a-42dcd32649c4"
);

generateSingleUseEthPubKey

Generates a single-use Ethereum public key.

javascript
// Import generateSingleUseEthPubKey from skc-trade-sdk
import { generateSingleUseEthPubKey } from "skc-trade-sdk";

// Import generateSingleUseEthPubKey from skc-sdk
import { generateSingleUseEthPubKey } from "skc-sdk";

const singleUseKey = generateSingleUseEthPubKey(
  "0x03364d3db1a3d49a53432473d953d680be3eebdae6775b243c2087b9ae92431f1a",
  "555d9159-bb35-40ea-bd8a-42dcd32649c4"
);

generateSalt

Generates a SHA-256 hash of the input passed.

javascript
// Import utils from skc-trade-sdk
import { utils } from "skc-trade-sdk";

// Import utils from skc-sdk
import { utils } from "skc-sdk";

const salt = await utils.generateSalt("your-input-string");

replacer

Replaces bigint values with their string representation during JSON serialization.

javascript
// Import utils from skc-trade-sdk
import { utils } from "skc-trade-sdk";

// Import utils from skc-sdk
import { utils } from "skc-sdk";

const bigIntReplacer = await utils.replacer("key", "big-int-value");

encryptWithPublicKey

Encrypts a message using ECDH and AES-256-CBC.

javascript
// Import utils from skc-trade-sdk
import { utils } from "skc-trade-sdk";

// Import utils from skc-sdk
import { utils } from "skc-sdk";

const publicKey = Buffer.from("your-public-key-in-hex", "hex");
const message = "Hello, secure world!";
const encrypted = await utils.encryptWithPublicKey(publicKey, message);

decryptWithPrivateKey

Decrypts a message encrypted using ECDH and AES-256-CBC.

javascript
// Import utils from skc-trade-sdk
import { utils } from "skc-trade-sdk";

// Import utils from skc-sdk
import { utils } from "skc-sdk";

const privateKey = Buffer.from("your-private-key-in-hex", "hex");
const cipherText = "your-cipher-text-hex-string";
const decrypted = await utils.decryptWithPrivateKey(privateKey, cipherText);

parse

Parses a serialized cipher hex string into its components.

javascript
// Import utils from skc-trade-sdk
import { utils } from "skc-trade-sdk";

// Import utils from skc-sdk
import { utils } from "skc-sdk";

const cipherText = "cipher-text-hex-string";
const parsedCipher = utils.parse(cipherText);

splitHexPrefix

Removes the 0x prefix from a hexadecimal string, if it exists.

javascript
// Import utils from skc-trade-sdk
import { utils } from "skc-trade-sdk";

// Import utils from skc-sdk
import { utils } from "skc-sdk";

const hexString = "0xabcdef";
const cleanHex = utils.splitHexPrefix(hexString);

stringify

Serializes a cipher object to a hex string.

javascript
// Import utils from skc-trade-sdk
import { utils } from "skc-trade-sdk";

// Import utils from skc-sdk
import { utils } from "skc-sdk";

const cipher = {
  cipherText: Buffer.from("cipher-text"),
  ephemPublicKey: Buffer.from("ephemeral-public-key"),
  iv: Buffer.from("initialization-vector"),
  mac: Buffer.from("message-authentication-code"),
};
const serializedCipher = utils.stringify(cipher);

encrypt

Encrypts a plaintext message using the public key of the counterparty.

javascript
// Import utils from skc-sdk
import { utils } from "skc-sdk";

const encryptedMessage = await utils.encrypt(
  "This is a secret message",
  "0xCounterPartyPublicKey"
);

prepareEthTransferTxData

Prepares transaction data for transferring Ether between addresses.

javascript
import { utils } from "skc-sdk";
import { ethers } from "ethers";

const fromAddress = "0xYourAddress";
const toAddress = "0xRecipientAddress";
const amountEther = "1.0";
const provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");
const txData = await utils.prepareEthTransferTxData(
  fromAddress,
  toAddress,
  amountEther,
  provider
);

prepareTxData

Prepares transaction data for a given smart contract method call.

javascript
import { utils } from "skc-sdk";
import { ethers } from "ethers";

const contract = new ethers.Contract("contract-address", ["ABI"], "signer");
const method = "methodName";
const args = ["arg1", "arg2"];
const signerAddress = "0xYourSignerAddress";
const provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");
const memo = "Your transaction memo";
const txData = await utils.prepareTxData(
  contract,
  method,
  args,
  signerAddress,
  provider,
  memo
);

retrieveMemoFromTxHash

Retrieves the memo (if present) from a transaction hash by decoding the transaction data.

javascript
import { utils } from "skc-sdk";
import { ethers } from "ethers";

const contract = new ethers.Contract("contract-address", ["ABI"], "signer");
const txHash = "transaction-hash";
const provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");
const memo = await utils.retrieveMemoFromTxHash(contract, txHash, provider);

These examples demonstrate how to import and use the utility functions from both skc-sdk and skc-trade-sdk. Each function has been documented with relevant examples to ensure clarity and ease of use.