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
