-
Notifications
You must be signed in to change notification settings - Fork 6
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
Extract stateless functions into wallet library module #202
Conversation
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
Wallet
store 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::sync
function 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_loan
can now be synchronous functions onWallet
becauseWallet
owns most of the data necessary to do the task (txouts, blinding key etc)
Some notes about this design:
- It should allow the
Backend
trait to be minimal and only concern itself with IO. baru
can provide an implementation ofBackend
for 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
Backend
as a parameter tosync
allowsWallet
to remain simple and without type parameters and allows for easier testing using dummy backends. - Having
Wallet
own state likeTxOuts
makes all the functions likeextract_loan
quite 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
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
baru
version0.3
. - Second commit: Introduce
EsploraClient
as a struct that owns thebase_url
- Third commit: Add a git dependency on
baru
that 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
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