Options
All
  • Public
  • Public/Protected
  • All
Menu

Module harmony-crypto

This package provides a collection of apis related to address management, kestore, encoding, and encrypt/decrypt.

Installation

npm install @harmony-js/crypto

Usage

const {
  encode,
  decode,
  randomBytes,
  toBech32,
  fromBech32,
  HarmonyAddress,
  generatePrivateKey,
  getPubkeyFromPrivateKey,
  getAddressFromPublicKey,
  getAddressFromPrivateKey,
  encryptPhrase,
  decryptPhrase
} = require('@harmony-js/crypto');
const { isPrivateKey, isAddress, isPublicKey } = require('@harmony-js/utils');

Address apis

const bytes = randomBytes(20);
const addr = new HarmonyAddress(bytes);

console.log(addr.checksum);
console.log(addr.bech32);

console.log(HarmonyAddress.isValidBech32(addr.bech32));

RLP apis

const encoded = '0x89010101010101010101';
const decoded = '0x010101010101010101';
console.log(encode(decoded));
console.log(decode(encoded));

Keystore apis

const prv = generatePrivateKey();
const pub = getPubkeyFromPrivateKey(prv);
const addr = getAddressFromPublicKey(pub);
const addrPrv = getAddressFromPrivateKey(prv);
console.log(isPrivateKey(prv));
console.log(isPublicKey(pub));
console.log(isAddress(addr));
console.log(isAddress(addrPrv));

Encrypt/decrypt apis

const { Wallet } = require('@harmony-js/account');

const myPhrase = new Wallet().newMnemonic();
console.log(myPhrase);
const pwd = '1234';
encryptPhrase(myPhrase, pwd).then((value) => {
  console.log(value);
  decryptPhrase(JSON.parse(value), pwd).then(value => {
    console.log(value);
  });
});

Index

Variables

Const secp256k1

secp256k1: any = elliptic.ec('secp256k1')

Functions

Const decrypt

  • decrypt(keystore: Keystore, password: string): Promise<string>
  • function

    decrypt

    Parameters

    • keystore: Keystore

      Keystore file

    • password: string

      password string

    Returns Promise<string>

    privateKey

Const decryptPhrase

  • decryptPhrase(keystore: Keystore, password: string): Promise<string>
  • decrypt phrase

    Parameters

    • keystore: Keystore
    • password: string

    Returns Promise<string>

Const encrypt

  • encrypt(privateKey: string, password: string, options?: EncryptOptions): Promise<string>
  • This method will map the current Account object to V3Keystore object.

    method

    encrypt

    Parameters

    • privateKey: string
    • password: string
    • Optional options: EncryptOptions

    Returns Promise<string>

Const encryptPhrase

  • encryptPhrase(phrase: string, password: string, options?: EncryptOptions): Promise<string>
  • encrypt Phrase

    Parameters

    • phrase: string
    • password: string
    • Optional options: EncryptOptions

    Returns Promise<string>

Const generatePrivateKey

  • generatePrivateKey(): string
  • function

    generatePrivateKey

    description

    generatePrivate key using eth-lib settings

    Returns string

getAddress

  • Using this function to get Harmony format address

    example
    const { Harmony } = require('@harmony-js/core');
    const { ChainID, ChainType } = require('@harmony-js/utils');
    const { randomBytes } = require('@harmony-js/crypto')
    
    const hmy = new Harmony(
      'http://localhost:9500',
      {
      chainType: ChainType.Harmony,
      chainId: ChainID.HmyLocal,
      },
    );
    
    const bytes = randomBytes(20);
    const hAddress = hmy.crypto.getAddress(bytes);
    console.log(hAddress)

    Parameters

    • address: string

    Returns HarmonyAddress

Const getAddressFromPrivateKey

  • getAddressFromPrivateKey(privateKey: string): string
  • function

    getAddressFromPrivateKey

    Parameters

    • privateKey: string

      private key string

    Returns string

    address with length = 40

Const getAddressFromPublicKey

  • getAddressFromPublicKey(publicKey: string): string
  • function

    getAddressFromPublicKey

    Parameters

    • publicKey: string

      public key string

    Returns string

    address with length = 40

getContractAddress

  • getContractAddress(from: string, nonce: number): string
  • Parameters

    • from: string
    • nonce: number

    Returns string

getDerivedKey

  • getDerivedKey(key: Buffer, kdf: KDF, params: KDFParams): Promise<Buffer>
  • getDerivedKey

    NOTE: only scrypt and pbkdf2 are supported.

    Parameters

    • key: Buffer

      the passphrase

    • kdf: KDF

      the key derivation function to be used

    • params: KDFParams

      params for the kdf

    Returns Promise<Buffer>

Const getPubkeyFromPrivateKey

  • getPubkeyFromPrivateKey(privateKey: string): string
  • function

    getPubkeyFromPrivateKey

    Parameters

    • privateKey: string

      private key String

    Returns string

Const getPublic

  • getPublic(privateKey: string, compress?: boolean): string
  • Parameters

    • privateKey: string
    • Optional compress: boolean

    Returns string

Const isValidChecksumAddress

  • isValidChecksumAddress(address: string): boolean
  • isValidChecksumAddress

    takes hex-encoded string and returns boolean if address is checksumed

    Parameters

    • address: string

    Returns boolean

Const randomBytes

  • randomBytes(bytes: number): string
  • Uses JS-native CSPRNG to generate a specified number of bytes.

    note

    this method throws if no PRNG is available.

    Parameters

    • bytes: number

      bytes number to generate

    Returns string

    ramdom hex string

recoverAddress

  • recoverAddress(digest: bytes.Arrayish | string, signature: Signature | string): string
  • Parameters

    • digest: bytes.Arrayish | string
    • signature: Signature | string

    Returns string

recoverPublicKey

  • recoverPublicKey(digest: bytes.Arrayish | string, signature: Signature | string): string
  • Parameters

    • digest: bytes.Arrayish | string
    • signature: Signature | string

    Returns string

Const sign

  • sign(digest: bytes.Arrayish | string, privateKey: string): Signature
  • Parameters

    • digest: bytes.Arrayish | string
    • privateKey: string

    Returns Signature

Const toChecksumAddress

  • toChecksumAddress(address: string): string
  • function

    toChecksumAddress

    Parameters

    • address: string

      raw address

    Returns string

    checksumed address

Const validatePrivateKey

  • validatePrivateKey(privateKey: string): boolean
  • Parameters

    • privateKey: string

    Returns boolean

verifySignature

  • verifySignature(digest: bytes.Arrayish, signature: Signature, publicKey: string): boolean
  • Parameters

    • digest: bytes.Arrayish
    • signature: Signature
    • publicKey: string

    Returns boolean

Generated using TypeDoc