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 }
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 } } }
func Test_Entry_Credit_Addresses(test *testing.T) { addr := NewAddress(primitives.Sha([]byte("A fake address")).Bytes()) uaddr := primitives.ConvertECAddressToUserStr(addr) if !primitives.ValidateECUserStr(uaddr) { fmt.Printf("1") test.Fail() } addrBack := primitives.ConvertUserStrToAddress(uaddr) if bytes.Compare(addrBack, addr.Bytes()) != 0 { fmt.Printf("2") test.Fail() } buaddr := []byte(uaddr) for i, v := range buaddr { for j := uint(0); j < 8; j++ { if !primitives.ValidateECUserStr(string(buaddr)) { fmt.Printf("3") test.Fail() return } buaddr[i] = v ^ (01 << j) if primitives.ValidateECUserStr(string(buaddr)) { fmt.Printf("4") test.Fail() return } buaddr[i] = v } } }