Ruby bitcoin development tutorial: creating a bitcoin wallet

Time:2019-10-23

Ruby bitcoin development tutorial: creating a bitcoin wallet

We’ve created a bot that replies to messages and a bot that automatically pays for bitcoins.

In this tutorial, you will learn the following

  1. How to create a bitcoin wallet.
  2. How to read the balance of the bitcoin wallet.
  3. How to pay in bitcoin and confirm immediately.
  4. How to withdraw Mixin Network bitcoins to your cold wallet or third-party exchange.

Prep: you need to have a Mixin Network account. The following code creates an account and writes it to a CSV file.

if File.file?(WALLET_NAME)
  p "mybitcoin_wallet.csv has already existed !"
  next
end
yaml_hash = YAML.load_file('./config.yml')

MixinBot.client_id  = yaml_hash["MIXIN_CLIENT_ID"]
MixinBot.session_id = yaml_hash["MIXIN_SESSION_ID"]
MixinBot.client_secret = yaml_hash["MIXIN_CLIENT_SECRET"]
MixinBot.pin_token   = yaml_hash["MIXIN_PIN_TOKEN"]
MixinBot.private_key = yaml_hash["MIXIN_PRIVATE_KEY"]

access_token = MixinBot.api.access_token("GET","/","")

rsa_key = OpenSSL::PKey::RSA.new(1024)
private_key = rsa_key.to_pem()
p private_key
public_key = rsa_key.public_key.to_pem
secret_client = public_key.sub("-----BEGIN PUBLIC KEY-----\n","").sub("\n-----END PUBLIC KEY-----\n","")
reqInfo = MixinBot.api.create_user("ruby bot",secret_client)
p reqInfo["data"]["pin_token"]
p reqInfo["data"]["user_id"]
p reqInfo["data"]["session_id"]


CSV.open(WALLET_NAME, "wb") do |csv|
  csv << [private_key, reqInfo["data"]["pin_token"], reqInfo["data"]["session_id"], reqInfo["data"]["user_id"]]
end

The above statement creates an RSA key pair locally, then calls Mixin Network to create the account, and finally saves the account information to the CSV file.

Now you need to take care of your account information, which will be used to read the balance of the account’s bitcoin assets or perform other operations.

Create a bitcoin wallet for your new account

The new account does not have a built-in bitcoin wallet by default, and now you can create one by reading the bitcoin balance.

if cmd == "2"
  table = CSV.read(WALLET_NAME)
  MixinBot.client_id = table[0][3]
  MixinBot.session_id = table[0][2]
  MixinBot.pin_token = table[0][1]
  MixinBot.private_key = table[0][0]
  botAssetsInfo = MixinBot.api.read_asset(BTC_ASSET_ID)
  p botAssetsInfo
  p "The BTC wallet address is " + botAssetsInfo["data"]["public_key"]
  p "The BTC wallet balance is " + botAssetsInfo["data"]["balance"]
end

The bitcoin asset details of the account created are as follows, where public key is the deposit address of bitcoin:

Make your choose(eg: q for Exit!):
2
{"data"=>{"type"=>"asset", "asset_id"=>"c6d0c728-2624-429b-8e0d-d9d19b6592fa",
"chain_id"=>"c6d0c728-2624-429b-8e0d-d9d19b6592fa", "symbol"=>"BTC",
 "name"=>"Bitcoin", "icon_url"=>"https://images.mixin.one/HvYGJsV5TGeZ-X9Ek3FEQohQZ3fE9LBEBGcOcn4c4BNHovP4fW4YB97Dg5LcXoQ1hUjMEgjbl1DPlKg1TW7kK6XP=s128",
 "balance"=>"0", "public_key"=>"1MQLQG1FshdVaQrDE36FLGabCitE3vPUA", "account_name"=>"",
 "account_tag"=>"", "price_btc"=>"1", "price_usd"=>"7948.30287759", "change_btc"=>"0",
 "change_usd"=>"0.006197123302394101", "asset_key"=>"c6d0c728-2624-429b-8e0d-d9d19b6592fa",
 "confirmations"=>6, "capitalization"=>0}}
"The BTC wallet address is 1MQLQG1FshdVaQrDE36FLGabCitE3vPUA"
"The BTC wallet balance is 0"
-------------------------------------------------------------------------

This API can provide a number of information related to bitcoin:

  • Deposit address :[public_key]
  • Logo: [icon_url]
  • Asset name :[name]
  • Uuid of asset in Mixin Network: [asset_key]
  • Price for usd (Coinmarketcap.com): [price_usd]
  • Number of blocks confirmed when depositing COINS :[confirmations]

What about bitcoin’s private key?

What about bitcoin’s private key? This private key is protected by Mixin Network through multiple signatures, so it is not visible to the user. Withdrawal and transfer of bitcoin assets require the user to provide the correct RSA signature, and the PIN code and session key can be completed.

Not just bitcoin, but ethereum, EOS and so on

This account not only supports bitcoin, but also ethereum, EOS, etc., complete list of blockchain support. This account also supports all ERC20 tokens and EOS tokens.

The process of creating another coin wallet is the same as that of creating a bitcoin wallet.

Cryptocurrency currently supported by Mixin Network (2019-02-19)

crypto uuid in Mixin Network
EOS 6cfe566e-4aad-470b-8c9a-2fd35b49c68d
CNB 965e5c6e-434c-3fa9-b780-c50f43cd955c
BTC c6d0c728-2624-429b-8e0d-d9d19b6592fa
ETC 2204c1ee-0ea2-4add-bb9a-b3719cfff93a
XRP 23dfb5a5-5d7b-48b6-905f-3970e3176e27
XEM 27921032-f73e-434e-955f-43d55672ee31
ETH 43d61dcd-e413-450d-80b8-101d5e903357
DASH 6472e7e3-75fd-48b6-b1dc-28d294ee1476
DOGE 6770a1e5-6086-44d5-b60f-545f9d9e8ffd
LTC 76c802a2-7c88-447f-a93e-c29c9e5dd9c8
SC 990c4c29-57e9-48f6-9819-7d986ea44985
ZEN a2c5d22b-62a2-4c13-b3f0-013290dbac60
ZEC c996abc9-d94e-4494-b1cf-2a3fd3ac5714
BCH fd11b6e3-0b87-41f1-a41f-f0e9b49e5bf0

The deposit address of EOS is somewhat different from other COINS. It consists of two parts: account_name and account tag. If you deposit EOS into Mixin Network, you need to fill in two pieces of data: the account name iseoswithmixinEnter your account_tag in notes, for example0aa2b00fad2c69059ca1b50de2b45569.

The return result of EOS’s asset balance is as follows:

Make your choose(eg: q for Exit!):
3
{"data"=>{"type"=>"asset", "asset_id"=>"6cfe566e-4aad-470b-8c9a-2fd35b49c68d",
 "chain_id"=>"6cfe566e-4aad-470b-8c9a-2fd35b49c68d", "symbol"=>"EOS",
  "name"=>"EOS", "icon_url"=>"https://images.mixin.one/a5dtG-IAg2IO0Zm4HxqJoQjfz-5nf1HWZ0teCyOnReMd3pmB8oEdSAXWvFHt2AJkJj5YgfyceTACjGmXnI-VyRo=s128",
   "balance"=>"0", "public_key"=>"", "account_name"=>"eoswithmixin",
   "account_tag"=>"60ec682616408d9486b5975a1361e269", "price_btc"=>"0.00079077",
   "price_usd"=>"6.28647972", "change_btc"=>"0.004235601507246285",
   "change_usd"=>"0.00974554134353926", "asset_key"=>"eosio.token:EOS",
   "confirmations"=>64, "capitalization"=>0}}
"The EOS wallet address is eoswithmixin 60ec682616408d9486b5975a1361e269"
"The EOS wallet balance is 0"
-------------------------------------------------------------------------

Deposit bitcoins and read bitcoins balance

Now, you can deposit COINS in your bitcoin wallet.

Of course, in the currency Network currency, poundage is quite expensive, the cost of the median in 0.001 BTC, according to the current price of $4000, at about $4, there is a convenient way, if you have a Mixin Messenger account, and have the COINS inside, can directly show the currency to the newly created account currency prepaid phone address, they are in the same Mixin Network within the Network, poundage is 0, and 1 second to the account.

The following code can read the balance of the bitcoin wallet.

table = CSV.read(WALLET_NAME)
MixinBot.client_id = table[0][3]
MixinBot.session_id = table[0][2]
MixinBot.pin_token = table[0][1]
MixinBot.private_key = table[0][0]
botAssetsInfo = MixinBot.api.read_asset(BTC_ASSET_ID)
p botAssetsInfo
p "The BTC wallet address is " + botAssetsInfo["data"]["public_key"]
p "The BTC wallet balance is " + botAssetsInfo["data"]["balance"]

Mixin Network is poundfree and confirmed immediately

Any currency traded within Mixin Network is poundless and immediately received.
Preliminary preparation: PIN is set in the account

For the newly created account, we set the new PIN code through updatePin. The code is as follows:

table = CSV.read(WALLET_NAME)
puts table[0][1]
MixinBot.client_id = table[0][3]
MixinBot.session_id = table[0][2]
MixinBot.pin_token = table[0][1]
MixinBot.private_key = table[0][0]
pinInfo = MixinBot.api.update_pin('',DEFAULT_PIN)
p pinInfo

Bitcoin payments between Mixin Network accounts

With Mixin Messenger, we can first transfer bitcoin to the robot and then have the robot transfer it to a new user.

if cmd == "teb"
  yaml_hash = YAML.load_file('./config.yml')

  MixinBot.client_id  = yaml_hash["MIXIN_CLIENT_ID"]
  MixinBot.session_id = yaml_hash["MIXIN_SESSION_ID"]
  MixinBot.client_secret = yaml_hash["MIXIN_CLIENT_SECRET"]
  MixinBot.pin_token   = yaml_hash["MIXIN_PIN_TOKEN"]
  MixinBot.private_key = yaml_hash["MIXIN_PRIVATE_KEY"]
  table = CSV.read(WALLET_NAME)
  wallet_userid = table[0][3]
  botAssetsInfo = MixinBot.api.read_asset(EOS_ASSET_ID)
  if botAssetsInfo["data"]["balance"].to_f > 0
    transInfo = MixinBot.api.create_transfer(MixinBot.api.encrypt_pin(yaml_hash["MIXIN_PIN_CODE"]),
                                      {
                                        asset_id: EOS_ASSET_ID,
                                        opponent_id: wallet_userid,
                                        amount: botAssetsInfo["data"]["balance"],
                                        trace_id: SecureRandom.uuid,
                                        memo: "from ruby"
                                      })
    p transInfo
 end
end

Read the balance of Bitcoin to confirm whether the Bitcoin transfer is successful!

    botAssetsInfo = MixinBot.api.read_assets()

How do you store bitcoins in your cold wallet or a third-party exchange

If you want to deposit money in your cold wallet or third-party exchange, first get the cold wallet or your wallet address in the third-party exchange, and then submit the wallet address to the Mixin Network.

  • The point tip: withdrawal is the need to pay to receive renewal fee, ready to bitcoin package address!

Add destination wallet address to Mixin Network

Calling the createAddress API returns an address_id that will be used for the next withdrawal operation.

if cmd == "wb"
  table = CSV.read(WALLET_NAME)
  MixinBot.client_id = table[0][3]
  MixinBot.session_id = table[0][2]
  MixinBot.pin_token = table[0][1]
  MixinBot.private_key = table[0][0]
  addressInfo = MixinBot.api.create_withdraw_address(BTC_ASSET_ID,
                                                       DEFAULT_PIN,
                                                       BTC_WALLET_ADDR,
                                                       "","",
                                                       "from ruby")
  p addressInfo
  p "The address id is " + addressInfo["data"]["address_id"] + " it is needed by read fee!"
end

Here,14T129GTbXXPGXXvZzVaNLRFPeHXD1C25CIt is a bitcoin wallet address, as shown below, the withdrawal fee is 0.0025738 BTC, the address_id is “345855b5-56a5-4f3b-ba9e-d99601ef86c1”.

Make your choose:wb
wb
{"type":"address","address_id":"58860d12-cbdc-40ae-8a4a-b0fc72f418b3",
"asset_id":"c6d0c728-2624-429b-8e0d-d9d19b6592fa","public_key":"14T129GTbXXPGXXvZzVaNLRFPeHXD1C25C",
"label":"hi","account_name":"","account_tag":"","fee":"0.00118846","reserve":"0","dust":"0.0001",
"updated_at":"2019-04-16T06:12:15.686517454Z"}
------------------------BTC---Withdrawal---Information---------------------------
The BTC Witchdrawal address is 14T129GTbXXPGXXvZzVaNLRFPeHXD1C25C
The BTC withdraw fee  is 0.00118846

After successfully creating the withdrawal address, you can use readAddress to read the latest withdrawal fee.

  addressInfo = MixinBot.api.get_withdraw_address(addressInfo["data"]["address_id"])
  p addressInfo

Submit a withdrawal request and the Mixin Network will process the withdrawal request immediately.

Submit withdrawal requests to the Mixin Network, withdrawAddress address_id createAddress is created.

if cmd == "wb"
  table = CSV.read(WALLET_NAME)
  MixinBot.client_id = table[0][3]
  MixinBot.session_id = table[0][2]
  MixinBot.pin_token = table[0][1]
  MixinBot.private_key = table[0][0]
  addressInfo = MixinBot.api.create_withdraw_address(BTC_ASSET_ID,
                                                       DEFAULT_PIN,
                                                       BTC_WALLET_ADDR,
                                                       "","",
                                                       "from ruby")
  p addressInfo
  p "The address id is " + addressInfo["data"]["address_id"] + " it is needed by read fee!"
  addressInfo2 = MixinBot.api.del_withdraw_address(addressInfo["data"]["address_id"], DEFAULT_PIN)
  p addressInfo2

  withdrawInfo = MixinBot.api.withdrawals(addressInfo["data"]["address_id"],
                                          DEFAULT_PIN,
                                          "0.1",
                                          SecureRandom.uuid,"from ruby")
  p withdrawInfo
end

You can see progress through blockchain explore.

The complete code is here

Recommended Today

Python can do anything! Today we can open and run computer software automatically!

Recently, we need to use software automation to do the project. Let’s share it with you!       Let’s take a look at Python operations:   First, how to open the software The first is to use the OS module, which is also the simplest one. If I want to open Notepad, I can: […]