Skip to content

Commit

Permalink
Refuse to send additional inscriptions (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Dec 5, 2022
1 parent 87832c6 commit 9678b85
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 51 deletions.
8 changes: 6 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ fn decode_outpoint(array: OutPointArray) -> OutPoint {
Decodable::consensus_decode(&mut io::Cursor::new(array)).unwrap()
}

fn decode_inscription_id(array: InscriptionIdArray) -> InscriptionId {
Decodable::consensus_decode(&mut io::Cursor::new(array)).unwrap()
}

pub(crate) struct Index {
auth: Auth,
chain: Chain,
Expand Down Expand Up @@ -567,14 +571,14 @@ impl Index {
}
}

pub(crate) fn get_inscription_satpoints(&self) -> Result<Vec<SatPoint>> {
pub(crate) fn get_inscriptions(&self) -> Result<BTreeMap<SatPoint, InscriptionId>> {
Ok(
self
.database
.begin_read()?
.open_table(SATPOINT_TO_INSCRIPTION_ID)?
.range([0; 44]..)?
.map(|(satpoint, _id)| decode_satpoint(*satpoint))
.map(|(satpoint, id)| (decode_satpoint(*satpoint), decode_inscription_id(*id)))
.collect(),
)
}
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#![allow(clippy::too_many_arguments, clippy::type_complexity)]
#![allow(
clippy::too_many_arguments,
clippy::type_complexity,
clippy::result_large_err
)]

use {
self::{
Expand Down Expand Up @@ -79,6 +83,8 @@ mod tally;

type Result<T = (), E = Error> = std::result::Result<T, E>;

pub(crate) type InscriptionId = Txid;

const DIFFCHANGE_INTERVAL: u64 = bitcoin::blockdata::constants::DIFFCHANGE_INTERVAL as u64;
const SUBSIDY_HALVING_INTERVAL: u64 =
bitcoin::blockdata::constants::SUBSIDY_HALVING_INTERVAL as u64;
Expand Down
2 changes: 1 addition & 1 deletion src/sat_point.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

#[derive(Debug, PartialEq, Copy, Clone)]
#[derive(Debug, PartialEq, Copy, Clone, Eq, PartialOrd, Ord)]
pub(crate) struct SatPoint {
pub(crate) outpoint: OutPoint,
pub(crate) offset: u64,
Expand Down
16 changes: 8 additions & 8 deletions src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ impl Inscribe {

let utxos = list_utxos(&options)?;

let inscription_satpoints = index.get_inscription_satpoints()?;
let inscriptions = index.get_inscriptions()?;

if inscription_satpoints.contains(&self.satpoint) {
if inscriptions.contains_key(&self.satpoint) {
return Err(anyhow!("sat at {} already inscribed", self.satpoint));
}

Expand All @@ -44,7 +44,7 @@ impl Inscribe {
let (unsigned_commit_tx, reveal_tx) = Inscribe::create_inscription_transactions(
self.satpoint,
inscription,
inscription_satpoints,
inscriptions,
options.chain.network(),
utxos,
commit_tx_change,
Expand All @@ -71,7 +71,7 @@ impl Inscribe {
fn create_inscription_transactions(
satpoint: SatPoint,
inscription: Inscription,
inscription_satpoints: Vec<SatPoint>,
inscriptions: BTreeMap<SatPoint, InscriptionId>,
network: bitcoin::Network,
utxos: BTreeMap<OutPoint, Amount>,
change: Vec<Address>,
Expand Down Expand Up @@ -105,7 +105,7 @@ impl Inscribe {

let unsigned_commit_tx = TransactionBuilder::build_transaction(
satpoint,
inscription_satpoints,
inscriptions,
utxos,
commit_tx_address.clone(),
change,
Expand Down Expand Up @@ -201,7 +201,7 @@ mod tests {
let (commit_tx, reveal_tx) = Inscribe::create_inscription_transactions(
satpoint(1, 0),
inscription,
vec![],
BTreeMap::new(),
bitcoin::Network::Signet,
utxos.into_iter().collect(),
vec![commit_address, change(1)],
Expand All @@ -228,7 +228,7 @@ mod tests {
assert!(Inscribe::create_inscription_transactions(
satpoint,
inscription,
vec![],
BTreeMap::new(),
bitcoin::Network::Signet,
utxos.into_iter().collect(),
vec![commit_address, change(1)],
Expand All @@ -250,7 +250,7 @@ mod tests {
let error = Inscribe::create_inscription_transactions(
satpoint,
inscription,
vec![],
BTreeMap::new(),
bitcoin::Network::Signet,
utxos.into_iter().collect(),
vec![commit_address, change(1)],
Expand Down
11 changes: 3 additions & 8 deletions src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Send {

let utxos = list_utxos(&options)?;

let inscription_satpoints = index.get_inscription_satpoints()?;
let inscriptions = index.get_inscriptions()?;

let change = get_change_addresses(&options, 2)?;

Expand All @@ -53,13 +53,8 @@ impl Send {
},
};

let unsigned_transaction = TransactionBuilder::build_transaction(
satpoint,
inscription_satpoints,
utxos,
self.address,
change,
)?;
let unsigned_transaction =
TransactionBuilder::build_transaction(satpoint, inscriptions, utxos, self.address, change)?;

let signed_tx = client
.sign_raw_transaction_with_wallet(&unsigned_transaction, None, None)?
Expand Down
Loading

0 comments on commit 9678b85

Please sign in to comment.