OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-wallet / cmd / send.js

87 lines · javascript ref:
// cmd/send.js - Send Payment Command

/**
 * Send 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: send [protocol] [chain] <to> <amount>
      // Examples:
      //   send wdk ethereum 0x... 0.1
      //   send wdk bitcoin bc1... 0.001
      //   send 0x... 0.1  (auto-detect)

      let protocol, chain, to, amount

      if (args.length === 4) {
        // Explicit: send wdk ethereum 0x... 0.1
        protocol = args[0]
        chain = args[1]
        to = args[2]
        amount = args[3]
      } else if (args.length === 3) {
        // Semi-explicit: send wdk 0x... 0.1 (auto-detect chain)
        protocol = args[0]
        to = args[1]
        amount = args[2]
      } else if (args.length === 2) {
        // Auto: send 0x... 0.1 (auto-detect protocol and chain)
        to = args[0]
        amount = args[1]
        // Use first enabled protocol
        protocol = Object.keys(enabledProtocols)[0]
      } else {
        console.error('❌ Invalid arguments')
        console.error('   Usage: hyper-wallet send <to> <amount>')
        console.error('          hyper-wallet send <protocol> <to> <amount>')
        console.error('          hyper-wallet send <protocol> <chain> <to> <amount>')
        console.error('')
        console.error('   Examples:')
        console.error('     hyper-wallet send 0x123... 0.1')
        console.error('     hyper-wallet send wdk 0x123... 0.1')
        console.error('     hyper-wallet send wdk ethereum 0x123... 0.1')
        process.exit(1)
      }

      // 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)
      }

      // Parse amount
      const amountNum = parseFloat(amount)
      if (isNaN(amountNum) || amountNum <= 0) {
        console.error('❌ Invalid amount:', amount)
        process.exit(1)
      }

      // Confirm transaction
      console.log('')
      console.log('📤 Preparing to send:')
      console.log(`   Protocol: ${protocol}`)
      if (chain) console.log(`   Chain: ${chain}`)
      console.log(`   To: ${to}`)
      console.log(`   Amount: ${amount}`)
      console.log('')
      console.log('⚠️  Transaction confirmation not yet implemented')
      console.log('   This would send real funds! Skipping for safety.')
      console.log('')
      console.log('TODO: Implement transaction confirmation and signing')

      // TODO: Implement actual sending
      // const result = await protocolHandler.send({ to, amount: amountNum, chain })
      // console.log('✅ Transaction sent!')
      // console.log(`   Hash: ${result.hash}`)
      // console.log(`   Explorer: <link>`)
    }
  ]
}