Skip to content

Commit

Permalink
Merge pull request #874 from sharetribe/improve-clientside-coord-obfu…
Browse files Browse the repository at this point in the history
…scation

Seeded random used on obfuscatedCoordinatesImpl
  • Loading branch information
Gnito authored Jul 27, 2018
2 parents f1c4228 + ff7a2f0 commit a032ad2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ way to update this template, but currently, we follow a pattern:

---

## Upcoming version
* [change] Use seeded random for client side coordinate obfuscation
[#874](https://github.com/sharetribe/flex-template-web/pull/874)

## v1.2.2
* [change] Change static map to dynamic map when clicked.
[#871](https://github.com/sharetribe/flex-template-web/pull/871)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"redux": "^4.0.0",
"redux-thunk": "^2.2.0",
"sanitize.css": "^5.0.0",
"seedrandom": "^2.4.3",
"sharetribe-scripts": "1.1.2",
"sharetribe-sdk": "https://github.com/sharetribe/sharetribe-sdk-js.git#b7ffe0fe0a5bdb0a1471e2ff36cdd25b997fb39b",
"smoothscroll-polyfill": "^0.4.0",
Expand Down
23 changes: 20 additions & 3 deletions src/util/maps.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
import memoize from 'lodash/memoize';
import seedrandom from 'seedrandom';
import { types as sdkTypes } from './sdkLoader';
import config from '../config';

const { LatLng } = sdkTypes;

const obfuscatedCoordinatesImpl = latlng => {
/**
* This obfuscatedCoordinatesImpl function is a temporary solution for the coordinate obfuscation.
* In the future, improved version needs to have protectedData working and
* available in accepted transaction.
*/
const obfuscatedCoordinatesImpl = (latlng, cacheKey) => {
const { lat, lng } = latlng;
const offset = config.coordinates.coordinateOffset;

// https://gis.stackexchange.com/questions/25877/generating-random-locations-nearby
const r = offset / 111300;
const y0 = lat;
const x0 = lng;
const u = Math.random();
const v = Math.random();

// Two seeded random numbers to be used to calculate new location
// We need seeded so that the static map URL doesn't change between requests
// (i.e. URL is cacheable)
const u = cacheKey ? seedrandom(cacheKey)() : Math.random();
const v = cacheKey
? seedrandom(
cacheKey
.split('')
.reverse()
.join('')
)()
: Math.random();
const w = r * Math.sqrt(u);
const t = 2 * Math.PI * v;
const x = w * Math.cos(t);
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7406,6 +7406,10 @@ scriptjs@^2.5.8:
version "2.5.8"
resolved "https://registry.yarnpkg.com/scriptjs/-/scriptjs-2.5.8.tgz#d0c43955c2e6bad33b6e4edf7b53b8965aa7ca5f"

seedrandom@^2.4.3:
version "2.4.3"
resolved "https://registry.yarnpkg.com/seedrandom/-/seedrandom-2.4.3.tgz#2438504dad33917314bff18ac4d794f16d6aaecc"

select-hose@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/select-hose/-/select-hose-2.0.0.tgz#625d8658f865af43ec962bfc376a37359a4994ca"
Expand Down

0 comments on commit a032ad2

Please sign in to comment.