Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/websocket transport #810

Draft
wants to merge 33 commits into
base: develop
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4543b57
feat: init acurast WebSocketP2P transport
IsaccoSordo Jul 26, 2024
ad43f7b
feat: init wsTransport
IsaccoSordo Jul 26, 2024
3edf5b6
feat: add libp2p type
IsaccoSordo Jul 26, 2024
a43eb79
feat: init libp2p transport in dappClient
IsaccoSordo Jul 26, 2024
6d04169
feat: add getPairingRequestInfo
IsaccoSordo Jul 26, 2024
836edfd
feat: add QR code generation
IsaccoSordo Jul 26, 2024
d975b11
feat: init peer
IsaccoSordo Aug 5, 2024
468c23c
fix: message send
IsaccoSordo Aug 6, 2024
d6092f8
fix: listener
IsaccoSordo Aug 6, 2024
306496e
fix: onMessage
IsaccoSordo Aug 7, 2024
825ebcb
fix: request permissions
IsaccoSordo Aug 7, 2024
3edea76
chore: update urls
IsaccoSordo Aug 7, 2024
549e116
fix: moved key generation to ws class
IsaccoSordo Aug 8, 2024
105ef0d
chore: alpha release
IsaccoSordo Aug 23, 2024
f23efd7
feat: add NodeDistributions
IsaccoSordo Aug 23, 2024
9f9ec0f
feat: init test folder
IsaccoSordo Aug 23, 2024
a3be68b
fix: removed unused helper
IsaccoSordo Aug 27, 2024
97905ce
fix: minor tweaks
IsaccoSordo Aug 27, 2024
6bbf71f
fix: use libp2p nodes
IsaccoSordo Aug 27, 2024
4eacdec
feat: generate pair from seed
IsaccoSordo Aug 27, 2024
86a3724
chore: bump node version
IsaccoSordo Aug 27, 2024
724432b
fix: elliptic version
IsaccoSordo Aug 27, 2024
3e7bea9
fix: lockfile
IsaccoSordo Aug 27, 2024
bb8bc99
fix: remove this.seed
IsaccoSordo Aug 28, 2024
e6e51f1
feat: allow alpha tags
IsaccoSordo Aug 28, 2024
38e24c7
fix: gitlab ci
IsaccoSordo Aug 28, 2024
ac6d119
fix: .prettierignore
IsaccoSordo Aug 28, 2024
32f72eb
fix: prettierignore
IsaccoSordo Aug 28, 2024
3bca04d
Merge branch 'feat/websocket_transport' of https://gitlab.papers.tech…
IsaccoSordo Aug 28, 2024
9603f2b
fix: utilities
IsaccoSordo Aug 28, 2024
c5ebb51
fix: script
IsaccoSordo Aug 28, 2024
40a891a
chore: alpha release
IsaccoSordo Aug 28, 2024
57eca9e
fix: version
IsaccoSordo Aug 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: init wsTransport
IsaccoSordo committed Jul 26, 2024
commit ad43f7bca1476a5da6f34bfd73349e4b970b36b0
102 changes: 0 additions & 102 deletions packages/beacon-transport-libp2p/src/P2PTransport.ts

This file was deleted.

52 changes: 52 additions & 0 deletions packages/beacon-transport-libp2p/src/WebSocketP2PTransport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { PeerManager, Transport } from '@airgap/beacon-core'
import { WebSocketP2PCommunicationClient } from './communication-client/WebSocketP2PCommunicationClient'
import {
P2PPairingRequest,
ExtendedP2PPairingResponse,
Storage,
StorageKey,
Origin,
} from '@airgap/beacon-types'
import { KeyPair } from '@acurast/dapp'

const DEFAULT_NODES = [
'wss://websocket-proxy-1.prod.gke.acurast.com/',
'wss://websocket-proxy-2.prod.gke.acurast.com/'
]

export class WebSocketP2PTransport<
T extends P2PPairingRequest | ExtendedP2PPairingResponse,
K extends StorageKey.TRANSPORT_P2P_PEERS_DAPP | StorageKey.TRANSPORT_P2P_PEERS_WALLET
> extends Transport<T, K, WebSocketP2PCommunicationClient> {
constructor(
name: string,
keyPair: KeyPair,
storage: Storage,
storageKey: K,
urls: string[] = DEFAULT_NODES
) {
super(
name,
new WebSocketP2PCommunicationClient(urls, keyPair),
new PeerManager(storage, storageKey)
)
}

async connect() {
await this.client.connect()
super.connect()
}

async disconnect(): Promise<void> {
await this.client.close()
}

async listen(publicKey: string): Promise<void> {
this.client.listenForEncryptedMessage(publicKey, (message) =>
this.notifyListeners(message, {
origin: Origin.P2P,
id: publicKey
})
)
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { CommunicationClient } from '@airgap/beacon-core'
import { PeerInfoType } from '@airgap/beacon-types'
import { AcurastClient, KeyPair } from '@acurast/dapp'
import { forgeMessage, Message } from '@acurast/transport-websocket'
import { hexFrom } from '../utils/bytes'
import { KeyPair as KP } from '@stablelib/ed25519'

export class WebSocketP2PCommunicationClient extends CommunicationClient {
private client: AcurastClient
private listeners: Map<string, (message: string) => void> = new Map()

constructor(urls: string[], keyPair: KeyPair) {
super(keyPair as unknown as KP)
this.client = this.initClient(urls)
}

private initClient(urls: string[]): AcurastClient {
const client = new AcurastClient(urls)
client.onMessage((message) => {
const fun = this.listeners.get(hexFrom(message.recipient))
fun && fun(hexFrom(forgeMessage(message as Message)))
})

return client
}

async unsubscribeFromEncryptedMessages(): Promise<void> {
this.listeners.clear()
}

/**
* Unsubscribe from the specified listener
* @param senderPublicKey
*/
async unsubscribeFromEncryptedMessage(senderPublicKey: string): Promise<void> {
this.listeners.delete(this.client.idFromPublicKey(senderPublicKey))
}

connect() {
return this.client.start(this.keyPair!)
}

close() {
return this.client.close()
}

async listenForEncryptedMessage(
senderPublicKey: string,
messageCallback: (message: string) => void
) {
this.listeners.set(this.client.idFromPublicKey(senderPublicKey), messageCallback)
}

async sendMessage(message: string, peer?: PeerInfoType): Promise<void> {
if (!peer) {
return
}

this.client.send(peer.publicKey, message)
}
}
5 changes: 3 additions & 2 deletions packages/beacon-transport-libp2p/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { P2PCommunicationClient } from './communication-client/P2PCommunicationClient'
export { P2PTransport } from './P2PTransport'
export { WebSocketP2PCommunicationClient } from './communication-client/WebSocketP2PCommunicationClient'

export { WebSocketP2PTransport } from './WebSocketP2PTransport'
58 changes: 0 additions & 58 deletions packages/beacon-transport-libp2p/src/matrix-client/EventEmitter.ts

This file was deleted.

403 changes: 0 additions & 403 deletions packages/beacon-transport-libp2p/src/matrix-client/MatrixClient.ts

This file was deleted.

This file was deleted.

This file was deleted.

170 changes: 0 additions & 170 deletions packages/beacon-transport-libp2p/src/matrix-client/MatrixHttpClient.ts

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

41 changes: 0 additions & 41 deletions packages/beacon-transport-libp2p/src/matrix-client/utils/events.ts

This file was deleted.

8 changes: 8 additions & 0 deletions packages/beacon-transport-libp2p/src/utils/bytes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function hexFrom(value: ArrayBuffer | Uint8Array | Buffer): string {
const buffer: Buffer = Buffer.isBuffer(value) ? value : Buffer.from(value)
return buffer.toString('hex')
}

export function hexTo(value: string): Uint8Array {
return Uint8Array.from(Buffer.from(value, 'hex'))
}