示例#1
0
func ValidatorsInfo(field string, do *definitions.Do) (string, error) {
	client := cclient.NewClient(do.Chain, "HTTP")

	r, err := client.ListValidators()
	if err != nil {
		return "", err
	}

	s, err := FormatOutput([]string{field}, 0, r)
	if err != nil {
		return "", err
	}

	type Account struct {
		Address string `mapstructure:"address" json:"address"`
	}

	var deconstructed []Account
	err = json.Unmarshal([]byte(s), &deconstructed)
	if err != nil {
		return "", err
	}

	vals := []string{}
	for _, v := range deconstructed {
		vals = append(vals, v.Address)
	}
	s = strings.Join(vals, ",")

	return s, nil
}
示例#2
0
func Broadcast(tx types.Tx, broadcastRPC string) (*rtypes.Receipt, error) {
	client := cclient.NewClient(broadcastRPC, "JSONRPC")
	rec, err := client.BroadcastTx(tx)
	if err != nil {
		return nil, err
	}
	return &rec.Receipt, nil
}
示例#3
0
func QueryContractJob(query *definitions.QueryContract, do *definitions.Do) (string, error) {
	// Preprocess variables. We don't preprocess data as it is processed by ReadAbiFormulateCall
	query.Source, _ = util.PreProcess(query.Source, do)
	query.Destination, _ = util.PreProcess(query.Destination, do)

	// Set the from and the to
	fromAddrBytes, err := hex.DecodeString(query.Source)
	if err != nil {
		return "", err
	}
	toAddrBytes, err := hex.DecodeString(query.Destination)
	if err != nil {
		return "", err
	}

	// Get the packed data from the ABI functions
	data, err := util.ReadAbiFormulateCall(query.Destination, query.Data, do)
	if err != nil {
		return "", err
	}
	dataBytes, err := hex.DecodeString(data)
	if err != nil {
		return "", err
	}

	// Call the client
	client := cclient.NewClient(do.Chain, "HTTP")
	retrn, err := client.Call(fromAddrBytes, toAddrBytes, dataBytes)
	if err != nil {
		return "", err
	}

	// Preprocess return
	result, err := util.FormatOutput([]string{"return"}, 0, retrn)
	if err != nil {
		return "", err
	}
	result, err = strconv.Unquote(result)
	if err != nil {
		return "", err
	}

	// Formally process the return
	logger.Debugf("Decoding Raw Result =>\t\t%s\n", result)
	result, err = util.ReadAndDecodeContractReturn(query.Destination, query.Data, result, do)
	if err != nil {
		return "", err
	}

	// Finalize
	logger.Printf("Return Value =>\t\t\t%s\n", result)
	return result, nil
}
示例#4
0
func ChainStatus(field string, do *definitions.Do) (string, error) {
	client := cclient.NewClient(do.Chain, "HTTP")

	r, err := client.Status()
	if err != nil {
		return "", err
	}

	s, err := FormatOutput([]string{field}, 0, r)
	if err != nil {
		return "", err
	}

	return s, nil
}
示例#5
0
func NamesInfo(account, field string, do *definitions.Do) (string, error) {
	client := cclient.NewClient(do.Chain, "HTTP")

	r, err := client.GetName(account)
	if err != nil {
		return "", err
	}
	if r == nil {
		return "", fmt.Errorf("Account %s does not exist", account)
	}

	r2 := r.Entry
	s, err := FormatOutput([]string{field}, 0, r2)
	if err != nil {
		return "", err
	}

	s, err = strconv.Unquote(s)
	if err != nil {
		return "", err
	}

	return s, nil
}
示例#6
0
func checkCommon(nodeAddr, signAddr, pubkey, addr, amtS, nonceS string) (pub account.PubKey, amt int64, nonce int64, err error) {
	if amtS == "" {
		err = fmt.Errorf("input must specify an amount with the --amt flag")
		return
	}

	var pubKeyBytes []byte
	if pubkey == "" && addr == "" {
		err = fmt.Errorf("at least one of --pubkey or --addr must be given")
		return
	} else if pubkey != "" {
		if addr != "" {
			// NOTE: if --addr given byt MINTX_PUBKEY is set, the pubkey still wins
			// TODO: fix this
			//logger.Errorln("you have specified both a pubkey and an address. the pubkey takes precedent")
		}
		pubKeyBytes, err = hex.DecodeString(pubkey)
		if err != nil {
			err = fmt.Errorf("pubkey is bad hex: %v", err)
			return
		}
	} else {
		// grab the pubkey from eris-keys
		pubKeyBytes, err = Pub(addr, signAddr)
		if err != nil {
			err = fmt.Errorf("failed to fetch pubkey for address (%s): %v", addr, err)
			return
		}

	}

	if len(pubKeyBytes) == 0 {
		err = fmt.Errorf("Error resolving public key")
		return
	}

	amt, err = strconv.ParseInt(amtS, 10, 64)
	if err != nil {
		err = fmt.Errorf("amt is misformatted: %v", err)
	}

	var pubArray [32]byte
	copy(pubArray[:], pubKeyBytes)
	pub = account.PubKeyEd25519(pubArray)
	addrBytes := pub.Address()

	if nonceS == "" {
		if nodeAddr == "" {
			err = fmt.Errorf("input must specify a nonce with the --nonce flag or use --node-addr (or MINTX_NODE_ADDR) to fetch the nonce from a node")
			return
		}

		// fetch nonce from node
		client := cclient.NewClient(nodeAddr, "HTTP")
		ac, err2 := client.GetAccount(addrBytes)
		if err2 != nil {
			err = fmt.Errorf("Error connecting to node (%s) to fetch nonce: %s", nodeAddr, err2.Error())
			return
		}
		if ac == nil || ac.Account == nil {
			err = fmt.Errorf("unknown account %X", addrBytes)
			return
		}
		nonce = int64(ac.Account.Sequence) + 1
	} else {
		nonce, err = strconv.ParseInt(nonceS, 10, 64)
		if err != nil {
			err = fmt.Errorf("nonce is misformatted: %v", err)
			return
		}
	}

	return
}
示例#7
0
func AccountsInfo(account, field string, do *definitions.Do) (string, error) {
	client := cclient.NewClient(do.Chain, "HTTP")

	addrBytes, err := hex.DecodeString(account)
	if err != nil {
		return "", fmt.Errorf("Account Addr %s is improper hex: %v", account, err)
	}

	r, err := client.GetAccount(addrBytes)
	if err != nil {
		return "", err
	}
	if r == nil {
		return "", fmt.Errorf("Account %s does not exist", account)
	}

	r2 := r.Account
	if r2 == nil {
		return "", fmt.Errorf("Account %s does not exist", account)
	}

	var s string
	if strings.Contains(field, "permissions") {

		type BasePermission struct {
			PermissionValue int `mapstructure:"perms" json:"perms"`
			SetBitValue     int `mapstructure:"set" json:"set"`
		}

		type AccountPermission struct {
			Base  *BasePermission `mapstructure:"base" json:"base"`
			Roles []string        `mapstructure:"roles" json:"roles"`
		}

		fields := strings.Split(field, ".")

		s, err = FormatOutput([]string{"permissions"}, 0, r2)

		var deconstructed AccountPermission
		err := json.Unmarshal([]byte(s), &deconstructed)
		if err != nil {
			return "", err
		}

		if len(fields) > 1 {
			switch fields[1] {
			case "roles":
				s = strings.Join(deconstructed.Roles, ",")
			case "base", "perms":
				s = strconv.Itoa(deconstructed.Base.PermissionValue)
			case "set":
				s = strconv.Itoa(deconstructed.Base.SetBitValue)
			}
		}
	} else {
		s, err = FormatOutput([]string{field}, 0, r2)
	}

	if err != nil {
		return "", err
	}

	return s, nil
}