Merge pull request #690 from shocknet/master

bsp
This commit is contained in:
Justin (shocknet) 2024-05-24 17:42:33 -04:00 committed by GitHub
commit e797a58c83
28 changed files with 5162 additions and 4543 deletions

View file

@ -67,6 +67,10 @@ Coming Soon
Coming Soon
## Docker
`docker pull ghcr.io/shocknet/lightning-pub:latest`
## Manual CLI Installation
#### Notes:

View file

@ -9,6 +9,8 @@
#LND_CERT_PATH=~/.lnd/tls.cert
#LND_MACAROON_PATH=~/.lnd/data/chain/bitcoin/mainnet/admin.macaroon
LIQUIDITY_PROVIDER_PUB=
#DB
#DATABASE_FILE=db.sqlite
#METRICS_DATABASE_FILE=metrics.sqlite

View file

@ -100,6 +100,11 @@ The nostr server will send back a message response, and inside the body there wi
- This methods has an __empty__ __request__ body
- output: [MigrationUpdate](#MigrationUpdate)
- GetHttpCreds
- auth type: __User__
- This methods has an __empty__ __request__ body
- output: [HttpCreds](#HttpCreds)
- BatchUser
- auth type: __User__
- This methods has an __empty__ __request__ body
@ -115,9 +120,9 @@ The nostr server will send back a message response, and inside the body there wi
- __User__:
- expected context content
- __user_id__: _string_
- __app_id__: _string_
- __app_user_id__: _string_
- __user_id__: _string_
- __Admin__:
- expected context content
@ -458,6 +463,13 @@ The nostr server will send back a message response, and inside the body there wi
- This methods has an __empty__ __request__ body
- output: [MigrationUpdate](#MigrationUpdate)
- GetHttpCreds
- auth type: __User__
- http method: __post__
- http route: __/api/user/http_creds__
- This methods has an __empty__ __request__ body
- output: [HttpCreds](#HttpCreds)
- BatchUser
- auth type: __User__
- http method: __post__
@ -470,45 +482,41 @@ The nostr server will send back a message response, and inside the body there wi
## Messages
### The content of requests and response from the methods
### AddAppInvoiceRequest
- __payer_identifier__: _string_
- __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### BanUserRequest
- __user_id__: _string_
### GetAppUserRequest
- __user_identifier__: _string_
### SendAppUserToAppUserPaymentRequest
- __from_user_identifier__: _string_
- __to_user_identifier__: _string_
- __amount__: _number_
### LnurlPayInfoResponse
- __tag__: _string_
- __callback__: _string_
- __maxSendable__: _number_
- __minSendable__: _number_
- __metadata__: _string_
- __allowsNostr__: _boolean_
- __nostrPubkey__: _string_
### SendAppUserToAppPaymentRequest
- __from_user_identifier__: _string_
- __amount__: _number_
### LinkNPubThroughTokenRequest
- __token__: _string_
- __nostr_pub__: _string_
### GetUserOperationsRequest
- __latestIncomingInvoice__: _number_
- __latestOutgoingInvoice__: _number_
- __latestIncomingTx__: _number_
- __latestOutgoingTx__: _number_
- __latestIncomingUserToUserPayment__: _number_
- __latestOutgoingUserToUserPayment__: _number_
- __max_size__: _number_
### NewInvoiceResponse
- __invoice__: _string_
### GetUserOperationsResponse
- __latestOutgoingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingTxOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingTxOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
- __latestIncomingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
### PayAddressRequest
- __address__: _string_
- __amoutSats__: _number_
- __satsPerVByte__: _number_
### ClosureMigration
- __closes_at_unix__: _number_
### GetProductBuyLinkResponse
- __link__: _string_
### OpenChannel
- __channel_id__: _string_
- __capacity__: _number_
- __active__: _boolean_
- __lifetime__: _number_
- __local_balance__: _number_
- __remote_balance__: _number_
### AuthAppRequest
- __name__: _string_
- __allow_user_creation__: _boolean_ *this field is optional
### Application
- __name__: _string_
@ -516,17 +524,18 @@ The nostr server will send back a message response, and inside the body there wi
- __balance__: _number_
- __npub__: _string_
### AuthApp
- __app__: _[Application](#Application)_
- __auth_token__: _string_
### AddAppInvoiceRequest
- __payer_identifier__: _string_
- __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### PayAppUserInvoiceRequest
### GetAppUserLNURLInfoRequest
- __user_identifier__: _string_
- __invoice__: _string_
- __amount__: _number_
- __base_url_override__: _string_
### OpenChannelResponse
- __channelId__: _string_
### LnurlLinkResponse
- __lnurl__: _string_
- __k1__: _string_
### LnurlWithdrawInfoResponse
- __tag__: _string_
@ -538,44 +547,73 @@ The nostr server will send back a message response, and inside the body there wi
- __balanceCheck__: _string_
- __payLink__: _string_
### MigrationUpdate
- __closure__: _[ClosureMigration](#ClosureMigration)_ *this field is optional
- __relays__: _[RelaysMigration](#RelaysMigration)_ *this field is optional
### BannedAppUser
- __app_name__: _string_
- __app_id__: _string_
- __user_identifier__: _string_
- __nostr_pub__: _string_
### AuthAppRequest
- __name__: _string_
- __allow_user_creation__: _boolean_ *this field is optional
### SendAppUserToAppUserPaymentRequest
- __from_user_identifier__: _string_
- __to_user_identifier__: _string_
### UserOperation
- __paidAtUnix__: _number_
- __type__: _[UserOperationType](#UserOperationType)_
- __inbound__: _boolean_
- __amount__: _number_
- __identifier__: _string_
- __operationId__: _string_
- __service_fee__: _number_
- __network_fee__: _number_
- __confirmed__: _boolean_
- __tx_hash__: _string_
- __internal__: _boolean_
### NewAddressResponse
- __address__: _string_
### RelaysMigration
- __relays__: ARRAY of: _string_
### SetMockAppUserBalanceRequest
### RequestNPubLinkingTokenRequest
- __user_identifier__: _string_
### EncryptionExchangeRequest
- __publicKey__: _string_
- __deviceId__: _string_
### AppsMetricsRequest
- __from_unix__: _number_ *this field is optional
- __to_unix__: _number_ *this field is optional
- __include_operations__: _boolean_ *this field is optional
### ClosedChannel
- __channel_id__: _string_
- __capacity__: _number_
- __closed_height__: _number_
### OpenChannelResponse
- __channelId__: _string_
### Product
- __id__: _string_
- __name__: _string_
- __price_sats__: _number_
### PayAppUserInvoiceRequest
- __user_identifier__: _string_
- __invoice__: _string_
- __amount__: _number_
### NewInvoiceRequest
- __amountSats__: _number_
- __memo__: _string_
### HandleLnurlPayResponse
- __pr__: _string_
- __routes__: ARRAY of: _[Empty](#Empty)_
### LiveUserOperation
- __operation__: _[UserOperation](#UserOperation)_
### ClosureMigration
- __closes_at_unix__: _number_
### HttpCreds
- __url__: _string_
- __token__: _string_
### Empty
### UsageMetric
- __processed_at_ms__: _number_
- __parsed_in_nano__: _number_
- __auth_in_nano__: _number_
- __validate_in_nano__: _number_
- __handle_in_nano__: _number_
- __rpc_name__: _string_
- __batch__: _boolean_
- __nostr__: _boolean_
- __batch_size__: _number_
### RoutingEvent
- __incoming_channel_id__: _number_
@ -591,70 +629,20 @@ The nostr server will send back a message response, and inside the body there wi
- __offchain__: _boolean_
- __forward_fail_event__: _boolean_
### ChannelRouting
### ChannelBalanceEvent
- __block_height__: _number_
- __channel_id__: _string_
- __send_errors__: _number_
- __receive_errors__: _number_
- __forward_errors_as_input__: _number_
- __forward_errors_as_output__: _number_
- __missed_forward_fee_as_input__: _number_
- __missed_forward_fee_as_output__: _number_
- __forward_fee_as_input__: _number_
- __forward_fee_as_output__: _number_
- __events_number__: _number_
- __local_balance_sats__: _number_
- __remote_balance_sats__: _number_
### BanUserResponse
- __balance_sats__: _number_
- __banned_app_users__: ARRAY of: _[BannedAppUser](#BannedAppUser)_
### HandleLnurlPayResponse
- __pr__: _string_
- __routes__: ARRAY of: _[Empty](#Empty)_
### UsageMetric
- __processed_at_ms__: _number_
- __parsed_in_nano__: _number_
- __auth_in_nano__: _number_
- __validate_in_nano__: _number_
- __handle_in_nano__: _number_
- __rpc_name__: _string_
- __batch__: _boolean_
- __nostr__: _boolean_
- __batch_size__: _number_
### UsersInfo
- __total__: _number_
- __no_balance__: _number_
- __negative_balance__: _number_
- __always_been_inactive__: _number_
- __balance_avg__: _number_
- __balance_median__: _number_
### PayInvoiceResponse
- __preimage__: _string_
- __amount_paid__: _number_
- __operation_id__: _string_
- __service_fee__: _number_
- __network_fee__: _number_
### AddAppUserRequest
- __identifier__: _string_
- __fail_if_exists__: _boolean_
- __balance__: _number_
### GetAppUserLNURLInfoRequest
- __user_identifier__: _string_
- __base_url_override__: _string_
### UserInfo
- __userId__: _string_
- __balance__: _number_
- __max_withdrawable__: _number_
- __user_identifier__: _string_
### AppUser
- __identifier__: _string_
- __info__: _[UserInfo](#UserInfo)_
- __max_withdrawable__: _number_
### NewAddressRequest
- __addressType__: _[AddressType](#AddressType)_
### UserOperations
- __fromIndex__: _number_
- __toIndex__: _number_
- __operations__: ARRAY of: _[UserOperation](#UserOperation)_
### ChainBalanceEvent
- __block_height__: _number_
@ -662,21 +650,6 @@ The nostr server will send back a message response, and inside the body there wi
- __unconfirmed_balance__: _number_
- __total_balance__: _number_
### SetMockAppBalanceRequest
- __amount__: _number_
### GetUserOperationsResponse
- __latestOutgoingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingTxOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingTxOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
- __latestIncomingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
### AddProductRequest
- __name__: _string_
- __price_sats__: _number_
### LndNodeMetrics
- __channels_balance_events__: ARRAY of: _[ChannelBalanceEvent](#ChannelBalanceEvent)_
- __chain_balance_events__: ARRAY of: _[ChainBalanceEvent](#ChainBalanceEvent)_
@ -688,90 +661,109 @@ The nostr server will send back a message response, and inside the body there wi
- __closed_channels__: ARRAY of: _[ClosedChannel](#ClosedChannel)_
- __channel_routing__: ARRAY of: _[ChannelRouting](#ChannelRouting)_
### SendAppUserToAppPaymentRequest
- __from_user_identifier__: _string_
### SetMockInvoiceAsPaidRequest
- __invoice__: _string_
- __amount__: _number_
### DecodeInvoiceResponse
- __amount__: _number_
### OpenChannelRequest
- __destination__: _string_
- __fundingAmount__: _number_
- __pushAmount__: _number_
- __closeAddress__: _string_
### LndGetInfoResponse
- __alias__: _string_
### AddAppRequest
- __name__: _string_
- __allow_user_creation__: _boolean_
### AddAppUserInvoiceRequest
- __receiver_identifier__: _string_
- __payer_identifier__: _string_
- __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### LndMetrics
- __nodes__: ARRAY of: _[LndNodeMetrics](#LndNodeMetrics)_
### LndGetInfoRequest
- __nodeId__: _number_
### DecodeInvoiceRequest
- __invoice__: _string_
### UserOperations
- __fromIndex__: _number_
- __toIndex__: _number_
- __operations__: ARRAY of: _[UserOperation](#UserOperation)_
### Product
- __id__: _string_
- __name__: _string_
- __price_sats__: _number_
### RelaysMigration
- __relays__: ARRAY of: _string_
### LiveUserOperation
- __operation__: _[UserOperation](#UserOperation)_
### RequestNPubLinkingTokenResponse
- __token__: _string_
### OpenChannelRequest
- __destination__: _string_
- __fundingAmount__: _number_
- __pushAmount__: _number_
- __closeAddress__: _string_
### UsageMetrics
- __metrics__: ARRAY of: _[UsageMetric](#UsageMetric)_
### AppsMetrics
- __apps__: ARRAY of: _[AppMetrics](#AppMetrics)_
### ClosedChannel
### OpenChannel
- __channel_id__: _string_
- __capacity__: _number_
- __closed_height__: _number_
- __active__: _boolean_
- __lifetime__: _number_
- __local_balance__: _number_
- __remote_balance__: _number_
### UserOperation
- __paidAtUnix__: _number_
- __type__: _[UserOperationType](#UserOperationType)_
- __inbound__: _boolean_
### NewAddressRequest
- __addressType__: _[AddressType](#AddressType)_
### LnurlPayInfoResponse
- __tag__: _string_
- __callback__: _string_
- __maxSendable__: _number_
- __minSendable__: _number_
- __metadata__: _string_
- __allowsNostr__: _boolean_
- __nostrPubkey__: _string_
### GetProductBuyLinkResponse
- __link__: _string_
### UsersInfo
- __total__: _number_
- __no_balance__: _number_
- __negative_balance__: _number_
- __always_been_inactive__: _number_
- __balance_avg__: _number_
- __balance_median__: _number_
### AddAppRequest
- __name__: _string_
- __allow_user_creation__: _boolean_
### DecodeInvoiceResponse
- __amount__: _number_
- __identifier__: _string_
- __operationId__: _string_
### LndMetrics
- __nodes__: ARRAY of: _[LndNodeMetrics](#LndNodeMetrics)_
### AddProductRequest
- __name__: _string_
- __price_sats__: _number_
### Empty
### ChannelRouting
- __channel_id__: _string_
- __send_errors__: _number_
- __receive_errors__: _number_
- __forward_errors_as_input__: _number_
- __forward_errors_as_output__: _number_
- __missed_forward_fee_as_input__: _number_
- __missed_forward_fee_as_output__: _number_
- __forward_fee_as_input__: _number_
- __forward_fee_as_output__: _number_
- __events_number__: _number_
### PayInvoiceResponse
- __preimage__: _string_
- __amount_paid__: _number_
- __operation_id__: _string_
- __service_fee__: _number_
- __network_fee__: _number_
- __confirmed__: _boolean_
- __tx_hash__: _string_
- __internal__: _boolean_
### SetMockInvoiceAsPaidRequest
- __invoice__: _string_
- __amount__: _number_
### UserInfo
- __userId__: _string_
- __balance__: _number_
- __max_withdrawable__: _number_
- __user_identifier__: _string_
### BanUserRequest
- __user_id__: _string_
### MigrationUpdate
- __closure__: _[ClosureMigration](#ClosureMigration)_ *this field is optional
- __relays__: _[RelaysMigration](#RelaysMigration)_ *this field is optional
### LinkNPubThroughTokenRequest
- __token__: _string_
- __nostr_pub__: _string_
### PayAddressRequest
- __address__: _string_
- __amoutSats__: _number_
- __satsPerVByte__: _number_
### PayAddressResponse
- __txId__: _string_
@ -779,24 +771,6 @@ The nostr server will send back a message response, and inside the body there wi
- __service_fee__: _number_
- __network_fee__: _number_
### GetUserOperationsRequest
- __latestIncomingInvoice__: _number_
- __latestOutgoingInvoice__: _number_
- __latestIncomingTx__: _number_
- __latestOutgoingTx__: _number_
- __latestIncomingUserToUserPayment__: _number_
- __latestOutgoingUserToUserPayment__: _number_
- __max_size__: _number_
### EncryptionExchangeRequest
- __publicKey__: _string_
- __deviceId__: _string_
### AppsMetricsRequest
- __from_unix__: _number_ *this field is optional
- __to_unix__: _number_ *this field is optional
- __include_operations__: _boolean_ *this field is optional
### AppMetrics
- __app__: _[Application](#Application)_
- __users__: _[UsersInfo](#UsersInfo)_
@ -808,26 +782,68 @@ The nostr server will send back a message response, and inside the body there wi
- __total_fees__: _number_
- __operations__: ARRAY of: _[UserOperation](#UserOperation)_
### ChannelBalanceEvent
- __block_height__: _number_
- __channel_id__: _string_
- __local_balance_sats__: _number_
- __remote_balance_sats__: _number_
### BanUserResponse
- __balance_sats__: _number_
- __banned_app_users__: ARRAY of: _[BannedAppUser](#BannedAppUser)_
### RequestNPubLinkingTokenRequest
- __user_identifier__: _string_
### AuthApp
- __app__: _[Application](#Application)_
- __auth_token__: _string_
### LndMetricsRequest
- __from_unix__: _number_ *this field is optional
- __to_unix__: _number_ *this field is optional
### BannedAppUser
- __app_name__: _string_
- __app_id__: _string_
- __user_identifier__: _string_
- __nostr_pub__: _string_
### GetAppUserRequest
- __user_identifier__: _string_
### SetMockAppBalanceRequest
- __amount__: _number_
### DecodeInvoiceRequest
- __invoice__: _string_
### RequestNPubLinkingTokenResponse
- __token__: _string_
### LndGetInfoRequest
- __nodeId__: _number_
### AppUser
- __identifier__: _string_
- __info__: _[UserInfo](#UserInfo)_
- __max_withdrawable__: _number_
### SetMockAppUserBalanceRequest
- __user_identifier__: _string_
- __amount__: _number_
### PayInvoiceRequest
- __invoice__: _string_
- __amount__: _number_
### LnurlLinkResponse
- __lnurl__: _string_
- __k1__: _string_
### AddAppUserRequest
- __identifier__: _string_
- __fail_if_exists__: _boolean_
- __balance__: _number_
### NewInvoiceResponse
- __invoice__: _string_
### AppsMetrics
- __apps__: ARRAY of: _[AppMetrics](#AppMetrics)_
### LndGetInfoResponse
- __alias__: _string_
### NewAddressResponse
- __address__: _string_
## Enums
### The enumerators used in the messages

File diff suppressed because it is too large Load diff

View file

@ -587,6 +587,7 @@ export default (params: ClientParams) => ({
},
GetLiveUserOperations: async (cb: (v:ResultError | ({ status: 'OK' }& Types.LiveUserOperation)) => void): Promise<void> => { throw new Error('http streams are not supported')},
GetMigrationUpdate: async (cb: (v:ResultError | ({ status: 'OK' }& Types.MigrationUpdate)) => void): Promise<void> => { throw new Error('http streams are not supported')},
GetHttpCreds: async (cb: (v:ResultError | ({ status: 'OK' }& Types.HttpCreds)) => void): Promise<void> => { throw new Error('http streams are not supported')},
BatchUser: async (requests:Types.UserMethodInputs[]): Promise<ResultError | ({ status: 'OK', responses:( Types.UserMethodOutputs)[] })> => {
const auth = await params.retrieveUserAuth()
if (auth === null) throw new Error('retrieveUserAuth() returned null')

View file

@ -253,6 +253,21 @@ export default (params: NostrClientParams, send: (to:string, message: NostrRequ
return cb({ status: 'ERROR', reason: 'invalid response' })
})
},
GetHttpCreds: async (cb: (res:ResultError | ({ status: 'OK' }& Types.HttpCreds)) => void): Promise<void> => {
const auth = await params.retrieveNostrUserAuth()
if (auth === null) throw new Error('retrieveNostrUserAuth() returned null')
const nostrRequest: NostrRequest = {}
subscribe(params.pubDestination, {rpcName:'GetHttpCreds',authIdentifier:auth, ...nostrRequest }, (data) => {
if (data.status === 'ERROR' && typeof data.reason === 'string') return cb(data)
if (data.status === 'OK') {
const result = data
if(!params.checkResult) return cb({ status: 'OK', ...result })
const error = Types.HttpCredsValidate(result)
if (error === null) { return cb({ status: 'OK', ...result }) } else return cb({ status: 'ERROR', reason: error.message })
}
return cb({ status: 'ERROR', reason: 'invalid response' })
})
},
BatchUser: async (requests:Types.UserMethodInputs[]): Promise<ResultError | ({ status: 'OK', responses:(Types.UserMethodOutputs)[] })> => {
const auth = await params.retrieveNostrUserAuth()
if (auth === null) throw new Error('retrieveNostrUserAuth() returned null')

View file

@ -16,7 +16,7 @@ export type NostrOptions = {
logger?: Logger
throwErrors?: true
metricsCallback: (metrics: Types.RequestMetric[]) => void
NostrUserAuthGuard: (appId?:string, identifier?: string) => Promise<Types.UserContext>
NostrUserAuthGuard: (appId?: string, identifier?: string) => Promise<Types.UserContext>
}
const logErrorAndReturnResponse = (error: Error, response: string, res: NostrResponse, logger: Logger, metric: Types.RequestMetric, metricsCallback: (metrics: Types.RequestMetric[]) => void) => {
logger.error(error.message || error); metricsCallback([{ ...metric, error: response }]); res({ status: 'ERROR', reason: response })
@ -39,11 +39,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.LinkNPubThroughTokenRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
await methods.LinkNPubThroughToken({rpcName:'LinkNPubThroughToken', ctx:authContext , req: request})
await methods.LinkNPubThroughToken({ rpcName: 'LinkNPubThroughToken', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK'})
res({ status: 'OK' })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'UserHealth':
try {
@ -52,11 +52,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
await methods.UserHealth({rpcName:'UserHealth', ctx:authContext })
await methods.UserHealth({ rpcName: 'UserHealth', ctx: authContext })
stats.handle = process.hrtime.bigint()
res({status: 'OK'})
res({ status: 'OK' })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetUserInfo':
try {
@ -65,11 +65,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
const response = await methods.GetUserInfo({rpcName:'GetUserInfo', ctx:authContext })
const response = await methods.GetUserInfo({ rpcName: 'GetUserInfo', ctx: authContext })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'AddProduct':
try {
@ -81,11 +81,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.AddProductRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.AddProduct({rpcName:'AddProduct', ctx:authContext , req: request})
const response = await methods.AddProduct({ rpcName: 'AddProduct', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'NewProductInvoice':
try {
@ -94,11 +94,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
const response = await methods.NewProductInvoice({rpcName:'NewProductInvoice', ctx:authContext ,query: req.query||{}})
const response = await methods.NewProductInvoice({ rpcName: 'NewProductInvoice', ctx: authContext, query: req.query || {} })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetUserOperations':
try {
@ -110,11 +110,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.GetUserOperationsRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.GetUserOperations({rpcName:'GetUserOperations', ctx:authContext , req: request})
const response = await methods.GetUserOperations({ rpcName: 'GetUserOperations', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'NewAddress':
try {
@ -126,11 +126,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.NewAddressRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.NewAddress({rpcName:'NewAddress', ctx:authContext , req: request})
const response = await methods.NewAddress({ rpcName: 'NewAddress', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'PayAddress':
try {
@ -142,11 +142,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.PayAddressRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.PayAddress({rpcName:'PayAddress', ctx:authContext , req: request})
const response = await methods.PayAddress({ rpcName: 'PayAddress', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'NewInvoice':
try {
@ -158,11 +158,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.NewInvoiceRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.NewInvoice({rpcName:'NewInvoice', ctx:authContext , req: request})
const response = await methods.NewInvoice({ rpcName: 'NewInvoice', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'DecodeInvoice':
try {
@ -174,11 +174,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.DecodeInvoiceRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.DecodeInvoice({rpcName:'DecodeInvoice', ctx:authContext , req: request})
const response = await methods.DecodeInvoice({ rpcName: 'DecodeInvoice', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'PayInvoice':
try {
@ -190,11 +190,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.PayInvoiceRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.PayInvoice({rpcName:'PayInvoice', ctx:authContext , req: request})
const response = await methods.PayInvoice({ rpcName: 'PayInvoice', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'OpenChannel':
try {
@ -206,11 +206,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.OpenChannelRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.OpenChannel({rpcName:'OpenChannel', ctx:authContext , req: request})
const response = await methods.OpenChannel({ rpcName: 'OpenChannel', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetLnurlWithdrawLink':
try {
@ -219,11 +219,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
const response = await methods.GetLnurlWithdrawLink({rpcName:'GetLnurlWithdrawLink', ctx:authContext })
const response = await methods.GetLnurlWithdrawLink({ rpcName: 'GetLnurlWithdrawLink', ctx: authContext })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetLnurlPayLink':
try {
@ -232,11 +232,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
const response = await methods.GetLnurlPayLink({rpcName:'GetLnurlPayLink', ctx:authContext })
const response = await methods.GetLnurlPayLink({ rpcName: 'GetLnurlPayLink', ctx: authContext })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetLNURLChannelLink':
try {
@ -245,11 +245,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
const response = await methods.GetLNURLChannelLink({rpcName:'GetLNURLChannelLink', ctx:authContext })
const response = await methods.GetLNURLChannelLink({ rpcName: 'GetLNURLChannelLink', ctx: authContext })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetLiveUserOperations':
try {
@ -258,11 +258,13 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
methods.GetLiveUserOperations({rpcName:'GetLiveUserOperations', ctx:authContext ,cb: (response, err) => {
methods.GetLiveUserOperations({
rpcName: 'GetLiveUserOperations', ctx: authContext, cb: (response, err) => {
stats.handle = process.hrtime.bigint()
if (err) { logErrorAndReturnResponse(err, err.message, res, logger, { ...info, ...stats, ...authContext }, opts.metricsCallback)} else { res({status: 'OK', ...response});opts.metricsCallback([{ ...info, ...stats, ...authContext }])}
}})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
if (err) { logErrorAndReturnResponse(err, err.message, res, logger, { ...info, ...stats, ...authContext }, opts.metricsCallback) } else { res({ status: 'OK', ...response }); opts.metricsCallback([{ ...info, ...stats, ...authContext }]) }
}
})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetMigrationUpdate':
try {
@ -271,17 +273,34 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
methods.GetMigrationUpdate({rpcName:'GetMigrationUpdate', ctx:authContext ,cb: (response, err) => {
methods.GetMigrationUpdate({
rpcName: 'GetMigrationUpdate', ctx: authContext, cb: (response, err) => {
stats.handle = process.hrtime.bigint()
if (err) { logErrorAndReturnResponse(err, err.message, res, logger, { ...info, ...stats, ...authContext }, opts.metricsCallback)} else { res({status: 'OK', ...response});opts.metricsCallback([{ ...info, ...stats, ...authContext }])}
}})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
if (err) { logErrorAndReturnResponse(err, err.message, res, logger, { ...info, ...stats, ...authContext }, opts.metricsCallback) } else { res({ status: 'OK', ...response }); opts.metricsCallback([{ ...info, ...stats, ...authContext }]) }
}
})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetHttpCreds':
try {
if (!methods.GetHttpCreds) throw new Error('method: GetHttpCreds is not implemented')
const authContext = await opts.NostrUserAuthGuard(req.appId, req.authIdentifier)
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
methods.GetHttpCreds({
rpcName: 'GetHttpCreds', ctx: authContext, cb: (response, err) => {
stats.handle = process.hrtime.bigint()
if (err) { logErrorAndReturnResponse(err, err.message, res, logger, { ...info, ...stats, ...authContext }, opts.metricsCallback) } else { res({ status: 'OK', ...response }); opts.metricsCallback([{ ...info, ...stats, ...authContext }]) }
}
})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'BatchUser':
try {
info.batch = true
const requests = req.body.requests as Types.UserMethodInputs[]
if (!Array.isArray(requests))throw new Error('invalid body, is not an array')
if (!Array.isArray(requests)) throw new Error('invalid body, is not an array')
info.batchSize = requests.length
if (requests.length > 10) throw new Error('too many requests in the batch')
const ctx = await opts.NostrUserAuthGuard(req.appId, req.authIdentifier)
@ -295,7 +314,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const opInfo: Types.RequestInfo = { rpcName: operation.rpcName, batch: true, nostr: true, batchSize: 0 }
const opStats: Types.RequestStats = { startMs, start: startTime, parse: stats.parse, guard: stats.guard, validate: 0n, handle: 0n }
try {
switch(operation.rpcName) {
switch (operation.rpcName) {
case 'LinkNPubThroughToken':
if (!methods.LinkNPubThroughToken) {
throw new Error('method not defined: LinkNPubThroughToken')
@ -303,7 +322,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.LinkNPubThroughTokenRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
await methods.LinkNPubThroughToken({...operation, ctx}); responses.push({ status: 'OK' })
await methods.LinkNPubThroughToken({ ...operation, ctx }); responses.push({ status: 'OK' })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -313,7 +332,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
throw new Error('method not defined: UserHealth')
} else {
opStats.validate = opStats.guard
await methods.UserHealth({...operation, ctx}); responses.push({ status: 'OK' })
await methods.UserHealth({ ...operation, ctx }); responses.push({ status: 'OK' })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -323,7 +342,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
throw new Error('method not defined: GetUserInfo')
} else {
opStats.validate = opStats.guard
const res = await methods.GetUserInfo({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.GetUserInfo({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -335,7 +354,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.AddProductRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.AddProduct({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.AddProduct({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -345,7 +364,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
throw new Error('method not defined: NewProductInvoice')
} else {
opStats.validate = opStats.guard
const res = await methods.NewProductInvoice({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.NewProductInvoice({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -357,7 +376,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.GetUserOperationsRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.GetUserOperations({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.GetUserOperations({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -369,7 +388,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.NewAddressRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.NewAddress({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.NewAddress({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -381,7 +400,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.PayAddressRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.PayAddress({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.PayAddress({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -393,7 +412,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.NewInvoiceRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.NewInvoice({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.NewInvoice({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -405,7 +424,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.DecodeInvoiceRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.DecodeInvoice({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.DecodeInvoice({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -417,7 +436,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.PayInvoiceRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.PayInvoice({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.PayInvoice({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -429,7 +448,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.OpenChannelRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.OpenChannel({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.OpenChannel({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -439,7 +458,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
throw new Error('method not defined: GetLnurlWithdrawLink')
} else {
opStats.validate = opStats.guard
const res = await methods.GetLnurlWithdrawLink({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.GetLnurlWithdrawLink({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -449,7 +468,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
throw new Error('method not defined: GetLnurlPayLink')
} else {
opStats.validate = opStats.guard
const res = await methods.GetLnurlPayLink({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.GetLnurlPayLink({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -459,7 +478,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
throw new Error('method not defined: GetLNURLChannelLink')
} else {
opStats.validate = opStats.guard
const res = await methods.GetLNURLChannelLink({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.GetLNURLChannelLink({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -467,14 +486,14 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
default:
throw new Error('unkown rpcName')
}
} catch(ex) {const e = ex as any; logger.error(e.message || e); callsMetrics.push({ ...opInfo, ...opStats, ...ctx, error: e.message }); responses.push({ status: 'ERROR', reason: e.message || e })}
} catch (ex) { const e = ex as any; logger.error(e.message || e); callsMetrics.push({ ...opInfo, ...opStats, ...ctx, error: e.message }); responses.push({ status: 'ERROR', reason: e.message || e }) }
}
stats.handle = process.hrtime.bigint()
res({ status: 'OK', responses })
opts.metricsCallback([{ ...info, ...stats, ...ctx }, ...callsMetrics])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
default: logger.error('unknown rpc call name from nostr event:'+req.rpcName)
default: logger.error('unknown rpc call name from nostr event:' + req.rpcName)
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -369,6 +369,12 @@ service LightningPub {
option (http_route) = "/api/user/migrations/sub";
option (nostr) = true;
}
rpc GetHttpCreds(structs.Empty) returns (stream structs.HttpCreds){
option (auth_type) = "User";
option (http_method) = "post";
option (http_route) = "/api/user/http_creds";
option (nostr) = true;
}
rpc BatchUser(structs.Empty) returns (structs.Empty){
option (auth_type) = "User";
option (http_method) = "post";

View file

@ -443,3 +443,7 @@ message LinkNPubThroughTokenRequest {
string nostr_pub = 2;
}
message HttpCreds {
string url = 1;
string token = 2;
}

View file

@ -16,10 +16,13 @@ const start = async () => {
log("manual process ended")
return
}
const { apps, mainHandler } = keepOn
const { apps, mainHandler, liquidityProviderInfo } = keepOn
const serverMethods = GetServerMethods(mainHandler)
const nostrSettings = LoadNosrtSettingsFromEnv()
const { Send } = nostrMiddleware(serverMethods, mainHandler, { ...nostrSettings, apps })
const { Send } = nostrMiddleware(serverMethods, mainHandler,
{ ...nostrSettings, apps, clients: [liquidityProviderInfo] },
(e, p) => mainHandler.liquidProvider.onEvent(e, p)
)
mainHandler.attachNostrSend(Send)
mainHandler.StartBeacons()
const Server = NewServer(serverMethods, serverOptions(mainHandler))

View file

@ -5,7 +5,7 @@ import * as Types from '../proto/autogenerated/ts/types.js'
import NewNostrTransport, { NostrRequest } from '../proto/autogenerated/ts/nostr_transport.js';
import { ERROR, getLogger } from "./services/helpers/logger.js";
export default (serverMethods: Types.ServerMethods, mainHandler: Main, nostrSettings: NostrSettings): { Stop: () => void, Send: NostrSend } => {
export default (serverMethods: Types.ServerMethods, mainHandler: Main, nostrSettings: NostrSettings, onClientEvent: (e: { requestId: string }, fromPub: string) => void): { Stop: () => void, Send: NostrSend } => {
const log = getLogger({})
const nostrTransport = NewNostrTransport(serverMethods, {
NostrUserAuthGuard: async (appId, pub) => {
@ -25,12 +25,16 @@ export default (serverMethods: Types.ServerMethods, mainHandler: Main, nostrSett
log(ERROR, "invalid json event received", event.content)
return
}
if (!j.rpcName) {
onClientEvent(j as { requestId: string }, event.pub)
return
}
if (j.authIdentifier !== event.pub) {
log(ERROR, "authIdentifier does not match", j.authIdentifier || "--", event.pub)
return
}
nostrTransport({ ...j, appId: event.appId }, res => {
nostr.Send(event.appId, { type: 'content', pub: event.pub, content: JSON.stringify({ ...res, requestId: j.requestId }) })
nostr.Send({ type: 'app', appId: event.appId }, { type: 'content', pub: event.pub, content: JSON.stringify({ ...res, requestId: j.requestId }) })
}, event.startAtNano, event.startAtMs)
})
return { Stop: () => nostr.Stop, Send: (...args) => nostr.Send(...args) }

View file

@ -17,5 +17,6 @@ export const LoadLndSettingsFromEnv = (): LndSettings => {
const feeRateLimit = EnvCanBeInteger("OUTBOUND_MAX_FEE_BPS", 60) / 10000
const feeFixedLimit = EnvCanBeInteger("OUTBOUND_MAX_FEE_EXTRA_SATS", 100)
const mockLnd = EnvCanBeBoolean("MOCK_LND")
return { mainNode: { lndAddr, lndCertPath, lndMacaroonPath }, feeRateLimit, feeFixedLimit, mockLnd }
const liquidityProviderPub = process.env.LIQUIDITY_PROVIDER_PUB || ""
return { mainNode: { lndAddr, lndCertPath, lndMacaroonPath }, feeRateLimit, feeFixedLimit, mockLnd, liquidityProviderPub, useOnlyLiquidityProvider: false }
}

View file

@ -0,0 +1,226 @@
import newNostrClient from '../../../proto/autogenerated/ts/nostr_client.js'
import { NostrRequest } from '../../../proto/autogenerated/ts/nostr_transport.js'
import * as Types from '../../../proto/autogenerated/ts/types.js'
import { decodeNprofile } from '../../custom-nip19.js'
import { getLogger } from '../helpers/logger.js'
import { NostrEvent, NostrSend } from '../nostr/handler.js'
import { relayInit } from '../nostr/tools/relay.js'
import { InvoicePaidCb } from './settings.js'
export type LiquidityRequest = { action: 'spend' | 'receive', amount: number }
export type nostrCallback<T> = { startedAtMillis: number, type: 'single' | 'stream', f: (res: T) => void }
export class LiquidityProvider {
client: ReturnType<typeof newNostrClient>
clientCbs: Record<string, nostrCallback<any>> = {}
clientId: string = ""
myPub: string = ""
log = getLogger({ component: 'liquidityProvider' })
nostrSend: NostrSend | null = null
ready = false
pubDestination: string
latestMaxWithdrawable: number | null = null
invoicePaidCb: InvoicePaidCb
connecting = false
readyInterval: NodeJS.Timeout
// make the sub process accept client
constructor(pubDestination: string, invoicePaidCb: InvoicePaidCb) {
if (!pubDestination) {
this.log("No pub provider to liquidity provider, will not be initialized")
}
this.pubDestination = pubDestination
this.invoicePaidCb = invoicePaidCb
this.client = newNostrClient({
pubDestination: this.pubDestination,
retrieveNostrUserAuth: async () => this.myPub,
}, this.clientSend, this.clientSub)
this.readyInterval = setInterval(() => {
if (this.ready) {
clearInterval(this.readyInterval)
this.Connect()
}
}, 1000)
}
Stop = () => {
clearInterval(this.readyInterval)
}
Connect = async () => {
await new Promise(res => setTimeout(res, 2000))
this.log("ready")
await this.CheckUserState()
if (this.latestMaxWithdrawable === null) {
return
}
this.log("subbing to user operations")
this.client.GetLiveUserOperations(res => {
console.log("got user operation", res)
if (res.status === 'ERROR') {
this.log("error getting user operations", res.reason)
return
}
this.log("got user operation", res.operation)
if (res.operation.type === Types.UserOperationType.INCOMING_INVOICE) {
this.log("invoice was paid", res.operation.identifier)
this.invoicePaidCb(res.operation.identifier, res.operation.amount, false)
}
})
}
CheckUserState = async () => {
const res = await this.client.GetUserInfo()
if (res.status === 'ERROR') {
this.log("error getting user info", res)
return
}
this.latestMaxWithdrawable = res.max_withdrawable
this.log("latest provider balance:", res.max_withdrawable)
return res
}
CanProviderHandle = (req: LiquidityRequest) => {
if (this.latestMaxWithdrawable === null) {
return false
}
if (req.action === 'spend') {
return this.latestMaxWithdrawable > req.amount
}
return true
}
AddInvoice = async (amount: number, memo: string) => {
const res = await this.client.NewInvoice({ amountSats: amount, memo })
if (res.status === 'ERROR') {
this.log("error creating invoice", res.reason)
throw new Error(res.reason)
}
this.log("new invoice", res.invoice)
this.CheckUserState()
return res.invoice
}
PayInvoice = async (invoice: string) => {
const res = await this.client.PayInvoice({ invoice, amount: 0 })
if (res.status === 'ERROR') {
this.log("error paying invoice", res.reason)
throw new Error(res.reason)
}
this.log("paid invoice", res)
this.CheckUserState()
return res
}
setNostrInfo = ({ clientId, myPub }: { myPub: string, clientId: string }) => {
this.clientId = clientId
this.myPub = myPub
this.setSetIfReady()
}
attachNostrSend(f: NostrSend) {
this.nostrSend = f
this.setSetIfReady()
}
setSetIfReady = () => {
if (this.nostrSend && !!this.pubDestination && !!this.clientId && !!this.myPub) {
this.ready = true
this.log("ready to send to ", this.pubDestination)
}
}
onEvent = async (res: { requestId: string }, fromPub: string) => {
if (fromPub !== this.pubDestination) {
this.log("got event from invalid pub", fromPub, this.pubDestination)
return false
}
if (this.clientCbs[res.requestId]) {
const cb = this.clientCbs[res.requestId]
cb.f(res)
if (cb.type === 'single') {
delete this.clientCbs[res.requestId]
this.log(this.getSingleSubs(), "single subs left")
}
return true
}
return false
}
clientSend = (to: string, message: NostrRequest): Promise<any> => {
if (!this.ready || !this.nostrSend) {
throw new Error("liquidity provider not initialized")
}
if (!message.requestId) {
message.requestId = makeId(16)
}
const reqId = message.requestId
if (this.clientCbs[reqId]) {
throw new Error("request was already sent")
}
this.nostrSend({ type: 'client', clientId: this.clientId }, {
type: 'content',
pub: to,
content: JSON.stringify(message)
})
//this.nostrSend(this.relays, to, JSON.stringify(message), this.settings)
this.log("subbing to single send", reqId, message.rpcName || 'no rpc name')
return new Promise(res => {
this.clientCbs[reqId] = {
startedAtMillis: Date.now(),
type: 'single',
f: (response: any) => { res(response) },
}
})
}
clientSub = (to: string, message: NostrRequest, cb: (res: any) => void): void => {
if (!this.ready || !this.nostrSend) {
throw new Error("liquidity provider not initialized")
}
if (!message.requestId) {
message.requestId = message.rpcName
}
const reqId = message.requestId
if (!reqId) {
throw new Error("invalid sub")
}
if (this.clientCbs[reqId]) {
this.clientCbs[reqId] = {
startedAtMillis: Date.now(),
type: 'stream',
f: (response: any) => { cb(response) },
}
this.log("sub for", reqId, "was already registered, overriding")
return
}
this.nostrSend({ type: 'client', clientId: this.clientId }, {
type: 'content',
pub: to,
content: JSON.stringify(message)
})
this.log("subbing to stream", reqId)
this.clientCbs[reqId] = {
startedAtMillis: Date.now(),
type: 'stream',
f: (response: any) => { cb(response) }
}
}
getSingleSubs = () => {
return Object.entries(this.clientCbs).filter(([_, cb]) => cb.type === 'single')
}
}
export const makeId = (length: number) => {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}

View file

@ -16,6 +16,7 @@ import { SendCoinsReq } from './sendCoinsReq.js';
import { LndSettings, AddressPaidCb, InvoicePaidCb, NodeInfo, Invoice, DecodedInvoice, PaidInvoice, NewBlockCb, HtlcCb, BalanceInfo } from './settings.js';
import { getLogger } from '../helpers/logger.js';
import { HtlcEvent_EventType } from '../../../proto/lnd/router.js';
import { LiquidityProvider, LiquidityRequest } from './liquidityProvider.js';
const DeadLineMetadata = (deadline = 10 * 1000) => ({ deadline: Date.now() + deadline })
const deadLndRetrySeconds = 5
export default class {
@ -34,7 +35,8 @@ export default class {
htlcCb: HtlcCb
log = getLogger({ component: 'lndManager' })
outgoingOpsLocked = false
constructor(settings: LndSettings, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb, newBlockCb: NewBlockCb, htlcCb: HtlcCb) {
liquidProvider: LiquidityProvider
constructor(settings: LndSettings, liquidProvider: LiquidityProvider, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb, newBlockCb: NewBlockCb, htlcCb: HtlcCb) {
this.settings = settings
this.addressPaidCb = addressPaidCb
this.invoicePaidCb = invoicePaidCb
@ -60,6 +62,7 @@ export default class {
this.invoices = new InvoicesClient(transport)
this.router = new RouterClient(transport)
this.chainNotifier = new ChainNotifierClient(transport)
this.liquidProvider = liquidProvider
}
LockOutgoingOperations(): void {
@ -74,6 +77,22 @@ export default class {
}
Stop() {
this.abortController.abort()
this.liquidProvider.Stop()
}
async ShouldUseLiquidityProvider(req: LiquidityRequest): Promise<boolean> {
if (this.settings.useOnlyLiquidityProvider) {
return true
}
if (!this.liquidProvider.CanProviderHandle(req)) {
return false
}
const channels = await this.ListChannels()
if (channels.channels.length === 0) {
this.log("no channels, will use liquidity provider")
return true
}
return false
}
async Warmup() {
this.SubscribeAddressPaid()
@ -212,7 +231,7 @@ export default class {
}, { abort: this.abortController.signal })
stream.responses.onMessage(invoice => {
if (invoice.state === Invoice_InvoiceState.SETTLED) {
this.log("An invoice was paid for", Number(invoice.amtPaidSat), "sats")
this.log("An invoice was paid for", Number(invoice.amtPaidSat), "sats", invoice.paymentRequest)
this.latestKnownSettleIndex = Number(invoice.settleIndex)
this.invoicePaidCb(invoice.paymentRequest, Number(invoice.amtPaidSat), false)
}
@ -251,6 +270,11 @@ export default class {
async NewInvoice(value: number, memo: string, expiry: number): Promise<Invoice> {
this.log("generating new invoice for", value, "sats")
await this.Health()
const shouldUseLiquidityProvider = await this.ShouldUseLiquidityProvider({ action: 'receive', amount: value })
if (shouldUseLiquidityProvider) {
const invoice = await this.liquidProvider.AddInvoice(value, memo)
return { payRequest: invoice }
}
const res = await this.lightning.addInvoice(AddInvoiceReq(value, expiry, false, memo), DeadLineMetadata())
this.log("new invoice", res.response.paymentRequest)
return { payRequest: res.response.paymentRequest }
@ -281,6 +305,11 @@ export default class {
}
await this.Health()
this.log("paying invoice", invoice, "for", amount, "sats")
const shouldUseLiquidityProvider = await this.ShouldUseLiquidityProvider({ action: 'spend', amount })
if (shouldUseLiquidityProvider) {
const res = await this.liquidProvider.PayInvoice(invoice)
return { feeSat: res.network_fee + res.service_fee, valueSat: res.amount_paid, paymentPreimage: res.preimage }
}
const abortController = new AbortController()
const req = PayInvoiceReq(invoice, amount, feeLimit)
const stream = this.router.sendPaymentV2(req, { abort: abortController.signal })

29
src/services/lnd/lsp.ts Normal file
View file

@ -0,0 +1,29 @@
import fetch from "node-fetch"
export class LSP {
serviceUrl: string
constructor(serviceUrl: string) {
this.serviceUrl = serviceUrl
}
getInfo = async () => {
const res = await fetch(`${this.serviceUrl}/getinfo`)
const json = await res.json() as { options: {}, uris: string[] }
}
createOrder = async (req: { public_key: string }) => {
const res = await fetch(`${this.serviceUrl}/create_order`, {
method: "POST",
body: JSON.stringify(req),
headers: { "Content-Type": "application/json" }
})
const json = await res.json() as {}
return json
}
getOrder = async (orderId: string) => {
const res = await fetch(`${this.serviceUrl}/get_order&order_id=${orderId}`)
const json = await res.json() as {}
return json
}
}

View file

@ -9,6 +9,8 @@ export type LndSettings = {
feeRateLimit: number
feeFixedLimit: number
mockLnd: boolean
liquidityProviderPub: string
useOnlyLiquidityProvider: boolean
otherNode?: NodeSettings
thirdNode?: NodeSettings

View file

@ -15,6 +15,7 @@ import { UnsignedEvent } from '../nostr/tools/event.js'
import { NostrSend } from '../nostr/handler.js'
import MetricsManager from '../metrics/index.js'
import { LoggedEvent } from '../storage/eventsLog.js'
import { LiquidityProvider } from "../lnd/liquidityProvider.js"
type UserOperationsSub = {
id: string
@ -35,12 +36,13 @@ export default class {
paymentManager: PaymentManager
paymentSubs: Record<string, ((op: Types.UserOperation) => void) | null> = {}
metricsManager: MetricsManager
liquidProvider: LiquidityProvider
nostrSend: NostrSend = () => { getLogger({})("nostr send not initialized yet") }
constructor(settings: MainSettings, storage: Storage) {
this.settings = settings
this.storage = storage
this.lnd = new LND(settings.lndSettings, this.addressPaidCb, this.invoicePaidCb, this.newBlockCb, this.htlcCb)
this.liquidProvider = new LiquidityProvider(settings.lndSettings.liquidityProviderPub, this.invoicePaidCb)
this.lnd = new LND(settings.lndSettings, this.liquidProvider, this.addressPaidCb, this.invoicePaidCb, this.newBlockCb, this.htlcCb)
this.metricsManager = new MetricsManager(this.storage, this.lnd)
this.paymentManager = new PaymentManager(this.storage, this.lnd, this.settings, this.addressPaidCb, this.invoicePaidCb)
@ -63,6 +65,7 @@ export default class {
attachNostrSend(f: NostrSend) {
this.nostrSend = f
this.liquidProvider.attachNostrSend(f)
}
htlcCb: HtlcCb = (e) => {
@ -208,7 +211,7 @@ export default class {
return
}
const message: Types.LiveUserOperation & { requestId: string, status: 'OK' } = { operation: op, requestId: "GetLiveUserOperations", status: 'OK' }
this.nostrSend(app.app_id, { type: 'content', content: JSON.stringify(message), pub: user.nostr_public_key })
this.nostrSend({ type: 'app', appId: app.app_id }, { type: 'content', content: JSON.stringify(message), pub: user.nostr_public_key })
}
async UpdateBeacon(app: Application, content: { type: 'service', name: string }) {
@ -224,7 +227,7 @@ export default class {
pubkey: app.nostr_public_key,
tags,
}
this.nostrSend(app.app_id, { type: 'event', event })
this.nostrSend({ type: 'app', appId: app.app_id }, { type: 'event', event })
}
async createZapReceipt(log: PubLogger, invoice: UserReceivingInvoice) {
@ -245,6 +248,6 @@ export default class {
tags,
}
log({ unsigned: event })
this.nostrSend(invoice.linkedApplication.app_id, { type: 'event', event })
this.nostrSend({ type: 'app', appId: invoice.linkedApplication.app_id }, { type: 'event', event })
}
}

View file

@ -1,4 +1,5 @@
import { PubLogger, getLogger } from "../helpers/logger.js"
import { LiquidityProvider } from "../lnd/liquidityProvider.js"
import Storage from "../storage/index.js"
import { TypeOrmMigrationRunner } from "../storage/migrations/runner.js"
import Main from "./index.js"
@ -16,6 +17,7 @@ export const initMainHandler = async (log: PubLogger, mainSettings: MainSettings
if (manualMigration) {
return
}
const mainHandler = new Main(mainSettings, storageManager)
await mainHandler.lnd.Warmup()
if (!mainSettings.skipSanityCheck) {
@ -38,11 +40,21 @@ export const initMainHandler = async (log: PubLogger, mainSettings: MainSettings
return { privateKey: app.nostr_private_key, publicKey: app.nostr_public_key, appId: app.app_id, name: app.name }
}
}))
const liquidityProviderApp = apps.find(app => app.name === 'wallet' || app.name === 'wallet-test')
if (!liquidityProviderApp) {
throw new Error("wallet app not initialized correctly")
}
const liquidityProviderInfo = {
privateKey: liquidityProviderApp.privateKey,
publicKey: liquidityProviderApp.publicKey,
name: "liquidity_provider", clientId: `client_${liquidityProviderApp.appId}`
}
mainHandler.liquidProvider.setNostrInfo({ clientId: liquidityProviderInfo.clientId, myPub: liquidityProviderInfo.publicKey })
const stop = await processArgs(mainHandler)
if (stop) {
return
}
return { mainHandler, apps }
return { mainHandler, apps, liquidityProviderInfo, liquidityProviderApp }
}
const processArgs = async (mainHandler: Main) => {

View file

@ -15,6 +15,7 @@ import { Event, verifiedSymbol, verifySignature } from '../nostr/tools/event.js'
import { AddressReceivingTransaction } from '../storage/entity/AddressReceivingTransaction.js'
import { UserTransactionPayment } from '../storage/entity/UserTransactionPayment.js'
import { Watchdog } from './watchdog.js'
import { LiquidityProvider } from '../lnd/liquidityProvider.js'
interface UserOperationInfo {
serial_id: number
paid_amount: number
@ -39,7 +40,6 @@ const defaultLnurlPayMetadata = `[["text/plain", "lnurl pay to Lightning.pub"]]`
const confInOne = 1000 * 1000
const confInTwo = 100 * 1000 * 1000
export default class {
storage: Storage
settings: MainSettings
lnd: LND
@ -297,7 +297,7 @@ export default class {
}
async GetLnurlWithdrawLink(ctx: Types.UserContext): Promise<Types.LnurlLinkResponse> {
if(this.isDefaultServiceUrl()) {
if (this.isDefaultServiceUrl()) {
throw new Error("Lnurl not enabled. Make sure to set SERVICE_URL env variable")
}
const app = await this.storage.applicationStorage.GetApplication(ctx.app_id)
@ -345,7 +345,7 @@ export default class {
}
async GetLnurlPayLink(ctx: Types.UserContext): Promise<Types.LnurlLinkResponse> {
if(this.isDefaultServiceUrl()) {
if (this.isDefaultServiceUrl()) {
throw new Error("Lnurl not enabled. Make sure to set SERVICE_URL env variable")
}
getLogger({})("getting lnurl pay link")
@ -360,7 +360,7 @@ export default class {
}
async GetLnurlPayInfoFromUser(userId: string, linkedApplication: Application, baseUrl?: string): Promise<Types.LnurlPayInfoResponse> {
if(this.isDefaultServiceUrl()) {
if (this.isDefaultServiceUrl()) {
throw new Error("Lnurl not enabled. Make sure to set SERVICE_URL env variable")
}
const payK1 = await this.storage.paymentStorage.AddUserEphemeralKey(userId, 'pay', linkedApplication)
@ -378,7 +378,7 @@ export default class {
}
async GetLnurlPayInfo(payInfoK1: string): Promise<Types.LnurlPayInfoResponse> {
if(this.isDefaultServiceUrl()) {
if (this.isDefaultServiceUrl()) {
throw new Error("Lnurl not enabled. Make sure to set SERVICE_URL env variable")
}
const key = await this.storage.paymentStorage.UseUserEphemeralKey(payInfoK1, 'pay', true)

View file

@ -49,7 +49,7 @@ export const LoadMainSettingsFromEnv = (): MainSettings => {
servicePort: EnvCanBeInteger("PORT", 1776),
recordPerformance: process.env.RECORD_PERFORMANCE === 'true' || false,
skipSanityCheck: process.env.SKIP_SANITY_CHECK === 'true' || false,
disableExternalPayments: process.env.DISABLE_EXTERNAL_PAYMENTS === 'true' || false
disableExternalPayments: process.env.DISABLE_EXTERNAL_PAYMENTS === 'true' || false,
}
}
@ -75,14 +75,15 @@ export const LoadTestSettingsFromEnv = (): TestSettings => {
lndAddr: EnvMustBeNonEmptyString("LND_FOURTH_ADDR"),
lndCertPath: EnvMustBeNonEmptyString("LND_FOURTH_CERT_PATH"),
lndMacaroonPath: EnvMustBeNonEmptyString("LND_FOURTH_MACAROON_PATH")
}
},
liquidityProviderPub: ""
},
skipSanityCheck: true,
bitcoinCoreSettings: {
port: EnvMustBeInteger("BITCOIN_CORE_PORT"),
user: EnvMustBeNonEmptyString("BITCOIN_CORE_USER"),
pass: EnvMustBeNonEmptyString("BITCOIN_CORE_PASS")
}
},
}
}

View file

@ -5,12 +5,15 @@ import { ERROR, getLogger } from '../helpers/logger.js'
import { encodeNprofile } from '../../custom-nip19.js'
const handledEvents: string[] = [] // TODO: - big memory leak here, add TTL
type AppInfo = { appId: string, publicKey: string, privateKey: string, name: string }
type ClientInfo = { clientId: string, publicKey: string, privateKey: string, name: string }
export type SendData = { type: "content", content: string, pub: string } | { type: "event", event: UnsignedEvent }
export type NostrSend = (appId: string, data: SendData, relays?: string[] | undefined) => void
export type SendInitiator = { type: 'app', appId: string } | { type: 'client', clientId: string }
export type NostrSend = (initiator: SendInitiator, data: SendData, relays?: string[] | undefined) => void
export type NostrSettings = {
apps: AppInfo[]
relays: string[]
clients: ClientInfo[]
}
export type NostrEvent = {
id: string
@ -20,6 +23,7 @@ export type NostrEvent = {
startAtNano: string
startAtMs: number
}
type SettingsRequest = {
type: 'settings'
settings: NostrSettings
@ -27,7 +31,7 @@ type SettingsRequest = {
type SendRequest = {
type: 'send'
appId: string
initiator: SendInitiator
data: SendData
relays?: string[]
}
@ -53,7 +57,7 @@ process.on("message", (message: ChildProcessRequest) => {
initSubprocessHandler(message.settings)
break
case 'send':
sendToNostr(message.appId, message.data, message.relays)
sendToNostr(message.initiator, message.data, message.relays)
break
default:
getLogger({ component: "nostrMiddleware" })(ERROR, "unknown nostr request", message)
@ -72,12 +76,12 @@ const initSubprocessHandler = (settings: NostrSettings) => {
})
})
}
const sendToNostr: NostrSend = (appId, data, relays) => {
const sendToNostr: NostrSend = (initiator, data, relays) => {
if (!subProcessHandler) {
getLogger({ component: "nostrMiddleware" })(ERROR, "nostr was not initialized")
return
}
subProcessHandler.Send(appId, data, relays)
subProcessHandler.Send(initiator, data, relays)
}
send({ type: 'ready' })
@ -147,9 +151,14 @@ export default class Handler {
return
}
const app = this.apps[pubTags[1]]
if (!app) {
if (app) {
await this.processEvent(e, app)
return
}
})
}
async processEvent(e: Event<21000>, app: AppInfo) {
const eventId = e.id
if (handledEvents.includes(eventId)) {
this.log("event already handled")
@ -161,29 +170,28 @@ export default class Handler {
const decoded = decodePayload(e.content)
const content = await decryptData(decoded, getSharedSecret(app.privateKey, e.pubkey))
this.eventCallback({ id: eventId, content, pub: e.pubkey, appId: app.appId, startAtNano, startAtMs })
})
}
async Send(appId: string, data: SendData, relays?: string[]) {
const appInfo = this.GetAppKeys({ appId })
async Send(initiator: SendInitiator, data: SendData, relays?: string[]) {
const keys = this.GetSendKeys(initiator)
let toSign: UnsignedEvent
if (data.type === 'content') {
const decoded = await encryptData(data.content, getSharedSecret(appInfo.privateKey, data.pub))
const decoded = await encryptData(data.content, getSharedSecret(keys.privateKey, data.pub))
const content = encodePayload(decoded)
toSign = {
content,
created_at: Math.floor(Date.now() / 1000),
kind: 21000,
pubkey: appInfo.publicKey,
pubkey: keys.publicKey,
tags: [['p', data.pub]],
}
} else {
toSign = data.event
}
const signed = finishEvent(toSign, appInfo.privateKey)
const signed = finishEvent(toSign, keys.privateKey)
let sent = false
const log = getLogger({ appName: appInfo.name })
const log = getLogger({ appName: keys.name })
await Promise.all(this.pool.publish(relays || this.settings.relays, signed).map(async p => {
try {
await p
@ -197,21 +205,22 @@ export default class Handler {
}
}
GetAppKeys(appInfo: Partial<AppInfo>) {
let check: (info: AppInfo) => boolean
if (appInfo.appId) {
check = (info: AppInfo) => info.appId === appInfo.appId
} else if (appInfo.privateKey) {
check = (info: AppInfo) => info.privateKey === appInfo.privateKey
} else if (appInfo.publicKey) {
check = (info: AppInfo) => info.publicKey === appInfo.publicKey
} else {
throw new Error("app info is empty")
}
const found = this.settings.apps.find(check)
GetSendKeys(initiator: SendInitiator) {
if (initiator.type === 'app') {
const { appId } = initiator
const found = this.settings.apps.find((info: AppInfo) => info.appId === appId)
if (!found) {
throw new Error("unkown app")
}
return found
} else if (initiator.type === 'client') {
const { clientId } = initiator
const found = this.settings.clients.find((info: ClientInfo) => info.clientId === clientId)
if (!found) {
throw new Error("unkown client")
}
return found
}
throw new Error("unkown initiator type")
}
}

View file

@ -1,6 +1,6 @@
import { ChildProcess, fork } from 'child_process'
import { EnvMustBeNonEmptyString } from "../helpers/envParser.js"
import { NostrSettings, NostrEvent, ChildProcessRequest, ChildProcessResponse, SendData } from "./handler.js"
import { NostrSettings, NostrEvent, ChildProcessRequest, ChildProcessResponse, SendData, SendInitiator } from "./handler.js"
type EventCallback = (event: NostrEvent) => void
const getEnvOrDefault = (name: string, defaultValue: string): string => {
@ -38,8 +38,8 @@ export default class NostrSubprocess {
this.childProcess.send(message)
}
Send(appId: string, data: SendData, relays?: string[]) {
this.sendToChildProcess({ type: 'send', data, appId, relays })
Send(initiator: SendInitiator, data: SendData, relays?: string[]) {
this.sendToChildProcess({ type: 'send', data, initiator, relays })
}
Stop() {
this.childProcess.kill()

View file

@ -0,0 +1,54 @@
import { disableLoggers } from '../services/helpers/logger.js'
import { runSanityCheck, safelySetUserBalance, TestBase, TestUserData } from './testBase.js'
import { initBootstrappedInstance } from './setupBootstrapped.js'
import Main from '../services/main/index.js'
import { AppData } from '../services/main/init.js'
export const ignore = false
export const dev = false
export default async (T: TestBase) => {
disableLoggers([], ["EventsLogManager", "watchdog", "htlcTracker", "debugHtlcs", "debugLndBalancev3", "metrics", "mainForTest", "main"])
await safelySetUserBalance(T, T.user1, 2000)
T.d("starting liquidityProvider tests...")
const { bootstrapped, bootstrappedUser, stop } = await initBootstrappedInstance(T)
await testInboundPaymentFromProvider(T, bootstrapped, bootstrappedUser)
await testOutboundPaymentFromProvider(T, bootstrapped, bootstrappedUser)
stop()
await runSanityCheck(T)
}
const testInboundPaymentFromProvider = async (T: TestBase, bootstrapped: Main, bUser: TestUserData) => {
T.d("starting testInboundPaymentFromProvider")
const invoiceRes = await bootstrapped.appUserManager.NewInvoice({ app_id: bUser.appId, user_id: bUser.userId, app_user_id: bUser.appUserIdentifier }, { amountSats: 2000, memo: "liquidityTest" })
await T.externalAccessToOtherLnd.PayInvoice(invoiceRes.invoice, 0, 100)
const userBalance = await bootstrapped.appUserManager.GetUserInfo({ app_id: bUser.appId, user_id: bUser.userId, app_user_id: bUser.appUserIdentifier })
T.expect(userBalance.balance).to.equal(2000)
const providerBalance = await bootstrapped.liquidProvider.CheckUserState()
if (!providerBalance) {
throw new Error("provider balance not found")
}
T.expect(providerBalance.balance).to.equal(2000)
T.d("testInboundPaymentFromProvider done")
}
const testOutboundPaymentFromProvider = async (T: TestBase, bootstrapped: Main, bootstrappedUser: TestUserData) => {
T.d("starting testOutboundPaymentFromProvider")
const invoice = await T.externalAccessToOtherLnd.NewInvoice(1000, "", 60 * 60)
const ctx = { app_id: bootstrappedUser.appId, user_id: bootstrappedUser.userId, app_user_id: bootstrappedUser.appUserIdentifier }
const res = await bootstrapped.appUserManager.PayInvoice(ctx, { invoice: invoice.payRequest, amount: 0 })
const userBalance = await bootstrapped.appUserManager.GetUserInfo(ctx)
T.expect(userBalance.balance).to.equal(986) // 2000 - (1000 + 6(x2) + 2)
const providerBalance = await bootstrapped.liquidProvider.CheckUserState()
if (!providerBalance) {
throw new Error("provider balance not found")
}
T.expect(providerBalance.balance).to.equal(992) // 2000 - (1000 + 6 +2)
T.d("testOutboundPaymentFromProvider done")
}

View file

@ -1,14 +1,15 @@
import { LoadTestSettingsFromEnv } from "../services/main/settings.js"
import { BitcoinCoreWrapper } from "./bitcoinCore.js"
import LND from '../services/lnd/lnd.js'
import { LiquidityProvider } from "../services/lnd/liquidityProvider.js"
export const setupNetwork = async () => {
const settings = LoadTestSettingsFromEnv()
const core = new BitcoinCoreWrapper(settings)
await core.InitAddress()
await core.Mine(1)
const alice = new LND(settings.lndSettings, () => { }, () => { }, () => { }, () => { })
const bob = new LND({ ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }, () => { }, () => { }, () => { }, () => { })
const alice = new LND(settings.lndSettings, new LiquidityProvider("", () => { }), () => { }, () => { }, () => { }, () => { })
const bob = new LND({ ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }, new LiquidityProvider("", () => { }), () => { }, () => { }, () => { }, () => { })
await tryUntil<void>(async i => {
const peers = await alice.ListPeers()
if (peers.peers.length > 0) {
@ -46,6 +47,9 @@ export const setupNetwork = async () => {
throw new Error("bob not synced to graph")
}
}, 15, 2000)
alice.Stop()
bob.Stop()
}
const tryUntil = async <T>(fn: (attempt: number) => Promise<T>, maxTries: number, interval: number) => {

View file

@ -0,0 +1,92 @@
import { getLogger } from '../services/helpers/logger.js'
import { initMainHandler } from '../services/main/init.js'
import { LoadTestSettingsFromEnv } from '../services/main/settings.js'
import { SendData } from '../services/nostr/handler.js'
import { TestBase, TestUserData } from './testBase.js'
import * as Types from '../../proto/autogenerated/ts/types.js'
export const initBootstrappedInstance = async (T: TestBase) => {
const settings = LoadTestSettingsFromEnv()
settings.lndSettings.useOnlyLiquidityProvider = true
settings.lndSettings.liquidityProviderPub = T.app.publicKey
settings.lndSettings.mainNode = settings.lndSettings.thirdNode
const initialized = await initMainHandler(getLogger({ component: "bootstrapped" }), settings)
if (!initialized) {
throw new Error("failed to initialize bootstrapped main handler")
}
const { mainHandler: bootstrapped, liquidityProviderInfo, liquidityProviderApp } = initialized
T.main.attachNostrSend(async (_, data, r) => {
if (data.type === 'event') {
throw new Error("unsupported event type")
}
if (data.pub !== liquidityProviderInfo.publicKey) {
throw new Error("invalid pub " + data.pub + " expected " + liquidityProviderInfo.publicKey)
}
const j = JSON.parse(data.content) as { requestId: string }
console.log("sending new operation to provider")
bootstrapped.liquidProvider.onEvent(j, T.app.publicKey)
})
bootstrapped.liquidProvider.attachNostrSend(async (_, data, r) => {
const res = await handleSend(T, data)
if (data.type === 'event') {
throw new Error("unsupported event type")
}
if (!res) {
return
}
bootstrapped.liquidProvider.onEvent(res, data.pub)
})
bootstrapped.liquidProvider.setNostrInfo({ clientId: liquidityProviderInfo.clientId, myPub: liquidityProviderInfo.publicKey })
await new Promise<void>(res => {
const interval = setInterval(() => {
if (bootstrapped.liquidProvider.CanProviderHandle({ action: 'receive', amount: 2000 })) {
clearInterval(interval)
res()
} else {
console.log("waiting for provider to be able to handle the request")
}
}, 500)
})
const bUser = await bootstrapped.applicationManager.AddAppUser(liquidityProviderApp.appId, { identifier: "user1_bootstrapped", balance: 0, fail_if_exists: true })
const bootstrappedUser: TestUserData = { userId: bUser.info.userId, appUserIdentifier: bUser.identifier, appId: liquidityProviderApp.appId }
return {
bootstrapped, liquidityProviderInfo, liquidityProviderApp, bootstrappedUser, stop: () => {
bootstrapped.Stop()
}
}
}
type TransportRequest = { requestId: string, authIdentifier: string } & (
{ rpcName: 'GetUserInfo' } |
{ rpcName: 'NewInvoice', body: Types.NewInvoiceRequest } |
{ rpcName: 'PayInvoice', body: Types.PayInvoiceRequest } |
{ rpcName: 'GetLiveUserOperations' } |
{ rpcName: "" }
)
const handleSend = async (T: TestBase, data: SendData) => {
if (data.type === 'event') {
throw new Error("unsupported event type")
}
if (data.pub !== T.app.publicKey) {
throw new Error("invalid pub")
}
const j = JSON.parse(data.content) as TransportRequest
const app = await T.main.storage.applicationStorage.GetApplication(T.app.appId)
const nostrUser = await T.main.storage.applicationStorage.GetOrCreateNostrAppUser(app, j.authIdentifier)
const userCtx = { app_id: app.app_id, user_id: nostrUser.user.user_id, app_user_id: nostrUser.identifier }
switch (j.rpcName) {
case 'GetUserInfo':
const infoRes = await T.main.appUserManager.GetUserInfo(userCtx)
return { ...infoRes, status: "OK", requestId: j.requestId }
case 'NewInvoice':
const genInvoiceRes = await T.main.appUserManager.NewInvoice(userCtx, j.body)
return { ...genInvoiceRes, status: "OK", requestId: j.requestId }
case 'PayInvoice':
const payRes = await T.main.appUserManager.PayInvoice(userCtx, j.body)
return { ...payRes, status: "OK", requestId: j.requestId }
case 'GetLiveUserOperations':
return
default:
console.log(data)
throw new Error("unsupported rpcName " + j.rpcName)
}
}

View file

@ -9,7 +9,8 @@ import chaiString from 'chai-string'
import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
import SanityChecker from '../services/main/sanityChecker.js'
import LND from '../services/lnd/lnd.js'
import { resetDisabledLoggers } from '../services/helpers/logger.js'
import { getLogger, resetDisabledLoggers } from '../services/helpers/logger.js'
import { LiquidityProvider } from '../services/lnd/liquidityProvider.js'
chai.use(chaiString)
export const expect = chai.expect
export type Describe = (message: string, failure?: boolean) => void
@ -33,7 +34,7 @@ export type TestBase = {
export const SetupTest = async (d: Describe): Promise<TestBase> => {
const settings = LoadTestSettingsFromEnv()
const initialized = await initMainHandler(console.log, settings)
const initialized = await initMainHandler(getLogger({ component: "mainForTest" }), settings)
if (!initialized) {
throw new Error("failed to initialize main handler")
}
@ -45,15 +46,15 @@ export const SetupTest = async (d: Describe): Promise<TestBase> => {
const user2 = { userId: u2.info.userId, appUserIdentifier: u2.identifier, appId: app.appId }
const externalAccessToMainLnd = new LND(settings.lndSettings, console.log, console.log, () => { }, () => { })
const externalAccessToMainLnd = new LND(settings.lndSettings, new LiquidityProvider("", () => { }), console.log, console.log, () => { }, () => { })
await externalAccessToMainLnd.Warmup()
const otherLndSetting = { ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }
const externalAccessToOtherLnd = new LND(otherLndSetting, console.log, console.log, () => { }, () => { })
const externalAccessToOtherLnd = new LND(otherLndSetting, new LiquidityProvider("", () => { }), console.log, console.log, () => { }, () => { })
await externalAccessToOtherLnd.Warmup()
const thirdLndSetting = { ...settings.lndSettings, mainNode: settings.lndSettings.thirdNode }
const externalAccessToThirdLnd = new LND(thirdLndSetting, console.log, console.log, () => { }, () => { })
const externalAccessToThirdLnd = new LND(thirdLndSetting, new LiquidityProvider("", () => { }), console.log, console.log, () => { }, () => { })
await externalAccessToThirdLnd.Warmup()

View file

@ -1,7 +1,7 @@
import { globby } from 'globby'
import { setupNetwork } from './networkSetup.js'
import { Describe, SetupTest, teardown, TestBase } from './testBase.js'
type TestModule = {
ignore?: boolean
dev?: boolean