Skip to content

Commit 007f319

Browse files
committed
multi: remove now unused DeclaredKnown field
Since we can now declare more than just knowledge of a script key by giving it a specific type, we no longer need the boolean flag that was added as a temporary workaround.
1 parent 1de2fc1 commit 007f319

21 files changed

+69
-244
lines changed

address/book.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,9 @@ type Storage interface {
134134

135135
// InsertScriptKey inserts an address related script key into the
136136
// database, so it can be recognized as belonging to the wallet when a
137-
// transfer comes in later on. The script key can be declared as known
138-
// if it contains an internal key that isn't derived by the backing
139-
// wallet (e.g. NUMS key) but it should still be recognized as a key
140-
// being relevant for the local wallet (e.g. show assets received on
141-
// this key in the asset list and balances).
137+
// transfer comes in later on.
142138
InsertScriptKey(ctx context.Context, scriptKey asset.ScriptKey,
143-
declareAsKnown bool, keyType asset.ScriptKeyType) error
139+
keyType asset.ScriptKeyType) error
144140
}
145141

146142
// KeyRing is used to create script and internal keys for Taproot Asset
@@ -406,7 +402,7 @@ func (b *Book) NewAddressWithKeys(ctx context.Context, addrVersion Version,
406402
// through an RPC call. So we make a guess here.
407403
keyType := scriptKey.GuessType()
408404

409-
err = b.cfg.Store.InsertScriptKey(ctx, scriptKey, true, keyType)
405+
err = b.cfg.Store.InsertScriptKey(ctx, scriptKey, keyType)
410406
if err != nil {
411407
return nil, fmt.Errorf("unable to insert script key: %w", err)
412408
}
@@ -443,11 +439,9 @@ func (b *Book) IsLocalKey(ctx context.Context,
443439

444440
// InsertScriptKey inserts an address related script key into the database.
445441
func (b *Book) InsertScriptKey(ctx context.Context, scriptKey asset.ScriptKey,
446-
declareAsKnown bool, keyType asset.ScriptKeyType) error {
442+
keyType asset.ScriptKeyType) error {
447443

448-
return b.cfg.Store.InsertScriptKey(
449-
ctx, scriptKey, declareAsKnown, keyType,
450-
)
444+
return b.cfg.Store.InsertScriptKey(ctx, scriptKey, keyType)
451445
}
452446

453447
// NextInternalKey derives then inserts an internal key into the database to
@@ -482,9 +476,7 @@ func (b *Book) NextScriptKey(ctx context.Context,
482476
}
483477

484478
scriptKey := asset.NewScriptKeyBip86(keyDesc)
485-
err = b.cfg.Store.InsertScriptKey(
486-
ctx, scriptKey, true, asset.ScriptKeyBip86,
487-
)
479+
err = b.cfg.Store.InsertScriptKey(ctx, scriptKey, asset.ScriptKeyBip86)
488480
if err != nil {
489481
return asset.ScriptKey{}, err
490482
}

asset/asset.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,15 +1049,6 @@ type TweakedScriptKey struct {
10491049
// public key. If this is nil, then a BIP-0086 tweak is assumed.
10501050
Tweak []byte
10511051

1052-
// DeclaredKnown indicates that this script key has been explicitly
1053-
// declared as being important to the local wallet, even if it might not
1054-
// be fully known to the local wallet. This could perhaps also be named
1055-
// "imported", though that might imply that the corresponding private
1056-
// key was also somehow imported and available. The only relevance this
1057-
// flag has is that assets with a declared key are shown in the asset
1058-
// list/balance.
1059-
DeclaredKnown bool
1060-
10611052
// Type is the type of script key that is being used.
10621053
Type ScriptKeyType
10631054
}
@@ -1130,12 +1121,9 @@ func (s *ScriptKey) IsEqual(otherScriptKey *ScriptKey) bool {
11301121
// the local wallet or was explicitly declared to be known by using the
11311122
// DeclareScriptKey RPC. Knowing the key conceptually means the key belongs to
11321123
// the local wallet or is at least known by a software that operates on the
1133-
// local wallet. The DeclaredAsKnown flag is never serialized in proofs, so this
1134-
// is never explicitly set for keys foreign to the local wallet. Therefore, if
1135-
// this method returns true for a script key, it means the asset with the script
1136-
// key will be shown in the wallet balance.
1124+
// local wallet.
11371125
func (s *ScriptKey) DeclaredAsKnown() bool {
1138-
return s.TweakedScriptKey != nil && s.TweakedScriptKey.DeclaredKnown
1126+
return s.TweakedScriptKey != nil && s.Type != ScriptKeyUnknown
11391127
}
11401128

11411129
// HasScriptPath returns true if we know the internals of the script key and
@@ -1606,8 +1594,7 @@ func (a *Asset) Copy() *Asset {
16061594

16071595
if a.ScriptKey.TweakedScriptKey != nil {
16081596
assetCopy.ScriptKey.TweakedScriptKey = &TweakedScriptKey{
1609-
DeclaredKnown: a.ScriptKey.DeclaredKnown,
1610-
Type: a.ScriptKey.Type,
1597+
Type: a.ScriptKey.Type,
16111598
}
16121599
assetCopy.ScriptKey.RawKey = a.ScriptKey.RawKey
16131600

asset/generators.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ var (
7171
})
7272
TweakedScriptKeyGen = rapid.Custom(func(t *rapid.T) TweakedScriptKey {
7373
return TweakedScriptKey{
74-
RawKey: KeyDescGen.Draw(t, "raw_key"),
75-
Tweak: HashBytesGen.Draw(t, "tweak"),
76-
DeclaredKnown: rapid.Bool().Draw(t, "declared_known"),
74+
RawKey: KeyDescGen.Draw(t, "raw_key"),
75+
Tweak: HashBytesGen.Draw(t, "tweak"),
7776
Type: ScriptKeyType(
7877
rapid.Int16().Draw(t, "script_key_type"),
7978
),

rpcserver.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8147,9 +8147,7 @@ func (r *rpcServer) DeclareScriptKey(ctx context.Context,
81478147
return nil, fmt.Errorf("script key type must be set")
81488148
}
81498149

8150-
err = r.cfg.TapAddrBook.InsertScriptKey(
8151-
ctx, *scriptKey, true, scriptKey.Type,
8152-
)
8150+
err = r.cfg.TapAddrBook.InsertScriptKey(ctx, *scriptKey, scriptKey.Type)
81538151
if err != nil {
81548152
return nil, fmt.Errorf("error inserting script key: %w", err)
81558153
}

tapchannel/aux_sweeper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ func (a *AuxSweeper) importCommitScriptKeys(req lnwallet.ResolutionReq) error {
12651265
ctxb := context.Background()
12661266
for _, key := range keysToImport {
12671267
err := a.cfg.AddrBook.InsertScriptKey(
1268-
ctxb, key, true, asset.ScriptKeyScriptPathChannel,
1268+
ctxb, key, asset.ScriptKeyScriptPathChannel,
12691269
)
12701270
if err != nil {
12711271
return fmt.Errorf("unable to insert script "+
@@ -1299,7 +1299,7 @@ func (a *AuxSweeper) importOutputScriptKeys(desc tapscriptSweepDescs) error {
12991299
limitSpewer.Sdump(scriptKey))
13001300

13011301
return a.cfg.AddrBook.InsertScriptKey(
1302-
ctxb, scriptKey, true, asset.ScriptKeyScriptPathChannel,
1302+
ctxb, scriptKey, asset.ScriptKeyScriptPathChannel,
13031303
)
13041304
}
13051305

@@ -1494,7 +1494,7 @@ func (a *AuxSweeper) importCommitTx(req lnwallet.ResolutionReq,
14941494
// balance correctly.
14951495
ctxb := context.Background()
14961496
err := a.cfg.AddrBook.InsertScriptKey(
1497-
ctxb, fundingScriptKey, true, asset.ScriptKeyScriptPathChannel,
1497+
ctxb, fundingScriptKey, asset.ScriptKeyScriptPathChannel,
14981498
)
14991499
if err != nil {
15001500
return fmt.Errorf("unable to insert script key: %w", err)

tapchannel/commitment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ func deriveFundingScriptKey(ctx context.Context, addrBook address.Storage,
14951495
// the asset will be materialized in the asset table and show up in the
14961496
// balance correctly.
14971497
err := addrBook.InsertScriptKey(
1498-
ctx, fundingScriptKey, true, asset.ScriptKeyScriptPathChannel,
1498+
ctx, fundingScriptKey, asset.ScriptKeyScriptPathChannel,
14991499
)
15001500
if err != nil {
15011501
return asset.ScriptKey{}, fmt.Errorf("unable to insert script "+

tapdb/addrs.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,7 @@ func (t *TapAddressBook) InsertInternalKey(ctx context.Context,
652652
// it can be recognized as belonging to the wallet when a transfer comes in
653653
// later on.
654654
func (t *TapAddressBook) InsertScriptKey(ctx context.Context,
655-
scriptKey asset.ScriptKey, declaredKnown bool,
656-
keyType asset.ScriptKeyType) error {
655+
scriptKey asset.ScriptKey, keyType asset.ScriptKeyType) error {
657656

658657
var writeTxOpts AddrBookTxOptions
659658
return t.db.ExecTx(ctx, &writeTxOpts, func(q AddrBook) error {
@@ -669,7 +668,6 @@ func (t *TapAddressBook) InsertScriptKey(ctx context.Context,
669668
InternalKeyID: internalKeyID,
670669
TweakedScriptKey: scriptKey.PubKey.SerializeCompressed(),
671670
Tweak: scriptKey.Tweak,
672-
DeclaredKnown: sqlBool(declaredKnown),
673671
KeyType: sqlInt16(keyType),
674672
})
675673
return err

tapdb/addrs_test.go

Lines changed: 9 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package tapdb
33
import (
44
"context"
55
"database/sql"
6-
"fmt"
76
"math/rand"
87
"testing"
98
"time"
@@ -660,40 +659,12 @@ func randScriptKey(t *testing.T) asset.ScriptKey {
660659
return scriptKey
661660
}
662661

663-
// insertScriptKeyWithNull is a helper function that inserts a script key with a
664-
// a NULL value for declared known. We use this so we can insert a NULL vs an
665-
// actual value. It is identical to the InsertScriptKey.
666-
func insertScriptKeyWithNull(ctx context.Context,
667-
key asset.ScriptKey) func(AddrBook) error {
668-
669-
return func(q AddrBook) error {
670-
internalKeyID, err := insertInternalKey(
671-
ctx, q, key.RawKey,
672-
)
673-
if err != nil {
674-
return fmt.Errorf("error inserting internal key: %w",
675-
err)
676-
}
677-
678-
_, err = q.UpsertScriptKey(ctx, NewScriptKey{
679-
InternalKeyID: internalKeyID,
680-
TweakedScriptKey: key.PubKey.SerializeCompressed(),
681-
Tweak: key.Tweak,
682-
DeclaredKnown: sql.NullBool{
683-
Valid: false,
684-
},
685-
})
686-
return err
687-
}
688-
}
689-
690662
func assertKeyKnowledge(t *testing.T, ctx context.Context,
691-
addrBook *TapAddressBook, scriptKey asset.ScriptKey, known bool,
663+
addrBook *TapAddressBook, scriptKey asset.ScriptKey,
692664
keyType asset.ScriptKeyType) {
693665

694666
dbScriptKey, err := addrBook.FetchScriptKey(ctx, scriptKey.PubKey)
695667
require.NoError(t, err)
696-
require.Equal(t, known, dbScriptKey.DeclaredKnown)
697668
require.Equal(t, keyType, dbScriptKey.Type)
698669
}
699670

@@ -705,87 +676,6 @@ func assertTweak(t *testing.T, ctx context.Context, addrBook *TapAddressBook,
705676
require.Equal(t, tweak, dbScriptKey.Tweak)
706677
}
707678

708-
// TestScriptKeyKnownUpsert tests that we can insert a script key, then insert
709-
// it again declared as known.
710-
func TestScriptKeyKnownUpsert(t *testing.T) {
711-
t.Parallel()
712-
713-
// First, make a new addr book instance we'll use in the test below.
714-
testClock := clock.NewTestClock(time.Now())
715-
addrBook, _ := newAddrBook(t, testClock)
716-
717-
ctx := context.Background()
718-
719-
// In this test, we insert the known field as false, and make sure we
720-
// can flip it back to true.
721-
t.Run("false_to_true", func(t *testing.T) {
722-
known := false
723-
scriptKey := randScriptKey(t)
724-
725-
// We'll insert a random script key into the database. We won't
726-
// declare it as known though.
727-
err := addrBook.InsertScriptKey(
728-
ctx, scriptKey, known, asset.ScriptKeyBip86,
729-
)
730-
require.NoError(t, err)
731-
732-
// We'll fetch the script key and confirm that it's not known.
733-
assertKeyKnowledge(
734-
t, ctx, addrBook, scriptKey, known,
735-
asset.ScriptKeyBip86,
736-
)
737-
738-
known = true
739-
740-
// We'll now insert it again, but this time declare it as known.
741-
err = addrBook.InsertScriptKey(
742-
ctx, scriptKey, known, asset.ScriptKeyBip86,
743-
)
744-
require.NoError(t, err)
745-
746-
// We'll fetch the script key and confirm that it's known.
747-
assertKeyKnowledge(
748-
t, ctx, addrBook, scriptKey, known,
749-
asset.ScriptKeyBip86,
750-
)
751-
})
752-
753-
// In this test, we insert a NULL value, and make sure that it can still
754-
// be set to true.
755-
t.Run("null_to_true", func(t *testing.T) {
756-
known := false
757-
scriptKey := randScriptKey(t)
758-
759-
// We'll lift the internal routine of InsertScriptKey so we can
760-
// insert an actual NULL here.
761-
err := addrBook.db.ExecTx(
762-
ctx, &AddrBookTxOptions{},
763-
insertScriptKeyWithNull(ctx, scriptKey),
764-
)
765-
require.NoError(t, err)
766-
767-
// We'll fetch the script key and confirm that it's not known.
768-
assertKeyKnowledge(
769-
t, ctx, addrBook, scriptKey, known,
770-
asset.ScriptKeyUnknown,
771-
)
772-
773-
known = true
774-
775-
// We'll now insert it again, but this time declare it as known.
776-
err = addrBook.InsertScriptKey(
777-
ctx, scriptKey, known, asset.ScriptKeyBip86,
778-
)
779-
require.NoError(t, err)
780-
781-
// We'll fetch the script key and confirm that it's known.
782-
assertKeyKnowledge(
783-
t, ctx, addrBook, scriptKey, known,
784-
asset.ScriptKeyBip86,
785-
)
786-
})
787-
}
788-
789679
// TestScriptKeyTweakUpsert tests that we can insert a script key, then insert
790680
// it again when we know the tweak for it.
791681
func TestScriptKeyTweakUpsert(t *testing.T) {
@@ -800,40 +690,35 @@ func TestScriptKeyTweakUpsert(t *testing.T) {
800690
// In this test, we insert the tweak as NULL, and make sure we overwrite
801691
// it with an actual value again later.
802692
t.Run("null_to_value", func(t *testing.T) {
803-
known := false
804693
scriptKey := randScriptKey(t)
805694
scriptKey.Tweak = nil
806695

807696
// We'll insert a random script key into the database. We won't
808697
// declare it as known though, and it doesn't have the tweak.
809698
err := addrBook.InsertScriptKey(
810-
ctx, scriptKey, known,
811-
asset.ScriptKeyScriptPathExternal,
699+
ctx, scriptKey, asset.ScriptKeyUnknown,
812700
)
813701
require.NoError(t, err)
814702

815703
// We'll fetch the script key and confirm that it's not known.
816704
assertKeyKnowledge(
817-
t, ctx, addrBook, scriptKey, known,
818-
asset.ScriptKeyScriptPathExternal,
705+
t, ctx, addrBook, scriptKey, asset.ScriptKeyUnknown,
819706
)
820707
assertTweak(t, ctx, addrBook, scriptKey, nil)
821708

822-
known = true
823709
randTweak := test.RandBytes(32)
824710
scriptKey.Tweak = randTweak
825711

826712
// We'll now insert it again, but this time declare it as known
827713
// and also know the tweak.
828714
err = addrBook.InsertScriptKey(
829-
ctx, scriptKey, known,
830-
asset.ScriptKeyScriptPathExternal,
715+
ctx, scriptKey, asset.ScriptKeyScriptPathExternal,
831716
)
832717
require.NoError(t, err)
833718

834719
// We'll fetch the script key and confirm that it's known.
835720
assertKeyKnowledge(
836-
t, ctx, addrBook, scriptKey, known,
721+
t, ctx, addrBook, scriptKey,
837722
asset.ScriptKeyScriptPathExternal,
838723
)
839724
assertTweak(t, ctx, addrBook, scriptKey, randTweak)
@@ -854,35 +739,32 @@ func TestScriptKeyTypeUpsert(t *testing.T) {
854739
// In this test, we insert the type as unknown, and make sure we
855740
// overwrite it with an actual value again later.
856741
t.Run("null_to_value", func(t *testing.T) {
857-
known := true
858742
scriptKey := randScriptKey(t)
859743
scriptKey.Tweak = nil
860744

861745
// We'll insert a random script key into the database. It is
862746
// declared as known, but doesn't have a known type.
863747
err := addrBook.InsertScriptKey(
864-
ctx, scriptKey, known, asset.ScriptKeyUnknown,
748+
ctx, scriptKey, asset.ScriptKeyUnknown,
865749
)
866750
require.NoError(t, err)
867751

868752
// We'll fetch the script key and confirm that it's not known.
869753
assertKeyKnowledge(
870-
t, ctx, addrBook, scriptKey, known,
871-
asset.ScriptKeyUnknown,
754+
t, ctx, addrBook, scriptKey, asset.ScriptKeyUnknown,
872755
)
873756
assertTweak(t, ctx, addrBook, scriptKey, nil)
874757

875758
// We'll now insert it again, but this time declare it as known
876759
// and also know the tweak.
877760
err = addrBook.InsertScriptKey(
878-
ctx, scriptKey, known, asset.ScriptKeyBip86,
761+
ctx, scriptKey, asset.ScriptKeyBip86,
879762
)
880763
require.NoError(t, err)
881764

882765
// We'll fetch the script key and confirm that it's known.
883766
assertKeyKnowledge(
884-
t, ctx, addrBook, scriptKey, known,
885-
asset.ScriptKeyBip86,
767+
t, ctx, addrBook, scriptKey, asset.ScriptKeyBip86,
886768
)
887769
})
888770
}

0 commit comments

Comments
 (0)