func DownloadFile(privKey acm.PrivKey, remote string, command btypes.CommandServeFile, outPath string) (n int64, err error) { // Create authCommandJSONBytes nonce, err := GetNonce(remote) if err != nil { return 0, err } commandBytes, signature := SignCommand(privKey, nonce+1, command) authCommand := btypes.AuthCommand{ CommandJSONStr: string(commandBytes), Signatures: []acm.Signature{signature}, } authCommandJSONBytes := wire.JSONBytes(authCommand) // Make request and write to outPath. httpResponse, err := http.PostForm(remote+"/download", url.Values{"auth_command": {string(authCommandJSONBytes)}}) if err != nil { return 0, err } defer httpResponse.Body.Close() outFile, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) if err != nil { return 0, err } defer outFile.Close() n, err = io.Copy(outFile, httpResponse.Body) if err != nil { return 0, err } return n, nil }
// Each developer runs this func SignCommand(privKey acm.PrivKey, nonce int64, command btypes.Command) ([]byte, acm.Signature) { noncedCommand := btypes.NoncedCommand{ Nonce: nonce, Command: command, } commandJSONBytes := wire.JSONBytes(noncedCommand) signature := privKey.Sign(commandJSONBytes) return commandJSONBytes, signature }
func (c *ClientJSON) RequestResponse(s rpctypes.RPCRequest) (b []byte, err error) { b = wire.JSONBytes(s) buf := bytes.NewBuffer(b) resp, err := http.Post(c.addr, "text/json", buf) if err != nil { return nil, err } defer resp.Body.Close() return ioutil.ReadAll(resp.Body) }
func (privVal *PrivValidator) save() { if privVal.filePath == "" { PanicSanity("Cannot save PrivValidator: filePath not set") } jsonBytes := wire.JSONBytes(privVal) err := WriteFileAtomic(privVal.filePath, jsonBytes) if err != nil { // `@; BOOM!!! PanicCrisis(err) } }
func DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) { roundState := consensusState.GetRoundState() peerRoundStates := []string{} for _, peer := range p2pSwitch.Peers().List() { // TODO: clean this up? peerState := peer.Data.Get(types.PeerStateKey).(*cm.PeerState) peerRoundState := peerState.GetRoundState() peerRoundStateStr := peer.Key + ":" + string(wire.JSONBytes(peerRoundState)) peerRoundStates = append(peerRoundStates, peerRoundStateStr) } return &ctypes.ResultDumpConsensusState{roundState.String(), peerRoundStates}, nil }
func gen_validator() { privValidator := types.GenPrivValidator() privValidatorJSONBytes := wire.JSONBytes(privValidator) fmt.Printf(`Generated a new validator! Paste the following JSON into your %v file %v `, config.GetString("priv_validator_file"), string(privValidatorJSONBytes), ) }
func (tx *BondTx) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) { wire.WriteTo([]byte(Fmt(`{"chain_id":%s`, jsonEscape(chainID))), w, n, err) wire.WriteTo([]byte(Fmt(`,"tx":[%v,{"inputs":[`, TxTypeBond)), w, n, err) for i, in := range tx.Inputs { in.WriteSignBytes(w, n, err) if i != len(tx.Inputs)-1 { wire.WriteTo([]byte(","), w, n, err) } } wire.WriteTo([]byte(Fmt(`],"pub_key":`)), w, n, err) wire.WriteTo(wire.JSONBytes(tx.PubKey), w, n, err) wire.WriteTo([]byte(`,"unbond_to":[`), w, n, err) for i, out := range tx.UnbondTo { out.WriteSignBytes(w, n, err) if i != len(tx.UnbondTo)-1 { wire.WriteTo([]byte(","), w, n, err) } } wire.WriteTo([]byte(`]}]}`), w, n, err) }
func Call(remote string, method string, params []interface{}, dest interface{}) (interface{}, error) { // Make request and get responseBytes request := RPCRequest{ JSONRPC: "2.0", Method: method, Params: params, ID: "", } requestBytes := wire.JSONBytes(request) requestBuf := bytes.NewBuffer(requestBytes) log.Info(Fmt("RPC request to %v: %v", remote, string(requestBytes))) httpResponse, err := http.Post(remote, "text/json", requestBuf) if err != nil { return dest, err } defer httpResponse.Body.Close() responseBytes, err := ioutil.ReadAll(httpResponse.Body) if err != nil { return dest, err } log.Info(Fmt("RPC response: %v", string(responseBytes))) // Parse response into JSONResponse response := RPCResponse{} err = json.Unmarshal(responseBytes, &response) if err != nil { return dest, err } // Parse response into dest resultJSONObject := response.Result errorStr := response.Error if errorStr != "" { return dest, errors.New(errorStr) } dest = wire.ReadJSONObject(dest, resultJSONObject, &err) return dest, err }
func show_validator() { privValidatorFile := config.GetString("priv_validator_file") privValidator := types.LoadOrGenPrivValidator(privValidatorFile) fmt.Println(string(wire.JSONBytes(privValidator.PubKey))) }