|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/ethereum/go-ethereum/cmd/utils" |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | + "github.com/ethereum/go-ethereum/core/types" |
| 13 | + "github.com/ethereum/go-ethereum/rlp" |
| 14 | + "github.com/ethereum/go-ethereum/rpc" |
| 15 | + "github.com/ethereum/go-ethereum/state" |
| 16 | + "github.com/ethereum/go-ethereum/xeth" |
| 17 | + "github.com/obscuren/otto" |
| 18 | +) |
| 19 | + |
| 20 | +/* |
| 21 | +node admin bindings |
| 22 | +*/ |
| 23 | + |
| 24 | +func (js *jsre) adminBindings() { |
| 25 | + js.re.Set("admin", struct{}{}) |
| 26 | + t, _ := js.re.Get("admin") |
| 27 | + admin := t.Object() |
| 28 | + admin.Set("suggestPeer", js.suggestPeer) |
| 29 | + admin.Set("startRPC", js.startRPC) |
| 30 | + admin.Set("startMining", js.startMining) |
| 31 | + admin.Set("stopMining", js.stopMining) |
| 32 | + admin.Set("nodeInfo", js.nodeInfo) |
| 33 | + admin.Set("peers", js.peers) |
| 34 | + admin.Set("newAccount", js.newAccount) |
| 35 | + admin.Set("unlock", js.unlock) |
| 36 | + admin.Set("import", js.importChain) |
| 37 | + admin.Set("export", js.exportChain) |
| 38 | + admin.Set("dumpBlock", js.dumpBlock) |
| 39 | +} |
| 40 | + |
| 41 | +func (js *jsre) startMining(call otto.FunctionCall) otto.Value { |
| 42 | + _, err := call.Argument(0).ToInteger() |
| 43 | + if err != nil { |
| 44 | + fmt.Println(err) |
| 45 | + return otto.FalseValue() |
| 46 | + } |
| 47 | + // threads now ignored |
| 48 | + err = js.ethereum.StartMining() |
| 49 | + if err != nil { |
| 50 | + fmt.Println(err) |
| 51 | + return otto.FalseValue() |
| 52 | + } |
| 53 | + return otto.TrueValue() |
| 54 | +} |
| 55 | + |
| 56 | +func (js *jsre) stopMining(call otto.FunctionCall) otto.Value { |
| 57 | + js.ethereum.StopMining() |
| 58 | + return otto.TrueValue() |
| 59 | +} |
| 60 | + |
| 61 | +func (js *jsre) startRPC(call otto.FunctionCall) otto.Value { |
| 62 | + addr, err := call.Argument(0).ToString() |
| 63 | + if err != nil { |
| 64 | + fmt.Println(err) |
| 65 | + return otto.FalseValue() |
| 66 | + } |
| 67 | + port, err := call.Argument(1).ToInteger() |
| 68 | + if err != nil { |
| 69 | + fmt.Println(err) |
| 70 | + return otto.FalseValue() |
| 71 | + } |
| 72 | + dataDir := js.ethereum.DataDir |
| 73 | + |
| 74 | + l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", addr, port)) |
| 75 | + if err != nil { |
| 76 | + fmt.Printf("Can't listen on %s:%d: %v", addr, port, err) |
| 77 | + return otto.FalseValue() |
| 78 | + } |
| 79 | + go http.Serve(l, rpc.JSONRPC(xeth.New(js.ethereum, nil), dataDir)) |
| 80 | + return otto.TrueValue() |
| 81 | +} |
| 82 | + |
| 83 | +func (js *jsre) suggestPeer(call otto.FunctionCall) otto.Value { |
| 84 | + nodeURL, err := call.Argument(0).ToString() |
| 85 | + if err != nil { |
| 86 | + fmt.Println(err) |
| 87 | + return otto.FalseValue() |
| 88 | + } |
| 89 | + err = js.ethereum.SuggestPeer(nodeURL) |
| 90 | + if err != nil { |
| 91 | + fmt.Println(err) |
| 92 | + return otto.FalseValue() |
| 93 | + } |
| 94 | + return otto.TrueValue() |
| 95 | +} |
| 96 | + |
| 97 | +func (js *jsre) unlock(call otto.FunctionCall) otto.Value { |
| 98 | + addr, err := call.Argument(0).ToString() |
| 99 | + if err != nil { |
| 100 | + fmt.Println(err) |
| 101 | + return otto.FalseValue() |
| 102 | + } |
| 103 | + seconds, err := call.Argument(2).ToInteger() |
| 104 | + if err != nil { |
| 105 | + fmt.Println(err) |
| 106 | + return otto.FalseValue() |
| 107 | + } |
| 108 | + arg := call.Argument(1) |
| 109 | + var passphrase string |
| 110 | + if arg.IsUndefined() { |
| 111 | + fmt.Println("Please enter a passphrase now.") |
| 112 | + passphrase, err = readPassword("Passphrase: ", true) |
| 113 | + if err != nil { |
| 114 | + utils.Fatalf("%v", err) |
| 115 | + } |
| 116 | + } else { |
| 117 | + passphrase, err = arg.ToString() |
| 118 | + if err != nil { |
| 119 | + fmt.Println(err) |
| 120 | + return otto.FalseValue() |
| 121 | + } |
| 122 | + } |
| 123 | + am := js.ethereum.AccountManager() |
| 124 | + // err := am.Unlock(common.FromHex(split[0]), split[1]) |
| 125 | + // if err != nil { |
| 126 | + // utils.Fatalf("Unlock account failed '%v'", err) |
| 127 | + // } |
| 128 | + err = am.TimedUnlock(common.FromHex(addr), passphrase, time.Duration(seconds)*time.Second) |
| 129 | + if err != nil { |
| 130 | + fmt.Printf("Unlock account failed '%v'\n", err) |
| 131 | + return otto.FalseValue() |
| 132 | + } |
| 133 | + return otto.TrueValue() |
| 134 | +} |
| 135 | + |
| 136 | +func (js *jsre) newAccount(call otto.FunctionCall) otto.Value { |
| 137 | + arg := call.Argument(0) |
| 138 | + var passphrase string |
| 139 | + if arg.IsUndefined() { |
| 140 | + fmt.Println("The new account will be encrypted with a passphrase.") |
| 141 | + fmt.Println("Please enter a passphrase now.") |
| 142 | + auth, err := readPassword("Passphrase: ", true) |
| 143 | + if err != nil { |
| 144 | + utils.Fatalf("%v", err) |
| 145 | + } |
| 146 | + confirm, err := readPassword("Repeat Passphrase: ", false) |
| 147 | + if err != nil { |
| 148 | + utils.Fatalf("%v", err) |
| 149 | + } |
| 150 | + if auth != confirm { |
| 151 | + utils.Fatalf("Passphrases did not match.") |
| 152 | + } |
| 153 | + passphrase = auth |
| 154 | + } else { |
| 155 | + var err error |
| 156 | + passphrase, err = arg.ToString() |
| 157 | + if err != nil { |
| 158 | + fmt.Println(err) |
| 159 | + return otto.FalseValue() |
| 160 | + } |
| 161 | + } |
| 162 | + acct, err := js.ethereum.AccountManager().NewAccount(passphrase) |
| 163 | + if err != nil { |
| 164 | + fmt.Printf("Could not create the account: %v", err) |
| 165 | + return otto.UndefinedValue() |
| 166 | + } |
| 167 | + return js.re.ToVal(common.Bytes2Hex(acct.Address)) |
| 168 | +} |
| 169 | + |
| 170 | +func (js *jsre) nodeInfo(call otto.FunctionCall) otto.Value { |
| 171 | + return js.re.ToVal(js.ethereum.NodeInfo()) |
| 172 | +} |
| 173 | + |
| 174 | +func (js *jsre) peers(call otto.FunctionCall) otto.Value { |
| 175 | + return js.re.ToVal(js.ethereum.PeersInfo()) |
| 176 | +} |
| 177 | + |
| 178 | +func (js *jsre) importChain(call otto.FunctionCall) otto.Value { |
| 179 | + if len(call.ArgumentList) == 0 { |
| 180 | + fmt.Println("err: require file name") |
| 181 | + return otto.FalseValue() |
| 182 | + } |
| 183 | + |
| 184 | + fn, err := call.Argument(0).ToString() |
| 185 | + if err != nil { |
| 186 | + fmt.Println(err) |
| 187 | + return otto.FalseValue() |
| 188 | + } |
| 189 | + |
| 190 | + var fh *os.File |
| 191 | + fh, err = os.OpenFile(fn, os.O_RDONLY, os.ModePerm) |
| 192 | + if err != nil { |
| 193 | + fmt.Println(err) |
| 194 | + return otto.FalseValue() |
| 195 | + } |
| 196 | + defer fh.Close() |
| 197 | + |
| 198 | + var blocks types.Blocks |
| 199 | + if err = rlp.Decode(fh, &blocks); err != nil { |
| 200 | + fmt.Println(err) |
| 201 | + return otto.FalseValue() |
| 202 | + } |
| 203 | + |
| 204 | + js.ethereum.ChainManager().Reset() |
| 205 | + if err = js.ethereum.ChainManager().InsertChain(blocks); err != nil { |
| 206 | + fmt.Println(err) |
| 207 | + return otto.FalseValue() |
| 208 | + } |
| 209 | + |
| 210 | + return otto.TrueValue() |
| 211 | +} |
| 212 | + |
| 213 | +func (js *jsre) exportChain(call otto.FunctionCall) otto.Value { |
| 214 | + if len(call.ArgumentList) == 0 { |
| 215 | + fmt.Println("err: require file name") |
| 216 | + return otto.FalseValue() |
| 217 | + } |
| 218 | + |
| 219 | + fn, err := call.Argument(0).ToString() |
| 220 | + if err != nil { |
| 221 | + fmt.Println(err) |
| 222 | + return otto.FalseValue() |
| 223 | + } |
| 224 | + |
| 225 | + data := js.ethereum.ChainManager().Export() |
| 226 | + if err := common.WriteFile(fn, data); err != nil { |
| 227 | + fmt.Println(err) |
| 228 | + return otto.FalseValue() |
| 229 | + } |
| 230 | + |
| 231 | + return otto.TrueValue() |
| 232 | +} |
| 233 | + |
| 234 | +func (js *jsre) dumpBlock(call otto.FunctionCall) otto.Value { |
| 235 | + var block *types.Block |
| 236 | + if len(call.ArgumentList) > 0 { |
| 237 | + if call.Argument(0).IsNumber() { |
| 238 | + num, _ := call.Argument(0).ToInteger() |
| 239 | + block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num)) |
| 240 | + } else if call.Argument(0).IsString() { |
| 241 | + hash, _ := call.Argument(0).ToString() |
| 242 | + block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash)) |
| 243 | + } else { |
| 244 | + fmt.Println("invalid argument for dump. Either hex string or number") |
| 245 | + } |
| 246 | + |
| 247 | + } else { |
| 248 | + block = js.ethereum.ChainManager().CurrentBlock() |
| 249 | + } |
| 250 | + if block == nil { |
| 251 | + fmt.Println("block not found") |
| 252 | + return otto.UndefinedValue() |
| 253 | + } |
| 254 | + |
| 255 | + statedb := state.New(block.Root(), js.ethereum.StateDb()) |
| 256 | + dump := statedb.RawDump() |
| 257 | + return js.re.ToVal(dump) |
| 258 | + |
| 259 | +} |
0 commit comments