Skip to content

Commit

Permalink
feat(BUX-395): Merge branch 'master' into feat-395-pike-capability
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain committed Mar 25, 2024
2 parents 9ba506e + a8c0275 commit 86df587
Show file tree
Hide file tree
Showing 22 changed files with 628 additions and 265 deletions.
30 changes: 7 additions & 23 deletions docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 7 additions & 23 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,11 @@
"type": "integer",
"example": 0
},
"paymail_external_derivation_num": {
"description": "PaymailExternalDerivationNum is the chain/num/(ext_derivation_num) location of the address related to the xPub.",
"type": "integer",
"example": 0
},
"type": {
"description": "Type is a destination's type.",
"type": "string",
Expand Down Expand Up @@ -2510,11 +2515,6 @@
"type": "string",
"example": "spvwallet.com"
},
"external_xpub_key": {
"description": "ExternalXpubKey is a paymail address's external xpub key.",
"type": "string",
"example": "xpub661MyMwAqRbcGpZVrSHU..."
},
"id": {
"description": "ID is a paymail address id.",
"type": "string",
Expand Down Expand Up @@ -3155,12 +3155,12 @@
}
},
"next_external_num": {
"description": "NextExternalNum is a next external num.",
"description": "NextExternalNum is the index derivation number use to generate NEXT external xPub (external xPub are used for address destinations).",
"type": "integer",
"example": 0
},
"next_internal_num": {
"description": "NextInternalNum is a next internal num.",
"description": "NextInternalNum is the index derivation number use to generate NEXT internal xPub (internal xPub are used for change destinations).",
"type": "integer",
"example": 0
},
Expand All @@ -3174,14 +3174,6 @@
"time.Duration": {
"type": "integer",
"enum": [
-9223372036854775808,
9223372036854775807,
1,
1000,
1000000,
1000000000,
60000000000,
3600000000000,
-9223372036854775808,
9223372036854775807,
1,
Expand All @@ -3192,14 +3184,6 @@
3600000000000
],
"x-enum-varnames": [
"minDuration",
"maxDuration",
"Nanosecond",
"Microsecond",
"Millisecond",
"Second",
"Minute",
"Hour",
"minDuration",
"maxDuration",
"Nanosecond",
Expand Down
31 changes: 9 additions & 22 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,11 @@ definitions:
description: Num is a destination's num representation.
example: 0
type: integer
paymail_external_derivation_num:
description: PaymailExternalDerivationNum is the chain/num/(ext_derivation_num)
location of the address related to the xPub.
example: 0
type: integer
type:
description: Type is a destination's type.
example: pubkeyhash
Expand Down Expand Up @@ -539,10 +544,6 @@ definitions:
description: Domain is a paymail address's domain (second part of paymail).
example: spvwallet.com
type: string
external_xpub_key:
description: ExternalXpubKey is a paymail address's external xpub key.
example: xpub661MyMwAqRbcGpZVrSHU...
type: string
id:
description: ID is a paymail address id.
example: c0ba4a52c89279268476a141be7569200cff2ca4892512b07ca75c25a95c16cd
Expand Down Expand Up @@ -1019,11 +1020,13 @@ definitions:
key2: value2
type: object
next_external_num:
description: NextExternalNum is a next external num.
description: NextExternalNum is the index derivation number use to generate
NEXT external xPub (external xPub are used for address destinations).
example: 0
type: integer
next_internal_num:
description: NextInternalNum is a next internal num.
description: NextInternalNum is the index derivation number use to generate
NEXT internal xPub (internal xPub are used for change destinations).
example: 0
type: integer
updated_at:
Expand All @@ -1041,14 +1044,6 @@ definitions:
- 1000000000
- 60000000000
- 3600000000000
- -9223372036854775808
- 9223372036854775807
- 1
- 1000
- 1000000
- 1000000000
- 60000000000
- 3600000000000
type: integer
x-enum-varnames:
- minDuration
Expand All @@ -1059,14 +1054,6 @@ definitions:
- Second
- Minute
- Hour
- minDuration
- maxDuration
- Nanosecond
- Microsecond
- Millisecond
- Second
- Minute
- Hour
transactions.NewTransaction:
properties:
config:
Expand Down
8 changes: 7 additions & 1 deletion engine/action_paymails.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *Client) NewPaymailAddress(ctx context.Context, xPubKey, address, public
ctx = c.GetOrStartTxn(ctx, "new_paymail_address")

// Get the xPub (make sure it exists)
_, err := getXpubWithCache(ctx, c, xPubKey, "", c.DefaultModelOptions()...)
xPub, err := getXpubWithCache(ctx, c, xPubKey, "", c.DefaultModelOptions()...)
if err != nil {
return nil, err
}
Expand All @@ -110,9 +110,15 @@ func (c *Client) NewPaymailAddress(ctx context.Context, xPubKey, address, public
return nil, err
}

externalXpubDerivation, err := xPub.GetNextExternalDerivationNum(ctx)
if err != nil {
return nil, err
}

// Start the new paymail address model
paymailAddress := newPaymail(
address,
externalXpubDerivation,
append(opts, c.DefaultModelOptions(
New(),
WithXPub(xPubKey),
Expand Down
2 changes: 1 addition & 1 deletion engine/action_paymails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var (
externalXPubID = "xpub69PUyEkuD8cqyA9ekUkp3FwaeW1uyLxbwybEy3bmyD7mM6zShsJqfRCv12B43h6KiEiZgF3BFSMnYLsVZr526n37qsqVXkPKYWQ8En2xbi1"
externalXPubID = "xpub6BYQwVS1dNAYoVVcN7cASn5qv4E7QEX4dCazSkJwAk89w32pAfbJqDLfibRW4ywEBwus54uCD8PwLYzahiyuMbyLujCT2oQD5z6QobaNyN1"
testAvatar = "https://i.imgur.com/MYSVX44.png"
testAvatar2 = "https://i.imgur.com/cBJKPDh.png"
testPaymail = "paymail@tester.com"
Expand Down
4 changes: 2 additions & 2 deletions engine/action_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (c *Client) NewTransaction(ctx context.Context, rawXpubKey string, config *
ctx = c.GetOrStartTxn(ctx, "new_transaction")

// Create the lock and set the release for after the function completes
unlock, err := newWaitWriteLock(
ctx, fmt.Sprintf(lockKeyProcessXpub, utils.Hash(rawXpubKey)), c.Cachestore(),
unlock, err := getWaitWriteLockForXpub(
ctx, c.Cachestore(), utils.Hash(rawXpubKey),
)
defer unlock()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions engine/client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func WithPaymailSupport(domains []string, defaultFromPaymail string, domainValid
}

// Add the paymail_address model in SPV Wallet Engine
c.addModels(migrateList, newPaymail(""))
c.addModels(migrateList, newPaymail("", 0))
}
}

Expand Down Expand Up @@ -519,7 +519,7 @@ func WithPaymailServerConfig(config *server.Configuration, defaultFromPaymail st
}

// Add the paymail_address model in SPV Wallet Engine
c.addModels(migrateList, newPaymail(""))
c.addModels(migrateList, newPaymail("", 0))
}
}

Expand Down
4 changes: 2 additions & 2 deletions engine/client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func TestWithModels(t *testing.T) {

t.Run("add custom models", func(t *testing.T) {
opts := DefaultClientOpts(false, true)
opts = append(opts, WithModels(newPaymail(testPaymail)))
opts = append(opts, WithModels(newPaymail(testPaymail, 0)))
opts = append(opts, WithLogger(&testLogger))

tc, err := NewClient(tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), opts...)
Expand Down Expand Up @@ -803,7 +803,7 @@ func TestWithAutoMigrate(t *testing.T) {

t.Run("one additional model", func(t *testing.T) {
opts := DefaultClientOpts(false, true)
opts = append(opts, WithAutoMigrate(newPaymail(testPaymail)))
opts = append(opts, WithAutoMigrate(newPaymail(testPaymail, 0)))
opts = append(opts, WithLogger(&testLogger))

tc, err := NewClient(tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), opts...)
Expand Down
14 changes: 13 additions & 1 deletion engine/locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package engine

import (
"context"
"fmt"

"github.com/mrz1836/go-cachestore"
)
Expand All @@ -10,7 +11,6 @@ const (
lockKeyProcessBroadcastTx = "process-broadcast-transaction-%s" // + Tx ID
lockKeyProcessP2PTx = "process-p2p-transaction-%s" // + Tx ID
lockKeyProcessSyncTx = "process-sync-transaction-task"
lockKeyProcessXpub = "action-xpub-id-%s" // + Xpub ID
lockKeyRecordTx = "action-record-transaction-%s" // + Tx ID
lockKeyReserveUtxo = "utxo-reserve-xpub-id-%s" // + Xpub ID
)
Expand All @@ -32,3 +32,15 @@ func newWaitWriteLock(ctx context.Context, lockKey string, cacheStore cachestore
_, _ = cacheStore.ReleaseLock(context.Background(), lockKey, secret)
}, err
}

func getWaitWriteLockForPaymail(ctx context.Context, cs cachestore.LockService, id string) (unlock func(), err error) {
lockKey := fmt.Sprintf("process-paymail-%s", id)
unlock, err = newWaitWriteLock(ctx, lockKey, cs)
return
}

func getWaitWriteLockForXpub(ctx context.Context, cs cachestore.LockService, id string) (unlock func(), err error) {
lockKey := fmt.Sprintf("action-xpub-id-%s", id)
unlock, err = newWaitWriteLock(ctx, lockKey, cs)
return
}
17 changes: 9 additions & 8 deletions engine/model_destinations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ type Destination struct {
Model `bson:",inline"`

// Model specific fields
ID string `json:"id" toml:"id" yaml:"id" gorm:"<-:create;type:char(64);primaryKey;comment:This is the hash of the locking script" bson:"_id"`
XpubID string `json:"xpub_id" toml:"xpub_id" yaml:"xpub_id" gorm:"<-:create;type:char(64);index;comment:This is the related xPub" bson:"xpub_id"`
LockingScript string `json:"locking_script" toml:"locking_script" yaml:"locking_script" gorm:"<-:create;type:text;comment:This is Bitcoin output script in hex" bson:"locking_script"`
Type string `json:"type" toml:"type" yaml:"type" gorm:"<-:create;type:text;comment:Type of output" bson:"type"`
Chain uint32 `json:"chain" toml:"chain" yaml:"chain" gorm:"<-:create;type:int;comment:This is the (chain)/num location of the address related to the xPub" bson:"chain"`
Num uint32 `json:"num" toml:"num" yaml:"num" gorm:"<-:create;type:int;comment:This is the chain/(num) location of the address related to the xPub" bson:"num"`
Address string `json:"address" toml:"address" yaml:"address" gorm:"<-:create;type:varchar(35);index;comment:This is the BitCoin address" bson:"address"`
DraftID string `json:"draft_id" toml:"draft_id" yaml:"draft_id" gorm:"<-:create;type:varchar(64);index;comment:This is the related draft id (if internal tx)" bson:"draft_id,omitempty"`
ID string `json:"id" toml:"id" yaml:"id" gorm:"<-:create;type:char(64);primaryKey;comment:This is the hash of the locking script" bson:"_id"`
XpubID string `json:"xpub_id" toml:"xpub_id" yaml:"xpub_id" gorm:"<-:create;type:char(64);index;comment:This is the related xPub" bson:"xpub_id"`
LockingScript string `json:"locking_script" toml:"locking_script" yaml:"locking_script" gorm:"<-:create;type:text;comment:This is Bitcoin output script in hex" bson:"locking_script"`
Type string `json:"type" toml:"type" yaml:"type" gorm:"<-:create;type:text;comment:Type of output" bson:"type"`
Chain uint32 `json:"chain" toml:"chain" yaml:"chain" gorm:"<-:create;type:int;comment:This is the (chain)/num location of the address related to the xPub" bson:"chain"`
Num uint32 `json:"num" toml:"num" yaml:"num" gorm:"<-:create;type:int;comment:This is the chain/(num) location of the address related to the xPub" bson:"num"`
PaymailExternalDerivationNum *uint32 `json:"paymail_external_derivation_num" toml:"paymail_external_derivation_num" yaml:"paymail_external_derivation_num" gorm:"<-:create;type:int;comment:This is the chain/num/(ext_derivation_num) location of the address related to the xPub" bson:"paymail_external_derivation_num"`
Address string `json:"address" toml:"address" yaml:"address" gorm:"<-:create;type:varchar(35);index;comment:This is the BitCoin address" bson:"address"`
DraftID string `json:"draft_id" toml:"draft_id" yaml:"draft_id" gorm:"<-:create;type:varchar(64);index;comment:This is the related draft id (if internal tx)" bson:"draft_id,omitempty"`
}

// newDestination will start a new Destination model for a locking script
Expand Down
Loading

0 comments on commit 86df587

Please # to comment.