├── .gitignore ├── getWeb3.js ├── package.json ├── demo1.js └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /getWeb3.js: -------------------------------------------------------------------------------- 1 | var Web3 = require('web3'); 2 | 3 | var web3; 4 | 5 | if (typeof web3 !== 'undefined') { 6 | web3 = new Web3(web3.currentProvider); 7 | } else { 8 | // set the provider you want from Web3.providers 9 | web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); 10 | } 11 | 12 | module.exports = web3; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web3demo", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "bignumber.js": "^6.0.0", 14 | "solc": "^0.4.21", 15 | "web3": "^1.0.0-beta.33" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /demo1.js: -------------------------------------------------------------------------------- 1 | var web3 = require('./getWeb3'); 2 | var BigNumber = require('bignumber.js'); 3 | 4 | //客户端或节点的版本信息 5 | // var version = web3.version.node; 6 | // console.log(version); 7 | 8 | //以太坊js的api版本 9 | // var version = web3.version.api; 10 | // console.log('api version:',version); 11 | 12 | //以太坊的协议版本 13 | // var version = web3.version.ethereum; 14 | // console.log('ethereum version:',version); 15 | 16 | //创建新账户 17 | // var account = web3.eth.accounts.create(); 18 | // var address = account.address; 19 | // console.log('address:',address); 20 | 21 | //获得账户余额 22 | web3.eth.getBalance('0xe70c4835b29e2fd35bd3f60c0a76413f70f17115').then(function(balance){ 23 | // console.log('balance:',balance) 24 | // var b1 = new BigNumber(balance) 25 | // console.log('b1:',b1) 26 | 27 | // var b2 = web3.utils.toBN(balance).toString(); 28 | // console.log('b2:',b2) 29 | 30 | }) 31 | 32 | 33 | // console.log(new BigNumber(20000)) 34 | // console.log(new BigNumber(100000000)) 35 | // console.log(new BigNumber(1111,2)) 36 | // console.log(new BigNumber(12345.678909)) 37 | // console.log(new BigNumber(-12345)) 38 | 39 | //查看区块内容 40 | // web3.eth.getBlock(0, function(error, result){ 41 | // if(!error) 42 | // console.log(result) 43 | // else 44 | // console.error(error); 45 | // }) 46 | 47 | 48 | //web3.utils.toHex 将任何值转为HEX 16进制 49 | //String|Number|Object|Array|BigNumber - 需要转化为HEX的值。如果是一个对象或数组类型,将会先用JSON.stringify1进行转换成字符串。如果传入的是BigNumber2,则将得到对应的Number的HEX 50 | // var str = "abcABC"; 51 | // var obj = {abc: 'ABC'}; 52 | // var bignumber = new BigNumber('12345678901234567890'); 53 | 54 | // var hstr = web3.utils.toHex(str); 55 | // var hobj = web3.utils.toHex(obj); 56 | // var hbg = web3.utils.toHex(bignumber); 57 | 58 | // console.log("Hex of Sring:" + hstr); 59 | // console.log("Hex of Object:" + hobj); 60 | // console.log("Hex of BigNumber:" + hbg); 61 | 62 | // //web3.utils.isHex(hex) 判断输入是不是16进制 63 | // web3.utils.isHex('0xc1912'); 64 | 65 | // //判断输入是不是一个地址 66 | // web3.utils.isAddress('0xc1912fee45d61c87cc5ea59dae31190fffff232d'); 67 | 68 | // //把金额单位换成wei 69 | // //web3.utils.toWei(number [, unit]) 70 | // web3.utils.toWei('1', 'ether'); 71 | // // > "1000000000000000000" 72 | 73 | // web3.utils.toWei('1', 'finney'); 74 | // // > "1000000000000000" 75 | 76 | // web3.utils.toWei('1', 'szabo'); 77 | // // > "1000000000000" 78 | 79 | // web3.utils.toWei('1', 'shannon'); 80 | // > "1000000000" 81 | 82 | // 把单位为wei的以太币换成 ether 83 | //web3.utils.fromWei(number [, unit]) 84 | // web3.utils.fromWei('1', 'ether'); 85 | // > "0.000000000000000001" 86 | 87 | // web3.utils.fromWei('1', 'finney'); 88 | // > "0.000000000000001" 89 | 90 | // web3.utils.fromWei('1', 'szabo'); 91 | // > "0.000000000001" 92 | 93 | // web3.utils.fromWei('1', 'shannon'); 94 | // > "0.000000001" 95 | 96 | // 判断给定的数据是否是 BigNumber 97 | //web3.utils.isBigNumber(bignumber) 98 | // var number = new BigNumber(10); 99 | // web3.utils.isBigNumber(number); 100 | 101 | 102 | //sha3算法 103 | //web3.utils.sha3(string) 104 | // web3.utils.keccak256(string) // ALIAS 105 | // web3.utils.sha3('234'); // taken as string 106 | // > "0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79" 107 | 108 | // web3.utils.sha3(new BN('234')); 109 | // > "0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a" 110 | 111 | // web3.utils.sha3(234); 112 | // > null // can't calculate the has of a number 113 | 114 | // web3.utils.sha3(0xea); // same as above, just the HEX representation of the number 115 | // > null 116 | 117 | // web3.utils.sha3('0xea'); // will be converted to a byte array first, and then hashed 118 | // > "0x2f20677459120677484f7104c76deb6846a2c071f9b3152c103bb12cd54d1a4a" 119 | 120 | 121 | var so = web3.eth.accounts.sign('Some data', '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318'); 122 | var hs = web3.eth.accounts.recover({ 123 | messageHash: so.messageHash, 124 | v: so.v, 125 | r: so.r, 126 | s: so.s 127 | }) 128 | console.log(so) 129 | console.log('------') 130 | console.log(hs) -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## 简介 2 | web3.js 是一个通过[RPC 调用](https://github.com/ethereum/wiki/wiki/JSON-RPC) 和本地以太坊节点进行通信的js库。web3.js可以与任何暴露了RPC接口的以太坊节点连接。 3 | web3中提供了eth对象 - web3.eth来与以太坊区块链进行交互。 4 | 5 | [在github上获得代码](https://github.com/cooleye/web3tutorial) 6 | ## 安装和和获取web3对象 7 | 安装 8 | npm: `npm install web3` 9 | bower: `bower install web3` 10 | metor: `meteor add ethereum:web3` 11 | vanilla: `dist./web3.min.js` 12 | 获得web3实例 13 | ``` 14 | if (typeof web3 !== 'undefined') { 15 | web3 = new Web3(web3.currentProvider); 16 | } else { 17 | // set the provider you want from Web3.providers 18 | web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); 19 | } 20 | ``` 21 | ## 使用实例 22 | 23 | 24 | ### 一、账户操作: 25 | ##### 创建新账户 26 | ``` 27 | web3.eth.accounts.create(); 28 | > { 29 | address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01", 30 | privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709", 31 | signTransaction: function(tx){...}, 32 | sign: function(data){...}, 33 | encrypt: function(password){...} 34 | } 35 | 36 | web3.eth.accounts.create('2435@#@#@±±±±!!!!678543213456764321§34567543213456785432134567'); 37 | > { 38 | address: "0xF2CD2AA0c7926743B1D4310b2BC984a0a453c3d4", 39 | privateKey: "0xd7325de5c2c1cf0009fac77d3d04a9c004b038883446b065871bc3e831dcd098", 40 | signTransaction: function(tx){...}, 41 | sign: function(data){...}, 42 | encrypt: function(password){...} 43 | } 44 | 45 | web3.eth.accounts.create(web3.utils.randomHex(32)); 46 | > { 47 | address: "0xe78150FaCD36E8EB00291e251424a0515AA1FF05", 48 | privateKey: "0xcc505ee6067fba3f6fc2050643379e190e087aeffe5d958ab9f2f3ed3800fa4e", 49 | signTransaction: function(tx){...}, 50 | sign: function(data){...}, 51 | encrypt: function(password){...} 52 | } 53 | ``` 54 | ##### 使用私钥创建账户 55 | web3.eth.accounts.privateKeyToAccount(privateKey); 56 | ``` 57 | web3.eth.accounts.privateKeyToAccount('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709'); 58 | > { 59 | address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01', 60 | privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709', 61 | signTransaction: function(tx){...}, 62 | sign: function(data){...}, 63 | encrypt: function(password){...} 64 | } 65 | 66 | web3.eth.accounts.privateKeyToAccount('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709'); 67 | > { 68 | address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01', 69 | privateKey: '0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709', 70 | signTransaction: function(tx){...}, 71 | sign: function(data){...}, 72 | encrypt: function(password){...} 73 | } 74 | ``` 75 | ##### 使用私钥签名一个交易 76 | web3.eth.accounts.signTransaction(tx, privateKey [, callback]); 77 | ``` 78 | web3.eth.accounts.signTransaction({ 79 | to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55', 80 | value: '1000000000', 81 | gas: 2000000 82 | }, '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318') 83 | .then(console.log); 84 | > { 85 | messageHash: '0x88cfbd7e51c7a40540b233cf68b62ad1df3e92462f1c6018d6d67eae0f3b08f5', 86 | v: '0x25', 87 | r: '0xc9cf86333bcb065d140032ecaab5d9281bde80f21b9687b3e94161de42d51895', 88 | s: '0x727a108a0b8d101465414033c3f705a9c7b826e596766046ee1183dbc8aeaa68', 89 | rawTransaction: '0xf869808504e3b29200831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a0c9cf86333bcb065d140032ecaab5d9281bde80f21b9687b3e94161de42d51895a0727a108a0b8d101465414033c3f705a9c7b826e596766046ee1183dbc8aeaa68' 90 | } 91 | 92 | web3.eth.accounts.signTransaction({ 93 | to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55', 94 | value: '1000000000', 95 | gas: 2000000, 96 | gasPrice: '234567897654321', 97 | nonce: 0, 98 | chainId: 1 99 | }, '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318') 100 | .then(console.log); 101 | > { 102 | messageHash: '0x6893a6ee8df79b0f5d64a180cd1ef35d030f3e296a5361cf04d02ce720d32ec5', 103 | r: '0x9ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9c', 104 | s: '0x440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428', 105 | v: '0x25', 106 | rawTransaction: '0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428' 107 | } 108 | ``` 109 | ##### 哈希一个消息 110 | web3.eth.accounts.hashMessage(message); 111 | ``` 112 | web3.eth.accounts.hashMessage("Hello World") 113 | > "0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2" 114 | 115 | // the below results in the same hash 116 | web3.eth.accounts.hashMessage(web3.utils.utf8ToHex("Hello World")) 117 | > "0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2" 118 | ``` 119 | ##### sign 给任意一个内容使用私钥签名 120 | web3.eth.accounts.sign(data, privateKey); 121 | ``` 122 | web3.eth.accounts.sign('Some data', '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318'); 123 | > { 124 | message: 'Some data', 125 | messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655', 126 | v: '0x1c', 127 | r: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd', 128 | s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029', 129 | signature: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c' 130 | } 131 | ``` 132 | ##### encrypt 加密 keystore 133 | 传入私钥和密码 134 | ``` 135 | web3.eth.accounts.encrypt('0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318', 'test!') 136 | > { 137 | version: 3, 138 | id: '04e9bcbb-96fa-497b-94d1-14df4cd20af6', 139 | address: '2c7536e3605d9c16a7a3d7b1898e529396a65c23', 140 | crypto: { 141 | ciphertext: 'a1c25da3ecde4e6a24f3697251dd15d6208520efc84ad97397e906e6df24d251', 142 | cipherparams: { iv: '2885df2b63f7ef247d753c82fa20038a' }, 143 | cipher: 'aes-128-ctr', 144 | kdf: 'scrypt', 145 | kdfparams: { 146 | dklen: 32, 147 | salt: '4531b3c174cc3ff32a6a7a85d6761b410db674807b2d216d022318ceee50be10', 148 | n: 262144, 149 | r: 8, 150 | p: 1 151 | }, 152 | mac: 'b8b010fff37f9ae5559a352a185e86f9b9c1d7f7a9f1bd4e82a5dd35468fc7f6' 153 | } 154 | } 155 | ``` 156 | ##### 解密 157 | ``` 158 | web3.eth.accounts.decrypt({ 159 | version: 3, 160 | id: '04e9bcbb-96fa-497b-94d1-14df4cd20af6', 161 | address: '2c7536e3605d9c16a7a3d7b1898e529396a65c23', 162 | crypto: { 163 | ciphertext: 'a1c25da3ecde4e6a24f3697251dd15d6208520efc84ad97397e906e6df24d251', 164 | cipherparams: { iv: '2885df2b63f7ef247d753c82fa20038a' }, 165 | cipher: 'aes-128-ctr', 166 | kdf: 'scrypt', 167 | kdfparams: { 168 | dklen: 32, 169 | salt: '4531b3c174cc3ff32a6a7a85d6761b410db674807b2d216d022318ceee50be10', 170 | n: 262144, 171 | r: 8, 172 | p: 1 173 | }, 174 | mac: 'b8b010fff37f9ae5559a352a185e86f9b9c1d7f7a9f1bd4e82a5dd35468fc7f6' 175 | } 176 | }, 'test!'); 177 | > { 178 | address: "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23", 179 | privateKey: "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318", 180 | signTransaction: function(tx){...}, 181 | sign: function(data){...}, 182 | encrypt: function(password){...} 183 | } 184 | ``` 185 | 186 | ### 钱包 187 | ##### 获得钱包 188 | 189 | eb3.eth.accounts.wallet; 190 | ``` 191 | web3.eth.accounts.wallet; 192 | > Wallet { 193 | 0: {...}, // account by index 194 | "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55": {...}, // same account by address 195 | "0xf0109fc8df283027b6285cc889f5aa624eac1f55": {...}, // same account by address lowercase 196 | 1: {...}, 197 | "0xD0122fC8DF283027b6285cc889F5aA624EaC1d23": {...}, 198 | "0xd0122fc8df283027b6285cc889f5aa624eac1d23": {...}, 199 | 200 | add: function(){}, 201 | remove: function(){}, 202 | save: function(){}, 203 | load: function(){}, 204 | clear: function(){}, 205 | 206 | length: 2, 207 | } 208 | ``` 209 | ##### 使用web3.eth.personal 新建账户 210 | web3.eth.personal.newAccount(password, [callback]) 211 | ``` 212 | web3.eth.personal.newAccount('!@superpassword') 213 | .then(console.log); 214 | > '0x1234567891011121314151617181920212223456' 215 | ``` 216 | ##### 获得账户余额 217 | ``` 218 | web3.eth.getBalance('0xe70c4835b29e2fd35bd3f60c0a76413f70f17115').then(function(balance){ 219 | console.log('balance:',balance) 220 | balance = new BigNumber(balance) 221 | console.log('balance:',balance) 222 | }) 223 | ``` 224 | 这里使用了BigNumber.js 处理余额显示,[参考我另一片博文](https://www.jianshu.com/p/bbb3fbbe9299)。 225 | 226 | 227 | ### utils工具函数 228 | 229 | #####web3.utils.toHex 将任何值转为HEX 16进制 230 | String|Number|Object|Array|BigNumber - 需要转化为HEX的值。如果是一个对象或数组类型,将会先用JSON.stringify1进行转换成字符串。如果传入的是BigNumber2,则将得到对应的Number的HEX 231 | ``` 232 | var str = "abcABC"; 233 | var obj = {abc: 'ABC'}; 234 | var bignumber = new BigNumber('12345678901234567890'); 235 | 236 | var hstr = web3.utils.toHex(str); 237 | var hobj = web3.utils.toHex(obj); 238 | var hbg = web3.utils.toHex(bignumber); 239 | 240 | console.log("Hex of Sring:" + hstr); 241 | console.log("Hex of Object:" + hobj); 242 | console.log("Hex of BigNumber:" + hbg); 243 | ``` 244 | ##### web3.utils.isHex(hex) 判断输入是不是16进制 245 | ``` 246 | web3.utils.isHex('0xc1912'); 247 | ``` 248 | ##### 判断输入是不是一个地址 249 | ``` 250 | web3.utils.isAddress('0xc1912fee45d61c87cc5ea59dae31190fffff232d'); 251 | ``` 252 | ##### 把金额单位换成wei 253 | web3.utils.toWei(number [, unit]) 254 | ``` 255 | web3.utils.toWei('1', 'ether'); 256 | > "1000000000000000000" 257 | 258 | web3.utils.toWei('1', 'finney'); 259 | > "1000000000000000" 260 | 261 | web3.utils.toWei('1', 'szabo'); 262 | > "1000000000000" 263 | 264 | web3.utils.toWei('1', 'shannon'); 265 | /> "1000000000" 266 | ``` 267 | ##### 把单位为wei的以太币换成 ether 268 | web3.utils.fromWei(number [, unit]) 269 | ``` 270 | web3.utils.fromWei('1', 'ether'); 271 | > "0.000000000000000001" 272 | 273 | web3.utils.fromWei('1', 'finney'); 274 | > "0.000000000000001" 275 | 276 | web3.utils.fromWei('1', 'szabo'); 277 | > "0.000000000001" 278 | 279 | web3.utils.fromWei('1', 'shannon'); 280 | > "0.000000001" 281 | ``` 282 | ##### 判断给定的数据是否是 BigNumber 283 | web3.utils.isBigNumber(bignumber) 284 | ``` 285 | var number = new BigNumber(10); 286 | web3.utils.isBigNumber(number); 287 | ``` 288 | 289 | ##### sha3算法 290 | // web3.utils.sha3(string) 291 | // web3.utils.keccak256(string) // ALIAS 292 | ``` 293 | web3.utils.sha3('234'); // taken as string 294 | > "0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79" 295 | 296 | web3.utils.sha3(new BN('234')); 297 | > "0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a" 298 | 299 | web3.utils.sha3(234); 300 | > null // can't calculate the has of a number 301 | 302 | web3.utils.sha3(0xea); // same as above, just the HEX representation of the number 303 | > null 304 | 305 | web3.utils.sha3('0xea'); // will be converted to a byte array first, and then hashed 306 | > "0x2f20677459120677484f7104c76deb6846a2c071f9b3152c103bb12cd54d1a4a" 307 | ``` 308 | 309 | 310 | [在github上获得代码](https://github.com/cooleye/web3tutorial) --------------------------------------------------------------------------------