📚 SDK Examples

Code examples and tutorials for MyTokenWallet SDK

Getting Started

1. Basic Setup

import { MyTokenWallet } from '@mytokenwallet/sdk';

const wallet = new MyTokenWallet({
  network: 'mainnet',
  apiEndpoint: 'https://api.mytokenwallet.com'
});

2. Get Public Key (BRC-43)

const publicKey = await wallet.getPublicKey({
  protocolID: [1, "MyApp"],
  keyID: "registration",
  counterparty: "self",
  forSelf: true
});

console.log('Public Key:', publicKey);

3. Create Identity Certificate (BRC-52)

const cert = await wallet.createCertificate({
  type: 'https://example.com/user-profile',
  subject: '0123456789abcdef...',
  serialNumber: '1',
  fields: {
    name: 'John Doe',
    email: 'john@example.com'
  }
});

console.log('Certificate:', cert);

4. Build Transaction

const tx = await wallet.createTransaction({
  outputs: [{
    satoshis: 1000,
    script: '76a914...88ac'
  }],
  description: 'Payment for service'
});

console.log('TXID:', tx.txid);

Advanced Examples

Sign Message

const signature = await wallet.signMessage({
  message: 'Hello, BSV!',
  protocolID: [1, "MyApp"],
  keyID: "signing-key"
});

console.log('Signature:', signature);

Verify Certificate

const isValid = await wallet.verifyCertificate({
  certificate: cert,
  verifier: '0123456789abcdef...'
});

console.log('Valid:', isValid);

Decrypt Data

const decrypted = await wallet.decrypt({
  ciphertext: encryptedData,
  protocolID: [2, "SecureMessaging"],
  keyID: "message-key",
  counterparty: senderPublicKey
});

console.log('Message:', decrypted);

Create Action

const action = await wallet.createAction({
  description: 'Send payment',
  inputs: [...],
  outputs: [...],
  lockTime: 0
});

console.log('Action:', action);

Real-World Use Cases

🔐 User Authentication

Implement passwordless authentication using BRC-43 protocol

// Generate authentication key
const authKey = await wallet.getPublicKey({
  protocolID: [1, "MyApp"],
  keyID: "auth",
  counterparty: "self",
  forSelf: true
});

// Sign challenge
const signature = await wallet.signMessage({
  message: challengeNonce,
  protocolID: [1, "MyApp"],
  keyID: "auth"
});

💳 Payment Processing

Accept BSV payments with transaction monitoring

// Create payment request
const payment = await wallet.createTransaction({
  outputs: [{
    satoshis: amountInSatoshis,
    script: recipientScript
  }],
  description: `Payment for Order #${orderId}`
});

// Monitor confirmation
await wallet.waitForConfirmation(payment.txid);

📝 Digital Signatures

Sign documents and verify authenticity

// Sign document hash
const docHash = sha256(documentContent);
const signature = await wallet.signMessage({
  message: docHash,
  protocolID: [2, "DocSigning"],
  keyID: "document-key"
});

// Store signature on-chain
await wallet.storeSignature(signature);

🔗 Data Encryption

End-to-end encrypted messaging

// Encrypt message for recipient
const encrypted = await wallet.encrypt({
  plaintext: messageData,
  protocolID: [2, "SecureChat"],
  keyID: "chat-key",
  counterparty: recipientPublicKey
});

// Send encrypted data
await sendMessage(encrypted);

Additional Resources