-
Notifications
You must be signed in to change notification settings - Fork 47
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
Add address index to return type of get_address #137
Add address index to return type of get_address #137
Conversation
ba23735
to
7f2bca0
Compare
Major update to this PR: This now defines a method I don't use the full |
7f2bca0
to
568392d
Compare
@artfuldev @notmandatory namespacing here is unclear for me, and I'm wondering if you have ideas to make sure it's consistent or insights in one way or another. Here is the problem as I see it: It is sometimes the case that we can't expose a struct fully, because it itself uses another struct which itself uses another struct and so on, which would all need to be defined/exposed in the ffi layer but are not always relevant to the task at hand. In such cases we can define "simplified" versions of those structs which flatten the complexity. But now those new structures need naming, and the bdk names are quite well chosen, so it's a bit of a tricky situation, also because from the user's point of view they now have to know that Here is what I've done in this PR. Not sure if it was a clean/good idea or if something else might be better, but let me know what you think. In cases where I needed to simplify a struct, I kept/stole the name from bdk and redefined a simpler struct, and then wrote the conversion methods (thanks for the tip @artfuldev!) which pull from the fully qualified bdk struct. It looks something like this: // we redefine a simpler version of bdk::wallet::AddressInfo
// it has an `address` field of type String
// instead of the `address` field of type `Address` defined in bitcoin::util::address::Address
pub struct AddressInfo {
pub index: u32,
pub address: String,
}
impl From<bdk::wallet::AddressInfo> for AddressInfo {
fn from(x: bdk::wallet::AddressInfo) -> AddressInfo {
AddressInfo {
index: x.index,
address: x.address.to_string()
}
}
} |
This is fine. We can retain the names and use bdk namespace qualifiers to refer to the original types. I don't think it's a deviation. We do the same for transaction, TxBuilder, and for the database and blockchain configuration types. |
One more thing I'm not so sure on is the use or not of a reference in the type parameter in the definition of the impl From<&bdk::TransactionDetails> for TransactionDetails {
fn from(x: &bdk::TransactionDetails) -> TransactionDetails {
TransactionDetails {
fees: x.fee,
txid: x.txid.to_string(),
received: x.received,
sent: x.sent,
}
}
} But mine doesn't take a reference: impl From<bdk::wallet::AddressInfo> for AddressInfo {
fn from(x: bdk::wallet::AddressInfo) -> AddressInfo {
AddressInfo {
index: x.index,
address: x.address.to_string()
}
}
} I'm unclear as per the tradeoff here between the two. |
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.
I've added a few minor improvements
We probably have to rebase after the bdk 0.18 update. |
src/lib.rs
Outdated
// we redefine a simpler version of bdk::wallet::AddressInfo | ||
// it has an `address` field of type String instead of the `address` field of type `Address` | ||
// defined in bitcoin::util::address::Address |
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.
Do we need this? I think it's fairly evident that we're shadowing/proxying here. If it's notes for the reviewers, we can add them as comments instead.
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.
Yeah agreed I'll remove them.
src/lib.rs
Outdated
// we redefine a simpler version of bdk::wallet::AddressIndex | ||
// only keeping the `New` and `LastUnused` variants of the enum |
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.
Same as above
src/lib.rs
Outdated
fn from(x: AddressIndex) -> bdk::wallet::AddressIndex { | ||
match x { | ||
AddressIndex::New => bdk::wallet::AddressIndex::New, | ||
AddressIndex::LastUnused => bdk::wallet::AddressIndex::LastUnused |
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.
nitpick: we can also alias bdk::wallet::AddressIndex
as BdkAddressIndex
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.
I like it for consistency with the others!
80ee2de
to
76b0a7f
Compare
0e354da
to
6beb98c
Compare
Thanks for the comments @artfuldev! |
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.
LGTM! 🎉
This is my first stab at adding an API from bdk into to bdk-ffi. Looking for feedback. In particular, I found I had to create a brand new struct in
lib.rs
(AddressInformation
) whose purpose was mostly just to mirror theAddressInfo
struct coming from bdk. I assume there is a better way and I should be able to use the type from bdk directly; please let me know if so.This PR is marked as draft because if the approach is agreed upon I'd still need to change the
get_new_address()
method to follow the same API. I assume also worthy of discussion is whether we might as well bring the whole ofAddressInfo
into bdk-ffi, which would add theKeychainKind
field (and associated type) toAddressInfo
/AddressInformation
.This PR fixes #130.
Notes to reviewers
This PR changes the return type of
get_last_unused_address()
fromString
toAddressInfo
.