Skip to content

Commit

Permalink
Merge pull request #93 from windingtree/fix/pubsub
Browse files Browse the repository at this point in the history
fix: 🐛 pubsub message encoding
  • Loading branch information
kostysh authored Feb 15, 2024
2 parents 236955e + db89cf1 commit a2ec6c2
Show file tree
Hide file tree
Showing 3 changed files with 2,613 additions and 7,209 deletions.
1 change: 1 addition & 0 deletions packages/pubsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"@chainsafe/libp2p-gossipsub": "^10.1.0",
"@libp2p/interface": "^0.1.3",
"@libp2p/peer-id": "^4.0.6",
"@multiformats/multiaddr": "^12.1.3",
"@windingtree/sdk-constants": "workspace:*",
"@windingtree/sdk-logger": "workspace:*",
Expand Down
10 changes: 9 additions & 1 deletion packages/pubsub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { decodeText } from '@windingtree/sdk-utils';
import { CashedMessageEntry, MessagesCache } from './cache.js';
import { createLogger } from '@windingtree/sdk-logger';
import { parse } from 'superjson';
import { peerIdFromBytes } from '@libp2p/peer-id';

const logger = createLogger('CenterSub');

Expand Down Expand Up @@ -179,13 +180,20 @@ export class CenterSub extends GossipSub {
logger.trace('messageTransformer not defined');
return;
}

rpcMsg.from = new Uint8Array(rpcMsg.from as ArrayBufferLike);
rpcMsg.signature = new Uint8Array(rpcMsg.signature as ArrayBufferLike);
rpcMsg.key = new Uint8Array(rpcMsg.key as ArrayBufferLike);
rpcMsg.data = new Uint8Array(rpcMsg.data as ArrayBufferLike);
rpcMsg.seqno = new Uint8Array(rpcMsg.seqno as ArrayBufferLike);

const msgId = await sha256.encode(rpcMsg.data);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const msgIdStr = this['msgIdToStrFn'](msgId) as string;
const transformed = this.messageTransformer(rpcMsg.data);
await this.messages.set(
msgIdStr,
rpcMsg.from.toString(),
peerIdFromBytes(rpcMsg.from).toString(),
rpcMsg,
Number(transformed.expire),
Number(transformed.nonce),
Expand Down
Loading

0 comments on commit a2ec6c2

Please sign in to comment.