-
Notifications
You must be signed in to change notification settings - Fork 14
Peer-socket API (WIP) #6
base: master
Are you sure you want to change the base?
Conversation
What's the main difference here in how this works vs dat peers? |
|
The biggest practical limitation of |
Over time it'll diverge more. Using separate sockets lets us construct the |
That's awesome. Especially if this is opening up the potential for other browsers to adopt a simiar API in the future. One could potentially run IPFS over this. 😂 This could even be used to experiment with new hyperdb features without waiting for Beaker to get first class integration. I think this will need to get additional |
Also, I think that Also since we're dreaming here, it'd be amazing if it was async iterable. 😁 |
I agree about the |
For the first iteration, there's not going to be any connection management (no |
This looks amazing. 😍 |
@RangerMauve Thanks! Still making lots of updates as we build and learn the requirements but I'm feeling good about it |
At a high level there are a couple possibilities I imagine for securing p2p channels. Option 1: Provide crypto functions that encapsulate identity (Crypto Identity API)Instead of integrating security into PeerSockets provide a more granular API for using the ed25519 Identity keys. Eg. const archive = await DatArchive.selectArchive({ authorizePrivateKey: true })
const encryptedText = await archive.publicKey.encrypt('super secret text')
const decryptedText = await archive.privateKey.decrypt(encryptedText)
const signature = await archive.privateKey.sign('super secret text') Pros:
Cons:
Option 2:Integrate security into PeerSockets directly. Eg. const archive = await DatArchive.selectArchive()
const swarm = await PeerSocket.joinSwarm({
myIdentity: archive
peerIdentityPublicKey: await archive.readFile('peerIdentity.pub')
algorithm: 'triple-diffie-hellman-aes-256`
}) Pros:
Cons:
Note So Option 2 is pretty much a Strawman here. When I started writing I thought there would be more reasons to support it but I haven't come up with them. If you can think of more reasons then let me know. Also there could be an Option 3 which would be a combined approach where we release both a secure PeerSocket API and a Identity Cryto API but I expect that the Secure PeerSocket API in that example would become a legacy maintenance headache with little benefit after community beaker crypto libraries are popularized. |
With regards to PeerSocket security, I think we were going to get transport level security within hyperswarm in the form of the noise protocol. |
I'm still catching up on this thread but is this feature perhaps a way to use hypercore feeds in Beaker? I recently became interested in that approach for storing sensor data efficiently (@dwblair). For the chat use case, perhaps as a way to store message history. |
It could, but there's also the Feed API that would be nice to have. I don't have the link offhand. |
We'll be adding the Hypercore API soon, so there shouldn't be any need to get PeerSockets involved. |
I think PeerSockets is still going to be important for stuff like the multiplayer demo I did at the dat event at DTN. :P |
I think @pfrazee you were referring to not needing PeerSockets to store a message history. Is that right? The difference is messages are saved when going over Hypercore as opposed to messages being ephemeral when going over PeerSockets? |
Yeah I just meant, we wont need PeerSockets as a means to get access to Hypercores |
TODOS
{readable, writeable}
implTarget API
PeerSocketSwarm
PeerSocketInfo
PeerSocket
Example usage
Notes about Readable streams
The
socket.readable
attribute is a ReadableStream. With async iteration, that will enable the following usage:Because the socket has the
{readable, writeable}
attributes it fits the shape of aTransformStream
and can therefore be used inpipeThrough()
.How multiple tabs & apps work
If an app has multiple tabs open, it will share access to the swarm and its sockets. The app needs to be aware of that and avoid duplicating behaviors. (It should coordinate with its other tabs.)
If multiple apps are in the same swarm (where an app is an origin, eg foo.com vs bar.com) then each app will have its own presence in the swarm and create its own sockets. That means a single device can have multiple presences in a swarm, one for each origin they have active in the swarm. (This decision was made because it'd be insane to force apps to coordinate with each other around shared socket ownership.)
Using
setDebugIdentity
for testingWhen testing an app, it's helpful to run multiple identities in separate tabs as a way to simulate multiple peers. This can be done with
PeerSocket.setDebugIdentity()
, which takes in a number:This will cause the tab to join the swarm as a separate identity than the other tabs, with its own set of sockets and events. The identity used is the number, and any number of identities can be used. If multiple tabs use the same debug identity number, they will share the identity.
setDebugIdentity
must be called before any otherPeerSocket
method.Relationship with Hyperswarm
PeerSocket
uses Hyperswarm. It automatically prefixes the "topic" identifers, thus the constructions are:Open vs Origin swarm
An open swarm can be joined by any origin that knows the swarm name. It is automatically used when
id
is set.An origin swarm can only be joined by pages that share an origin. This constraint is enforced by the browser, meaning a program outside of the browser could join the origin swarm. The origin swarm is used when no
id
is given for the swarm.Future plans
In the future, this API will be expanded to support: