Skip to content

Commit 660940e

Browse files
committed
tapdb: add FetchScriptKeyStore interface
1 parent 154644f commit 660940e

File tree

3 files changed

+86
-35
lines changed

3 files changed

+86
-35
lines changed

tapdb/addrs.go

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ type AddrBook interface {
9292
// asset groups related to them.
9393
GroupStore
9494

95+
// FetchScriptKeyStore houses the methods related to fetching all
96+
// information about a script key.
97+
FetchScriptKeyStore
98+
9599
// FetchAddrs returns all the addresses based on the constraints of the
96100
// passed AddrQuery.
97101
FetchAddrs(ctx context.Context, arg AddrQuery) ([]Addresses, error)
@@ -153,11 +157,6 @@ type AddrBook interface {
153157
FetchGenesisByAssetID(ctx context.Context,
154158
assetID []byte) (sqlc.GenesisInfoView, error)
155159

156-
// FetchScriptKeyByTweakedKey attempts to fetch the script key and
157-
// corresponding internal key from the database.
158-
FetchScriptKeyByTweakedKey(ctx context.Context,
159-
tweakedScriptKey []byte) (ScriptKey, error)
160-
161160
// FetchInternalKeyLocator fetches the key locator for an internal key.
162161
FetchInternalKeyLocator(ctx context.Context, rawKey []byte) (KeyLocator,
163162
error)
@@ -1158,43 +1157,21 @@ func (t *TapAddressBook) FetchScriptKey(ctx context.Context,
11581157
tweakedScriptKey *btcec.PublicKey) (*asset.TweakedScriptKey, error) {
11591158

11601159
var (
1161-
readOpts = NewAddrBookReadTx()
11621160
scriptKey *asset.TweakedScriptKey
1161+
err error
11631162
)
1164-
err := t.db.ExecTx(ctx, &readOpts, func(db AddrBook) error {
1165-
dbKey, err := db.FetchScriptKeyByTweakedKey(
1166-
ctx, tweakedScriptKey.SerializeCompressed(),
1167-
)
1168-
if err != nil {
1169-
return err
1170-
}
11711163

1172-
rawKey, err := btcec.ParsePubKey(dbKey.RawKey)
1173-
if err != nil {
1174-
return fmt.Errorf("unable to parse raw key: %w", err)
1175-
}
1176-
1177-
scriptKey = &asset.TweakedScriptKey{
1178-
Tweak: dbKey.Tweak,
1179-
RawKey: keychain.KeyDescriptor{
1180-
PubKey: rawKey,
1181-
KeyLocator: keychain.KeyLocator{
1182-
Family: keychain.KeyFamily(
1183-
dbKey.KeyFamily,
1184-
),
1185-
Index: uint32(dbKey.KeyIndex),
1186-
},
1187-
},
1188-
DeclaredKnown: dbKey.DeclaredKnown.Valid,
1189-
}
1190-
1191-
return nil
1164+
readOpts := NewAddrBookReadTx()
1165+
dbErr := t.db.ExecTx(ctx, &readOpts, func(db AddrBook) error {
1166+
scriptKey, err = fetchScriptKey(ctx, db, tweakedScriptKey)
1167+
return err
11921168
})
1169+
11931170
switch {
1194-
case errors.Is(err, sql.ErrNoRows):
1171+
case errors.Is(dbErr, sql.ErrNoRows):
11951172
return nil, address.ErrScriptKeyNotFound
11961173

1197-
case err != nil:
1174+
case dbErr != nil:
11981175
return nil, err
11991176
}
12001177

tapdb/asset_minting.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ type PendingAssetStore interface {
140140
// GroupStore houses the methods related to querying asset groups.
141141
GroupStore
142142

143+
// FetchScriptKeyStore houses the methods related to fetching all
144+
// information about a script key.
145+
FetchScriptKeyStore
146+
143147
// TapscriptTreeStore houses the methods related to storing, fetching,
144148
// and deleting tapscript trees.
145149
TapscriptTreeStore
@@ -1695,6 +1699,33 @@ func (a *AssetMintingStore) FetchGroupByGroupKey(ctx context.Context,
16951699
return dbGroup, nil
16961700
}
16971701

1702+
// FetchScriptKeyByTweakedKey fetches the populated script key given the tweaked
1703+
// script key.
1704+
func (a *AssetMintingStore) FetchScriptKeyByTweakedKey(ctx context.Context,
1705+
tweakedKey *btcec.PublicKey) (*asset.TweakedScriptKey, error) {
1706+
1707+
var (
1708+
scriptKey *asset.TweakedScriptKey
1709+
err error
1710+
)
1711+
1712+
readOpts := NewAssetStoreReadTx()
1713+
dbErr := a.db.ExecTx(ctx, &readOpts, func(q PendingAssetStore) error {
1714+
scriptKey, err = fetchScriptKey(ctx, q, tweakedKey)
1715+
return err
1716+
})
1717+
1718+
switch {
1719+
case errors.Is(dbErr, sql.ErrNoRows):
1720+
return nil, fmt.Errorf("script key not found")
1721+
1722+
case dbErr != nil:
1723+
return nil, err
1724+
}
1725+
1726+
return scriptKey, nil
1727+
}
1728+
16981729
// StoreTapscriptTree persists a Tapscript tree given a validated set of
16991730
// TapLeafs or a TapBranch. If the store succeeds, the root hash of the
17001731
// Tapscript tree is returned.

tapdb/assets_common.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,49 @@ func upsertScriptKey(ctx context.Context, scriptKey asset.ScriptKey,
422422
return scriptKeyID, nil
423423
}
424424

425+
// FetchScriptKeyStore houses the methods related to fetching all information
426+
// about a script key.
427+
type FetchScriptKeyStore interface {
428+
// FetchScriptKeyByTweakedKey attempts to fetch the script key and
429+
// corresponding internal key from the database.
430+
FetchScriptKeyByTweakedKey(ctx context.Context,
431+
tweakedScriptKey []byte) (ScriptKey, error)
432+
}
433+
434+
// fetchScriptKey attempts to fetch the full tweaked script key struct
435+
// (including the key descriptor) for the given tweaked script key.
436+
func fetchScriptKey(ctx context.Context, q FetchScriptKeyStore,
437+
tweakedScriptKey *btcec.PublicKey) (*asset.TweakedScriptKey, error) {
438+
439+
dbKey, err := q.FetchScriptKeyByTweakedKey(
440+
ctx, tweakedScriptKey.SerializeCompressed(),
441+
)
442+
if err != nil {
443+
return nil, err
444+
}
445+
446+
rawKey, err := btcec.ParsePubKey(dbKey.RawKey)
447+
if err != nil {
448+
return nil, fmt.Errorf("unable to parse raw key: %w", err)
449+
}
450+
451+
scriptKey := &asset.TweakedScriptKey{
452+
Tweak: dbKey.Tweak,
453+
RawKey: keychain.KeyDescriptor{
454+
PubKey: rawKey,
455+
KeyLocator: keychain.KeyLocator{
456+
Family: keychain.KeyFamily(
457+
dbKey.KeyFamily,
458+
),
459+
Index: uint32(dbKey.KeyIndex),
460+
},
461+
},
462+
DeclaredKnown: dbKey.DeclaredKnown.Valid,
463+
}
464+
465+
return scriptKey, nil
466+
}
467+
425468
// FetchGenesisStore houses the methods related to fetching genesis assets.
426469
type FetchGenesisStore interface {
427470
// FetchGenesisByID returns a single genesis asset by its primary key

0 commit comments

Comments
 (0)