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 := binary.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 uint64, command btypes.Command) ([]byte, acm.Signature) { noncedCommand := btypes.NoncedCommand{ Nonce: nonce, Command: command, } commandJSONBytes := binary.JSONBytes(noncedCommand) signature := privKey.Sign(commandJSONBytes) return commandJSONBytes, signature }
func (c *ClientJSON) RequestResponse(s rpctypes.RPCRequest) (b []byte, err error) { b = binary.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 == "" { panic("Cannot save PrivValidator: filePath not set") } jsonBytes := binary.JSONBytes(privVal) err := WriteFileAtomic(privVal.filePath, jsonBytes) if err != nil { // `@; BOOM!!! panic(err) } }
func gen_account() { privAccount := account.GenPrivAccount() privAccountJSONBytes := binary.JSONBytes(privAccount) fmt.Printf(`Generated a new account! %v `, string(privAccountJSONBytes), ) }
func DumpConsensusState() (*ctypes.ResponseDumpConsensusState, error) { roundState := consensusState.GetRoundState() peerRoundStates := []string{} for _, peer := range p2pSwitch.Peers().List() { // TODO: clean this up? peerState := peer.Data.Get(cm.PeerStateKey).(*cm.PeerState) peerRoundState := peerState.GetRoundState() peerRoundStateStr := peer.Key + ":" + string(binary.JSONBytes(peerRoundState)) peerRoundStates = append(peerRoundStates, peerRoundStateStr) } return &ctypes.ResponseDumpConsensusState{roundState.String(), peerRoundStates}, nil }
func gen_validator() { privValidator := sm.GenPrivValidator() privValidatorJSONBytes := binary.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(w io.Writer, n *int64, err *error) { // We hex encode the network name so we don't deal with escaping issues. binary.WriteTo([]byte(Fmt(`{"network":"%X"`, config.GetString("network"))), w, n, err) binary.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 { binary.WriteTo([]byte(","), w, n, err) } } binary.WriteTo([]byte(Fmt(`],"pub_key":`)), w, n, err) binary.WriteTo(binary.JSONBytes(tx.PubKey), w, n, err) binary.WriteTo([]byte(`,"unbond_to":[`), w, n, err) for i, out := range tx.UnbondTo { out.WriteSignBytes(w, n, err) if i != len(tx.UnbondTo)-1 { binary.WriteTo([]byte(","), w, n, err) } } binary.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: 0, } requestBytes := binary.JSONBytes(request) requestBuf := bytes.NewBuffer(requestBytes) log.Debug(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.Debug(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 = binary.ReadJSONObject(dest, resultJSONObject, &err) return dest, err }