Example #1
0
func Test_Factoid_Addresses(test *testing.T) {

	addr := NewAddress(primitives.Sha([]byte("A fake address")).Bytes())

	uaddr := primitives.ConvertFctAddressToUserStr(addr)

	if !primitives.ValidateFUserStr(uaddr) {
		test.Fail()
	}

	addrBack := primitives.ConvertUserStrToAddress(uaddr)

	if bytes.Compare(addrBack, addr.Bytes()) != 0 {
		test.Fail()
	}

	buaddr := []byte(uaddr)

	for i, v := range buaddr {
		for j := uint(0); j < 8; j++ {
			if !primitives.ValidateFUserStr(string(buaddr)) {
				test.Fail()
			}
			buaddr[i] = v ^ (01 << j)
			if primitives.ValidateFUserStr(string(buaddr)) {
				test.Fail()
			}
			buaddr[i] = v
		}
	}
}
Example #2
0
func HandleV2FactoidBalance(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) {
	fadr := new(AddressRequest)
	err := MapToObject(params, fadr)
	if err != nil {
		return nil, NewInvalidParamsError()
	}

	var adr []byte

	if primitives.ValidateFUserStr(fadr.Address) {
		adr = primitives.ConvertUserStrToAddress(fadr.Address)
	} else {
		adr, err = hex.DecodeString(fadr.Address)
		if err == nil && len(adr) != constants.HASH_LENGTH {
			return nil, NewInvalidAddressError()
		}
		if err != nil {
			return nil, NewInvalidAddressError()
		}
	}

	if len(adr) != constants.HASH_LENGTH {
		return nil, NewInvalidAddressError()
	}

	resp := new(FactoidBalanceResponse)
	resp.Balance = state.GetFactoidState().GetFactoidBalance(factoid.NewAddress(adr).Fixed())
	return resp, nil
}