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

Protect ordinals from being used in fees #369

Merged
merged 10 commits into from
Aug 30, 2022
Merged
Changes from 1 commit
Commits
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
Progress
terror committed Aug 30, 2022
commit 824c6f0167417e6529ea8d105b669c309ef8f091
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@ tokio-stream = "0.1.9"
tokio-util = {version = "0.7.3", features = ["compat"] }
tower = "0.4.13"
tower-http = { version = "0.3.3", features = ["cors"] }
lazy_static = "1.4.0"

[dependencies.bdk]
git = "https://github.com/terror/bdk.git"
12 changes: 2 additions & 10 deletions src/purse.rs
Original file line number Diff line number Diff line change
@@ -69,9 +69,7 @@ impl Purse {
Ok(Self { blockchain, wallet })
}

pub(crate) fn find(&self, options: &Options, ordinal: Ordinal) -> Result<LocalUtxo> {
let index = Index::index(options)?;

pub(crate) fn find(&self, index: &Index, ordinal: Ordinal) -> Result<LocalUtxo> {
for utxo in self.wallet.list_unspent()? {
match index.list(utxo.outpoint)? {
Some(List::Unspent(ranges)) => {
@@ -95,13 +93,7 @@ impl Purse {
bail!("No utxo contains {}˚.", ordinal);
}

pub(crate) fn special_ordinals(
&self,
options: &Options,
outpoint: OutPoint,
) -> Result<Vec<Ordinal>> {
let index = Index::index(options)?;

pub(crate) fn special_ordinals(&self, index: &Index, outpoint: OutPoint) -> Result<Vec<Ordinal>> {
match index.list(outpoint)? {
Some(List::Unspent(ranges)) => Ok(
ranges
4 changes: 3 additions & 1 deletion src/subcommand/wallet/identify.rs
Original file line number Diff line number Diff line change
@@ -3,13 +3,15 @@ use super::*;
pub(crate) fn run(options: Options) -> Result {
let purse = Purse::load(&options)?;

let index = Index::index(&options)?;

let mut ordinals = purse
.wallet
.list_unspent()?
.into_iter()
.map(|utxo| {
purse
.special_ordinals(&options, utxo.outpoint)
.special_ordinals(&index, utxo.outpoint)
.map(|ordinals| {
ordinals
.into_iter()
38 changes: 36 additions & 2 deletions src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
@@ -12,9 +12,11 @@ impl Send {
pub(crate) fn run(self, options: Options) -> Result {
let purse = Purse::load(&options)?;

let utxo = purse.find(&options, self.ordinal)?;
let index = Index::index(&options)?;

let ordinals = purse.special_ordinals(&options, utxo.outpoint)?;
let utxo = purse.find(&index, self.ordinal)?;

let ordinals = purse.special_ordinals(&index, utxo.outpoint)?;

if !ordinals.is_empty() && (ordinals.len() > 1 || ordinals[0] != self.ordinal) {
bail!(
@@ -40,6 +42,38 @@ impl Send {
builder.finish()?
};

let output_value = psbt
.unsigned_tx
.output
.iter()
.map(|output| output.value)
.sum::<u64>();

let list = index.list(utxo.outpoint)?;

let mut offset = 0;

match list {
Some(List::Unspent(ranges)) => {
for (start, end) in ranges {
if start <= self.ordinal.n() && self.ordinal.n() < end {
offset += self.ordinal.n() - start;
break;
} else {
offset += end - start;
}
}
}
Some(List::Spent(txid)) => {
todo!()
}
None => todo!(),
}

if offset >= output_value {
bail!("Trying to send ordinal that would have been used in fees");
}

if !purse.wallet.sign(&mut psbt, SignOptions::default())? {
bail!("Failed to sign transaction.");
}