btcruby 0.0.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +18 -0
- data/.travis.yml +7 -0
- data/FAQ.md +7 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +18 -0
- data/HOWTO.md +17 -0
- data/LICENSE +19 -0
- data/README.md +59 -0
- data/Rakefile +6 -0
- data/TODO.txt +40 -0
- data/bin/console +19 -0
- data/btcruby.gemspec +20 -0
- data/documentation/address.md +73 -0
- data/documentation/base58.md +52 -0
- data/documentation/block.md +127 -0
- data/documentation/block_header.md +120 -0
- data/documentation/constants.md +88 -0
- data/documentation/data.md +54 -0
- data/documentation/diagnostics.md +90 -0
- data/documentation/extensions.md +76 -0
- data/documentation/hash_functions.md +58 -0
- data/documentation/hash_id.md +22 -0
- data/documentation/index.md +230 -0
- data/documentation/key.md +177 -0
- data/documentation/keychain.md +180 -0
- data/documentation/network.md +75 -0
- data/documentation/opcode.md +220 -0
- data/documentation/openssl.md +7 -0
- data/documentation/p2pkh.md +71 -0
- data/documentation/p2sh.md +64 -0
- data/documentation/proof_of_work.md +84 -0
- data/documentation/script.md +280 -0
- data/documentation/signature.md +71 -0
- data/documentation/transaction.md +213 -0
- data/documentation/transaction_builder.md +188 -0
- data/documentation/transaction_input.md +133 -0
- data/documentation/transaction_output.md +130 -0
- data/documentation/wif.md +72 -0
- data/documentation/wire_format.md +70 -0
- data/lib/btcruby/address.rb +296 -0
- data/lib/btcruby/base58.rb +108 -0
- data/lib/btcruby/big_number.rb +47 -0
- data/lib/btcruby/block.rb +170 -0
- data/lib/btcruby/block_header.rb +231 -0
- data/lib/btcruby/constants.rb +59 -0
- data/lib/btcruby/currency_formatter.rb +64 -0
- data/lib/btcruby/data.rb +98 -0
- data/lib/btcruby/diagnostics.rb +92 -0
- data/lib/btcruby/errors.rb +8 -0
- data/lib/btcruby/extensions.rb +65 -0
- data/lib/btcruby/hash_functions.rb +54 -0
- data/lib/btcruby/hash_id.rb +18 -0
- data/lib/btcruby/key.rb +517 -0
- data/lib/btcruby/keychain.rb +464 -0
- data/lib/btcruby/network.rb +73 -0
- data/lib/btcruby/opcode.rb +197 -0
- data/lib/btcruby/open_assets/asset.rb +35 -0
- data/lib/btcruby/open_assets/asset_address.rb +49 -0
- data/lib/btcruby/open_assets/asset_definition.rb +75 -0
- data/lib/btcruby/open_assets/asset_id.rb +24 -0
- data/lib/btcruby/open_assets/asset_marker.rb +94 -0
- data/lib/btcruby/open_assets/asset_processor.rb +377 -0
- data/lib/btcruby/open_assets/asset_transaction.rb +184 -0
- data/lib/btcruby/open_assets/asset_transaction_builder/errors.rb +15 -0
- data/lib/btcruby/open_assets/asset_transaction_builder/provider.rb +32 -0
- data/lib/btcruby/open_assets/asset_transaction_builder/result.rb +47 -0
- data/lib/btcruby/open_assets/asset_transaction_builder.rb +418 -0
- data/lib/btcruby/open_assets/asset_transaction_input.rb +64 -0
- data/lib/btcruby/open_assets/asset_transaction_output.rb +140 -0
- data/lib/btcruby/open_assets.rb +26 -0
- data/lib/btcruby/openssl.rb +536 -0
- data/lib/btcruby/proof_of_work.rb +110 -0
- data/lib/btcruby/safety.rb +26 -0
- data/lib/btcruby/script.rb +733 -0
- data/lib/btcruby/signature_hashtype.rb +37 -0
- data/lib/btcruby/transaction.rb +511 -0
- data/lib/btcruby/transaction_builder/errors.rb +15 -0
- data/lib/btcruby/transaction_builder/provider.rb +54 -0
- data/lib/btcruby/transaction_builder/result.rb +73 -0
- data/lib/btcruby/transaction_builder/signer.rb +28 -0
- data/lib/btcruby/transaction_builder.rb +520 -0
- data/lib/btcruby/transaction_input.rb +298 -0
- data/lib/btcruby/transaction_outpoint.rb +30 -0
- data/lib/btcruby/transaction_output.rb +315 -0
- data/lib/btcruby/version.rb +3 -0
- data/lib/btcruby/wif.rb +118 -0
- data/lib/btcruby/wire_format.rb +362 -0
- data/lib/btcruby.rb +44 -2
- data/sample_code/creating_a_p2sh_multisig_address.rb +21 -0
- data/sample_code/creating_a_transaction_manually.rb +44 -0
- data/sample_code/generating_an_address.rb +20 -0
- data/sample_code/using_transaction_builder.rb +49 -0
- data/spec/address_spec.rb +206 -0
- data/spec/all.rb +6 -0
- data/spec/base58_spec.rb +83 -0
- data/spec/block_header_spec.rb +18 -0
- data/spec/block_spec.rb +18 -0
- data/spec/currency_formatter_spec.rb +46 -0
- data/spec/data_spec.rb +50 -0
- data/spec/diagnostics_spec.rb +41 -0
- data/spec/key_spec.rb +205 -0
- data/spec/keychain_spec.rb +261 -0
- data/spec/network_spec.rb +48 -0
- data/spec/open_assets/asset_address_spec.rb +33 -0
- data/spec/open_assets/asset_id_spec.rb +15 -0
- data/spec/open_assets/asset_marker_spec.rb +47 -0
- data/spec/open_assets/asset_processor_spec.rb +567 -0
- data/spec/open_assets/asset_transaction_builder_spec.rb +273 -0
- data/spec/open_assets/asset_transaction_spec.rb +70 -0
- data/spec/proof_of_work_spec.rb +53 -0
- data/spec/script_spec.rb +66 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/transaction_builder_spec.rb +338 -0
- data/spec/transaction_spec.rb +162 -0
- data/spec/wire_format_spec.rb +283 -0
- metadata +141 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 259e4ee0ead1612a0af37185e4da5eef97238912
|
|
4
|
+
data.tar.gz: 1b7f28e68c728175a0fc2c499b57291e58b9df0d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
data/FAQ.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
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
|
+
[](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
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).
|