Module: Crypto::Transaction

Extended by:
Transaction
Included in:
Transaction
Defined in:
lib/crypto/transaction.rb

Constant Summary collapse

SCALE_DECIMAL =
100000000

Instance Method Summary collapse

Instance Method Details

#__create_idObject



36
37
38
39
40
# File 'lib/crypto/transaction.rb', line 36

def __create_id
  tmp_id = SecureRandom.hex(32)
  return __create_id if tmp_id[0] == "0"
  tmp_id
end

#__create_signed_send_token_transaction(from_address, from_public_key, from_private_key, to_address, amount, fee = "0.0001", speed = "FAST") ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/crypto/transaction.rb', line 21

def __create_signed_send_token_transaction(from_address, from_public_key, from_private_key, to_address, amount, fee = "0.0001", speed = "FAST")
  transaction_id = __create_id
  timestamp = __timestamp
  scaled_amount = __scale_i64(amount)
  scaled_fee = __scale_i64(fee)

  unsigned_transaction = %Q{{"id":"#{transaction_id}","action":"send","senders":[{"address":"#{from_address}","public_key":"#{from_public_key}","amount":#{scaled_amount},"fee":#{scaled_fee},"signature":"0"}],"recipients":[{"address":"#{to_address}","amount":#{scaled_amount}}],"message":"","token":"AXNT","prev_hash":"0","timestamp":#{timestamp},"scaled":1,"kind":"#{speed}","version":"V1"}}

  payload_hash = Hashes.sha256(unsigned_transaction)
  signature = KeyUtils.sign(from_private_key, payload_hash)
  signed_transaction = __to_signed(unsigned_transaction, signature)

  %Q{{"transaction": #{signed_transaction}}}
end

#__get_private_key_from_wif(wif) ⇒ Object



50
51
52
# File 'lib/crypto/transaction.rb', line 50

def __get_private_key_from_wif(wif)
  Base64.strict_decode64(wif)[2..-7]
end

#__scale_i64(value) ⇒ Object



54
55
56
# File 'lib/crypto/transaction.rb', line 54

def __scale_i64(value)
  (BigDecimal(value) * SCALE_DECIMAL).to_i
end

#__timestampObject



42
43
44
# File 'lib/crypto/transaction.rb', line 42

def __timestamp
  Time.now.to_i * 1000
end

#__to_signed(unsigned_transaction, signature) ⇒ Object



46
47
48
# File 'lib/crypto/transaction.rb', line 46

def __to_signed(unsigned_transaction, signature)
  unsigned_transaction.gsub(%Q{"signature":"0"}, %Q{"signature":"#{signature}"})
end

#create_signed_send_transaction(from_address, from_public_key, wif, to_address, amount, fee = "0.0001", speed = "FAST") ⇒ Object



5
6
7
8
# File 'lib/crypto/transaction.rb', line 5

def create_signed_send_transaction(from_address, from_public_key, wif, to_address, amount, fee = "0.0001", speed = "FAST")
  from_private_key = __get_private_key_from_wif(wif)
  __create_signed_send_token_transaction(from_address, from_public_key, from_private_key, to_address, amount, fee, speed)
end

#post_transaction(transaction_json, url = "https://mainnet.axentro.io") ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/crypto/transaction.rb', line 10

def post_transaction(transaction_json, url = "https://mainnet.axentro.io")
  begin
    response = Faraday.post("#{url}/api/v1/transaction", transaction_json, "Content-Type" => "application/json")
    raise "status code was: #{response.status}" if response.status != 200
    json_response = JSON.parse(response.body)
    json_response["result"]["id"].to_s
  rescue => e
    puts "Error sending transaction: #{e}"
  end
end