Skip to content

Commit

Permalink
Add ord wallet recieve (ordinals#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Dec 7, 2022
1 parent 5388df7 commit b0c2d85
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use {super::*, transaction_builder::TransactionBuilder};

mod identify;
mod inscribe;
mod receive;
mod send;
mod transaction_builder;

Expand Down Expand Up @@ -58,6 +59,7 @@ fn get_change_addresses(options: &Options, n: usize) -> Result<Vec<Address>> {
pub(crate) enum Wallet {
Identify(identify::Identify),
Inscribe(inscribe::Inscribe),
Receive(receive::Receive),
Send(send::Send),
}

Expand All @@ -66,6 +68,7 @@ impl Wallet {
match self {
Self::Identify(identify) => identify.run(options),
Self::Inscribe(inscribe) => inscribe.run(options),
Self::Receive(receive) => receive.run(options),
Self::Send(send) => send.run(options),
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/subcommand/wallet/receive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use super::*;

#[derive(Debug, Parser)]
pub(crate) struct Receive {}

impl Receive {
pub(crate) fn run(self, options: Options) -> Result {
println!(
"{}",
options
.bitcoin_rpc_client_for_wallet_command("ord wallet receive")?
.get_new_address(None, None)?
);

Ok(())
}
}
7 changes: 7 additions & 0 deletions test-bitcoincore-rpc/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,11 @@ pub trait Api {
&self,
params: Vec<serde_json::Value>,
) -> Result<serde_json::Value, jsonrpc_core::Error>;

#[rpc(name = "getnewaddress")]
fn get_new_address(
&self,
label: Option<String>,
address_type: Option<()>,
) -> Result<bitcoin::Address, jsonrpc_core::Error>;
}
13 changes: 13 additions & 0 deletions test-bitcoincore-rpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,17 @@ impl Api for Server {
) -> Result<serde_json::Value, jsonrpc_core::Error> {
Ok(json!([{"success": true}]))
}

fn get_new_address(
&self,
_label: Option<String>,
_address_type: Option<()>,
) -> Result<bitcoin::Address, jsonrpc_core::Error> {
let secp256k1 = Secp256k1::new();
let key_pair = KeyPair::new(&secp256k1, &mut rand::thread_rng());
let (public_key, _parity) = XOnlyPublicKey::from_keypair(&key_pair);
let address = Address::p2tr(&secp256k1, public_key, None, self.network);

Ok(address)
}
}
2 changes: 1 addition & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use {
self::{command_builder::CommandBuilder, expected::Expected, test_server::TestServer},
bitcoin::{blockdata::constants::COIN_VALUE, Network, OutPoint, Txid},
bitcoin::{blockdata::constants::COIN_VALUE, Address, Network, OutPoint, Txid},
executable_path::executable_path,
pretty_assertions::assert_eq as pretty_assert_eq,
regex::Regex,
Expand Down
14 changes: 13 additions & 1 deletion tests/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::*;
use {super::*, std::str::FromStr};

fn reveal_txid_from_inscribe_stdout(stdout: &str) -> Txid {
stdout
Expand Down Expand Up @@ -522,3 +522,15 @@ fn inscriptions_cannot_be_sent_by_satpoint() {
.expected_exit_code(1)
.run();
}

#[test]
fn receive() {
let rpc_server = test_bitcoincore_rpc::spawn();

let stdout = CommandBuilder::new("wallet receive")
.rpc_server(&rpc_server)
.stdout_regex(".*")
.run();

assert!(Address::from_str(stdout.trim()).is_ok());
}

0 comments on commit b0c2d85

Please sign in to comment.