Module: Bitcoin

Extended by:
Util
Defined in:
lib/bitcoin/tx.rb,
lib/bitcoin.rb,
lib/bitcoin/ext.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/bip324.rb,
lib/bitcoin/errors.rb,
lib/bitcoin/logger.rb,
lib/bitcoin/slip39.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/sp/addr.rb,
lib/bitcoin/taproot.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/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/descriptor.rb,
lib/bitcoin/gcs_filter.rb,
lib/bitcoin/message/tx.rb,
lib/bitcoin/psbt/input.rb,
lib/bitcoin/slip39/sss.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/wallet/utxo.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/message_sign.rb,
lib/bitcoin/network/peer.rb,
lib/bitcoin/network/pool.rb,
lib/bitcoin/payment_code.rb,
lib/bitcoin/slip39/share.rb,
lib/bitcoin/bip324/cipher.rb,
lib/bitcoin/bip85_entropy.rb,
lib/bitcoin/descriptor/pk.rb,
lib/bitcoin/descriptor/sh.rb,
lib/bitcoin/descriptor/tr.rb,
lib/bitcoin/ext/array_ext.rb,
lib/bitcoin/message/block.rb,
lib/bitcoin/message/error.rb,
lib/bitcoin/script/script.rb,
lib/bitcoin/store/utxo_db.rb,
lib/bitcoin/descriptor/pkh.rb,
lib/bitcoin/descriptor/raw.rb,
lib/bitcoin/descriptor/wsh.rb,
lib/bitcoin/ext/object_ext.rb,
lib/bitcoin/message/reject.rb,
lib/bitcoin/script_witness.rb,
lib/bitcoin/secp256k1/ruby.rb,
lib/bitcoin/silent_payment.rb,
lib/bitcoin/wallet/account.rb,
lib/bitcoin/descriptor/addr.rb,
lib/bitcoin/descriptor/wpkh.rb,
lib/bitcoin/ext/json_parser.rb,
lib/bitcoin/message/addr_v2.rb,
lib/bitcoin/message/cfilter.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/descriptor/combo.rb,
lib/bitcoin/descriptor/multi.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/psbt/proprietary.rb,
lib/bitcoin/secp256k1/native.rb,
lib/bitcoin/descriptor/raw_tr.rb,
lib/bitcoin/message/block_txn.rb,
lib/bitcoin/message/cf_parser.rb,
lib/bitcoin/message/cfcheckpt.rb,
lib/bitcoin/message/cfheaders.rb,
lib/bitcoin/message/inventory.rb,
lib/bitcoin/message/not_found.rb,
lib/bitcoin/script/tx_checker.rb,
lib/bitcoin/secp256k1/rfc6979.rb,
lib/bitcoin/sighash_generator.rb,
lib/bitcoin/store/chain_entry.rb,
lib/bitcoin/store/db/level_db.rb,
lib/bitcoin/taproot/leaf_node.rb,
lib/bitcoin/wallet/master_key.rb,
lib/bitcoin/bip324/fs_chacha20.rb,
lib/bitcoin/descriptor/multi_a.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/descriptor/checksum.rb,
lib/bitcoin/message/cmpct_block.rb,
lib/bitcoin/message/filter_load.rb,
lib/bitcoin/message/get_headers.rb,
lib/bitcoin/rpc/request_handler.rb,
lib/bitcoin/script/script_error.rb,
lib/bitcoin/message/filter_clear.rb,
lib/bitcoin/message/get_cfilters.rb,
lib/bitcoin/message/merkle_block.rb,
lib/bitcoin/message/network_addr.rb,
lib/bitcoin/message/prefilled_tx.rb,
lib/bitcoin/message/send_addr_v2.rb,
lib/bitcoin/message/send_headers.rb,
lib/bitcoin/psbt/key_origin_info.rb,
lib/bitcoin/descriptor/expression.rb,
lib/bitcoin/message/get_block_txn.rb,
lib/bitcoin/message/get_cfcheckpt.rb,
lib/bitcoin/message/get_cfheaders.rb,
lib/bitcoin/taproot/control_block.rb,
lib/bitcoin/message/headers_parser.rb,
lib/bitcoin/network/peer_discovery.rb,
lib/bitcoin/taproot/simple_builder.rb,
lib/bitcoin/bip324/ell_swift_pubkey.rb,
lib/bitcoin/descriptor/sorted_multi.rb,
lib/bitcoin/network/message_handler.rb,
lib/bitcoin/rpc/bitcoin_core_client.rb,
lib/bitcoin/bip324/fs_chacha_poly1305.rb,
lib/bitcoin/descriptor/key_expression.rb,
lib/bitcoin/descriptor/sorted_multi_a.rb,
lib/bitcoin/script/script_interpreter.rb,
lib/bitcoin/message/block_transactions.rb,
lib/bitcoin/message/inventories_parser.rb,
lib/bitcoin/descriptor/script_expression.rb,
lib/bitcoin/message/header_and_short_ids.rb,
lib/bitcoin/taproot/custom_depth_builder.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: BIP324, Base58, Descriptor, Errors, Ext, HexConverter, KeyPath, Logger, Message, MessageSign, Multisig, Network, Node, Opcodes, PSBT, RPC, SLIP39, Secp256k1, SigHashGenerator, SilentPayment, Store, Taproot, Util, Wallet Classes: BIP85Entropy, BitStreamReader, BitStreamWriter, Block, BlockFilter, BlockHeader, BloomFilter, ChainParams, ExtKey, ExtPubkey, GCSFilter, Key, MerkleTree, Mnemonic, OutPoint, PaymentCode, 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 =
"1.7.0"
COIN =
100_000_000
MAX_MONEY =
21_000_000 * COIN
RIPEMD160_SIZE =

Byte size of the ripemd160 hash

20
SHA256_SIZE =

Byte size of the SHA256 hash

32
HASH160_SIZE =

Byte size of the HASH160 hash

20
HASH256_SIZE =

Byte size of the HASH256 hash

32
X_ONLY_PUBKEY_SIZE =

Byte size of x-only public key

32
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)
SCRIPT_VERIFY_TAPROOT =

Taproot/Tapscript validation (BIPs 341 & 342)

(1 << 17)
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION =

Making unknown Taproot leaf versions non-standard

(1 << 18)
SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS =

Making unknown OP_SUCCESS non-standard

(1 << 19)
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE =

Making unknown public key versions (in BIP 342 scripts) non-standard

(1 << 20)
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,
  SCRIPT_VERIFY_TAPROOT,
  SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION,
  SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS,
  SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE
].inject(SCRIPT_VERIFY_NONE){|flags, f| flags |= f}
WITNESS_VERSION_V0 =

witness version

0x00
WITNESS_VERSION_V1 =
Bitcoin::Opcodes::OP_1
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
ANNEX_TAG =

Tag for input annex. If there are at least two witness elements for a transaction input, and the first byte of the last element is 0x50, this last element is called annex, and has meanings independent of the script

0x50
VALIDATION_WEIGHT_PER_SIGOP_PASSED =

Validation weight per passing signature (Tapscript only, see BIP 342).

50
VALIDATION_WEIGHT_OFFSET =

How much weight budget is added to the witness size (Tapscript only, see BIP 342).

50
SIGHASH_TYPE =

Signature hash types/flags

{ all: 0x01, none: 0x02, single: 0x3, anyonecanpay: 0x80 , default: 0}
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
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 =
57
SCRIPT_ERR_MINIMALIF =
58
SCRIPT_ERR_SIG_NULLFAIL =
59
SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS =

softfork safeness

60
SCRIPT_ERR_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM =
61
SCRIPT_ERR_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION =
62
SCRIPT_ERR_DISCOURAGE_UNKNOWN_ANNEX =
63
SCRIPT_ERR_DISCOURAGE_OP_SUCCESS =
64
SCRIPT_ERR_DISCOURAGE_UPGRADABLE_PUBKEYTYPE =
65
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
SCRIPT_ERR_SCHNORR_SIG_SIZE =

Taproot

90
SCRIPT_ERR_SCHNORR_SIG_HASHTYPE =
91
SCRIPT_ERR_SCHNORR_SIG =
92
SCRIPT_ERR_TAPROOT_WRONG_CONTROL_SIZE =
93
SCRIPT_ERR_TAPSCRIPT_VALIDATION_WEIGHT =
94
SCRIPT_ERR_TAPSCRIPT_CHECKMULTISIG =
95
SCRIPT_ERR_TAPSCRIPT_MINIMALIF =
96
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
WITNESS_V0_SCRIPTHASH_SIZE =

Signature hash sizes

32
WITNESS_V0_KEYHASH_SIZE =
20
WITNESS_V1_TAPROOT_SIZE =
32
TAPROOT_LEAF_MASK =
0xfe
TAPROOT_LEAF_TAPSCRIPT =
0xc0
TAPROOT_CONTROL_BASE_SIZE =
33
TAPROOT_CONTROL_NODE_SIZE =
32
TAPROOT_CONTROL_MAX_NODE_COUNT =
128
TAPROOT_CONTROL_MAX_SIZE =
TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * TAPROOT_CONTROL_MAX_NODE_COUNT

Class Method Summary collapse

Methods included from Util

byte_to_bit, calc_checksum, decode_base58_address, double_sha256, encode_base58_address, hash160, hkdf_sha256, hmac_sha256, pack_boolean, pack_var_int, pack_var_string, padding_zero, sha256, tagged_hash, unpack_boolean, unpack_var_int, unpack_var_int_from_io, unpack_var_string, valid_address?

Class Method Details

.base_dirObject

base dir path that store blockchain data and wallet data



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

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

.chain_paramsObject

current bitcoin network chain params.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bitcoin.rb', line 82

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
  when :signet
    @current_chain = Bitcoin::ChainParams.signet
  end
  @current_chain
end

.chain_params=(name) ⇒ Object

set bitcoin network chain params



75
76
77
78
79
# File 'lib/bitcoin.rb', line 75

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

.hmac_sha256(key, data) ⇒ Object



112
113
114
# File 'lib/bitcoin.rb', line 112

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

.hmac_sha512(key, data) ⇒ Object



108
109
110
# File 'lib/bitcoin.rb', line 108

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

.secp_implObject

get secp implementation module



103
104
105
106
# File 'lib/bitcoin.rb', line 103

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