示例#1
0
func cliIntsToStrings(cmd *cobra.Command, args []string) {
	cmd.ParseFlags(args)
	if len(args) != 2 {
		Exit(fmt.Errorf("Please enter PermFlag and SetBit integers"))
	}

	pf, sb := args[0], args[1]
	perms, err := strconv.Atoi(pf)
	IfExit(err)
	setbits, err := strconv.Atoi(sb)
	IfExit(err)

	m := coreIntsToStrings(types.PermFlag(perms), types.PermFlag(setbits))
	printStringPerms(m)
}
示例#2
0
文件: cli.go 项目: alist/mint-client
//takes a csv in the format defined [here]
func parseCsv(path string) (pubkeys, amts, names []string, perms, setbits []ptypes.PermFlag) {

	csvFile, err := os.Open(path)
	if err != nil {
		Exit(fmt.Errorf("Couldn't open file: %s: %v", path, err))
	}
	defer csvFile.Close()

	r := csv.NewReader(csvFile)
	//r.FieldsPerRecord = # of records expected
	params, err := r.ReadAll()
	if err != nil {
		Exit(fmt.Errorf("Couldn't read file: %v", err))

	}

	pubkeys = make([]string, len(params))
	amts = make([]string, len(params))
	names = make([]string, len(params))
	permsS := make([]string, len(params))
	setbitS := make([]string, len(params))
	for i, each := range params {
		pubkeys[i] = each[0]
		amts[i] = ifExistsElse(each, 1, "1000")
		names[i] = ifExistsElse(each, 2, "")
		permsS[i] = ifExistsElse(each, 3, fmt.Sprintf("%d", ptypes.DefaultPermFlags))
		setbitS[i] = ifExistsElse(each, 4, permsS[i])
	}

	//TODO convert int to uint64, see issue #25
	perms = make([]ptypes.PermFlag, len(permsS))
	for i, perm := range permsS {
		pflag, err := strconv.Atoi(perm)
		if err != nil {
			Exit(fmt.Errorf("Permissions must be an integer"))
		}
		perms[i] = ptypes.PermFlag(pflag)
	}
	setbits = make([]ptypes.PermFlag, len(setbitS))
	for i, setbit := range setbitS {
		setbitsFlag, err := strconv.Atoi(setbit)
		if err != nil {
			Exit(fmt.Errorf("SetBits must be an integer"))
		}
		setbits[i] = ptypes.PermFlag(setbitsFlag)
	}
	return pubkeys, amts, names, perms, setbits
}
示例#3
0
func hasBasePerm(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) {
	if !HasPermission(appState, caller, ptypes.HasBase) {
		return nil, ErrInvalidPermission{caller.Address, "has_base"}
	}
	if len(args) != 2*32 {
		return nil, fmt.Errorf("hasBasePerm() takes two arguments (address, permFlag)")
	}
	addr, permNum := returnTwoArgs(args)
	vmAcc := appState.GetAccount(addr)
	if vmAcc == nil {
		return nil, fmt.Errorf("Unknown account %X", addr)
	}
	permN := ptypes.PermFlag(Uint64FromWord256(permNum)) // already shifted
	if !ValidPermN(permN) {
		return nil, ptypes.ErrInvalidPermission(permN)
	}
	var permInt byte
	if HasPermission(appState, vmAcc, permN) {
		permInt = 0x1
	} else {
		permInt = 0x0
	}
	dbg.Printf("snative.hasBasePerm(0x%X, %b) = %v\n", addr.Postfix(20), permN, permInt)
	return LeftPadWord256([]byte{permInt}).Bytes(), nil
}
示例#4
0
func testKnownCSV(csvFile string, csv GenDoc) error {
	chainID := "test_chainID"

	if err := ioutil.WriteFile(path.Join(DirFlag, "accounts.csv"), []byte(csvFile), 0600); err != nil {
		return err
	}

	genBytes, err := coreKnown(chainID, path.Join(DirFlag, "accounts.csv"))
	if err != nil {
		return err
	}

	if err := ioutil.WriteFile(path.Join(DirFlag, "genesis.json"), genBytes, 0600); err != nil {
		return err
	}

	db := dbm.NewMemDB()
	gDoc, _ := sm.MakeGenesisStateFromFile(db, path.Join(DirFlag, "genesis.json"))

	N := len(csv.pubkeys)
	if len(gDoc.Validators) != N {
		return fmt.Errorf("Expected %d validators. Got %d", N, len(gDoc.Validators))
	}

	for i, pub := range csv.pubkeys {
		pubBytes, _ := hex.DecodeString(pub)
		if !bytes.Equal(gDoc.Validators[i].PubKey[:], pubBytes) {
			return fmt.Errorf("failed to find validator %d:%X in genesis.json", i, pub)
		}
		if len(csv.amts) > 0 && gDoc.Accounts[i].Amount != int64(csv.amts[i]) {
			return fmt.Errorf("amts dont match. got %d, expected %d", gDoc.Accounts[i].Amount, csv.amts[i])
		}
		if len(csv.perms) > 0 && gDoc.Accounts[i].Permissions.Base.Perms != ptypes.PermFlag(csv.perms[i]) {
			return fmt.Errorf("perms dont match. got %d, expected %d", gDoc.Accounts[i].Permissions.Base.Perms, csv.perms[i])
		}
		if len(csv.setbits) > 0 && gDoc.Accounts[i].Permissions.Base.SetBit != ptypes.PermFlag(csv.setbits[i]) {
			return fmt.Errorf("setbits dont match. got %d, expected %d", gDoc.Accounts[i].Permissions.Base.SetBit, csv.setbits[i])
		}
	}
	return nil
}
示例#5
0
func coreIntsToStrings(perms, setbits types.PermFlag) map[string]bool {
	m := make(map[string]bool)

	for i := uint(0); i < types.NumPermissions; i++ {
		pf := types.PermFlag(1 << i)
		if pf&setbits > 0 {
			name := types.PermFlagToString(pf)
			m[name] = pf&perms > 0
		}
	}
	return m
}
示例#6
0
func unsetBasePerm(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) {
	if !HasPermission(appState, caller, ptypes.UnsetBase) {
		return nil, ErrInvalidPermission{caller.Address, "unset_base"}
	}
	if len(args) != 2*32 {
		return nil, fmt.Errorf("unsetBase() takes two arguments (address, permFlag)")
	}
	addr, permNum := returnTwoArgs(args)
	vmAcc := appState.GetAccount(addr)
	if vmAcc == nil {
		return nil, fmt.Errorf("Unknown account %X", addr)
	}
	permN := ptypes.PermFlag(Uint64FromWord256(permNum))
	if !ValidPermN(permN) {
		return nil, ptypes.ErrInvalidPermission(permN)
	}
	if err = vmAcc.Permissions.Base.Unset(permN); err != nil {
		return nil, err
	}
	appState.UpdateAccount(vmAcc)
	dbg.Printf("snative.unsetBasePerm(0x%X, %b)\n", addr.Postfix(20), permN)
	return permNum.Bytes(), nil
}
示例#7
0
func setGlobalPerm(appState AppState, caller *Account, args []byte, gas *int64) (output []byte, err error) {
	if !HasPermission(appState, caller, ptypes.SetGlobal) {
		return nil, ErrInvalidPermission{caller.Address, "set_global"}
	}
	if len(args) != 2*32 {
		return nil, fmt.Errorf("setGlobal() takes two arguments (permFlag, permission value)")
	}
	permNum, perm := returnTwoArgs(args)
	vmAcc := appState.GetAccount(ptypes.GlobalPermissionsAddress256)
	if vmAcc == nil {
		PanicSanity("cant find the global permissions account")
	}
	permN := ptypes.PermFlag(Uint64FromWord256(permNum))
	if !ValidPermN(permN) {
		return nil, ptypes.ErrInvalidPermission(permN)
	}
	permV := !perm.IsZero()
	if err = vmAcc.Permissions.Base.Set(permN, permV); err != nil {
		return nil, err
	}
	appState.UpdateAccount(vmAcc)
	dbg.Printf("snative.setGlobalPerm(%b, %v)\n", permN, permV)
	return perm.Bytes(), nil
}
示例#8
0
func printStringPerms(m map[string]bool) {
	for i := 0; i < int(types.NumPermissions); i++ {
		permName := types.PermFlagToString(types.PermFlag(1) << uint(i))
		fmt.Printf("%s: %v\n", permName, m[permName])
	}
}
示例#9
0
//takes a csv in the format defined [here]
func parseCsv(filePath string) (pubKeys []account.PubKeyEd25519, amts []int64, names []string, perms, setbits []ptypes.PermFlag, err error) {

	csvFile, err := os.Open(filePath)
	if err != nil {
		Exit(fmt.Errorf("Couldn't open file: %s: %v", filePath, err))
	}
	defer csvFile.Close()

	r := csv.NewReader(csvFile)
	//r.FieldsPerRecord = # of records expected
	params, err := r.ReadAll()
	if err != nil {
		Exit(fmt.Errorf("Couldn't read file: %v", err))

	}

	pubkeys := make([]string, len(params))
	amtS := make([]string, len(params))
	names = make([]string, len(params))
	permsS := make([]string, len(params))
	setbitS := make([]string, len(params))
	for i, each := range params {
		pubkeys[i] = each[0]
		amtS[i] = ifExistsElse(each, 1, "1000")
		names[i] = ifExistsElse(each, 2, "")
		permsS[i] = ifExistsElse(each, 3, fmt.Sprintf("%d", ptypes.DefaultPermFlags))
		setbitS[i] = ifExistsElse(each, 4, permsS[i])
	}

	//TODO convert int to uint64, see issue #25
	perms = make([]ptypes.PermFlag, len(permsS))
	for i, perm := range permsS {
		pflag, err := strconv.Atoi(perm)
		if err != nil {
			Exit(fmt.Errorf("Permissions must be an integer"))
		}
		perms[i] = ptypes.PermFlag(pflag)
	}
	setbits = make([]ptypes.PermFlag, len(setbitS))
	for i, setbit := range setbitS {
		setbitsFlag, err := strconv.Atoi(setbit)
		if err != nil {
			Exit(fmt.Errorf("SetBits must be an integer"))
		}
		setbits[i] = ptypes.PermFlag(setbitsFlag)
	}

	// convert amts to ints
	amts = make([]int64, len(amtS))
	for i, a := range amtS {
		if amts[i], err = strconv.ParseInt(a, 10, 64); err != nil {
			err = fmt.Errorf("Invalid amount: %v", err)
			return
		}
	}

	// convert pubkey hex strings to struct
	pubKeys = pubKeyStringsToPubKeys(pubkeys)

	return pubKeys, amts, names, perms, setbits, nil
}