Skip to content
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

Wallet uses JSON RPC #2929

Merged
merged 87 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
7c244c9
Wallet talks to index over http
raphjaph Dec 31, 2023
e0efc59
Fix test
raphjaph Jan 2, 2024
bf5d451
It compiles
raphjaph Jan 2, 2024
548ddae
Place clippy + fmt
raphjaph Jan 2, 2024
8166805
Merge branch 'master' of github.com:ordinals/ord into wallet-spawns-s…
raphjaph Jan 2, 2024
17ca387
Merge branch 'master' of github.com:ordinals/ord into wallet-spawns-s…
raphjaph Jan 3, 2024
bb9ab59
Put chain in wallet
raphjaph Jan 3, 2024
b3dcb8b
Fix wallet bug
raphjaph Jan 3, 2024
cfd83c0
Getting somewhere; need to add status json endpoint
raphjaph Jan 3, 2024
00d72a4
Add TODO
raphjaph Jan 3, 2024
2578136
Merge branch 'master' of github.com:ordinals/ord into wallet-spawns-s…
raphjaph Jan 3, 2024
a73b960
Merge branch 'master' of github.com:ordinals/ord into wallet-spawns-s…
raphjaph Jan 3, 2024
c495f1f
Get server status
raphjaph Jan 3, 2024
ab9b080
Inscribe does not use index
raphjaph Jan 4, 2024
b0771dd
Rid balance command of the index
raphjaph Jan 4, 2024
44c350e
inscriptions command no index
raphjaph Jan 4, 2024
c8ea786
Rid sats command of index
raphjaph Jan 4, 2024
903857d
Start work on send, waiting for rune json endpoint
raphjaph Jan 4, 2024
69d4ba2
output and cardinal command no index
raphjaph Jan 4, 2024
8cc344f
Merge branch 'master' of github.com:ordinals/ord into wallet-spawns-s…
raphjaph Jan 5, 2024
38571af
fix merge
raphjaph Jan 5, 2024
c1d3605
Etch no index
raphjaph Jan 5, 2024
080998f
Send no index
raphjaph Jan 5, 2024
e17e475
Merge branch 'master' of github.com:ordinals/ord into wallet-spawns-s…
raphjaph Jan 6, 2024
b274285
Stuck
raphjaph Jan 6, 2024
c6c1abe
Add in index for output JSON
raphjaph Jan 6, 2024
aef6879
Merge branch 'add-in-index-to-output' into wallet-spawns-server
raphjaph Jan 6, 2024
6a15930
Add check sync
raphjaph Jan 6, 2024
fbb2e99
Fix unsynced index test
raphjaph Jan 6, 2024
296bd38
Remove unused functions and only pass in wallet
raphjaph Jan 6, 2024
8ecb107
stashing
raphjaph Jan 7, 2024
364123b
Merge branch 'master' of github.com:ordinals/ord into wallet-spawns-s…
raphjaph Jan 8, 2024
5d08319
Remove bool
raphjaph Jan 8, 2024
32e191b
fmt
raphjaph Jan 8, 2024
cbeb7fb
quick fix
raphjaph Jan 8, 2024
d40d14c
It works, just have to fix one test
raphjaph Jan 10, 2024
42172fe
Merge branch 'master' of github.com:ordinals/ord into wallet-spawns-s…
raphjaph Jan 10, 2024
f8480ba
quick fix
raphjaph Jan 10, 2024
e6de26c
Fix preview test
raphjaph Jan 10, 2024
c297ab4
It works but tests are slow now
raphjaph Jan 10, 2024
7ef0544
Merge branch 'master' of github.com:ordinals/ord into wallet-spawns-s…
raphjaph Jan 10, 2024
e3f8114
Some cleanup
raphjaph Jan 10, 2024
5a80f92
quick fix
raphjaph Jan 11, 2024
8585a54
quick fix
raphjaph Jan 11, 2024
eab91f4
Merge branch 'master' of github.com:ordinals/ord into wallet-spawns-s…
raphjaph Jan 11, 2024
df9b15e
Place clippy
raphjaph Jan 11, 2024
39fe0d6
quick fix
raphjaph Jan 11, 2024
efeede5
Clean up some bits here and there
raphjaph Jan 11, 2024
85417cd
Merge branch 'master' of github.com:ordinals/ord into wallet-spawns-s…
raphjaph Jan 13, 2024
66755df
Move wallet into separate module
raphjaph Jan 13, 2024
8be34a5
Place clippy
raphjaph Jan 13, 2024
288d25d
Horrible state
raphjaph Jan 15, 2024
a22ca3d
Horribel state 2
raphjaph Jan 15, 2024
6815ce4
Tests pass
raphjaph Jan 16, 2024
55b091d
Split up batch stuff
raphjaph Jan 16, 2024
930bf5d
Merge branch 'master' of github.com:ordinals/ord into wallet-spawns-s…
raphjaph Jan 16, 2024
257c2d8
Fix test
raphjaph Jan 16, 2024
f59744b
Require running ord server for wallet commands
raphjaph Jan 16, 2024
0e291ab
Got one test to pass with new setup
raphjaph Jan 19, 2024
660da09
All unit tests pass
raphjaph Jan 19, 2024
a7c883e
I think all integration tests pass
raphjaph Jan 20, 2024
9633b60
Rename helper function
raphjaph Jan 20, 2024
607e539
Maybe faster now
raphjaph Jan 21, 2024
5ede49f
Placate clippy
raphjaph Jan 21, 2024
1be0799
Fix preview command
raphjaph Jan 22, 2024
4bb5963
Fixed preview test?
raphjaph Jan 22, 2024
6e29ccb
quick fix
raphjaph Jan 22, 2024
b68451c
quick fix
raphjaph Jan 22, 2024
75a119f
quick fix
raphjaph Jan 22, 2024
1ee921c
Remove preview command
raphjaph Jan 22, 2024
c16b3c6
remove unused options field
casey Jan 22, 2024
e375a7a
Sort
casey Jan 22, 2024
b986bbe
Gracefully shut down index in ctrl-c handler
casey Jan 22, 2024
5b6b5bd
Move shutdown into success branch
casey Jan 22, 2024
151cd4d
Tweak
casey Jan 22, 2024
050a3ca
check -> has
casey Jan 22, 2024
a1dc47d
Avoid clone
casey Jan 22, 2024
7755af3
Reuse bitcoin client
casey Jan 22, 2024
fe7197b
Reuse client
casey Jan 22, 2024
68aadc8
Rename modules
casey Jan 22, 2024
20ff4fa
REsuse client
casey Jan 22, 2024
2e2a9eb
Use correct create wallet
casey Jan 23, 2024
ea7ba4e
Wallet
casey Jan 23, 2024
b3cd660
Tweak
casey Jan 23, 2024
5ce1d28
Tweak
casey Jan 23, 2024
4d1da88
Tweak
casey Jan 23, 2024
fdeff81
Tweak
casey Jan 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix test
  • Loading branch information
raphjaph committed Jan 2, 2024
commit e0efc59bdbd9b974c39db8ac837a26e13ea05ef1
54 changes: 28 additions & 26 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,20 +381,6 @@ impl Index {
self.durability = durability;
}

pub(crate) fn check_sync(&self, utxos: &BTreeMap<OutPoint, Amount>) -> Result<bool> {
let rtx = self.database.begin_read()?;
let outpoint_to_value = rtx.open_table(OUTPOINT_TO_VALUE)?;
for outpoint in utxos.keys() {
if outpoint_to_value.get(&outpoint.store())?.is_none() {
return Err(anyhow!(
"output in Bitcoin Core wallet but not in ord index: {outpoint}"
));
}
}

Ok(true)
}

pub(crate) fn has_rune_index(&self) -> bool {
self.index_runes
}
Expand Down Expand Up @@ -3394,20 +3380,36 @@ mod tests {
let mut entropy = [0; 16];
rand::thread_rng().fill_bytes(&mut entropy);
let mnemonic = Mnemonic::from_entropy(&entropy).unwrap();
crate::subcommand::wallet::initialize("ord".into(), &context.options, mnemonic.to_seed(""))
.unwrap();

Arguments {
options: context.options.clone(),
subcommand: Subcommand::Wallet(crate::subcommand::wallet::WalletCommand {
name: "ord".into(),
subcommand: crate::subcommand::wallet::Subcommand::Create(
crate::subcommand::wallet::create::Create {
passphrase: "".into(),
},
),
}),
}
.run()
.unwrap();

context.rpc_server.mine_blocks(1);
assert_regex_match!(
crate::subcommand::wallet::get_unspent_outputs(
&crate::subcommand::wallet::bitcoin_rpc_client_for_wallet_command(
"ord".to_string(),
&context.options,
)
.unwrap(),
&context.index

let wallet = crate::subcommand::wallet::Wallet {
bitcoin_rpc_client: crate::subcommand::wallet::bitcoin_rpc_client_for_wallet(
"ord".into(),
&context.options,
)
.unwrap_err()
.to_string(),
.unwrap(),
ord_api_url: "127.0.0.1:8080".parse().unwrap(),
ord_http_client: reqwest::blocking::Client::new(),
wallet_name: "ord".into(),
};

assert_regex_match!(
wallet.get_unspent_outputs().unwrap_err().to_string(),
r"output in Bitcoin Core wallet but not in ord index: [[:xdigit:]]{64}:\d+"
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ mod tests {
);
}

fn parse_wallet_args(args: &str) -> (Options, subcommand::wallet::Wallet) {
fn parse_wallet_args(args: &str) -> (Options, subcommand::wallet::WalletCommand) {
match Arguments::try_parse_from(args.split_whitespace()) {
Ok(arguments) => match arguments.subcommand {
Subcommand::Wallet(wallet) => (arguments.options, wallet),
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub(crate) enum Subcommand {
#[command(about = "Display satoshi traits")]
Traits(traits::Traits),
#[command(about = "Wallet commands")]
Wallet(wallet::Wallet),
Wallet(wallet::WalletCommand),
}

impl Subcommand {
Expand Down
19 changes: 14 additions & 5 deletions src/subcommand/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,19 @@ impl Preview {
thread::sleep(Duration::from_millis(50));
}

super::wallet::create::Create {
passphrase: "".into(),
Arguments {
options: options.clone(),
subcommand: Subcommand::Wallet(crate::subcommand::wallet::WalletCommand {
name: "ord".into(),
subcommand: crate::subcommand::wallet::Subcommand::Create(
crate::subcommand::wallet::create::Create {
passphrase: "".into(),
},
),
}),
}
.run("ord".into(), options.clone())?;
.run()
.unwrap();
raphjaph marked this conversation as resolved.
Show resolved Hide resolved

let rpc_client = options.bitcoin_rpc_client(None)?;

Expand All @@ -102,7 +111,7 @@ impl Preview {
for file in files {
Arguments {
options: options.clone(),
subcommand: Subcommand::Wallet(super::wallet::Wallet {
subcommand: Subcommand::Wallet(super::wallet::WalletCommand {
name: "ord".into(),
subcommand: super::wallet::Subcommand::Inscribe(super::wallet::inscribe::Inscribe {
batch: None,
Expand Down Expand Up @@ -135,7 +144,7 @@ impl Preview {
for batch in batches {
Arguments {
options: options.clone(),
subcommand: Subcommand::Wallet(super::wallet::Wallet {
subcommand: Subcommand::Wallet(super::wallet::WalletCommand {
name: "ord".into(),
subcommand: super::wallet::Subcommand::Inscribe(super::wallet::inscribe::Inscribe {
batch: Some(batch),
Expand Down
27 changes: 13 additions & 14 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub mod transaction_builder;
pub mod transactions;

#[derive(Debug, Parser)]
pub(crate) struct Wallet {
pub(crate) struct WalletCommand {
#[arg(long, default_value = "ord", help = "Use wallet named <WALLET>.")]
pub(crate) name: String,
#[command(subcommand)]
Expand Down Expand Up @@ -65,7 +65,7 @@ pub(crate) enum Subcommand {
Cardinals,
}

impl Wallet {
impl WalletCommand {
pub(crate) fn run(self, options: Options) -> SubcommandResult {
let index = Arc::new(Index::open(&options)?);
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
let handle = axum_server::Handle::new();
Expand Down Expand Up @@ -95,8 +95,8 @@ impl Wallet {
});
}

let wallet_foo = WalletFoo {
bitcoin_rpc_client: bitcoin_rpc_client_for_wallet_command(self.name, &options)?,
let wallet = Wallet {
bitcoin_rpc_client: bitcoin_rpc_client_for_wallet(self.name, &options)?,
ord_api_url,
ord_http_client: {
let mut headers = header::HeaderMap::new();
Expand All @@ -113,12 +113,12 @@ impl Wallet {

match self.subcommand {
Subcommand::Balance => balance::run(self.name, options),
Subcommand::Create(create) => create.run(self.name, options),
Subcommand::Create(create) => create.run(wallet, options),
Subcommand::Etch(etch) => etch.run(self.name, options),
Subcommand::Inscribe(inscribe) => inscribe.run(self.name, options),
Subcommand::Inscribe(inscribe) => inscribe.run(wallet, options),
Subcommand::Inscriptions => inscriptions::run(self.name, options),
Subcommand::Receive => receive::run(self.name, options),
Subcommand::Restore(restore) => restore.run(self.name, options),
Subcommand::Restore(restore) => restore.run(wallet, options),
Subcommand::Sats(sats) => sats.run(self.name, options),
Subcommand::Send(send) => send.run(self.name, options),
Subcommand::Transactions(transactions) => transactions.run(self.name, options),
Expand All @@ -128,15 +128,14 @@ impl Wallet {
}
}

pub(crate) struct WalletFoo {
pub(crate) struct Wallet {
pub(crate) bitcoin_rpc_client: Client,
pub(crate) ord_api_url: Url,
// TODO: make this async
pub(crate) ord_http_client: reqwest::blocking::Client,
pub(crate) ord_http_client: reqwest::blocking::Client, // TODO: make async instead of blocking
pub(crate) wallet_name: String,
}

impl WalletFoo {
impl Wallet {
pub(crate) fn get_unspent_outputs(&self) -> Result<BTreeMap<OutPoint, Amount>> {
let mut utxos = BTreeMap::new();
utxos.extend(
Expand Down Expand Up @@ -232,14 +231,14 @@ impl WalletFoo {
)
}

pub(crate) fn initialize(&self, wallet: String, options: &Options, seed: [u8; 64]) -> Result {
pub(crate) fn initialize(&self, options: &Options, seed: [u8; 64]) -> Result {
let client = check_version(options.bitcoin_rpc_client(None)?)?;

let network = options.chain().network();

&self
.bitcoin_rpc_client
.create_wallet(&wallet, None, Some(true), None, None)?;
.create_wallet(&self.wallet_name, None, Some(true), None, None)?;

let secp = Secp256k1::new();

Expand Down Expand Up @@ -307,7 +306,7 @@ impl WalletFoo {
}
}

pub(crate) fn bitcoin_rpc_client_for_wallet_command(
pub(crate) fn bitcoin_rpc_client_for_wallet(
wallet_name: String,
options: &Options,
) -> Result<Client> {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) fn run(wallet: String, options: Options) -> SubcommandResult {
let index = Index::open(&options)?;
index.update()?;

let client = bitcoin_rpc_client_for_wallet_command(wallet, &options)?;
let client = bitcoin_rpc_client_for_wallet(wallet, &options)?;

let unspent_outputs = get_unspent_outputs(&client, &index)?;

Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/cardinals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) fn run(wallet: String, options: Options) -> SubcommandResult {

index.update()?;

let client = bitcoin_rpc_client_for_wallet_command(wallet, &options)?;
let client = bitcoin_rpc_client_for_wallet(wallet, &options)?;

let unspent_outputs = get_unspent_outputs(&client, &index)?;

Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/wallet/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ pub(crate) struct Create {
}

impl Create {
pub(crate) fn run(self, wallet: String, options: Options) -> SubcommandResult {
pub(crate) fn run(self, wallet: Wallet, options: Options) -> SubcommandResult {
let mut entropy = [0; 16];
rand::thread_rng().fill_bytes(&mut entropy);

let mnemonic = Mnemonic::from_entropy(&entropy)?;

wallet::initialize(wallet, &options, mnemonic.to_seed(self.passphrase.clone()))?;
wallet.initialize(&options, mnemonic.to_seed(self.passphrase.clone()))?;

Ok(Box::new(Output {
mnemonic,
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/etch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Etch {

index.update()?;

let client = bitcoin_rpc_client_for_wallet_command(wallet, &options)?;
let client = bitcoin_rpc_client_for_wallet(wallet, &options)?;

let SpacedRune { rune, spacers } = self.rune;

Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub(crate) struct Inscribe {
}

impl Inscribe {
pub(crate) fn run(self, wallet: WalletFoo, options: Options) -> SubcommandResult {
pub(crate) fn run(self, wallet: Wallet, options: Options) -> SubcommandResult {
let metadata = Inscribe::parse_metadata(self.cbor_metadata, self.json_metadata)?;

let index = Index::open(&options)?;
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/inscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) fn run(wallet: String, options: Options) -> SubcommandResult {
let index = Index::open(&options)?;
index.update()?;

let client = bitcoin_rpc_client_for_wallet_command(wallet, &options)?;
let client = bitcoin_rpc_client_for_wallet(wallet, &options)?;

let unspent_outputs = get_unspent_outputs(&client, &index)?;

Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) fn run(wallet: String, options: Options) -> SubcommandResult {

index.update()?;

let client = bitcoin_rpc_client_for_wallet_command(wallet, &options)?;
let client = bitcoin_rpc_client_for_wallet(wallet, &options)?;

let mut outputs = Vec::new();
for (output, amount) in get_unspent_outputs(&client, &index)? {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct Output {
}

pub(crate) fn run(wallet: String, options: Options) -> SubcommandResult {
let address = bitcoin_rpc_client_for_wallet_command(wallet, &options)?
let address = bitcoin_rpc_client_for_wallet(wallet, &options)?
.get_new_address(None, Some(bitcoincore_rpc::json::AddressType::Bech32m))?;

Ok(Box::new(Output { address }))
Expand Down
8 changes: 2 additions & 6 deletions src/subcommand/wallet/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ pub(crate) struct Restore {
}

impl Restore {
pub(crate) fn run(self, wallet_name: String, options: Options) -> SubcommandResult {
wallet::initialize(
wallet_name,
&options,
self.mnemonic.to_seed(self.passphrase),
)?;
pub(crate) fn run(self, wallet: Wallet, options: Options) -> SubcommandResult {
wallet.initialize(&options, self.mnemonic.to_seed(self.passphrase))?;

Ok(Box::new(Empty {}))
}
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/sats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Sats {

index.update()?;

let client = bitcoin_rpc_client_for_wallet_command(wallet, &options)?;
let client = bitcoin_rpc_client_for_wallet(wallet, &options)?;

let utxos = get_unspent_output_ranges(&client, &index)?;

Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Send {

index.update()?;

let client = bitcoin_rpc_client_for_wallet_command(wallet, &options)?;
let client = bitcoin_rpc_client_for_wallet(wallet, &options)?;

let chain = options.chain();

Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Output {

impl Transactions {
pub(crate) fn run(self, wallet: String, options: Options) -> SubcommandResult {
let client = bitcoin_rpc_client_for_wallet_command(wallet, &options)?;
let client = bitcoin_rpc_client_for_wallet(wallet, &options)?;

let mut output = Vec::new();
for tx in client.list_transactions(
Expand Down
Loading