|
| 1 | +// Copyright 2018 The go-ethereum Authors |
| 2 | +// This file is part of go-ethereum. |
| 3 | +// |
| 4 | +// go-ethereum is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// go-ethereum is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "io/ioutil" |
| 21 | + "strconv" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/ethereum/go-ethereum/accounts/keystore" |
| 25 | + "github.com/ethereum/go-ethereum/cmd/utils" |
| 26 | + "github.com/ethereum/go-ethereum/common" |
| 27 | + "github.com/ethereum/go-ethereum/console" |
| 28 | + "github.com/ethereum/go-ethereum/contracts/checkpointoracle" |
| 29 | + "github.com/ethereum/go-ethereum/ethclient" |
| 30 | + "github.com/ethereum/go-ethereum/params" |
| 31 | + "github.com/ethereum/go-ethereum/rpc" |
| 32 | + "gopkg.in/urfave/cli.v1" |
| 33 | +) |
| 34 | + |
| 35 | +// newClient creates a client with specified remote URL. |
| 36 | +func newClient(ctx *cli.Context) *ethclient.Client { |
| 37 | + client, err := ethclient.Dial(ctx.GlobalString(nodeURLFlag.Name)) |
| 38 | + if err != nil { |
| 39 | + utils.Fatalf("Failed to connect to Ethereum node: %v", err) |
| 40 | + } |
| 41 | + return client |
| 42 | +} |
| 43 | + |
| 44 | +// newRPCClient creates a rpc client with specified node URL. |
| 45 | +func newRPCClient(url string) *rpc.Client { |
| 46 | + client, err := rpc.Dial(url) |
| 47 | + if err != nil { |
| 48 | + utils.Fatalf("Failed to connect to Ethereum node: %v", err) |
| 49 | + } |
| 50 | + return client |
| 51 | +} |
| 52 | + |
| 53 | +// getContractAddr retrieves the register contract address through |
| 54 | +// rpc request. |
| 55 | +func getContractAddr(client *rpc.Client) common.Address { |
| 56 | + var addr string |
| 57 | + if err := client.Call(&addr, "les_getCheckpointContractAddress"); err != nil { |
| 58 | + utils.Fatalf("Failed to fetch checkpoint oracle address: %v", err) |
| 59 | + } |
| 60 | + return common.HexToAddress(addr) |
| 61 | +} |
| 62 | + |
| 63 | +// getCheckpoint retrieves the specified checkpoint or the latest one |
| 64 | +// through rpc request. |
| 65 | +func getCheckpoint(ctx *cli.Context, client *rpc.Client) *params.TrustedCheckpoint { |
| 66 | + var checkpoint *params.TrustedCheckpoint |
| 67 | + |
| 68 | + if ctx.GlobalIsSet(indexFlag.Name) { |
| 69 | + var result [3]string |
| 70 | + index := uint64(ctx.GlobalInt64(indexFlag.Name)) |
| 71 | + if err := client.Call(&result, "les_getCheckpoint", index); err != nil { |
| 72 | + utils.Fatalf("Failed to get local checkpoint %v, please ensure the les API is exposed", err) |
| 73 | + } |
| 74 | + checkpoint = ¶ms.TrustedCheckpoint{ |
| 75 | + SectionIndex: index, |
| 76 | + SectionHead: common.HexToHash(result[0]), |
| 77 | + CHTRoot: common.HexToHash(result[1]), |
| 78 | + BloomRoot: common.HexToHash(result[2]), |
| 79 | + } |
| 80 | + } else { |
| 81 | + var result [4]string |
| 82 | + err := client.Call(&result, "les_latestCheckpoint") |
| 83 | + if err != nil { |
| 84 | + utils.Fatalf("Failed to get local checkpoint %v, please ensure the les API is exposed", err) |
| 85 | + } |
| 86 | + index, err := strconv.ParseUint(result[0], 0, 64) |
| 87 | + if err != nil { |
| 88 | + utils.Fatalf("Failed to parse checkpoint index %v", err) |
| 89 | + } |
| 90 | + checkpoint = ¶ms.TrustedCheckpoint{ |
| 91 | + SectionIndex: index, |
| 92 | + SectionHead: common.HexToHash(result[1]), |
| 93 | + CHTRoot: common.HexToHash(result[2]), |
| 94 | + BloomRoot: common.HexToHash(result[3]), |
| 95 | + } |
| 96 | + } |
| 97 | + return checkpoint |
| 98 | +} |
| 99 | + |
| 100 | +// newContract creates a registrar contract instance with specified |
| 101 | +// contract address or the default contracts for mainnet or testnet. |
| 102 | +func newContract(client *rpc.Client) (common.Address, *checkpointoracle.CheckpointOracle) { |
| 103 | + addr := getContractAddr(client) |
| 104 | + if addr == (common.Address{}) { |
| 105 | + utils.Fatalf("No specified registrar contract address") |
| 106 | + } |
| 107 | + contract, err := checkpointoracle.NewCheckpointOracle(addr, ethclient.NewClient(client)) |
| 108 | + if err != nil { |
| 109 | + utils.Fatalf("Failed to setup registrar contract %s: %v", addr, err) |
| 110 | + } |
| 111 | + return addr, contract |
| 112 | +} |
| 113 | + |
| 114 | +// promptPassphrase prompts the user for a passphrase. |
| 115 | +// Set confirmation to true to require the user to confirm the passphrase. |
| 116 | +func promptPassphrase(confirmation bool) string { |
| 117 | + passphrase, err := console.Stdin.PromptPassword("Passphrase: ") |
| 118 | + if err != nil { |
| 119 | + utils.Fatalf("Failed to read passphrase: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + if confirmation { |
| 123 | + confirm, err := console.Stdin.PromptPassword("Repeat passphrase: ") |
| 124 | + if err != nil { |
| 125 | + utils.Fatalf("Failed to read passphrase confirmation: %v", err) |
| 126 | + } |
| 127 | + if passphrase != confirm { |
| 128 | + utils.Fatalf("Passphrases do not match") |
| 129 | + } |
| 130 | + } |
| 131 | + return passphrase |
| 132 | +} |
| 133 | + |
| 134 | +// getPassphrase obtains a passphrase given by the user. It first checks the |
| 135 | +// --password command line flag and ultimately prompts the user for a |
| 136 | +// passphrase. |
| 137 | +func getPassphrase(ctx *cli.Context) string { |
| 138 | + passphraseFile := ctx.String(utils.PasswordFileFlag.Name) |
| 139 | + if passphraseFile != "" { |
| 140 | + content, err := ioutil.ReadFile(passphraseFile) |
| 141 | + if err != nil { |
| 142 | + utils.Fatalf("Failed to read passphrase file '%s': %v", |
| 143 | + passphraseFile, err) |
| 144 | + } |
| 145 | + return strings.TrimRight(string(content), "\r\n") |
| 146 | + } |
| 147 | + // Otherwise prompt the user for the passphrase. |
| 148 | + return promptPassphrase(false) |
| 149 | +} |
| 150 | + |
| 151 | +// getKey retrieves the user key through specified key file. |
| 152 | +func getKey(ctx *cli.Context) *keystore.Key { |
| 153 | + // Read key from file. |
| 154 | + keyFile := ctx.GlobalString(keyFileFlag.Name) |
| 155 | + keyJson, err := ioutil.ReadFile(keyFile) |
| 156 | + if err != nil { |
| 157 | + utils.Fatalf("Failed to read the keyfile at '%s': %v", keyFile, err) |
| 158 | + } |
| 159 | + // Decrypt key with passphrase. |
| 160 | + passphrase := getPassphrase(ctx) |
| 161 | + key, err := keystore.DecryptKey(keyJson, passphrase) |
| 162 | + if err != nil { |
| 163 | + utils.Fatalf("Failed to decrypt user key '%s': %v", keyFile, err) |
| 164 | + } |
| 165 | + return key |
| 166 | +} |
0 commit comments