btcruby 0.0.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +18 -0
  3. data/.travis.yml +7 -0
  4. data/FAQ.md +7 -0
  5. data/Gemfile +3 -0
  6. data/Gemfile.lock +18 -0
  7. data/HOWTO.md +17 -0
  8. data/LICENSE +19 -0
  9. data/README.md +59 -0
  10. data/Rakefile +6 -0
  11. data/TODO.txt +40 -0
  12. data/bin/console +19 -0
  13. data/btcruby.gemspec +20 -0
  14. data/documentation/address.md +73 -0
  15. data/documentation/base58.md +52 -0
  16. data/documentation/block.md +127 -0
  17. data/documentation/block_header.md +120 -0
  18. data/documentation/constants.md +88 -0
  19. data/documentation/data.md +54 -0
  20. data/documentation/diagnostics.md +90 -0
  21. data/documentation/extensions.md +76 -0
  22. data/documentation/hash_functions.md +58 -0
  23. data/documentation/hash_id.md +22 -0
  24. data/documentation/index.md +230 -0
  25. data/documentation/key.md +177 -0
  26. data/documentation/keychain.md +180 -0
  27. data/documentation/network.md +75 -0
  28. data/documentation/opcode.md +220 -0
  29. data/documentation/openssl.md +7 -0
  30. data/documentation/p2pkh.md +71 -0
  31. data/documentation/p2sh.md +64 -0
  32. data/documentation/proof_of_work.md +84 -0
  33. data/documentation/script.md +280 -0
  34. data/documentation/signature.md +71 -0
  35. data/documentation/transaction.md +213 -0
  36. data/documentation/transaction_builder.md +188 -0
  37. data/documentation/transaction_input.md +133 -0
  38. data/documentation/transaction_output.md +130 -0
  39. data/documentation/wif.md +72 -0
  40. data/documentation/wire_format.md +70 -0
  41. data/lib/btcruby/address.rb +296 -0
  42. data/lib/btcruby/base58.rb +108 -0
  43. data/lib/btcruby/big_number.rb +47 -0
  44. data/lib/btcruby/block.rb +170 -0
  45. data/lib/btcruby/block_header.rb +231 -0
  46. data/lib/btcruby/constants.rb +59 -0
  47. data/lib/btcruby/currency_formatter.rb +64 -0
  48. data/lib/btcruby/data.rb +98 -0
  49. data/lib/btcruby/diagnostics.rb +92 -0
  50. data/lib/btcruby/errors.rb +8 -0
  51. data/lib/btcruby/extensions.rb +65 -0
  52. data/lib/btcruby/hash_functions.rb +54 -0
  53. data/lib/btcruby/hash_id.rb +18 -0
  54. data/lib/btcruby/key.rb +517 -0
  55. data/lib/btcruby/keychain.rb +464 -0
  56. data/lib/btcruby/network.rb +73 -0
  57. data/lib/btcruby/opcode.rb +197 -0
  58. data/lib/btcruby/open_assets/asset.rb +35 -0
  59. data/lib/btcruby/open_assets/asset_address.rb +49 -0
  60. data/lib/btcruby/open_assets/asset_definition.rb +75 -0
  61. data/lib/btcruby/open_assets/asset_id.rb +24 -0
  62. data/lib/btcruby/open_assets/asset_marker.rb +94 -0
  63. data/lib/btcruby/open_assets/asset_processor.rb +377 -0
  64. data/lib/btcruby/open_assets/asset_transaction.rb +184 -0
  65. data/lib/btcruby/open_assets/asset_transaction_builder/errors.rb +15 -0
  66. data/lib/btcruby/open_assets/asset_transaction_builder/provider.rb +32 -0
  67. data/lib/btcruby/open_assets/asset_transaction_builder/result.rb +47 -0
  68. data/lib/btcruby/open_assets/asset_transaction_builder.rb +418 -0
  69. data/lib/btcruby/open_assets/asset_transaction_input.rb +64 -0
  70. data/lib/btcruby/open_assets/asset_transaction_output.rb +140 -0
  71. data/lib/btcruby/open_assets.rb +26 -0
  72. data/lib/btcruby/openssl.rb +536 -0
  73. data/lib/btcruby/proof_of_work.rb +110 -0
  74. data/lib/btcruby/safety.rb +26 -0
  75. data/lib/btcruby/script.rb +733 -0
  76. data/lib/btcruby/signature_hashtype.rb +37 -0
  77. data/lib/btcruby/transaction.rb +511 -0
  78. data/lib/btcruby/transaction_builder/errors.rb +15 -0
  79. data/lib/btcruby/transaction_builder/provider.rb +54 -0
  80. data/lib/btcruby/transaction_builder/result.rb +73 -0
  81. data/lib/btcruby/transaction_builder/signer.rb +28 -0
  82. data/lib/btcruby/transaction_builder.rb +520 -0
  83. data/lib/btcruby/transaction_input.rb +298 -0
  84. data/lib/btcruby/transaction_outpoint.rb +30 -0
  85. data/lib/btcruby/transaction_output.rb +315 -0
  86. data/lib/btcruby/version.rb +3 -0
  87. data/lib/btcruby/wif.rb +118 -0
  88. data/lib/btcruby/wire_format.rb +362 -0
  89. data/lib/btcruby.rb +44 -2
  90. data/sample_code/creating_a_p2sh_multisig_address.rb +21 -0
  91. data/sample_code/creating_a_transaction_manually.rb +44 -0
  92. data/sample_code/generating_an_address.rb +20 -0
  93. data/sample_code/using_transaction_builder.rb +49 -0
  94. data/spec/address_spec.rb +206 -0
  95. data/spec/all.rb +6 -0
  96. data/spec/base58_spec.rb +83 -0
  97. data/spec/block_header_spec.rb +18 -0
  98. data/spec/block_spec.rb +18 -0
  99. data/spec/currency_formatter_spec.rb +46 -0
  100. data/spec/data_spec.rb +50 -0
  101. data/spec/diagnostics_spec.rb +41 -0
  102. data/spec/key_spec.rb +205 -0
  103. data/spec/keychain_spec.rb +261 -0
  104. data/spec/network_spec.rb +48 -0
  105. data/spec/open_assets/asset_address_spec.rb +33 -0
  106. data/spec/open_assets/asset_id_spec.rb +15 -0
  107. data/spec/open_assets/asset_marker_spec.rb +47 -0
  108. data/spec/open_assets/asset_processor_spec.rb +567 -0
  109. data/spec/open_assets/asset_transaction_builder_spec.rb +273 -0
  110. data/spec/open_assets/asset_transaction_spec.rb +70 -0
  111. data/spec/proof_of_work_spec.rb +53 -0
  112. data/spec/script_spec.rb +66 -0
  113. data/spec/spec_helper.rb +8 -0
  114. data/spec/transaction_builder_spec.rb +338 -0
  115. data/spec/transaction_spec.rb +162 -0
  116. data/spec/wire_format_spec.rb +283 -0
  117. metadata +141 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64bf9829ab2ad396b5a77eaf7a559b76ff2834cf
4
- data.tar.gz: 9b08712780116753656e27884e32dd5ebee91823
3
+ metadata.gz: 259e4ee0ead1612a0af37185e4da5eef97238912
4
+ data.tar.gz: 1b7f28e68c728175a0fc2c499b57291e58b9df0d
5
5
  SHA512:
6
- metadata.gz: 1e8b8c4617eb33b730bbaafef511a623852bd3a814def2eee5245ceb47c0c10e78b0d9080f969b218e0399a10ab4447e6b9b7b1e251e3d746fbdf07dc57faf66
7
- data.tar.gz: 7c35a9ec7b63c5e2c8e6357f9bf0897c7481d6fab531b4080196501208ac6fa1ecd37f693f24aa79774778e40868cde1d6ce6e4604021aab305bf330efe7567d
6
+ metadata.gz: a0bb5fbceb5754b8a2ca60085aea0767adf9bec1b6fda5072dd11dd845f640b9aea7507900766165031f4d1b41851da48e0ef1dcb7fb4128311b81272eb5a232
7
+ data.tar.gz: a864ca92fe3cb78465c6bd4d1b9a042f0c9afbb495f7d7b6a77d224df6fd0b02234919fe6b887d4927d24db0dda7386a3f9f69c0a26ccd5234aeb4b0d3262178
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ build/
2
+ *.pbxuser
3
+ !default.pbxuser
4
+ *.mode1v3
5
+ !default.mode1v3
6
+ *.mode2v3
7
+ !default.mode2v3
8
+ *.perspectivev3
9
+ !default.perspectivev3
10
+ xcuserdata
11
+ *.xccheckout
12
+ *.moved-aside
13
+ DerivedData
14
+ *.hmap
15
+ *.ipa
16
+ *.xcuserstate
17
+ *.xcodeproj
18
+ *.xcodeproj/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ - 2.1.0
5
+ notifications:
6
+ slack:
7
+ secure: ZbYOCxRusuhvSdSl6nW9/emtZTnwK+o1uuhMKK5yrWDVL/kNV/ejcGTuInOA8TW1ULv6fNI9j2c7Q+zSu3CDO5oTMM7PK4TNeKsCY4koyk+91QKltWIH+aAFpPzbMcWbvnTSzHzTkhRW3ZPhfcQTkb6MRLEA/B6SjmUVc5ADTtc=
data/FAQ.md ADDED
@@ -0,0 +1,7 @@
1
+
2
+ BTCRuby FAQ
3
+ ===========
4
+
5
+ This will answer questions like "what is compressed public key", "why when I convert private key to address it is not correct?" etc.
6
+
7
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+ gem 'rake'
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ btcruby (0.1.0)
5
+ ffi (~> 1.9, >= 1.9.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ffi (1.9.3)
11
+ rake (10.3.2)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ btcruby!
18
+ rake
data/HOWTO.md ADDED
@@ -0,0 +1,17 @@
1
+ # BTCRuby HOWTO
2
+
3
+ * [Generating Private/Public Keys](#generating-privatepublic-keys)
4
+
5
+ ## Generating Private/Public Keys
6
+
7
+ ```ruby
8
+ require 'btcruby'
9
+ require 'btcruby/extensions'
10
+
11
+ key = BTC::Key.random
12
+
13
+ prv = key.to_wif # => L2yVhzwp5F7NXUmP31j274MPjnWi7WbFs3qRJEcdrjyBZ9jmEdWb
14
+ pub = key.address.to_s # => 15M8ocGxsWtaenLQy6hDKXeLHqQgG3ebpB
15
+
16
+ puts [prv, pub].join(" - ")
17
+ ```
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014 Chain Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # BTCRuby
2
+
3
+ [![Build Status](https://magnum.travis-ci.com/oleganza/btcruby.svg?token=84LHn4zp2Z1676MxCHjR)](https://magnum.travis-ci.com/oleganza/btcruby)
4
+
5
+ BTCRuby aims at clarity, security and flexibility. The API is designed simultenously
6
+ with [CoreBitcoin](https://github.com/oleganza/CoreBitcoin) (Objective-C library) and polished on real-life applications.
7
+
8
+ ## Documentation and Examples
9
+
10
+ Please see [BTCRuby Reference](documentation/index.md) for API documentation and examples.
11
+
12
+ ## Basic Features
13
+
14
+ * Encoding/decoding of addresses, WIF private keys (`BTC::Address`).
15
+ * APIs to construct and inspect blocks, transactions and scripts.
16
+ * Native BIP32 and BIP44 ("HW Wallets") support (see `BTC::Keychain`).
17
+ * Explicit APIs to handle compressed and uncompressed public keys.
18
+ * Explicit APIs to handle mainnet/testnet (see `BTC::Network`)
19
+ * Consistent API for data encoding used throughout the library itself (see `BTC::Data` and `BTC::WireFormat`).
20
+ * Flexible transaction builder that can work with arbitrary data sources that provide unspent outputs.
21
+ * Handy extensions on built-in classes (e.g. `String#to_hex`) are optional (see `extensions.rb`).
22
+ * Optional attributes on Transaction, TransactionOutput and TransactionInput to hold additional data
23
+ provided by 3rd party APIs.
24
+
25
+ ## Advanced Features
26
+
27
+ * ECDSA signatures are deterministic and normalized according to [RFC6979](https://tools.ietf.org/html/rfc6979)
28
+ and [BIP62](https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki).
29
+ * Automatic normalization of existing ECDSA signatures (see `BTC::Key#normalized_signature`).
30
+ * Rich script analysis and compositing support (see `BTC::Script`).
31
+ * Powerful diagnostics API covering the entire library (see `BTC::Diagnostics`).
32
+ * Canonicality checks for transactions, public keys and script elements.
33
+ * Fee computation and signature script simulation for building transactions without signing them.
34
+ * Complete OpenAssets implementation: validating OpenAssets transactions, easy to use transaction builder, API for handling Asset Definition.
35
+
36
+ ## Philosophy
37
+
38
+ * We use clear, expressive names for all methods and classes.
39
+ * Self-contained implementation. Only external dependency is `ffi` gem that helps linking directly with OpenSSL.
40
+ * For efficiency and consistency we use binary strings throughout the library (not the hex strings as in other libraries).
41
+ * We do not pollute standard classes with our methods. To use utility extensions like `String#to_hex` you should explicitly `require 'btcruby/extensions'`.
42
+ * We use OpenSSL `BIGNUM` implementation where compatibility is critical (instead of the built-in Ruby Bignum).
43
+ * We enforces canonical and determinstic ECDSA signatures for maximum compatibility and security using native OpenSSL functions.
44
+ * We treat endianness explicitly. Even though most systems are little-endian, it never hurts to show where indianness is important.
45
+
46
+ The goal is to provide a complete Bitcoin toolkit in Ruby.
47
+
48
+ ## How to run tests
49
+
50
+ ```
51
+ $ bundle install
52
+ $ rake
53
+ ```
54
+
55
+ ## Authors
56
+
57
+ * [Oleg Andreev](http://oleganza.com/)
58
+ * [Ryan Smith](http://r.32k.io)
59
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'rake/testtask'
2
+ Rake::TestTask.new do |t|
3
+ t.pattern = "spec/*_spec.rb"
4
+ end
5
+
6
+ task :default => :test
data/TODO.txt ADDED
@@ -0,0 +1,40 @@
1
+
2
+ TODO:
3
+
4
+ - verify integrity of the block after reading: hash all txs in a merkle root and compare with the declared merkle root.
5
+ - add BIP70 payment request support
6
+ - add convenience methods to handle BIP44 accounts in BTC::Keychain (like in CoreBitcoin)
7
+ - add Bitcoin URI support to encode and parse `bitcoin:` URIs.
8
+
9
+ + replace factory methods like `C.with_data` or `C.with_key` with initializer since we now throw exceptions instead of returning nil on input errors.
10
+ + block and block header classes
11
+ + redesigned transaction builder to return a result object like in CoreBitcoin
12
+ + data helpers
13
+ + tests
14
+ + base58
15
+ + addresses
16
+ + simplify API for addresses: Address.address_with_string() -> Address.with_string()
17
+ + add test for private key address
18
+ + keys and signatures support
19
+ + canonical signature
20
+ + deterministic signatures: k = HMAC-SHA256(hash, privkey)
21
+ + add ffi to Gemspec as a dependency
22
+ + transaction parsing and composing
23
+ + script parsing and composing
24
+ + tx import/export in bitcoin-QT dictionary format
25
+ + add docs for to_wif/from_wif
26
+ + add diagnostics to check canonicality of the signature
27
+ + BIP32 implementation: Keychain
28
+ + base58/base58check mess: untangle
29
+ + specs and fixes for zero-padded private keys
30
+ + clearer testnet/mainnet API for Script
31
+ + fee computation for transactions
32
+ + signature_hash for transaction
33
+ + transaction builder
34
+ + tx ID conversion to/from hash
35
+ + helper properties for Transaction and inputs/outputs to hold extra data received from APIs.
36
+
37
+ - test tool to detect broken or non-canonical transactions
38
+
39
+ - compact signatures support
40
+
data/bin/console ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/btcruby.rb'
3
+ require_relative '../lib/btcruby/extensions.rb'
4
+
5
+ include BTC
6
+
7
+ require 'irb'
8
+ require 'irb/completion'
9
+
10
+ # IRB.conf[:PROMPT]||={}
11
+ # IRB.conf[:PROMPT][:BTC_PROMPT] = { # name of prompt mode
12
+ # :AUTO_INDENT => true, # enables auto-indent mode
13
+ # :PROMPT_I => %{>>}, # normal prompt
14
+ # :PROMPT_S => %{">}, # prompt for continuated strings
15
+ # :PROMPT_C => %{*>}, # prompt for continuated statement
16
+ # :RETURN => "=>%s\n" # format to return value
17
+ # }
18
+ # IRB.conf[:PROMPT_MODE] = :BTC_PROMPT
19
+ IRB.start
data/btcruby.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ #encoding: UTF-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "btcruby/version"
4
+
5
+ files = `git ls-files`.split("\n")
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "btcruby"
9
+ s.email = "oleganza+btcruby@gmail.com"
10
+ s.version = BTC::VERSION
11
+ s.description = "Ruby library for interacting with Bitcoin."
12
+ s.summary = "Ruby library for interacting with Bitcoin."
13
+ s.authors = ["Oleg Andreev", "Ryan Smith"]
14
+ s.homepage = "https://github.com/oleganza/btcruby"
15
+ s.rubyforge_project = "btcruby"
16
+ s.license = "MIT"
17
+ s.files = files
18
+ s.require_paths = ["lib"]
19
+ s.add_runtime_dependency 'ffi', '~> 1.9', '>= 1.9.3'
20
+ end
@@ -0,0 +1,73 @@
1
+ [Index](index.md)
2
+
3
+ BTC::Address
4
+ ============
5
+
6
+ BTC::Address is a base class for [P2PKH](p2pkh.md), [P2SH](p2sh.md) addresses and [WIF](wif.md) private key serialization format. It is defined in **address.rb**.
7
+
8
+ Use BTC::Address to *decode* addresses of the unknown type (or when it does not matter). Use subclasses to *encode* addresses of a specific type.
9
+
10
+ Subclasses
11
+ ----------
12
+
13
+ * [BTC::PublicKeyAddress](p2pkh.md)
14
+ * [BTC::ScriptHashAddress](p2sh.md)
15
+ * [BTC::WIF](wif.md)
16
+
17
+ Class Methods
18
+ -------------
19
+
20
+ #### parse(*argument*)
21
+
22
+ If `argument` is a String, parses it and returns an instance of one of the concrete subclasses of BTC::Address.
23
+
24
+ If `argument` is a subclass of BTC::Address returns `argument`.
25
+
26
+ ```ruby
27
+ >> Address.parse("1CBtcGivXmHQ8ZqdPgeMfcpQNJrqTrSAcG")
28
+ => #<BTC::PublicKeyAddress:1CBtcGiv...>
29
+
30
+ >> Address.parse("3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8")
31
+ => #<BTC::ScriptHashAddress:3NukJ6fY...>
32
+
33
+ >> Address.parse("5KQntKuhYWSRXNqp2yhdXzjekYAR7US3MT1715Mbv5CyUKV6hVe")
34
+ => #<BTC::WIF:5KQntKuh... privkey:d20b62cd... (uncompressed pubkey)>
35
+ ```
36
+
37
+ Instance Methods
38
+ ----------------
39
+
40
+ #### data
41
+
42
+ Returns an underlying binary string stored within an address.
43
+ For [WIF](wif.md) format it is a 32-byte private key.
44
+ For [P2PKH](p2pkh.md) or [P2PSH](p2sh.md) addresses it is a 20-byte hash.
45
+
46
+ #### to_s
47
+
48
+ Returns string representation of the address in [Base58Check](base58.md) encoding.
49
+
50
+ #### network
51
+
52
+ Returns a [BTC::Network](network.md) instance based on version prefix of the address (mainnet or testnet).
53
+
54
+ #### version
55
+
56
+ Returns an integer value of the one-byte prefix of the address.
57
+
58
+ #### public_address
59
+
60
+ Returns a corresponding public address. Typically returns `self`, but for [WIF](wif.md) returns
61
+ an address based on the public key corresponding to the WIF-encoded private key.
62
+
63
+ #### p2pkh?
64
+
65
+ Returns `true` if this address is a subclass of [PublicKeyAddress](p2pkh.md). Otherwise returns `false`.
66
+
67
+ #### p2sh?
68
+
69
+ Returns `true` if this address is a subclass of [ScriptHashAddress](p2sh.md). Otherwise returns `false`.
70
+
71
+ #### ==
72
+
73
+ Returns `true` if underlying data and versions of both addresses match.
@@ -0,0 +1,52 @@
1
+ [Index](index.md)
2
+
3
+ BTC::Base58
4
+ ===========
5
+
6
+ **Base58** and **Base58Check** are two encodings invented by Satoshi for Bitcoin [addresses](addresses.md)
7
+ to make sure they don't contain ambiguous characters and can be selected with a double click.
8
+
9
+ Note: you normally do not use Base58 directly. To encode/decode Bitcoin addresses, use [BTC::Address](address.md) class.
10
+ To encode/decode private keys in WIF format, use [BTC::WIF](wif.md) class.
11
+
12
+ Satoshi:
13
+
14
+ > Why base-58 instead of standard base-64 encoding?
15
+ > - Don't want 0OIl characters that look the same in some fonts and could be used to create visually identical looking account numbers.
16
+ > - A string with non-alphanumeric characters is not as easily accepted as an account number.
17
+ > - E-mail usually won't line-break if there's no punctuation to break at.
18
+ > - Double-clicking selects the whole number as one word if it's all alphanumeric.
19
+
20
+ Base58 treats a binary string as a big-endian integer and encodes it in Base58 alphabet:
21
+
22
+ ```
23
+ 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
24
+ ```
25
+
26
+ Leading zero bytes are encoded as repeated "1"s.
27
+
28
+ **Base58Check** appends a 4-byte portion of [Hash256](hash_functions.md) of the binary string as a checksum, then encodes the resulting string in Base58.
29
+
30
+ Module Functions
31
+ ----------------
32
+
33
+ #### base58\_from\_data(*data*)
34
+
35
+ Returns a Base58-encoded string for a given binary string `data`.
36
+
37
+ #### data\_from\_base58(*string*)
38
+
39
+ Returns a binary string decoded from Base58-encoded `string`.
40
+
41
+ Raises `FormatError` if encoding is not valid.
42
+
43
+ #### base58check\_from\_data(*data*)
44
+
45
+ Returns a Base58-encoded string with appended checksum for a given binary string `data`.
46
+
47
+ #### data\_from\_base58check(*string*)
48
+
49
+ Returns a binary string decoded from Base58Check-encoded `string`.
50
+
51
+ Raises `FormatError` if checksum is not valid or encoding is not valid.
52
+
@@ -0,0 +1,127 @@
1
+ [Index](index.md)
2
+
3
+ BTC::Block
4
+ ==========
5
+
6
+ BTC::Block represents a collection of transactions within the block chain.
7
+ It consists of [block header](block_header.md) and an array of [transactions](transaction.md).
8
+
9
+ Class Methods
10
+ -------------
11
+
12
+ #### genesis_mainnet
13
+
14
+ Returns a genesis block for [mainnet](network.md#mainnet). See also [BTC::Network#genesis_block](network.md#genesis_block).
15
+
16
+ #### genesis_testnet
17
+
18
+ Returns a genesis block for [testnet](network.md#testnet). See also [BTC::Network#genesis_block](network.md#genesis_block).
19
+
20
+ Initializers
21
+ ------------
22
+
23
+ #### new(data: *String*)
24
+
25
+ Returns a new block initialized with a binary string in [wire format](wire_format.md).
26
+ Raises `ArgumentError` if block is incomplete or incorrectly encoded.
27
+
28
+ #### new(stream: *IO*)
29
+
30
+ Returns a new block initialized with data in [wire format](wire_format.md) read from a given stream.
31
+ Raises `ArgumentError` if block is incomplete or incorrectly encoded.
32
+
33
+ #### new(*attributes*)
34
+
35
+ Returns a new block with named [attributes](#attributes). All attributes are optional and have appropriate default values.
36
+
37
+ ```ruby
38
+ Block.new(
39
+ version: 2,
40
+ previous_block_id: "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
41
+ merkle_root: "...",
42
+ transactions: [...])
43
+ ```
44
+
45
+ Attributes
46
+ ----------
47
+
48
+ #### version
49
+
50
+ Block version (1 or 2).
51
+
52
+ #### previous\_block\_hash
53
+
54
+ Binary hash of the previous block.
55
+
56
+ #### previous\_block\_id
57
+
58
+ Hex big-endian hash of the previous block. See [Hash↔ID Conversion](hash_id.md).
59
+
60
+ #### merkle_root
61
+
62
+ Binary root hash of the transactions’ merkle tree.
63
+
64
+ #### timestamp
65
+
66
+ 32-bit unsigned UNIX timestamp.
67
+
68
+ #### time
69
+
70
+ Time object derived from timestamp
71
+
72
+ #### bits
73
+
74
+ Proof-of-work target in a compact form (uint32). See [Proof-of-Work Conversion Routines](proof_of_work.md).
75
+
76
+ #### nonce
77
+
78
+ Proof-of-work nonce (uint32 value iterated during mining).
79
+
80
+ #### transactions
81
+
82
+ List of [BTC::Transaction](transaction.md) instances contained within the block.
83
+
84
+ #### height
85
+
86
+ Optional height in the block chain (genesis block has height 0).
87
+ Not stored within block's [binary representation](#data).
88
+ Third party APIs may set this value for user’s convenience.
89
+
90
+ #### confirmations
91
+
92
+ Optional number of the confirmations for transactions in this block.
93
+ If this block is the latest one, `confirmations` equals 1.
94
+ Not stored within block's [binary representation](#data).
95
+ Third party APIs may set this value for user’s convenience.
96
+
97
+
98
+ Instance Methods
99
+ ----------------
100
+
101
+ #### block_hash
102
+
103
+ Binary hash of the block. Equals `SHA256(SHA256(header_data))`.
104
+
105
+ #### block_id
106
+
107
+ Hex big-endian hash of the block. See [Hash↔ID Conversion](hash_id.md).
108
+
109
+ #### block_header
110
+
111
+ Returns a [BTC::BlockHeader](block_header.md) instance containing all attributes of this block except `transactions`.
112
+
113
+ #### header_data
114
+
115
+ Returns block’s header data in [wire format](wire_format.md).
116
+
117
+ #### data
118
+
119
+ Returns block’s entire data in [wire format](wire_format.md).
120
+
121
+ #### dup
122
+
123
+ Returns a copy of the block.
124
+
125
+ #### ==
126
+
127
+ Returns `true` if binary representation of both blocks is equal (external attributes `height`, `confirmations` are ignored).
@@ -0,0 +1,120 @@
1
+ [Index](index.md)
2
+
3
+ BTC::BlockHeader
4
+ ================
5
+
6
+ Block header is a part of the [block](block.md) that contains date, merkle root of the block's [transactions](transaction.md) and additional data related to proof-of-work.
7
+
8
+ Class Methods
9
+ -------------
10
+
11
+ #### genesis_mainnet
12
+
13
+ Returns a genesis block header for [mainnet](network.md#mainnet).
14
+ See also [BTC::Network#genesis_block_header](network.md#genesis_block_header).
15
+
16
+ #### genesis_testnet
17
+
18
+ Returns a genesis block header for [testnet](network.md#testnet).
19
+ See also [BTC::Network#genesis_block_header](network.md#genesis_block_header).
20
+
21
+ Initializers
22
+ ------------
23
+
24
+ #### new(data: *String*)
25
+
26
+ Returns a new block header initialized with a binary string in [wire format](wire_format.md).
27
+ Raises `ArgumentError` if block header is incomplete or incorrectly encoded.
28
+
29
+ #### new(stream: *IO*)
30
+
31
+ Returns a new block header initialized with data in [wire format](wire_format.md) read from a given stream.
32
+ Raises `ArgumentError` if block header is incomplete or incorrectly encoded.
33
+
34
+ #### new(*attributes*)
35
+
36
+ Returns a new block header with named [attributes](#attributes). All attributes are optional and have appropriate default values.
37
+
38
+ ```ruby
39
+ BlockHeader.new(
40
+ version: 2,
41
+ previous_block_id: "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
42
+ merkle_root: "...",
43
+ timestamp: ...)
44
+ ```
45
+
46
+ Attributes
47
+ ----------
48
+
49
+ #### version
50
+
51
+ Block version (1 or 2).
52
+
53
+ #### previous\_block\_hash
54
+
55
+ Binary hash of the previous block.
56
+
57
+ #### previous\_block\_id
58
+
59
+ Hex big-endian hash of the previous block. See [Hash↔ID Conversion](hash_id.md).
60
+
61
+ #### merkle_root
62
+
63
+ Binary root hash of the transactions’ merkle tree.
64
+
65
+ #### timestamp
66
+
67
+ 32-bit unsigned UNIX timestamp.
68
+
69
+ #### time
70
+
71
+ Time object derived from timestamp
72
+
73
+ #### bits
74
+
75
+ Proof-of-work target in a compact form (uint32). See [Proof-of-Work Conversion Routines](proof_of_work.md).
76
+
77
+ #### nonce
78
+
79
+ Proof-of-work nonce (uint32 value iterated during mining).
80
+
81
+ #### height
82
+
83
+ Optional height in the block chain (genesis block has height 0).
84
+ Not stored within block's [binary representation](#data).
85
+ Third party APIs may set this value for user’s convenience.
86
+
87
+ #### confirmations
88
+
89
+ Optional number of the confirmations for transactions in this block.
90
+ If this block is the latest one, `confirmations` equals 1.
91
+ Not stored within block's [binary representation](#data).
92
+ Third party APIs may set this value for user’s convenience.
93
+
94
+
95
+ Instance Methods
96
+ ----------------
97
+
98
+ #### block_hash
99
+
100
+ Binary hash of the block header. Equals `SHA256(SHA256(header_data))`.
101
+
102
+ #### block_id
103
+
104
+ Hex big-endian hash of the block header. See [Hash↔ID Conversion](hash_id.md).
105
+
106
+ #### header_data
107
+
108
+ Returns block’s header data in [wire format](wire_format.md).
109
+
110
+ #### data
111
+
112
+ Returns `header_data`.
113
+
114
+ #### dup
115
+
116
+ Returns a copy of the block header.
117
+
118
+ #### ==
119
+
120
+ Returns `true` if binary representation of both block headers is equal (external attributes `height`, `confirmations` are ignored).