Skip to content

Commit

Permalink
fix: message send
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaccoSordo committed Aug 6, 2024
1 parent d975b11 commit 468c23c
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 62 deletions.
88 changes: 75 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,20 @@ export class DAppClient extends Client {
.exportKey('raw', acurastKeyPair.publicKey)
.then((arrayBuffer) => Buffer.from(arrayBuffer))
])
const publicKeyCompressedSize = (publicKeyRaw.length - 1) / 2
const publicKeyCompressed = Buffer.concat([
new Uint8Array([publicKeyRaw[2 * publicKeyCompressedSize] % 2 ? 3 : 2]),
publicKeyRaw.subarray(1, publicKeyCompressedSize + 1)
])
const publicKeyHash = await crypto.subtle.digest('SHA-256', publicKeyCompressed)

this.libp2pTransport = new DappLibP2PTransport(
this.name,
{
publicKey: publicKeyRaw,
secretKey: privateKeyRaw
},
Buffer.from(publicKeyHash.slice(0, 16)).toString('hex'),
this.storage
)

Expand Down
17 changes: 14 additions & 3 deletions packages/beacon-dapp/src/transports/DappLibP2PTransport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ExtendedP2PPairingResponse, Storage, StorageKey, TransportStatus } from '@airgap/beacon-types'
import {
ExtendedP2PPairingResponse,
Storage,
StorageKey,
TransportStatus
} from '@airgap/beacon-types'
import { WebSocketP2PTransport } from '@airgap/beacon-transport-libp2p'
import { KeyPair } from '@stablelib/ed25519'
import { Logger } from '@airgap/beacon-core'
Expand All @@ -9,8 +14,14 @@ export class DappLibP2PTransport extends WebSocketP2PTransport<
ExtendedP2PPairingResponse,
StorageKey.TRANSPORT_LIBP2P_PEERS_DAPP
> {
constructor(name: string, keyPair: KeyPair, storage: Storage, urls?: string[]) {
super(name, keyPair, storage, StorageKey.TRANSPORT_LIBP2P_PEERS_DAPP, urls)
constructor(
name: string,
keyPair: KeyPair,
senderId: string,
storage: Storage,
urls?: string[]
) {
super(name, keyPair, senderId, storage, StorageKey.TRANSPORT_LIBP2P_PEERS_DAPP, urls)
}

public async startOpenChannelListener(): Promise<void> {
Expand Down
22 changes: 12 additions & 10 deletions packages/beacon-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@
"url": "https://github.com/airgap-it/beacon-sdk/issues"
},
"dependencies": {
"@airgap/beacon-blockchain-substrate": "4.2.2",
"@airgap/beacon-blockchain-tezos": "4.2.2",
"@airgap/beacon-core": "4.2.2",
"@airgap/beacon-dapp": "4.2.2",
"@airgap/beacon-transport-matrix": "4.2.2",
"@airgap/beacon-transport-postmessage": "4.2.2",
"@airgap/beacon-types": "4.2.2",
"@airgap/beacon-ui": "4.2.2",
"@airgap/beacon-utils": "4.2.2",
"@airgap/beacon-wallet": "4.2.2"
"@airgap/beacon-blockchain-substrate": "file:packages/beacon-blockchain-substrate",
"@airgap/beacon-blockchain-tezos": "file:packages/beacon-blockchain-tezos",
"@airgap/beacon-blockchain-tezos-sapling": "file:packages/beacon-blockchain-tezos-sapling",
"@airgap/beacon-core": "file:packages/beacon-core",
"@airgap/beacon-dapp": "file:packages/beacon-dapp",
"@airgap/beacon-transport-matrix": "file:packages/beacon-transport-matrix",
"@airgap/beacon-transport-postmessage": "file:packages/beacon-transport-postmessage",
"@airgap/beacon-transport-walletconnect": "file:packages/beacon-transport-walletconnect",
"@airgap/beacon-types": "file:packages/beacon-types",
"@airgap/beacon-ui": "file:packages/beacon-ui",
"@airgap/beacon-utils": "file:packages/beacon-utils",
"@airgap/beacon-wallet": "file:packages/beacon-wallet"
}
}
2 changes: 1 addition & 1 deletion packages/beacon-transport-libp2p/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airgap/beacon-transport-libp2p",
"version": "4.2.8",
"version": "4.2.23",
"description": "This package contains methods to facilitate communication over the Beacon network, a decentralised P2P network that is based on the matrix protocol.",
"author": "Andreas Gassmann <[email protected]>",
"homepage": "https://walletbeacon.io",
Expand Down
8 changes: 3 additions & 5 deletions packages/beacon-transport-libp2p/src/WebSocketP2PTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import {
} from '@airgap/beacon-types'
import { KeyPair } from '@stablelib/ed25519'

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

export class WebSocketP2PTransport<
T extends P2PPairingRequest | ExtendedP2PPairingResponse,
Expand All @@ -23,13 +20,14 @@ export class WebSocketP2PTransport<
constructor(
name: string,
keyPair: KeyPair,
pkHash: string,
storage: Storage,
storageKey: K,
urls: string[] = DEFAULT_NODES
) {
super(
name,
new WebSocketP2PCommunicationClient(name, urls, keyPair),
new WebSocketP2PCommunicationClient(name, urls, keyPair, pkHash),
new PeerManager(storage, storageKey)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ export class WebSocketP2PCommunicationClient extends CommunicationClient {
(pairingResponse: ExtendedP2PPairingResponse) => void
> = new Map()
private listeners: Map<string, (message: string) => void> = new Map()
private selectedNode: string

constructor(
private name: string,
private urls: string[],
keyPair: KeyPair
keyPair: KeyPair,
private senderId: string
) {
super(keyPair)
this.client = this.initClient(urls)
this.selectedNode = this.urls[Math.floor(Math.random() * this.urls.length)]
this.client = this.initClient([this.selectedNode])
}

private initClient(urls: string[]): AcurastClient {
Expand All @@ -47,8 +50,8 @@ export class WebSocketP2PCommunicationClient extends CommunicationClient {
* Unsubscribe from the specified listener
* @param senderPublicKey
*/
async unsubscribeFromEncryptedMessage(senderPublicKey: string): Promise<void> {
this.listeners.delete(this.client.idFromPublicKey(senderPublicKey))
async unsubscribeFromEncryptedMessage(senderId: string): Promise<void> {
this.listeners.delete(senderId)
this.channelOpeningListeners.delete('channelOpening')
}

Expand All @@ -70,46 +73,46 @@ export class WebSocketP2PCommunicationClient extends CommunicationClient {
}

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

public async getPairingRequestInfo(): Promise<P2PPairingRequest> {
return new P2PPairingRequest(
await generateGUID(),
this.name,
await this.getPublicKey(),
this.senderId,
BEACON_VERSION,
this.urls[Math.floor(Math.random() * this.urls.length)]
this.selectedNode
)
}

public async getPairingResponseInfo(request: P2PPairingRequest): Promise<P2PPairingResponse> {
const info: P2PPairingResponse = new P2PPairingResponse(
return new P2PPairingResponse(
request.id,
this.name,
await this.getPublicKey(),
this.senderId,
request.version,
request.relayServer
this.selectedNode
)

return info
}

public async sendPairingResponse(pairingRequest: P2PPairingRequest): Promise<void> {
// TODO add encryption
const message = JSON.stringify(await this.getPairingResponseInfo(pairingRequest))
const msg = [
'@channel-open',
this.client.idFromPublicKey(pairingRequest.publicKey),
message
].join(':')
this.sendRequest(
pairingRequest.publicKey,
JSON.stringify(await this.getPairingResponseInfo(pairingRequest))
)
}

this.sendMessage(msg, { publicKey: pairingRequest.publicKey } as any)
async sendRequest(senderId: string, message: string) {
this.client.send(senderId, message)
}

/**
* @deprecated The method should not be used. Use `sendRequest` instead.
*/
async sendMessage(message: string, { publicKey }: PeerInfoType): Promise<void> {
this.client.send(this.client.idFromPublicKey(publicKey), message)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-wallet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airgap/beacon-wallet",
"version": "4.2.6",
"version": "4.2.7",
"description": "Use this package in your wallet to instanciate a WalletClient object and communicate to dApps.",
"author": "Andreas Gassmann <[email protected]>",
"homepage": "https://walletbeacon.io",
Expand Down
17 changes: 11 additions & 6 deletions packages/beacon-wallet/src/client/WalletClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,25 @@ export class WalletClient extends Client {
.then((arrayBuffer) => Buffer.from(arrayBuffer))
])

const publicKeyCompressedSize = (publicKeyRaw.length - 1) / 2
const publicKeyCompressed = Buffer.concat([
new Uint8Array([publicKeyRaw[2 * publicKeyCompressedSize] % 2 ? 3 : 2]),
publicKeyRaw.subarray(1, publicKeyCompressedSize + 1)
])
const publicKeyHash = await crypto.subtle.digest('SHA-256', publicKeyCompressed)

return {
publicKey: publicKeyRaw,
secretKey: privateKeyRaw
secretKey: privateKeyRaw,
senderId: Buffer.from(publicKeyHash.slice(0, 16)).toString('hex')
}
}

public async init(): Promise<TransportType> {
const keyPair = await this.keyPair // We wait for keypair here so the P2P Transport creation is not delayed and causing issues
const { publicKey, secretKey, senderId } = await this.generateAcurastKeyPair()
const p2pTransport = this.enableWSP
? new WalletWebSocketP2PTransport(
this.name,
await this.generateAcurastKeyPair(),
this.storage
)
? new WalletWebSocketP2PTransport(this.name, { publicKey, secretKey }, senderId, this.storage)
: new WalletP2PTransport(
this.name,
keyPair,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export class WalletWebSocketP2PTransport extends WebSocketP2PTransport<
P2PPairingRequest,
StorageKey.TRANSPORT_LIBP2P_PEERS_WALLET
> {
constructor(name: string, keyPair: KeyPair, storage: Storage) {
super(name, keyPair, storage, StorageKey.TRANSPORT_LIBP2P_PEERS_WALLET)
constructor(name: string, keyPair: KeyPair, senderId: string, storage: Storage) {
super(name, keyPair, senderId, storage, StorageKey.TRANSPORT_LIBP2P_PEERS_WALLET)
}

public async addPeer(
Expand Down

0 comments on commit 468c23c

Please sign in to comment.