From 6abc2fa3bd0cdf5e66621aaac7546e088dea9ed4 Mon Sep 17 00:00:00 2001 From: Joel Torstensson Date: Tue, 5 Feb 2019 21:07:45 +0100 Subject: [PATCH] ref(api): Move getVerifiedAccounts to api --- README.md | 2 +- readme-template.md | 2 +- src/3box.js | 23 +---------------------- src/api.js | 27 ++++++++++++++++++++++++++- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 3230257e..db5bc3de 100644 --- a/README.md +++ b/README.md @@ -189,7 +189,7 @@ This runs a simple server at `http://localhost:3000/` that serves the static `ex If you only want to fetch profile data from 3Box's profile APIs you can optimize by importing only those functions or the API specific dist file. Since this includes minimal dependencies, file size is ~ 80kb vs 4+mb for the full build. ```js -const { profileGraphQL, getProfile, getProfiles } = require('3box/lib/api') +const { profileGraphQL, getProfile, getProfiles, getVerifiedAccounts } = require('3box/lib/api') ``` ```html diff --git a/readme-template.md b/readme-template.md index 147e6721..cadf5169 100644 --- a/readme-template.md +++ b/readme-template.md @@ -189,7 +189,7 @@ This runs a simple server at `http://localhost:3000/` that serves the static `ex If you only want to fetch profile data from 3Box's profile APIs you can optimize by importing only those functions or the API specific dist file. Since this includes minimal dependencies, file size is ~ 80kb vs 4+mb for the full build. ```js -const { profileGraphQL, getProfile, getProfiles } = require('3box/lib/api') +const { profileGraphQL, getProfile, getProfiles, getVerifiedAccounts } = require('3box/lib/api') ``` ```html diff --git a/src/3box.js b/src/3box.js index 7ab0724b..1a8858c6 100644 --- a/src/3box.js +++ b/src/3box.js @@ -11,7 +11,6 @@ const PrivateStore = require('./privateStore') const Verified = require('./verified') const Space = require('./space') const utils = require('./utils/index') -const verifier = require('./utils/verifier') const config = require('./config.js') const API = require('./api') @@ -281,27 +280,7 @@ class Box { * @return {Object} An object containing the accounts that have been verified */ static async getVerifiedAccounts (profile) { - let verifs = {} - try { - const did = await verifier.verifyDID(profile.proof_did) - if (profile.proof_github) { - try { - verifs.github = await verifier.verifyGithub(did, profile.proof_github) - } catch (err) { - // Invalid github verification - } - } - if (profile.proof_twitter) { - try { - verifs.twitter = await verifier.verifyTwitter(did, profile.proof_twitter) - } catch (err) { - // Invalid twitter verification - } - } - } catch (err) { - // Invalid proof for DID return an empty profile - } - return verifs + return API.getVerifiedAccounts(profile) } /** diff --git a/src/api.js b/src/api.js index 1ae05b9c..9edc1ae0 100644 --- a/src/api.js +++ b/src/api.js @@ -1,5 +1,6 @@ const graphQLRequest = require('graphql-request').request const utils = require('./utils/index') +const verifier = require('./utils/verifier') const config = require('./config.js') const GRAPHQL_SERVER_URL = config.graphql_server_url @@ -39,4 +40,28 @@ async function profileGraphQL (query, opts = {}) { return graphQLRequest(opts.graphqlServer, query) } -module.exports = { profileGraphQL, getProfile, getRootStoreAddress, getProfiles } +async function getVerifiedAccounts (profile) { + let verifs = {} + try { + const did = await verifier.verifyDID(profile.proof_did) + if (profile.proof_github) { + try { + verifs.github = await verifier.verifyGithub(did, profile.proof_github) + } catch (err) { + // Invalid github verification + } + } + if (profile.proof_twitter) { + try { + verifs.twitter = await verifier.verifyTwitter(did, profile.proof_twitter) + } catch (err) { + // Invalid twitter verification + } + } + } catch (err) { + // Invalid proof for DID return an empty profile + } + return verifs +} + +module.exports = { profileGraphQL, getProfile, getRootStoreAddress, getProfiles, getVerifiedAccounts }