Module: Bitcoin::BitcoinConsensus

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

Overview

binding for src/.libs/bitcoinconsensus.so (github.com/bitcoin/bitcoin) tag: v0.11.0 commit: d26f951802c762de04fb68e1a112d611929920ba

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 }.freeze

Class Method Summary collapse

Class Method Details

.ffi_load_functions(file) ⇒ Object



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

def self.ffi_load_functions(file)
  class_eval <<-RUBY # rubocop:disable Style/EvalWithLocation
    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



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

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)


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

def self.lib_available?
  @__lib_path ||= [ # rubocop:disable Naming/MemoizedInstanceVariableName
    ENV['BITCOINCONSENSUS_LIB_PATH'], 'vendor/bitcoin/src/.libs/libbitcoinconsensus.so'
  ].find { |f| File.exist?(f.to_s) }
end

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



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

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

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

  ret = bitcoinconsensus_verify_script(
    script_pub_key, script_pub_key.size, tx_to, tx_to.size, input_index, script_flags, error_ret
  )

  case ret
  when 0
    false
  when 1
    ERR_CODES[error_ret.read_int] == :ok
  else
    raise 'error invalid result'
  end
end

.versionObject

api version



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

def self.version
  init
  bitcoinconsensus_version
end