Skip to content

XDeFi-tech/wallet-standard

Repository files navigation

Wallet Standard - Bitcoin Implementation

This package provides a wallet-standard compliant Bitcoin wallet implementation that can be integrated with any dApp supporting the wallet-standard interface.

Features

  • Wallet Standard Compliance: Implements the official wallet-standard specification
  • Bitcoin Support: Full support for Bitcoin mainnet and testnet
  • Multiple Features: Sign messages, sign transactions, sign PSBTs, and more
  • Event Handling: Proper event emission for wallet state changes
  • TypeScript Support: Full TypeScript definitions included

Installation

npm install @xdefi-tech/wallet-standard

Usage

Basic Setup

import { initialize } from '@xdefi-tech/wallet-standard';
import { ExampleBitcoinProvider } from './your-bitcoin-provider';

// Create your Bitcoin provider (must implement ICtrlBitcoinWallet)
const bitcoinProvider = new ExampleBitcoinProvider();

// Initialize the wallet with wallet-standard
initialize(bitcoinProvider);

Bitcoin Provider Interface

Your Bitcoin provider must implement the ICtrlBitcoinWallet interface:

interface ICtrlBitcoinWallet {
    // Event emitter methods
    on(event: string, listener: any, context?: any): void;
    off(event: string, listener: any, context?: any): void;

    // Core methods
    request(payload: XDEFIBitcoinPayload | string, callbackFunction: any): Promise<any>;
    getAccounts(): Promise<string[]>;
    requestAccounts(): Promise<string[]>;
    signPsbt(params: { psbt: string; broadcast?: boolean }): Promise<any>;
}

Supported Features

The wallet implements the following wallet-standard features:

  • standard:connect - Connect to the wallet
  • standard:disconnect - Disconnect from the wallet
  • standard:events - Listen to wallet events
  • bitcoin:signMessage - Sign Bitcoin messages
  • bitcoin:signTransaction - Sign Bitcoin transactions
  • bitcoin:signPsbt - Sign Bitcoin PSBTs
  • bitcoin:connect - Bitcoin-specific connection

Supported Chains

  • bitcoin:mainnet - Bitcoin mainnet

dApp Integration

dApps can discover and use the wallet through the wallet-standard interface:

// Get available wallets
const wallets = await window.navigator.wallets?.get();

// Find Bitcoin wallet
const bitcoinWallet = wallets.find((wallet) => wallet.chains.some((chain) => chain.startsWith('bitcoin:')));

if (bitcoinWallet) {
    // Connect to wallet
    const connectFeature = bitcoinWallet.features['standard:connect'];
    const { accounts } = await connectFeature.connect();

    // Sign a message
    const signMessageFeature = bitcoinWallet.features['bitcoin:signMessage'];
    const { signature } = await signMessageFeature.signMessage({
        message: new TextEncoder().encode('Hello Bitcoin!'),
        account: accounts[0],
    });

    // Sign a PSBT
    const signPsbtFeature = bitcoinWallet.features['bitcoin:signPsbt'];
    const result = await signPsbtFeature.signPsbt({
        psbt: 'your_psbt_base64_string',
        account: accounts[0],
        broadcast: false,
    });
}

Architecture

Core Components

  1. CtrlWalletBitcoinAccount: Implements the WalletAccount interface
  2. CtrlBitcoinWallet: Main wallet implementation implementing the Wallet interface
  3. Bitcoin Chain Definitions: Chain identifiers for mainnet and testnet
  4. Wallet Registration: Integration with wallet-standard registration system

Event Flow

  1. Provider emits events (connect, disconnect, accountChanged)
  2. Wallet listens to these events and updates internal state
  3. Wallet emits wallet-standard events (change)
  4. dApps receive wallet-standard events and update their UI

Development

Building

npm run build

Testing

npm test

Type Checking

npm run tsc

Examples

See the examples/ directory for complete usage examples:

  • bitcoin-usage.ts - Basic Bitcoin wallet usage
  • test-bitcoin.ts - Testing Bitcoin wallet functionality

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

Apache-2.0

Support

For support and questions, please open an issue on GitHub or contact the maintainers.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •