Module: BitcoinOpReturn

Defined in:
lib/bitcoin_op_return.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.bitcoind_cmdObject

Returns the value of attribute bitcoind_cmd.



12
13
14
# File 'lib/bitcoin_op_return.rb', line 12

def bitcoind_cmd
  @bitcoind_cmd
end

.transaction_feeObject

Returns the value of attribute transaction_fee.



12
13
14
# File 'lib/bitcoin_op_return.rb', line 12

def transaction_fee
  @transaction_fee
end

Class Method Details

.create(options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bitcoin_op_return.rb', line 14

def create options
  send_address = options[:address].to_s
  send_amount = options[:amount].to_f
   = options[:metadata].to_s
  testnet = !options[:testnet].nil?
  transaction_fee = options[:transaction_fee].to_f || self.transaction_fee

  # convert to hex where possible
   = [  ].pack("H*") if  =~ /\A([0-9A-Fa-f]{2})*\z/

  return { :error => "invalid address" } unless bitcoind("validateaddress", testnet, send_address)["isvalid"]
  return { :error => "metadata too long, limit is 75, recommended is 40 bytes" } if .length > 75

  # get the unspent inputs
  unspent_inputs = bitcoind("listunspent", testnet, 0)
  return { :error => "unable to retrieve unspent inputs" } unless unspent_inputs.kind_of? Array

  unspent_inputs.each do |input|
    input["priority"] = input["amount"] * input["confirmations"]
  end

  unspent_inputs.sort! do |a, b|
    a = a["priority"]
    b = b["priority"]
    # a follows b => -1
    # a == b => 0
    # b follows a => 1
    a == b ? 0 : (a < b ? 1 : -1)
  end

  inputs_spend = []
  output_amount = send_amount + self.transaction_fee
  inputs_amount = 0

  unspent_inputs.each do |input|
    inputs_spend << input
    inputs_amount += input["amount"]
    break if inputs_amount >= output_amount
  end

  return { :error => "insufficient funds to carry out transaction" } if inputs_amount < output_amount


  outputs_hash = {}
  outputs_hash[send_address] = send_amount


  unless inputs_amount == output_amount # no change
    change = inputs_amount - output_amount
    outputs_hash[bitcoind("getrawchangeaddress", testnet)] = change
  end

  # pack then unpack
  raw_txn = bitcoind("createrawtransaction", testnet, inputs_spend, outputs_hash)

  unpacked_txn = unpack_raw_txn(raw_txn)

  # append opreturn (6a represents op_return)
  
  op_return_script = "6a" + "#{metadata.length.chr}#{metadata}".unpack("H*")[0]

  unpacked_txn["vout"].push({
    "value" => 0,
    "scriptPubKey" => op_return_script
  })

  # $raw_txn=coinspark_pack_raw_txn($txn_unpacked);
  raw_txn = pack_raw_txn(unpacked_txn)
    
  sign_txn_response = bitcoind("signrawtransaction", testnet, raw_txn)
  # txid = bitcoind("sendrawtransaction", testnet, signed_txn)

  return { :error => "error signing transaction" } unless sign_txn_response["complete"]

  txid = bitcoind("sendrawtransaction", testnet, sign_txn_response["hex"])

  if txid.length != 64
    { :error => "could not send transaction" }
  else
    { :txid => txid }
  end
end