Skip to content

Geth测试常用信息

Meta data

创世文件genesis.json

json
{
  "config": {
    "chainId": 666,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "ethash": {},
    "cryptoType": 0
  },
  "nonce": "0x0",
  "timestamp": "0x5fc496af",
  "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0xffffffffffff",
  "difficulty": "0x00002",
  "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "number": "0x0",
  "gasUsed": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "alloc": {
    "0x47c9a59fe5d28ff862f8eaf5924dbc90af00b0ce": {
      "balance": "0xffffffffffffffff"
    },
    "0x19dbca3be6358f474caea47a0f177a33afa5a1d2": {
      "balance": "0xffffffffffffffff"
    }
  }
}

初始化命令

bash
geth --datadir /path/to/data/ init /path/to/genesis.json

启动命令

bash
geth --nodiscover --maxpeers 300 --identity "666" --rpc --rpccorsdomain "*" --rpcport 8545 --datadir "/path/to/data/" --port "30303" --allow-insecure-unlock --rpcapi "eth,net,web3,txpool,personal,ethash,miner,admin,debug" --networkid 666 console

解锁

bash
personal.unlockAccount(eth.accounts[0])

转账

bash
eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(0.0000001,'ether')})

私钥文件UTC--2021-01-14T03-06-22.416540000Z--47c9a59fe5d28ff862f8eaf5924dbc90af00b0ce

json
{"address":"47c9a59fe5d28ff862f8eaf5924dbc90af00b0ce","crypto":{"cipher":"aes-128-ctr","ciphertext":"3fd871d49cf8c477d043565b3a8bafdc8e229c43fc6fc448fe6065368cc6d234","cipherparams":{"iv":"2e85bed1e31dc6d09c6985f2b9b4bd87"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"638385b17830fe0495effa6f924b22d67be9b0c98b6d5c36ba36ce57c6c1094a"},"mac":"27fc10498073a194e68b3f35d91e940a4e1bddb22168fc9783cbb6e202624de4"},"id":"fd12612c-bbe0-4448-8961-666e2c81e4b3","version":3}

私钥文件UTC--2021-01-14T03-07-11.612118000Z--19dbca3be6358f474caea47a0f177a33afa5a1d2

json
{"address":"19dbca3be6358f474caea47a0f177a33afa5a1d2","crypto":{"cipher":"aes-128-ctr","ciphertext":"14cb694a4d2cc2082a930456f90c61fe982e191197c97496324fe3b931ce1863","cipherparams":{"iv":"25ca29c835c69f52320feab50ad2f58f"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"f237b3eb3eb4b49d28bb0e22afda5bdab4d3d9f2015c9d63efc6fbaa0949f197"},"mac":"0e96315c3f303536aaa87e92cceb357a029b027fb4dcae1e11afe0d62328a45f"},"id":"7af1a435-8c41-4d25-9c10-1db406a11d6b","version":3}

Docker image

fuming/geth-test -Docker Hub

Compile smart contract

sol -> abi

bash
solc --abi dataAuthentic.sol -o ./

abi -> go

bash
abigen --abi xxx.abi --out=xxx.go --pkg=xxx

sol -> go

bash
abigen -sol xxx.sol --pkg=contract --out=xxx.go

--pkg is the package name of the output go file.

Ganache in docker

docker-compose.yml

yaml
version: '3'

services:
  ganache:
    image: trufflesuite/ganache:latest
    command: --database.dbPath /app/db/ --wallet.accounts 0x0c1d607ea7c79dfaf4a146dca048798f2b9c17a947b9412d6587f41cca2e1bbd,10000000000000000000000000000000000000000000000 --wallet.accounts 0x588df36721ed0c7b77f361ea8b7262854b119564fe088003e7745205597c1212,10000000000000000000000000000000000000000000000 --wallet.accounts 0x0ced085d3e85413aeb2ee5a2b3612a2386189f07165fef4e565f8ebe637251b2,10000000000000000000000000000000000000000000000 --wallet.accounts 0x5810dc8847ff5bebf1e2b6de20572558dc095070e8b720e82b4122f08d354db7,10000000000000000000000000000000000000000000000 --wallet.accounts 0x242e012605dc7cec37e67bff406e97e3405ae54e1543689fbb14cf2691ec3626,10000000000000000000000000000000000000000000000 --wallet.accounts 0x71aa1c72cf84977dab1d87d481228fb7f99bb5761f46d08954b56edc2ed1fbac,10000000000000000000000000000000000000000000000 --wallet.accounts 0x0d3fbd3e393675197e1abe198893bbf26ba717ea334f54160d8b14adee6a6fea,10000000000000000000000000000000000000000000000 --wallet.accounts 0x28e9835010f34b15ce05383ba34361b9dbff604425e6a228c56fe1cf1f3f4de7,10000000000000000000000000000000000000000000000 --wallet.accounts 0xdbb38c21df394939c135ad21ff0c13f74cbf5e8768cc556696ba6ca3cccdd416,10000000000000000000000000000000000000000000000 --wallet.accounts 0x9b0606da37c31a3150286632f53c53825db7a7df36f47a36df7148945eb62cdc,10000000000000000000000000000000000000000000000
    ports:
      - "18545:8545"
    volumes:
      - db:/app/db
volumes:
  db:

Geth in Docker

docker-compose.yml

yaml
version: '3.5'
services:
  eth-test:
    image: fuming/geth-test:latest
    container_name: eth-test
    ports:
      - "8545:8545" #RPC
      - "8546:8546" #WebSocket
      - "30303:30303"
    volumes:
      - eth-data:/root/eth_data
    environment:
      data_dir: /root/eth_data

volumes:
  eth-data:

Get 64 characters long private key

private key fileUTC--2021-01-14T03-07-11.612118000Z--19dbca3be6358f474caea47a0f177a33afa5a1d2

json
{"address":"19dbca3be6358f474caea47a0f177a33afa5a1d2","crypto":{"cipher":"aes-128-ctr","ciphertext":"14cb694a4d2cc2082a930456f90c61fe982e191197c97496324fe3b931ce1863","cipherparams":{"iv":"25ca29c835c69f52320feab50ad2f58f"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"f237b3eb3eb4b49d28bb0e22afda5bdab4d3d9f2015c9d63efc6fbaa0949f197"},"mac":"0e96315c3f303536aaa87e92cceb357a029b027fb4dcae1e11afe0d62328a45f"},"id":"7af1a435-8c41-4d25-9c10-1db406a11d6b","version":3}

js code:

javascript
// npm install web3@4.0.2
const {Web3} = require('web3')
const web3 = new Web3()
web3.eth.accounts.decrypt({
    version: 3,
    id: '7af1a435-8c41-4d25-9c10-1db406a11d6b',
    address: '19dbca3be6358f474caea47a0f177a33afa5a1d2',
    crypto:
        {
            ciphertext:
                '14cb694a4d2cc2082a930456f90c61fe982e191197c97496324fe3b931ce1863',
            cipherparams: {iv: '25ca29c835c69f52320feab50ad2f58f'},
            cipher: 'aes-128-ctr',
            kdf: 'scrypt',
            kdfparams:
                {
                    dklen: 32,
                    salt:
                        'f237b3eb3eb4b49d28bb0e22afda5bdab4d3d9f2015c9d63efc6fbaa0949f197',
                    n: 262144,
                    r: 8,
                    p: 1
                },
            mac:
                '0e96315c3f303536aaa87e92cceb357a029b027fb4dcae1e11afe0d62328a45f'
        }
}, '123456').then((res) => {
    console.log(res)
}).catch((err) => {
    console.error(err)
})
//'123456' is the key passphrase

then you can get the output:

NOTE: web3.js is running without provider. You need to pass a provider in order to interact with the network!
{
  address: '0x19DbCa3Be6358F474CAEa47a0f177A33AFa5a1D2',
  privateKey: '0x4b5656188f0a85205937006befd54a65ebe3231f823a3893af228e8abf9afdf0',
  signTransaction: [Function: signTransaction],
  sign: [Function: sign],
  encrypt: [Function: encrypt]
}

Done