Options
All
  • Public
  • Public/Protected
  • All
Menu

@metamask/eth-sig-util

Index

Type aliases

TypedDataV1

TypedDataV1: EIP712TypedData[]

This is the message format used for V1 of signTypedData.

Variables

TYPED_MESSAGE_SCHEMA

TYPED_MESSAGE_SCHEMA: { properties: { domain: { type: string }; message: { type: string }; primaryType: { type: string }; types: { additionalProperties: { items: { properties: { name: { type: string }; type: { enum: string[]; type: string } }; required: string[]; type: string }; type: string }; type: string } }; required: string[]; type: string } = ...

Type declaration

  • properties: { domain: { type: string }; message: { type: string }; primaryType: { type: string }; types: { additionalProperties: { items: { properties: { name: { type: string }; type: { enum: string[]; type: string } }; required: string[]; type: string }; type: string }; type: string } }
    • domain: { type: string }
      • type: string
    • message: { type: string }
      • type: string
    • primaryType: { type: string }
      • type: string
    • types: { additionalProperties: { items: { properties: { name: { type: string }; type: { enum: string[]; type: string } }; required: string[]; type: string }; type: string }; type: string }
      • additionalProperties: { items: { properties: { name: { type: string }; type: { enum: string[]; type: string } }; required: string[]; type: string }; type: string }
        • items: { properties: { name: { type: string }; type: { enum: string[]; type: string } }; required: string[]; type: string }
          • properties: { name: { type: string }; type: { enum: string[]; type: string } }
            • name: { type: string }
              • type: string
            • type: { enum: string[]; type: string }
              • enum: string[]
              • type: string
          • required: string[]
          • type: string
        • type: string
      • type: string
  • required: string[]
  • type: string

TypedDataUtils

TypedDataUtils: { eip712Hash: <T>(typedData: TypedMessage<T>, version: V3 | V4) => Buffer; encodeData: (primaryType: string, data: Record<string, unknown>, types: Record<string, MessageTypeProperty[]>, version: V3 | V4) => Buffer; encodeType: (primaryType: string, types: Record<string, MessageTypeProperty[]>) => string; findTypeDependencies: (primaryType: string, types: Record<string, MessageTypeProperty[]>, results?: Set<string>) => Set<string>; hashStruct: (primaryType: string, data: Record<string, unknown>, types: Record<string, MessageTypeProperty[]>, version: V3 | V4) => Buffer; hashType: (primaryType: string, types: Record<string, MessageTypeProperty[]>) => Buffer; sanitizeData: <T>(data: TypedMessage<T>) => TypedMessage<T> } = ...

A collection of utility functions used for signing typed data.

Type declaration

  • eip712Hash: <T>(typedData: TypedMessage<T>, version: V3 | V4) => Buffer
      • Hash a typed message according to EIP-712. The returned message starts with the EIP-712 prefix, which is "1901", followed by the hash of the domain separator, then the data (if any). The result is hashed again and returned.

        This function does not sign the message. The resulting hash must still be signed to create an EIP-712 signature.

        Type parameters

        Parameters

        • typedData: TypedMessage<T>

          The typed message to hash.

        • version: V3 | V4

          The EIP-712 version the encoding should comply with.

        Returns Buffer

        The hash of the typed message.

  • encodeData: (primaryType: string, data: Record<string, unknown>, types: Record<string, MessageTypeProperty[]>, version: V3 | V4) => Buffer
      • (primaryType: string, data: Record<string, unknown>, types: Record<string, MessageTypeProperty[]>, version: V3 | V4): Buffer
      • Encodes an object by encoding and concatenating each of its members.

        Parameters

        • primaryType: string

          The root type.

        • data: Record<string, unknown>

          The object to encode.

        • types: Record<string, MessageTypeProperty[]>

          Type definitions for all types included in the message.

        • version: V3 | V4

          The EIP-712 version the encoding should comply with.

        Returns Buffer

        An encoded representation of an object.

  • encodeType: (primaryType: string, types: Record<string, MessageTypeProperty[]>) => string
      • Encodes the type of an object by encoding a comma delimited list of its members.

        Parameters

        • primaryType: string

          The root type to encode.

        • types: Record<string, MessageTypeProperty[]>

          Type definitions for all types included in the message.

        Returns string

        An encoded representation of the primary type.

  • findTypeDependencies: (primaryType: string, types: Record<string, MessageTypeProperty[]>, results?: Set<string>) => Set<string>
      • (primaryType: string, types: Record<string, MessageTypeProperty[]>, results?: Set<string>): Set<string>
      • Finds all types within a type definition object.

        Parameters

        • primaryType: string

          The root type.

        • types: Record<string, MessageTypeProperty[]>

          Type definitions for all types included in the message.

        • results: Set<string> = ...

          The current set of accumulated types.

        Returns Set<string>

        The set of all types found in the type definition.

  • hashStruct: (primaryType: string, data: Record<string, unknown>, types: Record<string, MessageTypeProperty[]>, version: V3 | V4) => Buffer
      • (primaryType: string, data: Record<string, unknown>, types: Record<string, MessageTypeProperty[]>, version: V3 | V4): Buffer
      • Hashes an object.

        Parameters

        • primaryType: string

          The root type.

        • data: Record<string, unknown>

          The object to hash.

        • types: Record<string, MessageTypeProperty[]>

          Type definitions for all types included in the message.

        • version: V3 | V4

          The EIP-712 version the encoding should comply with.

        Returns Buffer

        The hash of the object.

  • hashType: (primaryType: string, types: Record<string, MessageTypeProperty[]>) => Buffer
      • Hashes the type of an object.

        Parameters

        • primaryType: string

          The root type to hash.

        • types: Record<string, MessageTypeProperty[]>

          Type definitions for all types included in the message.

        Returns Buffer

        The hash of the object type.

  • sanitizeData: <T>(data: TypedMessage<T>) => TypedMessage<T>

Functions

concatSig

  • concatSig(v: Buffer, r: Buffer, s: Buffer): string
  • Concatenate an extended ECDSA signature into a single '0x'-prefixed hex string.

    Parameters

    • v: Buffer

      The 'v' portion of the signature.

    • r: Buffer

      The 'r' portion of the signature.

    • s: Buffer

      The 's' portion of the signature.

    Returns string

    The concatenated ECDSA signature as a '0x'-prefixed string.

decrypt

  • decrypt(options: { encryptedData: EthEncryptedData; privateKey: string }): string
  • Decrypt a message.

    Parameters

    • options: { encryptedData: EthEncryptedData; privateKey: string }

      The decryption options.

      • encryptedData: EthEncryptedData

        The encrypted data.

      • privateKey: string

        The private key to decrypt with.

    Returns string

    The decrypted message.

decryptSafely

  • decryptSafely(options: { encryptedData: EthEncryptedData; privateKey: string }): string
  • Decrypt a message that has been encrypted using encryptSafely.

    Parameters

    • options: { encryptedData: EthEncryptedData; privateKey: string }

      The decryption options.

      • encryptedData: EthEncryptedData

        The encrypted data.

      • privateKey: string

        The private key to decrypt with.

    Returns string

    The decrypted message.

encrypt

  • encrypt(options: { data: unknown; publicKey: string; version: string }): EthEncryptedData
  • Encrypt a message.

    Parameters

    • options: { data: unknown; publicKey: string; version: string }

      The encryption options.

      • data: unknown

        The message data.

      • publicKey: string

        The public key of the message recipient.

      • version: string

        The type of encryption to use.

    Returns EthEncryptedData

    The encrypted data.

encryptSafely

  • encryptSafely(options: { data: unknown; publicKey: string; version: string }): EthEncryptedData
  • Encrypt a message in a way that obscures the message length.

    The message is padded to a multiple of 2048 before being encrypted so that the length of the resulting encrypted message can't be used to guess the exact length of the original message.

    Parameters

    • options: { data: unknown; publicKey: string; version: string }

      The encryption options.

      • data: unknown

        The message data.

      • publicKey: string

        The public key of the message recipient.

      • version: string

        The type of encryption to use.

    Returns EthEncryptedData

    The encrypted data.

extractPublicKey

  • extractPublicKey(options: { data: unknown; signature: string }): string
  • Recover the public key of the account used to create the given Ethereum signature. The message must have been signed using the personalSign function, or an equivalent function.

    Parameters

    • options: { data: unknown; signature: string }

      The public key recovery options.

      • data: unknown

        The hex data that was signed.

      • signature: string

        The '0x'-prefixed hex encoded message signature.

    Returns string

    The '0x'-prefixed hex encoded public key of the message signer.

getEncryptionPublicKey

  • getEncryptionPublicKey(privateKey: string): string
  • Get the encryption public key for the given key.

    Parameters

    • privateKey: string

      The private key to generate the encryption public key with.

    Returns string

    The encryption public key.

normalize

  • normalize(input: string | number): string
  • Normalize the input to a lower-cased '0x'-prefixed hex string.

    Parameters

    • input: string | number

      The value to normalize.

    Returns string

    The normalized value.

personalSign

  • personalSign(options: { data: unknown; privateKey: Buffer }): string
  • Create an Ethereum-specific signature for a message.

    This function is equivalent to the eth_sign Ethereum JSON-RPC method as specified in EIP-1417, as well as the MetaMask's personal_sign method.

    Parameters

    • options: { data: unknown; privateKey: Buffer }

      The personal sign options.

      • data: unknown

        The hex data to sign.

      • privateKey: Buffer

        The key to sign with.

    Returns string

    The '0x'-prefixed hex encoded signature.

recoverPersonalSignature

  • recoverPersonalSignature(options: { data: unknown; signature: string }): string
  • Recover the address of the account used to create the given Ethereum signature. The message must have been signed using the personalSign function, or an equivalent function.

    Parameters

    • options: { data: unknown; signature: string }

      The signature recovery options.

      • data: unknown

        The hex data that was signed.

      • signature: string

        The '0x'-prefixed hex encoded message signature.

    Returns string

    The '0x'-prefixed hex encoded address of the message signer.

recoverTypedSignature

  • recoverTypedSignature<V, T>(options: { data: V extends "V1" ? TypedDataV1 : TypedMessage<T>; signature: string; version: V }): string
  • Recover the address of the account that created the given EIP-712 signature. The version provided must match the version used to create the signature.

    Type parameters

    Parameters

    • options: { data: V extends "V1" ? TypedDataV1 : TypedMessage<T>; signature: string; version: V }

      The signature recovery options.

      • data: V extends "V1" ? TypedDataV1 : TypedMessage<T>

        The typed data that was signed.

      • signature: string

        The '0x-prefixed hex encoded message signature.

      • version: V

        The signing version to use.

    Returns string

    The '0x'-prefixed hex address of the signer.

signTypedData

  • signTypedData<V, T>(options: { data: V extends "V1" ? TypedDataV1 : TypedMessage<T>; privateKey: Buffer; version: V }): string
  • Sign typed data according to EIP-712. The signing differs based upon the version.

    V1 is based upon an early version of EIP-712 that lacked some later security improvements, and should generally be neglected in favor of later versions.

    V3 is based on EIP-712, except that arrays and recursive data structures are not supported.

    V4 is based on EIP-712, and includes full support of arrays and recursive data structures.

    Type parameters

    Parameters

    • options: { data: V extends "V1" ? TypedDataV1 : TypedMessage<T>; privateKey: Buffer; version: V }

      The signing options.

      • data: V extends "V1" ? TypedDataV1 : TypedMessage<T>

        The typed data to sign.

      • privateKey: Buffer

        The private key to sign with.

      • version: V

        The signing version to use.

    Returns string

    The '0x'-prefixed hex encoded signature.

typedSignatureHash

  • Generate the "V1" hash for the provided typed message.

    The hash will be generated in accordance with an earlier version of the EIP-712 specification. This hash is used in signTypedData_v1.

    Parameters

    Returns string

    The '0x'-prefixed hex encoded hash representing the type of the provided message.