{"id":26912932,"url":"https://github.com/shengxinjing/iblockchain","last_synced_at":"2025-07-27T02:37:46.303Z","repository":{"id":285454408,"uuid":"151587151","full_name":"shengxinjing/iblockchain","owner":"shengxinjing","description":"Learn blockchain by building one in node.js","archived":false,"fork":false,"pushed_at":"2018-10-08T17:36:46.000Z","size":475,"stargazers_count":134,"open_issues_count":0,"forks_count":43,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-31T20:51:21.476Z","etag":null,"topics":["blockchain","demo","learn","nodejs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shengxinjing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2018-10-04T14:40:06.000Z","updated_at":"2025-03-31T13:23:29.000Z","dependencies_parsed_at":"2025-03-31T20:51:23.269Z","dependency_job_id":null,"html_url":"https://github.com/shengxinjing/iblockchain","commit_stats":null,"previous_names":["shengxinjing/iblockchain"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shengxinjing%2Fiblockchain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shengxinjing%2Fiblockchain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shengxinjing%2Fiblockchain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shengxinjing%2Fiblockchain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shengxinjing","download_url":"https://codeload.github.com/shengxinjing/iblockchain/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246540518,"owners_count":20793930,"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","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":["blockchain","demo","learn","nodejs"],"created_at":"2025-04-01T15:38:23.747Z","updated_at":"2025-04-01T15:38:24.550Z","avatar_url":"https://github.com/shengxinjing.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n\u003cp align=\"center\"\u003e\n  \u003cimg  src=\"./logo.svg\" alt=\"iblockchain logo\"\u003e\n\u003c/p\u003e \n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://npmcharts.com/compare/iblockchain\"\u003e\u003cimg src=\"https://img.shields.io/npm/dm/iblockchain.svg\" alt=\"Downloads\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/iblockchain\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/iblockchain.svg\" alt=\"Version\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/iblockchain\"\u003e\u003cimg src=\"https://img.shields.io/npm/l/iblockchain.svg\" alt=\"License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## 功能\n\n- 学习区块链的演示demo\n- 简单的挖矿、交易、P2P网络的实现\n- 一系列demo演示命令，辅助理解blockchain\n\n## 安装\n\n```bash\nnpm install -g iblockchain\n```\n\n## 上手\n\n执行`iblockchain` 后 进入demo页面\n\n```\nWelcome to iblockchain !\n\n  Commands:\n\n    help [command...]             Provides help for a given command.\n    exit                          Exits application.\n    mine                          开始挖矿\n    blockchain                    查看整个区块链 [bc]\n    pending                       查看还没有打包进区块的交易\n    trans \u003cto\u003e \u003camount\u003e           给人转账\n    blance [address]              查看地址的余额\n    pub                           本地公钥(公钥就是地址)\n    prv                           本地私钥\n    detail \u003cindex\u003e                查看第n个区块的详情\n    peer                          查看P2P网络节点\n    --------我是分割线--------\n    hash \u003cvalue\u003e                  [演示] 计算sha256哈希\n    minedemo \u003cdata\u003e \u003cdifficulty\u003e  [演示] 挖矿计算逻辑\n    getpub \u003cvalue\u003e                [演示] 根据私钥计算出公钥(私钥加密 公钥解密，公钥可以从私钥计算出来)\n    sign \u003cvalue\u003e \u003cprv\u003e            [演示] 用私钥加密信息 得到签名\n    verify \u003cvalue\u003e \u003cpub\u003e \u003csig\u003e    [演示] 使用签名 校验信息 \u003c消息\u003e \u003c公钥\u003e \u003c签名\u003e\n    valid                         [演示] 区块链是否合法\n    update \u003cindex\u003e                [演示] 篡改\u003cindex\u003e个block的数据,第一个转账信息amount+1\n    mineblock \u003cindex\u003e             [演示] 对\u003cindex\u003e个上的区块数据重新挖矿,使其变成合法的小块块\n\niblockchain =\u003e [信息]: UDP服务监听完毕: 0.0.0.0:51881\n[信息]: 更新本地区块链\niblockchain =\u003e \n// 继续输入命令 体验功能\n\n```\n\n\n\n## 体验一下下\n\n**查看整个区块链数据**\n\n```bash\niblockchain =\u003e blockchain\n\n// output\n\n┌────────────────────┬────────────────────┬────────────────────┬────────────────────┬────────────────────┬────────────────────┐\n│ index              │ previousHash       │ timestamp          │ data               │ hash               │ nonce              │\n├────────────────────┼────────────────────┼────────────────────┼────────────────────┼────────────────────┼────────────────────┤\n│ 0                  │ \"0\"                │ 1538669227813      │ \"Welcome to ibloc… │ \"00000aa1fbf27775… │ 979911             │\n├────────────────────┼────────────────────┼────────────────────┼────────────────────┼────────────────────┼────────────────────┤\n│ 1                  │ \"00000aa1fbf27775… │ 1538988895613      │ [                  │ \"0000050c2b1b65a0… │ 69438              │\n│                    │                    │                    │  {                 │                    │                    │\n│                    │                    │                    │   \"from\": \"0\",     │                    │                    │\n│                    │                    │                    │   \"to\": \"0414b351… │                    │                    │\n│                    │                    │                    │   \"amount\": 100,   │                    │                    │\n│                    │                    │                    │   \"sig\": \"3046022… │                    │                    │\n│                    │                    │                    │   \"timestamp\": 15… │                    │                    │\n│                    │                    │                    │  }                 │                    │                    │\n│                    │                    │                    │ ]                  │                    │                    │\n├────────────────────┼────────────────────┼────────────────────┼────────────────────┼────────────────────┼────────────────────┤\n│ 2                  │ \"0000050c2b1b65a0… │ 1538988929584      │ [                  │ \"000001ee70cd40e8… │ 766700             │\n│                    │                    │                    │  {                 │                    │                    │\n│                    │                    │                    │   \"from\": \"0\",     │                    │                    │\n│                    │                    │                    │   \"to\": \"0429b0e2… │                    │                    │\n│                    │                    │                    │   \"amount\": 100,   │                    │                    │\n│                    │                    │                    │   \"sig\": \"3044022… │                    │                    │\n│                    │                    │                    │   \"timestamp\": 15… │                    │                    │\n│                    │                    │                    │  }                 │                    │                    │\n│                    │                    │                    │ ]                  │                    │                    │\n└────────────────────┴────────────────────┴────────────────────┴────────────────────┴────────────────────┴────────────────────┘\n```\n\n**挖矿,新增区块 mine**\n\n```bash\niblockchain =\u003e mine\n\n// output\n[信息]: 挖矿结束 用时4.30s , 算了806180次, 哈希值是00000c3633bd15a4b2c45bbc9373658694b55c078de4777bec28e10cea9e53fd，入账100 请笑纳\n\n┌────────────────────┬────────────────────┬────────────────────┬────────────────────┬────────────────────┬────────────────────┐\n│ index              │ previousHash       │ timestamp          │ nonce              │ hash               │ data               │\n├────────────────────┼────────────────────┼────────────────────┼────────────────────┼────────────────────┼────────────────────┤\n│ 14                 │ \"00000b93ccf0c0b6… │ 1539017632369      │ 806180             │ \"00000c3633bd15a4… │ [                  │\n│                    │                    │                    │                    │                    │  {                 │\n│                    │                    │                    │                    │                    │   \"from\": \"0\",     │\n│                    │                    │                    │                    │                    │   \"to\": \"041ad79c… │\n│                    │                    │                    │                    │                    │   \"amount\": 100,   │\n│                    │                    │                    │                    │                    │   \"sig\": \"3045022… │\n│                    │                    │                    │                    │                    │   \"timestamp\": 15… │\n│                    │                    │                    │                    │                    │  }                 │\n│                    │                    │                    │                    │                    │ ]                  │\n└────────────────────┴────────────────────┴────────────────────┴────────────────────┴────────────────────┴────────────────────┘\n```\n\n**查看本地地址（公钥） pub**\n\n```bash\niblockchain =\u003e pub\n//output\n041ad79cddfcc575efdae26e46e14a13c600967aff1db6d7eba66f770d585c542dfaed897f72b5bc40595f5601a868837f63bface539350ac6d013b99d17a0fbeb\n```\n\n**转账 trans**\n```bash\niblockchain =\u003e trans 0446b3de736bca3db5e19a7db06e56851a0a45fd07bbf74a355ef91f480151ff9ee7720367b4010c525bada702df62c98768d6a34bdd066e69653ad92e8f99f065 20\n\n//output\n┌────────────────────┬────────────────────┬────────────────────┬────────────────────┬────────────────────┐\n│ from               │ to                 │ amount             │ sig                │ timestamp          │\n├────────────────────┼────────────────────┼────────────────────┼────────────────────┼────────────────────┤\n│ \"041ad79cddfcc575… │ \"0446b3de736bca3d… │ 20                 │ \"3045022100804809… │ 1539017836747      │\n└────────────────────┴────────────────────┴────────────────────┴────────────────────┴────────────────────┘\n\n```\n\n**查看还没打包的交易 pending**\n\n```bash\niblockchain =\u003e pending\n\n//output\n┌────────────────────┬────────────────────┬────────────────────┬────────────────────┬────────────────────┐\n│ from               │ to                 │ amount             │ sig                │ timestamp          │\n├────────────────────┼────────────────────┼────────────────────┼────────────────────┼────────────────────┤\n│ \"041ad79cddfcc575… │ \"0446b3de736bca3d… │ 20                 │ \"3045022100804809… │ 1539017836747      │\n├────────────────────┼────────────────────┼────────────────────┼────────────────────┼────────────────────┤\n│ \"041ad79cddfcc575… │ \"0446b3de736bca3d… │ 12                 │ \"30460221008b13a5… │ 1539017874219      │\n└────────────────────┴────────────────────┴────────────────────┴────────────────────┴────────────────────┘\n```\n\n\n\n**查看地址余额(默认本地)) blance**\n\n```bash\niblockchain =\u003e blance 041ad79cddfcc575efdae26e46e14a13c600967aff1db6d7eba66f770d585c542dfaed897f72b5bc40595f5601a868837f63bface539350ac6d013b99d17a0fbeb\n{\n  \"address\": \"041ad79cddfcc575efdae26e46e14a13c600967aff1db6d7eba66f770d585c542dfaed897f72b5bc40595f5601a868837f63bface539350ac6d013b99d17a0fbeb\",\n  \"blance\": 168\n}\n┌────────────────────┬────────────────────┐\n│ address            │ blance             │\n├────────────────────┼────────────────────┤\n│ \"041ad79cddfcc575… │ 168                │\n└────────────────────┴────────────────────┘\n```\n\n**查看网络节点列表 peer**\n```bash\niblockchain =\u003e peer\n//output\n┌────────────────────┬────────────────────┐\n│ port               │ address            │\n├────────────────────┼────────────────────┤\n│ 8001               │ \"47.94.5.240\"      │\n├────────────────────┼────────────────────┤\n│ 8002               │ \"42.56.89.132\"     │\n├────────────────────┼────────────────────┤\n│ 59838              │ \"124.127.208.135\"  │\n├────────────────────┼────────────────────┤\n│ 27122              │ \"123.114.48.94\"    │\n└────────────────────┴────────────────────┘\n```\n\n## 演示命令\n\n**帮助 help**\n```bash\niblockchain =\u003e help\n```\n\n\n**计算sha256哈希 hash**\n```bash\niblockchain =\u003e hash imooc\n\n//output\n5401cc2e57b309427fd10963f8b8ee58545284e35bfba0e00780369307e54849\n┌────────────────────┬────────────────────┐\n│ hash               │ value              │\n├────────────────────┼────────────────────┤\n│ \"5401cc2e57b30942… │ \"imooc\"            │\n└────────────────────┴────────────────────┘\n```\n\n\n**演示挖矿逻辑 minedemo**\n```bash\niblockchain =\u003e minedemo imooc 2\n\n// output\n[信息] 数据是 imooc0 哈希值是25c771cbd916307a0417ebf4f8a705e8319bb100c93db4cde74f4fbca7da54d6\n[信息] 数据是 imooc1 哈希值是3b8368f43662598160ade6788b0504d789700c59574613c3dffb8b467aab8d5f\n[信息] 数据是 imooc2 哈希值是f3cf53d3b2d6f83ac857e8e018ee4bbd78c1fdd5d96660b20116d2bc6a4e281e\n...省略很多条\n[信息] 数据是 imooc248 哈希值是e6e8c271edec6c1b6f2b4bfa9725fecc0fe50356dcec47766440da2e29837e88\n[信息] 数据是 imooc249 哈希值是d1e79e718588ae125252d88c4a373934ebab3a9b1015f50d89329a151b78e406\n[信息] 数据是 imooc250 哈希值是008781ebdfdff9e1660841b4ab159f2cb6c3fca3217865366c30a1912d620cc5\n```\n\n\n**根据私钥算出公钥 getpub**\n```bash\niblockchain =\u003e getpub imooc\n私钥imooc计算出的公钥 04c8d1d62200e93e7bbf4b0c65da66d343656a6aebd51c146ed60f9dbbe39a4beededb5bbeeb1660269695d5a6959470c411c07545952e93f80460e76e31fcdde0\n```\n\n**RSA私钥加密 sign**\n```bash\n// 字符imooc作为私钥 对信息imoocrocks进行签名\niblockchain =\u003e sign imoocrocks imooc\n签名 304402206cc8d37c9e9ac3d679267aa0203550e04e7bb7ee6ba3f6c2193b597aa9bdaa58022029e54e94065c2a377a7632bdda0f51b6bd8e28c8667568459b2329683deb8db8\n```\n**RSA公钥验证签名 verify**\n```bash\niblockchain =\u003e verify imoocrocks 04c8d....dde0 3044....8db8\ntrue\n```\n\n\n**校验区块链是否合法 valid**\n```bash\niblockchain =\u003e valid\n校验结果: true\n```\n\n\n**篡改blockchain交易数据 update**\n```bash\niblockchain =\u003e update 16\n修改了 转账金额从20=\u003e21\n\niblockchain =\u003e valid\n[错误]: 第 16个区块hash不对,算出的是b2f20b3bde5fe69ab5d5092f50222ea57c980902d4709de19563e2e797759efd 区块里本来的hash是000001b0538910078e426c2989d561a4f715ab11838545ce4efe7838754f5fef 看来数据被篡改了\n[错误]: 第16个区块不合法\n校验结果: false\n\n```\n\n\n**重新对一个区块挖矿修复 mineblock**\n```bash\niblockchain =\u003e mineblock 16\n[错误]: 第 16个区块hash不对,算出的是b2f20b3bde5fe69ab5d5092f50222ea57c980902d4709de19563e2e797759efd 区块里本来的hash是000001b0538910078e426c2989d561a4f715ab11838545ce4efe7838754f5fef 看来数据被篡改了\n[信息]: 区块16修复完毕\n```\n\n**退出 exit**\n```bash\niblockchain =\u003e exit\n[信息]: 网络一线牵，珍惜这段缘 再见\n```\n\n## 一些p2p网络传递的信息\n```bash\n[信息]: 交易合法 新增一下 { from: '0444d....bcf1'\n[信息]: 挖矿结束 用时0.50s , 算了69438次, 哈希值是0000050c2b1b65a01659e85a17dfc3ad96fe8548dfaf694ff88ae478832ac37d，入账100 请笑纳\n[信息]: 大家快去和新朋友打个招呼\n[信息]: 有人挖矿成功，我们恭喜这位幸运儿\n ....\n\n```\n\n\n\n\n## 更多信息\n\n- [知乎](https://www.zhihu.com/people/woniuppp/activities)\n- 如何写出这个demo(教程)\n\n\n## 联系\n\n大家可以加我微信\n\n\u003cimg src=\"./woniu.png\" width='150px'\u003e\n\n或者直接捐赠\n\n\u003cimg src=\"./8.jpeg\" width='150px'\u003e\n\n\n## 协议\n\nMIT\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshengxinjing%2Fiblockchain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshengxinjing%2Fiblockchain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshengxinjing%2Fiblockchain/lists"}