OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-wallet / cmd / receive.js

71 lines · javascript ref:
// cmd/receive.js - Receive Payment Command

import QRCode from 'qrcode'

/**
 * Receive command handler
 * @param {WalletContext} context - Wallet context
 * @param {Object} enabledProtocols - Enabled protocol objects
 * @returns {Array} - Command definition
 */
export default function (context, enabledProtocols) {
  return [
    async function handler(args) {
      // Parse arguments
      // Format: receive [protocol] [chain] [amount]
      // Examples:
      //   receive
      //   receive wdk ethereum
      //   receive wdk ethereum 0.1

      let protocol, chain, amount

      if (args.length === 3) {
        protocol = args[0]
        chain = args[1]
        amount = args[2]
      } else if (args.length === 2) {
        protocol = args[0]
        chain = args[1]
      } else if (args.length === 1) {
        protocol = args[0]
      } else {
        // Default to first protocol
        protocol = Object.keys(enabledProtocols)[0]
      }

      // Validate protocol
      const protocolHandler = context.protocols[protocol]
      if (!protocolHandler) {
        console.error(`❌ Protocol "${protocol}" not available`)
        console.error(`   Available protocols: ${Object.keys(context.protocols).join(', ')}`)
        process.exit(1)
      }

      // Get receiving address
      const result = await protocolHandler.receive({ chain, amount })

      console.log('')
      console.log('📥 Receive Payment')
      console.log(`   Protocol: ${protocol}`)
      console.log(`   Chain: ${result.chain}`)
      console.log(`   Currency: ${result.currency}`)
      console.log('')
      console.log(`   Address: ${result.address}`)

      if (amount) {
        console.log(`   Amount: ${amount} ${result.currency}`)
      }

      // Generate QR code
      console.log('')
      console.log('QR Code:')
      const qrString = await QRCode.toString(result.address, { type: 'terminal', small: true })
      console.log(qrString)

      console.log('')
      console.log('Share this address or QR code to receive payments')
    }
  ]
}