Skip to content

Commit 8c230eb

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 f8f67b1 commit 8c230eb

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
@@ -1037,15 +1037,6 @@ type TweakedScriptKey struct {
10371037
// public key. If this is nil, then a BIP-0086 tweak is assumed.
10381038
Tweak []byte
10391039

1040-
// DeclaredKnown indicates that this script key has been explicitly
1041-
// declared as being important to the local wallet, even if it might not
1042-
// be fully known to the local wallet. This could perhaps also be named
1043-
// "imported", though that might imply that the corresponding private
1044-
// key was also somehow imported and available. The only relevance this
1045-
// flag has is that assets with a declared key are shown in the asset
1046-
// list/balance.
1047-
DeclaredKnown bool
1048-
10491040
// Type is the type of script key that is being used.
10501041
Type ScriptKeyType
10511042
}
@@ -1118,12 +1109,9 @@ func (s *ScriptKey) IsEqual(otherScriptKey *ScriptKey) bool {
11181109
// the local wallet or was explicitly declared to be known by using the
11191110
// DeclareScriptKey RPC. Knowing the key conceptually means the key belongs to
11201111
// the local wallet or is at least known by a software that operates on the
1121-
// local wallet. The DeclaredAsKnown flag is never serialized in proofs, so this
1122-
// is never explicitly set for keys foreign to the local wallet. Therefore, if
1123-
// this method returns true for a script key, it means the asset with the script
1124-
// key will be shown in the wallet balance.
1112+
// local wallet.
11251113
func (s *ScriptKey) DeclaredAsKnown() bool {
1126-
return s.TweakedScriptKey != nil && s.TweakedScriptKey.DeclaredKnown
1114+
return s.TweakedScriptKey != nil && s.Type != ScriptKeyUnknown
11271115
}
11281116

11291117
// HasScriptPath returns true if we know the internals of the script key and
@@ -1587,8 +1575,7 @@ func (a *Asset) Copy() *Asset {
15871575

15881576
if a.ScriptKey.TweakedScriptKey != nil {
15891577
assetCopy.ScriptKey.TweakedScriptKey = &TweakedScriptKey{
1590-
DeclaredKnown: a.ScriptKey.DeclaredKnown,
1591-
Type: a.ScriptKey.Type,
1578+
Type: a.ScriptKey.Type,
15921579
}
15931580
assetCopy.ScriptKey.RawKey = a.ScriptKey.RawKey
15941581

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
@@ -7926,9 +7926,7 @@ func (r *rpcServer) DeclareScriptKey(ctx context.Context,
79267926
return nil, fmt.Errorf("script key type must be set")
79277927
}
79287928

7929-
err = r.cfg.TapAddrBook.InsertScriptKey(
7930-
ctx, *scriptKey, true, scriptKey.Type,
7931-
)
7929+
err = r.cfg.TapAddrBook.InsertScriptKey(ctx, *scriptKey, scriptKey.Type)
79327930
if err != nil {
79337931
return nil, fmt.Errorf("error inserting script key: %w", err)
79347932
}

tapchannel/aux_funding_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ func (f *FundingController) fundVirtualPacket(ctx context.Context,
782782
// the asset will be materialized in the asset table and show up in the
783783
// balance correctly.
784784
err := f.cfg.AddrBook.InsertScriptKey(
785-
ctx, fundingScriptKey, true, asset.ScriptKeyScriptPathChannel,
785+
ctx, fundingScriptKey, asset.ScriptKeyScriptPathChannel,
786786
)
787787
if err != nil {
788788
return nil, fmt.Errorf("unable to insert script key: %w", err)

tapchannel/aux_sweeper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ func (a *AuxSweeper) importCommitScriptKeys(req lnwallet.ResolutionReq) error {
12811281
ctxb := context.Background()
12821282
for _, key := range keysToImport {
12831283
err := a.cfg.AddrBook.InsertScriptKey(
1284-
ctxb, key, true, asset.ScriptKeyScriptPathChannel,
1284+
ctxb, key, asset.ScriptKeyScriptPathChannel,
12851285
)
12861286
if err != nil {
12871287
return fmt.Errorf("unable to insert script "+
@@ -1315,7 +1315,7 @@ func (a *AuxSweeper) importOutputScriptKeys(desc tapscriptSweepDescs) error {
13151315
limitSpewer.Sdump(scriptKey))
13161316

13171317
return a.cfg.AddrBook.InsertScriptKey(
1318-
ctxb, scriptKey, true, asset.ScriptKeyScriptPathChannel,
1318+
ctxb, scriptKey, asset.ScriptKeyScriptPathChannel,
13191319
)
13201320
}
13211321

@@ -1510,7 +1510,7 @@ func (a *AuxSweeper) importCommitTx(req lnwallet.ResolutionReq,
15101510
// balance correctly.
15111511
ctxb := context.Background()
15121512
err := a.cfg.AddrBook.InsertScriptKey(
1513-
ctxb, fundingScriptKey, true, asset.ScriptKeyScriptPathChannel,
1513+
ctxb, fundingScriptKey, asset.ScriptKeyScriptPathChannel,
15141514
)
15151515
if err != nil {
15161516
return fmt.Errorf("unable to insert script key: %w", err)

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)