Skip to content

Commit

Permalink
Introduce a new version of commit_tx builder with fixed fees for test…
Browse files Browse the repository at this point in the history
…s and examples
  • Loading branch information
kobby-pentangeli committed Mar 28, 2024
1 parent f7d1bdf commit a544a09
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 125 deletions.
6 changes: 3 additions & 3 deletions examples/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use argh::FromArgs;
use bitcoin::secp256k1::Secp256k1;
use bitcoin::{Address, Network, PrivateKey};
use log::{debug, info};
use ord_rs::wallet::{CreateCommitTransactionArgs, RevealTransactionArgs};
use ord_rs::wallet::{CreateCommitTransactionArgsV2, RevealTransactionArgs};
use ord_rs::{Brc20, OrdTransactionBuilder};
use utils::rpc_client;

Expand Down Expand Up @@ -84,9 +84,9 @@ async fn main() -> anyhow::Result<()> {
};

let commit_tx = builder
.build_commit_transaction(
.build_commit_transaction_with_fixed_fees(
network,
CreateCommitTransactionArgs {
CreateCommitTransactionArgsV2 {
inputs,
inscription: Brc20::deploy(ticker, amount, Some(limit), None),
txin_script_pubkey: sender_address.script_pubkey(),
Expand Down
6 changes: 3 additions & 3 deletions examples/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use argh::FromArgs;
use bitcoin::secp256k1::Secp256k1;
use bitcoin::{Address, Network, PrivateKey};
use log::{debug, info};
use ord_rs::wallet::{CreateCommitTransactionArgs, RevealTransactionArgs};
use ord_rs::wallet::{CreateCommitTransactionArgsV2, RevealTransactionArgs};
use ord_rs::{Brc20, OrdTransactionBuilder};

use self::utils::rpc_client;
Expand Down Expand Up @@ -79,9 +79,9 @@ async fn main() -> anyhow::Result<()> {
};

let commit_tx = builder
.build_commit_transaction(
.build_commit_transaction_with_fixed_fees(
network,
CreateCommitTransactionArgs {
CreateCommitTransactionArgsV2 {
inputs,
inscription: Brc20::mint(ticker, amount),
txin_script_pubkey: sender_address.script_pubkey(),
Expand Down
6 changes: 3 additions & 3 deletions examples/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use argh::FromArgs;
use bitcoin::secp256k1::Secp256k1;
use bitcoin::{Address, Network, PrivateKey};
use log::{debug, info};
use ord_rs::wallet::{CreateCommitTransactionArgs, RevealTransactionArgs};
use ord_rs::wallet::{CreateCommitTransactionArgsV2, RevealTransactionArgs};
use ord_rs::{Brc20, OrdTransactionBuilder};

use self::utils::rpc_client;
Expand Down Expand Up @@ -78,9 +78,9 @@ async fn main() -> anyhow::Result<()> {
};

let commit_tx = builder
.build_commit_transaction(
.build_commit_transaction_with_fixed_fees(
network,
CreateCommitTransactionArgs {
CreateCommitTransactionArgsV2 {
inputs,
inscription: Brc20::transfer(ticker, amount),
txin_script_pubkey: sender_address.script_pubkey(),
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub use inscription::Inscription;
pub use result::OrdResult;
pub use utils::fees::MultisigConfig;
pub use wallet::{
CreateCommitTransaction, CreateCommitTransactionArgs, CreateCommitTransactionArgsV2,
ExternalSigner, OrdParser, OrdTransactionBuilder, RevealTransactionArgs, Utxo, Wallet,
WalletType,
CreateCommitTransaction, CreateCommitTransactionArgs, ExternalSigner, OrdParser,
OrdTransactionBuilder, RevealTransactionArgs, Utxo, Wallet, WalletType,
};
226 changes: 113 additions & 113 deletions src/wallet/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,6 @@ pub struct OrdTransactionBuilder {
#[derive(Debug)]
/// Arguments for creating a commit transaction
pub struct CreateCommitTransactionArgs<T>
where
T: Inscription,
{
/// UTXOs to be used as inputs of the transaction
pub inputs: Vec<Utxo>,
/// Inscription to write
pub inscription: T,
/// Address to send the leftovers BTC of the trasnsaction
pub leftovers_recipient: Address,
/// Fee to pay for the commit transaction
pub commit_fee: Amount,
/// Fee to pay for the reveal transaction
pub reveal_fee: Amount,
/// Script pubkey of the inputs
pub txin_script_pubkey: ScriptBuf,
}

#[derive(Debug)]
/// Arguments for creating a commit transaction
pub struct CreateCommitTransactionArgsV2<T>
where
T: Inscription,
{
Expand Down Expand Up @@ -111,11 +91,11 @@ impl OrdTransactionBuilder {
}

/// Creates the commit transaction.
pub async fn build_commit_transaction_v2<T>(
pub async fn build_commit_transaction<T>(
&mut self,
network: Network,
recipient_address: Address,
args: CreateCommitTransactionArgsV2<T>,
args: CreateCommitTransactionArgs<T>,
) -> OrdResult<CreateCommitTransaction>
where
T: Inscription,
Expand Down Expand Up @@ -248,11 +228,96 @@ impl OrdTransactionBuilder {
})
}

/// Creates the commit transaction.
pub async fn build_commit_transaction<T>(
/// Create the reveal transaction
pub async fn build_reveal_transaction(
&mut self,
args: RevealTransactionArgs,
) -> OrdResult<Transaction> {
// previous output
let previous_output = OutPoint {
txid: args.input.id,
vout: args.input.index,
};
// tx out
let tx_out = vec![TxOut {
value: Amount::from_sat(POSTAGE),
script_pubkey: args.recipient_address.script_pubkey(),
}];
// txin
let tx_in = vec![TxIn {
previous_output,
script_sig: ScriptBuf::new(),
sequence: Sequence::from_consensus(0xffffffff),
witness: Witness::new(),
}];

// make transaction and sign it
let unsigned_tx = Transaction {
version: Version::TWO,
lock_time: LockTime::ZERO,
input: tx_in,
output: tx_out,
};

let tx = match self.taproot_payload.as_ref() {
Some(taproot_payload) => self.signer.sign_reveal_transaction_schnorr(
taproot_payload,
&args.redeem_script,
unsigned_tx,
),
None => {
self.signer
.sign_reveal_transaction_ecdsa(
&self.public_key,
&args.input,
unsigned_tx,
&args.redeem_script,
)
.await
}
}?;

Ok(tx)
}

/// Generate redeem script from script pubkey and inscription
fn generate_redeem_script<T>(
&self,
inscription: &T,
pubkey: RedeemScriptPubkey,
) -> OrdResult<ScriptBuf>
where
T: Inscription,
{
Ok(inscription
.generate_redeem_script(ScriptBuilder::new(), pubkey)?
.into_script())
}

/// Initialize a new `OrdTransactionBuilder` with the given private key and use P2TR as script type (preferred).
pub fn p2tr(private_key: bitcoin::PrivateKey) -> Self {
use signer::WalletType;

let public_key = private_key.public_key(&secp256k1::Secp256k1::new());
let wallet = Wallet::new_with_signer(WalletType::Local { private_key });
Self::new(public_key, ScriptType::P2TR, wallet)
}

/// Initialize a new `OrdTransactionBuilder` with the given private key and use P2WSH as script type.
/// P2WSH may not be supported by all the indexers, so P2TR should be preferred.
pub fn p2wsh(private_key: bitcoin::PrivateKey) -> Self {
use signer::WalletType;

let public_key = private_key.public_key(&secp256k1::Secp256k1::new());
let wallet = Wallet::new_with_signer(WalletType::Local { private_key });
Self::new(public_key, ScriptType::P2WSH, wallet)
}

/// Creates the commit transaction with predetermined commit and reveal fees.
pub async fn build_commit_transaction_with_fixed_fees<T>(
&mut self,
network: Network,
args: CreateCommitTransactionArgs<T>,
args: CreateCommitTransactionArgsV2<T>,
) -> OrdResult<CreateCommitTransaction>
where
T: Inscription,
Expand Down Expand Up @@ -360,91 +425,26 @@ impl OrdTransactionBuilder {
reveal_balance: Amount::from_sat(reveal_balance),
})
}
}

/// Create the reveal transaction
pub async fn build_reveal_transaction(
&mut self,
args: RevealTransactionArgs,
) -> OrdResult<Transaction> {
// previous output
let previous_output = OutPoint {
txid: args.input.id,
vout: args.input.index,
};
// tx out
let tx_out = vec![TxOut {
value: Amount::from_sat(POSTAGE),
script_pubkey: args.recipient_address.script_pubkey(),
}];
// txin
let tx_in = vec![TxIn {
previous_output,
script_sig: ScriptBuf::new(),
sequence: Sequence::from_consensus(0xffffffff),
witness: Witness::new(),
}];

// make transaction and sign it
let unsigned_tx = Transaction {
version: Version::TWO,
lock_time: LockTime::ZERO,
input: tx_in,
output: tx_out,
};

let tx = match self.taproot_payload.as_ref() {
Some(taproot_payload) => self.signer.sign_reveal_transaction_schnorr(
taproot_payload,
&args.redeem_script,
unsigned_tx,
),
None => {
self.signer
.sign_reveal_transaction_ecdsa(
&self.public_key,
&args.input,
unsigned_tx,
&args.redeem_script,
)
.await
}
}?;

Ok(tx)
}

/// Generate redeem script from script pubkey and inscription
fn generate_redeem_script<T>(
&self,
inscription: &T,
pubkey: RedeemScriptPubkey,
) -> OrdResult<ScriptBuf>
where
T: Inscription,
{
Ok(inscription
.generate_redeem_script(ScriptBuilder::new(), pubkey)?
.into_script())
}

/// Initialize a new `OrdTransactionBuilder` with the given private key and use P2TR as script type (preferred).
pub fn p2tr(private_key: bitcoin::PrivateKey) -> Self {
use signer::WalletType;

let public_key = private_key.public_key(&secp256k1::Secp256k1::new());
let wallet = Wallet::new_with_signer(WalletType::Local { private_key });
Self::new(public_key, ScriptType::P2TR, wallet)
}

/// Initialize a new `OrdTransactionBuilder` with the given private key and use P2WSH as script type.
/// P2WSH may not be supported by all the indexers, so P2TR should be preferred.
pub fn p2wsh(private_key: bitcoin::PrivateKey) -> Self {
use signer::WalletType;

let public_key = private_key.public_key(&secp256k1::Secp256k1::new());
let wallet = Wallet::new_with_signer(WalletType::Local { private_key });
Self::new(public_key, ScriptType::P2WSH, wallet)
}
#[derive(Debug)]
/// Arguments for creating a commit transaction
pub struct CreateCommitTransactionArgsV2<T>
where
T: Inscription,
{
/// UTXOs to be used as inputs of the transaction
pub inputs: Vec<Utxo>,
/// Inscription to write
pub inscription: T,
/// Address to send the leftovers BTC of the trasnsaction
pub leftovers_recipient: Address,
/// Fee to pay for the commit transaction
pub commit_fee: Amount,
/// Fee to pay for the reveal transaction
pub reveal_fee: Amount,
/// Script pubkey of the inputs
pub txin_script_pubkey: ScriptBuf,
}

/// Unspent transaction output to be used as input of a transaction
Expand Down Expand Up @@ -481,7 +481,7 @@ mod test {

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

let commit_transaction_args = CreateCommitTransactionArgs {
let commit_transaction_args = CreateCommitTransactionArgsV2 {
inputs: vec![Utxo {
id: Txid::from_str(
"791b415dc6946d864d368a0e5ec5c09ee2ad39cf298bc6e3f9aec293732cfda7",
Expand All @@ -497,7 +497,7 @@ mod test {
reveal_fee: Amount::from_sat(4_700),
};
let tx_result = builder
.build_commit_transaction(Network::Testnet, commit_transaction_args)
.build_commit_transaction_with_fixed_fees(Network::Testnet, commit_transaction_args)
.await
.unwrap();

Expand Down Expand Up @@ -585,7 +585,7 @@ mod test {

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

let commit_transaction_args = CreateCommitTransactionArgs {
let commit_transaction_args = CreateCommitTransactionArgsV2 {
inputs: vec![Utxo {
id: Txid::from_str(
"791b415dc6946d864d368a0e5ec5c09ee2ad39cf298bc6e3f9aec293732cfda7",
Expand All @@ -601,7 +601,7 @@ mod test {
reveal_fee: Amount::from_sat(4_700),
};
let tx_result = builder
.build_commit_transaction(Network::Testnet, commit_transaction_args)
.build_commit_transaction_with_fixed_fees(Network::Testnet, commit_transaction_args)
.await
.unwrap();

Expand Down

0 comments on commit a544a09

Please sign in to comment.