Skip to content

Commit 4bb4d5a

Browse files
authored
Merge pull request #1620 from lightninglabs/detect-vpsbt-script-type
tapfreighter+itest: store change script key to DB
2 parents d6d2c9b + f3b8951 commit 4bb4d5a

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

itest/psbt_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,6 +2878,20 @@ func testPsbtExternalCommit(t *harnessTest) {
28782878
AssertBalanceByID(
28792879
t.t, bobTapd, targetAssetGenesis.AssetId, assetsToSend,
28802880
)
2881+
2882+
// We make sure the change and the passive are shown properly in the
2883+
// sender's wallet.
2884+
AssertBalances(
2885+
t.t, aliceTapd, targetAsset.Amount-assetsToSend,
2886+
WithAssetID(targetAssetGenesis.AssetId), WithNumUtxos(1),
2887+
WithScriptKeyType(asset.ScriptKeyBip86),
2888+
)
2889+
passiveAsset := assets[1]
2890+
AssertBalances(
2891+
t.t, aliceTapd, passiveAsset.Amount,
2892+
WithAssetID(passiveAsset.AssetGenesis.AssetId), WithNumUtxos(1),
2893+
WithScriptKeyType(asset.ScriptKeyBip86),
2894+
)
28812895
}
28822896

28832897
// testPsbtLockTimeSend tests that we can send minted assets into a ScriptKey

tapfreighter/fund.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func createFundedPacketWithInputs(ctx context.Context, exporter proof.Exporter,
121121
}
122122
}
123123

124-
err = deriveChangeOutputKey(ctx, vPkt, keyRing)
124+
err = deriveChangeOutputKey(ctx, vPkt, keyRing, addrBook)
125125
if err != nil {
126126
return nil, fmt.Errorf("unable to derive change "+
127127
"output key: %w", err)
@@ -201,7 +201,7 @@ func annotateLocalScriptKeys(ctx context.Context, vPkt *tappsbt.VPacket,
201201
// back to the local node, assuming there is a change output and it isn't a
202202
// zero-value tombstone.
203203
func deriveChangeOutputKey(ctx context.Context, vPkt *tappsbt.VPacket,
204-
keyRing KeyRing) error {
204+
keyRing KeyRing, addrBook AddrBook) error {
205205

206206
// If we don't have a split output then there's no change.
207207
if !vPkt.HasSplitRootOutput() {
@@ -238,6 +238,16 @@ func deriveChangeOutputKey(ctx context.Context, vPkt *tappsbt.VPacket,
238238
changeOut.ScriptKey = asset.NewScriptKeyBip86(
239239
changeScriptKey,
240240
)
241+
242+
// To make sure we recognize the script key again if it is
243+
// used over the vPSBT RPCs (where we don't encode the script
244+
// key type), we now also store the key in the DB.
245+
err = addrBook.InsertScriptKey(
246+
ctx, changeOut.ScriptKey, changeOut.ScriptKey.Type,
247+
)
248+
if err != nil {
249+
return fmt.Errorf("cannot insert script key: %w", err)
250+
}
241251
}
242252

243253
return nil

tapfreighter/fund_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ type mockAddrBook struct {
6666
scriptKeys []asset.TweakedScriptKey
6767
}
6868

69+
func (m *mockAddrBook) InsertScriptKey(_ context.Context,
70+
scriptKey asset.ScriptKey, _ asset.ScriptKeyType) error {
71+
72+
if scriptKey.TweakedScriptKey == nil {
73+
return fmt.Errorf("script key must be tweaked")
74+
}
75+
76+
m.scriptKeys = append(m.scriptKeys, *scriptKey.TweakedScriptKey)
77+
78+
return nil
79+
}
80+
6981
func (m *mockAddrBook) FetchScriptKey(_ context.Context,
7082
tweakedScriptKey *btcec.PublicKey) (*asset.TweakedScriptKey, error) {
7183

tapfreighter/wallet.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ type AddrBook interface {
152152
// ErrInternalKeyNotFound is returned.
153153
FetchInternalKeyLocator(ctx context.Context,
154154
rawKey *btcec.PublicKey) (keychain.KeyLocator, error)
155+
156+
// InsertScriptKey inserts an address related script key into the
157+
// database, so it can be recognized as belonging to the wallet when a
158+
// transfer comes in later on.
159+
InsertScriptKey(ctx context.Context,
160+
scriptKey asset.ScriptKey, keyType asset.ScriptKeyType) error
155161
}
156162

157163
// AnchorVTxnsParams holds all the parameters needed to create a BTC level

0 commit comments

Comments
 (0)