From b0fa955dbd3da9e18a57b8e715bc8e5109fe7dbe Mon Sep 17 00:00:00 2001 From: mrz1836 Date: Wed, 6 Apr 2022 09:50:56 -0400 Subject: [PATCH] Added private field for decrypted value --- definitions.go | 7 +++++++ model_paymails.go | 31 +++++++++++++++++-------------- model_paymails_test.go | 4 ++-- paymail_service_provider.go | 2 +- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/definitions.go b/definitions.go index f8073909..9843d763 100644 --- a/definitions.go +++ b/definitions.go @@ -69,8 +69,10 @@ const ( ReferenceIDField = "reference_id" // Internal field names + aliasField = "alias" broadcastStatusField = "broadcast_status" currentBalanceField = "current_balance" + domainField = "domain" draftIDField = "draft_id" idField = "id" metadataField = "metadata" @@ -162,5 +164,10 @@ var ( &Utxo{ Model: *NewBaseModel(ModelUtxo), }, + + // Paymail addresses related to XPubs (automatically added when paymail is enabled) + /*&PaymailAddress{ + Model: *NewBaseModel(ModelPaymailAddress), + },*/ } ) diff --git a/model_paymails.go b/model_paymails.go index 6f2deac3..f4423c92 100644 --- a/model_paymails.go +++ b/model_paymails.go @@ -28,6 +28,9 @@ type PaymailAddress struct { Username string `json:"username" toml:"username" yaml:"username" gorm:"<-;type:varchar(255);uniqueIndex;comment:This is username" bson:"username"` // Full username Avatar string `json:"avatar" toml:"avatar" yaml:"avatar" gorm:"<-;type:text;comment:This is avatar url" bson:"avatar"` // This is the url of the user (public profile) ExternalXpubKey string `json:"external_xpub_key" toml:"external_xpub_key" yaml:"external_xpub_key" gorm:"<-:create;type:varchar(512);index;comment:This is full xPub for external use, encryption optional" bson:"external_xpub_key"` // PublicKey hex encoded + + // Private fields + externalXpubKeyDecrypted string } // newPaymail create new paymail model @@ -57,8 +60,8 @@ func getPaymail(ctx context.Context, address string, opts ...ModelOps) (*Paymail paymailAddress := newPaymail(address, opts...) paymailAddress.ID = "" conditions := map[string]interface{}{ - "alias": paymailAddress.Alias, - "domain": paymailAddress.Domain, + aliasField: paymailAddress.Alias, + domainField: paymailAddress.Domain, } if err := Get( @@ -114,11 +117,14 @@ func (m *PaymailAddress) setXPub() error { return err } + // Set the decrypted version + m.externalXpubKeyDecrypted = paymailExternalKey.String() + // Encrypt the xPub if len(m.encryptionKey) > 0 { - m.ExternalXpubKey, err = utils.Encrypt(m.encryptionKey, paymailExternalKey.String()) + m.ExternalXpubKey, err = utils.Encrypt(m.encryptionKey, m.externalXpubKeyDecrypted) } else { - m.ExternalXpubKey = paymailExternalKey.String() + m.ExternalXpubKey = m.externalXpubKeyDecrypted } return err @@ -143,20 +149,19 @@ func (m *PaymailAddress) GetIdentityXpub() (*bip32.ExtendedKey, error) { func (m *PaymailAddress) GetExternalXpub() (*bip32.ExtendedKey, error) { // Check if the xPub was encrypted - var externalKey string if len(m.ExternalXpubKey) != utils.XpubKeyLength { var err error - if externalKey, err = utils.Decrypt( + if m.externalXpubKeyDecrypted, err = utils.Decrypt( m.encryptionKey, m.ExternalXpubKey, ); err != nil { return nil, err } } else { - externalKey = m.ExternalXpubKey + m.externalXpubKeyDecrypted = m.ExternalXpubKey } // Get the xPub - xPub, err := bitcoin.GetHDKeyFromExtendedPublicKey(externalKey) + xPub, err := bitcoin.GetHDKeyFromExtendedPublicKey(m.externalXpubKeyDecrypted) if err != nil { return nil, err } @@ -187,10 +192,6 @@ func (m *PaymailAddress) GetID() string { func (m *PaymailAddress) BeforeCreating(_ context.Context) (err error) { m.DebugLog("starting: " + m.Name() + " BeforeCreating hook...") - if _, err = utils.ValidateXPub(m.ExternalXpubKey); err != nil { - return - } - if m.ID == "" { return ErrMissingPaymailID } @@ -205,6 +206,10 @@ func (m *PaymailAddress) BeforeCreating(_ context.Context) (err error) { if len(m.ExternalXpubKey) == 0 { return ErrMissingPaymailExternalXPub + } else if len(m.externalXpubKeyDecrypted) > 0 { + if _, err = utils.ValidateXPub(m.externalXpubKeyDecrypted); err != nil { + return + } } if len(m.XpubID) == 0 { @@ -234,14 +239,12 @@ func (m *PaymailAddress) Migrate(client datastore.ClientInterface) error { // migratePostgreSQL is specific migration SQL for Postgresql func (m *PaymailAddress) migratePostgreSQL(client datastore.ClientInterface, tableName string) error { - tx := client.Execute(`CREATE UNIQUE INDEX IF NOT EXISTS "idx_paymail_address" ON "` + tableName + `" ("alias","domain")`) return tx.Error } // migrateMySQL is specific migration SQL for MySQL func (m *PaymailAddress) migrateMySQL(client datastore.ClientInterface, tableName string) error { - tx := client.Execute("CREATE UNIQUE INDEX idx_paymail_address ON `" + tableName + "` (alias, domain)") return tx.Error } diff --git a/model_paymails_test.go b/model_paymails_test.go index 0169772c..8b783b98 100644 --- a/model_paymails_test.go +++ b/model_paymails_test.go @@ -58,8 +58,8 @@ func TestNewPaymail(t *testing.T) { ) p2.ID = "" // Remove ID (to make query work) conditions := map[string]interface{}{ - "alias": p.Alias, - "domain": p.Domain, + aliasField: p.Alias, + domainField: p.Domain, } err = Get(ctx, p2, conditions, false, 0) require.NoError(t, err) diff --git a/paymail_service_provider.go b/paymail_service_provider.go index 6226f98f..c31b7783 100644 --- a/paymail_service_provider.go +++ b/paymail_service_provider.go @@ -34,7 +34,7 @@ func (p *PaymailDefaultServiceProvider) createMetadata(serverMetaData *server.Re metadata["note"] = serverMetaData.Note } if serverMetaData.Domain != "" { - metadata["domain"] = serverMetaData.Domain + metadata[domainField] = serverMetaData.Domain } if serverMetaData.IPAddress != "" { metadata["ip_address"] = serverMetaData.IPAddress