Sunday, December 22, 2024

taproot – P2tr transaction with musig2 rust

I am attempting to construct a p2tr that spends a 2-2 musig aggregated deal with. My course of is as observe:

  1. Create SecretKey, PublicKey pair from every get together
  2. Mixture these 2 public keys
  3. Create and publish a transaction to ship deposit cash to that aggregated deal with (Tx0)
  4. Utilizing musig2 to aggregated a sound signature to spend that deal with
  5. Create a p2tr transaction to spend that deal with with the legitimate signature (Tx1)

Each time I publish Tx1 to the blocktream, it at all times returns the Invalid Schnoor Signature error.

Within the context of making the keypair in step 1, I used this code :

 let owner_keypair = Keypair::new(&secp, &mut rand::thread_rng());
 let owner_seckey = SecretKey::from_keypair(&owner_keypair);
 let owner_pubkey = PublicKey::from_keypair(&owner_keypair);

Do we have to tweak these keys earlier than the aggregation step, since I do know that the legitimate pubkey for BIP340 is an even-y-coordinate pubkey.

FYI I’ve this operate to create the aggregated keys :

pub fn aggregate_pubkeys(
    owner_pubkey: PublicKey,
    se_pubkey: PublicKey,
) -> (PublicKey, PublicKey, Deal with, KeyAggContext) {
    let secp = Secp256k1::new();
    let mut pubkeys: Vec<PublicKey> = vec![];
    pubkeys.push(owner_pubkey);
    pubkeys.push(se_pubkey);
    let key_agg_ctx_tw = KeyAggContext::new(pubkeys.clone())
        .unwrap()
        .with_unspendable_taproot_tweak()
        .unwrap();

    let aggregated_pubkey: PublicKey = key_agg_ctx_tw.aggregated_pubkey_untweaked();
    let aggregated_pubkey_tw: PublicKey = key_agg_ctx_tw.aggregated_pubkey();

    let aggregated_address = Deal with::p2tr(
        &secp,
        aggregated_pubkey.x_only_public_key().0,
        None,
        Community::Testnet,
    );

    (
        aggregated_pubkey,
        aggregated_pubkey_tw,
        aggregated_address,
        key_agg_ctx_tw,
    )
}

The aggregated_pubkey is used to create the scriptpubkey for Tx0 and aggregated_pubkey_tw is used for the musig2 course of.
Thank you numerous for serving to me.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles