From b1e8b9d865795c1590c72a7b5f57d1399271b91a Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Fri, 28 Nov 2025 08:49:40 +0700 Subject: [PATCH 1/4] fix: synchronous msgIdFn --- package-lock.json | 172 ++----------------------------------------- package.json | 2 +- src/index.ts | 2 +- src/types.ts | 2 +- src/utils/msgIdFn.ts | 7 +- 5 files changed, 12 insertions(+), 173 deletions(-) diff --git a/package-lock.json b/package-lock.json index f77d1acd..535c784e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "14.1.2", "license": "Apache-2.0", "dependencies": { + "@chainsafe/as-sha256": "^1.2.0", "@libp2p/crypto": "^5.0.0", "@libp2p/interface": "^2.0.0", "@libp2p/interface-internal": "^2.0.0", @@ -25,7 +26,6 @@ "uint8arrays": "^5.0.1" }, "devDependencies": { - "@chainsafe/as-sha256": "^0.4.1", "@dapplion/benchmark": "^0.2.4", "@libp2p/floodsub": "^10.0.0", "@libp2p/interface-compliance-tests": "^6.0.0", @@ -1577,10 +1577,10 @@ "dev": true }, "node_modules/@chainsafe/as-sha256": { - "version": "0.4.2", - "resolved": "https://registry.npmjs.org/@chainsafe/as-sha256/-/as-sha256-0.4.2.tgz", - "integrity": "sha512-HJ8GZBRjLeWtRsAXf3EbNsNzmTGpzTFjfpSf4yHkLYC+E52DhT6hwz+7qpj6I/EmFzSUm5tYYvT9K8GZokLQCQ==", - "dev": true + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@chainsafe/as-sha256/-/as-sha256-1.2.0.tgz", + "integrity": "sha512-H2BNHQ5C3RS+H0ZvOdovK6GjFAyq5T6LClad8ivwj9Oaiy28uvdsGVS7gNJKuZmg0FGHAI+n7F0Qju6U0QkKDA==", + "license": "Apache-2.0" }, "node_modules/@chainsafe/is-ip": { "version": "2.0.2", @@ -14505,8 +14505,6 @@ }, "node_modules/npm/node_modules/@npmcli/agent": { "version": "2.2.2", - "resolved": "https://registry.npmjs.org/@npmcli/agent/-/agent-2.2.2.tgz", - "integrity": "sha512-OrcNPXdpSl9UX7qPVRWbmWMCSXrcDa2M9DvrbOTj7ao1S4PlqVFYv9/yLKMkrJKZ/V5A/kDBC690or307i26Og==", "dev": true, "inBundle": true, "license": "ISC", @@ -14523,8 +14521,6 @@ }, "node_modules/npm/node_modules/@npmcli/arborist": { "version": "7.5.4", - "resolved": "https://registry.npmjs.org/@npmcli/arborist/-/arborist-7.5.4.tgz", - "integrity": "sha512-nWtIc6QwwoUORCRNzKx4ypHqCk3drI+5aeYdMTQQiRCcn4lOOgfQh7WyZobGYTxXPSq1VwV53lkpN/BRlRk08g==", "dev": true, "inBundle": true, "license": "ISC", @@ -14574,8 +14570,6 @@ }, "node_modules/npm/node_modules/@npmcli/config": { "version": "8.3.4", - "resolved": "https://registry.npmjs.org/@npmcli/config/-/config-8.3.4.tgz", - "integrity": "sha512-01rtHedemDNhUXdicU7s+QYz/3JyV5Naj84cvdXGH4mgCdL+agmSYaLF4LUG4vMCLzhBO8YtS0gPpH1FGvbgAw==", "dev": true, "inBundle": true, "license": "ISC", @@ -14595,8 +14589,6 @@ }, "node_modules/npm/node_modules/@npmcli/fs": { "version": "3.1.1", - "resolved": "https://registry.npmjs.org/@npmcli/fs/-/fs-3.1.1.tgz", - "integrity": "sha512-q9CRWjpHCMIh5sVyefoD1cA7PkvILqCZsnSOEUUivORLjxCO/Irmue2DprETiNgEqktDBZaM1Bi+jrarx1XdCg==", "dev": true, "inBundle": true, "license": "ISC", @@ -14609,8 +14601,6 @@ }, "node_modules/npm/node_modules/@npmcli/git": { "version": "5.0.8", - "resolved": "https://registry.npmjs.org/@npmcli/git/-/git-5.0.8.tgz", - "integrity": "sha512-liASfw5cqhjNW9UFd+ruwwdEf/lbOAQjLL2XY2dFW/bkJheXDYZgOyul/4gVvEV4BWkTXjYGmDqMw9uegdbJNQ==", "dev": true, "inBundle": true, "license": "ISC", @@ -14631,8 +14621,6 @@ }, "node_modules/npm/node_modules/@npmcli/installed-package-contents": { "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@npmcli/installed-package-contents/-/installed-package-contents-2.1.0.tgz", - "integrity": "sha512-c8UuGLeZpm69BryRykLuKRyKFZYJsZSCT4aVY5ds4omyZqJ172ApzgfKJ5eV/r3HgLdUYgFVe54KSFVjKoe27w==", "dev": true, "inBundle": true, "license": "ISC", @@ -14649,8 +14637,6 @@ }, "node_modules/npm/node_modules/@npmcli/map-workspaces": { "version": "3.0.6", - "resolved": "https://registry.npmjs.org/@npmcli/map-workspaces/-/map-workspaces-3.0.6.tgz", - "integrity": "sha512-tkYs0OYnzQm6iIRdfy+LcLBjcKuQCeE5YLb8KnrIlutJfheNaPvPpgoFEyEFgbjzl5PLZ3IA/BWAwRU0eHuQDA==", "dev": true, "inBundle": true, "license": "ISC", @@ -14666,8 +14652,6 @@ }, "node_modules/npm/node_modules/@npmcli/metavuln-calculator": { "version": "7.1.1", - "resolved": "https://registry.npmjs.org/@npmcli/metavuln-calculator/-/metavuln-calculator-7.1.1.tgz", - "integrity": "sha512-Nkxf96V0lAx3HCpVda7Vw4P23RILgdi/5K1fmj2tZkWIYLpXAN8k2UVVOsW16TsS5F8Ws2I7Cm+PU1/rsVF47g==", "dev": true, "inBundle": true, "license": "ISC", @@ -14684,8 +14668,6 @@ }, "node_modules/npm/node_modules/@npmcli/name-from-folder": { "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@npmcli/name-from-folder/-/name-from-folder-2.0.0.tgz", - "integrity": "sha512-pwK+BfEBZJbKdNYpHHRTNBwBoqrN/iIMO0AiGvYsp3Hoaq0WbgGSWQR6SCldZovoDpY3yje5lkFUe6gsDgJ2vg==", "dev": true, "inBundle": true, "license": "ISC", @@ -14695,8 +14677,6 @@ }, "node_modules/npm/node_modules/@npmcli/node-gyp": { "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@npmcli/node-gyp/-/node-gyp-3.0.0.tgz", - "integrity": "sha512-gp8pRXC2oOxu0DUE1/M3bYtb1b3/DbJ5aM113+XJBgfXdussRAsX0YOrOhdd8WvnAR6auDBvJomGAkLKA5ydxA==", "dev": true, "inBundle": true, "license": "ISC", @@ -14706,8 +14686,6 @@ }, "node_modules/npm/node_modules/@npmcli/package-json": { "version": "5.2.0", - "resolved": "https://registry.npmjs.org/@npmcli/package-json/-/package-json-5.2.0.tgz", - "integrity": "sha512-qe/kiqqkW0AGtvBjL8TJKZk/eBBSpnJkUWvHdQ9jM2lKHXRYYJuyNpJPlJw3c8QjC2ow6NZYiLExhUaeJelbxQ==", "dev": true, "inBundle": true, "license": "ISC", @@ -14726,8 +14704,6 @@ }, "node_modules/npm/node_modules/@npmcli/promise-spawn": { "version": "7.0.2", - "resolved": "https://registry.npmjs.org/@npmcli/promise-spawn/-/promise-spawn-7.0.2.tgz", - "integrity": "sha512-xhfYPXoV5Dy4UkY0D+v2KkwvnDfiA/8Mt3sWCGI/hM03NsYIH8ZaG6QzS9x7pje5vHZBZJ2v6VRFVTWACnqcmQ==", "dev": true, "inBundle": true, "license": "ISC", @@ -14740,8 +14716,6 @@ }, "node_modules/npm/node_modules/@npmcli/query": { "version": "3.1.0", - "resolved": "https://registry.npmjs.org/@npmcli/query/-/query-3.1.0.tgz", - "integrity": "sha512-C/iR0tk7KSKGldibYIB9x8GtO/0Bd0I2mhOaDb8ucQL/bQVTmGoeREaFj64Z5+iCBRf3dQfed0CjJL7I8iTkiQ==", "dev": true, "inBundle": true, "license": "ISC", @@ -14754,8 +14728,6 @@ }, "node_modules/npm/node_modules/@npmcli/redact": { "version": "2.0.1", - "resolved": "https://registry.npmjs.org/@npmcli/redact/-/redact-2.0.1.tgz", - "integrity": "sha512-YgsR5jCQZhVmTJvjduTOIHph0L73pK8xwMVaDY0PatySqVM9AZj93jpoXYSJqfHFxFkN9dmqTw6OiqExsS3LPw==", "dev": true, "inBundle": true, "license": "ISC", @@ -14765,8 +14737,6 @@ }, "node_modules/npm/node_modules/@npmcli/run-script": { "version": "8.1.0", - "resolved": "https://registry.npmjs.org/@npmcli/run-script/-/run-script-8.1.0.tgz", - "integrity": "sha512-y7efHHwghQfk28G2z3tlZ67pLG0XdfYbcVG26r7YIXALRsrVQcTq4/tdenSmdOrEsNahIYA/eh8aEVROWGFUDg==", "dev": true, "inBundle": true, "license": "ISC", @@ -14796,8 +14766,6 @@ }, "node_modules/npm/node_modules/@sigstore/bundle": { "version": "2.3.2", - "resolved": "https://registry.npmjs.org/@sigstore/bundle/-/bundle-2.3.2.tgz", - "integrity": "sha512-wueKWDk70QixNLB363yHc2D2ItTgYiMTdPwK8D9dKQMR3ZQ0c35IxP5xnwQ8cNLoCgCRcHf14kE+CLIvNX1zmA==", "dev": true, "inBundle": true, "license": "Apache-2.0", @@ -14810,8 +14778,6 @@ }, "node_modules/npm/node_modules/@sigstore/core": { "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@sigstore/core/-/core-1.1.0.tgz", - "integrity": "sha512-JzBqdVIyqm2FRQCulY6nbQzMpJJpSiJ8XXWMhtOX9eKgaXXpfNOF53lzQEjIydlStnd/eFtuC1dW4VYdD93oRg==", "dev": true, "inBundle": true, "license": "Apache-2.0", @@ -14821,8 +14787,6 @@ }, "node_modules/npm/node_modules/@sigstore/protobuf-specs": { "version": "0.3.2", - "resolved": "https://registry.npmjs.org/@sigstore/protobuf-specs/-/protobuf-specs-0.3.2.tgz", - "integrity": "sha512-c6B0ehIWxMI8wiS/bj6rHMPqeFvngFV7cDU/MY+B16P9Z3Mp9k8L93eYZ7BYzSickzuqAQqAq0V956b3Ju6mLw==", "dev": true, "inBundle": true, "license": "Apache-2.0", @@ -14832,8 +14796,6 @@ }, "node_modules/npm/node_modules/@sigstore/sign": { "version": "2.3.2", - "resolved": "https://registry.npmjs.org/@sigstore/sign/-/sign-2.3.2.tgz", - "integrity": "sha512-5Vz5dPVuunIIvC5vBb0APwo7qKA4G9yM48kPWJT+OEERs40md5GoUR1yedwpekWZ4m0Hhw44m6zU+ObsON+iDA==", "dev": true, "inBundle": true, "license": "Apache-2.0", @@ -14851,8 +14813,6 @@ }, "node_modules/npm/node_modules/@sigstore/tuf": { "version": "2.3.4", - "resolved": "https://registry.npmjs.org/@sigstore/tuf/-/tuf-2.3.4.tgz", - "integrity": "sha512-44vtsveTPUpqhm9NCrbU8CWLe3Vck2HO1PNLw7RIajbB7xhtn5RBPm1VNSCMwqGYHhDsBJG8gDF0q4lgydsJvw==", "dev": true, "inBundle": true, "license": "Apache-2.0", @@ -14866,8 +14826,6 @@ }, "node_modules/npm/node_modules/@sigstore/verify": { "version": "1.2.1", - "resolved": "https://registry.npmjs.org/@sigstore/verify/-/verify-1.2.1.tgz", - "integrity": "sha512-8iKx79/F73DKbGfRf7+t4dqrc0bRr0thdPrxAtCKWRm/F0tG71i6O1rvlnScncJLLBZHn3h8M3c1BSUAb9yu8g==", "dev": true, "inBundle": true, "license": "Apache-2.0", @@ -14893,8 +14851,6 @@ }, "node_modules/npm/node_modules/@tufjs/models": { "version": "2.0.1", - "resolved": "https://registry.npmjs.org/@tufjs/models/-/models-2.0.1.tgz", - "integrity": "sha512-92F7/SFyufn4DXsha9+QfKnN03JGqtMFMXgSHbZOo8JG59WkTni7UzAouNQDf7AuP9OAMxVOPQcqG3sB7w+kkg==", "dev": true, "inBundle": true, "license": "MIT", @@ -14908,8 +14864,6 @@ }, "node_modules/npm/node_modules/abbrev": { "version": "2.0.0", - "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-2.0.0.tgz", - "integrity": "sha512-6/mh1E2u2YgEsCHdY0Yx5oW+61gZU+1vXaoiHHrpKeuRNNgFvS+/jrwHiQhB5apAf5oB7UB7E19ol2R2LKH8hQ==", "dev": true, "inBundle": true, "license": "ISC", @@ -14997,8 +14951,6 @@ }, "node_modules/npm/node_modules/bin-links": { "version": "4.0.4", - "resolved": "https://registry.npmjs.org/bin-links/-/bin-links-4.0.4.tgz", - "integrity": "sha512-cMtq4W5ZsEwcutJrVId+a/tjt8GSbS+h0oNkdl6+6rBuEv8Ot33Bevj5KPm40t309zuhVic8NjpuL42QCiJWWA==", "dev": true, "inBundle": true, "license": "ISC", @@ -15039,8 +14991,6 @@ }, "node_modules/npm/node_modules/cacache": { "version": "18.0.4", - "resolved": "https://registry.npmjs.org/cacache/-/cacache-18.0.4.tgz", - "integrity": "sha512-B+L5iIa9mgcjLbliir2th36yEwPftrzteHYujzsx3dFP/31GCHcIeS8f5MGd80odLOjaOvSpU3EEAmRQptkxLQ==", "dev": true, "inBundle": true, "license": "ISC", @@ -15089,8 +15039,6 @@ }, "node_modules/npm/node_modules/ci-info": { "version": "4.0.0", - "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-4.0.0.tgz", - "integrity": "sha512-TdHqgGf9odd8SXNuxtUBVx8Nv+qZOejE6qyqiy5NtbYYQOeFa6zmHkxlPzmaLxWWHsU6nJmB7AETdVPi+2NBUg==", "dev": true, "funding": [ { @@ -15106,8 +15054,6 @@ }, "node_modules/npm/node_modules/cidr-regex": { "version": "4.1.1", - "resolved": "https://registry.npmjs.org/cidr-regex/-/cidr-regex-4.1.1.tgz", - "integrity": "sha512-ekKcVp+iRB9zlKFXyx7io7nINgb0oRjgRdXNEodp1OuxRui8FXr/CA40Tz1voWUp9DPPrMyQKy01vJhDo4N1lw==", "dev": true, "inBundle": true, "license": "BSD-2-Clause", @@ -15146,8 +15092,6 @@ }, "node_modules/npm/node_modules/cmd-shim": { "version": "6.0.3", - "resolved": "https://registry.npmjs.org/cmd-shim/-/cmd-shim-6.0.3.tgz", - "integrity": "sha512-FMabTRlc5t5zjdenF6mS0MBeFZm0XqHqeOkcskKFb/LYCcRQ5fVgLOHVc4Lq9CqABd9zhjwPjMBCJvMCziSVtA==", "dev": true, "inBundle": true, "license": "ISC", @@ -15313,8 +15257,6 @@ }, "node_modules/npm/node_modules/exponential-backoff": { "version": "3.1.1", - "resolved": "https://registry.npmjs.org/exponential-backoff/-/exponential-backoff-3.1.1.tgz", - "integrity": "sha512-dX7e/LHVJ6W3DE1MHWi9S1EYzDESENfLrYohG2G++ovZrYOkm4Knwa0mc1cn84xJOR4KEU0WSchhLbd0UklbHw==", "dev": true, "inBundle": true, "license": "Apache-2.0" @@ -15431,8 +15373,6 @@ }, "node_modules/npm/node_modules/https-proxy-agent": { "version": "7.0.5", - "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.5.tgz", - "integrity": "sha512-1e4Wqeblerz+tMKPIq2EMGiiWW1dIjZOksyHWSUm1rmuvw/how9hBHZ38lAGj5ID4Ik6EdkOw7NmWPy6LAwalw==", "dev": true, "inBundle": true, "license": "MIT", @@ -15461,8 +15401,6 @@ }, "node_modules/npm/node_modules/ignore-walk": { "version": "6.0.5", - "resolved": "https://registry.npmjs.org/ignore-walk/-/ignore-walk-6.0.5.tgz", - "integrity": "sha512-VuuG0wCnjhnylG1ABXT3dAuIpTNDs/G8jlpmwXY03fXoXy/8ZK8/T+hMzt8L4WnrLCJgdybqgPagnF/f97cg3A==", "dev": true, "inBundle": true, "license": "ISC", @@ -15497,8 +15435,6 @@ }, "node_modules/npm/node_modules/ini": { "version": "4.1.3", - "resolved": "https://registry.npmjs.org/ini/-/ini-4.1.3.tgz", - "integrity": "sha512-X7rqawQBvfdjS10YU1y1YVreA3SsLrW9dX2CewP2EbBJM4ypVNLDkO5y04gejPwKIY9lR+7r9gn3rFPt/kmWFg==", "dev": true, "inBundle": true, "license": "ISC", @@ -15508,8 +15444,6 @@ }, "node_modules/npm/node_modules/init-package-json": { "version": "6.0.3", - "resolved": "https://registry.npmjs.org/init-package-json/-/init-package-json-6.0.3.tgz", - "integrity": "sha512-Zfeb5ol+H+eqJWHTaGca9BovufyGeIfr4zaaBorPmJBMrJ+KBnN+kQx2ZtXdsotUTgldHmHQV44xvUWOUA7E2w==", "dev": true, "inBundle": true, "license": "ISC", @@ -15557,8 +15491,6 @@ }, "node_modules/npm/node_modules/is-cidr": { "version": "5.1.0", - "resolved": "https://registry.npmjs.org/is-cidr/-/is-cidr-5.1.0.tgz", - "integrity": "sha512-OkVS+Ht2ssF27d48gZdB+ho1yND1VbkJRKKS6Pc1/Cw7uqkd9IOJg8/bTwBDQL6tfBhSdguPRnlGiE8pU/X5NQ==", "dev": true, "inBundle": true, "license": "BSD-2-Clause", @@ -15582,8 +15514,6 @@ }, "node_modules/npm/node_modules/is-lambda": { "version": "1.0.1", - "resolved": "https://registry.npmjs.org/is-lambda/-/is-lambda-1.0.1.tgz", - "integrity": "sha512-z7CMFGNrENq5iFB9Bqo64Xk6Y9sg+epq1myIcdHaGnbMTYOxvzsEtdYqQUylB7LxfkvgrrjP32T6Ywciio9UIQ==", "dev": true, "inBundle": true, "license": "MIT" @@ -15623,8 +15553,6 @@ }, "node_modules/npm/node_modules/json-parse-even-better-errors": { "version": "3.0.2", - "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-3.0.2.tgz", - "integrity": "sha512-fi0NG4bPjCHunUJffmLd0gxssIgkNmArMvis4iNah6Owg1MCJjWhEcDLmsK6iGkJq3tHwbDkTlce70/tmXN4cQ==", "dev": true, "inBundle": true, "license": "MIT", @@ -15672,8 +15600,6 @@ }, "node_modules/npm/node_modules/libnpmaccess": { "version": "8.0.6", - "resolved": "https://registry.npmjs.org/libnpmaccess/-/libnpmaccess-8.0.6.tgz", - "integrity": "sha512-uM8DHDEfYG6G5gVivVl+yQd4pH3uRclHC59lzIbSvy7b5FEwR+mU49Zq1jEyRtRFv7+M99mUW9S0wL/4laT4lw==", "dev": true, "inBundle": true, "license": "ISC", @@ -15687,8 +15613,6 @@ }, "node_modules/npm/node_modules/libnpmdiff": { "version": "6.1.4", - "resolved": "https://registry.npmjs.org/libnpmdiff/-/libnpmdiff-6.1.4.tgz", - "integrity": "sha512-KCNoCY8kjQ16/EE4VoW7AYqecsb9frNIh/cPwWQSk1s2grzZMQH+Scp7Yo7Fk6SWTkyVDLZEYfUT/vKONYrfmg==", "dev": true, "inBundle": true, "license": "ISC", @@ -15708,8 +15632,6 @@ }, "node_modules/npm/node_modules/libnpmexec": { "version": "8.1.4", - "resolved": "https://registry.npmjs.org/libnpmexec/-/libnpmexec-8.1.4.tgz", - "integrity": "sha512-/5mQ8d7PJT3Ok1iTfXVZaev4mppno9IbimHv7x1g8ZLIQtRE0SWrdFN7SB43vzxexdw0bJ3LKu2OsY6L3h60Kg==", "dev": true, "inBundle": true, "license": "ISC", @@ -15731,8 +15653,6 @@ }, "node_modules/npm/node_modules/libnpmfund": { "version": "5.0.12", - "resolved": "https://registry.npmjs.org/libnpmfund/-/libnpmfund-5.0.12.tgz", - "integrity": "sha512-lox1UHcv8/r/TE+T9B+aOylU3c13tK2IuwwUwUm+YMw+C/iq14dqskHqhGPTqa75ZJbiVOW7PMWO92Wn5HG49Q==", "dev": true, "inBundle": true, "license": "ISC", @@ -15745,8 +15665,6 @@ }, "node_modules/npm/node_modules/libnpmhook": { "version": "10.0.5", - "resolved": "https://registry.npmjs.org/libnpmhook/-/libnpmhook-10.0.5.tgz", - "integrity": "sha512-XulT+N/s3o9oFlIq6pGRv3OG2qR1NVRbVQOKLchycDwyf16RZA3oXbeEgs2H3oE7hRZPUMBZqsalQXMMPal3cQ==", "dev": true, "inBundle": true, "license": "ISC", @@ -15760,8 +15678,6 @@ }, "node_modules/npm/node_modules/libnpmorg": { "version": "6.0.6", - "resolved": "https://registry.npmjs.org/libnpmorg/-/libnpmorg-6.0.6.tgz", - "integrity": "sha512-4MVxsAS4H2z7su/sU0GsrirfBm4ssfqPRSDvoZ8qmRw58kEWJ0qE0cQ2VilRlFgCWKzKPhfoPeyNPyxBTnOusA==", "dev": true, "inBundle": true, "license": "ISC", @@ -15775,8 +15691,6 @@ }, "node_modules/npm/node_modules/libnpmpack": { "version": "7.0.4", - "resolved": "https://registry.npmjs.org/libnpmpack/-/libnpmpack-7.0.4.tgz", - "integrity": "sha512-oKZA0afbueiC88lskXzAEr3DCN9BTMbUySjUce6qhBV9CjYF2R/x347KhgHu75+p9W2Rd57ZvKz81c5a2+9h6Q==", "dev": true, "inBundle": true, "license": "ISC", @@ -15792,8 +15706,6 @@ }, "node_modules/npm/node_modules/libnpmpublish": { "version": "9.0.9", - "resolved": "https://registry.npmjs.org/libnpmpublish/-/libnpmpublish-9.0.9.tgz", - "integrity": "sha512-26zzwoBNAvX9AWOPiqqF6FG4HrSCPsHFkQm7nT+xU1ggAujL/eae81RnCv4CJ2In9q9fh10B88sYSzKCUh/Ghg==", "dev": true, "inBundle": true, "license": "ISC", @@ -15813,8 +15725,6 @@ }, "node_modules/npm/node_modules/libnpmsearch": { "version": "7.0.6", - "resolved": "https://registry.npmjs.org/libnpmsearch/-/libnpmsearch-7.0.6.tgz", - "integrity": "sha512-PmiER4bgiIqN9OjBtgPn2/PxwU+OdJWtLBFM+vewOrn4VmaNAHSUKDt/wxOOkZSDLyMICVUBp61Ji1+XxhSrKw==", "dev": true, "inBundle": true, "license": "ISC", @@ -15827,8 +15737,6 @@ }, "node_modules/npm/node_modules/libnpmteam": { "version": "6.0.5", - "resolved": "https://registry.npmjs.org/libnpmteam/-/libnpmteam-6.0.5.tgz", - "integrity": "sha512-iJW4Cq42GMqMwZEV+Mx8ZLj0Np5kGXQ9P/BAekHjIpYC1v3/vJqbmfJkzkwFvGxEhUotmx+xpLChZCDJ7c3rxA==", "dev": true, "inBundle": true, "license": "ISC", @@ -15842,8 +15750,6 @@ }, "node_modules/npm/node_modules/libnpmversion": { "version": "6.0.3", - "resolved": "https://registry.npmjs.org/libnpmversion/-/libnpmversion-6.0.3.tgz", - "integrity": "sha512-Kjk1anQ9sPn7E/qF1jXumItvr2OA1914tYWkSTXH9G2rYoY+Ol1+KNrWfGeje2aBvFfKlt4VeKdCfM3yxMXNBw==", "dev": true, "inBundle": true, "license": "ISC", @@ -15868,8 +15774,6 @@ }, "node_modules/npm/node_modules/make-fetch-happen": { "version": "13.0.1", - "resolved": "https://registry.npmjs.org/make-fetch-happen/-/make-fetch-happen-13.0.1.tgz", - "integrity": "sha512-cKTUFc/rbKUd/9meOvgrpJ2WrNzymt6jfRDdwg5UCnVzv9dTpEj9JS5m3wtziXVCjluIXyL8pcaukYqezIzZQA==", "dev": true, "inBundle": true, "license": "ISC", @@ -15935,8 +15839,6 @@ }, "node_modules/npm/node_modules/minipass-fetch": { "version": "3.0.5", - "resolved": "https://registry.npmjs.org/minipass-fetch/-/minipass-fetch-3.0.5.tgz", - "integrity": "sha512-2N8elDQAtSnFV0Dk7gt15KHsS0Fyz6CbYZ360h0WTYV1Ty46li3rAXVOQj1THMNLdmrD9Vt5pBPtWtVkpwGBqg==", "dev": true, "inBundle": true, "license": "MIT", @@ -16081,8 +15983,6 @@ }, "node_modules/npm/node_modules/mute-stream": { "version": "1.0.0", - "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-1.0.0.tgz", - "integrity": "sha512-avsJQhyd+680gKXyG/sQc0nXaC6rBkPOfyHYcFb9+hdkqQkR9bdnkJ0AMZhke0oesPqIO+mFFJ+IdBc7mst4IA==", "dev": true, "inBundle": true, "license": "ISC", @@ -16092,8 +15992,6 @@ }, "node_modules/npm/node_modules/negotiator": { "version": "0.6.3", - "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz", - "integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==", "dev": true, "inBundle": true, "license": "MIT", @@ -16103,8 +16001,6 @@ }, "node_modules/npm/node_modules/node-gyp": { "version": "10.2.0", - "resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-10.2.0.tgz", - "integrity": "sha512-sp3FonBAaFe4aYTcFdZUn2NYkbP7xroPGYvQmP4Nl5PxamznItBnNCgjrVTKrEfQynInMsJvZrdmqUnysCJ8rw==", "dev": true, "inBundle": true, "license": "MIT", @@ -16129,8 +16025,6 @@ }, "node_modules/npm/node_modules/nopt": { "version": "7.2.1", - "resolved": "https://registry.npmjs.org/nopt/-/nopt-7.2.1.tgz", - "integrity": "sha512-taM24ViiimT/XntxbPyJQzCG+p4EKOpgD3mxFwW38mGjVUrfERQOeY4EDHjdnptttfHuHQXFx+lTP08Q+mLa/w==", "dev": true, "inBundle": true, "license": "ISC", @@ -16162,8 +16056,6 @@ }, "node_modules/npm/node_modules/npm-audit-report": { "version": "5.0.0", - "resolved": "https://registry.npmjs.org/npm-audit-report/-/npm-audit-report-5.0.0.tgz", - "integrity": "sha512-EkXrzat7zERmUhHaoren1YhTxFwsOu5jypE84k6632SXTHcQE1z8V51GC6GVZt8LxkC+tbBcKMUBZAgk8SUSbw==", "dev": true, "inBundle": true, "license": "ISC", @@ -16173,8 +16065,6 @@ }, "node_modules/npm/node_modules/npm-bundled": { "version": "3.0.1", - "resolved": "https://registry.npmjs.org/npm-bundled/-/npm-bundled-3.0.1.tgz", - "integrity": "sha512-+AvaheE/ww1JEwRHOrn4WHNzOxGtVp+adrg2AeZS/7KuxGUYFuBta98wYpfHBbJp6Tg6j1NKSEVHNcfZzJHQwQ==", "dev": true, "inBundle": true, "license": "ISC", @@ -16187,8 +16077,6 @@ }, "node_modules/npm/node_modules/npm-install-checks": { "version": "6.3.0", - "resolved": "https://registry.npmjs.org/npm-install-checks/-/npm-install-checks-6.3.0.tgz", - "integrity": "sha512-W29RiK/xtpCGqn6f3ixfRYGk+zRyr+Ew9F2E20BfXxT5/euLdA/Nm7fO7OeTGuAmTs30cpgInyJ0cYe708YTZw==", "dev": true, "inBundle": true, "license": "BSD-2-Clause", @@ -16201,8 +16089,6 @@ }, "node_modules/npm/node_modules/npm-normalize-package-bin": { "version": "3.0.1", - "resolved": "https://registry.npmjs.org/npm-normalize-package-bin/-/npm-normalize-package-bin-3.0.1.tgz", - "integrity": "sha512-dMxCf+zZ+3zeQZXKxmyuCKlIDPGuv8EF940xbkC4kQVDTtqoh6rJFO+JTKSA6/Rwi0getWmtuy4Itup0AMcaDQ==", "dev": true, "inBundle": true, "license": "ISC", @@ -16212,8 +16098,6 @@ }, "node_modules/npm/node_modules/npm-package-arg": { "version": "11.0.3", - "resolved": "https://registry.npmjs.org/npm-package-arg/-/npm-package-arg-11.0.3.tgz", - "integrity": "sha512-sHGJy8sOC1YraBywpzQlIKBE4pBbGbiF95U6Auspzyem956E0+FtDtsx1ZxlOJkQCZ1AFXAY/yuvtFYrOxF+Bw==", "dev": true, "inBundle": true, "license": "ISC", @@ -16229,8 +16113,6 @@ }, "node_modules/npm/node_modules/npm-packlist": { "version": "8.0.2", - "resolved": "https://registry.npmjs.org/npm-packlist/-/npm-packlist-8.0.2.tgz", - "integrity": "sha512-shYrPFIS/JLP4oQmAwDyk5HcyysKW8/JLTEA32S0Z5TzvpaeeX2yMFfoK1fjEBnCBvVyIB/Jj/GBFdm0wsgzbA==", "dev": true, "inBundle": true, "license": "ISC", @@ -16243,8 +16125,6 @@ }, "node_modules/npm/node_modules/npm-pick-manifest": { "version": "9.1.0", - "resolved": "https://registry.npmjs.org/npm-pick-manifest/-/npm-pick-manifest-9.1.0.tgz", - "integrity": "sha512-nkc+3pIIhqHVQr085X9d2JzPzLyjzQS96zbruppqC9aZRm/x8xx6xhI98gHtsfELP2bE+loHq8ZaHFHhe+NauA==", "dev": true, "inBundle": true, "license": "ISC", @@ -16260,8 +16140,6 @@ }, "node_modules/npm/node_modules/npm-profile": { "version": "10.0.0", - "resolved": "https://registry.npmjs.org/npm-profile/-/npm-profile-10.0.0.tgz", - "integrity": "sha512-DXnge3nHYnEnPxmVd/kPmgcXKXwVUqFihGnU+EJUiu5mIOs3awq6zEm0rRp3kSQNhFsoqdLu8L1TIfRyeBOCog==", "dev": true, "inBundle": true, "license": "ISC", @@ -16275,8 +16153,6 @@ }, "node_modules/npm/node_modules/npm-registry-fetch": { "version": "17.1.0", - "resolved": "https://registry.npmjs.org/npm-registry-fetch/-/npm-registry-fetch-17.1.0.tgz", - "integrity": "sha512-5+bKQRH0J1xG1uZ1zMNvxW0VEyoNWgJpY9UDuluPFLKDfJ9u2JmmjmTJV1srBGQOROfdBMiVvnH2Zvpbm+xkVA==", "dev": true, "inBundle": true, "license": "ISC", @@ -16296,8 +16172,6 @@ }, "node_modules/npm/node_modules/npm-user-validate": { "version": "2.0.1", - "resolved": "https://registry.npmjs.org/npm-user-validate/-/npm-user-validate-2.0.1.tgz", - "integrity": "sha512-d17PKaF2h8LSGFl5j4b1gHOJt1fgH7YUcCm1kNSJvaLWWKXlBsuUvx0bBEkr0qhsVA9XP5LtRZ83hdlhm2QkgA==", "dev": true, "inBundle": true, "license": "BSD-2-Clause", @@ -16332,8 +16206,6 @@ }, "node_modules/npm/node_modules/pacote": { "version": "18.0.6", - "resolved": "https://registry.npmjs.org/pacote/-/pacote-18.0.6.tgz", - "integrity": "sha512-+eK3G27SMwsB8kLIuj4h1FUhHtwiEUo21Tw8wNjmvdlpOEr613edv+8FUsTj/4F/VN5ywGE19X18N7CC2EJk6A==", "dev": true, "inBundle": true, "license": "ISC", @@ -16365,8 +16237,6 @@ }, "node_modules/npm/node_modules/parse-conflict-json": { "version": "3.0.1", - "resolved": "https://registry.npmjs.org/parse-conflict-json/-/parse-conflict-json-3.0.1.tgz", - "integrity": "sha512-01TvEktc68vwbJOtWZluyWeVGWjP+bZwXtPDMQVbBKzbJ/vZBif0L69KH1+cHv1SZ6e0FKLvjyHe8mqsIqYOmw==", "dev": true, "inBundle": true, "license": "ISC", @@ -16410,8 +16280,6 @@ }, "node_modules/npm/node_modules/postcss-selector-parser": { "version": "6.1.2", - "resolved": "https://registry.npmjs.org/postcss-selector-parser/-/postcss-selector-parser-6.1.2.tgz", - "integrity": "sha512-Q8qQfPiZ+THO/3ZrOrO0cJJKfpYCagtMUkXbnEfmgUjwXg6z/WBeOyS9APBBPCTSiDV+s4SwQGu8yFsiMRIudg==", "dev": true, "inBundle": true, "license": "MIT", @@ -16425,8 +16293,6 @@ }, "node_modules/npm/node_modules/proc-log": { "version": "4.2.0", - "resolved": "https://registry.npmjs.org/proc-log/-/proc-log-4.2.0.tgz", - "integrity": "sha512-g8+OnU/L2v+wyiVK+D5fA34J7EH8jZ8DDlvwhRCMxmMj7UCBvxiO1mGeN+36JXIKF4zevU4kRBd8lVgG9vLelA==", "dev": true, "inBundle": true, "license": "ISC", @@ -16436,8 +16302,6 @@ }, "node_modules/npm/node_modules/proggy": { "version": "2.0.0", - "resolved": "https://registry.npmjs.org/proggy/-/proggy-2.0.0.tgz", - "integrity": "sha512-69agxLtnI8xBs9gUGqEnK26UfiexpHy+KUpBQWabiytQjnn5wFY8rklAi7GRfABIuPNnQ/ik48+LGLkYYJcy4A==", "dev": true, "inBundle": true, "license": "ISC", @@ -16467,8 +16331,6 @@ }, "node_modules/npm/node_modules/promise-inflight": { "version": "1.0.1", - "resolved": "https://registry.npmjs.org/promise-inflight/-/promise-inflight-1.0.1.tgz", - "integrity": "sha512-6zWPyEOFaQBJYcGMHBKTKJ3u6TBsnMFOIZSa6ce1e/ZrrsOlnHRHbabMjLiBYKp+n44X9eUI6VUPaukCXHuG4g==", "dev": true, "inBundle": true, "license": "ISC" @@ -16490,8 +16352,6 @@ }, "node_modules/npm/node_modules/promzard": { "version": "1.0.2", - "resolved": "https://registry.npmjs.org/promzard/-/promzard-1.0.2.tgz", - "integrity": "sha512-2FPputGL+mP3jJ3UZg/Dl9YOkovB7DX0oOr+ck5QbZ5MtORtds8k/BZdn+02peDLI8/YWbmzx34k5fA+fHvCVQ==", "dev": true, "inBundle": true, "license": "ISC", @@ -16514,8 +16374,6 @@ }, "node_modules/npm/node_modules/read": { "version": "3.0.1", - "resolved": "https://registry.npmjs.org/read/-/read-3.0.1.tgz", - "integrity": "sha512-SLBrDU/Srs/9EoWhU5GdbAoxG1GzpQHo/6qiGItaoLJ1thmYpcNIM1qISEUvyHBzfGlWIyd6p2DNi1oV1VmAuw==", "dev": true, "inBundle": true, "license": "ISC", @@ -16528,8 +16386,6 @@ }, "node_modules/npm/node_modules/read-cmd-shim": { "version": "4.0.0", - "resolved": "https://registry.npmjs.org/read-cmd-shim/-/read-cmd-shim-4.0.0.tgz", - "integrity": "sha512-yILWifhaSEEytfXI76kB9xEEiG1AiozaCJZ83A87ytjRiN+jVibXjedjCRNjoZviinhG+4UkalO3mWTd8u5O0Q==", "dev": true, "inBundle": true, "license": "ISC", @@ -16539,8 +16395,6 @@ }, "node_modules/npm/node_modules/read-package-json-fast": { "version": "3.0.2", - "resolved": "https://registry.npmjs.org/read-package-json-fast/-/read-package-json-fast-3.0.2.tgz", - "integrity": "sha512-0J+Msgym3vrLOUB3hzQCuZHII0xkNGCtz/HJH9xZshwv9DbDwkw1KaE3gx/e2J5rpEY5rtOy6cyhKOPrkP7FZw==", "dev": true, "inBundle": true, "license": "ISC", @@ -16627,8 +16481,6 @@ }, "node_modules/npm/node_modules/sigstore": { "version": "2.3.1", - "resolved": "https://registry.npmjs.org/sigstore/-/sigstore-2.3.1.tgz", - "integrity": "sha512-8G+/XDU8wNsJOQS5ysDVO0Etg9/2uA5gR9l4ZwijjlwxBcrU6RPfwi2+jJmbP+Ap1Hlp/nVAaEO4Fj22/SL2gQ==", "dev": true, "inBundle": true, "license": "Apache-2.0", @@ -16658,8 +16510,6 @@ }, "node_modules/npm/node_modules/socks": { "version": "2.8.3", - "resolved": "https://registry.npmjs.org/socks/-/socks-2.8.3.tgz", - "integrity": "sha512-l5x7VUUWbjVFbafGLxPWkYsHIhEvmF85tbIeFZWc8ZPtoMyybuEhL7Jye/ooC4/d48FgOjSJXgsF/AJPYCW8Zw==", "dev": true, "inBundle": true, "license": "MIT", @@ -16674,8 +16524,6 @@ }, "node_modules/npm/node_modules/socks-proxy-agent": { "version": "8.0.4", - "resolved": "https://registry.npmjs.org/socks-proxy-agent/-/socks-proxy-agent-8.0.4.tgz", - "integrity": "sha512-GNAq/eg8Udq2x0eNiFkr9gRg5bA7PXEWagQdeRX4cPSG+X/8V38v637gim9bjFptMk1QWsCTr0ttrJEiXbNnRw==", "dev": true, "inBundle": true, "license": "MIT", @@ -16746,8 +16594,6 @@ }, "node_modules/npm/node_modules/ssri": { "version": "10.0.6", - "resolved": "https://registry.npmjs.org/ssri/-/ssri-10.0.6.tgz", - "integrity": "sha512-MGrFH9Z4NP9Iyhqn16sDtBpRRNJ0Y2hNa6D65h736fVSaPCHr4DM4sWUNvVaSuC+0OBGhwsrydQwmgfg5LncqQ==", "dev": true, "inBundle": true, "license": "ISC", @@ -16915,8 +16761,6 @@ }, "node_modules/npm/node_modules/tuf-js": { "version": "2.2.1", - "resolved": "https://registry.npmjs.org/tuf-js/-/tuf-js-2.2.1.tgz", - "integrity": "sha512-GwIJau9XaA8nLVbUXsN3IlFi7WmQ48gBUrl3FTkkL/XLu/POhBzfmX9hd33FNMX1qAsfl6ozO1iMmW9NC8YniA==", "dev": true, "inBundle": true, "license": "MIT", @@ -16931,8 +16775,6 @@ }, "node_modules/npm/node_modules/unique-filename": { "version": "3.0.0", - "resolved": "https://registry.npmjs.org/unique-filename/-/unique-filename-3.0.0.tgz", - "integrity": "sha512-afXhuC55wkAmZ0P18QsVE6kp8JaxrEokN2HGIoIVv2ijHQd419H0+6EigAFcIzXeMIkcIkNBpB3L/DXB3cTS/g==", "dev": true, "inBundle": true, "license": "ISC", @@ -16945,8 +16787,6 @@ }, "node_modules/npm/node_modules/unique-slug": { "version": "4.0.0", - "resolved": "https://registry.npmjs.org/unique-slug/-/unique-slug-4.0.0.tgz", - "integrity": "sha512-WrcA6AyEfqDX5bWige/4NQfPZMtASNVxdmWR76WESYQVAACSgWcR6e9i0mofqqBxYFtL4oAxPIptY73/0YE1DQ==", "dev": true, "inBundle": true, "license": "ISC", @@ -17142,8 +16982,6 @@ }, "node_modules/npm/node_modules/write-file-atomic": { "version": "5.0.1", - "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-5.0.1.tgz", - "integrity": "sha512-+QU2zd6OTD8XWIJCbffaiQeH9U73qIqafo1x6V1snCWYGJf6cVE0cDR4D8xRzcEnfI21IFrUPzPGtcPf8AC+Rw==", "dev": true, "inBundle": true, "license": "ISC", diff --git a/package.json b/package.json index 3136726d..6378e268 100644 --- a/package.json +++ b/package.json @@ -84,12 +84,12 @@ "it-pipe": "^3.0.1", "it-pushable": "^3.2.3", "multiformats": "^13.0.1", + "@chainsafe/as-sha256": "^1.2.0", "protons-runtime": "^5.5.0", "uint8arraylist": "^2.4.8", "uint8arrays": "^5.0.1" }, "devDependencies": { - "@chainsafe/as-sha256": "^0.4.1", "@dapplion/benchmark": "^0.2.4", "@libp2p/floodsub": "^10.0.0", "@libp2p/interface-compliance-tests": "^6.0.0", diff --git a/src/index.ts b/src/index.ts index fc89ff7a..8befa611 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1330,7 +1330,7 @@ export class GossipSub extends TypedEventEmitter implements Pub // - reject messages claiming to be from ourselves but not locally published // Calculate the message id on the transformed data. - const msgId = await this.msgIdFn(msg) + const msgId = this.msgIdFn(msg) const msgIdStr = this.msgIdToStrFn(msgId) const messageId = { msgId, msgIdStr } diff --git a/src/types.ts b/src/types.ts index 8455eb4c..41b624b9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -28,7 +28,7 @@ export interface MsgIdToStrFn { (msgId: Uint8Array): string } * Compute spec'ed msg-id. Used for IHAVE / IWANT messages */ export interface MsgIdFn { - (msg: Message): Promise | Uint8Array + (msg: Message): Uint8Array } export interface DataTransform { diff --git a/src/utils/msgIdFn.ts b/src/utils/msgIdFn.ts index 510378de..72e0724a 100644 --- a/src/utils/msgIdFn.ts +++ b/src/utils/msgIdFn.ts @@ -1,5 +1,6 @@ import { msgId } from '@libp2p/pubsub/utils' -import { sha256 } from 'multiformats/hashes/sha2' +import { digest } from '@chainsafe/as-sha256' + import type { Message } from '@libp2p/interface' /** @@ -19,6 +20,6 @@ export function msgIdFnStrictSign (msg: Message): Uint8Array { /** * Generate a message id, based on message `data` */ -export async function msgIdFnStrictNoSign (msg: Message): Promise { - return sha256.encode(msg.data) +export function msgIdFnStrictNoSign (msg: Message): Uint8Array { + return digest(msg.data) } From 95da5e35ecf6430fc6530fa5cb6fb9cb7d4eac9d Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Fri, 28 Nov 2025 08:58:46 +0700 Subject: [PATCH 2/4] fix: move async topic validation outside of validateReceivedMessage() --- src/index.ts | 49 +++++++++++++++++++++++--------------------- src/utils/msgIdFn.ts | 3 +-- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8befa611..fd47c97b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1214,7 +1214,31 @@ export class GossipSub extends TypedEventEmitter implements Pub private async handleReceivedMessage (from: PeerId, rpcMsg: RPC.Message): Promise { this.metrics?.onMsgRecvPreValidation(rpcMsg.topic) - const validationResult = await this.validateReceivedMessage(from, rpcMsg) + let validationResult = await this.validateReceivedMessage(from, rpcMsg) + + if (validationResult.code === MessageStatus.valid) { + // (Optional) Provide custom validation here with dynamic validators per topic + // NOTE: This custom topicValidator() must resolve fast (< 100ms) to allow scores + // to not penalize peers for long validation times. + const msgIdStr = validationResult.messageId.msgIdStr + const topicValidator = this.topicValidators.get(rpcMsg.topic) + if (topicValidator != null) { + let acceptance: TopicValidatorResult + // Use try {} catch {} in case topicValidator() is synchronous + try { + acceptance = await topicValidator(from, validationResult.msg) + } catch (e) { + const errCode = (e as { code: string }).code + if (errCode === constants.ERR_TOPIC_VALIDATOR_IGNORE) acceptance = TopicValidatorResult.Ignore + if (errCode === constants.ERR_TOPIC_VALIDATOR_REJECT) acceptance = TopicValidatorResult.Reject + else acceptance = TopicValidatorResult.Ignore + } + + if (acceptance !== TopicValidatorResult.Accept) { + validationResult = { code: MessageStatus.invalid, reason: rejectReasonFromAcceptance(acceptance), msgIdStr } + } + } + } this.metrics?.onPrevalidationResult(rpcMsg.topic, validationResult.code) @@ -1353,27 +1377,6 @@ export class GossipSub extends TypedEventEmitter implements Pub this.sendIDontWants(msgId, rpcMsg.topic, propagationSource.toString()) } - // (Optional) Provide custom validation here with dynamic validators per topic - // NOTE: This custom topicValidator() must resolve fast (< 100ms) to allow scores - // to not penalize peers for long validation times. - const topicValidator = this.topicValidators.get(rpcMsg.topic) - if (topicValidator != null) { - let acceptance: TopicValidatorResult - // Use try {} catch {} in case topicValidator() is synchronous - try { - acceptance = await topicValidator(propagationSource, msg) - } catch (e) { - const errCode = (e as { code: string }).code - if (errCode === constants.ERR_TOPIC_VALIDATOR_IGNORE) acceptance = TopicValidatorResult.Ignore - if (errCode === constants.ERR_TOPIC_VALIDATOR_REJECT) acceptance = TopicValidatorResult.Reject - else acceptance = TopicValidatorResult.Ignore - } - - if (acceptance !== TopicValidatorResult.Accept) { - return { code: MessageStatus.invalid, reason: rejectReasonFromAcceptance(acceptance), msgIdStr } - } - } - return { code: MessageStatus.valid, messageId, msg } } @@ -2237,7 +2240,7 @@ export class GossipSub extends TypedEventEmitter implements Pub const { raw: rawMsg, msg } = await buildRawMessage(this.publishConfig, topic, data, transformedData) // calculate the message id from the un-transformed data - const msgId = await this.msgIdFn(msg) + const msgId = this.msgIdFn(msg) const msgIdStr = this.msgIdToStrFn(msgId) // Current publish opt takes precedence global opts, while preserving false value diff --git a/src/utils/msgIdFn.ts b/src/utils/msgIdFn.ts index 72e0724a..2d67f353 100644 --- a/src/utils/msgIdFn.ts +++ b/src/utils/msgIdFn.ts @@ -1,6 +1,5 @@ -import { msgId } from '@libp2p/pubsub/utils' import { digest } from '@chainsafe/as-sha256' - +import { msgId } from '@libp2p/pubsub/utils' import type { Message } from '@libp2p/interface' /** From f6830fa63111e15e0229116b1b468e455d930f3d Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Fri, 28 Nov 2025 09:16:03 +0700 Subject: [PATCH 3/4] fix: move validateToRawMessage() out of validateReceivedMessage() --- src/index.ts | 76 +++++++++--------- src/utils/buildRawMessage.ts | 148 +++++++++++++++++------------------ 2 files changed, 109 insertions(+), 115 deletions(-) diff --git a/src/index.ts b/src/index.ts index fd47c97b..b47e14e1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -54,7 +54,7 @@ import { type MessageId, type PublishOpts } from './types.js' -import { buildRawMessage, validateToRawMessage } from './utils/buildRawMessage.js' +import { buildRawMessage, validateStrictNoSignMessage, validateStrictSignMessage, type ValidationResult } from './utils/buildRawMessage.js' import { createGossipRpc, ensureControl } from './utils/create-gossip-rpc.js' import { shuffle, messageIdToString } from './utils/index.js' import { msgIdFnStrictNoSign, msgIdFnStrictSign } from './utils/msgIdFn.js' @@ -1214,28 +1214,40 @@ export class GossipSub extends TypedEventEmitter implements Pub private async handleReceivedMessage (from: PeerId, rpcMsg: RPC.Message): Promise { this.metrics?.onMsgRecvPreValidation(rpcMsg.topic) - let validationResult = await this.validateReceivedMessage(from, rpcMsg) - - if (validationResult.code === MessageStatus.valid) { - // (Optional) Provide custom validation here with dynamic validators per topic - // NOTE: This custom topicValidator() must resolve fast (< 100ms) to allow scores - // to not penalize peers for long validation times. - const msgIdStr = validationResult.messageId.msgIdStr - const topicValidator = this.topicValidators.get(rpcMsg.topic) - if (topicValidator != null) { - let acceptance: TopicValidatorResult - // Use try {} catch {} in case topicValidator() is synchronous - try { - acceptance = await topicValidator(from, validationResult.msg) - } catch (e) { - const errCode = (e as { code: string }).code - if (errCode === constants.ERR_TOPIC_VALIDATOR_IGNORE) acceptance = TopicValidatorResult.Ignore - if (errCode === constants.ERR_TOPIC_VALIDATOR_REJECT) acceptance = TopicValidatorResult.Reject - else acceptance = TopicValidatorResult.Ignore - } + let validationResult: ReceivedMessageResult + // Fast message ID stuff + const fastMsgIdStr = this.fastMsgIdFn?.(rpcMsg) + const msgIdCached = fastMsgIdStr !== undefined ? this.fastMsgIdCache?.get(fastMsgIdStr) : undefined - if (acceptance !== TopicValidatorResult.Accept) { - validationResult = { code: MessageStatus.invalid, reason: rejectReasonFromAcceptance(acceptance), msgIdStr } + if (msgIdCached != null) { + // This message has been seen previously. Ignore it + validationResult = { code: MessageStatus.duplicate, msgIdStr: msgIdCached } + } else { + const rawValidationResult = this.globalSignaturePolicy === StrictNoSign ? validateStrictNoSignMessage(rpcMsg) : await validateStrictSignMessage(rpcMsg) + // Perform basic validation on message and convert to RawGossipsubMessage for fastMsgIdFn() + validationResult = this.validateReceivedMessage(from, rpcMsg, fastMsgIdStr, rawValidationResult) + + if (validationResult.code === MessageStatus.valid) { + // (Optional) Provide custom validation here with dynamic validators per topic + // NOTE: This custom topicValidator() must resolve fast (< 100ms) to allow scores + // to not penalize peers for long validation times. + const msgIdStr = validationResult.messageId.msgIdStr + const topicValidator = this.topicValidators.get(rpcMsg.topic) + if (topicValidator != null) { + let acceptance: TopicValidatorResult + // Use try {} catch {} in case topicValidator() is synchronous + try { + acceptance = await topicValidator(from, validationResult.msg) + } catch (e) { + const errCode = (e as { code: string }).code + if (errCode === constants.ERR_TOPIC_VALIDATOR_IGNORE) acceptance = TopicValidatorResult.Ignore + if (errCode === constants.ERR_TOPIC_VALIDATOR_REJECT) acceptance = TopicValidatorResult.Reject + else acceptance = TopicValidatorResult.Ignore + } + + if (acceptance !== TopicValidatorResult.Accept) { + validationResult = { code: MessageStatus.invalid, reason: rejectReasonFromAcceptance(acceptance), msgIdStr } + } } } } @@ -1316,22 +1328,12 @@ export class GossipSub extends TypedEventEmitter implements Pub * Handles a newly received message from an RPC. * May forward to all peers in the mesh. */ - private async validateReceivedMessage ( + private validateReceivedMessage ( propagationSource: PeerId, - rpcMsg: RPC.Message - ): Promise { - // Fast message ID stuff - const fastMsgIdStr = this.fastMsgIdFn?.(rpcMsg) - const msgIdCached = fastMsgIdStr !== undefined ? this.fastMsgIdCache?.get(fastMsgIdStr) : undefined - - if (msgIdCached != null) { - // This message has been seen previously. Ignore it - return { code: MessageStatus.duplicate, msgIdStr: msgIdCached } - } - - // Perform basic validation on message and convert to RawGossipsubMessage for fastMsgIdFn() - const validationResult = await validateToRawMessage(this.globalSignaturePolicy, rpcMsg) - + rpcMsg: RPC.Message, + fastMsgIdStr: string | number | undefined, + validationResult: ValidationResult + ): ReceivedMessageResult { if (!validationResult.valid) { return { code: MessageStatus.invalid, reason: RejectReason.Error, error: validationResult.error } } diff --git a/src/utils/buildRawMessage.ts b/src/utils/buildRawMessage.ts index 2bee7d9b..ddd8ae8c 100644 --- a/src/utils/buildRawMessage.ts +++ b/src/utils/buildRawMessage.ts @@ -1,6 +1,6 @@ import { randomBytes } from '@libp2p/crypto' import { publicKeyFromProtobuf } from '@libp2p/crypto/keys' -import { StrictSign, StrictNoSign, type Message, type PublicKey, type PeerId } from '@libp2p/interface' +import { type Message, type PublicKey, type PeerId } from '@libp2p/interface' import { peerIdFromMultihash } from '@libp2p/peer-id' import * as Digest from 'multiformats/hashes/digest' import { concat as uint8ArrayConcat } from 'uint8arrays/concat' @@ -80,92 +80,84 @@ export async function buildRawMessage ( export type ValidationResult = { valid: true, message: Message } | { valid: false, error: ValidateError } -export async function validateToRawMessage ( - signaturePolicy: typeof StrictNoSign | typeof StrictSign, +export function validateStrictNoSignMessage ( + msg: RPC.Message +): ValidationResult { + if (msg.signature != null) return { valid: false, error: ValidateError.SignaturePresent } + if (msg.seqno != null) return { valid: false, error: ValidateError.SeqnoPresent } + if (msg.from != null) return { valid: false, error: ValidateError.FromPresent } + + return { valid: true, message: { type: 'unsigned', topic: msg.topic, data: msg.data ?? new Uint8Array(0) } } +} + +export async function validateStrictSignMessage ( msg: RPC.Message ): Promise { - // If strict-sign, verify all - // If anonymous (no-sign), ensure no preven - - switch (signaturePolicy) { - case StrictNoSign: - if (msg.signature != null) return { valid: false, error: ValidateError.SignaturePresent } - if (msg.seqno != null) return { valid: false, error: ValidateError.SeqnoPresent } - if (msg.from != null) return { valid: false, error: ValidateError.FromPresent } - - return { valid: true, message: { type: 'unsigned', topic: msg.topic, data: msg.data ?? new Uint8Array(0) } } - - case StrictSign: { - // Verify seqno - if (msg.seqno == null) return { valid: false, error: ValidateError.InvalidSeqno } - if (msg.seqno.length !== 8) { - return { valid: false, error: ValidateError.InvalidSeqno } - } + // Verify seqno + if (msg.seqno == null) return { valid: false, error: ValidateError.InvalidSeqno } + if (msg.seqno.length !== 8) { + return { valid: false, error: ValidateError.InvalidSeqno } + } - if (msg.signature == null) return { valid: false, error: ValidateError.InvalidSignature } - if (msg.from == null) return { valid: false, error: ValidateError.InvalidPeerId } + if (msg.signature == null) return { valid: false, error: ValidateError.InvalidSignature } + if (msg.from == null) return { valid: false, error: ValidateError.InvalidPeerId } - let fromPeerId: PeerId - try { - // TODO: Fix PeerId types - fromPeerId = peerIdFromMultihash(Digest.decode(msg.from)) - } catch (e) { - return { valid: false, error: ValidateError.InvalidPeerId } - } + let fromPeerId: PeerId + try { + // TODO: Fix PeerId types + fromPeerId = peerIdFromMultihash(Digest.decode(msg.from)) + } catch (e) { + return { valid: false, error: ValidateError.InvalidPeerId } + } - // - check from defined - // - transform source to PeerId - // - parse signature - // - get .key, else from source - // - check key == source if present - // - verify sig - - let publicKey: PublicKey - if (msg.key != null) { - publicKey = publicKeyFromProtobuf(msg.key) - // TODO: Should `fromPeerId.pubKey` be optional? - if (fromPeerId.publicKey !== undefined && !publicKey.equals(fromPeerId.publicKey)) { - return { valid: false, error: ValidateError.InvalidPeerId } - } - } else { - if (fromPeerId.publicKey == null) { - return { valid: false, error: ValidateError.InvalidPeerId } - } - publicKey = fromPeerId.publicKey - } + // - check from defined + // - transform source to PeerId + // - parse signature + // - get .key, else from source + // - check key == source if present + // - verify sig + + let publicKey: PublicKey + if (msg.key != null) { + publicKey = publicKeyFromProtobuf(msg.key) + // TODO: Should `fromPeerId.pubKey` be optional? + if (fromPeerId.publicKey !== undefined && !publicKey.equals(fromPeerId.publicKey)) { + return { valid: false, error: ValidateError.InvalidPeerId } + } + } else { + if (fromPeerId.publicKey == null) { + return { valid: false, error: ValidateError.InvalidPeerId } + } + publicKey = fromPeerId.publicKey + } - const rpcMsgPreSign: RPC.Message = { - from: msg.from, - data: msg.data, - seqno: msg.seqno, - topic: msg.topic, - signature: undefined, // Exclude signature field for signing - key: undefined // Exclude key field for signing - } + const rpcMsgPreSign: RPC.Message = { + from: msg.from, + data: msg.data, + seqno: msg.seqno, + topic: msg.topic, + signature: undefined, // Exclude signature field for signing + key: undefined // Exclude key field for signing + } - // Get the message in bytes, and prepend with the pubsub prefix - // the signature is over the bytes "libp2p-pubsub:" - const bytes = uint8ArrayConcat([SignPrefix, RPC.Message.encode(rpcMsgPreSign)]) + // Get the message in bytes, and prepend with the pubsub prefix + // the signature is over the bytes "libp2p-pubsub:" + const bytes = uint8ArrayConcat([SignPrefix, RPC.Message.encode(rpcMsgPreSign)]) - if (!(await publicKey.verify(bytes, msg.signature))) { - return { valid: false, error: ValidateError.InvalidSignature } - } + if (!(await publicKey.verify(bytes, msg.signature))) { + return { valid: false, error: ValidateError.InvalidSignature } + } - return { - valid: true, - message: { - type: 'signed', - from: fromPeerId, - data: msg.data ?? new Uint8Array(0), - sequenceNumber: BigInt(`0x${uint8ArrayToString(msg.seqno, 'base16')}`), - topic: msg.topic, - signature: msg.signature, - key: publicKey - } - } + return { + valid: true, + message: { + type: 'signed', + from: fromPeerId, + data: msg.data ?? new Uint8Array(0), + sequenceNumber: BigInt(`0x${uint8ArrayToString(msg.seqno, 'base16')}`), + topic: msg.topic, + signature: msg.signature, + key: publicKey } - - default: - throw new Error('Unreachable') } } From 43766295856f9be4f0e60fa5515713baca0fabe5 Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Fri, 28 Nov 2025 09:41:42 +0700 Subject: [PATCH 4/4] fix: lint --- src/index.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index b47e14e1..40c99783 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1240,9 +1240,7 @@ export class GossipSub extends TypedEventEmitter implements Pub acceptance = await topicValidator(from, validationResult.msg) } catch (e) { const errCode = (e as { code: string }).code - if (errCode === constants.ERR_TOPIC_VALIDATOR_IGNORE) acceptance = TopicValidatorResult.Ignore - if (errCode === constants.ERR_TOPIC_VALIDATOR_REJECT) acceptance = TopicValidatorResult.Reject - else acceptance = TopicValidatorResult.Ignore + acceptance = (errCode === constants.ERR_TOPIC_VALIDATOR_IGNORE) ? TopicValidatorResult.Ignore : (errCode === constants.ERR_TOPIC_VALIDATOR_REJECT) ? TopicValidatorResult.Reject : TopicValidatorResult.Ignore } if (acceptance !== TopicValidatorResult.Accept) {