Skip to content

Commit

Permalink
Merge PeerContact into Contact protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Le Cam committed Jun 28, 2019
1 parent 07bb9e2 commit 4527993
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 154 deletions.
111 changes: 59 additions & 52 deletions __tests__/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,33 +208,63 @@ describe('protocols', () => {
signBytes: async (bytes, key) => sign(bytes, key),
})

test('contact', async () => {
const aliceKeyPair = createKeyPair()
const bobKeyPair = createKeyPair()
describe('contact protocols', () => {
test('peerContact', async () => {
const aliceKeyPair = createKeyPair()
const bobKeyPair = createKeyPair()

const sendContact = {
profile: {
displayName: 'Alice',
},
}
const sendPeerContact = {
contactPublicKey: createKeyPair().getPublic('hex'),
peerAddress: getPublicAddress(aliceKeyPair),
}

// Write Alice -> Bob peerContact using Alice's private key and Bob's public key
await writePeerContact(
{
bzz,
keyPair: aliceKeyPair,
peerKey: bobKeyPair.getPublic('hex'),
},
sendPeerContact,
)

// Write Alice -> Bob contact using Alice's private key and Bob's public key
await writeContact(
{
// Read Alice -> Bob peerContact using Alice's public key and Bob's private key
const receivedPeerContact = await readPeerContact({
bzz,
keyPair: aliceKeyPair,
contactKey: bobKeyPair.getPublic('hex'),
},
sendContact,
)
keyPair: bobKeyPair,
peerKey: aliceKeyPair.getPublic('hex'),
})
expect(receivedPeerContact).toEqual(sendPeerContact)
})

// Read Alice -> Bob contact using Alice's public key and Bob's private key
const receivedContact = await readContact({
bzz,
keyPair: bobKeyPair,
contactKey: aliceKeyPair.getPublic('hex'),
test('contact', async () => {
const aliceKeyPair = createKeyPair()
const bobKeyPair = createKeyPair()

const sendContact = {
profile: {
displayName: 'Alice',
},
}

// Write Alice -> Bob contact using Alice's private key and Bob's public key
await writeContact(
{
bzz,
keyPair: aliceKeyPair,
contactKey: bobKeyPair.getPublic('hex'),
},
sendContact,
)

// Read Alice -> Bob contact using Alice's public key and Bob's private key
const receivedContact = await readContact({
bzz,
keyPair: bobKeyPair,
contactKey: aliceKeyPair.getPublic('hex'),
})
expect(receivedContact).toEqual(sendContact)
})
expect(receivedContact).toEqual(sendContact)
})

test('messaging', async done => {
Expand Down Expand Up @@ -367,34 +397,6 @@ describe('protocols', () => {
await publish(peer)
})

test('peerContact', async () => {
const aliceKeyPair = createKeyPair()
const bobKeyPair = createKeyPair()

const sendPeerContact = {
contactPublicKey: createKeyPair().getPublic('hex'),
peerAddress: getPublicAddress(aliceKeyPair),
}

// Write Alice -> Bob peerContact using Alice's private key and Bob's public key
await writePeerContact(
{
bzz,
keyPair: aliceKeyPair,
peerKey: bobKeyPair.getPublic('hex'),
},
sendPeerContact,
)

// Read Alice -> Bob peerContact using Alice's public key and Bob's private key
const receivedPeerContact = await readPeerContact({
bzz,
keyPair: bobKeyPair,
peerKey: aliceKeyPair.getPublic('hex'),
})
expect(receivedPeerContact).toEqual(sendPeerContact)
})

test('end-to-end flow', async () => {
jest.setTimeout(30000)

Expand Down Expand Up @@ -476,8 +478,8 @@ describe('protocols', () => {
reader: aliceBobPeerContact.contactPublicKey,
})

// Push a file to Alice's FS and share the FS public key with Bob in their contact channel
await aliceBobFS.uploadFile('/readme.txt', 'Hello!', { encrypt: true })
// Push changes to Alice's FS and share the public key with Bob in their contact channel
await Promise.all([
aliceBobFS.push(),
writeContact(
Expand Down Expand Up @@ -509,6 +511,7 @@ describe('protocols', () => {
const fileFromAlice = bobAliceFS.getFile('/readme.txt')
expect(fileFromAlice).toBeDefined()

// Now let's add a third user, Chloe, who is going to interact with Bob
const chloeKeyPair = createKeyPair()
const chloeAddress = getPublicAddress(chloeKeyPair)
const chloeBobKeyPair = createKeyPair()
Expand All @@ -531,7 +534,7 @@ describe('protocols', () => {
),
])

// Bob can access Chloe's peer and peerContact data
// Bob can now access Chloe's peer and peerContact data
const chloePeer = await readPeer({ bzz, peer: chloeAddress })
expect(chloePeer).toBeDefined()

Expand All @@ -555,6 +558,7 @@ describe('protocols', () => {
publishMessage({
title: 'Hello',
body: 'See attachment',
// Bob is attaching the metadat of the file Alice shared with him
attachments: [{ name: 'readme.txt', file: fileFromAlice }],
}),
writeContact(
Expand All @@ -574,6 +578,7 @@ describe('protocols', () => {
),
])

// Chloe reads Bob's peerContact and contact payloads
const chloeBobPeerContact = await readPeerContact({
bzz,
keyPair: chloeKeyPair,
Expand All @@ -588,6 +593,7 @@ describe('protocols', () => {
expect(chloeBobContact).toBeDefined()
expect(chloeBobContact.mailboxes).toBeDefined()

// Chloe reads from the mailbox Bob has created and loads the message sent
const reader = createMailboxReader({
bzz,
keyPair: chloeBobKeyPair,
Expand All @@ -599,6 +605,7 @@ describe('protocols', () => {
const attachment = chapter.content.data.attachments[0]
expect(attachment).toBeDefined()

// Chloe downloads the file originally shared by Alice
const fileStream = await downloadFile(bzz, attachment.file)
const text = await getStream(fileStream)
expect(text).toBe('Hello!')
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export {
// Contact
createContactSubscriber,
createPeerContactSubscriber,
readContact,
readPeerContact,
writeContact,
writePeerContact,
// Messaging
MailboxReaderParams,
MailboxWriterParams,
Expand All @@ -24,10 +27,6 @@ export {
createPeerSubscriber,
readPeer,
writePeer,
// PeerContact
createPeerContactSubscriber,
readPeerContact,
writePeerContact,
} from './protocols'
export { Peer, peerSchema } from './schemas/peer'
export {
Expand Down
59 changes: 57 additions & 2 deletions packages/core/src/protocols/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import Bzz, { PollContentOptions } from '@erebos/api-bzz-base'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'

import { Contact } from '../schemas/contact'
import { Contact, PeerContact } from '../schemas/contact'
import {
createEntityFeedSubscriber,
readFeedEntity,
writeFeedEntity,
} from '../channels'
import { CONTACT_NAME } from '../namespace'
import { CONTACT_NAME, PEER_CONTACT_NAME } from '../namespace'

export interface ContactParams {
bzz: Bzz<any>
Expand Down Expand Up @@ -64,3 +64,58 @@ export function createContactSubscriber(
writer: params.contactKey,
}).pipe(map((payload: any) => payload.data))
}

export interface PeerContactParams {
bzz: Bzz<any>
keyPair: any // TODO: keyPair type
peerKey: string
}

export async function readPeerContact(
params: PeerContactParams,
): Promise<PeerContact | null> {
return await readFeedEntity<PeerContact>({
bzz: params.bzz,
entityType: PEER_CONTACT_NAME,
keyPair: params.keyPair,
name: PEER_CONTACT_NAME,
writer: params.peerKey,
})
}

export async function writePeerContact(
params: PeerContactParams,
data: PeerContact,
): Promise<string> {
return await writeFeedEntity<PeerContact>(
{
bzz: params.bzz,
entityType: PEER_CONTACT_NAME,
keyPair: params.keyPair,
name: PEER_CONTACT_NAME,
reader: params.peerKey,
},
data,
)
}

export interface PeerContactSubscriberParams extends PeerContactParams {
options: PollContentOptions
}

export function createPeerContactSubscriber(
params: PeerContactSubscriberParams,
): Observable<PeerContact> {
return createEntityFeedSubscriber<PeerContact>({
bzz: params.bzz,
entityType: PEER_CONTACT_NAME,
keyPair: params.keyPair,
name: PEER_CONTACT_NAME,
options: {
whenEmpty: 'ignore',
...params.options,
mode: 'raw',
},
writer: params.peerKey,
}).pipe(map((payload: any) => payload.data))
}
14 changes: 8 additions & 6 deletions packages/core/src/protocols/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export { createContactSubscriber, readContact, writeContact } from './contact'
export {
createContactSubscriber,
createPeerContactSubscriber,
readContact,
readPeerContact,
writeContact,
writePeerContact,
} from './contact'
export {
MailboxReaderParams,
MailboxWriterParams,
Expand All @@ -23,8 +30,3 @@ export {
readPeer,
writePeer,
} from './peer'
export {
createPeerContactSubscriber,
readPeerContact,
writePeerContact,
} from './peerContact'
66 changes: 0 additions & 66 deletions packages/core/src/protocols/peerContact.ts

This file was deleted.

21 changes: 19 additions & 2 deletions packages/core/src/schemas/contact.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CONTACT_NAME, getID } from '../namespace'
import { CONTACT_NAME, PEER_CONTACT_NAME, getID } from '../namespace'

import { Mailboxes, mailboxesProperty } from './messaging'
import { Profile, profileProperty } from './profile'
import { publicKeyProperty } from './scalars'
import { ethereumAddressProperty, publicKeyProperty } from './scalars'

export interface Contact {
fileSystemKey?: string
Expand All @@ -21,3 +21,20 @@ export const contactSchema = {
},
additionalProperties: false,
}

export interface PeerContact {
contactPublicKey: string
peerAddress: string
}

export const peerContactSchema = {
$async: true,
$id: getID(PEER_CONTACT_NAME),
type: 'object',
required: ['contactPublicKey', 'peerAddress'],
properties: {
contactPublicKey: publicKeyProperty,
peerAddress: ethereumAddressProperty,
},
additionalProperties: false,
}
Loading

0 comments on commit 4527993

Please sign in to comment.