Exemple #1
0
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
}
Exemple #2
0
// 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
}
Exemple #3
0
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)
}
Exemple #4
0
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)
	}
}
Exemple #5
0
func gen_account() {
	privAccount := account.GenPrivAccount()
	privAccountJSONBytes := binary.JSONBytes(privAccount)
	fmt.Printf(`Generated a new account!

%v

`,
		string(privAccountJSONBytes),
	)
}
Exemple #6
0
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
}
Exemple #7
0
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),
	)
}
Exemple #8
0
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)
}
Exemple #9
0
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
}