bitcoin-p2p 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 912b3329e673276821ab88a78f8c2dadd75158325bcd70a9ffeddb4e1362a195
4
+ data.tar.gz: ac7bce6b03bfa60c58d99823f432a6d8c388b8ed3b9967728122e74c3eab0a89
5
+ SHA512:
6
+ metadata.gz: dc99e62b73c210889a25557098c6d4fa60c48c6ec6e31a5c57c8e1076364c84b31db3046e640cc7525b1840297941386ba18e061d8a2aecba978834f81e294bc
7
+ data.tar.gz: 63302fdb95e862c891c3b130e2f7bfde424acd55c42d8d472b6ad6c6e924bd5e8305555a27c6c5e5ce59e80d1d2b041bbe11cc95a706b79df5b041db4a920cdf
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ bitcon-p2p
@@ -0,0 +1 @@
1
+ ruby-3.0.0
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at azuchi@chaintope.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in bitcoin-p2p.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 azuchi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,57 @@
1
+ # Bitcoin P2P message REPL tool
2
+
3
+ `bitcoin-p2p` is an irb-based tool for interactively exchanging P2P messages with Bitcoin nodes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bitcoin-p2p'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bitcoin-p2p
20
+
21
+ ## Usage
22
+
23
+ Start `bitcoin-p2p` by specifying the address and network of the node to connect to.
24
+
25
+ $ bitcoin-p2p -h localhost -n signet
26
+ > connected! You can send version message.
27
+
28
+ Once connected, you can use the [bitcoinrb message class](https://github.com/chaintope/bitcoinrb/wiki/P2P-Message)
29
+ to construct a P2P message and use the `send_message` method to send that message to the other party.
30
+ For example, the handshake can be done as follows:
31
+
32
+ > ver = Bitcoin::Message::Vresion.new
33
+ => #<Bitcoin::Message::Version:0x00005590317410f0 @version=70013, @services=8, @timestamp=1611462693, @local_addr=#<Bitcoin::Message::NetworkAddr:0x0000559031738ae0 @time=1611462693, @ip_addr=#<IPAddr: IPv4:127.0.0.1/255.255.255.255>, @port=3...
34
+ > send_message(ver)
35
+ => send message data: 0a03cf4076657273696f6e00000000006700000093cb71427d110100080000000000000025f80c6000000000080000000000000000000000000000000000ffff7f00000195bd080000000000000000000000000000000000ffff7f00000195bd519f0f6807576b1c112f626974636f696e72623a302e362e302f0000000000
36
+ ...
37
+ => receive version. payload: {"version"=>70016, "services"=>1097, "timestamp"=>1611462696, "local_addr"=>{"time"=>nil, "ip_addr"=>#<IPAddr: IPv6:0000:0000:0000:0000:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>, "port"=>0, "services"=>0}, "remote_addr"=>{"time"=>nil, "ip_addr"=>#<IPAddr: IPv6:0000:0000:0000:0000:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>, "port"=>0, "services"=>1033}, "nonce"=>5555482496462444166, "user_agent"=>"/Satoshi:0.21.0/", "start_height"=>21843, "relay"=>true}
38
+ > ack Bitcoin::Message::VerAck.new
39
+ => #<Bitcoin::Message::VerAck:0x00005590316342c0>
40
+ > send_message(ack)
41
+ => send message data: 0a03cf4076657261636b000000000000000000005df6e0e2
42
+
43
+ Note, You will be able to send other messages, but if the message you send does not comply with the specifications or
44
+ violates the policy of the other party, you will be disconnected.
45
+ In this case, a message will be displayed on the console saying `=> connection is closed. Please stop.`, so end with `stop`.
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bitcoin-p2p. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/bitcoin-p2p/blob/master/CODE_OF_CONDUCT.md).
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
54
+
55
+ ## Code of Conduct
56
+
57
+ Everyone interacting in the Bitcoin::P2p project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/bitcoin-p2p/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bitcoin'
4
+ require 'bitcoin/p2p'
5
+ require 'optparse'
6
+ require 'irb'
7
+
8
+ # Parse command line params.
9
+ cli = Bitcoin::P2P::Option.new
10
+ cli.parse
11
+
12
+ # Initialize network parameter.
13
+ Bitcoin.chain_params = cli.network
14
+ port = Bitcoin.chain_params.default_port
15
+
16
+ def send_message(message)
17
+ msg = message.is_a?(Bitcoin::Message::Base) ? message.to_pkt : message.htb
18
+ puts " => send message data: #{msg.bth}"
19
+ @conn.send_data(msg)
20
+ end
21
+
22
+ def stop
23
+ @node.exit
24
+ @console.exit
25
+ end
26
+
27
+ @console = Thread.new do
28
+ IRB.start
29
+ end
30
+
31
+ @node = Thread.new do
32
+ EM.run do
33
+ @conn = EM.connect(cli.host, port, Bitcoin::P2P::Handler)
34
+ end
35
+ end
36
+
37
+ @node.join
38
+ @console.join
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/bitcoin/p2p/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "bitcoin-p2p"
5
+ spec.version = Bitcoin::P2P::VERSION
6
+ spec.authors = ["azuchi"]
7
+ spec.email = ["azuchi@chaintope.com"]
8
+
9
+ spec.summary = %q{Bitcoin p2p message repl tool.}
10
+ spec.description = %q{Bitcoin p2p message repl tool.}
11
+ spec.homepage = "https://github.com/azuchi/bitcoin-p2p"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "bin"
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_runtime_dependency 'bitcoinrb'
28
+ end
@@ -0,0 +1,11 @@
1
+ require "bitcoin/p2p/version"
2
+
3
+ module Bitcoin
4
+ module P2P
5
+ class Error < StandardError; end
6
+
7
+ autoload :Handler, 'bitcoin/p2p/handler'
8
+ autoload :Option, 'bitcoin/p2p/option'
9
+
10
+ end
11
+ end
@@ -0,0 +1,97 @@
1
+ module Bitcoin
2
+ module P2P
3
+
4
+ module Handler
5
+
6
+ def post_init
7
+ puts "> connected! You can send version message."
8
+ end
9
+
10
+ def receive_data(data)
11
+ begin
12
+ parse(data)
13
+ rescue Bitcoin::Message::Error => e
14
+ puts "invalid header magic. #{e.message}"
15
+ end
16
+ end
17
+
18
+ def parse(message)
19
+ @message ||= ''
20
+ @message += message
21
+ command, payload, rest = parse_header
22
+ return unless command
23
+ msg = decode_msg(command, payload)
24
+ puts
25
+ puts " => receive #{command}. #{msg.to_h}"
26
+ @message = ""
27
+ parse(rest) if rest && rest.bytesize > 0
28
+ end
29
+
30
+ def parse_header
31
+ head_magic = Bitcoin.chain_params.magic_head
32
+ return if @message.nil? || @message.size < Bitcoin::MESSAGE_HEADER_SIZE
33
+
34
+ magic, command, length, checksum = @message.unpack('a4A12Va4')
35
+ raise Bitcoin::Message::Error, "invalid header magic. #{magic.bth}" unless magic.bth == head_magic
36
+
37
+ payload = @message[Bitcoin::MESSAGE_HEADER_SIZE...(Bitcoin::MESSAGE_HEADER_SIZE + length)]
38
+ return if payload.size < length
39
+ raise Bitcoin::Message::Error, "header checksum mismatch. #{checksum.bth}" unless Bitcoin.double_sha256(payload)[0...4] == checksum
40
+
41
+ rest = @message[(Bitcoin::MESSAGE_HEADER_SIZE + length)..-1]
42
+ [command, payload, rest]
43
+ end
44
+
45
+ def decode_msg(command, payload = nil)
46
+ case command
47
+ when Bitcoin::Message::Version::COMMAND
48
+ Bitcoin::Message::Version.parse_from_payload(payload)
49
+ when Bitcoin::Message::VerAck::COMMAND
50
+ Bitcoin::Message::VerAck.new
51
+ when Bitcoin::Message::GetAddr::COMMAND
52
+ Bitcoin::Message::GetAddr.new
53
+ when Bitcoin::Message::Addr::COMMAND
54
+ Bitcoin::Message::Addr.parse_from_payload(payload)
55
+ when Bitcoin::Message::SendHeaders::COMMAND
56
+ Bitcoin::Message::SendHeaders.new
57
+ when Bitcoin::Message::FeeFilter::COMMAND
58
+ Bitcoin::Message::FeeFilter.parse_from_payload(payload)
59
+ when Bitcoin::Message::Ping::COMMAND
60
+ Bitcoin::Message::Ping.parse_from_payload(payload)
61
+ when Bitcoin::Message::Pong::COMMAND
62
+ Bitcoin::Message::Pong.parse_from_payload(payload)
63
+ when Bitcoin::Message::GetHeaders::COMMAND
64
+ Bitcoin::Message::GetHeaders.parse_from_payload(payload)
65
+ when Bitcoin::Message::Headers::COMMAND
66
+ Bitcoin::Message::Headers.parse_from_payload(payload)
67
+ when Bitcoin::Message::Block::COMMAND
68
+ Bitcoin::Message::Block.parse_from_payload(payload)
69
+ when Bitcoin::Message::Tx::COMMAND
70
+ Bitcoin::Message::Tx.parse_from_payload(payload)
71
+ when Bitcoin::Message::NotFound::COMMAND
72
+ Bitcoin::Message::NotFound.parse_from_payload(payload)
73
+ when Bitcoin::Message::MemPool::COMMAND
74
+ Bitcoin::Message::MemPool.new
75
+ when Bitcoin::Message::Reject::COMMAND
76
+ Bitcoin::Message::Reject.parse_from_payload(payload)
77
+ when Bitcoin::Message::SendCmpct::COMMAND
78
+ Bitcoin::Message::SendCmpct.parse_from_payload(payload)
79
+ when Bitcoin::Message::Inv::COMMAND
80
+ Bitcoin::Message::Inv.parse_from_payload(payload)
81
+ when Bitcoin::Message::MerkleBlock::COMMAND
82
+ Bitcoin::Message::MerkleBlock.parse_from_payload(payload)
83
+ when Bitcoin::Message::CmpctBlock::COMMAND
84
+ Bitcoin::Message::CmpctBlock.parse_from_payload(payload)
85
+ when Bitcoin::Message::GetData::COMMAND
86
+ Bitcoin::Message::GetData.parse_from_payload(payload)
87
+ end
88
+ end
89
+
90
+ def unbind
91
+ puts "\n => connection is closed. please stop."
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,43 @@
1
+ module Bitcoin
2
+ module P2P
3
+
4
+ class Option
5
+
6
+ attr_accessor :host
7
+ attr_accessor :network
8
+
9
+ def parse
10
+ op = OptionParser.new do |opts|
11
+ opts.banner = "Usage: bitcoin-p2p -h <host> -n <network>"
12
+
13
+ opts.on("-h host", "--host host", "IP address of the connection destination.") do |v|
14
+ self.host = v
15
+ end
16
+
17
+ opts.on("-n network", "--network network", "Network to connect to.(mainnet or testnet or signet)") do |v|
18
+ self.network = v.to_sym
19
+ end
20
+
21
+ [host, network]
22
+ end
23
+
24
+ begin
25
+ puts op.parse(ARGV)
26
+ unless host
27
+ puts "Specify the host to connect to via -h option."
28
+ exit(1)
29
+ end
30
+ unless %w(mainnet testnet signet).include?(network.to_s)
31
+ puts "The network option must be either mainnet, testnet, or signet."
32
+ exit(1)
33
+ end
34
+ rescue OptionParser::InvalidOption => e
35
+ puts e.message
36
+ end
37
+
38
+ 4.times {ARGV.shift}
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ module Bitcoin
2
+ module P2P
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitcoin-p2p
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - azuchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bitcoinrb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Bitcoin p2p message repl tool.
28
+ email:
29
+ - azuchi@chaintope.com
30
+ executables:
31
+ - bitcoin-p2p
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".rspec"
37
+ - ".ruby-gemset"
38
+ - ".ruby-version"
39
+ - ".travis.yml"
40
+ - CODE_OF_CONDUCT.md
41
+ - Gemfile
42
+ - LICENSE.txt
43
+ - README.md
44
+ - Rakefile
45
+ - bin/bitcoin-p2p
46
+ - bitcoin-p2p.gemspec
47
+ - lib/bitcoin/p2p.rb
48
+ - lib/bitcoin/p2p/handler.rb
49
+ - lib/bitcoin/p2p/option.rb
50
+ - lib/bitcoin/p2p/version.rb
51
+ homepage: https://github.com/azuchi/bitcoin-p2p
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ homepage_uri: https://github.com/azuchi/bitcoin-p2p
56
+ source_code_uri: https://github.com/azuchi/bitcoin-p2p
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.3.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.2.3
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Bitcoin p2p message repl tool.
76
+ test_files: []