Options
All
  • Public
  • Public/Protected
  • All
Menu

Package @purser/core

@purser/core

A collection of helpers, utils, validators and normalizers to assist the individual purser modules.

Unless you want something specific found in this module, it doesn't need to be manually required as each module already makes use of it internally.

Installation

npm install @purser/core

(Usually you won't install this package just on its own. It's meant to be an accompanying peer-dependency to the other @purser modules)

Quick Usage

import { bigNumber } from '@purser/core/utils'

const value = bigNumber('0.00000001').toWei();

console.log(value); // { negative: 0, words: Array(4), length: 4, red: null }

Documentation

You can find more in-depth description for this module's API in the purser docs.

Contributing

This package is part of the purser monorepo package.

Please read our Contributing Guidelines for how to get started.

License

The @purser/core library along with the whole purser monorepo are MIT licensed.

Index

Variables

Const HEX_HASH_TYPE

HEX_HASH_TYPE: BufferEncoding = "hex"

Functions

Const addressNormalizer

  • addressNormalizer(rawAddress: string, prefix?: boolean): string
  • Normalize an Ethereum address

    This method assumes the address is already validatated and is in the correct format.

    Parameters

    • rawAddress: string
    • Optional prefix: boolean

      Should the final value have a prefix?

    Returns string

    The normalized string

Const addressValidator

  • addressValidator(address: string): boolean
  • Validate a BIP32 Ethereum Address

    Parameters

    • address: string

      The 'hex' address to check

    Returns boolean

    It only returns true if the string is a valid address format, otherwise an Error will be thrown and this will not finish execution.

Const bigNumber

  • bigNumber(value: number | string | BN): ExtendedBN
  • Wrapper for the bn.js constructor to use as an utility for big numbers

    Make sure to inform the users that this is the preffered way of interacting with big numbers inside this library, as even if the underlying Big Number library will change, this API will (mostly) stay the same.

    See: BigInt https://developers.google.com/web/updates/2018/05/bigint

    Parameters

    • value: number | string | BN

      the value to convert to a big number

    Returns ExtendedBN

    The new bignumber instance

Const derivationPathSerializer

  • derivationPathSerializer(__namedParameters?: { account: number; addressIndex: number; change: number; coinType: number; purpose: number }): string
  • Serialize an derivation path object's props into it's string counterpart

    Parameters

    • Default value __namedParameters: { account: number; addressIndex: number; change: number; coinType: number; purpose: number } = {}
      • account: number
      • addressIndex: number
      • change: number
      • coinType: number
      • purpose: number

    Returns string

    The serialized path

Const getChainDefinition

  • getChainDefinition(chainId: number): TransactionOptions
  • In order to support EIP-155, it's necessary to specify various definitions for a given chain (e.g. the chain ID, network ID, hardforks).

    Given a chain ID, this function returns a chain definition in the format expected by ethereumjs-common.

    Parameters

    • chainId: number

      The given chain ID (as defined in EIP-155)

    Returns TransactionOptions

    The common chain definition

Const getRandomValues

  • getRandomValues(typedArray?: Uint8Array): Uint8Array
  • A very basic polyfill method to generate randomness for use in wallet entropy. This will fall back to nodejs's crypto library if the browser that's using this doesn't have the webcrypto API implemented yet.

    Parameters

    • Default value typedArray: Uint8Array = new Uint8Array(10)

      An initial unsigned 8-bit integer array to generate randomness from

    Returns Uint8Array

    A new 8-bit unsigned integer array filled with random bytes

Const hexSequenceNormalizer

  • hexSequenceNormalizer(hexString: string, prefix?: boolean): string
  • Normalize a hex string sequence.

    Transforms it to lower case, and, depending on the prefix argument, either add it (0x) or remove it

    This method assumes the address is already validatated and is in the correct format.

    Parameters

    • hexString: string

      The hex string sequence to normalize

    • Default value prefix: boolean = true

      Should the final value have a prefix?

    Returns string

    The normalized string

Const hexSequenceValidator

  • hexSequenceValidator(hexSequence: string): boolean
  • Validate a hex string

    Parameters

    • hexSequence: string

      The hex string to check

    Returns boolean

    It only returns true if the string is a valid hex format, otherwise an Error will be thrown and this will not finish execution.

Const messageOrDataValidator

  • messageOrDataValidator(__namedParameters: { message: string; messageData: string | Uint8Array }): string | Uint8Array
  • Parameters

    • __namedParameters: { message: string; messageData: string | Uint8Array }
      • message: string
      • messageData: string | Uint8Array

    Returns string | Uint8Array

Const messageVerificationObjectValidator

  • messageVerificationObjectValidator(__namedParameters: { message: string; signature: string }): VerifyMessageData
  • Validate a signature verification message object

    Parameters

    • __namedParameters: { message: string; signature: string }
      • message: string
      • signature: string

    Returns VerifyMessageData

    The validated signature object containing the exact passed in values

Const objectToErrorString

  • objectToErrorString(object?: Record<string, any>): string
  • Convert an object to a key (value) concatenated string. This is useful to list values inside of error messages, where you can only pass in a string and not the whole object.

    Parameters

    • Default value object: Record<string, any> = {}

      The object to convert

    Returns string

    The string containing the object's key (value) pairs

Const recoverPublicKey

  • recoverPublicKey(__namedParameters: { message: string; signature: string }): string
  • Recover a public key from a message and the signature of that message.

    Parameters

    • __namedParameters: { message: string; signature: string }
      • message: string
      • signature: string

    Returns string

    The recovered public key.

Const safeIntegerValidator

  • safeIntegerValidator(integer: number): boolean
  • Validate an integer passed in to make sure is safe (< 9007199254740991) and positive

    Parameters

    • integer: number

      The integer to validate

    Returns boolean

    It only returns true if the integer is safe and positive, otherwise an Error will be thrown and this will not finish execution.

Const transactionObjectValidator

  • transactionObjectValidator(__namedParameters: { chainId: number; gasLimit: ExtendedBN; gasPrice: ExtendedBN; inputData: string; nonce: number; to: string; value: ExtendedBN }): TransactionObjectTypeWithTo
  • Validate an transaction object

    Parameters

    • __namedParameters: { chainId: number; gasLimit: ExtendedBN; gasPrice: ExtendedBN; inputData: string; nonce: number; to: string; value: ExtendedBN }
      • chainId: number
      • gasLimit: ExtendedBN
      • gasPrice: ExtendedBN
      • inputData: string
      • nonce: number
      • to: string
      • value: ExtendedBN

    Returns TransactionObjectTypeWithTo

    The validated transaction object containing the exact passed in values

Const userInputValidator

  • userInputValidator(__namedParameters?: { firstArgument: {}; requiredAll: string[]; requiredEither: string[]; requiredOr: string[] }): void
  • Check if the user provided input is in the form of an Object and it's required props

    Parameters

    • Default value __namedParameters: { firstArgument: {}; requiredAll: string[]; requiredEither: string[]; requiredOr: string[] } = {}
      • firstArgument: {}
      • requiredAll: string[]
      • requiredEither: string[]
      • requiredOr: string[]

    Returns void

Const warning

  • warning(...args: Array<any>): void
  • If we're in dev mode, show an warning to the console

    This way you won't have to explicitly tell it which message from messages.js to show Arguments will be split into three types: First arg will be the message string Rest of them will be template literals that will replace %s values in the previous messsage string (with one exception) If the last argument is an object that has only one prop named level, it will be interpreted as an option object (if level equals low it will only warn, if the level equals high, it will error)

    Parameters

    • Rest ...args: Array<any>

      Arguments array that will be passed down to console methods (see above)

    Returns void

Object literals

Const CHAIN_IDS

CHAIN_IDS: object

GOERLI

GOERLI: number = 5

HOMESTEAD

HOMESTEAD: number = 1

KOVAN

KOVAN: number = 42

LOCAL

LOCAL: number = 1337

RINKEBY

RINKEBY: number = 4

ROPSTEN

ROPSTEN: number = 3

Const PATH

PATH: object

ACCOUNT

ACCOUNT: number = 0

CHANGE

CHANGE: number = 0

COIN_MAINNET

COIN_MAINNET: number = 60

COIN_TESTNET

COIN_TESTNET: number = 1

DELIMITER

DELIMITER: string = "'/"

HEADER_KEY

HEADER_KEY: string = "m"

INDEX

INDEX: number = 0

PURPOSE

PURPOSE: number = 44

Const REQUIRED_PROPS

REQUIRED_PROPS: object

SIGN_MESSAGE

SIGN_MESSAGE: string[] = ['message', 'messageData']

VERIFY_MESSAGE

VERIFY_MESSAGE: string[] = ['message', 'signature']

Generated using TypeDoc