Module: Bitcoin::BitcoinConsensus

Extended by:
FFI::Library
Defined in:
lib/bitcoin/ffi/bitcoinconsensus.rb

Constant Summary collapse

SCRIPT_VERIFY_NONE =
0
SCRIPT_VERIFY_P2SH =
(1 << 0)
SCRIPT_VERIFY_STRICTENC =
(1 << 1)
SCRIPT_VERIFY_DERSIG =
(1 << 2)
SCRIPT_VERIFY_LOW_S =
(1 << 3)
SCRIPT_VERIFY_NULLDUMMY =
(1 << 4)
SCRIPT_VERIFY_SIGPUSHONLY =
(1 << 5)
SCRIPT_VERIFY_MINIMALDATA =
(1 << 6)
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS =
(1 << 7)
SCRIPT_VERIFY_CLEANSTACK =
(1 << 8)
ERR_CODES =
{ 0 => :ok, 1 => :tx_index, 2 => :tx_size_mismatch, 3 => :tx_deserialize }

Class Method Summary collapse

Class Method Details

.ffi_load_functions(file) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bitcoin/ffi/bitcoinconsensus.rb', line 26

def self.ffi_load_functions(file)
  class_eval <<-RUBY
    ffi_lib [ %[#{file}] ]
    attach_function :bitcoinconsensus_version, [], :uint

    # int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen,
    #                                    const unsigned char *txTo        , unsigned int txToLen,
    #                                    unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err);
    attach_function :bitcoinconsensus_verify_script, [:pointer, :uint, :pointer, :uint, :uint, :uint, :pointer], :int
  RUBY
end

.initObject



42
43
44
45
46
47
# File 'lib/bitcoin/ffi/bitcoinconsensus.rb', line 42

def self.init
  return if @bitcoin_consensus
  lib_path = lib_available?
  ffi_load_functions(lib_path)
  @bitcoin_consensus = true
end

.lib_available?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/bitcoin/ffi/bitcoinconsensus.rb', line 38

def self.lib_available?
  @__lib_path ||= [ ENV['BITCOINCONSENSUS_LIB_PATH'], 'vendor/bitcoin/src/.libs/libbitcoinconsensus.so' ].find{|f| File.exists?(f.to_s) }
end

.verify_script(input_index, script_pubkey, tx_payload, script_flags) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bitcoin/ffi/bitcoinconsensus.rb', line 55

def self.verify_script(input_index, script_pubkey, tx_payload, script_flags)
  init

  scriptPubKey = FFI::MemoryPointer.new(:uchar, script_pubkey.bytesize).put_bytes(0, script_pubkey)
  txTo = FFI::MemoryPointer.new(:uchar, tx_payload.bytesize).put_bytes(0, tx_payload)
  error_ret = FFI::MemoryPointer.new(:uint)

  ret = bitcoinconsensus_verify_script(scriptPubKey, scriptPubKey.size, txTo, txTo.size, input_index, script_flags, error_ret)

  case ret
  when 0
    false
  when 1
    (ERR_CODES[error_ret.read_int] == :ok) ? true : false
  else
    raise "error invalid result"
  end
end

.versionObject

api version



50
51
52
53
# File 'lib/bitcoin/ffi/bitcoinconsensus.rb', line 50

def self.version
  init
  bitcoinconsensus_version
end