Blockchain: Managing the BlockchainWith the Web3.js Javascript API, along with other APIs, we can use Javascript to interact with our blockchain. We can see when we attach to our blockchain service that we have APIs such as admin, eth and miner:
C:\Program Files\Geth> geth attach
Welcome to the Geth JavaScript console!
instance: Geth/v1.6.6-stable-10a45cb5/windows-amd64/go1.8.3
coinbase: 0xc7552f45deb093cafb47286a0bc9415845ca3735
at block: 5181 (Sat, 01 Jul 2017 17:00:48 BST)
datadir: d:\eth
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
First let's look at the admin JavaScript API:
> admin.datadir
"d:\\eth"
Next we have the miner JavaScript API: > miner.start() null > miner.stop() true Next let's look at the personal JavaScript API: > personal.listAccounts ["0xc7552f45deb093cafb47286a0bc9415845ca3735", "0x0851db3e133a15cd1c32531ffff96b4526e3cbcd"] > personal.newAccount('Password') "0xe9d07dcfea16a68bba57ad026ac2cdb65f77eae3" > personal.listAccounts ["0xc7552f45deb093cafb47286a0bc9415845ca3735", "0x0851db3e133a15cd1c32531ffff96b4526e3cbcd", "0xe9d07dcfea16a68bba57ad026ac2cdb65f77eae3"] > personal.unlockAccount('0xc7552f45deb093cafb47286a0bc9415845ca3735','Qwerty') true Now let's look at the Web3 API: > web3.version.api "0.18.1" > web3.version.node "Geth/v1.6.6-stable-10a45cb5/windows-amd64/go1.8.3" > web3.version.network "1" > web3.version.ethereum "0x3f" > web3.sha3("hello") "0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8" In Ethereum we use Kecca-256 (pronouced as "Ket-chak") for our hashing function. We can try and test [here]. Now, let's look at some more properties and functions of the Web3 API: > web3.toHex("Hello") "0x48656c6c6f" > web3.toAscii("0x68656c6c6f") "hello" > web3.toDecimal("0x1234") 4660 > web3.fromDecimal(4660) "0x1234" > web3.fromWei(eth.getBalance(eth.accounts[0]), "ether") 25905.288230376151511744 > web3.toWei(0.01, "ether") "10000000000000000" > web3.toBigNumber(10000000000000000) 10000000000000000 In order to span a JavaScript file, we can use: val=web3.version.api; console.log("API:"+val); val=web3.version.node; console.log("Node:"+val); val=web3.version.network; console.log("Verison:"+val); val=web3.version.ethereum; console.log("Version:"+val); val=web3.sha3("hello"); console.log("Hash (hello):"+val); val=web3.toHex("Hello"); console.log("Hex (Hello):"+val); val=web3.toAscii("0x68656c6c6f"); console.log("ASCII:"+val); val=web3.toDecimal("0x1234"); console.log("Decimal:"+val); val=web3.fromDecimal(4660); console.log("FromDecimal:"val); val=web3.fromWei(eth.getBalance(eth.accounts[0]),"ether"); console.log("FromWei:"val); Now let's look at web3.eth to view some of the details of our blockchain: > web3.eth.accounts ["0xc7552f45deb093cafb47286a0bc9415845ca3735", "0x0851db3e133a15cd1c32531ffff96b4526e3cbcd"] > web3.eth.defaultAccount = '0xc7552f45deb093cafb47286a0bc9415845ca3735' "0xc7552f45deb093cafb47286a0bc9415845ca3735" > > web3.eth.defaultAccount "0xc7552f45deb093cafb47286a0bc9415845ca3735" > web3.eth.defaultBlock "latest" > web3.eth.coinbase "0xc7552f45deb093cafb47286a0bc9415845ca3735" > web3.eth.gasPrice 18000000000 > miner.start() null > web3.eth.hashrate 2110 > miner.stop() true > web3.eth.blockNumber 5188 > web3.eth.getBalance('0xc7552f45deb093cafb47286a0bc9415845ca3735') 2.5940288230376151511744e+22 > web3.eth.getCode('0xc7552f45deb093cafb47286a0bc9415845ca3735') "0x" > web3.eth.getTransaction('0xbfe234697a506bfb7b2c19202bdeb9938e53eb9ae78104b22f3ff95477547861') { blockHash: "0xcd416ab6a3fb87eb88ce5d830d78f888db48d31d5b3eef200241ba5aeb46b377", blockNumber: 45, from: "0xc7552f45deb093cafb47286a0bc9415845ca3735", gas: 90000, gasPrice: 18000000000, hash: "0xbfe234697a506bfb7b2c19202bdeb9938e53eb9ae78104b22f3ff95477547861", input: "0x", nonce: 1, r: "0xfde84f388a55d630d3d3888c32ace00678f679b4035135870655480774a9b527", s: "0x229e7689eb74b5fda7480306ab8c01b32b3c789f6734396f8980188e66c3e667", to: "0x0851db3e133a15cd1c32531ffff96b4526e3cbcd", transactionIndex: 0, v: "0x41", value: 100000 } > web3.eth.getBlock(1000) { difficulty: 200275, extraData: "0xd983010606846765746887676f312e382e338777696e646f7773", gasLimit: 4712388, gasUsed: 0, hash: "0x3d661b1c9febced4f34512f084e5ac7dd69c67d9b3924dc20f33451b120aeca9", logsBloom: "0x0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000", miner: "0xc7552f45deb093cafb47286a0bc9415845ca3735", mixHash: "0x277ad79b424fb9b193d199380610173473d15de8f147d1e5b38fdad74edad23f", nonce: "0x404f2ec20278bd66", number: 1000, parentHash: "0xa54b09bda3f8705e6237b6dc2976843fe9c926120b1642e9ae31ed009eaef77e", receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", size: 539, stateRoot: "0x7b0b4cc36e4debb73f1150c054b77aaa23c5091570da6a0ddd61975e9b403dbc", timestamp: 1498558323, totalDifficulty: 159425092, transactions: [], transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", uncles: [] } > web3.eth.getBlock('0x3d661b1c9febced4f34512f084e5ac7dd69c67d9b3924dc20f33451b120aeca9') { difficulty: 200275, extraData: "0xd983010606846765746887676f312e382e338777696e646f7773", gasLimit: 4712388, gasUsed: 0, hash: "0x3d661b1c9febced4f34512f084e5ac7dd69c67d9b3924dc20f33451b120aeca9", logsBloom: "0x0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000", miner: "0xc7552f45deb093cafb47286a0bc9415845ca3735", mixHash: "0x277ad79b424fb9b193d199380610173473d15de8f147d1e5b38fdad74edad23f", nonce: "0x404f2ec20278bd66", number: 1000, parentHash: "0xa54b09bda3f8705e6237b6dc2976843fe9c926120b1642e9ae31ed009eaef77e", receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", size: 539, stateRoot: "0x7b0b4cc36e4debb73f1150c054b77aaa23c5091570da6a0ddd61975e9b403dbc", timestamp: 1498558323, totalDifficulty: 159425092, transactions: [], transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", uncles: [] } > web3.eth.getTransaction('0xbfe234697a506bfb7b2c19202bdeb9938e53eb9ae78104b22f3ff95477547861') { blockHash: "0xcd416ab6a3fb87eb88ce5d830d78f888db48d31d5b3eef200241ba5aeb46b377", blockNumber: 45, from: "0xc7552f45deb093cafb47286a0bc9415845ca3735", gas: 90000, gasPrice: 18000000000, hash: "0xbfe234697a506bfb7b2c19202bdeb9938e53eb9ae78104b22f3ff95477547861", input: "0x", nonce: 1, r: "0xfde84f388a55d630d3d3888c32ace00678f679b4035135870655480774a9b527", s: "0x229e7689eb74b5fda7480306ab8c01b32b3c789f6734396f8980188e66c3e667", to: "0x0851db3e133a15cd1c32531ffff96b4526e3cbcd", transactionIndex: 0, v: "0x41", value: 100000 } If we have an object in JavaScript we can then display it as: var tx = web3.eth.getTransaction('0xbfe234697a506bfb7b2c19202bdeb9938e53eb9ae78104b22f3ff95477547861') console.log(tx.gas) console.log(tx.value) |