Class: Cryptos::Script

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

Constant Summary collapse

OPCODES =
{
  'OP_0' =>  0x00,
  'OP_1' =>  0x51,
  'OP_2' =>  0x52,
  'OP_DUP' =>  0x76,
  'OP_HASH160' =>  0xA9,
  'OP_EQUAL' =>  0x87,
  'OP_EQUALVERIFY' =>  0x88,
  'OP_CHECKSIG' =>  0xAC,
  'OP_CHECKMULTISIG' => 0xAE
}.freeze

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, #sha256

Constructor Details

#initialize(script) ⇒ Script

Returns a new instance of Script.



54
55
56
# File 'lib/cryptos/script.rb', line 54

def initialize(script)
  @script = script
end

Instance Attribute Details

#scriptObject (readonly)

Returns the value of attribute script.



17
18
19
# File 'lib/cryptos/script.rb', line 17

def script
  @script
end

Class Method Details

.bare(script) ⇒ Object



50
51
52
# File 'lib/cryptos/script.rb', line 50

def self.bare(script)
  new script
end

.multisig(address1, address2) ⇒ Object

multisign redeem script



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

def self.multisig(address1, address2)
  new "OP_2 #{address1.public_key.to_sec} #{address2.public_key.to_sec} OP_2 OP_CHECKMULTISIG"
end

.p2pkh(address_or_hex) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/cryptos/script.rb', line 32

def self.p2pkh(address_or_hex)
  hash160 = if address_or_hex.is_a? String
              Address.to_hash160 address_or_hex
            else
              address_or_hex.to_hash160
            end
  new "OP_DUP OP_HASH160 #{hash160} OP_EQUALVERIFY OP_CHECKSIG"
end

.p2sh(script) ⇒ Object



41
42
43
# File 'lib/cryptos/script.rb', line 41

def self.p2sh(script)
  new "OP_HASH160 #{script.to_hash160} OP_EQUAL"
end

.sig_multisig(der1, der2, redeem_script) ⇒ Object

scriptSig for pay-to-multisig-hash outputs



28
29
30
# File 'lib/cryptos/script.rb', line 28

def self.sig_multisig(der1, der2, redeem_script)
  new "OP_0 #{der1.serialize} #{der2.serialize} #{redeem_script.serialize}"
end

.sig_pubkey(der, public_key) ⇒ Object

scriptSig for pay-to-pubkey-hash outputs

Parameters:

  • der
    • signature in der format

  • public_key
    • the public key

Returns:

  • script - a Script object holding scriptSig



23
24
25
# File 'lib/cryptos/script.rb', line 23

def self.sig_pubkey(der, public_key)
  new "#{der.serialize} #{public_key.to_sec}"
end

Instance Method Details

#sizeObject



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

def size
  [to_hex].pack('H*').size
end

#to_asmObject



67
68
69
# File 'lib/cryptos/script.rb', line 67

def to_asm
  script.to_s
end

#to_hash160Object



58
59
60
# File 'lib/cryptos/script.rb', line 58

def to_hash160
  hash160 to_hex
end

#to_hexObject Also known as: serialize



62
63
64
# File 'lib/cryptos/script.rb', line 62

def to_hex
  @hex ||= to_asm.split.map { |token| token.start_with?('OP') ? opcode(token) : data(token) }.join
end

#to_sObject



71
72
73
# File 'lib/cryptos/script.rb', line 71

def to_s
  to_asm
end