Skip to content

Commit

Permalink
fix: separate method for etching
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Jul 31, 2024
1 parent 184f02a commit 237bcf3
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 173 deletions.
2 changes: 0 additions & 2 deletions examples/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ async fn main() -> anyhow::Result<()> {
},
recipient_address: sender_address,
redeem_script: commit_tx.redeem_script,
#[cfg(feature = "rune")]
runestone: None,
})
.await?;
debug!("reveal transaction: {reveal_transaction:?}");
Expand Down
7 changes: 3 additions & 4 deletions examples/etch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bitcoin::secp256k1::Secp256k1;
use bitcoin::{Address, Network, PrivateKey};
use log::{debug, info};
use ord_rs::wallet::{
CreateCommitTransactionArgsV2, RevealTransactionArgs, Runestone, SignCommitTransactionArgs,
CreateCommitTransactionArgsV2, EtchingTransactionArgs, Runestone, SignCommitTransactionArgs,
};
use ord_rs::{Nft, OrdTransactionBuilder};
use ordinals::{Etching, Rune, Terms};
Expand Down Expand Up @@ -158,16 +158,15 @@ async fn main() -> anyhow::Result<()> {

debug!("getting reveal transaction...");
let reveal_transaction = builder
.build_reveal_transaction(RevealTransactionArgs {
.build_etching_transaction(EtchingTransactionArgs {
input: ord_rs::wallet::Utxo {
id: commit_txid,
index: 0,
amount: commit_tx.reveal_balance,
},
recipient_address: sender_address,
redeem_script: commit_tx.redeem_script,
#[cfg(feature = "rune")]
runestone: Some(runestone),
runestone,
})
.await?;
debug!("reveal transaction: {reveal_transaction:?}");
Expand Down
2 changes: 0 additions & 2 deletions examples/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ async fn main() -> anyhow::Result<()> {
},
recipient_address: sender_address,
redeem_script: commit_tx.redeem_script,
#[cfg(feature = "rune")]
runestone: None,
})
.await?;
debug!("reveal transaction: {reveal_transaction:?}");
Expand Down
2 changes: 0 additions & 2 deletions examples/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ async fn main() -> anyhow::Result<()> {
},
recipient_address: sender_address, // NOTE: it's correct, see README.md to read about how transfer works
redeem_script: commit_tx.redeem_script,
#[cfg(feature = "rune")]
runestone: None,
})
.await?;
debug!("reveal transaction: {reveal_transaction:?}");
Expand Down
2 changes: 1 addition & 1 deletion src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ pub use builder::{
SignCommitTransactionArgs, TaprootPayload, TxInputInfo, Utxo,
};
#[cfg(feature = "rune")]
pub use builder::{CreateEdictTxArgs, Runestone};
pub use builder::{CreateEdictTxArgs, EtchingTransactionArgs, Runestone};
pub use parser::OrdParser;
153 changes: 1 addition & 152 deletions src/wallet/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use bitcoin::{
secp256k1, Address, Amount, FeeRate, Network, OutPoint, PublicKey, ScriptBuf, Sequence,
Transaction, TxIn, TxOut, Txid, Witness, XOnlyPublicKey,
};
#[cfg(feature = "rune")]
use ordinals::Runestone as OrdRunestone;
use signer::Wallet;

use self::taproot::generate_keypair;
Expand All @@ -21,7 +19,7 @@ use crate::{OrdError, OrdResult};
#[cfg(feature = "rune")]
mod rune;
#[cfg(feature = "rune")]
pub use rune::{CreateEdictTxArgs, Runestone};
pub use rune::{CreateEdictTxArgs, EtchingTransactionArgs, Runestone};

use crate::wallet::builder::signer::LocalSigner;

Expand Down Expand Up @@ -91,9 +89,6 @@ pub struct RevealTransactionArgs {
pub recipient_address: Address,
/// The redeem script returned by `create_commit_transaction`
pub redeem_script: ScriptBuf,
#[cfg(feature = "rune")]
/// Optional runestone to append to the tx outputs
pub runestone: Option<Runestone>,
}

/// Type of the script to use. Both are supported, but P2WSH may not be supported by all the indexers
Expand Down Expand Up @@ -323,32 +318,11 @@ impl OrdTransactionBuilder {
};

// tx out
#[cfg(feature = "rune")]
let mut tx_out = vec![TxOut {
value: Amount::from_sat(POSTAGE),
script_pubkey: args.recipient_address.script_pubkey(),
}];
#[cfg(not(feature = "rune"))]
let tx_out = vec![TxOut {
value: Amount::from_sat(POSTAGE),
script_pubkey: args.recipient_address.script_pubkey(),
}];

#[cfg(feature = "rune")]
if let Some(runestone) = args.runestone {
let runestone = OrdRunestone::from(runestone);
// ! Step required to encode the runestone because Bitcoin version mismatches
let btc_030_script = runestone.encipher();
let btc_031_script = ScriptBuf::from_bytes(btc_030_script.to_bytes());
tx_out.push(TxOut {
value: Amount::from_sat(POSTAGE),
script_pubkey: args.recipient_address.script_pubkey(),
});
tx_out.push(TxOut {
value: Amount::from_sat(0),
script_pubkey: btc_031_script,
});
}
// txin
let tx_in = vec![TxIn {
previous_output,
Expand Down Expand Up @@ -668,8 +642,6 @@ mod test {
},
recipient_address: recipient_address.clone(),
redeem_script: tx_result.redeem_script,
#[cfg(feature = "rune")]
runestone: None,
})
.await
.unwrap();
Expand Down Expand Up @@ -773,134 +745,11 @@ mod test {
},
recipient_address: recipient_address.clone(),
redeem_script: tx_result.redeem_script,
#[cfg(feature = "rune")]
runestone: None,
})
.await
.unwrap();

let witness = reveal_transaction.input[0].witness.clone().to_vec();
assert_eq!(witness.len(), 3);
}

#[tokio::test]
#[cfg(feature = "rune")]
async fn test_should_append_runestone() {
// this test refers to these testnet transactions, commit and reveal:
// <https://mempool.space/testnet/tx/973f78eb7b3cc666dc4133ff6381c363fd29edda0560d36ea3cfd31f1e85d9f9>
// <https://mempool.space/testnet/tx/a35802655b63f1c99c1fd3ff8fdf3415f3abb735d647d402c0af5e9a73cbe4c6>
// made by address tb1qzc8dhpkg5e4t6xyn4zmexxljc4nkje59dg3ark

use ordinals::{Etching, Rune, Terms};
let private_key = PrivateKey::from_wif(WIF).unwrap();
let public_key = private_key.public_key(&Secp256k1::new());
let address = Address::p2wpkh(&public_key, Network::Testnet).unwrap();

let mut builder = OrdTransactionBuilder::p2tr(private_key);

let inputs = vec![Utxo {
id: Txid::from_str("791b415dc6946d864d368a0e5ec5c09ee2ad39cf298bc6e3f9aec293732cfda7")
.unwrap(), // the transaction that funded our wallet
index: 1,
amount: Amount::from_sat(8_000),
}];
let commit_transaction_args = CreateCommitTransactionArgsV2 {
inputs: inputs.clone(),
txin_script_pubkey: address.script_pubkey(),
inscription: Brc20::transfer("mona".to_string(), 100),
leftovers_recipient: address.clone(),
commit_fee: Amount::from_sat(2_500),
reveal_fee: Amount::from_sat(4_700),
};
let tx_result = builder
.build_commit_transaction_with_fixed_fees(Network::Testnet, commit_transaction_args)
.unwrap();

assert!(builder.taproot_payload.is_some());

// sign
let sign_args = SignCommitTransactionArgs {
inputs,
txin_script_pubkey: address.script_pubkey(),
};
let tx = builder
.sign_commit_transaction(tx_result.unsigned_tx, sign_args)
.await
.unwrap();

let witness = tx.input[0].witness.clone().to_vec();
assert_eq!(witness.len(), 2);
assert_eq!(
witness[1],
hex!("02d1c2aebced475b0c672beb0336baa775a44141263ee82051b5e57ad0f2248240")
);

let encoded_pubkey = builder
.taproot_payload
.as_ref()
.unwrap()
.keypair
.public_key()
.serialize();
println!("{} {}", encoded_pubkey.len(), hex::encode(encoded_pubkey));

// check redeem script contains pubkey for taproot
let redeem_script = &tx_result.redeem_script;
assert_eq!(
redeem_script.as_bytes()[0],
bitcoin::opcodes::all::OP_PUSHBYTES_32.to_u8()
);

let tx_id = tx.txid();
let recipient_address = Address::from_str("tb1qax89amll2uas5k92tmuc8rdccmqddqw94vrr86")
.unwrap()
.require_network(Network::Testnet)
.unwrap();

let etching = Etching {
rune: Some(Rune::from_str("SUPERMAXRUNENAME").unwrap()),
divisibility: Some(2),
premine: Some(10_000),
spacers: None,
symbol: Some('$'),
terms: Some(Terms {
amount: Some(2000),
cap: Some(500),
height: (None, None),
offset: (None, None),
}),
turbo: true,
};
let runestone = Runestone {
etching: Some(etching),
edicts: vec![],
mint: None,
pointer: None,
};

let expected_script_030 = OrdRunestone::from(runestone.clone()).encipher();
let expected_script = ScriptBuf::from_bytes(expected_script_030.to_bytes());

let reveal_transaction = builder
.build_reveal_transaction(RevealTransactionArgs {
input: Utxo {
id: tx_id,
index: 0,
amount: tx_result.reveal_balance,
},
recipient_address: recipient_address.clone(),
redeem_script: tx_result.redeem_script,
runestone: Some(Runestone {
edicts: vec![],
etching: Some(runestone.etching.unwrap()),
mint: None,
pointer: None,
}),
})
.await
.unwrap();

assert_eq!(reveal_transaction.output.len(), 3);
assert_eq!(reveal_transaction.output[2].script_pubkey, expected_script);
}
}
Loading

0 comments on commit 237bcf3

Please sign in to comment.