예제 #1
0
//Conversion to []byte based on "Type"
func PackProcessType(typ string, value string) ([]byte, error) {
	t := getMajorType(typ)
	switch t {
	case "byte", "string":
		return common.RightPadBytes([]byte(value), lengths["retBlock"]), nil
	case "uint":
		val, err := strconv.ParseUint(value, 10, 64)
		if err != nil {
			return nil, err
		}
		return U2U256(val), nil
	case "int":
		val, err := strconv.ParseInt(value, 10, 64)
		if err != nil {
			return nil, err
		}
		return S2S256(val), nil
	case "address":
		return common.LeftPadBytes(common.AddressStringToBytes(value), lengths["retBlock"]), nil
	case "bool":
		if value == "1" {
			return common.LeftPadBytes(common.Big1.Bytes(), lengths["retBlock"]), nil
		} else {
			return common.LeftPadBytes(common.Big0.Bytes(), lengths["retBlock"]), nil
		}
	default:
		return nil, fmt.Errorf("Unknown type. Cannot pack.")
	}
}
예제 #2
0
func S256(n *big.Int) []byte {
	sint := common.S256(n)
	ret := common.LeftPadBytes(sint.Bytes(), 32)
	if sint.Cmp(common.Big0) < 0 {
		for i, b := range ret {
			if b == 0 {
				ret[i] = 1
				continue
			}
			break
		}
	}

	return ret
}
예제 #3
0
// U256 will ensure unsigned 256bit on big nums
func U256(n *big.Int) []byte {
	return common.LeftPadBytes(common.U256(n).Bytes(), 32)
}