P2SH

# Create main wallet
bitcoin-cli -regtest createwallet "wallet1"

# Create watch-only wallet
bitcoin-cli -regtest createwallet "watch_only_wallet" true true "" false
bitcoin-cli -rpcwallet=wallet1 settxfee 0.00001 bitcoin-cli -rpcwallet=watch_only_wallet settxfee 0.00001
bitcoin-cli -regtest -rpcwallet=wallet1 generatetoaddress 101 \
    "$(bitcoin-cli -regtest -rpcwallet=wallet1 getnewaddress)"
# First key pair
ADDR1=$(bitcoin-cli -regtest -rpcwallet=wallet1 getnewaddress)
PUBKEY1=$(bitcoin-cli -regtest -rpcwallet=wallet1 getaddressinfo "$ADDR1" | jq -r '.pubkey')

# Second key pair
ADDR2=$(bitcoin-cli -regtest -rpcwallet=wallet1 getnewaddress)
PUBKEY2=$(bitcoin-cli -regtest -rpcwallet=wallet1 getaddressinfo "$ADDR2" | jq -r '.pubkey')

# Third key pair
ADDR3=$(bitcoin-cli -regtest -rpcwallet=wallet1 getnewaddress)
PUBKEY3=$(bitcoin-cli -regtest -rpcwallet=wallet1 getaddressinfo "$ADDR3" | jq -r '.pubkey')
# Create descriptor
DESC_NO_CHECKSUM="sh(multi(2,$PUBKEY1,$PUBKEY2,$PUBKEY3))"
DESC_WITH_CHECKSUM=$(bitcoin-cli -regtest getdescriptorinfo "$DESC_NO_CHECKSUM" | jq -r '.descriptor')

# Import to watch-only wallet
bitcoin-cli -regtest -rpcwallet=watch_only_wallet importdescriptors \
    '[{"desc": "'"$DESC_WITH_CHECKSUM"'", "timestamp": "now", "internal": false, "watchonly": true}]'
# Get P2SH address
P2SH_MULTISIG=$(bitcoin-cli -regtest deriveaddresses "$DESC_WITH_CHECKSUM" | jq -r '.[0]')

# Send 1 BTC to the P2SH address
bitcoin-cli -regtest -rpcwallet=wallet1 sendtoaddress "$P2SH_MULTISIG" 1

# Generate a block to confirm the funding transaction
bitcoin-cli -regtest generatetoaddress 1 \
    "$(bitcoin-cli -regtest -rpcwallet=wallet1 getnewaddress)"
# Create P2SH destination address
DEST_ADDR=$(bitcoin-cli -regtest -rpcwallet=wallet1 getnewaddress "" "p2sh-segwit")

# Create P2SH change address
CHANGE_ADDR=$(bitcoin-cli -regtest -rpcwallet=wallet1 getnewaddress "" "p2sh-segwit")

# Get the UTXO from the watch-only wallet
UTXO=$(bitcoin-cli -regtest -rpcwallet=watch_only_wallet listunspent | jq -r '.[0]')
TXID=$(echo $UTXO | jq -r '.txid')
VOUT=$(echo $UTXO | jq -r '.vout')

# Create the PSBT
PSBT=$(bitcoin-cli -regtest -rpcwallet=watch_only_wallet walletcreatefundedpsbt \
    "[{\"txid\":\"$TXID\",\"vout\":$VOUT}]" \
    "[{\"$DEST_ADDR\":0.9999}]" \
    0 \
    "{\"includeWatching\":true, \"changeAddress\":\"$CHANGE_ADDR\"}" | jq -r '.psbt')
# Sign the PSBT
SIGNED_PSBT=$(bitcoin-cli -regtest -rpcwallet=wallet1 walletprocesspsbt "$PSBT" | jq -r '.psbt')

# Finalize the PSBT
FINAL_TX=$(bitcoin-cli -regtest finalizepsbt "$SIGNED_PSBT" | jq -r '.hex')

# Broadcast the transaction
TXID=$(bitcoin-cli -regtest sendrawtransaction "$FINAL_TX")

# Generate a block to confirm the transaction
bitcoin-cli -regtest generatetoaddress 1 \
    "$(bitcoin-cli -regtest -rpcwallet=wallet1 getnewaddress)"
bitcoin-cli -regtest getrawtransaction "$TXID" true
#check for redeem script in getrawtransaction
bitcoin-cli -regtest getrawtransaction $TXID 1 | jq -r '.vin[].redeemScript.asm
#check redeem script in getblock..2
bitcoin-cli getblock 5b9438aef225b1924603a390a6cebff661379ff9f23715ba200670f3fa2fc100 2 | jq -r '.tx[].vin[].redeemScript.asm'

P2WSH

# Create wallet3
    bitcoin-cli createwallet "wallet3"
    
    # Generate some blocks to get coins
    bitcoin-cli -rpcwallet=wallet3 generatetoaddress 101 $(bitcoin-cli -rpcwallet=wallet3 getnewaddress)
# Generate three key pairs
    KEY1=$(bitcoin-cli -rpcwallet=wallet3 getnewaddress)
    PUBKEY1=$(bitcoin-cli -rpcwallet=wallet3 getaddressinfo $KEY1 | jq -r '.pubkey')
    
    KEY2=$(bitcoin-cli -rpcwallet=wallet3 getnewaddress)
    PUBKEY2=$(bitcoin-cli -rpcwallet=wallet3 getaddressinfo $KEY2 | jq -r '.pubkey')
    
    KEY3=$(bitcoin-cli -rpcwallet=wallet3 getnewaddress)
    PUBKEY3=$(bitcoin-cli -rpcwallet=wallet3 getaddressinfo $KEY3 | jq -r '.pubkey')
# Create 2-of-3 multisig P2WSH address
            WSH_ADDRESS=$(bitcoin-cli -rpcwallet=wallet3 createmultisig 2 '''[
            "'$PUBKEY1'",
            "'$PUBKEY2'",
            "'$PUBKEY3'"
            ]''' "bech32")
            
# Send 1 BTC to the P2WSH address
    TXID=$(bitcoin-cli -rpcwallet=wallet3 sendtoaddress $(echo $WSH_ADDRESS | jq -r '.address') 1.0)
    
    # Generate a block to confirm the transaction
    bitcoin-cli -rpcwallet=wallet3 generatetoaddress 1 $(bitcoin-cli -rpcwallet=wallet3 getnewaddress)
# Create raw transaction spending from P2WSH
    RAWTX=$(bitcoin-cli -rpcwallet=wallet3 createrawtransaction \
    '[{"txid":"'$TXID'","vout":0}]' \
    '{"'$(bitcoin-cli -rpcwallet=wallet3 getnewaddress)'":49.0000}')
    
    # Sign the transaction
    SIGNEDTX=$(bitcoin-cli -rpcwallet=wallet3 signrawtransactionwithwallet $RAWTX)
    
    # Broadcast the transaction
    SPENT_TXID=$(bitcoin-cli -rpcwallet=wallet3 sendrawtransaction $(echo $SIGNEDTX | jq -r '.hex'))
bitcoin-cli -rpcwallet=wallet3 generatetoaddress 1 $(bitcoin-cli -rpcwallet=wallet3 getnewaddress)
# Get full transaction details with witness data
    bitcoin-cli getrawtransaction $SPENT_TXID true
    
    # View the witness stack (includes witnessScript)
    bitcoin-cli getrawtransaction $SPENT_TXID true | jq -r '.vin[].txinwitness[]'
Contact