ready for migration

This commit is contained in:
hatim 2023-05-08 21:31:05 +02:00
parent 46819446d5
commit c050de214a
42 changed files with 3903 additions and 2262 deletions

File diff suppressed because it is too large Load diff

View file

@ -7,12 +7,13 @@ export type Logger = { log: (v: any) => void, error: (v: any) => void }
export type ServerOptions = {
allowCors?: true
staticFiles?: string
allowNotImplementedMethods?: true
allowNotImplementedMethods?: number
logger?: Logger
throwErrors?: true
GuestAuthGuard: (authorizationHeader?: string) => Promise<Types.GuestContext>
UserAuthGuard: (authorizationHeader?: string) => Promise<Types.UserContext>
AdminAuthGuard: (authorizationHeader?: string) => Promise<Types.AdminContext>
AppAuthGuard: (authorizationHeader?: string) => Promise<Types.AppContext>
decryptCallback: (encryptionDeviceId: string, body: any) => Promise<any>
encryptCallback: (encryptionDeviceId: string, plain: any) => Promise<any>
}
@ -51,19 +52,129 @@ export default (methods: Types.ServerMethods, opts: ServerOptions) => {
} 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.post('/api/lnd/getinfo', async (req, res) => {
app.post('/api/admin/lnd/getinfo', async (req, res) => {
try {
if (!methods.LndGetInfo) throw new Error('method: LndGetInfo is not implemented')
const authContext = await opts.AdminAuthGuard(req.headers['authorization'])
const encryptionDeviceId = req.headers['x-e2ee-device-id-x']
if (typeof encryptionDeviceId !== 'string' || encryptionDeviceId === '') throw new Error('invalid encryption header provided')
const request = await opts.decryptCallback(encryptionDeviceId, req.body)
const request = req.body
const error = Types.LndGetInfoRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.LndGetInfo({ ...authContext, ...query, ...params }, request)
res.json({status: 'OK', ...await opts.encryptCallback(encryptionDeviceId, response)})
res.json({status: 'OK', ...response})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.AddApp) throw new Error('method: AddApp is not implemented')
app.post('/api/admin/app/add', async (req, res) => {
try {
if (!methods.AddApp) throw new Error('method: AddApp is not implemented')
const authContext = await opts.AdminAuthGuard(req.headers['authorization'])
const request = req.body
const error = Types.AddAppRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.AddApp({ ...authContext, ...query, ...params }, request)
res.json({status: 'OK', ...response})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.AddAppUser) throw new Error('method: AddAppUser is not implemented')
app.post('/api/app/user/add', async (req, res) => {
try {
if (!methods.AddAppUser) throw new Error('method: AddAppUser is not implemented')
const authContext = await opts.AppAuthGuard(req.headers['authorization'])
const request = req.body
const error = Types.AddAppUserRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.AddAppUser({ ...authContext, ...query, ...params }, request)
res.json({status: 'OK', ...response})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.AddAppInvoice) throw new Error('method: AddAppInvoice is not implemented')
app.post('/api/app/add/invoice', async (req, res) => {
try {
if (!methods.AddAppInvoice) throw new Error('method: AddAppInvoice is not implemented')
const authContext = await opts.AppAuthGuard(req.headers['authorization'])
const request = req.body
const error = Types.AddAppInvoiceRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.AddAppInvoice({ ...authContext, ...query, ...params }, request)
res.json({status: 'OK', ...response})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.AddAppUserInvoice) throw new Error('method: AddAppUserInvoice is not implemented')
app.post('/api/app/user/add/invoice', async (req, res) => {
try {
if (!methods.AddAppUserInvoice) throw new Error('method: AddAppUserInvoice is not implemented')
const authContext = await opts.AppAuthGuard(req.headers['authorization'])
const request = req.body
const error = Types.AddAppUserInvoiceRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.AddAppUserInvoice({ ...authContext, ...query, ...params }, request)
res.json({status: 'OK', ...response})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.GetAppUser) throw new Error('method: GetAppUser is not implemented')
app.post('/api/app/user/get', async (req, res) => {
try {
if (!methods.GetAppUser) throw new Error('method: GetAppUser is not implemented')
const authContext = await opts.AppAuthGuard(req.headers['authorization'])
const request = req.body
const error = Types.GetAppUserRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.GetAppUser({ ...authContext, ...query, ...params }, request)
res.json({status: 'OK', ...response})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.PayAppUserInvoice) throw new Error('method: PayAppUserInvoice is not implemented')
app.post('/api/app/invoice/pay', async (req, res) => {
try {
if (!methods.PayAppUserInvoice) throw new Error('method: PayAppUserInvoice is not implemented')
const authContext = await opts.AppAuthGuard(req.headers['authorization'])
const request = req.body
const error = Types.PayAppUserInvoiceRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.PayAppUserInvoice({ ...authContext, ...query, ...params }, request)
res.json({status: 'OK', ...response})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.SendAppUserToAppUserPayment) throw new Error('method: SendAppUserToAppUserPayment is not implemented')
app.post('/api/app/user/internal/pay', async (req, res) => {
try {
if (!methods.SendAppUserToAppUserPayment) throw new Error('method: SendAppUserToAppUserPayment is not implemented')
const authContext = await opts.AppAuthGuard(req.headers['authorization'])
const request = req.body
const error = Types.SendAppUserToAppUserPaymentRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
await methods.SendAppUserToAppUserPayment({ ...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.SendAppUserToAppPayment) throw new Error('method: SendAppUserToAppPayment is not implemented')
app.post('/api/app/internal/pay', async (req, res) => {
try {
if (!methods.SendAppUserToAppPayment) throw new Error('method: SendAppUserToAppPayment is not implemented')
const authContext = await opts.AppAuthGuard(req.headers['authorization'])
const request = req.body
const error = Types.SendAppUserToAppPaymentRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
await methods.SendAppUserToAppPayment({ ...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.AddUser) throw new Error('method: AddUser is not implemented')

View file

@ -8,6 +8,7 @@ export type ClientParams = {
retrieveGuestAuth: () => Promise<string | null>
retrieveUserAuth: () => Promise<string | null>
retrieveAdminAuth: () => Promise<string | null>
retrieveAppAuth: () => Promise<string | null>
encryptCallback: (plain: any) => Promise<any>
decryptCallback: (encrypted: any) => Promise<any>
deviceId: string
@ -39,17 +40,123 @@ export default (params: ClientParams) => ({
LndGetInfo: async (request: Types.LndGetInfoRequest): Promise<ResultError | ({ status: 'OK' }& Types.LndGetInfoResponse)> => {
const auth = await params.retrieveAdminAuth()
if (auth === null) throw new Error('retrieveAdminAuth() returned null')
let finalRoute = '/api/lnd/getinfo'
const { data } = await axios.post(params.baseUrl + finalRoute, await params.encryptCallback(request), { headers: { 'authorization': auth, 'x-e2ee-device-id-x': params.deviceId } })
let finalRoute = '/api/admin/lnd/getinfo'
const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const result = await params.decryptCallback(data)
const result = data
if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.LndGetInfoResponseValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
AddApp: async (request: Types.AddAppRequest): Promise<ResultError | ({ status: 'OK' }& Types.AddAppResponse)> => {
const auth = await params.retrieveAdminAuth()
if (auth === null) throw new Error('retrieveAdminAuth() returned null')
let finalRoute = '/api/admin/app/add'
const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const result = data
if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.AddAppResponseValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
AddAppUser: async (request: Types.AddAppUserRequest): Promise<ResultError | ({ status: 'OK' }& Types.AppUser)> => {
const auth = await params.retrieveAppAuth()
if (auth === null) throw new Error('retrieveAppAuth() returned null')
let finalRoute = '/api/app/user/add'
const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const result = data
if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.AppUserValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
AddAppInvoice: async (request: Types.AddAppInvoiceRequest): Promise<ResultError | ({ status: 'OK' }& Types.NewInvoiceResponse)> => {
const auth = await params.retrieveAppAuth()
if (auth === null) throw new Error('retrieveAppAuth() returned null')
let finalRoute = '/api/app/add/invoice'
const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const result = data
if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.NewInvoiceResponseValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
AddAppUserInvoice: async (request: Types.AddAppUserInvoiceRequest): Promise<ResultError | ({ status: 'OK' }& Types.NewInvoiceResponse)> => {
const auth = await params.retrieveAppAuth()
if (auth === null) throw new Error('retrieveAppAuth() returned null')
let finalRoute = '/api/app/user/add/invoice'
const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const result = data
if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.NewInvoiceResponseValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
GetAppUser: async (request: Types.GetAppUserRequest): Promise<ResultError | ({ status: 'OK' }& Types.AppUser)> => {
const auth = await params.retrieveAppAuth()
if (auth === null) throw new Error('retrieveAppAuth() returned null')
let finalRoute = '/api/app/user/get'
const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const result = data
if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.AppUserValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
PayAppUserInvoice: async (request: Types.PayAppUserInvoiceRequest): Promise<ResultError | ({ status: 'OK' }& Types.PayAppUserInvoiceResponse)> => {
const auth = await params.retrieveAppAuth()
if (auth === null) throw new Error('retrieveAppAuth() returned null')
let finalRoute = '/api/app/invoice/pay'
const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const result = data
if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.PayAppUserInvoiceResponseValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
SendAppUserToAppUserPayment: async (request: Types.SendAppUserToAppUserPaymentRequest): Promise<ResultError | ({ status: 'OK' })> => {
const auth = await params.retrieveAppAuth()
if (auth === null) throw new Error('retrieveAppAuth() returned null')
let finalRoute = '/api/app/user/internal/pay'
const { data } = await axios.post(params.baseUrl + finalRoute, 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' }
},
SendAppUserToAppPayment: async (request: Types.SendAppUserToAppPaymentRequest): Promise<ResultError | ({ status: 'OK' })> => {
const auth = await params.retrieveAppAuth()
if (auth === null) throw new Error('retrieveAppAuth() returned null')
let finalRoute = '/api/app/internal/pay'
const { data } = await axios.post(params.baseUrl + finalRoute, 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' }
},
AddUser: async (request: Types.AddUserRequest): Promise<ResultError | ({ status: 'OK' }& Types.AddUserResponse)> => {
const auth = await params.retrieveGuestAuth()
if (auth === null) throw new Error('retrieveGuestAuth() returned null')

File diff suppressed because it is too large Load diff

BIN
proto/protoc-gen-pub.exe → proto/protoc-gen-pub Normal file → Executable file

Binary file not shown.

View file

@ -25,11 +25,19 @@ option (file_options) = {
{
id: "admin",
name: "Admin",
encrypted:true,
//encrypted:true,
context:{
key:"admin_id",
value:"string"
}
},
{
id:"app",
name:"App",
context:{
key:"app_id",
value: "string"
}
}
];
};
@ -73,11 +81,65 @@ service LightningPub {
option (http_method) = "post";
option (http_route) = "/api/encryption/exchange";
};
rpc LndGetInfo(structs.LndGetInfoRequest) returns (structs.LndGetInfoResponse){
option (auth_type) = "Admin";
option (http_method) = "post";
option (http_route) = "/api/lnd/getinfo";
option (http_route) = "/api/admin/lnd/getinfo";
};
// <App>
rpc AddApp(structs.AddAppRequest) returns (structs.AddAppResponse) {
option (auth_type) = "Admin";
option (http_method) = "post";
option (http_route) = "/api/admin/app/add";
};
rpc AddAppUser(structs.AddAppUserRequest)returns (structs.AppUser) {
option (auth_type) = "App";
option (http_method) = "post";
option (http_route) = "/api/app/user/add";
};
rpc AddAppInvoice(structs.AddAppInvoiceRequest) returns (structs.NewInvoiceResponse) {
option (auth_type) = "App";
option (http_method) = "post";
option (http_route) = "/api/app/add/invoice";
}
rpc AddAppUserInvoice(structs.AddAppUserInvoiceRequest) returns (structs.NewInvoiceResponse) {
option (auth_type) = "App";
option (http_method) = "post";
option (http_route) = "/api/app/user/add/invoice";
}
rpc GetAppUser(structs.GetAppUserRequest) returns (structs.AppUser) {
option (auth_type) = "App";
option (http_method) = "post";
option (http_route) = "/api/app/user/get";
}
rpc PayAppUserInvoice(structs.PayAppUserInvoiceRequest) returns (structs.PayAppUserInvoiceResponse) {
option (auth_type) = "App";
option (http_method) = "post";
option (http_route) = "/api/app/invoice/pay";
}
rpc SendAppUserToAppUserPayment(structs.SendAppUserToAppUserPaymentRequest) returns (structs.Empty) {
option (auth_type) = "App";
option (http_method) = "post";
option (http_route) = "/api/app/user/internal/pay";
}
rpc SendAppUserToAppPayment(structs.SendAppUserToAppPaymentRequest) returns (structs.Empty) {
option (auth_type) = "App";
option (http_method) = "post";
option (http_route) = "/api/app/internal/pay";
}
// </App>
rpc AddUser(structs.AddUserRequest)returns (structs.AddUserResponse){
option (auth_type) = "Guest";
option (http_method) = "post";

View file

@ -19,6 +19,69 @@ message LndGetInfoRequest {
message LndGetInfoResponse {
string alias = 1;
}
message AddAppRequest {
string name = 1;
}
message AddAppResponse {
string name = 1;
string id = 2;
string auth_token = 3;
}
message AddAppUserRequest {
string identifier = 1;
bool fail_if_exists = 2;
int64 balance = 3;
}
message AppUser {
string identifier = 1;
UserInfo info = 2;
int64 max_withdrawable = 3;
}
message AddAppInvoiceRequest {
string payer_identifier = 1;
string http_callback_url = 2;
NewInvoiceRequest invoice_req = 3;
}
message AddAppUserInvoiceRequest {
string receiver_identifier = 1;
string payer_identifier = 2;
string http_callback_url = 3;
NewInvoiceRequest invoice_req = 4;
}
message GetAppUserRequest {
string user_identifier = 1;
}
message PayAppUserInvoiceRequest {
string user_identifier = 1;
string invoice = 2;
int64 amount = 3;
}
message PayAppUserInvoiceResponse {
string preimage = 1;
int64 amount_paid = 2;
}
message SendAppUserToAppUserPaymentRequest {
string from_user_identifier = 1;
string to_user_identifier = 2;
int64 amount = 3;
}
message SendAppUserToAppPaymentRequest {
string from_user_identifier = 1;
int64 amount = 2;
}
enum AddressType {
WITNESS_PUBKEY_HASH = 0;
NESTED_PUBKEY_HASH = 1;
@ -61,6 +124,7 @@ message PayInvoiceRequest{
message PayInvoiceResponse{
string preimage = 1;
int64 amount_paid = 2;
}
message OpenChannelRequest{
@ -129,13 +193,18 @@ message GetUserOperationsRequest{
int64 latestOutgoingInvoice = 2;
int64 latestIncomingTx = 3;
int64 latestOutgoingTx = 4;
int64 latestIncomingUserToUserPayment = 5;
int64 latestOutgoingUserToUserPayment = 6;
}
enum UserOperationType {
INCOMING_TX =0;
OUTGOING_TX =1;
INCOMING_INVOICE =2;
OUTGOING_INVOICE=3;
OUTGOING_USER_TO_USER=4;
INCOMING_USER_TO_USER=5;
}
message UserOperation{
int64 paidAtUnix=1;
UserOperationType type = 2;
@ -144,14 +213,16 @@ message UserOperation{
}
message UserOperations {
int64 fromIndex=1;
int64 toIndex=2;
repeated UserOperation operations=3;
int64 toIndex=2;
repeated UserOperation operations=3;
}
message GetUserOperationsResponse{
UserOperations latestOutgoingInvoiceOperations=1;
UserOperations latestIncomingInvoiceOperations=2;
UserOperations latestOutgoingTxOperations=3;
UserOperations latestIncomingTxOperations=4;
UserOperations latestOutgoingUserToUserPayemnts=5;
UserOperations latestIncomingUserToUserPayemnts=6 ;
}
message AddProductRequest {