-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
357 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use super::*; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct Purse { | ||
pub(crate) wallet: bdk::wallet::Wallet<SqliteDatabase>, | ||
pub(crate) blockchain: RpcBlockchain, | ||
} | ||
|
||
impl Purse { | ||
pub(crate) fn init(options: &Options) -> Result { | ||
let path = data_dir() | ||
.ok_or_else(|| anyhow!("Failed to retrieve data dir"))? | ||
.join("ord"); | ||
|
||
if path.exists() { | ||
return Err(anyhow!("Wallet already exists.")); | ||
} | ||
|
||
fs::create_dir_all(&path)?; | ||
|
||
let seed = Mnemonic::generate_in_with(&mut rand::thread_rng(), Language::English, 12)?; | ||
|
||
fs::write(path.join("entropy"), seed.to_entropy())?; | ||
|
||
let wallet = bdk::wallet::Wallet::new( | ||
Bip84((seed.clone(), None), KeychainKind::External), | ||
None, | ||
options.network, | ||
SqliteDatabase::new( | ||
path | ||
.join("wallet.sqlite") | ||
.to_str() | ||
.ok_or_else(|| anyhow!("Failed to convert path to str"))? | ||
.to_string(), | ||
), | ||
)?; | ||
|
||
wallet.sync( | ||
&RpcBlockchain::from_config(&RpcConfig { | ||
url: options.rpc_url(), | ||
auth: Auth::Cookie { | ||
file: options.cookie_file()?, | ||
}, | ||
network: options.network, | ||
wallet_name: wallet_name_from_descriptor( | ||
Bip84((seed, None), KeychainKind::External), | ||
None, | ||
options.network, | ||
&Secp256k1::new(), | ||
)?, | ||
skip_blocks: None, | ||
})?, | ||
SyncOptions::default(), | ||
)?; | ||
|
||
eprintln!("Wallet initialized."); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(crate) fn load(options: &Options) -> Result<Self> { | ||
let path = data_dir() | ||
.ok_or_else(|| anyhow!("Failed to retrieve data dir"))? | ||
.join("ord"); | ||
|
||
if !path.exists() { | ||
return Err(anyhow!("Wallet doesn't exist.")); | ||
} | ||
|
||
let key = ( | ||
Mnemonic::from_entropy(&fs::read(path.join("entropy"))?)?, | ||
None, | ||
); | ||
|
||
let wallet = bdk::wallet::Wallet::new( | ||
Bip84(key.clone(), KeychainKind::External), | ||
None, | ||
options.network, | ||
SqliteDatabase::new( | ||
path | ||
.join("wallet.sqlite") | ||
.to_str() | ||
.ok_or_else(|| anyhow!("Failed to convert path to str"))? | ||
.to_string(), | ||
), | ||
)?; | ||
|
||
let blockchain = RpcBlockchain::from_config(&RpcConfig { | ||
url: options.rpc_url(), | ||
auth: Auth::Cookie { | ||
file: options.cookie_file()?, | ||
}, | ||
network: options.network, | ||
wallet_name: wallet_name_from_descriptor( | ||
Bip84(key, KeychainKind::External), | ||
None, | ||
options.network, | ||
&Secp256k1::new(), | ||
)?, | ||
skip_blocks: None, | ||
})?; | ||
|
||
wallet.sync(&blockchain, SyncOptions::default())?; | ||
|
||
Ok(Self { wallet, blockchain }) | ||
} | ||
|
||
pub(crate) fn find(&self, options: &Options, ordinal: Ordinal) -> Result<LocalUtxo> { | ||
let index = Index::index(options)?; | ||
|
||
for utxo in self.wallet.list_unspent()? { | ||
if let Some(ranges) = index.list(utxo.outpoint)? { | ||
for (start, end) in ranges { | ||
if ordinal.0 >= start && ordinal.0 < end { | ||
return Ok(utxo); | ||
} | ||
} | ||
} | ||
} | ||
|
||
bail!("No utxo contains {}˚.", ordinal); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use super::*; | ||
|
||
pub(crate) fn run(options: Options) -> Result { | ||
println!("{}", get_wallet(options)?.get_balance()?); | ||
println!("{}", Purse::load(&options)?.wallet.get_balance()?); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
use super::*; | ||
|
||
pub(crate) fn run(options: Options) -> Result { | ||
println!("{}", get_wallet(options)?.get_address(LastUnused)?.address); | ||
println!( | ||
"{}", | ||
Purse::load(&options)? | ||
.wallet | ||
.get_address(LastUnused)? | ||
.address | ||
); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,5 @@ | ||
use super::*; | ||
|
||
pub(crate) fn run(options: Options) -> Result { | ||
let path = data_dir() | ||
.ok_or_else(|| anyhow!("Failed to retrieve data dir"))? | ||
.join("ord"); | ||
|
||
if path.exists() { | ||
return Err(anyhow!("Wallet already exists.")); | ||
} | ||
|
||
fs::create_dir_all(&path)?; | ||
|
||
let seed = Mnemonic::generate_in_with(&mut rand::thread_rng(), Language::English, 12)?; | ||
|
||
fs::write(path.join("entropy"), seed.to_entropy())?; | ||
|
||
let wallet = bdk::wallet::Wallet::new( | ||
Bip84((seed.clone(), None), KeychainKind::External), | ||
None, | ||
options.network, | ||
SqliteDatabase::new( | ||
path | ||
.join("wallet.sqlite") | ||
.to_str() | ||
.ok_or_else(|| anyhow!("Failed to convert path to str"))? | ||
.to_string(), | ||
), | ||
)?; | ||
|
||
wallet.sync( | ||
&RpcBlockchain::from_config(&RpcConfig { | ||
url: options.rpc_url(), | ||
auth: Auth::Cookie { | ||
file: options.cookie_file()?, | ||
}, | ||
network: options.network, | ||
wallet_name: wallet_name_from_descriptor( | ||
Bip84((seed, None), KeychainKind::External), | ||
None, | ||
options.network, | ||
&Secp256k1::new(), | ||
)?, | ||
skip_blocks: None, | ||
})?, | ||
SyncOptions::default(), | ||
)?; | ||
|
||
eprintln!("Wallet initialized."); | ||
|
||
Ok(()) | ||
Purse::init(&options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use super::*; | ||
|
||
#[derive(Debug, Parser)] | ||
pub(crate) struct Send { | ||
#[clap(long)] | ||
address: Address, | ||
#[clap(long)] | ||
ordinal: Ordinal, | ||
} | ||
|
||
impl Send { | ||
pub(crate) fn run(self, options: Options) -> Result { | ||
let wallet = Purse::load(&options)?; | ||
|
||
let utxo = wallet.find(&options, self.ordinal)?; | ||
|
||
let (mut psbt, _details) = { | ||
let mut builder = wallet.wallet.build_tx(); | ||
|
||
builder | ||
.manually_selected_only() | ||
.fee_absolute(0) | ||
.add_utxo(utxo.outpoint)? | ||
.add_recipient(self.address.script_pubkey(), utxo.txout.value); | ||
|
||
builder.finish()? | ||
}; | ||
|
||
if !wallet.wallet.sign(&mut psbt, SignOptions::default())? { | ||
bail!("Failed to sign transaction."); | ||
} | ||
|
||
let tx = psbt.extract_tx(); | ||
|
||
wallet.blockchain.broadcast(&tx)?; | ||
|
||
println!( | ||
"Sent ordinal {} to address {}: {}", | ||
self.ordinal.0, | ||
self.address, | ||
tx.txid() | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.