{"id":26287638,"url":"https://github.com/dozyio/js-libp2p-middleware-evm","last_synced_at":"2025-08-28T05:12:43.352Z","repository":{"id":281561014,"uuid":"945561426","full_name":"dozyio/js-libp2p-middleware-evm","owner":"dozyio","description":null,"archived":false,"fork":false,"pushed_at":"2025-08-06T10:52:49.000Z","size":940,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-09T09:52:37.471Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dozyio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-03-09T17:56:40.000Z","updated_at":"2025-06-28T14:16:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"1f399273-4a2d-4e93-b888-9faf037827ca","html_url":"https://github.com/dozyio/js-libp2p-middleware-evm","commit_stats":null,"previous_names":["dozyio/js-libp2p-middleware-evm"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/dozyio/js-libp2p-middleware-evm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dozyio%2Fjs-libp2p-middleware-evm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dozyio%2Fjs-libp2p-middleware-evm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dozyio%2Fjs-libp2p-middleware-evm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dozyio%2Fjs-libp2p-middleware-evm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dozyio","download_url":"https://codeload.github.com/dozyio/js-libp2p-middleware-evm/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dozyio%2Fjs-libp2p-middleware-evm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272443742,"owners_count":24935998,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-28T02:00:10.768Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-14T21:52:01.860Z","updated_at":"2025-08-28T05:12:43.347Z","avatar_url":"https://github.com/dozyio.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# js-libp2p-middleware-evm\n\nRequires https://github.com/libp2p/js-libp2p/pull/3204. Uses https://github.com/dozyio/evm-rule-engine\n\n## Example\n\nAn slightly contrived example of 2 js-libp2p peers using a EVM blockchain to\nvalidate that each peer is holding 1 Eth.\n\nThe middleware is run after connection setup and encryption / multiplexing is\nnegotiated but before another stream is setup. The middleware is mutual i.e.\nboth sides run middleware checks, so it should run twice per connection.\n\nIf the middleware fails, i.e. a peer isn't holding 1 Eth, the connection closes.\n\n### Example Source\n\n```typescript\n// evm.ts\nimport { tcp } from \"@libp2p/tcp\"\nimport { createLibp2p } from \"libp2p\"\nimport { noise } from \"@chainsafe/libp2p-noise\"\nimport { identify } from '@libp2p/identify'\nimport { ping } from '@libp2p/ping'\nimport { yamux } from \"@chainsafe/libp2p-yamux\"\nimport { ethers, Wallet } from 'ethers'\nimport { prefixLogger } from '@libp2p/logger'\nimport { MiddlewareRegistrar } from 'libp2p-middleware-registrar'\nimport { middlewareEVM } from 'libp2p-middleware-evm'\nimport { EVMRuleEngine, createRulesFromDefinitions } from 'evm-rule-engine'\nimport type { Networks } from 'evm-rule-engine'\n\nconst networks: Networks = [\n  {\n    provider: new ethers.JsonRpcProvider('http://127.0.0.1:8545'),\n    chainId: '31337'\n  }\n]\n\nconst engine = new EVMRuleEngine({ networks })\n\nconst ruleDefinitions = [\n  {\n    type: 'walletBalance',\n    chainId: '31337',\n    params: {\n      value: ethers.parseEther('1'),\n      compareType: 'gte'\n    }\n  },\n]\n\nconst rules = createRulesFromDefinitions(networks, ruleDefinitions)\nengine.addRules(rules)\n\nasync function newNode(port: string, nickname: string) {\n  let signer: Wallet\n  if (nickname === 'n1') {\n    signer = new Wallet('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80')\n  } else if (nickname === 'n2') {\n    signer = new Wallet('0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d')\n  }\n  const node = await createLibp2p({\n    logger: prefixLogger(nickname),\n    addresses: {\n      listen: [\n        `/ip6/::/tcp/${port}`\n      ]\n    },\n    transports: [\n      tcp(),\n    ],\n    connectionEncrypters: [noise()],\n    streamMuxers: [yamux()],\n    services: {\n      identify: identify(),\n      ping: ping()\n    },\n    registrar: (components) =\u003e {\n      const middleware = middlewareEVM({ signer, evmRuleEngine: engine })\n\n      return new MiddlewareRegistrar(components.registrar, middleware(components), components.logger)\n    }\n  })\n\n  await node.start()\n\n  console.log(`Node started with id ${node.peerId.toString()}`)\n  console.log('Mutliaddrs', node.getMultiaddrs())\n\n  return node\n}\n\nconst n1 = await newNode('12345', 'n1')\nconst n2 = await newNode('12346', 'n2')\n\nconst rtt1 = await n1.services.ping.ping(n2.getMultiaddrs()[0])\nconsole.log('rtt1', rtt1)\n```\n\n### Running Example\nRun anvil in a terminal window\n```sh\nanvil --port 8545 --chain-id 31337\n```\n\nRun the example with logging (note: use node 23 to run typescript)\n```sh\nDEBUG=* node evm.ts\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdozyio%2Fjs-libp2p-middleware-evm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdozyio%2Fjs-libp2p-middleware-evm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdozyio%2Fjs-libp2p-middleware-evm/lists"}