liveBook · Manning

Database transaction

As explained in The Joy of JavaScript

A book-local transaction object in the blockchain domain that models a transfer record and related behavior.

A transaction is a transfer record: a sender transfers an amount of funds to a recipient. In this blockchain model, transactions wait pending before being collected into blocks. Once recorded, changing a transaction would change the block’s hash and invalidate the chain. section 1.6

The JavaScript Transaction object holds the transfer details and gains behavior through composition. HasHash, HasSignature, and HasValidation can add hashing, signing, and validation without making every behavior part of a transaction-specific inheritance hierarchy. The illustrated hash implementation is not cryptographically secure; cryptographic algorithms and key handling are left outside this model. section 3.4.1 section 3.5

A coffee payment

A digital-wallet payment for coffee creates a pending transaction. Introductory objects use email addresses for sender and recipient and a number for funds; an actual public ledger uses public keys rather than personal email addresses. The payment joins other transactions in a block after validation. section 1.6 section 3.5

Listing 3.20 Assigning mixins to a single instance of Transaction
Object.assign( 
   new Transaction(...),                                      #1
   HasHash(['timestamp', 'sender', 'recipient', 'funds']), 
   HasSignature(['sender', 'recipient', 'funds']), 
   HasValidation()
)

The listing attaches hashing, signing, and validation to one Transaction instance. It also shows which transaction properties participate in its hash and signature. section 3.5

Transaction need not be built only as a class. A prototype-linked object, a constructor function, or a class inside an immediately invoked function can model or package it. The IIFE version keeps its hash helper private while exposing Transaction through an application namespace. section 2.1 section 2.2.1 section 6.2.2

How The Joy of JavaScript covers it

How deep it goes
Works through object construction and composable behavior, but defers cryptographic hash internals and key handling.
Examples
Uses a coffee payment, prototype-linked transfer objects, constructor functions, and a Transaction instance assembled with mixins.
What it assumes
Assumes familiarity with JavaScript objects and prototypes while introducing the simplified blockchain transfer model.
What you can do after
Build it from scratch as a JavaScript transfer object and compose its hashing, signing, and validation interfaces.
How it connects
Connects transactions to blocks, wallets, prototype inheritance, mixins, hashing, digital signatures, and validation.

Also explained in

Compare how 14 books explain database transaction →

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.