Module: Bitcoin

Extended by:
Util
Defined in:
lib/bitcoin/tx.rb,
lib/bitcoin.rb,
lib/bitcoin/key.rb,
lib/bitcoin/rpc.rb,
lib/bitcoin/node.rb,
lib/bitcoin/psbt.rb,
lib/bitcoin/util.rb,
lib/bitcoin/block.rb,
lib/bitcoin/store.rb,
lib/bitcoin/tx_in.rb,
lib/bitcoin/base58.rb,
lib/bitcoin/logger.rb,
lib/bitcoin/tx_out.rb,
lib/bitcoin/wallet.rb,
lib/bitcoin/ext_key.rb,
lib/bitcoin/message.rb,
lib/bitcoin/network.rb,
lib/bitcoin/opcodes.rb,
lib/bitcoin/psbt/tx.rb,
lib/bitcoin/version.rb,
lib/bitcoin/key_path.rb,
lib/bitcoin/mnemonic.rb,
lib/bitcoin/node/cli.rb,
lib/bitcoin/node/spv.rb,
lib/bitcoin/payments.rb,
lib/bitcoin/store/db.rb,
lib/bitcoin/constants.rb,
lib/bitcoin/out_point.rb,
lib/bitcoin/secp256k1.rb,
lib/bitcoin/wallet/db.rb,
lib/bitcoin/bit_stream.rb,
lib/bitcoin/gcs_filter.rb,
lib/bitcoin/message/tx.rb,
lib/bitcoin/psbt/input.rb,
lib/bitcoin/validation.rb,
lib/bitcoin/merkle_tree.rb,
lib/bitcoin/message/inv.rb,
lib/bitcoin/psbt/output.rb,
lib/bitcoin/wallet/base.rb,
lib/bitcoin/block_filter.rb,
lib/bitcoin/block_header.rb,
lib/bitcoin/bloom_filter.rb,
lib/bitcoin/chain_params.rb,
lib/bitcoin/message/addr.rb,
lib/bitcoin/message/base.rb,
lib/bitcoin/message/ping.rb,
lib/bitcoin/message/pong.rb,
lib/bitcoin/network/peer.rb,
lib/bitcoin/network/pool.rb,
lib/bitcoin/message/block.rb,
lib/bitcoin/message/error.rb,
lib/bitcoin/script/script.rb,
lib/bitcoin/message/reject.rb,
lib/bitcoin/script_witness.rb,
lib/bitcoin/secp256k1/ruby.rb,
lib/bitcoin/wallet/account.rb,
lib/bitcoin/message/headers.rb,
lib/bitcoin/message/ver_ack.rb,
lib/bitcoin/message/version.rb,
lib/bitcoin/rpc/http_server.rb,
lib/bitcoin/script/multisig.rb,
lib/bitcoin/store/spv_chain.rb,
lib/bitcoin/message/get_addr.rb,
lib/bitcoin/message/get_data.rb,
lib/bitcoin/message/mem_pool.rb,
lib/bitcoin/psbt/hd_key_path.rb,
lib/bitcoin/secp256k1/native.rb,
lib/bitcoin/message/block_txn.rb,
lib/bitcoin/message/inventory.rb,
lib/bitcoin/message/not_found.rb,
lib/bitcoin/script/tx_checker.rb,
lib/bitcoin/store/chain_entry.rb,
lib/bitcoin/store/db/level_db.rb,
lib/bitcoin/wallet/master_key.rb,
lib/bitcoin/message/fee_filter.rb,
lib/bitcoin/message/filter_add.rb,
lib/bitcoin/message/get_blocks.rb,
lib/bitcoin/message/send_cmpct.rb,
lib/bitcoin/network/connection.rb,
lib/bitcoin/node/configuration.rb,
lib/bitcoin/payments/output.pb.rb,
lib/bitcoin/message/cmpct_block.rb,
lib/bitcoin/message/filter_load.rb,
lib/bitcoin/message/get_headers.rb,
lib/bitcoin/payments/payment.pb.rb,
lib/bitcoin/rpc/request_handler.rb,
lib/bitcoin/script/script_error.rb,
lib/bitcoin/message/filter_clear.rb,
lib/bitcoin/message/merkle_block.rb,
lib/bitcoin/message/network_addr.rb,
lib/bitcoin/message/prefilled_tx.rb,
lib/bitcoin/message/send_headers.rb,
lib/bitcoin/psbt/key_origin_info.rb,
lib/bitcoin/message/get_block_txn.rb,
lib/bitcoin/message/headers_parser.rb,
lib/bitcoin/network/peer_discovery.rb,
lib/bitcoin/network/message_handler.rb,
lib/bitcoin/payments/payment_ack.pb.rb,
lib/bitcoin/rpc/bitcoin_core_client.rb,
lib/bitcoin/script/script_interpreter.rb,
lib/bitcoin/message/block_transactions.rb,
lib/bitcoin/message/inventories_parser.rb,
lib/bitcoin/payments/payment_details.pb.rb,
lib/bitcoin/payments/payment_request.pb.rb,
lib/bitcoin/message/header_and_short_ids.rb,
lib/bitcoin/payments/x509_certificates.pb.rb,
lib/bitcoin/message/block_transaction_request.rb

Overview

Porting part of the code from bitcoin-ruby. see the license. github.com/lian/bitcoin-ruby/blob/master/COPYING

Defined Under Namespace

Modules: Base58, KeyPath, Logger, Message, Multisig, Network, Node, Opcodes, PSBT, Payments, RPC, Secp256k1, Store, Util, Wallet Classes: BitStreamReader, BitStreamWriter, Block, BlockFilter, BlockHeader, BloomFilter, ChainParams, ExtKey, ExtPubkey, GCSFilter, Key, MerkleTree, Mnemonic, OutPoint, Script, ScriptError, ScriptInterpreter, ScriptWitness, Tx, TxChecker, TxIn, TxOut, Validation, ValidationState

Constant Summary collapse

CURVE_ORDER =

Integers modulo the order of the curve(secp256k1)

ECDSA::Group::Secp256k1.order
VERSION =
"0.2.9"
COIN =
100_000_000
MAX_MONEY =
21_000_000 * COIN
MAX_BLOCK_SERIALIZED_SIZE =

The maximum allowed size for a serialized block, in bytes (only for buffer size limits)

4_000_000
MAX_BLOCK_WEIGHT =

The maximum allowed weight for a block, see BIP 141 (network rule)

4_000_000
MAX_BLOCK_SIGOPS_COST =

The maximum allowed number of signature check operations in a block (network rule)

80_000
COINBASE_MATURITY =

Coinbase transaction outputs can only be spent after this number of new blocks (network rule)

100
WITNESS_SCALE_FACTOR =
4
MIN_TRANSACTION_WEIGHT =

60 is the lower bound for the size of a valid serialized Tx

WITNESS_SCALE_FACTOR * 60
MIN_SERIALIZABLE_TRANSACTION_WEIGHT =

10 is the lower bound for the size of a serialized Tx

WITNESS_SCALE_FACTOR * 10
LOCKTIME_VERIFY_SEQUENCE =

Flags for nSequence and nLockTime locks

(1 << 0)
LOCKTIME_MEDIAN_TIME_PAST =
(1 << 1)
DUST_RELAY_TX_FEE =

Min feerate for defining dust.

3000
SCRIPT_VERIFY_NONE =

script verify flags

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)
SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY =

Verify CHECKLOCKTIMEVERIFY (BIP-65)

(1 << 9)
SCRIPT_VERIFY_CHECKSEQUENCEVERIFY =

support CHECKSEQUENCEVERIFY opcode (BIP-112)

(1 << 10)
SCRIPT_VERIFY_WITNESS =

Support segregated witness

(1 << 11)
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM =

Making v1-v16 witness program non-standard

(1 << 12)
SCRIPT_VERIFY_MINIMALIF =

Segwit script only: Require the argument of OP_IF/NOTIF to be exactly 0x01 or empty vector

(1 << 13)
SCRIPT_VERIFY_NULLFAIL =

Signature(s) must be empty vector if an CHECK(MULTI)SIG operation failed

(1 << 14)
SCRIPT_VERIFY_WITNESS_PUBKEYTYPE =

Public keys in segregated witness scripts must be compressed

(1 << 15)
SCRIPT_VERIFY_CONST_SCRIPTCODE =

Making OP_CODESEPARATOR and FindAndDelete fail any non-segwit scripts

(1 << 16)
MANDATORY_SCRIPT_VERIFY_FLAGS =
SCRIPT_VERIFY_P2SH
STANDARD_SCRIPT_VERIFY_FLAGS =

Standard script verification flags that standard transactions will comply with.

[MANDATORY_SCRIPT_VERIFY_FLAGS,
SCRIPT_VERIFY_DERSIG,
SCRIPT_VERIFY_STRICTENC,
SCRIPT_VERIFY_MINIMALDATA,
SCRIPT_VERIFY_NULLDUMMY,
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS,
SCRIPT_VERIFY_CLEANSTACK,
SCRIPT_VERIFY_MINIMALIF,
SCRIPT_VERIFY_NULLFAIL,
SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY,
SCRIPT_VERIFY_CHECKSEQUENCEVERIFY,
SCRIPT_VERIFY_LOW_S,
SCRIPT_VERIFY_WITNESS,
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM,
SCRIPT_VERIFY_WITNESS_PUBKEYTYPE,
SCRIPT_VERIFY_CONST_SCRIPTCODE].inject(SCRIPT_VERIFY_NONE){|flags, f| flags |= f}
WITNESS_VERSION =

witness version

0x00
MAX_SCRIPT_SIZE =

Maximum script length in bytes

10000
MAX_PUBKEYS_PER_MULTISIG =

Maximum number of public keys per multisig

20
MAX_OPS_PER_SCRIPT =

Maximum number of non-push operations per script

201
MAX_SCRIPT_ELEMENT_SIZE =

Maximum number of bytes pushable to the stack

520
MAX_STACK_SIZE =

Maximum number of size in the stack

1000
LOCKTIME_THRESHOLD =

Threshold for nLockTime: below this value it is interpreted as block number, otherwise as UNIX timestamp.

500000000
SIGHASH_TYPE =

Signature hash types/flags

{ all: 1, none: 2, single: 3, anyonecanpay: 128 }
SIGHASH_FORK_ID =

SIGHASH_FORK_ID for replay protection of the fork coin

0x40
FORK_ID_CASH =

fork coin id.

0
FORK_ID_GOLD =
79
DEFAULT_MAX_NUM_SIZE =

Maximum number length in bytes

4
MAX_OP_RETURN_RELAY =

80 bytes of data, 1 for OP_RETURN, 2 for the pushdata opcodes.

83
SIG_VERSION =
[:base, :witness_v0]
SCRIPT_ERR_OK =

for script error

0
SCRIPT_ERR_UNKNOWN_ERROR =
1
SCRIPT_ERR_EVAL_FALSE =
2
SCRIPT_ERR_OP_RETURN =
3
SCRIPT_ERR_SCRIPT_SIZE =

Max sizes

10
SCRIPT_ERR_PUSH_SIZE =
11
SCRIPT_ERR_OP_COUNT =
12
SCRIPT_ERR_STACK_SIZE =
13
SCRIPT_ERR_SIG_COUNT =
14
SCRIPT_ERR_PUBKEY_COUNT =
15
SCRIPT_ERR_VERIFY =

Failed verify operations

20
SCRIPT_ERR_EQUALVERIFY =
21
SCRIPT_ERR_CHECKMULTISIGVERIFY =
22
SCRIPT_ERR_CHECKSIGVERIFY =
23
SCRIPT_ERR_NUMEQUALVERIFY =
24
SCRIPT_ERR_BAD_OPCODE =

Logical/Format/Canonical errors

30
SCRIPT_ERR_DISABLED_OPCODE =
31
SCRIPT_ERR_INVALID_STACK_OPERATION =
32
SCRIPT_ERR_INVALID_ALTSTACK_OPERATION =
33
SCRIPT_ERR_UNBALANCED_CONDITIONAL =
34
SCRIPT_ERR_NEGATIVE_LOCKTIME =

CHECKLOCKTIMEVERIFY and CHECKSEQUENCEVERIFY

40
SCRIPT_ERR_UNSATISFIED_LOCKTIME =
41
SCRIPT_ERR_SIG_HASHTYPE =

Malleability

50
SCRIPT_ERR_SIG_DER =
51
SCRIPT_ERR_MINIMALDATA =
52
SCRIPT_ERR_SIG_PUSHONLY =
53
SCRIPT_ERR_SIG_HIGH_S =
54
SCRIPT_ERR_SIG_NULLDUMMY =
55
SCRIPT_ERR_PUBKEYTYPE =
56
SCRIPT_ERR_CLEANSTACK =
56
SCRIPT_ERR_MINIMALIF =
57
SCRIPT_ERR_SIG_NULLFAIL =
58
SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS =

softfork safeness

60
SCRIPT_ERR_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM =
61
SCRIPT_ERR_WITNESS_PROGRAM_WRONG_LENGTH =

segregated witness

70
SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY =
71
SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH =
72
SCRIPT_ERR_WITNESS_MALLEATED =
73
SCRIPT_ERR_WITNESS_MALLEATED_P2SH =
74
SCRIPT_ERR_WITNESS_UNEXPECTED =
75
SCRIPT_ERR_WITNESS_PUBKEYTYPE =
76
SCRIPT_ERR_OP_CODESEPARATOR =

Constant scriptCode

77
SCRIPT_ERR_SIG_FINDANDDELETE =
78
SCRIPT_ERR_ERROR_COUNT =
80
ERRCODES_MAP =
NAME_MAP =
WITNESS_COMMITMENT_HEADER =

witness commitment

'aa21a9ed'
COINBASE_WTXID =
'00'* 32
MESSAGE_HEADER_SIZE =

for message

24
PARALLEL_THREAD =

for peer

3
MAX_FUTURE_BLOCK_TIME =

Maximum amount of time that a block timestamp is allowed to exceed the current network-adjusted time before the block will be accepted.

2 * 60 * 60
MEDIAN_TIME_SPAN =

Size of set to pick median time from.

11
BIP32_EXTKEY_WITH_VERSION_SIZE =
78
HARDENED_THRESHOLD =

2**31

2147483648

Constants included from Util

Util::DIGEST_NAME_SHA256

Class Method Summary collapse

Methods included from Util

byte_to_bit, calc_checksum, decode_base58_address, double_sha256, encode_base58_address, hash160, hmac_sha256, pack_boolean, pack_var_int, pack_var_string, sha256, unpack_boolean, unpack_var_int, unpack_var_int_from_io, unpack_var_string

Class Method Details

.base_dirObject

base dir path that store blockchain data and wallet data



84
85
86
# File 'lib/bitcoin.rb', line 84

def self.base_dir
  "#{Dir.home}/.bitcoinrb/#{@chain_param}"
end

.chain_paramsObject

current bitcoin network chain params.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bitcoin.rb', line 70

def self.chain_params
  return @current_chain if @current_chain
  case @chain_param
  when :mainnet
    @current_chain = Bitcoin::ChainParams.mainnet
  when :testnet
    @current_chain = Bitcoin::ChainParams.testnet
  when :regtest
    @current_chain = Bitcoin::ChainParams.regtest
  end
  @current_chain
end

.chain_params=(name) ⇒ Object

set bitcoin network chain params



63
64
65
66
67
# File 'lib/bitcoin.rb', line 63

def self.chain_params=(name)
  raise "chain params for #{name} is not defined." unless %i(mainnet testnet regtest).include?(name.to_sym)
  @current_chain = nil
  @chain_param = name.to_sym
end

.hmac_sha256(key, data) ⇒ Object



98
99
100
# File 'lib/bitcoin.rb', line 98

def self.hmac_sha256(key, data)
  OpenSSL::HMAC.digest(OpenSSL::Digest.new('SHA256'), key, data)
end

.hmac_sha512(key, data) ⇒ Object



94
95
96
# File 'lib/bitcoin.rb', line 94

def self.hmac_sha512(key, data)
  OpenSSL::HMAC.digest(OpenSSL::Digest.new('SHA512'), key, data)
end

.secp_implObject

get secp implementation module



89
90
91
92
# File 'lib/bitcoin.rb', line 89

def self.secp_impl
  path = ENV['SECP256K1_LIB_PATH']
  (path && File.exist?(path)) ? Bitcoin::Secp256k1::Native : Bitcoin::Secp256k1::Ruby
end