Skip to content

BRC-103/104 auth matches @bsv/sdk 4.1.1 Peer and AuthFetch - #22

Merged
rohenaz merged 13 commits into
mainfrom
auth-ts4-interop
Nov 16, 2026
Merged

rohenaz merged 13 commits into
mainfrom
auth-ts4-interop

Conversation

@rohenaz

@rohenaz rohenaz commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Before: Swift auth sessions and certificate exchange differed from @bsv/sdk 4.1.1 in ways a TS peer notices. Certificate responses were not matched to requests the way TS Peer does, a peer could keep any number of certificate requests open, a forged certificate response was matched before its signature was checked, and auth accepted payloads the wallet then refused to sign.

After: a Swift peer and a TS 4.1.1 peer settle handshakes, certificate requests and general messages the same way. A response that fits both a later request and the handshake policy settles both, as Peer.ts does. Open received requests share one byte budget and repeats are refused. Every message is verified and its nonce claimed before it can be refused. Auth and the wallet share one payload limit, 16 MiB plus 512 KiB, as in AuthFetch.ts.

How: BSVAuth Peer/session handling follows Peer.ts:1088-1092 and 1202-1247; validateSubset throws on no certificates as validateCertificates.ts does; new PeerAuthenticator.withdrawCertificateRequest lets a caller drop a request its transport could not deliver (Peer.ts:480-485); makeGeneralMessage throws resourceLimit for an oversized payload. WalletCryptoLimits.defaultPayloadByteCount is the single payload limit. Errors from a caller's wallet are logged by type with their description private. COMP-041, 052 and 060 record the remaining Swift-only rules. swift test: 1013 tests and 155 XCTest pass in Docker (swift 6.1); public API check passes.

Callers may see: validateSubset([]) now throws; a repeated or over-budget received certificate request is refused; a certificate response with a bad signature throws invalidSignature; an oversized general message throws resourceLimit instead of notAuthenticated.

Responder session nonces are 48 bytes. Requests without
x-bsv-auth-message-type are accepted as general. An absent or empty body
signs as -1 in requests and responses, and a received request is rebuilt
without the client JSON body default. content-type is normalised as
authenticatedContentType does, keeping multipart parameters.

Certificate messages carry and check initialNonce. The codec parses JSON
in member order and verifies certificate signatures over the received
text, rebuilt as Peer.ts does, with JSON.stringify escaping. Outgoing
messages are written by hand so the wire bytes equal the signed bytes.
Requests and responses follow the TypeScript allowlist: empty type maps
and field lists, subset and empty responses, handshake policies returned
as certificate requests, and responder authentication on any signed
certificate message.

Default limits match the TypeScript SDK, with a separate limit for
x-bsv-payment.

TypeScriptInteropTests replays messages recorded from @bsv/sdk 4.1.1 and
can write Swift messages for a live TypeScript check.
Update the rulings for 48-byte session nonces, the optional request
message-type header, -1 empty bodies, content-type normalisation, the
TypeScript limits, and allowlist certificate exchange with member-order
JSON preimages. Add TypeScriptAuthVectors.md with the scripts that record
the vectors and check Swift messages against a live TypeScript peer.
Sort signed BRC-104 headers and sign certificate fields in JavaScript
localeCompare order (ICU root collation), and accept certificate
signatures over either that order or UTF-8 byte order.

Refuse BRC-104 statuses outside 200...599, methods that are not 1 to 32
uppercase token characters, and request targets over 8192 characters,
as AuthFetch and the auth middleware do.

Accept requested certificate sets with no certifiers, which ask for
nothing. Read and write x-bsv-auth-requested-certificates on general
messages. Add beginAuthentication(with:requesting:) and surface
certificates a peer includes in initialResponse.
Update COMP-041, COMP-052, COMP-059, COMP-060 and GO-063, and record the
provenance and Apache-2.0 license of the vectors recorded from
@bsv/sdk 4.1.1 in FixtureSources.md and VectorInventory.md.
- PeerAuthenticator(certificatesToRequest:) sends a responder policy in
  initialResponse, which a TypeScript AuthFetch client answers on its own.
- Keep up to 16 open certificate requests per direction; a response settles
  the oldest request it fits, or the one named with answering:.
- Add AuthCertificateExchange.validateSubset, matching validateCertificates.
- Refuse to issue certificates with field names outside printable ASCII.
- Log the detailed cause of collapsed errors to dev.opl.swift-sdk/BSVAuth.
- Make error descriptions name no side.
- Leave every x-bsv-auth header unsigned and refuse signed header values
  that fetch would trim or reinterpret.
- Test the responder policy against a recorded AuthFetch exchange.
Describe open requests, responder policies, validateSubset and logging in
COMP-060, the ASCII field-name rule in COMP-041, the header rules in
COMP-052, and the new TypeScript vector scenario.
- Charge the requests a peer has open in a session against one
  maximumCertificateAggregateBytes budget, and refuse a repeat of an open one.
- Keep the handshake policy apart from certificateRequests and settle a
  response against certificateRequests first, as Peer.ts does.
- Verify a certificateResponse signature before matching or logging it.
- validateSubset fails on no certificates, as validateCertificates does.
- Log the description of an error this SDK does not define as private.
- Say that answering: settles a request on the sending side only.
- Update COMP-060, its Rulings row and the GO-062 note.
Conflicts in AuthModels.swift, BRC104Payload.swift, the framing tests and
Rulings.md. The branch's 16 MiB plus 512 KiB payload default is kept, since
it covers PR 19's 16 MiB; PR 19's large-payload tests now check that default.
COMP-041 notes that the wallet binary codec now reads fields in any order.
On this branch an empty body is written as -1 in nine bytes, so the
overhead taken from an empty response was eight bytes too large.
# Conflicts:
#	Documentation/Compatibility/Rulings.md
…yload limit

- A response whose certificates fit both a certificateRequest and the
  handshake policy settles both on each side, and the receiver returns one
  certificate-response action per request (Peer.ts:1247).
- WalletCryptoLimits defaults to 16 MiB plus 512 KiB, defined once and used
  for AuthLimits.defaultPayloadBytes, so ProtoWallet signs and verifies every
  general payload the authenticator accepts.
- A refused certificateRequest uses up its nonce and counts as a message.
- A certificateResponse is verified before it is refused for having nothing
  to answer.
- Add withdrawCertificateRequest so a sender can stop waiting for a request
  the peer refused.
- makeGeneralMessage throws resourceLimit for an oversized payload.
- Log entries keep the private part apart, and a test checks a caller
  wallet's error through a real call path.
- Update COMP-060, COMP-052 and the COMP-060 row.
@rohenaz
rohenaz merged commit 3dae243 into main Nov 16, 2026
4 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant