Me: sources and import logic
The ten me* data sources in src/me/sources.ts and the pure helpers in src/me/logic.ts: arguments, replies, network calls, timeouts, error messages and tests.

src/me/sources.ts exports the Me package's sources, merged into the dispatcher in app.ts (The data module). src/me/logic.ts holds the parts with "No clock, no randomness" so they can be tested alone.

Reply shape

Most mutations answer MeReply: {ok, error, value}. ok(value) and refuse(error) build the two cases. A refusal is a normal answer, not a thrown error, so the screen can show the message.

Sources

Source

Arguments

Answer

meIdentities

none

MeIdentity[]: {accountId, keyName, shortId, createdAt}, oldest first

meProfileNames

server, ids

MeProfile[]: {accountId, name, icon} for accounts with a published name or icon

meCheckImport

text

MeCheck: {kind, valid, message, count}

meNewWords

none

MeWords: {mnemonic, words} from 16 random bytes

meCreateIdentity

mnemonic, name, nowMs

MeReply, value is the new account id

meImportIdentity

text, name, nowMs

MeReply, value is the account id

meRemoveIdentity

accountId, current

MeReply, value is the account to make current next

meRenameIdentity

accountId, name

MeReply

meExportKey

accountId

MeExport: {ok, error, text} with the .seedkey JSON

meCheckServer

input

MeReply, value is the normalized origin

Creating and importing

meCreateIdentity re-checks the phrase and refuses anything that is not a valid mnemonic with The recovery phrase is not valid. meImportIdentity branches on the check: a key file goes through parseSeedKey and keeps the file's createTime when present; a phrase goes through addFromMnemonic.

addFromMnemonic awaits keyPairFromMnemonicAsync, which yields during PBKDF2 for the 100 ms turn budget. The uniqueness check then runs after the derivation, in keepIdentity: "the check and save then execute in one synchronous turn", so two concurrent imports cannot both take one name. keepIdentity calls nameError and saveKey from Identity and turns an IdentityStorageError into its display message; any other failure becomes Could not save this identity.

Re-importing an identity already on the device succeeds and changes nothing, because saveKey is idempotent per account.

Removing

meRemoveIdentity calls removeKey, which leaves the {removed: true} marker the vault needs. The reply's value keeps current when it still exists and was not the one removed; otherwise it is the oldest remaining identity, or '' when none is left.

Network reads

fetchJson(url, ms) passes exactTimeout to fetch. The comment explains: "Source timers only run after native fetch settles. Give the transport the deadline so an unresponsive server cannot leave this source pending."

Call

Timeout

Use

GET {server}/api/Account?id={accountId}

8,000 ms

meProfileNames, one request per identity in parallel. A failure for one account leaves it out

GET {origin}/hm/api/config

10,000 ms

meCheckServer. The server is usable only when registeredAccountUid is a non-empty string, otherwise This server has no registered site. A transport failure answers Failed to connect: ...

Both endpoints are described in Seed site read API.

logic.ts

Input

Result

empty

kind: '', not valid, no message

starts with {

kind: 'seedkey', valid, message Key file (the file is parsed only on submit)

unknown words

Word N is not a valid recovery word. or Words at positions 2, 4 are not valid recovery words.

not 12 or 24 words

N of 12 words.

bad checksum

Invalid recovery phrase checksum. The Nth word must match the checksum of the others.

valid

N words, checksum OK.

    parseServerUrl(input): trims, prepends https:// when there is no scheme, accepts only http: and https:, rejects user names and passwords, and keeps only the origin. Errors: Enter a server address. and Invalid URL. Enter a valid server address.

    hostOf(url): the host name, or the input when it does not parse.

    readAccount(server, body): reads {name, icon} from an Account response, unwrapping the superjson json envelope. An ipfs:// icon becomes {server}/hm/api/image/{cid}?size=S; an https:// icon is kept; anything else is dropped. account-not-found yields empty strings.

Tests

    sources.test.ts, 6 tests: name trimming and uniqueness, idempotent re-import, concurrent imports racing for one name, key-file text refused by create, the next-current rule on removal, and the two exactTimeout values (10000 and 8000).

    logic.test.ts, 11 tests: six for checkImport, three for parseServerUrl (including credentials and non-hierarchical schemes), two for readAccount.

Both are part of the Bun unit tests.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime

Attributes
name
Me: sources and import logic
summary
The ten me* data sources in src/me/sources.ts and the pure helpers in src/me/logic.ts: arguments, replies, network calls, timeouts, error messages and tests.