Skip to content

Commit

Permalink
tapdb: allow tweak to be upserted
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
guggero committed Nov 27, 2024
1 parent d387d03 commit c96f369
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 9 additions & 3 deletions tapdb/sqlc/assets.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions tapdb/sqlc/queries/assets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,8 @@ INSERT INTO script_keys (
) VALUES (
$1, $2, $3, $4
) ON CONFLICT (tweaked_script_key)
-- As a NOP, we just set the script key to the one that triggered the
-- conflict.
-- Overwrite the declared_known and tweak fields if they were previously
-- unknown.
DO UPDATE SET
tweaked_script_key = EXCLUDED.tweaked_script_key,
-- If the script key was previously unknown, we'll update to the new
Expand All @@ -862,7 +862,13 @@ INSERT INTO script_keys (
WHEN script_keys.declared_known IS NULL OR script_keys.declared_known = FALSE
THEN COALESCE(EXCLUDED.declared_known, script_keys.declared_known)
ELSE script_keys.declared_known
END
END,
-- If the tweak was previously unknown, we'll update to the new value.
tweak = CASE
WHEN script_keys.tweak IS NULL
THEN COALESCE(EXCLUDED.tweak, script_keys.tweak)
ELSE script_keys.tweak
END
RETURNING script_key_id;

-- name: FetchScriptKeyIDByTweakedKey :one
Expand Down

0 comments on commit c96f369

Please sign in to comment.