{"id":1802,"date":"2023-03-25T12:59:53","date_gmt":"2023-03-25T04:59:53","guid":{"rendered":""},"modified":"2023-03-25T12:59:53","modified_gmt":"2023-03-25T04:59:53","slug":"web3.js \u51c6\u5907\u8d26\u53f7","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1802.html","title":{"rendered":"web3.js \u51c6\u5907\u8d26\u53f7"},"content":{"rendered":"
\n
var <\/span>Tx = require('ethereumjs-tx'<\/span>).Transaction<\/pre>\n<\/code>\n <\/div>\n
\n \u5efa\u7acb\u4e00\u4e2aWeb3\u8fde\u63a5:\n <\/div>\n\nconst Web3 = require('web3'<\/span>)\r\nconst web3 = new <\/span>Web3('https:\/\/ropsten.infura.io\/YOUR_INFURA_API_KEY'<\/span>)<\/pre>\n<\/code>\n <\/div>\n
\u521b\u5efa\u8d26\u53f7<\/h2>\n\n \u6ce8\u610f\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u662fRopsten\u6d4b\u8bd5\u7f51\u7edc\uff0c\u5b83\u4e0e\u6211\u4eec\u5728\u524d\u9762\u7ae0\u8282\u4f7f\u7528\u7684Ethereum\u4e3b\u7f51\u4e0d\u540c\uff0c\u6d4b\u8bd5\u7f51\u7edc\u4f7f\u7528\u7684\u662f\u4f2a\u4ee5\u592a\u5e01\uff0c\u4e0d\u82b1\u94b1\u3002\n <\/div>\n\n \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f2a\u4ee5\u592a\u5e01\u4ece\u4e00\u4e2a\u5e10\u6237\u53d1\u9001\u5230\u53e6\u4e00\u4e2a\u5e10\u6237\uff0c\u56e0\u6b64\uff0c\u9700\u8981\u51c6\u59072\u4e2a\u5e10\u6237\u548c\u53ca\u5176\u79c1\u94a5\u3002\n <\/div>\n\u521b\u5efa2\u4e2a\u8d26\u53f7<\/h3>\n\n \u53ef\u4ee5\u5728node\u63a7\u5236\u53f0\uff0c\u4f7f\u7528Web3.js\u521b\u5efa\u65b0\u8d26\u6237\uff0c\u5982\u4e0b\u6240\u793a:\n <\/div>\n\nKevin@QIKEGU g:\\qikegu\\ethereum\\web3js\r\n> node\r\n> web3.eth.accounts.create<\/span>()\r\nThrown:\r\nReferenceError: web3 is not defined\r\n> const Web3 = require('web3'<\/span>)\r\nundefined\r\n> const web3 = new <\/span>Web3('https:\/\/ropsten.infura.io\/v3\/YOUR_INFURA_API_KEY'<\/span>)\r\nundefined\r\n> web3.eth.accounts.create<\/span>()\r\n{ address: '0xf4Ab5314ee8d7AA0eB00b366c52cEEccC62d6B4B'<\/span>,\r\n privateKey:\r\n '0xb75e2bcaec74857cf9bb6636d66a04784d4c0fcfd908f4a2bc213428eba5af0d'<\/span>,\r\n signTransaction: [Function: signTransaction],\r\n sign: [Function: sign],\r\n encrypt: [Function: encrypt] }\r\n> web3.eth.accounts.create<\/span>()\r\n{ address: '0xff96B8B43ECd6C49805747F94747bFfa3A960b69'<\/span>,\r\n privateKey:\r\n '0xac0adfdbaeb0770a479e79aac78779d82fdc2f9262e0c8f751ae70fb63ef6196'<\/span>,\r\n signTransaction: [Function: signTransaction],\r\n sign: [Function: sign],\r\n encrypt: [Function: encrypt] }\r\n><\/pre>\n<\/code>\n <\/div>\n
\u83b7\u53d6\u6d4b\u8bd5\u4ee5\u592a\u5e01<\/h2>\n\n \u521b\u5efa\u597d\u8d26\u53f7\u540e\uff0c\u53ef\u4ee5\u4ece\u4e0b\u9762\u7f51\u5740\u83b7\u53d6\u4f2a\u4ee5\u592a\u5e01\uff1a\n <\/div>\n\n ipfs.io\n <\/div>\n\n\n \u6ce8\u610f \u5982\u679c\u4e0a\u9762\u5730\u5740\u5931\u6548\uff0c\u53ef\u4ee5\u5728google\/bing\u4e2d\uff0c\u641c\u7d22 \u201cropsten faucet\u201d\uff0c\u67e5\u627e\u80fd\u83b7\u53d6\u6d4b\u8bd5\u4ee5\u592a\u5e01\u7684\u5730\u65b9\u3002\n <\/div>\n<\/blockquote>\n\u628a\u79c1\u94a5\u8bbe\u4e3a\u73af\u5883\u53d8\u91cf<\/h2>\n\n \u628a\u79c1\u94a5\u8bbe\u4e3a\u73af\u5883\u53d8\u91cf\uff0c\u7136\u540e\u4ece\u4ee3\u7801\u4e2d\u8bfb\u53d6\u73af\u5883\u53d8\u91cf\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u628a\u79c1\u94a5\u786c\u7f16\u7801\u5230\u6e90\u6587\u4ef6\u4e2d\uff0c\u79c1\u94a5\u662f\u7981\u6b62\u516c\u5f00\u7684\u3002\n <\/div>\n\n \u5728Windows\u4e0b\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a\n <\/div>\n\n> SET PRIVATE_KEY_1='b75e2bcaec74857cf9bb6636d66a04784d4c0fcfd908f4a2bc213428eba5af0d'<\/span>\r\n> SET PRIVATE_KEY_2='ac0adfdbaeb0770a479e79aac78779d82fdc2f9262e0c8f751ae70fb63ef6196'<\/span><\/pre>\n<\/code>\n <\/div>\n
\n \u5728linux\u4e0b\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a\n <\/div>\n\n$ export PRIVATE_KEY_1='b75e2bcaec74857cf9bb6636d66a04784d4c0fcfd908f4a2bc213428eba5af0d'<\/span>\r\n$ export PRIVATE_KEY_2='ac0adfdbaeb0770a479e79aac78779d82fdc2f9262e0c8f751ae70fb63ef6196'<\/span><\/pre>\n<\/code>\n <\/div>\n
app.js\u4e2d\u58f0\u660e\u8d26\u53f7\u53d8\u91cf\uff0c\u8bfb\u53d6\u79c1\u94a5<\/h2>\n\n \u58f0\u660e2\u4e2a\u8d26\u53f7\u53d8\u91cf\uff0c\u8bf7\u52a1\u5fc5\u4f7f\u7528\u4f60\u81ea\u5df1\u7684\u5e10\u6237\uff1a\n <\/div>\n\nconst account1 = '0xf4Ab5314ee8d7AA0eB00b366c52cEEccC62d6B4B'<\/span>\r\nconst account2 = '0xff96B8B43ECd6C49805747F94747bFfa3A960b69'<\/span><\/pre>\n<\/code>\n <\/div>\n
\n \u73b0\u5728\uff0c\u5728
\n app.js<\/b>\u6587\u4ef6\u4e2d\uff0c\u8bfb\u53d6\u8fd9\u4e9b\u79c1\u94a5\u73af\u5883\u53d8\u91cf\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5230\u53d8\u91cf\u4e2d:\n <\/div>\n\nconst privateKey1 = process.env.PRIVATE_KEY_1\r\nconst privateKey2 = process.env.PRIVATE_KEY_2<\/pre>\n<\/code>\n <\/div>\n
\n \u4e3a\u4e86\u4f7f\u7528\u79c1\u94a5\u5bf9\u4ea4\u6613\u8fdb\u884c\u7b7e\u540d\uff0c\u5fc5\u987b\u4f7f\u7528\u7f13\u51b2\u533a\u5c06\u5b83\u4eec\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u6570\u636e\u5b57\u7b26\u4e32\uff0c\u7f13\u51b2\u533a\u662fNodeJS\u4e2d\u7684\u5168\u5c40\u6a21\u5757\u3002\n <\/div>\n\nconst privateKey1 = Buffer.from<\/span>(process.env.PRIVATE_KEY_1)\r\nconst privateKey1 = Buffer.from<\/span>(process.env.PRIVATE_KEY_2)<\/pre>\n<\/code>\n <\/div>\n
\n \u597d\u4e86\uff0c\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u8bbe\u7f6e\u597d\u4e86\u9700\u8981\u7684\u53d8\u91cf!\n <\/div>\n <\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"web3.js \u51c6\u5907\u8d26\u53f7zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[155],"tags":[],"class_list":["post-1802","post","type-post","status-publish","format-standard","hentry","category-web3js"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1802"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1802"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1802\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}