Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Added private field for decrypted value
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Apr 6, 2022
1 parent b3020be commit b0fa955
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
7 changes: 7 additions & 0 deletions definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -162,5 +164,10 @@ var (
&Utxo{
Model: *NewBaseModel(ModelUtxo),
},

// Paymail addresses related to XPubs (automatically added when paymail is enabled)
/*&PaymailAddress{
Model: *NewBaseModel(ModelPaymailAddress),
},*/
}
)
31 changes: 17 additions & 14 deletions model_paymails.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions model_paymails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion paymail_service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b0fa955

Please sign in to comment.