Skip to content

Add wallet creation example using RPC #519

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

Conversation

rajarshimaitra
Copy link
Contributor

Description

As mentioned in bitcoindevkit/bitcoindevkit.org#83 (comment) it would be helpful
to have wallet creation example code in the example directory.

This is an attempt to create the most simplistic bdk wallet with a RPC backend and demonstrate receiving, creating, signing
and broadcasting transaction.

The code is a refinement of the RPC tutorial https://bitcoindevkit.org/blog/bitcoin-core-rpc-demo/ with more elaborate doc comments.

Notes to the reviewers

To automate the background RPC process electrsd is used which is already a dev-dependency of bdk.

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

@rajarshimaitra
Copy link
Contributor Author

Apparently cargo test is trying to run the example which has some extra feature dependency and causing the test failure in CI.

I am not sure why thats not the case for other examples though (they also have special required deps).

@notmandatory @afilini am I missing something in the cargo configuration?

@rajarshimaitra
Copy link
Contributor Author

Fixed build failure, all tests passing..

@ConorOkus
Copy link

Trying to run this locally at the moment, but failing. What command should I run? cargo run --example rpcwallet?

@rajarshimaitra
Copy link
Contributor Author

@ConorOkus this should work cargo run --features="all-keys key-value-db rpc" --example rpcwallet.

@ConorOkus
Copy link

tACK

@LLFourn
Copy link
Contributor

LLFourn commented Jan 25, 2022

utACK does the example get run in the ci?

@rajarshimaitra
Copy link
Contributor Author

utACK does the example get run in the ci?

I think currently it doesn't. But we can explicitly ask the CI to run examples I think. Haven't tried that out.

@afilini
Copy link
Member

afilini commented Jan 27, 2022

I think it's ok if we just compile this example in the CI, I'm guessing what it does is already covered by specific unit tests

@ConorOkus
Copy link

I'm trying to do a similar thing in LDK here

@afilini
Copy link
Member

afilini commented Jan 28, 2022

It's not as easy for BDK because different examples might require different features. So if you just compile them like you did, they will most likely fail.

You would have to get the list of features from the Cargo.toml and then compile using those

@rajarshimaitra
Copy link
Contributor Author

rajarshimaitra commented Feb 9, 2022

Retested

All looks good on this one to me.. Its good to go I think..

@notmandatory
Copy link
Member

This example LGTM but I'm going hold off merging it until #535 goes in so it can be refactored as needed first. Also removing from next milestone since we're already a bit behind and have some higher priority PRs to get in.

@xraid
Copy link

xraid commented Mar 18, 2022

has this been massaged to accommodate new stuff in merged #535

@rajarshimaitra
Copy link
Contributor Author

has this been massaged to accommodate new stuff in merged #535

Good point, This will definitely have effect on the example. Will update it accordingly..

@notmandatory
Copy link
Member

I think this PR also needs a rebase to pick up the latest verify changes, in particular related to the CI Blockchain jobs.

@rajarshimaitra rajarshimaitra force-pushed the example-rpc-wallet branch 2 times, most recently from 14cb771 to a722c52 Compare March 22, 2022 07:07
@rajarshimaitra
Copy link
Contributor Author

Fixed the example in light of #535 and rebased..

Copy link
Member

@notmandatory notmandatory left a comment

Choose a reason for hiding this comment

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

I tried testing but got an error, do I need to do something else to run it?:

% cargo run --features keys-bip39,key-value-db,rpc --example rpcwallet
warning: field is never read: `network`
  --> src/blockchain/rpc.rs:58:5
   |
58 |     network: Network,
   |     ^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: `bdk` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.07s
     Running `target/debug/examples/rpcwallet`
>> Setting up bitcoind
>> bitcoind setup complete
Available coins in Core wallet : 50.00000000 BTC

>> Setting up BDK wallet
Error: Rpc(JsonRpc(Transport(Timeout)))

This adds an example wallet creation code with sled DB and RPC
Blockchain.

The backend RPC is managed using electrsd::bitcoind
@rajarshimaitra
Copy link
Contributor Author

Thanks @notmandatory for the review.. I fixed the nits.

I tried testing but got an error, do I need to do something else to run it?:

This seems very weird. Its not happening in my local. And it seems the error is originating from blockchain::rpc not the example. Thats even more odd. But I am not sure how to reproduce it.. Probably because of a different rustc version you are using??

@xraid
Copy link

xraid commented Mar 27, 2022

i get :

Setting up bitcoind
bitcoind setup complete
Available coins in Core wallet : 50.00000000 BTC

Setting up BDK wallet
Error: Rpc(JsonRpc(Transport(SocketError(Os { code: 35, kind: WouldBlock, message: "Resource temporarily unavailable" }))))

when i swap out in file rpcwallet.rs to use :

use bdk::bitcoincore_rpc::{Auth as rpc_auth, Client, RpcApi};

with Client invocation dance => instead of using :

use electrsd;

the BDK wallet finds bitcoind and all is good ...

While running a local regtest bitcoind with correct auth ...

@rajarshimaitra
Copy link
Contributor Author

Thanks @xraid for reporting.. I am still not being able to reproduce any of the issues. Its all working fine in my local as normal.

Did you mean you have to do the bitcoin core rpc setup manually instead of using electrsd crate and then it worked?

@xraid
Copy link

xraid commented Mar 28, 2022

the err says it blocks ? @afilini mentioned something about sharing connections to backends - that might not be the issue here, maybe it is the bitcoin.cookie in auth for the bdk-wallet ?

i changed and used Client instead of the electrsd lines [50...75]

line 103 reuse bitcoind object for bdk-wallet that was used for core-wallet prior
let rpc_config = RpcConfig { url: bitcoind.params.rpc_socket.to_string(), auth: bitcoind_auth,

i am still to fresh in Rust and the BDK code base to make any sense in try debug,

so i leave it to You and meanwhile watch ...

@rajarshimaitra
Copy link
Contributor Author

the err says it blocks ? @afilini mentioned something about sharing connections to backends - that might not be the issue here, maybe it is the bitcoin.cookie in auth for the bdk-wallet ?

This should not be an issue because elecrsd deploys a local bitcoind only for the code in concern, and deletes it once the example ends.. So there should be only one instance of bdk trying to connect with the temporary bitcoind.

i changed and used Client instead of the electrsd lines [50...75]

In that case did you manually deploy the regtest node in the background?

Ya I will keep digging to see if I can repro this..

2 similar comments
@rajarshimaitra
Copy link
Contributor Author

the err says it blocks ? @afilini mentioned something about sharing connections to backends - that might not be the issue here, maybe it is the bitcoin.cookie in auth for the bdk-wallet ?

This should not be an issue because elecrsd deploys a local bitcoind only for the code in concern, and deletes it once the example ends.. So there should be only one instance of bdk trying to connect with the temporary bitcoind.

i changed and used Client instead of the electrsd lines [50...75]

In that case did you manually deploy the regtest node in the background?

Ya I will keep digging to see if I can repro this..

@rajarshimaitra
Copy link
Contributor Author

the err says it blocks ? @afilini mentioned something about sharing connections to backends - that might not be the issue here, maybe it is the bitcoin.cookie in auth for the bdk-wallet ?

This should not be an issue because elecrsd deploys a local bitcoind only for the code in concern, and deletes it once the example ends.. So there should be only one instance of bdk trying to connect with the temporary bitcoind.

i changed and used Client instead of the electrsd lines [50...75]

In that case did you manually deploy the regtest node in the background?

Ya I will keep digging to see if I can repro this..

@xraid
Copy link

xraid commented Mar 29, 2022

i run a local Nigiri regtest bitcoind electrum and used the bdk::bitcoincore_rpc::Client for core-wallet and for bdk-wallet i use :

bdk::blockchain::rpc::RpcBlockchain

where i added auth and url in config :

// Set RPC username and password
let auth = Auth::UserPass {
    username: "admin1".to_string(),
    password: "123".to_string()
};
// Set RPC url
let mut rpc_url = "http://".to_string();
rpc_url.push_str("127.0.0.1:18443");

// Create a RPC configuration of the running bitcoind backend we created in last step
// Note: If you are using custom regtest node, use the appropriate url and auth
let rpc_config = RpcConfig {
    url: rpc_url, // bitcoind.params.rpc_socket.to_string(),
    auth: auth, //bitcoind_auth,
    network: Network::Regtest,
    wallet_name,
    skip_blocks: None,
};

// Use the above configuration to create a RPC blockchain backend
let blockchain = RpcBlockchain::from_config(&rpc_config)?;

so there is 2 diff connections to bitcoind rpc - one for core and one for bdk

Why the example with using the use electrsd; succeeds with first connect to core wallet and fails when trying to connect from initiated bdk wallet, i dont know ...

Copy link
Member

@notmandatory notmandatory left a comment

Choose a reason for hiding this comment

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

tACK 8a98e69

I verified this runs on my Linux (ubuntu 20.04) box. I'm still getting an error on my macOS system, but I suspect it's something specific to the mac. I'd like to know what's blocking it on the mac but not a reason to hold up this PR.

@notmandatory notmandatory merged commit ff6db18 into bitcoindevkit:master Mar 29, 2022
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.

6 participants