Skip to main content

Importing and Creating a Wallet

Managing wallets is a core feature of interacting with the OP_NET metaprotocol. Whether you're importing an existing wallet or creating a new one, this guide walks you through both processes.


Importing a Wallet

The Wallet class from @btc-vision/transaction provides a convenient method for importing a wallet using a WIF (Wallet Import Format) private key. This method is straightforward and ideal for users who already have their private keys.

Using Wallet.fromWif

The fromWif method allows you to import a wallet into the OP_NET ecosystem.

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

const network = networks.regtest; // or networks.testnet, networks.mainnet

// Import a wallet using a WIF private key
const wallet = Wallet.fromWif("your-private-key-in-wif-format", network);

console.log("Wallet:", wallet);

Creating a Wallet (Example)

While OP_NET doesn't natively provide a method for creating wallets, you can use libraries like @btc-vision/bitcoin, bip32, and bip39 to create a wallet compatible with the OP_NET ecosystem. Here's an example implementation of a WalletManager class:

WalletManager Class

The WalletManager class below demonstrates how to create and import wallets using mnemonic phrases and standard derivation paths.

import * as bitcoinjs from "@btc-vision/bitcoin";
import { BIP32API, BIP32Factory } from "bip32";
import { generateMnemonic, mnemonicToSeedSync, validateMnemonic } from "bip39";
import * as ecc from "tiny-secp256k1";

export class WalletManager {
private bip32: BIP32API;

constructor() {
this.bip32 = BIP32Factory(ecc);
bitcoinjs.initEccLib(ecc);
}

// Create a new wallet
createWallet(network: bitcoinjs.Network) {
const mnemonic = generateMnemonic(256);
const seed = mnemonicToSeedSync(mnemonic);

const root = this.bip32.fromSeed(seed, network);
const path = "m/86'/0'/0'/0/0";
const child = root.derivePath(path);

const { address } = bitcoinjs.payments.p2tr({
internalPubkey: Buffer.from(child.publicKey.slice(1, 33)),
network,
});

return {
mnemonic,
privateKey: child.toWIF(),
address,
};
}

// Import an existing wallet using a mnemonic
importWallet(mnemonic: string, network: bitcoinjs.Network) {
if (!validateMnemonic(mnemonic)) return null;

const seed = mnemonicToSeedSync(mnemonic);

const root = this.bip32.fromSeed(seed, network);
const path = "m/86'/0'/0'/0/0";
const child = root.derivePath(path);

const { address } = bitcoinjs.payments.p2tr({
internalPubkey: Buffer.from(child.publicKey.slice(1, 33)),
network,
});

return {
mnemonic,
privateKey: child.toWIF(),
address,
};
}
}

Example Usage

Creating a Wallet

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

const walletManager = new WalletManager();
const network = networks.regtest; // or networks.testnet, networks.mainnet

const newWallet = walletManager.createWallet(network);
console.log("Mnemonic:", newWallet.mnemonic);
console.log("Address:", newWallet.address);
console.log("Private Key:", newWallet.privateKey);

Importing a Wallet

const mnemonic = "your-existing-mnemonic-phrase";
const importedWallet = walletManager.importWallet(mnemonic, network);

if (importedWallet) {
console.log("Imported Address:", importedWallet.address);
} else {
console.error("Invalid mnemonic phrase.");
}

Best Practices

  • Never share your private keys or mnemonics. Store them securely to prevent unauthorized access.
  • Always generate mnemonics with sufficient entropy (e.g., 24 words) for maximum security.
  • Ensure the network (e.g., regtest, mainnet) matches your intended usage to avoid transaction failures.
  • Always validate mnemonics before importing wallets to prevent errors.