Skip to main content

Retrieving a Wallet's Tweaked Public Key

On OP_NET, wallets are associated with Tweaked Public Keys, which are essential for interacting with the metaprotocol. The Wallet class from @btc-vision/transaction provides a straightforward way to retrieve the tweaked public key after importing a wallet.


Understanding the Tweaked Public Key

The Tweaked Public Key is a modified version of the wallet’s public key, used to interact with smart contracts and OP_NET features securely. This key is automatically derived from the wallet instance.

Key Features of the Tweaked Public Key
  • Security: Ensures compatibility with Bitcoin’s Taproot addresses.
  • Uniqueness: Each tweaked key is unique to the specific wallet.
  • Integration: Used in contract interactions, address generation, and transaction signing.
tip

Learn more about Unified Accounts and how public keys are unified across different address types on OP_NET.


Retrieving the Tweaked Public Key

To retrieve the tweaked public key from a wallet instance, use the tweakedPubKeyKey property of the Wallet class.

Step 1: Importing the Wallet

First, import your wallet using the fromWif method:

import { Wallet } from "@btc-vision/transaction";
import { networks } from "@btc-vision/bitcoin";

// Import a wallet using WIF (Wallet Import Format)
const network = networks.regtest; // or networks.testnet, networks.mainnet
const wallet = Wallet.fromWif("your-wif-private-key", network);

Step 2: Accessing the Tweaked Public Key

After importing the wallet, retrieve the tweaked public key:

const tweakedPublicKey = wallet.tweakedPubKeyKey;

console.log("Tweaked Public Key:", tweakedPublicKey.toString("hex"));

Additional Wallet Information

The Wallet class also provides access to other useful information about the wallet:

  • Taproot Address (p2tr):
    Retrieve the Taproot address associated with the wallet.

    console.log("Taproot Address:", wallet.p2tr);
  • SegWit Address (p2wpkh):
    Get the SegWit address for compatibility with legacy systems.

    console.log("SegWit Address:", wallet.p2wpkh);
  • Public Key (publicKey):
    Access the raw public key of the wallet.

    console.log("Public Key:", wallet.publicKey.toString("hex"));

Best Practices

  • Always store your private keys securely to prevent unauthorized access.
  • Ensure the network (e.g., regtest, mainnet) matches the intended use case to avoid errors.