-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
refuse to send if other rare ordinals would also be sent to recipient or fee #683
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -35,6 +35,7 @@ use { | |||||
pub(crate) enum Error { | ||||||
NotInWallet(Ordinal), | ||||||
NotEnoughCardinalUtxos, | ||||||
AccidentalRareOrdinalSend(Ordinal), | ||||||
} | ||||||
|
||||||
impl fmt::Display for Error { | ||||||
|
@@ -45,6 +46,10 @@ impl fmt::Display for Error { | |||||
f, | ||||||
"Wallet does not contain enough cardinal UTXOs. Please add additional funds to wallet." | ||||||
), | ||||||
Error::AccidentalRareOrdinalSend(ordinal) => write!( | ||||||
f, | ||||||
"This transaction would also send along Ordinal {ordinal}" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
), | ||||||
} | ||||||
} | ||||||
} | ||||||
|
@@ -76,16 +81,14 @@ impl TransactionBuilder { | |||||
recipient: Address, | ||||||
change: Vec<Address>, | ||||||
) -> Result<Transaction> { | ||||||
Ok( | ||||||
Self::new(ranges, ordinal, recipient, change) | ||||||
.select_ordinal()? | ||||||
.align_ordinal() | ||||||
.pad_alignment_output()? | ||||||
.add_postage()? | ||||||
.strip_excess_postage() | ||||||
.deduct_fee() | ||||||
.build(), | ||||||
) | ||||||
Self::new(ranges, ordinal, recipient, change) | ||||||
.select_ordinal()? | ||||||
.align_ordinal() | ||||||
.pad_alignment_output()? | ||||||
.add_postage()? | ||||||
.strip_excess_postage() | ||||||
.deduct_fee() | ||||||
.build() | ||||||
} | ||||||
|
||||||
fn new( | ||||||
|
@@ -262,7 +265,7 @@ impl TransactionBuilder { | |||||
Self::TARGET_FEE_RATE * dummy_transaction.vsize().try_into().unwrap() | ||||||
} | ||||||
|
||||||
fn build(self) -> Transaction { | ||||||
fn build(self) -> Result<Transaction> { | ||||||
let ordinal = self.ordinal.n(); | ||||||
let recipient = self.recipient.script_pubkey(); | ||||||
let transaction = Transaction { | ||||||
|
@@ -393,7 +396,38 @@ impl TransactionBuilder { | |||||
); | ||||||
} | ||||||
|
||||||
transaction | ||||||
let mut offset = 0; | ||||||
let mut rare_ordinals = Vec::<(Ordinal, u64)>::new(); | ||||||
for input in &transaction.input { | ||||||
for (start, end) in &self.ranges[&input.previous_output] { | ||||||
if Ordinal(*start).rarity() > Rarity::Common { | ||||||
rare_ordinals.push((Ordinal(*start), offset)); | ||||||
} | ||||||
offset += end - start; | ||||||
} | ||||||
} | ||||||
let total_input_amount = offset; | ||||||
|
||||||
let mut offset = 0; | ||||||
let mut recipient_range = (0, 0); | ||||||
for output in &transaction.output { | ||||||
if output.script_pubkey == self.recipient.script_pubkey() { | ||||||
recipient_range = (offset, offset + output.value); | ||||||
break; | ||||||
} | ||||||
offset += output.value; | ||||||
} | ||||||
|
||||||
for (rare_ordinal, offset) in &rare_ordinals { | ||||||
if rare_ordinal != &self.ordinal | ||||||
&& (offset >= &recipient_range.0 && offset < &recipient_range.1 | ||||||
|| offset >= &(total_input_amount - fee.to_sat())) | ||||||
{ | ||||||
return Err(Error::AccidentalRareOrdinalSend(*rare_ordinal)); | ||||||
} | ||||||
} | ||||||
|
||||||
Ok(transaction) | ||||||
} | ||||||
|
||||||
fn calculate_ordinal_offset(&self) -> u64 { | ||||||
|
@@ -542,7 +576,7 @@ mod tests { | |||||
|
||||||
pretty_assert_eq!( | ||||||
tx_builder.build(), | ||||||
Transaction { | ||||||
Ok(Transaction { | ||||||
version: 1, | ||||||
lock_time: PackedLockTime::ZERO, | ||||||
input: vec![tx_in(outpoint(1)), tx_in(outpoint(2)), tx_in(outpoint(3))], | ||||||
|
@@ -551,7 +585,7 @@ mod tests { | |||||
tx_out(5_000, change(0)), | ||||||
tx_out(1_774, change(1)) | ||||||
], | ||||||
} | ||||||
}) | ||||||
) | ||||||
} | ||||||
|
||||||
|
@@ -685,7 +719,8 @@ mod tests { | |||||
recipient(), | ||||||
vec![change(0), change(1)], | ||||||
) | ||||||
.build(); | ||||||
.build() | ||||||
.unwrap(); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
|
@@ -697,7 +732,8 @@ mod tests { | |||||
recipient(), | ||||||
vec![change(0), change(1)], | ||||||
) | ||||||
.build(); | ||||||
.build() | ||||||
.unwrap(); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
|
@@ -716,7 +752,7 @@ mod tests { | |||||
.parse() | ||||||
.unwrap(); | ||||||
|
||||||
builder.build(); | ||||||
builder.build().unwrap(); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
|
@@ -733,7 +769,7 @@ mod tests { | |||||
|
||||||
builder.outputs[0].1 = Amount::from_sat(0); | ||||||
|
||||||
builder.build(); | ||||||
builder.build().unwrap(); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
|
@@ -775,7 +811,8 @@ mod tests { | |||||
) | ||||||
.select_ordinal() | ||||||
.unwrap() | ||||||
.build(); | ||||||
.build() | ||||||
.unwrap(); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
|
@@ -845,7 +882,7 @@ mod tests { | |||||
|
||||||
builder.change_addresses = BTreeSet::new(); | ||||||
|
||||||
builder.build(); | ||||||
builder.build().unwrap(); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
|
@@ -866,7 +903,8 @@ mod tests { | |||||
.unwrap() | ||||||
.strip_excess_postage() | ||||||
.deduct_fee() | ||||||
.build(); | ||||||
.build() | ||||||
.unwrap(); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
|
@@ -884,7 +922,8 @@ mod tests { | |||||
.unwrap() | ||||||
.strip_excess_postage() | ||||||
.deduct_fee() | ||||||
.build(); | ||||||
.build() | ||||||
.unwrap(); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
|
@@ -901,7 +940,8 @@ mod tests { | |||||
.select_ordinal() | ||||||
.unwrap() | ||||||
.strip_excess_postage() | ||||||
.build(); | ||||||
.build() | ||||||
.unwrap(); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
|
@@ -927,4 +967,34 @@ mod tests { | |||||
}) | ||||||
) | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn rare_ordinals_are_not_sent_to_recipient() { | ||||||
let utxos = vec![(outpoint(1), vec![(15_000, 25_000), (0, 10_000)])]; | ||||||
|
||||||
pretty_assert_eq!( | ||||||
TransactionBuilder::build_transaction( | ||||||
utxos.into_iter().collect(), | ||||||
Ordinal(24_000), | ||||||
recipient(), | ||||||
vec![change(0), change(1),], | ||||||
), | ||||||
Err(Error::AccidentalRareOrdinalSend(Ordinal(0))) | ||||||
) | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn rare_ordinals_are_not_sent_as_fee() { | ||||||
let utxos = vec![(outpoint(1), vec![(15_000, 25_000), (0, 100)])]; | ||||||
|
||||||
pretty_assert_eq!( | ||||||
TransactionBuilder::build_transaction( | ||||||
utxos.into_iter().collect(), | ||||||
Ordinal(24_000), | ||||||
recipient(), | ||||||
vec![change(0), change(1),], | ||||||
), | ||||||
Err(Error::AccidentalRareOrdinalSend(Ordinal(0))) | ||||||
) | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.