Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi: store optional key tweak in assets table and tweak script keys BIP0086 style #125

Merged
merged 11 commits into from
Sep 19, 2022

Conversation

Roasbeef
Copy link
Member

Replaces #112

Rebased on top of master. The best way to review this is the entire diff since I've retained all the intermediate commits to ensure proper authorship.

The main diff between this and #112 is that everywhere here ScriptKey.PubKey (which is just btcec.PublicKey is just the right key. We only reach into the full key desc when we need to go sign, etc .

Fixes #94

@Roasbeef
Copy link
Member Author

Itests failing due to a mismatch w/ the on disk indexes, commented here: https://github.com/lightninglabs/taro/pull/112/files#r973646487

The issue is that we're using the tweaked key externally, but only storing the raw key on the DB. All the queries are then related to the raw key, so when we go to export by tweaked key that fails. I think we also need to add the tweaked key itself into the internal_keys table. This way we can check for "both" whenever we do this type of query.

Alternatively, we store the tweaked key itself along side the assets row.

@Roasbeef
Copy link
Member Author

Alternatively, we can return both the normal and tweaked key on the RPC interface, then the test can use this value to look up the proof file.

@bhandras
Copy link
Member

Quickly fixed the linter error, plus the last commit is now squashed. Originally I removed that DROP TABLE line because it was duplicated but I think it was fixed somewhere else and became obsolete after rebasing to a freshly fetched main later on.

bhandras and others added 11 commits September 18, 2022 20:50
Fixes bug where the proof file insert would fail since we're using the
raw script key in a join clause within `UpsertAssetProof`.
In this commit, we add a new struct to house the tweaked script key information. This new struct lets us leave the old scriptkey.pubkey as the main key we always use to encode and also verify signatures against.

The new struct is then embedded within the ScriptKey struct as a pointer, so anytime we're comparing to wire assets, it'll match up properly.
In this commit, we move the script key tweak to live inside the asset
rather than within the internal key. This matches the existing managed
utxo struct that contains the tweak (sibling and taro root) inside the
row, which is then used to construct the actual key that the managed
UTXO commits to.

We also do this for the address as well.

In the future, this'll allow us to use the tweak as a foreign key in
another row that contains the actual scripts we're committing to.
This test fails as is, since it relies on using the actual script key
instead of the raw script key to fetch the proof on disk.

The next commit will resolve this by storing both the tweaked key and
the regular key similar to the way we handle key families on disk.
In this commit, we modify things to use a distinct table for script_keys. Moving to this surfaced a bug for which a test case was added in the past commit. This move mirrors the family keys where we store the tweaked key and the raw key, along with all the items we need to re-derive the key.

This move also surfaced an issue in the existing itests which import a proof right at the very end: we only have the tweaked script key, so the import just mirrors the state and doesn't actually let us spend the assets. For the addr send case, we've already created the actual script key, so we can use the tweaked key in the new asset to find the actual ID we need.
@Roasbeef
Copy link
Member Author

Roasbeef commented Sep 19, 2022

So ended up just rolling things into a new script_keys table to mirror the way we handle the internal keys. I also added another test case in the import proof unit tests to catch an earlier error that only showed up in the itests. Found a few subtle things along the way that needed fixing, gotta love when a seemingly small issue starts to sprawl over the horizon...

Everything works e2e now, though there's a bit of a hack in place to allow the existing itest to work where we mirror the state of another node. Before we had the raw pubkey and just inserted that with no real key desc information. We can't do that now since we have a tweaked key (or it could be w/e) so that test is just good for mirroring the state of one node to another.

I think the best way to review this is the full diff vs the individual commits.

Copy link
Member

@bhandras bhandras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

tarodb/sqlite/migrations/000002_assets.up.sql Show resolved Hide resolved
tarodb/addrs.go Show resolved Hide resolved
tarodb/assets_store.go Show resolved Hide resolved
tarodb/sqlite/migrations/000002_assets.up.sql Show resolved Hide resolved
Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, both of you! This should make it pretty easy to enable scripts for assets as well 🎉

Found some small things as well, but will merge this PR to unblock all of us and fix the nits and smaller things in #117 (which I think is pretty close to be merged as well).

tarogarden/caretaker.go Show resolved Hide resolved
@@ -821,6 +831,8 @@ func (a *AssetStore) importAssetFromProof(ctx context.Context,

// Next, we'll insert the genesis_asset row which holds the base
// information for this asset.
//
// TODO(roasbeef): should be an upsert
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a commit in one of my PRs, will remove the TODO there.

tarodb/sqlite/migrations/000002_assets.up.sql Show resolved Hide resolved
@guggero guggero merged commit a775d85 into main Sep 19, 2022
@guggero guggero deleted the bip86-keys-fixes branch September 19, 2022 15:23
guggero added a commit that referenced this pull request Sep 19, 2022
This commit fixes most of the review comments in #125.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vm+addr: always use BIP 86 keys for script keys
4 participants