Verified contractExactMIT
Telegraph
Address0x7b72d14952bff1f65bdc7e2f4cf4d693be2f5429
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.28
- Same code
Only this contract
Details
- Compiler
- solc 0.8.28+commit.7893614a
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 25,896,442
- Creation transaction
- 0x8dd84652d5…5ee4326e96
identitymd/Telegraph.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
interface IIdentity {
function holderOf(uint256 id) external view returns (address);
}
// ===========================================================================
// IDENTITY.MD · PART III · TRAFFIC
// ===========================================================================
//
// STATION an Identity card. Its id is its address on this network.
// OPERATOR whoever holds that card right now, verified live at every call.
// PAYLOAD an opaque string of octets. This contract never reads one.
//
// -- WHAT THIS IS ---------------------------------------------------------
//
// A telegraph. Stations send octets to stations. Nothing here parses, decodes,
// validates or renders a payload; it is carried and committed to, and what it
// means is a matter between the two operators. There is no string type in this
// file for that reason. Hexadecimal is not a display format here, it is the
// wire format, and Etherscan is a sufficient terminal for both ends of it.
//
// -- PROCEDURE · WORKING FROM ETHERSCAN -----------------------------------
//
// TO SEND
// Contract > Write Contract > Connect > transmit
// from your station id, decimal. IdentityMD.cardsOf(you) lists them.
// to their station id, or 0 to broadcast to no one in particular
// payload 0x followed by an even count of hex digits, up to 2048 of them
// retain true also parks it in their mailbox, readable without a log query
//
// TO READ ADDRESSED TRAFFIC
// Contract > Read Contract > mailbox(id) returns the last retained payload as
// hex. It is the only view here that returns a body. Everything else lives in
// the log, because that is where an archive belongs and storage is not free.
//
// TO READ THE ARCHIVE
// The Events tab, filtered on Traffic. topic1 is the sender, topic2 the
// recipient, topic3 the running digest. The payload is the unindexed field and
// Etherscan prints it raw, uninterpreted, which is the entire intention.
//
// TO PROVE YOU HAVE ALL OF IT
// Replay a station's Traffic events in order through {digest}, starting from
// bytes32(0), and compare the result against station(id).tip. A relay that
// dropped, reordered or invented one message cannot reproduce the tip. This is
// the reason a transmission touches storage at all: a log you cannot verify is
// a log you are trusting someone else to have served you completely.
//
// -- THE TWO BOUNDS AND WHY THEY DIFFER -----------------------------------
//
// MAX_OCTETS 1024 logged payload. Calldata and log data, no storage.
// MAX_RETAIN 128 mailbox payload. Storage, four words, paid every time.
//
// Retention is for short control traffic: a status word, a key fingerprint, a
// pointer to something larger. Anything of size goes in the log, where a
// kilobyte costs log gas rather than thirty-two SSTOREs.
//
// -- MEASURED -------------------------------------------------------------
//
// gas_identitymd.js, mainnet fork at block 25,896,457. FIRST is a station that
// has never transmitted; REPEAT is the same call again, everything warm.
//
// first repeat
// transmit 26 octets, logged 102,494 51,194
// transmit 256 octets, logged -- 56,769
// transmit 1024 octets, logged -- 75,644
// transmit 128 octets, RETAINED 166,794 67,451
// broadcast 26 octets 42,310 --
// setCallsign 57,245 --
// setBeacon 64 octets 97,482 --
// clearMailbox 43,361 --
//
// A station's first transmission writes three slots that were zero (its tip, its
// counters, the recipient's counters) and costs roughly double the second.
//
// The ratio that decided the design: RETAINING 128 octets costs 67,451, while
// LOGGING 1,024 of them costs 75,644. Eight times the payload, for 12% more gas.
// That is why the archive is the log and the mailbox is a doorstep.
//
// -- WHAT THIS DOES NOT DO ------------------------------------------------
//
// It does not encrypt. A payload is public the moment it is mined and stays
// public for as long as the chain does. If it should be private, encrypt it
// before you paste it in and this contract will carry the ciphertext without
// knowing or caring. Addressing is a label and not a permission: anyone can read
// traffic sent to any station, and `to` is a claim about intent, not a lock.
//
// It also holds no ether, charges no postage, has no owner, no pause, no
// upgrade and no administrator. If every operator walked away it would keep
// carrying traffic for whoever remained.
contract Telegraph {
/// @notice The card contract. A station exists exactly while its card does.
IIdentity public immutable card;
/// @notice Logged payload ceiling, in octets.
uint256 public constant MAX_OCTETS = 1024;
/// @notice Retained (stored) payload ceiling, in octets.
uint256 public constant MAX_RETAIN = 128;
/// @notice Beacon ceiling, in octets.
uint256 public constant MAX_BEACON = 256;
struct Station {
bytes32 tip; // running commitment to everything this station has sent
uint64 sent;
uint64 received;
bytes8 callsign; // 0 if unclaimed
uint64 lastAt; // block timestamp of the last transmission sent
}
mapping(uint256 => Station) public station;
mapping(bytes8 => uint256) public bearerOf; // callsign -> station id, 0 if free
mapping(uint256 => bytes) public mailbox; // station id -> last retained inbound
mapping(uint256 => bytes) public beacon; // station id -> its own public blob
event Traffic(uint256 indexed from, uint256 indexed to, bytes32 indexed digest, bytes payload);
event Retained(uint256 indexed to, uint256 indexed from, uint256 octets);
event MailboxCleared(uint256 indexed id);
event CallsignSet(uint256 indexed id, bytes8 indexed callsign);
event CallsignReleased(uint256 indexed id, bytes8 indexed callsign);
event BeaconSet(uint256 indexed id, uint256 octets);
error NotBearer(uint256 id);
error NoSuchStation(uint256 id);
error TooLong(uint256 got, uint256 max);
error CallsignTaken(bytes8 callsign, uint256 heldBy);
error CallsignEmpty();
error ZeroAddress();
constructor(address _card) {
if (_card == address(0)) revert ZeroAddress();
card = IIdentity(_card);
}
/// @dev Live check. Sell the card and you stop being the operator in the same block.
modifier onlyBearer(uint256 id) {
if (card.holderOf(id) != msg.sender) revert NotBearer(id);
_;
}
// -- the wire -------------------------------------------------------------
/// @notice The commitment for one transmission. Pure, and used verbatim by
/// {transmit}, so an off-chain replay and the on-chain tip cannot drift.
function digest(bytes32 prev, uint256 from, uint256 to, bytes calldata payload)
public pure returns (bytes32)
{
return keccak256(abi.encodePacked(prev, from, to, keccak256(payload)));
}
/// @notice Send octets from your station to another.
/// @param from your station id
/// @param to recipient station id, or 0 to broadcast
/// @param payload opaque octets, at most MAX_OCTETS
/// @param retain also park it in the recipient's mailbox (costs storage, capped
/// at MAX_RETAIN; ignored for a broadcast)
function transmit(uint256 from, uint256 to, bytes calldata payload, bool retain)
external onlyBearer(from) returns (bytes32 d)
{
if (payload.length > MAX_OCTETS) revert TooLong(payload.length, MAX_OCTETS);
Station storage s = station[from];
d = digest(s.tip, from, to, payload);
s.tip = d;
unchecked { s.sent += 1; }
s.lastAt = uint64(block.timestamp);
if (to != 0) {
// never awakened, or slept since: there is nothing at that address
if (card.holderOf(to) == address(0)) revert NoSuchStation(to);
unchecked { station[to].received += 1; }
if (retain) {
if (payload.length > MAX_RETAIN) revert TooLong(payload.length, MAX_RETAIN);
mailbox[to] = payload;
emit Retained(to, from, payload.length);
}
}
emit Traffic(from, to, d, payload);
}
/// @notice Transmit to no particular station. Same log, same chain, `to` is 0.
function broadcast(uint256 from, bytes calldata payload) external onlyBearer(from) returns (bytes32 d) {
if (payload.length > MAX_OCTETS) revert TooLong(payload.length, MAX_OCTETS);
Station storage s = station[from];
d = digest(s.tip, from, 0, payload);
s.tip = d;
unchecked { s.sent += 1; }
s.lastAt = uint64(block.timestamp);
emit Traffic(from, 0, d, payload);
}
/// @notice Empty your own mailbox. Releases the storage and refunds part of the cost.
function clearMailbox(uint256 id) external onlyBearer(id) {
delete mailbox[id];
emit MailboxCleared(id);
}
// -- addressing -----------------------------------------------------------
/// @notice Claim a callsign for your station. Eight octets, unique, and released
/// automatically if you replace it. A callsign follows the STATION, not the
/// wallet: sell the card and the new operator inherits it.
function setCallsign(uint256 id, bytes8 cs) external onlyBearer(id) {
if (cs == bytes8(0)) revert CallsignEmpty();
uint256 taken = bearerOf[cs];
if (taken != 0 && taken != id) revert CallsignTaken(cs, taken);
bytes8 old = station[id].callsign;
if (old != bytes8(0)) {
delete bearerOf[old];
emit CallsignReleased(id, old);
}
station[id].callsign = cs;
bearerOf[cs] = id;
emit CallsignSet(id, cs);
}
/// @notice Your station's own public blob. Whatever you want it to mean.
function setBeacon(uint256 id, bytes calldata data) external onlyBearer(id) {
if (data.length > MAX_BEACON) revert TooLong(data.length, MAX_BEACON);
beacon[id] = data;
emit BeaconSet(id, data.length);
}
// -- reading --------------------------------------------------------------
/// @notice Station id and current operator behind a callsign. Zeroes if unclaimed.
function resolve(bytes8 cs) external view returns (uint256 id, address operator) {
id = bearerOf[cs];
if (id != 0) operator = card.holderOf(id);
}
/// @notice Everything about a station in one call: its operator, its counters, its
/// callsign and the commitment to its entire outbound history.
function report(uint256 id)
external view
returns (address operator, bytes32 tip, uint64 sent, uint64 received, bytes8 callsign, uint64 lastAt, uint256 mailboxOctets)
{
Station storage s = station[id];
operator = card.holderOf(id);
return (operator, s.tip, s.sent, s.received, s.callsign, s.lastAt, mailbox[id].length);
}
/// @notice Octets currently retained for `id`. The body itself is `mailbox(id)`.
function mailboxLength(uint256 id) external view returns (uint256) {
return mailbox[id].length;
}
}
Compiler settingsShow
{
"viaIR": true,
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.