Skip to content

Commit c14ccf4

Browse files
committed
tapdb: use embeddings for script key
1 parent def2bfb commit c14ccf4

File tree

10 files changed

+227
-336
lines changed

10 files changed

+227
-336
lines changed

tapdb/addrs.go

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -404,22 +404,13 @@ func (t *TapAddressBook) QueryAddrs(ctx context.Context,
404404
}
405405
}
406406

407-
rawScriptKey, err := btcec.ParsePubKey(
408-
addr.RawScriptKey,
407+
scriptKey, err := parseScriptKey(
408+
addr.InternalKey, addr.ScriptKey,
409409
)
410410
if err != nil {
411411
return fmt.Errorf("unable to decode "+
412412
"script key: %w", err)
413413
}
414-
rawScriptKeyDesc := keychain.KeyDescriptor{
415-
KeyLocator: keychain.KeyLocator{
416-
Family: keychain.KeyFamily(
417-
addr.ScriptKeyFamily,
418-
),
419-
Index: uint32(addr.ScriptKeyIndex),
420-
},
421-
PubKey: rawScriptKey,
422-
}
423414

424415
internalKey, err := btcec.ParsePubKey(addr.RawTaprootKey)
425416
if err != nil {
@@ -436,11 +427,6 @@ func (t *TapAddressBook) QueryAddrs(ctx context.Context,
436427
PubKey: internalKey,
437428
}
438429

439-
scriptKey, err := btcec.ParsePubKey(addr.TweakedScriptKey)
440-
if err != nil {
441-
return err
442-
}
443-
444430
taprootOutputKey, err := schnorr.ParsePubKey(
445431
addr.TaprootOutputKey,
446432
)
@@ -467,8 +453,8 @@ func (t *TapAddressBook) QueryAddrs(ctx context.Context,
467453

468454
tapAddr, err := address.New(
469455
address.Version(addr.Version), assetGenesis,
470-
groupKey, groupWitness,
471-
*scriptKey, *internalKey, uint64(addr.Amount),
456+
groupKey, groupWitness, *scriptKey.PubKey,
457+
*internalKey, uint64(addr.Amount),
472458
tapscriptSibling, t.params, *proofCourierAddr,
473459
address.WithAssetVersion(
474460
asset.Version(addr.AssetVersion),
@@ -478,16 +464,9 @@ func (t *TapAddressBook) QueryAddrs(ctx context.Context,
478464
return fmt.Errorf("unable to make addr: %w", err)
479465
}
480466

481-
declaredKnown := extractBool(
482-
addr.ScriptKeyDeclaredKnown,
483-
)
484467
addrs = append(addrs, address.AddrWithKeyInfo{
485-
Tap: tapAddr,
486-
ScriptKeyTweak: asset.TweakedScriptKey{
487-
RawKey: rawScriptKeyDesc,
488-
Tweak: addr.ScriptKeyTweak,
489-
DeclaredKnown: declaredKnown,
490-
},
468+
Tap: tapAddr,
469+
ScriptKeyTweak: *scriptKey.TweakedScriptKey,
491470
InternalKeyDesc: internalKeyDesc,
492471
TaprootOutputKey: *taprootOutputKey,
493472
CreationTime: addr.CreationTime.UTC(),
@@ -571,21 +550,7 @@ func fetchAddr(ctx context.Context, db AddrBook, params *address.ChainParams,
571550
}
572551
}
573552

574-
rawScriptKey, err := btcec.ParsePubKey(dbAddr.RawScriptKey)
575-
if err != nil {
576-
return nil, fmt.Errorf("unable to decode script key: %w", err)
577-
}
578-
scriptKeyDesc := keychain.KeyDescriptor{
579-
KeyLocator: keychain.KeyLocator{
580-
Family: keychain.KeyFamily(
581-
dbAddr.ScriptKeyFamily,
582-
),
583-
Index: uint32(dbAddr.ScriptKeyIndex),
584-
},
585-
PubKey: rawScriptKey,
586-
}
587-
588-
scriptKey, err := btcec.ParsePubKey(dbAddr.TweakedScriptKey)
553+
scriptKey, err := parseScriptKey(dbAddr.InternalKey, dbAddr.ScriptKey)
589554
if err != nil {
590555
return nil, fmt.Errorf("unable to decode script key: %w", err)
591556
}
@@ -622,23 +587,18 @@ func fetchAddr(ctx context.Context, db AddrBook, params *address.ChainParams,
622587

623588
tapAddr, err := address.New(
624589
address.Version(dbAddr.Version), genesis, groupKey,
625-
groupWitness, *scriptKey, *internalKey, uint64(dbAddr.Amount),
626-
tapscriptSibling, params, *proofCourierAddr,
590+
groupWitness, *scriptKey.PubKey, *internalKey,
591+
uint64(dbAddr.Amount), tapscriptSibling, params,
592+
*proofCourierAddr,
627593
address.WithAssetVersion(asset.Version(dbAddr.AssetVersion)),
628594
)
629595
if err != nil {
630596
return nil, fmt.Errorf("unable to make addr: %w", err)
631597
}
632598

633599
return &address.AddrWithKeyInfo{
634-
Tap: tapAddr,
635-
ScriptKeyTweak: asset.TweakedScriptKey{
636-
RawKey: scriptKeyDesc,
637-
Tweak: dbAddr.ScriptKeyTweak,
638-
DeclaredKnown: extractBool(
639-
dbAddr.ScriptKeyDeclaredKnown,
640-
),
641-
},
600+
Tap: tapAddr,
601+
ScriptKeyTweak: *scriptKey.TweakedScriptKey,
642602
InternalKeyDesc: internalKeyDesc,
643603
TaprootOutputKey: *taprootOutputKey,
644604
CreationTime: dbAddr.CreationTime.UTC(),

tapdb/asset_minting.go

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -853,35 +853,12 @@ func fetchAssetSprouts(ctx context.Context, q PendingAssetStore,
853853
for i, sprout := range dbSprout {
854854
// First, we'll decode the script key which very asset must
855855
// specify, and populate the key locator information
856-
tweakedScriptKey, err := btcec.ParsePubKey(
857-
sprout.TweakedScriptKey,
856+
scriptKey, err := parseScriptKey(
857+
sprout.InternalKey, sprout.ScriptKey,
858858
)
859859
if err != nil {
860-
return nil, err
861-
}
862-
863-
internalScriptKey, err := btcec.ParsePubKey(
864-
sprout.ScriptKeyRaw,
865-
)
866-
if err != nil {
867-
return nil, err
868-
}
869-
870-
scriptKeyDesc := keychain.KeyDescriptor{
871-
PubKey: internalScriptKey,
872-
KeyLocator: keychain.KeyLocator{
873-
Index: uint32(sprout.ScriptKeyIndex),
874-
Family: keychain.KeyFamily(sprout.ScriptKeyFam),
875-
},
876-
}
877-
declaredKnown := extractBool(sprout.ScriptKeyDeclaredKnown)
878-
scriptKey := asset.ScriptKey{
879-
PubKey: tweakedScriptKey,
880-
TweakedScriptKey: &asset.TweakedScriptKey{
881-
RawKey: scriptKeyDesc,
882-
Tweak: sprout.Tweak,
883-
DeclaredKnown: declaredKnown,
884-
},
860+
return nil, fmt.Errorf("unable to decode script key: "+
861+
"%w", err)
885862
}
886863

887864
// Not all assets have a key group, so we only need to

tapdb/assets_common.go

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -485,26 +485,59 @@ func fetchScriptKey(ctx context.Context, q FetchScriptKeyStore,
485485
return nil, err
486486
}
487487

488-
rawKey, err := btcec.ParsePubKey(dbKey.RawKey)
488+
scriptKey, err := parseScriptKey(dbKey.InternalKey, dbKey.ScriptKey)
489489
if err != nil {
490-
return nil, fmt.Errorf("unable to parse raw key: %w", err)
491-
}
492-
493-
scriptKey := &asset.TweakedScriptKey{
494-
Tweak: dbKey.Tweak,
495-
RawKey: keychain.KeyDescriptor{
496-
PubKey: rawKey,
497-
KeyLocator: keychain.KeyLocator{
498-
Family: keychain.KeyFamily(
499-
dbKey.KeyFamily,
500-
),
501-
Index: uint32(dbKey.KeyIndex),
490+
return nil, err
491+
}
492+
493+
return scriptKey.TweakedScriptKey, nil
494+
}
495+
496+
// parseScriptKey maps a script key and internal key from the database into a
497+
// ScriptKey struct. Both the internal raw public key and the tweaked public key
498+
// must be set and valid.
499+
func parseScriptKey(ik sqlc.InternalKey, sk sqlc.ScriptKey) (asset.ScriptKey,
500+
error) {
501+
502+
var emptyKey asset.ScriptKey
503+
if len(sk.TweakedScriptKey) == 0 {
504+
return emptyKey, fmt.Errorf("tweaked script key is empty")
505+
}
506+
507+
if len(ik.RawKey) == 0 {
508+
return emptyKey, fmt.Errorf("internal raw key is empty")
509+
}
510+
511+
var (
512+
locator = keychain.KeyLocator{
513+
Index: uint32(ik.KeyIndex),
514+
Family: keychain.KeyFamily(ik.KeyFamily),
515+
}
516+
result = asset.ScriptKey{
517+
TweakedScriptKey: &asset.TweakedScriptKey{
518+
RawKey: keychain.KeyDescriptor{
519+
KeyLocator: locator,
520+
},
521+
Tweak: sk.Tweak,
522+
DeclaredKnown: extractBool(sk.DeclaredKnown),
502523
},
503-
},
504-
DeclaredKnown: extractBool(dbKey.DeclaredKnown),
524+
}
525+
err error
526+
)
527+
528+
result.PubKey, err = btcec.ParsePubKey(sk.TweakedScriptKey)
529+
if err != nil {
530+
return emptyKey, fmt.Errorf("error parsing tweaked script "+
531+
"key: %w", err)
532+
}
533+
534+
result.RawKey.PubKey, err = btcec.ParsePubKey(ik.RawKey)
535+
if err != nil {
536+
return emptyKey, fmt.Errorf("error parsing internal raw "+
537+
"key: %w", err)
505538
}
506539

507-
return scriptKey, nil
540+
return result, nil
508541
}
509542

510543
// FetchGenesisStore houses the methods related to fetching genesis assets.

tapdb/assets_store.go

Lines changed: 20 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -633,16 +633,12 @@ func (a *AssetStore) dbAssetsToChainAssets(dbAssets []ConfirmedAsset,
633633

634634
// First, we'll decode the script key which every asset must
635635
// specify, and populate the key locator information.
636-
rawScriptKeyPub, err := btcec.ParsePubKey(sprout.ScriptKeyRaw)
636+
scriptKey, err := parseScriptKey(
637+
sprout.InternalKey, sprout.ScriptKey,
638+
)
637639
if err != nil {
638-
return nil, err
639-
}
640-
rawScriptKeyDesc := keychain.KeyDescriptor{
641-
PubKey: rawScriptKeyPub,
642-
KeyLocator: keychain.KeyLocator{
643-
Index: uint32(sprout.ScriptKeyIndex),
644-
Family: keychain.KeyFamily(sprout.ScriptKeyFam),
645-
},
640+
return nil, fmt.Errorf("unable to decode script key: "+
641+
"%w", err)
646642
}
647643

648644
// Not all assets have a key group, so we only need to
@@ -726,20 +722,6 @@ func (a *AssetStore) dbAssetsToChainAssets(dbAssets []ConfirmedAsset,
726722
amount = 1
727723
}
728724

729-
scriptKeyPub, err := btcec.ParsePubKey(sprout.TweakedScriptKey)
730-
if err != nil {
731-
return nil, err
732-
}
733-
declaredKnown := extractBool(sprout.ScriptKeyDeclaredKnown)
734-
scriptKey := asset.ScriptKey{
735-
PubKey: scriptKeyPub,
736-
TweakedScriptKey: &asset.TweakedScriptKey{
737-
RawKey: rawScriptKeyDesc,
738-
Tweak: sprout.ScriptKeyTweak,
739-
DeclaredKnown: declaredKnown,
740-
},
741-
}
742-
743725
assetSprout, err := asset.New(
744726
assetGenesis, amount, lockTime, relativeLocktime,
745727
scriptKey, groupKey,
@@ -753,7 +735,9 @@ func (a *AssetStore) dbAssetsToChainAssets(dbAssets []ConfirmedAsset,
753735
// We cannot use 0 as the amount when creating a new asset with
754736
// the New function above. But if this is a tombstone asset, we
755737
// actually have to set the amount to 0.
756-
if scriptKeyPub.IsEqual(asset.NUMSPubKey) && sprout.Amount == 0 {
738+
if scriptKey.PubKey.IsEqual(asset.NUMSPubKey) &&
739+
sprout.Amount == 0 {
740+
757741
assetSprout.Amount = 0
758742
}
759743

@@ -2787,29 +2771,14 @@ func fetchAssetTransferOutputs(ctx context.Context, q ActiveAssetsStore,
27872771
"key: %w", err)
27882772
}
27892773

2790-
scriptKey, err := btcec.ParsePubKey(dbOut.ScriptKeyBytes)
2774+
scriptKey, err := parseScriptKey(
2775+
dbOut.InternalKey, dbOut.ScriptKey,
2776+
)
27912777
if err != nil {
27922778
return nil, fmt.Errorf("unable to decode script key: "+
27932779
"%w", err)
27942780
}
27952781

2796-
rawScriptKey, err := btcec.ParsePubKey(
2797-
dbOut.ScriptKeyRawKeyBytes,
2798-
)
2799-
if err != nil {
2800-
return nil, fmt.Errorf("unable to decode raw script "+
2801-
"key: %w", err)
2802-
}
2803-
2804-
scriptKeyLocator := keychain.KeyLocator{
2805-
Family: keychain.KeyFamily(
2806-
dbOut.ScriptKeyFamily,
2807-
),
2808-
Index: uint32(
2809-
dbOut.ScriptKeyIndex,
2810-
),
2811-
}
2812-
28132782
var splitRootHash mssmt.NodeHash
28142783
copy(splitRootHash[:], dbOut.SplitCommitmentRootHash)
28152784

@@ -2824,7 +2793,6 @@ func fetchAssetTransferOutputs(ctx context.Context, q ActiveAssetsStore,
28242793
err)
28252794
}
28262795

2827-
declaredKnown := extractBool(dbOut.ScriptKeyDeclaredKnown)
28282796
outputAnchor := tapfreighter.Anchor{
28292797
Value: btcutil.Amount(
28302798
dbOut.AnchorValue,
@@ -2876,19 +2844,9 @@ func fetchAssetTransferOutputs(ctx context.Context, q ActiveAssetsStore,
28762844
LockTime: uint64(dbOut.LockTime.Int32),
28772845
RelativeLockTime: uint64(dbOut.RelativeLockTime.Int32),
28782846
AssetVersion: asset.Version(dbOut.AssetVersion),
2879-
ScriptKey: asset.ScriptKey{
2880-
PubKey: scriptKey,
2881-
TweakedScriptKey: &asset.TweakedScriptKey{
2882-
RawKey: keychain.KeyDescriptor{
2883-
PubKey: rawScriptKey,
2884-
KeyLocator: scriptKeyLocator,
2885-
},
2886-
Tweak: dbOut.ScriptKeyTweak,
2887-
DeclaredKnown: declaredKnown,
2888-
},
2889-
},
2890-
ScriptKeyLocal: dbOut.ScriptKeyLocal,
2891-
WitnessData: witnessData,
2847+
ScriptKey: scriptKey,
2848+
ScriptKeyLocal: dbOut.ScriptKeyLocal,
2849+
WitnessData: witnessData,
28922850
SplitCommitmentRoot: mssmt.NewComputedNode(
28932851
splitRootHash,
28942852
uint64(dbOut.SplitCommitmentRootValue.Int64),
@@ -3167,21 +3125,22 @@ func (a *AssetStore) LogAnchorTxConfirm(ctx context.Context,
31673125
"witness: %w", err)
31683126
}
31693127

3170-
scriptPubKey, err := btcec.ParsePubKey(
3171-
out.ScriptKeyBytes,
3128+
fullScriptKey, err := parseScriptKey(
3129+
out.InternalKey, out.ScriptKey,
31723130
)
31733131
if err != nil {
31743132
return fmt.Errorf("unable to decode script "+
31753133
"key: %w", err)
31763134
}
3135+
scriptPubKey := fullScriptKey.PubKey
31773136

31783137
isNumsKey := scriptPubKey.IsEqual(asset.NUMSPubKey)
31793138
isTombstone := isNumsKey &&
31803139
out.Amount == 0 &&
31813140
out.OutputType == int16(tappsbt.TypeSplitRoot)
31823141
isBurn := !isNumsKey && len(witnessData) > 0 &&
31833142
asset.IsBurnKey(scriptPubKey, witnessData[0])
3184-
isKnown := extractBool(out.ScriptKeyDeclaredKnown)
3143+
isKnown := fullScriptKey.DeclaredKnown
31853144
skipAssetCreation := !isTombstone && !isBurn &&
31863145
!out.ScriptKeyLocal && !isKnown
31873146

@@ -3235,7 +3194,7 @@ func (a *AssetStore) LogAnchorTxConfirm(ctx context.Context,
32353194
}
32363195

32373196
params := ApplyPendingOutput{
3238-
ScriptKeyID: out.ScriptKeyID,
3197+
ScriptKeyID: out.ScriptKey.ScriptKeyID,
32393198
AnchorUtxoID: sqlInt64(
32403199
out.AnchorUtxoID,
32413200
),
@@ -3277,7 +3236,7 @@ func (a *AssetStore) LogAnchorTxConfirm(ctx context.Context,
32773236
if !ok {
32783237
return fmt.Errorf("no proof found for output "+
32793238
"with script key %x",
3280-
out.ScriptKeyBytes)
3239+
scriptPubKey.SerializeCompressed())
32813240
}
32823241
localProofKeys = append(localProofKeys, outKey)
32833242

0 commit comments

Comments
 (0)