-
Notifications
You must be signed in to change notification settings - Fork 7
Extract stateless functions into wallet library module #202
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
Conversation
thomaseizinger
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall a good starting point. Try to push for more value-based APIs where possible instead of depending on traits.
Additionally, I think it should actually be the responsibility of baru to provide "backends" like esplora. So maybe the Wallet trait can be trimmed down to a Blockchain backend (similar to bdk) and then we define functionality composed on top of this trait?
Also, I think baru should offer an actual Wallet struct that the extension can re-use that holds state like the keys, and UTXOs. When given a Backend, the wallet can then sync the UTXOs and perform IO-less stuff like signing a loan transaction etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is coming together nicely.
Take the following with a grain of salt because maybe it is not fully-possible but I was thinking we could do something like:
- Have
Walletstore a list of unspent transaction outputs. Whatever data structure we store, it should be everything we need to know about our UTXOs. In the future, we can abstract this into some kind of backend rather then a just an in-memory list. - Have a
Wallet::syncfunction that looks roughly like this:
impl Wallet {
async fn sync(&mut self, backend: &impl Backend) {
let utxos = backend.fetch_utxos(self.address());
// for utxo in utxos, fetch transaction and compute txout ...
// self.txouts =
}
}- All APIs like
extract_loancan now be synchronous functions onWalletbecauseWalletowns most of the data necessary to do the task (txouts, blinding key etc)
Some notes about this design:
- It should allow the
Backendtrait to be minimal and only concern itself with IO. barucan provide an implementation ofBackendfor esplora but given that specific need of caching using the browser's cache etc, I would probably not bother and leave that within the extension for now.- Passing the
Backendas a parameter tosyncallowsWalletto remain simple and without type parameters and allows for easier testing using dummy backends. - Having
Walletown state likeTxOutsmakes all the functions likeextract_loanquite easy to use.
On top of all this, the extension should probably define its own Wallet struct that wraps the one provided by baru and carry around the state for usdt_asset_id, chain, etc.
I recognize this may seem contradicting to my previous review. However, for this design to be possible, it was important to first split things into pure functions (value-based). Now that this is the case, we can easily re-compose everything that is there by bundling the necessary state for these functions up in the Wallet struct.
good idea. another benefit is the caller won't have to pass around the touts in their app |
4479142 to
9e4c835
Compare
thomaseizinger
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, I think we are almost there!
To summarize:
- Add a coin-selection API to
Wallet - Add a signing API to
Wallet
That should allow you to remove the utxos() accessor on Wallet and greatly reduce the duplication between the exposed wallet functions.
9d16766 to
6fb65df
Compare
97246f8 to
529f548
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The diff here is quite big :/
What is your appetite for making it smaller?
I think there might be a reasonably easy way of doing that.
- Make a new branch from master.
- First commit: Bump to
baruversion0.3. - Second commit: Introduce
EsploraClientas a struct that owns thebase_url - Third commit: Add a git dependency on
baruthat uses the new wallet and deletes all the old stuff.
Given that the overall structure of the #[wasm_bindgen] functions etc didn't change much, (4) should be relatively straight forward and should result in a much smaller diff :)
2f8aa4a to
5d0fdac
Compare
thomaseizinger
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! This diff is actually reviewable now :)
| }) | ||
| .collect() | ||
| } | ||
| pub use baru::BalanceEntry; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should go to the very top to the other exports.
|
You probably want to rebase your stuff onto #233 and drop the commit where you upgrade to |
5d0fdac to
c06bc77
Compare
c06bc77 to
57bbdaf
Compare
fa23aeb to
1cc5d7e
Compare
57bbdaf to
6bfdb4e
Compare
The client takes ownership of the base url
6bfdb4e to
69a6b4c
Compare
Coin selection and estimate transaction size has been moved to baru
69a6b4c to
c38d2cc
Compare
Sorry for tagging everyone as reviewer but It kind of does concern everyone.
NOTE: probably easier to checkout the branch and look at it in your IDE
The goal of this commit/branch is to extract wallet
functionality into a module so it can moved into the baru repo The
motivation for this refactor is so the platform team can upgrade the
protocols to use PSET's. This should also take some workload away
from from product team.
The stuff that will be moved into the baru repo is in the baru-wallet crate