Options
All
  • Public
  • Public/Protected
  • All
Menu

External module harmony-crypto

@harmony-js/crypot provides a series of functions to deal with keys

How to use this package

Create a Harmony Instance

const { Harmony } = require('@harmony-js/core');
const { ChainID, ChainType } = require('@harmony-js/utils');

const hmy = new Harmony(
  'http://localhost:9500',
  {
    chainType: ChainType.Harmony,
    chainId: ChainID.HmyLocal,
  },
);

Some examples

// randomBytes
const bytes = hmy.crypto.randomBytes(20);
console.log(bytes)

// encryptPhrase
const myPhrase = hmy.wallet.newMnemonic();
const pwd = '1234';
hmy.crypto.encryptPhrase(myPhrase, pwd).then((value) => {
  console.log(value);
})

// decryptThePhrase
hmy.crypto.encryptPhrase(myPhrase, pwd).then((keystore) => {
  hmy.crypto.decryptPhrase(JSON.parse(keystore), pwd).then((value) => {
    console.log(value);
  })
})

// generatePrivateKey
const privateKey = hmy.crypto.generatePrivateKey();
console.log(privateKey)

// getPubkeyFromPrivateKey
const publicKey = hmy.crypto.getPubkeyFromPrivateKey(privateKey);
console.log(publicKey);

// getAddressFromPrivateKey
const address = hmy.crypto.getAddressFromPrivateKey(privateKey);
console.log(address);

// getAddressFromPublicKey
const address = hmy.crypto.getAddressFromPublicKey(publicKey);
console.log(address);

// toChecksumAddress
const checksumAddr = hmy.crypto.toChecksumAddress(address);
console.log(checksumAddr);

Index

Variables

Const secp256k1

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

Functions

Const decrypt

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

Const decryptPhrase

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

Const encrypt

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

    Parameters

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

    Returns Promise<string>

Const encryptPhrase

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

Const generatePrivateKey

  • generatePrivateKey(): 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

Const getAddressFromPublicKey

  • getAddressFromPublicKey(publicKey: string): string

getContractAddress

  • getContractAddress(from: string, nonce: number): 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

Const getPublic

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

Const isValidChecksumAddress

  • isValidChecksumAddress(address: string): boolean

Const randomBytes

  • randomBytes(bytes: number): string

recoverAddress

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

recoverPublicKey

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

Const sign

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

Const toChecksumAddress

  • toChecksumAddress(address: string): string

Const validatePrivateKey

  • validatePrivateKey(privateKey: string): boolean

verifySignature

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

Generated using TypeDoc