@@ -695,6 +695,14 @@ func assertKeyKnowledge(t *testing.T, ctx context.Context,
695695 require .Equal (t , known , dbScriptKey .DeclaredKnown )
696696}
697697
698+ func assertTweak (t * testing.T , ctx context.Context , addrBook * TapAddressBook ,
699+ scriptKey asset.ScriptKey , tweak []byte ) {
700+
701+ dbScriptKey , err := addrBook .FetchScriptKey (ctx , scriptKey .PubKey )
702+ require .NoError (t , err )
703+ require .Equal (t , tweak , dbScriptKey .Tweak )
704+ }
705+
698706// TestScriptKeyKnownUpsert tests that we can insert a script key, then insert
699707// it again declared as known.
700708func TestScriptKeyKnownUpsert (t * testing.T ) {
@@ -757,3 +765,45 @@ func TestScriptKeyKnownUpsert(t *testing.T) {
757765 assertKeyKnowledge (t , ctx , addrBook , scriptKey , known )
758766 })
759767}
768+
769+ // TestScriptKeyTweakUpsert tests that we can insert a script key, then insert
770+ // it again when we know the tweak for it.
771+ func TestScriptKeyTweakUpsert (t * testing.T ) {
772+ t .Parallel ()
773+
774+ // First, make a new addr book instance we'll use in the test below.
775+ testClock := clock .NewTestClock (time .Now ())
776+ addrBook , _ := newAddrBook (t , testClock )
777+
778+ ctx := context .Background ()
779+
780+ // In this test, we insert the tweak as NULL, and make sure we overwrite
781+ // it with an actual value again later.
782+ t .Run ("null_to_value" , func (t * testing.T ) {
783+ known := false
784+ scriptKey := randScriptKey (t )
785+ scriptKey .Tweak = nil
786+
787+ // We'll insert a random script key into the database. We won't
788+ // declare it as known though, and it doesn't have the tweak.
789+ err := addrBook .InsertScriptKey (ctx , scriptKey , known )
790+ require .NoError (t , err )
791+
792+ // We'll fetch the script key and confirm that it's not known.
793+ assertKeyKnowledge (t , ctx , addrBook , scriptKey , known )
794+ assertTweak (t , ctx , addrBook , scriptKey , nil )
795+
796+ known = true
797+ randTweak := test .RandBytes (32 )
798+ scriptKey .Tweak = randTweak
799+
800+ // We'll now insert it again, but this time declare it as known
801+ // and also know the tweak.
802+ err = addrBook .InsertScriptKey (ctx , scriptKey , known )
803+ require .NoError (t , err )
804+
805+ // We'll fetch the script key and confirm that it's known.
806+ assertKeyKnowledge (t , ctx , addrBook , scriptKey , known )
807+ assertTweak (t , ctx , addrBook , scriptKey , randTweak )
808+ })
809+ }
0 commit comments