Skip to content

btc-vision/opnet

Repository files navigation

OPNet - Smart Contracts on Bitcoin L1

Bitcoin TypeScript NodeJS NPM

code style: prettier

The official client library for building Bitcoin-based applications on OPNet. Full TypeScript support with type-safe contract interactions.

Installation

npm install opnet @btc-vision/transaction @btc-vision/bitcoin

Documentation

Check out the full documentation in /docs!

RPC Endpoints

Network URL
Mainnet https://mainnet.opnet.org
Regtest https://regtest.opnet.org

Quick Start

import { BitcoinUtils, getContract, IOP20Contract, JSONRpcProvider, OP_20_ABI } from 'opnet';
import { AddressTypes, Mnemonic, MLDSASecurityLevel } from '@btc-vision/transaction';
import { networks } from '@btc-vision/bitcoin';

// Connect to OPNet
const provider = new JSONRpcProvider('https://regtest.opnet.org', networks.regtest);

// Create wallet from mnemonic
const mnemonic = new Mnemonic(
    'your twenty four word seed phrase goes here ...',
    '',
    networks.regtest,
    MLDSASecurityLevel.LEVEL2,
);
const wallet = mnemonic.deriveUnisat(AddressTypes.P2TR, 0);
const myAddress = wallet.address;

// Interact with a token contract
const token = getContract<IOP20Contract>(
    'op1...', // contract address
    OP_20_ABI,
    provider,
    networks.regtest,
    myAddress
);

// Read token info
const name = await token.name();
const balance = await token.balanceOf(myAddress);

console.log('Token:', name.properties.name);
console.log('Balance:', balance.properties.balance);

// Send a transaction (100 tokens with 8 decimals)
const amount = BitcoinUtils.expandToDecimals(100, 8);
const simulation = await token.transfer(recipientAddress, amount);

if (simulation.revert) {
    throw new Error(`Transfer would fail: ${simulation.revert}`);
}

const tx = await simulation.sendTransaction({
    signer: wallet.keypair,
    mldsaSigner: wallet.mldsaKeypair,
    refundTo: wallet.p2tr,
    maximumAllowedSatToSpend: 50000n,
    feeRate: 10,
    network: networks.regtest,
});

console.log('TX ID:', tx.transactionId);

Requirements

  • Node.js >= 24.0.0
  • TypeScript >= 5.9

Development

git clone https://github.com/btc-vision/opnet.git
cd opnet
npm install
npm run build

Links

Contributing

Contributions welcome! See CONTRIBUTING.md for guidelines.

License

Apache-2.0

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 7