This commit is contained in:
hatim boufnichel 2022-05-15 21:24:50 +02:00
parent 8bcb3a7e85
commit de2755f8ed
173 changed files with 47169 additions and 20113 deletions

View file

@ -0,0 +1,60 @@
// This file was autogenerated from a .proto file, DO NOT EDIT!
import express, { Response } from 'express'
import * as Types from './types'
export type Logger = { log: (v: any) => void, error: (v: any) => void }
export type ServerOptions = {
allowNotImplementedMethods?: number
logger?: Logger
throwErrors?: true
NoAuthAuthGuard: (authorizationHeader?: string) => Promise<Types.NoAuthContext>
GuestAuthGuard: (authorizationHeader?: string) => Promise<Types.GuestContext>
AdminAuthGuard: (authorizationHeader?: string) => Promise<Types.AdminContext>
encryptionCallback: (ctx: Types.AuthContext, body: any) => Promise<string>
}
const logErrorAndReturnResponse = (error: Error, response: string, res: Response, logger: Logger) => { logger.error(error.message || error); res.json({ status: 'ERROR', reason: response }) }
export default (methods: Types.ServerMethods, opts: ServerOptions) => {
const logger = opts.logger || { log: console.log, error: console.error }
const app = express()
if (!opts.allowNotImplementedMethods && !methods.Health) throw new Error('method: Health is not implemented')
app.get('/health', async (req, res) => {
try {
if (!methods.Health) throw new Error('method: Health is not implemented')
const authContext = await opts.NoAuthAuthGuard(req.headers['authorization'])
const query = req.query
const params = req.params
await methods.Health({ ...authContext, ...query, ...params })
res.json({ status: 'OK' })
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.EncryptionExchange) throw new Error('method: EncryptionExchange is not implemented')
app.post('/api/encryption/exchange', async (req, res) => {
try {
if (!methods.EncryptionExchange) throw new Error('method: EncryptionExchange is not implemented')
const authContext = await opts.NoAuthAuthGuard(req.headers['authorization'])
const request = req.body
const error = Types.EncryptionExchangeRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
await methods.EncryptionExchange({ ...authContext, ...query, ...params }, request)
res.json({ status: 'OK' })
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.LndGetInfo) throw new Error('method: LndGetInfo is not implemented')
app.get('/api/lnd/getinfo', async (req, res) => {
try {
if (!methods.LndGetInfo) throw new Error('method: LndGetInfo is not implemented')
const authContext = await opts.NoAuthAuthGuard(req.headers['authorization'])
const query = req.query
const params = req.params
const response = await methods.LndGetInfo({ ...authContext, ...query, ...params })
res.json({ status: 'OK', result: response })
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
})
var server: { close: () => void } | undefined
return {
Close: () => { if (!server) { throw new Error('tried closing server before starting') } else server.close() },
Listen: (port: number) => { server = app.listen(port, () => logger.log('Example app listening on port ' + port)) }
}
}

View file

@ -0,0 +1,44 @@
// This file was autogenerated from a .proto file, DO NOT EDIT!
import axios from 'axios'
import * as Types from './types'
export type ResultError = { status: 'ERROR', reason: string }
export type ClientParams = {
baseUrl: string
retrieveNoAuthAuth: () => Promise<string | null>
retrieveGuestAuth: () => Promise<string | null>
retrieveAdminAuth: () => Promise<string | null>
}
export default (params: ClientParams) => ({
Health: async (): Promise<ResultError | { status: 'OK' }> => {
const auth = await params.retrieveNoAuthAuth()
if (auth === null) throw new Error('retrieveNoAuthAuth() returned null')
const { data } = await axios.get(params.baseUrl + '/health', { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
return data
}
return { status: 'ERROR', reason: 'invalid response' }
},
EncryptionExchange: async (request: Types.EncryptionExchangeRequest): Promise<ResultError | { status: 'OK' }> => {
const auth = await params.retrieveNoAuthAuth()
if (auth === null) throw new Error('retrieveNoAuthAuth() returned null')
const { data } = await axios.post(params.baseUrl + '/api/encryption/exchange', request, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
return data
}
return { status: 'ERROR', reason: 'invalid response' }
},
LndGetInfo: async (): Promise<ResultError | { status: 'OK', result: Types.Empty }> => {
const auth = await params.retrieveNoAuthAuth()
if (auth === null) throw new Error('retrieveNoAuthAuth() returned null')
const { data } = await axios.get(params.baseUrl + '/api/lnd/getinfo', { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const error = Types.LndGetInfoResponseValidate(data.result)
if (error === null) { return data } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
})

View file

@ -0,0 +1,92 @@
// This file was autogenerated from a .proto file, DO NOT EDIT!
export type NoAuthContext = {
}
export type GuestContext = {
token: string
}
export type AdminContext = {
pub: string
}
export type AuthContext = NoAuthContext | GuestContext | AdminContext
export type Health_Query = {
}
export type Health_RouteParams = {
}
export type Health_Context = Health_Query & Health_RouteParams & NoAuthContext
export type EncryptionExchange_Query = {
}
export type EncryptionExchange_RouteParams = {
}
export type EncryptionExchange_Context = EncryptionExchange_Query & EncryptionExchange_RouteParams & NoAuthContext
export type LndGetInfo_Query = {
}
export type LndGetInfo_RouteParams = {
}
export type LndGetInfo_Context = LndGetInfo_Query & LndGetInfo_RouteParams & NoAuthContext
export type ServerMethods = {
Health?: (ctx: Health_Context) => Promise<void>
EncryptionExchange?: (ctx: EncryptionExchange_Context, req: EncryptionExchangeRequest) => Promise<void>
LndGetInfo?: (ctx: LndGetInfo_Context) => Promise<LndGetInfoResponse>
}
export type OptionsBaseMessage = {
allOptionalsAreSet?: true
}
export type LndGetInfoResponse = {
alias: string
}
export const LndGetInfoResponseOptionalFields: [] = []
export type LndGetInfoResponseOptions = OptionsBaseMessage & {
checkOptionalsAreSet?: []
alias_CustomCheck?: (v: string) => boolean
}
export const LndGetInfoResponseValidate = (o?: LndGetInfoResponse, opts: LndGetInfoResponseOptions = {}, path: string = 'LndGetInfoResponse::root.'): Error | null => {
if (opts.checkOptionalsAreSet && opts.allOptionalsAreSet) return new Error(path + ': only one of checkOptionalsAreSet or allOptionalNonDefault can be set for each message')
if (typeof o !== 'object' || o === null) return new Error(path + ': object is not an instance of an object or is null')
if (typeof o.alias !== 'string') return new Error(`${path}.alias: is not a string`)
if (opts.alias_CustomCheck && !opts.alias_CustomCheck(o.alias)) return new Error(`${path}.alias: custom check failed`)
return null
}
export type Empty = {
}
export const EmptyOptionalFields: [] = []
export type EmptyOptions = OptionsBaseMessage & {
checkOptionalsAreSet?: []
}
export const EmptyValidate = (o?: Empty, opts: EmptyOptions = {}, path: string = 'Empty::root.'): Error | null => {
if (opts.checkOptionalsAreSet && opts.allOptionalsAreSet) return new Error(path + ': only one of checkOptionalsAreSet or allOptionalNonDefault can be set for each message')
if (typeof o !== 'object' || o === null) return new Error(path + ': object is not an instance of an object or is null')
return null
}
export type EncryptionExchangeRequest = {
public_key: string
device_id: string
}
export const EncryptionExchangeRequestOptionalFields: [] = []
export type EncryptionExchangeRequestOptions = OptionsBaseMessage & {
checkOptionalsAreSet?: []
public_key_CustomCheck?: (v: string) => boolean
device_id_CustomCheck?: (v: string) => boolean
}
export const EncryptionExchangeRequestValidate = (o?: EncryptionExchangeRequest, opts: EncryptionExchangeRequestOptions = {}, path: string = 'EncryptionExchangeRequest::root.'): Error | null => {
if (opts.checkOptionalsAreSet && opts.allOptionalsAreSet) return new Error(path + ': only one of checkOptionalsAreSet or allOptionalNonDefault can be set for each message')
if (typeof o !== 'object' || o === null) return new Error(path + ': object is not an instance of an object or is null')
if (typeof o.public_key !== 'string') return new Error(`${path}.public_key: is not a string`)
if (opts.public_key_CustomCheck && !opts.public_key_CustomCheck(o.public_key)) return new Error(`${path}.public_key: custom check failed`)
if (typeof o.device_id !== 'string') return new Error(`${path}.device_id: is not a string`)
if (opts.device_id_CustomCheck && !opts.device_id_CustomCheck(o.device_id)) return new Error(`${path}.device_id: custom check failed`)
return null
}