Class: Cryptos::Transaction

Inherits:
Struct
  • Object
show all
Includes:
Utils::Hashes, Utils::Hexas
Defined in:
lib/cryptos/transaction.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Hexas

#bignum_to_hex, #bin_to_hex, #byte_to_hex, #bytes_to_hex, #hex_size, #hex_to_little, #int_to_hex, #long_to_hex

Methods included from Utils::Bytes

#bignum_to_bytes, #bytes_to_bignum

Methods included from Utils::Hashes

#hash160, #hash256, #ripemd160, #sha256

Instance Attribute Details

#inputsObject

Returns the value of attribute inputs

Returns:

  • (Object)

    the current value of inputs



2
3
4
# File 'lib/cryptos/transaction.rb', line 2

def inputs
  @inputs
end

#locktimeObject

Returns the value of attribute locktime

Returns:

  • (Object)

    the current value of locktime



2
3
4
# File 'lib/cryptos/transaction.rb', line 2

def locktime
  @locktime
end

#outputsObject

Returns the value of attribute outputs

Returns:

  • (Object)

    the current value of outputs



2
3
4
# File 'lib/cryptos/transaction.rb', line 2

def outputs
  @outputs
end

#versionObject

Returns the value of attribute version

Returns:

  • (Object)

    the current value of version



2
3
4
# File 'lib/cryptos/transaction.rb', line 2

def version
  @version
end

Class Method Details

.from_ioc(input, output, change, version: 1, locktime: 0) ⇒ Object



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

def self.from_ioc(input, output, change, version: 1, locktime: 0)
  new version, [input], [output, change], locktime
end

Instance Method Details

#broadcast(cli) ⇒ Object



75
76
77
# File 'lib/cryptos/transaction.rb', line 75

def broadcast(cli)
  cli.send_raw_transaction serialize
end

#hashObject



16
17
18
# File 'lib/cryptos/transaction.rb', line 16

def hash
  hex_to_little sha256(sha256(serialize))
end

#multi_sign_input(index, address1, address2, sighash_type = 0x01) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cryptos/transaction.rb', line 50

def multi_sign_input(index, address1, address2, sighash_type = 0x01)
  redeem_script = Cryptos::Script.multisig address1, address2
  bytes_string = signature_hash redeem_script, sighash_type

  r, s = ecdsa_sign address1.public_key.private_key.value, bytes_string
  der1 = Cryptos::Der.new r: r, s: s
  r, s = ecdsa_sign address2.public_key.private_key.value, bytes_string
  der2 = Cryptos::Der.new r: r, s: s
  inputs[index].script_sig = Script.sig_multisig der1, der2, redeem_script

  serialize
end

#serializeObject



9
10
11
12
13
14
# File 'lib/cryptos/transaction.rb', line 9

def serialize
  inputs_hex = inputs.map(&:serialize).join
  outputs_hex = outputs.map(&:serialize).join
  int_to_hex(version) + byte_to_hex(inputs.size) + inputs_hex +
    byte_to_hex(outputs.size) + outputs_hex + int_to_hex(locktime)
end

#sign(private_key, public_key, script_pubkey, sighash_type = 0x01) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/cryptos/transaction.rb', line 26

def sign(private_key, public_key, script_pubkey, sighash_type = 0x01)
  bytes_string = signature_hash script_pubkey, sighash_type
  r, s = ecdsa_sign private_key.value, bytes_string
  der = Cryptos::Der.new r: r, s: s
  inputs.first.script_sig = Script.sig_pubkey(der, public_key)
  serialize
end

#sign_atomic_swap(secret, secret_hash, to_address, locktime, from_address, sighash_type = 0x01) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cryptos/transaction.rb', line 63

def sign_atomic_swap(secret, secret_hash, to_address, locktime, from_address, sighash_type = 0x01)
  redeem_script = Cryptos::Script.swap secret_hash, to_address, locktime, from_address
  bytes_string = signature_hash redeem_script, sighash_type

  r, s = ecdsa_sign to_address.public_key.private_key.value, bytes_string
  der = Cryptos::Der.new r: r, s: s
  sig_swap = Script.sig_swap der, to_address.public_key, secret, redeem_script
  inputs[0].script_sig = sig_swap

  serialize
end

#sign_input(index, address, sighash_type = 0x01) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cryptos/transaction.rb', line 34

def sign_input(index, address, sighash_type = 0x01)
  # TODO: get script_pubkey from input?
  script_pubkey = Cryptos::Script.p2pkh address
  bytes_string = signature_hash script_pubkey, sighash_type

  r, s = ecdsa_sign address.public_key.private_key.value, bytes_string
  der = Cryptos::Der.new r: r, s: s
  inputs[index].script_sig = Script.sig_pubkey der, address.public_key

  serialize
end

#sign_single_input(address) ⇒ Object



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

def sign_single_input(address)
  sign_input 0, address
end

#signature_hash(script_pubkey = nil, sighash_type = 0x01) ⇒ Object



20
21
22
23
24
# File 'lib/cryptos/transaction.rb', line 20

def signature_hash(script_pubkey = nil, sighash_type = 0x01)
  inputs.first.script_sig = script_pubkey if script_pubkey
  hash = sha256(sha256(serialize + int_to_hex(sighash_type)))
  [hash].pack('H*')
end

#to_sObject



79
80
81
# File 'lib/cryptos/transaction.rb', line 79

def to_s
  inputs.to_s + outputs.to_s
end