Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
feat: catch 404, remove circ dep
Browse files Browse the repository at this point in the history
  • Loading branch information
zachferland committed May 27, 2020
1 parent 5b87684 commit 5341310
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
},
"homepage": "https://github.com/3box/3id-connect#readme",
"dependencies": {
"3box": "^1.19.0-rc.1",
"3id-blockchain-utils": "github:ceramicnetwork/js-3id-blockchain-utils#feat/ethereum-auth-function",
"@babel/runtime": "^7.1.2",
"identity-wallet": "github:3box/identity-wallet-js#feat/3id-connect-v2",
Expand Down
12 changes: 2 additions & 10 deletions src/threeIdConnectService.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import { expose, caller } from 'postmsg-rpc'
import { fakeIpfs } from 'identity-wallet/lib/utils'
const IdentityWallet = require('identity-wallet')
const API = require('3box/lib/api.js')
const Url = require('url-parse')
const store = require('store')
const { isLinked } = require('./utils')

const consentKey = (address, domain, space) => `3id_consent_${address}_${domain}_${space}`
const serializedKey = (address) => `serialized3id_${address}`

const mobileRegex = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i
const checkIsMobile = () => mobileRegex.test(navigator.userAgent)

async function getLinkedData(address) {
try {
return API.getRootStoreAddress(address)
} catch (err) {
return null
}
}

/**
* ThreeIdConnectService runs an identity wallet instance and rpc server with
* bindings to receive and relay rpc messages to identity wallet
Expand Down Expand Up @@ -76,7 +68,7 @@ class ThreeIdConnectService {
const cached3id = this._get3idState(address)

if (!cached3id) {
this.linkPromise = getLinkedData(address)
this.linkPromise = isLinked(address)
}

const diffSpaces = this._diff3idState(cached3id, address, spaces)
Expand Down
36 changes: 36 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Partically redundant with 3boxjs utils, but added to remove circular dependency entirely for now

const HTTPError = (status, message) => {
const e = new Error(message)
e.statusCode = status
return e
}

const fetchJson = async (url, body) => {
let opts
if (body) {
opts = { body: JSON.stringify(body), method: 'POST', headers: { 'Content-Type': 'application/json' } }
}
const r = await window.fetch(url, opts)

if (r.ok) {
let res = await r.json()
return res
} else {
throw HTTPError(r.status, (await r.json()).message)
}
}

const isLinked = async (address) => {
try {
const res = await fetchJson(`https://beta.3box.io/address-server/odbAddress/${address}`)
return Boolean(res.data.rootStoreAddress)
} catch (err) {
return false
}
}

export {
fetchJson,
isLinked
}

0 comments on commit 5341310

Please sign in to comment.