Beacon is an implementation of tzip-10 and Matrix is the transport layer, which as I understand is a pub/sub system. The communication details between the two however isn't clear. Can you provide the details how the handshake between the the dApp and a wallet occurs and how Beacon connects and authenticates to Matrix nodes?
The flow is as follows:
- Both the wallet and dApp create their own sets of key pairs (just a standard Ed25519 key pair, not related to Matrix at this point).
- Both the wallet and dApp log in to Matrix using derivatives of their public keys as the user ID and start listening for events.
- Upon request, the dApp generates a QR code which contains, amongst others, its public key (from step 1).
- The wallet scans the QR code, creates a new private room and invites the dApp using the key from the QR code. It waits then for the dApp to join the room.
- The dApp receives the invite and joins the room.
- The wallet uses the room to send a pairing response (basically the same info you get in the QR code but this time it’s generated by the wallet) to the dApp.
- At this point the wallet and dApp are paired and will continue communicating via the room, i.e. when the dApp initiates a request, it sends it to the room where it gets read and processed by the wallet, the wallet next creates a response and sends it back to the room where the dApp sees it. Before it ends up in the room, every message (JSON) is serialized with Base58Check and encrypted with a session key created from a combination of keys from step 1. The list of messages can be found in the proposal or the docs.