Skip to content

Commit 1e85b09

Browse files
committed
tapdb: allow tweak to be upserted
This fixes a bug where if we ever inserted a script key before knowing its tweak, we could never update the tweak later on when we learn it. This meant that such keys would be seen as BIP-086 keys, even though we later learn they aren't.
1 parent 447cba5 commit 1e85b09

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

tapdb/sqlc/assets.sql.go

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/queries/assets.sql

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,8 @@ INSERT INTO script_keys (
852852
) VALUES (
853853
$1, $2, $3, $4
854854
) ON CONFLICT (tweaked_script_key)
855-
-- As a NOP, we just set the script key to the one that triggered the
856-
-- conflict.
855+
-- Overwrite the declared_known and tweak fields if they were previously
856+
-- unknown.
857857
DO UPDATE SET
858858
tweaked_script_key = EXCLUDED.tweaked_script_key,
859859
-- If the script key was previously unknown, we'll update to the new
@@ -862,7 +862,13 @@ INSERT INTO script_keys (
862862
WHEN script_keys.declared_known IS NULL OR script_keys.declared_known = FALSE
863863
THEN COALESCE(EXCLUDED.declared_known, script_keys.declared_known)
864864
ELSE script_keys.declared_known
865-
END
865+
END,
866+
-- If the tweak was previously unknown, we'll update to the new value.
867+
tweak = CASE
868+
WHEN script_keys.tweak IS NULL
869+
THEN COALESCE(EXCLUDED.tweak, script_keys.tweak)
870+
ELSE script_keys.tweak
871+
END
866872
RETURNING script_key_id;
867873

868874
-- name: FetchScriptKeyIDByTweakedKey :one

0 commit comments

Comments
 (0)