liveBook · Manning

Wallet

As explained in The Joy of JavaScript

A book-local user-facing object representing a holder of blockchain addresses or keys.

A Wallet represents a user through a public key, private key, and address. Its address exposes the public key, while balance calculation interprets transactions recorded in a blockchain ledger. section 3.5 section 4.7

Listing 3.19 Wallet object
class Wallet {
  constructor(publicKey, privateKey) {
    this.publicKey = publicKey
    this.privateKey = privateKey
  }
  get address() {
    return this.publicKey
  }
  balance(ledger) {
    //...              #1
  }
}

The compact Wallet class separates identity data from the balance operation: construction stores both keys, address reads the public key, and balance accepts a ledger. section 3.5

Wallet balance can be expressed in two related styles:

  • The imperative method loops through blocks and transactions, skips the genesis block, adjusts a running Money value for sends and receives, and rounds the result. section 4.7
  • A functional design extracts computeBalance(address, ledger), then filters genesis blocks, flattens transaction data, maps each transaction to a balance effect, and reduces with Money.sum. section 4.7
  • The balance method can delegate to the pure computation with this.address, keeping object-facing access while making the data flow explicit. section 4.7

Transaction-level balance contribution

balanceOf(address) adds funds when the wallet is the recipient and subtracts them when it is the sender; unrelated transactions contribute zero. section 4.7

How The Joy of JavaScript covers it

How deep it goes
Defines keys and address, then descends into imperative and functional balance algorithms without detailing key generation or cryptography.
Examples
Uses a Wallet class, imperative ledger traversal, computeBalance, balanceOf, and map/filter/reduce pipelines.
What it assumes
Assumes blockchain ledgers contain blocks and transactions, while teaching Wallet structure and balance computation explicitly.
What you can do after
Build it from scratch as a key-bearing Wallet and calculate balances through either imperative traversal or a composable functional pipeline.
How it connects
Connects wallets to public keys, private keys, addresses, ledgers, blocks, transactions, Money, and functional programming.

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.