liveBook · Manning

HasHash

As explained in The Joy of JavaScript

HasHash is a book-local mixin pattern that adds hashing-related behavior to an object or class.

HasHash is a JavaScript mixin factory that adds calculateHash to an object or class. It accepts property keys, selects those values for hashing, and keeps algorithm and encoding options configurable. This separates hashing behavior from the target object's primary responsibilities. section 3.4.1 section 4.4.2

The implementation composes three steps: extract selected properties, serialize them predictably, and pass the result to a hashing function. Currying and composition keep these operations small, pure, and reusable across blockchain domain objects. section 4.4.2

HasHash makes several design choices explicit:

  • Hash only the fields named by the caller rather than the entire object. section 4.4.2
  • Use JSON.stringify during assembly so primitive and object values become a predictable string representation. section 4.4.2
  • Expose a simple calculateHash method while allowing the hashing algorithm and output encoding to change independently. section 3.4.1 section 4.4.2
  • Compose the mixin into classes or plain objects, establishing a HAS-A relationship instead of requiring inheritance. section 3.4.1

Hashing selected transaction fields

const hashTransaction = Object.assign( { sender: 'luis@tjoj.com', recipient: 'luke@tjoj.com', funds: 10 }, HasHash(['sender', 'recipient', 'funds']) ); hashTransaction.calculateHash(); // '04a635cf3f19a6dcc30ca7b63b9a1a6a1c42a9820002788781abae9bec666902' The same pattern can augment a Transaction or Block prototype with different key sets. section 4.4.2 section 6.2.3

Listing 4.12 Final implementation of HasHash
const HasHash = (
  keys,
  options = { 
       algorithm: 'SHA256', 
       encoding:  'hex' 
    }
) => ({
  calculateHash() {
     return compose(
      computeCipher(options),
      assemble,
      props(keys)
    )(this);           #1
  }
});

The final implementation shows the complete pipeline from selected keys to composed hashing and demonstrates the mixin's compact public shape. section 4.4.2

How The Joy of JavaScript covers it

How deep it goes
Explains the mixin shape, selected-field assembly, serialization, currying, composition, and Node.js hashing, while initially deferring cryptographic internals.
Examples
Uses hashable objects, blockchain transactions, selected property keys, SHA-256 digests, and prototype augmentation for Transaction and Block.
What it assumes
Assumes intermediate JavaScript knowledge while demonstrating object assignment, closures, arrow-function this binding, and functional composition.
What you can do after
Build it from scratch as a composable JavaScript mixin that hashes chosen object properties with configurable Node.js crypto options.
How it connects
Connects mixins with object composition, closures, currying, function composition, cryptographic hashing, and IIFEs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.