Skip to content

Commit

Permalink
Fix HBS and springboard configuration to work with Vite and Vue (#67)
Browse files Browse the repository at this point in the history
* swap kyc logic into holo & holochain stores

* break out holoport API store

* rework springboard modal

* rework env vars

* rework env vars

* refactor to pass environment vars to hbs calls

* fix compile issue

* PR suggestions
  • Loading branch information
Paterick authored Sep 15, 2023
1 parent 5ae75e4 commit bc4cc46
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 166 deletions.
22 changes: 10 additions & 12 deletions src/components/GoToSpringboardModal.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<BaseModal :is-visible="visibleModal === EModal.go_to_springboard" @close="hideModal">
<BaseModal :is-visible="true" @close="closeModal">
<div class="go-to-springboard-modal">
<img src="/images/holo-logo-2.png" class="go-to-springboard-modal__springboard-logo" alt="springboard-logo" />

Expand Down Expand Up @@ -32,34 +32,32 @@
</template>

<script setup>
import { ref } from 'vue'
import { useModals } from '../composables/useModals'
import { EButtonType, EModal } from '../types/ui'
import { EButtonType } from '../types/ui'
import BaseButton from './BaseButton.vue'
import BaseModal from './BaseModal.vue'
const { visibleModal, hideModal } = useModals()
import { springBoardUrl } from '../utils/springboardConfiguration'
const props = defineProps({
appName: {
type: String,
required: true
},
springboardUrl: {
environment: {
type: String,
required: true
}
}
})
const emit = defineEmits(['login'])
function handleSpringboardLogin() {
hideModal()
emit('login')
const tabName = `${props.appName}-springboard`
window.open(`${springBoardUrl(props.environment)}/home?kyc=true`, tabName).focus()
}
function closeModal() {
emit('login')
const tabName = `${props.appName}-hf`
window.open(props.springboardUrl, tabName).focus()
}
</script>
Expand Down
63 changes: 30 additions & 33 deletions src/services/hbs.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
import axios from 'axios'
import { AUTH_SERVICE_URL, AUTH_SERVICE_VERSION } from '../utils/hbsConfiguration'
import { authServiceUrl, authServiceVersion } from '../utils/hbsConfiguration'
import { httpCall } from '../utils/httpProvider'
import { useHoloStore } from 'src/stores'

async function authCall(args) {
export const kycLevel1 = 'holo_kyc_1'
export const kycLevel2 = 'holo_kyc_2'

async function authCall(args, envirionment, hbsServicePort) {
return httpCall({
serviceUrl: AUTH_SERVICE_URL,
version: AUTH_SERVICE_VERSION,
serviceUrl: authServiceUrl(envirionment),
version: authServiceVersion(hbsServicePort),
method: 'post',
...args
})
}

export async function authenticateAgent(email, public_key) {
console.log(`hbs->authenticateAgent - email: ${email} public_key: ${public_key}`)

const holoStore = useHoloStore();
const payload = {
"email": email,
"timestamp": Date.now() - (30 * 1000), // Subtract 30 sec to prevent "future" timestamp error from API
"pubKey": public_key
}

const { _, signature } = await holoStore.signPayload(payload)
console.log(`authenticateAgent - signature: ${signature}`, payload)
export async function authenticateAgent(payload, signature, envirionment, hbsServicePort) {
try {
const result = await authCall({
params: payload,
endpoint: 'holo-client',
headers: {
'X-Signature': signature
},
envirionment,
hbsServicePort
})

try {
const result = await authCall({
params: payload,
endpoint: 'holo-client',
headers: {
'X-Signature': signature
}
})

return result.data
} catch (e) {
if (axios.isAxiosError(e)) {
return e.message
} else {
return 'unknown error'
}
return result.data
} catch (e) {
if (axios.isAxiosError(e)) {
return e.message
} else {
return 'unknown error'
}
}
}

export async function fetchAgentKycLevel(payload, signature, envirionment, hbsServicePort) {
const authResult = await authenticateAgent(payload, signature, envirionment, hbsServicePort)
return (authResult && authResult.kyc) ? (authResult.kyc === kycLevel2) ? 2 : 1 : null
}

12 changes: 10 additions & 2 deletions src/stores/useClientStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { encodeAgentId } from '../utils/agent'
const makeUseClientStore = ({ useInterfaceStore, onInit }) => defineStore('client', {
state: () => ({
agentKey: null, // the Uint8Array of raw bytes. See also agentId in getters, below
isReady: false
isReady: false,
agentKyc: null
}),
getters: {
agentId: state => state.agentKey && encodeAgentId(state.agentKey)
agentId: state => state.agentKey && encodeAgentId(state.agentKey),
agentKycLevel: state => state.agentKyc
},
actions: {
async initialize() {
Expand Down Expand Up @@ -45,6 +47,12 @@ const makeUseClientStore = ({ useInterfaceStore, onInit }) => defineStore('clien

return result
},

async loadAgentKycLevel(envirionment, hbsServicePort) {
const kycLevel = await useInterfaceStore().loadAgentKycLevel(envirionment, hbsServicePort)
this.agentKyc = kycLevel
return kycLevel
}
}
})

Expand Down
35 changes: 0 additions & 35 deletions src/stores/useHoloBusinessServiceStore.js

This file was deleted.

20 changes: 15 additions & 5 deletions src/stores/useHoloStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import WebSdk from '@holo-host/web-sdk'
import { defineStore } from 'pinia'
import useIsLoadingStore from './useIsLoadingStore'
import useSignalStore from './useSignalStore'
import { fetchAgentKycLevel } from '../services/hbs'

let client

Expand All @@ -13,7 +14,8 @@ const makeUseHoloStore = ({ connectionArgs, MockWebSdk }) => defineStore('holo',
isAuthFormOpen: false,
// These two values are subscribed to by clientStore
isReady: false,
appInfo: null
appInfo: null,
kycLevel: null
}),
getters: {
isAnonymous: state => state.agentState && state.agentState.isAnonymous,
Expand All @@ -22,7 +24,8 @@ const makeUseHoloStore = ({ connectionArgs, MockWebSdk }) => defineStore('holo',
error: state => state.agentState && !state.agentState.isAvailable && (state.connectionError || state.agentState.unrecoverableError),
agentKey: (state) => state.appInfo?.agent_pub_key,
agentId: state => state.agentState?.id,
agentEmail: state => state.agentState?.email
agentEmail: state => state.agentState?.email,
agentKycLevel: state => state.kycLevel
},
actions: {
async initialize() {
Expand Down Expand Up @@ -99,11 +102,18 @@ const makeUseHoloStore = ({ connectionArgs, MockWebSdk }) => defineStore('holo',
this.appInfo = await client.appInfo()
return this.appInfo
},
async loadAgentKycLevel(envirionment, hbsServicePort) {
const payload = {
"email": this.agentEmail,
"timestamp": Date.now() - (30 * 1000), // Subtract 30 sec to prevent "future" timestamp error from API
"pubKey": this.agentId
}

async signPayload(payload) {
return await client.signPayload(payload)
const { _, signature } = await client.signPayload(payload)
const kycLevel = await fetchAgentKycLevel(payload, signature, envirionment, hbsServicePort)
this.kycLevel = kycLevel
return kycLevel
}

}
})

Expand Down
80 changes: 11 additions & 69 deletions src/stores/useHolochainStore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { inspect } from 'util'
import axios from 'axios'
import { AdminWebsocket, AppWebsocket, generateSigningKeyPair, setSigningCredentials } from '@holochain/client'
import { defineStore } from 'pinia'
import { presentHcSignal, listify } from '../utils'
Expand Down Expand Up @@ -105,79 +104,22 @@ const makeUseHolochainStore = ({ installed_app_id, app_ws_url, is_hpos_served, h

return result
},
async zomeCall(args) {
const zomeCallArgs = {
appId: installed_app_id,
roleId: args.role_name,
zomeName: args.zome_name,
fnName: args.fn_name,
payload: args.payload
}

const response = await this.hposHolochainCall({path: 'zome_call', headers: {}, params: zomeCallArgs})
return response
},
setCredentials(cellId) {
this.signingCredentials = new Promise(async (resolve, reject) => {
if( !is_hpos_served ) { // If running a raw holochain we need to authorize zome calls once
try {
const adminWs = await AdminWebsocket.connect(`ws:localhost:${hc_admin_port}`)
await adminWs.authorizeSigningCredentials(cellId)
} catch(e) {
console.log(`holochainCallZome error authorizeSigningCredentials AdminWebsocket: ws:localhost:${hc_admin_port}`, e)
reject()
}

resolve()
} else {
try {
const [keyPair, signingKey] = await generateSigningKeyPair()
const params = { cellId, signingKey }
const cap_token = await this.hposHolochainCall({path: 'cap_token', headers: {}, params})

const signingCredentials = {
capSecret: new Uint8Array(listify(cap_token, (_, value) => (Number(value)))),
keyPair,
signingKey
}

await setSigningCredentials(cellId, signingCredentials)
} catch (e) {
console.log(`Error setting signing credentials`, e)
reject()
}

resolve()
try {
const adminWs = await AdminWebsocket.connect(`ws:localhost:${hc_admin_port}`)
await adminWs.authorizeSigningCredentials(cellId)
} catch(e) {
console.log(`holochainCallZome error authorizeSigningCredentials AdminWebsocket: ws:localhost:${hc_admin_port}`, e)
reject()
}

resolve()
})
},
async hposHolochainCall({
path,
headers: userHeaders = {},
params
}) {
const axiosConfig = {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
}

const HPOS_API_URL = `${window.location.protocol}//${window.location.host}`
const pathPrefix = '/holochain-api/v1/'
const fullUrl = `${HPOS_API_URL}${pathPrefix}${path}`

const authToken = localStorage.getItem('authToken')

const headers = {
'X-Hpos-Auth-Token': authToken,
...axiosConfig.headers,
...userHeaders
}

const response = await axios.post(fullUrl, params, { headers })
return response.data
}
async loadAgentKycLevel(_, __) {
return null // raw holochain doesn't have a mechanism for fetching agent's kyc level
},
}
})

Expand Down
Loading

0 comments on commit bc4cc46

Please sign in to comment.