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
