Relay & Subscription

The paid relay mesh: the Subscription contract that keeps the registry, the RelayNode contract each operator runs, how fees are quoted and paid, how clients register and resolve each other, and how encrypted mail moves between relays. Companion pages: Contact & Handshake and Groups & Contracts.

1. Overview — two contracts, one market

Offline delivery needs someone to hold your mail. Undertown makes that a market with two contract types, deployed per network:

ContractOne per…Role
Subscription network The registry: which user pays which relay, until when; which relays are registered and unblocked; fees in USD; the price feed.
RelayNode relay operator The relay's own record: owner, per-period fee, contact code (where its signed Contact lives), deny flags, group registrations.

A user subscribes to a relay (paying in native currency, priced in USD); a relay registers on the Subscription contract (also paid); and every handshake later — client registration, relay-to-relay transfer, resolution — falls back to the same on-chain question: hasSubscription(wallet, relay).

2. The Subscription contract

uint256 REGISTRATION_PERIOD = 10 days     // one billing period
uint256 MAX_REGISTRATION_PERIODS = 109     // relay may register up to ~3 years ahead
struct SubscriptionEntry { address relayContract; uint256 expiresAt; }

address admin;                             // deployer
address priceFeed;                         // Chainlink feed (or zero for manual)
uint256 manualNativePrice;                 // wei-per-USD when no feed
uint256 registrationFeeUSD;                // relay operators, per 10-day period
uint256 subscriptionFeeUSD;                // users, per add/renew call
mapping(address => SubscriptionEntry[]) subscriptions;
mapping(address => uint256) registeredUntil;   // relay => paid until
mapping(address => bool)    blockedRelays;

Key functions, by role:

RoleFunctionEffect
useraddSubscription(relay) payablePay the quoted total; the Subscription contract forwards to the relay's subscribe() and records your expiry.
userrenewSubscription(relay) payableExtend expiresAt by the paid periods.
userremoveSubscription(relay)End the subscription (no refund of elapsed time).
operatorregisterRelay(relay) payablePay registration periods for a RelayNode; rejected if a subscription period would outlive the registration.
relayremoveSubscriber(wallet, relay)A relay ends its own relationship with a subscriber.
adminblockRelay / unblockRelay / adminForceUnregister / pruneRelayListCuration: bad relays disappear from discovery and can be force-removed (their registered slots cleaned).
adminsetRegistrationFee / setSubscriptionFee / setPriceFeed / setManualNativePrice / setMetadata / withdrawMarket configuration and treasury.
viewgetSubscriptions(wallet), hasSubscription(wallet, relay), getActiveRelays(), isRelayRegistered(relay), getRegisteredUntil(relay), getBlockedRelays()Everything the mesh needs to verify claims — these views are what handshakes call.

3. The RelayNode contract

address owner                    // the operator (set at deploy)
address immutable subscriptionContract   // permanently wired to one registry
string  contactCode              // base58 Contact — the relay's network identity
string  name, description, site  // discovery metadata
uint256 feeUSD                   // user fee per 10-day period
bool    denyNewUsers, denyRenew  // operator switches
mapping(address => address[]) userRegisteredGroups;   // groups each user linked (max 20)

4. Pricing — from USD to wei

Fees are authored in USD so operators can think in dollars while users pay in native currency. The conversion has two branches, and the client mirrors the contract's arithmetic exactly (same truncation, same overflow behavior):

// Chainlink feed configured:
native = feeUSD × 1e18 × 1e8 / answer          // answer: 8-decimal feed price

// Manual price set (feed = 0x0):
native = feeUSD × manualNativePrice             // operator pins wei-per-USD

5. Operator lifecycle

6. Subscriber lifecycle

All of this is driven from Settings → Relays & Subscriptions (or the 33-tool MCP surface, section 10):

  1. Browse — pick a tracked contract + chain; the app calls getActiveRelays() and per-relay metadata to render name, description, and fee.
  2. Quote — subscription.quote_add returns the exact breakdown: subscription fee (USD & wei) + relay fee (USD & wei) = total in ETH, with the price-source hint. Nothing is sent until you confirm.
  3. Subscribe — subscription.add broadcasts addSubscription; success surfaces the tx hash and refreshes your list. Expiry comes back from SubscriptionEntry.expiresAt.
  4. Renew — same flow with renewSubscription; expiry extends by the purchased periods.
  5. Remove — removeSubscription (a plain, non-payable transaction — no quote needed).

Tracking a contract. The app only browses Subscription contracts it tracks. Tracking writes the chain:0xaddress entry into your signed Contact (subscriptions: line) and re-signs it — which is what lets your peers, and the relay mesh, trust which networks you belong to. Untracked contract? The settings tab offers a one-click "Track this contract" recovery.

7. Client registration at the relay

An on-chain subscription is entitlement; registration is how the relay learns who its clients actually are. It is the wallet-signed challenge-response covered fully in Contact & Handshake §9. In brief:

8. Resolution — finding a recipient's relay

When you send to someone served by a different relay, your relay must find theirs. It asks the registry, never a contact book:

Subscription.getSubscriptions(recipient)      // unexpired entries only
        │
        ▼  for each: RelayNode(relay).contactCode()
base58 contact code ──► decode ──► verify wallet signature
        │
        ▼  trusted multiaddrs of the recipient's relay
dial & transfer

9. Relay → relay transfer

With the destination resolved, the holding relay dials the recipient's relay for a single ephemeral transfer, gated by the three-factor handshake (full detail in Contact & Handshake and the narrative in the blog post):

  1. Sender's signature — a routing authorization the client signed at push time, listing the exact posts, both relays, both users, and a 1-hour expiry.
  2. The registry's word — on-chain: isRelayRegistered(relayA), hasSubscription(sender, relayA), hasSubscription(recipient, relayB).
  3. Operator challenge — a fresh nonce signed by relayA's contract owner, recovered against a live owner() read.

The queue behind it is built for failure:

ParameterValueMeaning
TRANSFER_MAX_POSTS64Per-connection chunk; oversized batches are split, each chunk covered by the same authorization.
TRANSFER_MAX_BYTES256 KiBPer-connection byte cap.
Routing-auth TTL1 hourAuthorizations expire; nothing is delivered without a valid one.
Queue entry TTL24 hoursUnroutable entries die of old age.
Per-target cap2 MiBBounded memory per recipient.
Attempts / backoff5 · 30 s × 2ⁿ (cap 1 h)Exponential retry between sweeps (60 s).
Dead-letter store500 entriesBounded graveyard with per-post reasons.
Session TTL5 minutesOne transfer per connection; the session is consumed on ack.

Everything crossing that handshake is ciphertext — the enc: payload plus routing clocks. Live end-to-end tests assert both relay databases end with zero plaintext rows; only the recipient's device, holding the channel key, ever decrypts.

10. The tool surface (MCP)

Every operation above is scriptable over the built-in MCP server — 33 tools, no deploy surface:

GroupTools
Configsubscription_set_contracts (re-signs your Contact; needs password), subscription_get_contracts
Usersubscription_add, subscription_renew, subscription_remove, subscription_list, subscription_has, subscription_quote_add, subscription_quote_renew
Relay (user)relay_get_info, relay_quote_register, relay_groups_register, relay_groups_unregister, relay_groups_list
Relay (owner)relay_owner_set_fee, relay_owner_set_metadata, relay_owner_update_contact, relay_owner_set_deny_new_users, relay_owner_set_deny_renew, relay_owner_withdraw
Adminsubscription_admin_set_registration_fee, set_subscription_fee, set_metadata, set_price_feed, set_manual_price, block_relay, unblock_relay, force_unregister, withdraw, prune_relays, blocked_list
Discoverysubscription_active_relays, subscription_relay_info

Every write funnels through the password/session-key path and simulates with eth_call before broadcasting; owner-only relay operations are guard-checked against owner() before a transaction is even built.

11. Security notes

← back to documentation