diff --git a/client.go b/client.go index 0afbf948..0ad7903d 100644 --- a/client.go +++ b/client.go @@ -80,9 +80,10 @@ type ( // PaymailServerOptions is the options for the Paymail server PaymailServerOptions struct { - *server.Configuration // Server configuration if Paymail is enabled - DefaultFromPaymail string // IE: from@domain.com - DefaultNote string // IE: some note for address resolution + *server.Configuration // Server configuration if Paymail is enabled + options []server.ConfigOps // Options for the paymail server + DefaultFromPaymail string // IE: from@domain.com + DefaultNote string // IE: some note for address resolution } // taskManagerOptions holds the configuration for taskmanager diff --git a/client_internal.go b/client_internal.go index c7045394..be43b609 100644 --- a/client_internal.go +++ b/client_internal.go @@ -103,12 +103,29 @@ func (c *Client) registerAllTasks() error { // loadDefaultPaymailConfig will load the default paymail server configuration func (c *Client) loadDefaultPaymailConfig() (err error) { - c.options.paymail.serverConfig.DefaultFromPaymail = defaultSenderPaymail - c.options.paymail.serverConfig.DefaultNote = defaultAddressResolutionPurpose + + // Default FROM paymail + if len(c.options.paymail.serverConfig.DefaultFromPaymail) == 0 { + c.options.paymail.serverConfig.DefaultFromPaymail = defaultSenderPaymail + } + + // Default note for address resolution + if len(c.options.paymail.serverConfig.DefaultNote) == 0 { + c.options.paymail.serverConfig.DefaultNote = defaultAddressResolutionPurpose + } + + // Set default options if none are found + if len(c.options.paymail.serverConfig.options) == 0 { + c.options.paymail.serverConfig.options = append(c.options.paymail.serverConfig.options, + server.WithGenericCapabilities(), + server.WithDomainValidationDisabled(), + ) + } + + // Create the paymail configuration using the client and default service provider c.options.paymail.serverConfig.Configuration, err = server.NewConfig( &PaymailServiceProvider{client: c}, - server.WithGenericCapabilities(), - server.WithDomainValidationDisabled(), + c.options.paymail.serverConfig.options..., ) return } diff --git a/client_options.go b/client_options.go index ccbf39fe..c6bbd4a0 100644 --- a/client_options.go +++ b/client_options.go @@ -71,6 +71,7 @@ func defaultClientOptions() *clientOptions { client: nil, serverConfig: &PaymailServerOptions{ Configuration: nil, + options: []server.ConfigOps{}, }, }, @@ -424,7 +425,42 @@ func WithPaymailClient(client paymail.ClientInterface) ClientOps { } // WithPaymailServer will set the server configuration for Paymail -func WithPaymailServer(config *server.Configuration, defaultFromPaymail, defaultNote string) ClientOps { +func WithPaymailServer(domains []string, defaultFromPaymail, defaultNote string, + domainValidation, senderValidation bool) ClientOps { + return func(c *clientOptions) { + + // Add generic capabilities + c.paymail.serverConfig.options = append(c.paymail.serverConfig.options, server.WithGenericCapabilities()) + + // Add each domain + for _, domain := range domains { + c.paymail.serverConfig.options = append(c.paymail.serverConfig.options, server.WithDomain(domain)) + } + + // Set the sender validation + if senderValidation { + c.paymail.serverConfig.options = append(c.paymail.serverConfig.options, server.WithSenderValidation()) + } + + // Domain validation + if !domainValidation { + c.paymail.serverConfig.options = append(c.paymail.serverConfig.options, server.WithDomainValidationDisabled()) + } + + // Add default values + if len(defaultFromPaymail) > 0 { + c.paymail.serverConfig.DefaultFromPaymail = defaultFromPaymail + } + if len(defaultNote) > 0 { + c.paymail.serverConfig.DefaultNote = defaultNote + } + } +} + +// WithPaymailServerConfig will set the custom server configuration for Paymail +// +// This will allow overriding the Configuration.actions (paymail service provider) +func WithPaymailServerConfig(config *server.Configuration, defaultFromPaymail, defaultNote string) ClientOps { return func(c *clientOptions) { if config != nil { c.paymail.serverConfig.Configuration = config diff --git a/client_test.go b/client_test.go index 86673c15..c802535c 100644 --- a/client_test.go +++ b/client_test.go @@ -187,12 +187,12 @@ func TestClient_ModifyPaymailConfig(t *testing.T) { t.Run("update from sender and note", func(t *testing.T) { opts := DefaultClientOpts(false, true) - opts = append(opts, WithPaymailServer(&server.Configuration{ - APIVersion: "v1", - BSVAliasVersion: paymail.DefaultBsvAliasVersion, - Port: paymail.DefaultPort, - ServiceName: paymail.DefaultServiceName, - }, defaultSenderPaymail, defaultAddressResolutionPurpose)) + opts = append(opts, WithPaymailServer( + []string{testDomain}, + defaultSenderPaymail, + defaultAddressResolutionPurpose, + false, false, + )) tc, err := NewClient(context.Background(), opts...) require.NoError(t, err) @@ -208,12 +208,12 @@ func TestClient_ModifyPaymailConfig(t *testing.T) { t.Run("update server config", func(t *testing.T) { opts := DefaultClientOpts(false, true) - opts = append(opts, WithPaymailServer(&server.Configuration{ - APIVersion: "v1", - BSVAliasVersion: paymail.DefaultBsvAliasVersion, - Port: paymail.DefaultPort, - ServiceName: paymail.DefaultServiceName, - }, defaultSenderPaymail, defaultAddressResolutionPurpose)) + opts = append(opts, WithPaymailServer( + []string{testDomain}, + defaultSenderPaymail, + defaultAddressResolutionPurpose, + false, false, + )) tc, err := NewClient(context.Background(), opts...) require.NoError(t, err) @@ -252,12 +252,12 @@ func TestClient_PaymailServerConfig(t *testing.T) { t.Run("valid paymail server config", func(t *testing.T) { opts := DefaultClientOpts(false, true) - opts = append(opts, WithPaymailServer(&server.Configuration{ - APIVersion: "v1", - BSVAliasVersion: paymail.DefaultBsvAliasVersion, - Port: paymail.DefaultPort, - ServiceName: paymail.DefaultServiceName, - }, defaultSenderPaymail, defaultAddressResolutionPurpose)) + opts = append(opts, WithPaymailServer( + []string{testDomain}, + defaultSenderPaymail, + defaultAddressResolutionPurpose, + false, false, + )) tc, err := NewClient(context.Background(), opts...) require.NoError(t, err) @@ -345,12 +345,12 @@ func TestPaymailOptions_ServerConfig(t *testing.T) { t.Run("valid server config", func(t *testing.T) { opts := DefaultClientOpts(false, true) - opts = append(opts, WithPaymailServer(&server.Configuration{ - APIVersion: "v1", - BSVAliasVersion: paymail.DefaultBsvAliasVersion, - Port: paymail.DefaultPort, - ServiceName: paymail.DefaultServiceName, - }, defaultSenderPaymail, defaultAddressResolutionPurpose)) + opts = append(opts, WithPaymailServer( + []string{testDomain}, + defaultSenderPaymail, + defaultAddressResolutionPurpose, + false, false, + )) tc, err := NewClient(context.Background(), opts...) require.NoError(t, err) diff --git a/interface.go b/interface.go index 1e874dd3..15c19235 100644 --- a/interface.go +++ b/interface.go @@ -72,9 +72,20 @@ type PaymailService interface { metadata Metadata, opts ...ModelOps) (*PaymailAddress, error) } +// ClientServices is the client related services +type ClientServices interface { + Cachestore() cachestore.ClientInterface + Chainstate() chainstate.ClientInterface + Datastore() datastore.ClientInterface + Logger() logger.Interface + PaymailClient() paymail.ClientInterface + Taskmanager() taskmanager.ClientInterface +} + // ClientInterface is the client (bux engine) interface comprised of all services type ClientInterface interface { AccessKeyService + ClientServices DestinationService PaymailService TransactionService @@ -82,10 +93,7 @@ type ClientInterface interface { XPubService AddModels(ctx context.Context, autoMigrate bool, models ...interface{}) error AuthenticateRequest(ctx context.Context, req *http.Request, adminXPubs []string, adminRequired, requireSigning, signingDisabled bool) (*http.Request, error) - Cachestore() cachestore.ClientInterface - Chainstate() chainstate.ClientInterface Close(ctx context.Context) error - Datastore() datastore.ClientInterface Debug(on bool) DefaultModelOptions(opts ...ModelOps) []ModelOps EnableNewRelic() @@ -96,12 +104,9 @@ type ClientInterface interface { IsITCEnabled() bool IsIUCEnabled() bool IsNewRelicEnabled() bool - Logger() logger.Interface ModifyPaymailConfig(config *server.Configuration, defaultFromPaymail, defaultNote string) ModifyTaskPeriod(name string, period time.Duration) error - PaymailClient() paymail.ClientInterface PaymailServerConfig() *PaymailServerOptions - Taskmanager() taskmanager.ClientInterface UserAgent() string Version() string }