All files AbstractType.js

100% Statements 10/10
100% Branches 0/0
100% Functions 10/10
100% Lines 8/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83                                      162x                 17x                 43x                   40x                   35x                 23x                 1x         1x    
import { keccak256 } from 'js-sha3'
 
/**
  * @classdesc
  * This is the abstract base class which represents all nonprimitive types in
  * the EIP712 scheme.  Both domains and proper Struct types inherit from this
  * class, as the static encoding functionality is required by each.
  */
export default class AbstractType {
  static name
  static properties
 
  /**
    * @static @private
    * Gather the type definition as a single string, without including 
    * definitions of dependent types
    * @returns {String} 
    */
  static encodeTypeFragment() {
    return `${this.name}(${this.properties.map(({name, type}) => `${type} ${name}`).join(',')})`
  }
 
  /**
   * @static @abstract @private
   * Return the encoding of all types upon which this type depends. Only implemented
   * by types that support dependencies, i.e. structure types (not Domain types)
   */
  static encodeDependentTypes() {
    return ''
  }
 
  /**
    * @static
    * Gather the type definition into a single string
    * @returns {String} The full encoding of the type, including all dependent types (if applicable)
    */
  static encodeType() {
    return `${this.encodeTypeFragment()}${this.encodeDependentTypes()}`
  }
 
  /**
    * @static
    * Calculate the keccak256 hash of the abi encoding of this type according
    * to the EIP712 specification
    * @returns {String} the typeHash of this type
    */
  static typeHash() {
    return Buffer.from(keccak256(this.encodeType()), 'hex')
  }
 
  /**
    * @static
    * Return a list of {name, type} pairs that define this type as a new object 
    * @returns {Object[]}
    */
  static typeDef() {
    // Use map to deep copy the properties array
    return this.properties.map(({name, type}) => ({name, type}))
  }
 
  /**
   * Calculate the EIP712 hash for this object according to the specification
   * @returns {String} the keccak256 hash of the concatenation of the typeHash,
   *                   and the encoded data of this instance
   */
  hashStruct() {
    return Buffer.from(keccak256(this.encodeData()), 'hex')
  }
 
  /********************************************************************
   * Instance methods which should be overridden by subclasses
   *******************************************************************/
 
  /** @abstract */
  encodeData() {
    throw new Error('This is an abstract class, Subclasses of AbstractType should override encodeData()')
  }
 
  /** @abstract */
  toObject() {
    throw new Error('This is an abstract class, Subclasses of AbstractType should override toObject()')
  }
}