Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

fix(signatures): Add signature checks [fixes DXJ-488] #357

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Changes from all commits
Commits
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
61 changes: 32 additions & 29 deletions packages/core/js-client/src/connection/RelayConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@ export class RelayConnection implements IConnection {
},
connectionGater: {
// By default, this function forbids connections to private peers. For example multiaddr with ip 127.0.0.1 isn't allowed
denyDialMultiaddr: () => Promise.resolve(false)
denyDialMultiaddr: () => Promise.resolve(false),
},
services: {
identify: identifyService(),
ping: pingService()
}
ping: pingService(),
},
});

const supportedProtocols = (await this.lib2p2Peer.peerStore.get(this.lib2p2Peer.peerId)).protocols;
await this.lib2p2Peer.peerStore.patch(this.lib2p2Peer.peerId, {
protocols: [...supportedProtocols, PROTOCOL_NAME]
protocols: [...supportedProtocols, PROTOCOL_NAME],
});

await this.connect();
}

Expand Down Expand Up @@ -166,14 +166,10 @@ export class RelayConnection implements IConnection {
log.trace('created stream with id ', stream.id);
const sink = stream.sink;

await pipe(
[fromString(serializeToString(particle))],
encode(),
sink,
);
await pipe([fromString(serializeToString(particle))], encode(), sink);
log.trace('data written to sink');
}

private async processIncomingMessage(msg: string, stream: Stream) {
let particle: Particle | undefined;
try {
Expand All @@ -182,13 +178,19 @@ export class RelayConnection implements IConnection {
const initPeerId = peerIdFromString(particle.initPeerId);

if (initPeerId.publicKey === undefined) {
log.error('cannot retrieve public key from init_peer_id. particle id: %s. init_peer_id: %s', particle.id, particle.initPeerId);
log.error(
'cannot retrieve public key from init_peer_id. particle id: %s. init_peer_id: %s',
particle.id,
particle.initPeerId,
);
return;
}

// TODO: uncomment this after nox rolls out signature verification
// const isVerified = await KeyPair.verifyWithPublicKey(initPeerId.publicKey, buildParticleMessage(particle), particle.signature);
const isVerified = true;

const isVerified = await KeyPair.verifyWithPublicKey(
initPeerId.publicKey,
buildParticleMessage(particle),
particle.signature,
);
if (isVerified) {
this.particleSource.next(particle);
} else {
Expand All @@ -208,20 +210,21 @@ export class RelayConnection implements IConnection {

await this.lib2p2Peer.handle(
[PROTOCOL_NAME],
async ({ connection, stream }) => pipe(
stream.source,
decode(),
(source) => map(source, (buf) => toString(buf.subarray())),
async (source) => {
try {
for await (const msg of source) {
await this.processIncomingMessage(msg, stream);
async ({ connection, stream }) =>
pipe(
stream.source,
decode(),
(source) => map(source, (buf) => toString(buf.subarray())),
async (source) => {
try {
for await (const msg of source) {
await this.processIncomingMessage(msg, stream);
}
} catch (e) {
log.error('connection closed: %j', e);
}
} catch (e) {
log.error('connection closed: %j', e);
}
},
),
},
),
{
maxInboundStreams: this.config.maxInboundStreams,
maxOutboundStreams: this.config.maxOutboundStreams,
Expand Down