Skip to content

Wallet schema v2 #2146

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

Merged
merged 15 commits into from
May 18, 2025
Merged

Wallet schema v2 #2146

merged 15 commits into from
May 18, 2025

Conversation

ekzyis
Copy link
Member

@ekzyis ekzyis commented May 2, 2025

Description

based on #2092 because it's the target branch

This PR does the following:

  • add static table WalletTemplate which stores which send and receive protocols a wallet in UserWallet supports
    => allows multiple send and receive protocols per wallet
  • split wallets by send and receive for easier validation of configuration instead of having both send and receive optional and rely on application logic to make sure the configuration is not invalid
  • add wallet protocol tables (WalletSend..., WalletRecv...) which store configuration per user wallet
  • use triggers to make sure that a protocol of a user wallet is actually supported by the wallet

How to add a new wallet:

  • add a new row to the WalletV2 table and declare which send and receive protocols it supports
  • if it supports a new protocol, update enums and add a new wallet protocol table to store configuration

My beautiful TODO list

  • use unique index for walletId in wallet protocol tables so a user wallet can't have the same protocol twice for the same wallet ✅
  • check if HTTP protocols to send and recv (LNbits, Blink, Phoenixd) can be abstracted away via one WalletSendHTTP table without resorting to JSON columns ❌ won't do in this PR, maybe in the future
  • check if trigger to check wallet support can be unified ✅
  • wallet migration ✅
    • wallet seed ✅
    • eliminate duplicate wallets before migration (Delete duplicate wallets #2163) ✅
    • migrate wallets ✅
    • wallet seed with multiple wallets (LNbits, NWC, ...) per user ✅
    • put send/receive protocols into the same user wallet if exists ✅
    • verify wallet migration with SQL assertions after migration ❌ decided not to, too much work
    • add trigger to update JSON in ProtocolWallet row ✅
      • split JSON column into two columns for send and receive? ❌ no longer needed since we switched to ProtocolWallet
      • resolve vault entries in JSON trigger ✅
    • drop old tables, triggers etc. ✅
    • keep ids stable during migration to make new foreign keys work ✅
  • figure out why Withdrawal foreign key fails ✅ fixed it, but didn't really figure out what happened
  • Q: Are there more edge cases with our old wallets that might break migration? ✅ No, I don't think so.
  • Q: add priority to ProtocolWallet, too? ✅ No, I don't think that's important and we can change it later.
  • verify wallet support 🚧
  • update code to save/load wallets 🚧
  • rename walletId foreign keys that point to UserWallet to userWalletId to avoid confusion? 🤔
  • add User(id, vaultKeyHash) foreign key to Vault table with new userId, keyHash columns? ❌ out of scope of this PR

Additional Context

This is based on the latest discussion with @huumn how to plan further ahead with the wallet schema than to just migrate the vault in #2092.

Checklist

Are your changes backwards compatible? Please answer below:

tbd

On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below:

tbd

For frontend changes: Tested on mobile, light and dark mode? Please answer below:

n/a

Did you introduce any new environment variables? If so, call them out explicitly here:

no

@ekzyis ekzyis added the wallets label May 2, 2025
@ekzyis ekzyis marked this pull request as draft May 2, 2025 06:11
@huumn huumn mentioned this pull request May 5, 2025
13 tasks
@ekzyis ekzyis force-pushed the wallet-refactor-migrate-vault branch from 7efb350 to a10af10 Compare May 5, 2025 23:05
@ekzyis ekzyis force-pushed the wallet-schema-v2 branch 3 times, most recently from eccc4d0 to 840bb20 Compare May 7, 2025 05:35
Copy link

gitguardian bot commented May 7, 2025

⚠️ GitGuardian has uncovered 1 secret following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secret in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
16991000 Triggered Generic High Entropy Secret c52b973 docker/db/wallet-seed.sql View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secret safely. Learn here the best practices.
  3. Revoke and rotate this secret.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

@ekzyis ekzyis force-pushed the wallet-schema-v2 branch 5 times, most recently from 36e027f to 84a7dcd Compare May 11, 2025 04:21
@ekzyis ekzyis force-pushed the wallet-refactor-migrate-vault branch from a10af10 to 020e297 Compare May 13, 2025 17:55
@ekzyis ekzyis force-pushed the wallet-schema-v2 branch from d58cca2 to af9311a Compare May 13, 2025 17:55
@ekzyis ekzyis force-pushed the wallet-schema-v2 branch 2 times, most recently from 9a6ce22 to c2a22f6 Compare May 15, 2025 04:57
@ekzyis ekzyis force-pushed the wallet-refactor-migrate-vault branch from 020e297 to e604617 Compare May 15, 2025 22:58
@ekzyis ekzyis force-pushed the wallet-schema-v2 branch 7 times, most recently from 6c07696 to e60046f Compare May 16, 2025 02:38
@ekzyis ekzyis force-pushed the wallet-refactor-migrate-vault branch 2 times, most recently from 310f17c to e604617 Compare May 16, 2025 22:12
@ekzyis ekzyis changed the base branch from wallet-refactor-migrate-vault to wallet-v2 May 18, 2025 02:46
ekzyis added 2 commits May 17, 2025 22:04
For some reason, if we run the vault_refactor migration before we run the delete_duplicate_wallets migration, the wallet_v2 migration fails with this error:

```
app  | 2025-05-16T20:56:53.127625000Z Applying migration `20250516042253_wallet_v2`
app  | 2025-05-16T20:56:53.202274000Z Error: P3018
app  | 2025-05-16T20:56:53.202294000Z
app  | 2025-05-16T20:56:53.202309000Z A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
app  | 2025-05-16T20:56:53.202322000Z
app  | 2025-05-16T20:56:53.202337000Z Migration name: 20250516042253_wallet_v2
app  | 2025-05-16T20:56:53.202349000Z
app  | 2025-05-16T20:56:53.202365000Z Database error code: 23503
app  | 2025-05-16T20:56:53.202379000Z
app  | 2025-05-16T20:56:53.202392000Z Database error:
app  | 2025-05-16T20:56:53.202406000Z ERROR: insert or update on table "Withdrawl" violates foreign key constraint "Withdrawl_walletId_fkey"
app  | 2025-05-16T20:56:53.202427000Z DETAIL: Key (walletId)=(29) is not present in table "ProtocolWallet".
```

I don't really understand how the wallet_v2 migration error is related to the order of the vault_refactor and delete_duplicate_wallets migrations.
@ekzyis ekzyis force-pushed the wallet-schema-v2 branch from 3d6d3c4 to 9b25ff2 Compare May 18, 2025 03:04
@ekzyis ekzyis marked this pull request as ready for review May 18, 2025 03:59
@ekzyis
Copy link
Member Author

ekzyis commented May 18, 2025

See #2092 (comment)

@ekzyis ekzyis force-pushed the wallet-schema-v2 branch from 0e387a4 to 1637f1d Compare May 18, 2025 04:00
@ekzyis ekzyis merged commit 3a5324c into wallet-v2 May 18, 2025
6 checks passed
@ekzyis ekzyis deleted the wallet-schema-v2 branch May 18, 2025 05:00
@ekzyis ekzyis mentioned this pull request May 18, 2025
ekzyis added a commit that referenced this pull request May 20, 2025
ekzyis added a commit that referenced this pull request May 21, 2025
ekzyis added a commit that referenced this pull request May 23, 2025
ekzyis added a commit that referenced this pull request May 25, 2025
ekzyis added a commit that referenced this pull request May 31, 2025
ekzyis added a commit that referenced this pull request Jun 3, 2025
ekzyis added a commit that referenced this pull request Jun 4, 2025
ekzyis added a commit that referenced this pull request Jun 6, 2025
ekzyis added a commit that referenced this pull request Jun 6, 2025
ekzyis added a commit that referenced this pull request Jun 11, 2025
ekzyis added a commit that referenced this pull request Jun 13, 2025
ekzyis added a commit that referenced this pull request Jun 15, 2025
ekzyis added a commit that referenced this pull request Jun 15, 2025
ekzyis added a commit that referenced this pull request Jun 18, 2025
ekzyis added a commit that referenced this pull request Jun 18, 2025
ekzyis added a commit that referenced this pull request Jun 19, 2025
ekzyis added a commit that referenced this pull request Jun 20, 2025
ekzyis added a commit that referenced this pull request Jun 20, 2025
ekzyis added a commit that referenced this pull request Jun 26, 2025
ekzyis added a commit that referenced this pull request Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant