| 1 | class WalletEvmJob < ApplicationJob |
| 2 | queue_as :default |
| 3 | |
| 4 | def perform(user_id) |
| 5 | @user = User.find_by_id(user_id) |
| 6 | return unless @user |
| 7 | |
| 8 | change_to_working_directory |
| 9 | generate_wallet |
| 10 | finish |
| 11 | end |
| 12 | |
| 13 | private |
| 14 | |
| 15 | def change_to_working_directory |
| 16 | working_directory_path = if Rails.env.production? |
| 17 | '/mnt/HC_Volume_15126575/apps/musik88/evm_wallets/' |
| 18 | else |
| 19 | '../evm_wallets_devnet/' |
| 20 | end |
| 21 | Dir.chdir(working_directory_path) |
| 22 | |
| 23 | system("echo 'Change working directory to #{working_directory_path}'") |
| 24 | end |
| 25 | |
| 26 | def generate_wallet |
| 27 | system("echo 'Generating address for #{@user.name}'") |
| 28 | |
| 29 | @filename = "evm_wallet_#{@user.id}.json" |
| 30 | system("hdwallet generate -s ETH > #{@filename}") |
| 31 | |
| 32 | system("echo 'Wallet is generated for #{@user.name}'") |
| 33 | end |
| 34 | |
| 35 | def finish |
| 36 | wallet_information = JSON.parse(File.read(@filename)) |
| 37 | |
| 38 | wallet = Wallet.find_by(user_id: @user.id, wallet_type: Wallet.wallet_types[:evm]) |
| 39 | if wallet.nil? |
| 40 | system("echo 'Wallet for #{@user.name} does not exist.'") |
| 41 | return |
| 42 | end |
| 43 | wallet.update(pubkey: wallet_information['addresses']['p2pkh'], seed_phrase: wallet_information['mnemonic'], |
| 44 | status: :done) |
| 45 | wallet.save |
| 46 | end |
| 47 | end |