liquidty provider w nostr transport

This commit is contained in:
boufni95 2024-05-21 21:19:37 +02:00
parent b6259a64a9
commit 779d3204c3
21 changed files with 4870 additions and 4533 deletions

View file

@ -9,6 +9,8 @@
#LND_CERT_PATH=~/.lnd/tls.cert #LND_CERT_PATH=~/.lnd/tls.cert
#LND_MACAROON_PATH=~/.lnd/data/chain/bitcoin/mainnet/admin.macaroon #LND_MACAROON_PATH=~/.lnd/data/chain/bitcoin/mainnet/admin.macaroon
LIQUIDITY_PROVIDER_PUB=
#DB #DB
#DATABASE_FILE=db.sqlite #DATABASE_FILE=db.sqlite
#METRICS_DATABASE_FILE=metrics.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 - This methods has an __empty__ __request__ body
- output: [MigrationUpdate](#MigrationUpdate) - output: [MigrationUpdate](#MigrationUpdate)
- GetHttpCreds
- auth type: __User__
- This methods has an __empty__ __request__ body
- output: [HttpCreds](#HttpCreds)
- BatchUser - BatchUser
- auth type: __User__ - auth type: __User__
- This methods has an __empty__ __request__ body - 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__: - __User__:
- expected context content - expected context content
- __user_id__: _string_
- __app_id__: _string_ - __app_id__: _string_
- __app_user_id__: _string_ - __app_user_id__: _string_
- __user_id__: _string_
- __Admin__: - __Admin__:
- expected context content - 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 - This methods has an __empty__ __request__ body
- output: [MigrationUpdate](#MigrationUpdate) - 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 - BatchUser
- auth type: __User__ - auth type: __User__
- http method: __post__ - http method: __post__
@ -470,45 +482,41 @@ The nostr server will send back a message response, and inside the body there wi
## Messages ## Messages
### The content of requests and response from the methods ### The content of requests and response from the methods
### AddAppInvoiceRequest ### BanUserRequest
- __payer_identifier__: _string_ - __user_id__: _string_
- __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### GetAppUserRequest ### SendAppUserToAppUserPaymentRequest
- __user_identifier__: _string_ - __from_user_identifier__: _string_
- __to_user_identifier__: _string_
- __amount__: _number_
### LnurlPayInfoResponse ### SendAppUserToAppPaymentRequest
- __tag__: _string_ - __from_user_identifier__: _string_
- __callback__: _string_ - __amount__: _number_
- __maxSendable__: _number_
- __minSendable__: _number_
- __metadata__: _string_
- __allowsNostr__: _boolean_
- __nostrPubkey__: _string_
### LinkNPubThroughTokenRequest ### GetUserOperationsRequest
- __token__: _string_ - __latestIncomingInvoice__: _number_
- __nostr_pub__: _string_ - __latestOutgoingInvoice__: _number_
- __latestIncomingTx__: _number_
- __latestOutgoingTx__: _number_
- __latestIncomingUserToUserPayment__: _number_
- __latestOutgoingUserToUserPayment__: _number_
- __max_size__: _number_
### NewInvoiceResponse ### GetUserOperationsResponse
- __invoice__: _string_ - __latestOutgoingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingTxOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingTxOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
- __latestIncomingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
### PayAddressRequest ### ClosureMigration
- __address__: _string_ - __closes_at_unix__: _number_
- __amoutSats__: _number_
- __satsPerVByte__: _number_
### GetProductBuyLinkResponse ### AuthAppRequest
- __link__: _string_ - __name__: _string_
- __allow_user_creation__: _boolean_ *this field is optional
### OpenChannel
- __channel_id__: _string_
- __capacity__: _number_
- __active__: _boolean_
- __lifetime__: _number_
- __local_balance__: _number_
- __remote_balance__: _number_
### Application ### Application
- __name__: _string_ - __name__: _string_
@ -516,17 +524,18 @@ The nostr server will send back a message response, and inside the body there wi
- __balance__: _number_ - __balance__: _number_
- __npub__: _string_ - __npub__: _string_
### AuthApp ### AddAppInvoiceRequest
- __app__: _[Application](#Application)_ - __payer_identifier__: _string_
- __auth_token__: _string_ - __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### PayAppUserInvoiceRequest ### GetAppUserLNURLInfoRequest
- __user_identifier__: _string_ - __user_identifier__: _string_
- __invoice__: _string_ - __base_url_override__: _string_
- __amount__: _number_
### OpenChannelResponse ### LnurlLinkResponse
- __channelId__: _string_ - __lnurl__: _string_
- __k1__: _string_
### LnurlWithdrawInfoResponse ### LnurlWithdrawInfoResponse
- __tag__: _string_ - __tag__: _string_
@ -538,44 +547,73 @@ The nostr server will send back a message response, and inside the body there wi
- __balanceCheck__: _string_ - __balanceCheck__: _string_
- __payLink__: _string_ - __payLink__: _string_
### MigrationUpdate ### UserOperation
- __closure__: _[ClosureMigration](#ClosureMigration)_ *this field is optional - __paidAtUnix__: _number_
- __relays__: _[RelaysMigration](#RelaysMigration)_ *this field is optional - __type__: _[UserOperationType](#UserOperationType)_
- __inbound__: _boolean_
### 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_
- __amount__: _number_ - __amount__: _number_
- __identifier__: _string_
- __operationId__: _string_
- __service_fee__: _number_
- __network_fee__: _number_
- __confirmed__: _boolean_
- __tx_hash__: _string_
- __internal__: _boolean_
### NewAddressResponse ### RelaysMigration
- __address__: _string_ - __relays__: ARRAY of: _string_
### SetMockAppUserBalanceRequest ### RequestNPubLinkingTokenRequest
- __user_identifier__: _string_ - __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_ - __amount__: _number_
### NewInvoiceRequest ### NewInvoiceRequest
- __amountSats__: _number_ - __amountSats__: _number_
- __memo__: _string_ - __memo__: _string_
### HandleLnurlPayResponse ### LiveUserOperation
- __pr__: _string_ - __operation__: _[UserOperation](#UserOperation)_
- __routes__: ARRAY of: _[Empty](#Empty)_
### ClosureMigration ### HttpCreds
- __closes_at_unix__: _number_ - __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 ### RoutingEvent
- __incoming_channel_id__: _number_ - __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_ - __offchain__: _boolean_
- __forward_fail_event__: _boolean_ - __forward_fail_event__: _boolean_
### ChannelRouting ### ChannelBalanceEvent
- __block_height__: _number_
- __channel_id__: _string_ - __channel_id__: _string_
- __send_errors__: _number_ - __local_balance_sats__: _number_
- __receive_errors__: _number_ - __remote_balance_sats__: _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_
### BanUserResponse ### HandleLnurlPayResponse
- __balance_sats__: _number_ - __pr__: _string_
- __banned_app_users__: ARRAY of: _[BannedAppUser](#BannedAppUser)_ - __routes__: ARRAY of: _[Empty](#Empty)_
### UsageMetric ### UserOperations
- __processed_at_ms__: _number_ - __fromIndex__: _number_
- __parsed_in_nano__: _number_ - __toIndex__: _number_
- __auth_in_nano__: _number_ - __operations__: ARRAY of: _[UserOperation](#UserOperation)_
- __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)_
### ChainBalanceEvent ### ChainBalanceEvent
- __block_height__: _number_ - __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_ - __unconfirmed_balance__: _number_
- __total_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 ### LndNodeMetrics
- __channels_balance_events__: ARRAY of: _[ChannelBalanceEvent](#ChannelBalanceEvent)_ - __channels_balance_events__: ARRAY of: _[ChannelBalanceEvent](#ChannelBalanceEvent)_
- __chain_balance_events__: ARRAY of: _[ChainBalanceEvent](#ChainBalanceEvent)_ - __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)_ - __closed_channels__: ARRAY of: _[ClosedChannel](#ClosedChannel)_
- __channel_routing__: ARRAY of: _[ChannelRouting](#ChannelRouting)_ - __channel_routing__: ARRAY of: _[ChannelRouting](#ChannelRouting)_
### SendAppUserToAppPaymentRequest ### SetMockInvoiceAsPaidRequest
- __from_user_identifier__: _string_ - __invoice__: _string_
- __amount__: _number_ - __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 ### AddAppUserInvoiceRequest
- __receiver_identifier__: _string_ - __receiver_identifier__: _string_
- __payer_identifier__: _string_ - __payer_identifier__: _string_
- __http_callback_url__: _string_ - __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_ - __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### LndMetrics ### OpenChannelRequest
- __nodes__: ARRAY of: _[LndNodeMetrics](#LndNodeMetrics)_ - __destination__: _string_
- __fundingAmount__: _number_
### LndGetInfoRequest - __pushAmount__: _number_
- __nodeId__: _number_ - __closeAddress__: _string_
### 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_
### UsageMetrics ### UsageMetrics
- __metrics__: ARRAY of: _[UsageMetric](#UsageMetric)_ - __metrics__: ARRAY of: _[UsageMetric](#UsageMetric)_
### AppsMetrics ### OpenChannel
- __apps__: ARRAY of: _[AppMetrics](#AppMetrics)_
### ClosedChannel
- __channel_id__: _string_ - __channel_id__: _string_
- __capacity__: _number_ - __capacity__: _number_
- __closed_height__: _number_ - __active__: _boolean_
- __lifetime__: _number_
- __local_balance__: _number_
- __remote_balance__: _number_
### UserOperation ### NewAddressRequest
- __paidAtUnix__: _number_ - __addressType__: _[AddressType](#AddressType)_
- __type__: _[UserOperationType](#UserOperationType)_
- __inbound__: _boolean_ ### 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_ - __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_ - __service_fee__: _number_
- __network_fee__: _number_ - __network_fee__: _number_
- __confirmed__: _boolean_
- __tx_hash__: _string_
- __internal__: _boolean_
### SetMockInvoiceAsPaidRequest ### UserInfo
- __invoice__: _string_ - __userId__: _string_
- __amount__: _number_ - __balance__: _number_
- __max_withdrawable__: _number_
- __user_identifier__: _string_
### BanUserRequest ### MigrationUpdate
- __user_id__: _string_ - __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 ### PayAddressResponse
- __txId__: _string_ - __txId__: _string_
@ -779,24 +771,6 @@ The nostr server will send back a message response, and inside the body there wi
- __service_fee__: _number_ - __service_fee__: _number_
- __network_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 ### AppMetrics
- __app__: _[Application](#Application)_ - __app__: _[Application](#Application)_
- __users__: _[UsersInfo](#UsersInfo)_ - __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_ - __total_fees__: _number_
- __operations__: ARRAY of: _[UserOperation](#UserOperation)_ - __operations__: ARRAY of: _[UserOperation](#UserOperation)_
### ChannelBalanceEvent ### BanUserResponse
- __block_height__: _number_ - __balance_sats__: _number_
- __channel_id__: _string_ - __banned_app_users__: ARRAY of: _[BannedAppUser](#BannedAppUser)_
- __local_balance_sats__: _number_
- __remote_balance_sats__: _number_
### RequestNPubLinkingTokenRequest ### AuthApp
- __user_identifier__: _string_ - __app__: _[Application](#Application)_
- __auth_token__: _string_
### LndMetricsRequest ### LndMetricsRequest
- __from_unix__: _number_ *this field is optional - __from_unix__: _number_ *this field is optional
- __to_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 ### PayInvoiceRequest
- __invoice__: _string_ - __invoice__: _string_
- __amount__: _number_ - __amount__: _number_
### LnurlLinkResponse ### AddAppUserRequest
- __lnurl__: _string_ - __identifier__: _string_
- __k1__: _string_ - __fail_if_exists__: _boolean_
- __balance__: _number_
### NewInvoiceResponse
- __invoice__: _string_
### AppsMetrics
- __apps__: ARRAY of: _[AppMetrics](#AppMetrics)_
### LndGetInfoResponse
- __alias__: _string_
### NewAddressResponse
- __address__: _string_
## Enums ## Enums
### The enumerators used in the messages ### 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')}, 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')}, 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)[] })> => { BatchUser: async (requests:Types.UserMethodInputs[]): Promise<ResultError | ({ status: 'OK', responses:( Types.UserMethodOutputs)[] })> => {
const auth = await params.retrieveUserAuth() const auth = await params.retrieveUserAuth()
if (auth === null) throw new Error('retrieveUserAuth() returned null') 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' }) 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)[] })> => { BatchUser: async (requests:Types.UserMethodInputs[]): Promise<ResultError | ({ status: 'OK', responses:(Types.UserMethodOutputs)[] })> => {
const auth = await params.retrieveNostrUserAuth() const auth = await params.retrieveNostrUserAuth()
if (auth === null) throw new Error('retrieveNostrUserAuth() returned null') if (auth === null) throw new Error('retrieveNostrUserAuth() returned null')

View file

@ -258,10 +258,12 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint() stats.guard = process.hrtime.bigint()
authCtx = authContext authCtx = authContext
stats.validate = stats.guard 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() 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 }]) } 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 } } 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 break
case 'GetMigrationUpdate': case 'GetMigrationUpdate':
@ -271,10 +273,27 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint() stats.guard = process.hrtime.bigint()
authCtx = authContext authCtx = authContext
stats.validate = stats.guard 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() 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 }]) } 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 } } 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 break
case 'BatchUser': case 'BatchUser':

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 (http_route) = "/api/user/migrations/sub";
option (nostr) = true; 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){ rpc BatchUser(structs.Empty) returns (structs.Empty){
option (auth_type) = "User"; option (auth_type) = "User";
option (http_method) = "post"; option (http_method) = "post";

View file

@ -443,3 +443,7 @@ message LinkNPubThroughTokenRequest {
string nostr_pub = 2; 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") log("manual process ended")
return return
} }
const { apps, mainHandler } = keepOn const { apps, mainHandler, liquidityProviderInfo } = keepOn
const serverMethods = GetServerMethods(mainHandler) const serverMethods = GetServerMethods(mainHandler)
const nostrSettings = LoadNosrtSettingsFromEnv() 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.attachNostrSend(Send)
mainHandler.StartBeacons() mainHandler.StartBeacons()
const Server = NewServer(serverMethods, serverOptions(mainHandler)) 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 NewNostrTransport, { NostrRequest } from '../proto/autogenerated/ts/nostr_transport.js';
import { ERROR, getLogger } from "./services/helpers/logger.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 log = getLogger({})
const nostrTransport = NewNostrTransport(serverMethods, { const nostrTransport = NewNostrTransport(serverMethods, {
NostrUserAuthGuard: async (appId, pub) => { NostrUserAuthGuard: async (appId, pub) => {
@ -25,12 +25,16 @@ export default (serverMethods: Types.ServerMethods, mainHandler: Main, nostrSett
log(ERROR, "invalid json event received", event.content) log(ERROR, "invalid json event received", event.content)
return return
} }
if (!j.rpcName) {
onClientEvent(j as { requestId: string }, event.pub)
return
}
if (j.authIdentifier !== event.pub) { if (j.authIdentifier !== event.pub) {
log(ERROR, "authIdentifier does not match", j.authIdentifier || "--", event.pub) log(ERROR, "authIdentifier does not match", j.authIdentifier || "--", event.pub)
return return
} }
nostrTransport({ ...j, appId: event.appId }, res => { 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) }, event.startAtNano, event.startAtMs)
}) })
return { Stop: () => nostr.Stop, Send: (...args) => nostr.Send(...args) } return { Stop: () => nostr.Stop, Send: (...args) => nostr.Send(...args) }

View file

@ -7,5 +7,6 @@ export const LoadLndSettingsFromEnv = (): LndSettings => {
const feeRateLimit = EnvCanBeInteger("OUTBOUND_MAX_FEE_BPS", 60) / 10000 const feeRateLimit = EnvCanBeInteger("OUTBOUND_MAX_FEE_BPS", 60) / 10000
const feeFixedLimit = EnvCanBeInteger("OUTBOUND_MAX_FEE_EXTRA_SATS", 100) const feeFixedLimit = EnvCanBeInteger("OUTBOUND_MAX_FEE_EXTRA_SATS", 100)
const mockLnd = EnvCanBeBoolean("MOCK_LND") 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 }
} }

View file

@ -0,0 +1,153 @@
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'
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 = ""
log = getLogger({ component: 'liquidityProvider' })
nostrSend: NostrSend | null = null
ready = false
pubDestination: string
// make the sub process accept client
constructor(pubDestination: string) {
if (!pubDestination) {
this.log("No pub provider to liquidity provider, will not be initialized")
}
this.client = newNostrClient({
pubDestination: pubDestination,
retrieveNostrUserAuth: async () => "",
}, this.clientSend, this.clientSub)
const interval = setInterval(() => {
if (this.ready) {
this.log("ready")
clearInterval(interval)
this.client.GetUserInfo().then(res => {
if (res.status === 'ERROR') {
this.log("error getting user info", res)
return
}
this.log("got user info", res)
})
}
}, 1000)
}
setClientId = (clientId: string) => {
this.clientId = clientId
if (this.nostrSend && this.pubDestination !== "") {
this.ready = true
}
}
attachNostrSend(f: NostrSend) {
this.nostrSend = f
if (this.clientId !== "" && this.pubDestination !== "") {
this.ready = true
}
}
IsReady = () => {
return this.ready
}
onEvent = async (res: { requestId: string }, fromPub: string) => {
if (fromPub !== this.pubDestination) {
return false
}
if (this.clientCbs[res.requestId]) {
const cb = this.clientCbs[res.requestId]
cb.f(res)
if (cb.type === 'single') {
const deleteOk = (delete this.clientCbs[res.requestId])
console.log(this.getSingleSubs(), "single subs left", deleteOk)
}
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)
console.log("subbing to single send", reqId, message.rpcName)
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) },
}
console.log("sub for", reqId, "was already registered, overriding")
return
}
this.nostrSend({ type: 'client', clientId: this.clientId }, {
type: 'content',
pub: to,
content: JSON.stringify(message)
})
console.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 { LndSettings, AddressPaidCb, InvoicePaidCb, NodeInfo, Invoice, DecodedInvoice, PaidInvoice, NewBlockCb, HtlcCb, BalanceInfo } from './settings.js';
import { getLogger } from '../helpers/logger.js'; import { getLogger } from '../helpers/logger.js';
import { HtlcEvent_EventType } from '../../../proto/lnd/router.js'; import { HtlcEvent_EventType } from '../../../proto/lnd/router.js';
import { LiquidityProvider } from './liquidityProvider.js';
const DeadLineMetadata = (deadline = 10 * 1000) => ({ deadline: Date.now() + deadline }) const DeadLineMetadata = (deadline = 10 * 1000) => ({ deadline: Date.now() + deadline })
const deadLndRetrySeconds = 5 const deadLndRetrySeconds = 5
export default class { export default class {
@ -34,7 +35,9 @@ export default class {
htlcCb: HtlcCb htlcCb: HtlcCb
log = getLogger({ component: 'lndManager' }) log = getLogger({ component: 'lndManager' })
outgoingOpsLocked = false outgoingOpsLocked = false
constructor(settings: LndSettings, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb, newBlockCb: NewBlockCb, htlcCb: HtlcCb) { liquidProvider: LiquidityProvider
useLiquidityProvider = false
constructor(settings: LndSettings, liquidProvider: LiquidityProvider, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb, newBlockCb: NewBlockCb, htlcCb: HtlcCb) {
this.settings = settings this.settings = settings
this.addressPaidCb = addressPaidCb this.addressPaidCb = addressPaidCb
this.invoicePaidCb = invoicePaidCb this.invoicePaidCb = invoicePaidCb
@ -60,6 +63,8 @@ export default class {
this.invoices = new InvoicesClient(transport) this.invoices = new InvoicesClient(transport)
this.router = new RouterClient(transport) this.router = new RouterClient(transport)
this.chainNotifier = new ChainNotifierClient(transport) this.chainNotifier = new ChainNotifierClient(transport)
this.liquidProvider = liquidProvider
this.useLiquidityProvider = !!settings.liquidityProviderPub
} }
LockOutgoingOperations(): void { LockOutgoingOperations(): void {

View file

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

View file

@ -15,6 +15,7 @@ import { UnsignedEvent } from '../nostr/tools/event.js'
import { NostrSend } from '../nostr/handler.js' import { NostrSend } from '../nostr/handler.js'
import MetricsManager from '../metrics/index.js' import MetricsManager from '../metrics/index.js'
import { LoggedEvent } from '../storage/eventsLog.js' import { LoggedEvent } from '../storage/eventsLog.js'
import { LiquidityProvider } from "../lnd/liquidityProvider.js"
type UserOperationsSub = { type UserOperationsSub = {
id: string id: string
@ -35,12 +36,14 @@ export default class {
paymentManager: PaymentManager paymentManager: PaymentManager
paymentSubs: Record<string, ((op: Types.UserOperation) => void) | null> = {} paymentSubs: Record<string, ((op: Types.UserOperation) => void) | null> = {}
metricsManager: MetricsManager metricsManager: MetricsManager
liquidProvider: LiquidityProvider
nostrSend: NostrSend = () => { getLogger({})("nostr send not initialized yet") } nostrSend: NostrSend = () => { getLogger({})("nostr send not initialized yet") }
constructor(settings: MainSettings, storage: Storage) { constructor(settings: MainSettings, storage: Storage, liquidProvider: LiquidityProvider) {
this.settings = settings this.settings = settings
this.storage = storage this.storage = storage
this.liquidProvider = liquidProvider
this.lnd = new LND(settings.lndSettings, this.addressPaidCb, this.invoicePaidCb, this.newBlockCb, this.htlcCb) 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.metricsManager = new MetricsManager(this.storage, this.lnd)
this.paymentManager = new PaymentManager(this.storage, this.lnd, this.settings, this.addressPaidCb, this.invoicePaidCb) this.paymentManager = new PaymentManager(this.storage, this.lnd, this.settings, this.addressPaidCb, this.invoicePaidCb)
@ -63,6 +66,7 @@ export default class {
attachNostrSend(f: NostrSend) { attachNostrSend(f: NostrSend) {
this.nostrSend = f this.nostrSend = f
this.liquidProvider.attachNostrSend(f)
} }
htlcCb: HtlcCb = (e) => { htlcCb: HtlcCb = (e) => {
@ -208,7 +212,7 @@ export default class {
return return
} }
const message: Types.LiveUserOperation & { requestId: string, status: 'OK' } = { operation: op, requestId: "GetLiveUserOperations", status: 'OK' } 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 }) { async UpdateBeacon(app: Application, content: { type: 'service', name: string }) {
@ -224,7 +228,7 @@ export default class {
pubkey: app.nostr_public_key, pubkey: app.nostr_public_key,
tags, 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) { async createZapReceipt(log: PubLogger, invoice: UserReceivingInvoice) {
@ -245,6 +249,6 @@ export default class {
tags, tags,
} }
log({ unsigned: event }) 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 { PubLogger, getLogger } from "../helpers/logger.js"
import { LiquidityProvider } from "../lnd/liquidityProvider.js"
import Storage from "../storage/index.js" import Storage from "../storage/index.js"
import { TypeOrmMigrationRunner } from "../storage/migrations/runner.js" import { TypeOrmMigrationRunner } from "../storage/migrations/runner.js"
import Main from "./index.js" import Main from "./index.js"
@ -16,7 +17,8 @@ export const initMainHandler = async (log: PubLogger, mainSettings: MainSettings
if (manualMigration) { if (manualMigration) {
return return
} }
const mainHandler = new Main(mainSettings, storageManager) const liquidityProvider = new LiquidityProvider(mainSettings.lndSettings.liquidityProviderPub)
const mainHandler = new Main(mainSettings, storageManager, liquidityProvider)
await mainHandler.lnd.Warmup() await mainHandler.lnd.Warmup()
if (!mainSettings.skipSanityCheck) { if (!mainSettings.skipSanityCheck) {
const sanityChecker = new SanityChecker(storageManager, mainHandler.lnd) const sanityChecker = new SanityChecker(storageManager, mainHandler.lnd)
@ -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 } 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}`
}
liquidityProvider.setClientId(liquidityProviderInfo.clientId)
const stop = await processArgs(mainHandler) const stop = await processArgs(mainHandler)
if (stop) { if (stop) {
return return
} }
return { mainHandler, apps } return { mainHandler, apps, liquidityProviderInfo }
} }
const processArgs = async (mainHandler: Main) => { 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 { AddressReceivingTransaction } from '../storage/entity/AddressReceivingTransaction.js'
import { UserTransactionPayment } from '../storage/entity/UserTransactionPayment.js' import { UserTransactionPayment } from '../storage/entity/UserTransactionPayment.js'
import { Watchdog } from './watchdog.js' import { Watchdog } from './watchdog.js'
import { LiquidityProvider } from '../lnd/liquidityProvider.js'
interface UserOperationInfo { interface UserOperationInfo {
serial_id: number serial_id: number
paid_amount: number paid_amount: number
@ -39,7 +40,6 @@ const defaultLnurlPayMetadata = `[["text/plain", "lnurl pay to Lightning.pub"]]`
const confInOne = 1000 * 1000 const confInOne = 1000 * 1000
const confInTwo = 100 * 1000 * 1000 const confInTwo = 100 * 1000 * 1000
export default class { export default class {
storage: Storage storage: Storage
settings: MainSettings settings: MainSettings
lnd: LND lnd: LND

View file

@ -49,7 +49,7 @@ export const LoadMainSettingsFromEnv = (): MainSettings => {
servicePort: EnvCanBeInteger("PORT", 1776), servicePort: EnvCanBeInteger("PORT", 1776),
recordPerformance: process.env.RECORD_PERFORMANCE === 'true' || false, recordPerformance: process.env.RECORD_PERFORMANCE === 'true' || false,
skipSanityCheck: process.env.SKIP_SANITY_CHECK === '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,
} }
} }

View file

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