Skip to content

Commit

Permalink
Add Contact protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Le Cam committed Jun 27, 2019
1 parent abdd6be commit 98cf1fa
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 6 deletions.
31 changes: 31 additions & 0 deletions __tests__/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import getStream from 'get-stream'

import {
// protocols
readContact,
writeContact,
FileSystemReader,
FileSystemWriter,
downloadFile,
Expand Down Expand Up @@ -202,6 +204,35 @@ describe('protocols', () => {
signBytes: async (bytes, key) => sign(bytes, key),
})

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)
})

describe('fileSystem', () => {
test('uploadFile() and downloadFile()', async () => {
const data = 'Hello test'
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export {
// Contact
createContactSubscriber,
readContact,
writeContact,
// FileSystem
FileSystemReader,
FileSystemReaderParams,
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ export const NAMESPACE_VERSION = 'v0'

export const PROTOCOL_NAME = 'aegle://'

export function getID(name: string): string {
return name.startsWith(PROTOCOL_NAME) ? name : PROTOCOL_NAME + name
}

export function namespace(
name: string,
version: string = NAMESPACE_VERSION,
): string {
return `${NAMESPACE_PREFIX}.${name}.${version}`
}

export function getID(name: string): string {
return name.startsWith(PROTOCOL_NAME) ? name : PROTOCOL_NAME + name
}

export const CONTACT_NAME = namespace('contact')
export const FILE_SYSTEM_NAME = namespace('fileSystem')
export const PEER_NAME = namespace('peer')
Expand Down
66 changes: 66 additions & 0 deletions packages/core/src/protocols/contact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Bzz, { PollContentOptions } from '@erebos/api-bzz-base'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'

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

export interface ContactParams {
bzz: Bzz<any>
keyPair: any // TODO: keyPair type
contactKey: string
}

export async function readContact(
params: ContactParams,
): Promise<Contact | null> {
return await readFeedEntity<Contact>({
bzz: params.bzz,
entityType: CONTACT_NAME,
keyPair: params.keyPair,
name: CONTACT_NAME,
writer: params.contactKey,
})
}

export async function writeContact(
params: ContactParams,
data: Contact,
): Promise<string> {
return await writeFeedEntity<Contact>(
{
bzz: params.bzz,
entityType: CONTACT_NAME,
keyPair: params.keyPair,
name: CONTACT_NAME,
reader: params.contactKey,
},
data,
)
}

export interface ContactSubscriberParams extends ContactParams {
options: PollContentOptions
}

export function createContactSubscriber(
params: ContactSubscriberParams,
): Observable<Contact> {
return createEntityFeedSubscriber<Contact>({
bzz: params.bzz,
entityType: CONTACT_NAME,
keyPair: params.keyPair,
name: CONTACT_NAME,
options: {
whenEmpty: 'ignore',
...params.options,
mode: 'raw',
},
writer: params.contactKey,
}).pipe(map((payload: any) => payload.data))
}
1 change: 1 addition & 0 deletions packages/core/src/protocols/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { createContactSubscriber, readContact, writeContact } from './contact'
export {
FileSystemReader,
FileSystemReaderParams,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/protocols/peerContact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { map } from 'rxjs/operators'

import { PeerContact } from '../schemas/peerContact'
import {
createFeedSubscriber,
createEntityFeedSubscriber,
readFeedEntity,
writeFeedEntity,
} from '../channels'
Expand Down Expand Up @@ -51,7 +51,7 @@ export interface PeerContactSubscriberParams extends PeerContactParams {
export function createPeerContactSubscriber(
params: PeerContactSubscriberParams,
): Observable<PeerContact> {
return createFeedSubscriber<PeerContact>({
return createEntityFeedSubscriber<PeerContact>({
bzz: params.bzz,
entityType: PEER_CONTACT_NAME,
keyPair: params.keyPair,
Expand Down

0 comments on commit 98cf1fa

Please sign in to comment.