tapyrus 0.3.9 → 0.4.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/.github/workflows/ruby.yml +2 -2
- data/.ruby-version +1 -1
- data/README.md +43 -2
- data/lib/tapyrus/ext_key.rb +8 -4
- data/lib/tapyrus/key.rb +1 -1
- data/lib/tapyrus/key_path.rb +17 -13
- data/lib/tapyrus/message/fee_filter.rb +2 -2
- data/lib/tapyrus/message/header_and_short_ids.rb +4 -4
- data/lib/tapyrus/message/inventory.rb +1 -1
- data/lib/tapyrus/message/network_addr.rb +2 -2
- data/lib/tapyrus/message/ping.rb +2 -2
- data/lib/tapyrus/message/pong.rb +2 -2
- data/lib/tapyrus/message/send_cmpct.rb +2 -2
- data/lib/tapyrus/message/version.rb +3 -3
- data/lib/tapyrus/mnemonic.rb +9 -4
- data/lib/tapyrus/pstt/input.rb +422 -0
- data/lib/tapyrus/pstt/key_origin_info.rb +63 -0
- data/lib/tapyrus/pstt/output.rb +129 -0
- data/lib/tapyrus/pstt/proprietary.rb +54 -0
- data/lib/tapyrus/pstt/tx.rb +658 -0
- data/lib/tapyrus/pstt.rb +335 -0
- data/lib/tapyrus/rpc.rb +0 -2
- data/lib/tapyrus/script/script.rb +14 -4
- data/lib/tapyrus/secp256k1/rfc6979.rb +6 -3
- data/lib/tapyrus/tip0020.rb +341 -0
- data/lib/tapyrus/tx.rb +6 -2
- data/lib/tapyrus/tx_out.rb +2 -2
- data/lib/tapyrus/util.rb +3 -3
- data/lib/tapyrus/version.rb +1 -1
- data/lib/tapyrus/wallet/account.rb +2 -2
- data/lib/tapyrus/wallet/db.rb +3 -3
- data/lib/tapyrus/wallet/master_key.rb +2 -2
- data/lib/tapyrus.rb +3 -3
- data/tapyrusrb.gemspec +0 -3
- metadata +10 -61
- data/exe/tapyrusrb-cli +0 -5
- data/exe/tapyrusrbd +0 -42
- data/lib/tapyrus/network/connection.rb +0 -70
- data/lib/tapyrus/network/message_handler.rb +0 -244
- data/lib/tapyrus/network/peer.rb +0 -210
- data/lib/tapyrus/network/peer_discovery.rb +0 -43
- data/lib/tapyrus/network/pool.rb +0 -135
- data/lib/tapyrus/network.rb +0 -11
- data/lib/tapyrus/node/cli.rb +0 -114
- data/lib/tapyrus/node/configuration.rb +0 -36
- data/lib/tapyrus/node/spv.rb +0 -78
- data/lib/tapyrus/node.rb +0 -7
- data/lib/tapyrus/rpc/http_server.rb +0 -64
- data/lib/tapyrus/rpc/request_handler.rb +0 -146
data/lib/tapyrus/network/pool.rb
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
module Tapyrus
|
|
2
|
-
module Network
|
|
3
|
-
# Time between pings automatically sent out for latency probing and keepalive (in seconds).
|
|
4
|
-
PING_INTERVAL = 2 * 60
|
|
5
|
-
|
|
6
|
-
# Time after which to disconnect, after waiting for a ping response (or inactivity).
|
|
7
|
-
TIMEOUT_INTERVAL = 20 * 60
|
|
8
|
-
|
|
9
|
-
# Maximum number of automatic outgoing nodes
|
|
10
|
-
MAX_OUTBOUND_CONNECTIONS = 4
|
|
11
|
-
|
|
12
|
-
# peer pool class.
|
|
13
|
-
class Pool
|
|
14
|
-
include Observable
|
|
15
|
-
|
|
16
|
-
attr_reader :peers # active peers
|
|
17
|
-
attr_reader :pending_peers # currently connecting peer
|
|
18
|
-
attr_reader :node
|
|
19
|
-
attr_reader :chain
|
|
20
|
-
attr_reader :max_outbound
|
|
21
|
-
attr_reader :logger
|
|
22
|
-
attr_reader :peer_discovery
|
|
23
|
-
attr_accessor :started
|
|
24
|
-
attr_reader :mutex
|
|
25
|
-
|
|
26
|
-
def initialize(node, chain, configuration)
|
|
27
|
-
@node = node
|
|
28
|
-
@peers = []
|
|
29
|
-
@pending_peers = []
|
|
30
|
-
@max_outbound = MAX_OUTBOUND_CONNECTIONS
|
|
31
|
-
@chain = chain
|
|
32
|
-
@logger = Tapyrus::Logger.create(:debug)
|
|
33
|
-
@configuration = configuration
|
|
34
|
-
@peer_discovery = PeerDiscovery.new(configuration)
|
|
35
|
-
@started = false
|
|
36
|
-
@mutex = Mutex.new
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
# connecting other peers and begin network activity.
|
|
40
|
-
def start
|
|
41
|
-
raise "Cannot start a peer pool twice." if started
|
|
42
|
-
logger.debug "Start connecting other pears."
|
|
43
|
-
addr_list = peer_discovery.peers
|
|
44
|
-
|
|
45
|
-
connect(addr_list)
|
|
46
|
-
|
|
47
|
-
@started = true
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
# detect new peer connection.
|
|
51
|
-
def handle_new_peer(peer)
|
|
52
|
-
logger.debug "connected new peer #{peer.addr}."
|
|
53
|
-
mutex.synchronize do
|
|
54
|
-
peer.id = allocate_peer_id
|
|
55
|
-
unless peers.find(&:primary?)
|
|
56
|
-
peer.primary = true
|
|
57
|
-
peer.start_block_header_download
|
|
58
|
-
end
|
|
59
|
-
peers << peer
|
|
60
|
-
end
|
|
61
|
-
pending_peers.delete(peer)
|
|
62
|
-
filter_load(peer) if node.wallet
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def handle_close_peer(peer)
|
|
66
|
-
return unless started
|
|
67
|
-
peers.delete(peer)
|
|
68
|
-
pending_peers.delete(peer)
|
|
69
|
-
addr_list = peer_discovery.peers - peers.map(&:host) - pending_peers.map(&:host) - [peer.host]
|
|
70
|
-
connect(addr_list)
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
# terminate peers.
|
|
74
|
-
def terminate
|
|
75
|
-
peers.each { |peer| peer.close("terminate") }
|
|
76
|
-
pending_peers.each { |peer| peer.close("terminate") }
|
|
77
|
-
@peers = []
|
|
78
|
-
@started = false
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
# broadcast tx to connecting peer.
|
|
82
|
-
def broadcast(tx)
|
|
83
|
-
peers.each { |peer| peer.broadcast_tx(tx) }
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
# new bloom filter.
|
|
87
|
-
def filter_load(peer)
|
|
88
|
-
peer.send_filter_load(node.bloom)
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
# add element to bloom filter.
|
|
92
|
-
def filter_add(element)
|
|
93
|
-
peers.each { |peer| peer.send_filter_add(element) }
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
# clear bloom filter.
|
|
97
|
-
def filter_clear
|
|
98
|
-
peers.each { |peer| peer.send_filter_clear }
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
def handle_error(e)
|
|
102
|
-
terminate
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
private
|
|
106
|
-
|
|
107
|
-
# get primary peer
|
|
108
|
-
def primary_peer
|
|
109
|
-
peers.find(&:primary?)
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
# allocate new peer id
|
|
113
|
-
def allocate_peer_id
|
|
114
|
-
id = 0
|
|
115
|
-
id += 1 until peers.empty? || peers.find { |p| p.id == id }.nil?
|
|
116
|
-
id
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
def connect(addr_list)
|
|
120
|
-
port = Tapyrus.chain_params.default_port
|
|
121
|
-
|
|
122
|
-
EM::Iterator
|
|
123
|
-
.new(addr_list, Tapyrus::PARALLEL_THREAD)
|
|
124
|
-
.each do |ip, iter|
|
|
125
|
-
if pending_peers.size + peers.size < MAX_OUTBOUND_CONNECTIONS
|
|
126
|
-
peer = Peer.new(ip, port, self, @configuration)
|
|
127
|
-
pending_peers << peer
|
|
128
|
-
peer.connect
|
|
129
|
-
iter.next
|
|
130
|
-
end
|
|
131
|
-
end
|
|
132
|
-
end
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
end
|
data/lib/tapyrus/network.rb
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
require "eventmachine"
|
|
2
|
-
|
|
3
|
-
module Tapyrus
|
|
4
|
-
module Network
|
|
5
|
-
autoload :MessageHandler, "tapyrus/network/message_handler"
|
|
6
|
-
autoload :Connection, "tapyrus/network/connection"
|
|
7
|
-
autoload :Pool, "tapyrus/network/pool"
|
|
8
|
-
autoload :Peer, "tapyrus/network/peer"
|
|
9
|
-
autoload :PeerDiscovery, "tapyrus/network/peer_discovery"
|
|
10
|
-
end
|
|
11
|
-
end
|
data/lib/tapyrus/node/cli.rb
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
require "net/http"
|
|
2
|
-
require "thor"
|
|
3
|
-
require "json"
|
|
4
|
-
|
|
5
|
-
module Tapyrus
|
|
6
|
-
module Node
|
|
7
|
-
class CLI < Thor
|
|
8
|
-
class_option :network, aliases: "-n", default: :prod
|
|
9
|
-
|
|
10
|
-
desc "getblockchaininfo", "Returns an object containing various state info regarding blockchain processing."
|
|
11
|
-
def getblockchaininfo
|
|
12
|
-
request("getblockchaininfo")
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
desc "stop", "Stop Tapyrus server."
|
|
16
|
-
def stop
|
|
17
|
-
request("stop")
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
desc 'getblockheader "hash" ( verbose )',
|
|
21
|
-
'If verbose is false, returns a string that is serialized, hex-encoded data for blockheader "hash". If verbose is true, returns an Object with information about blockheader <hash>.'
|
|
22
|
-
def getblockheader(hash, verbose = true)
|
|
23
|
-
verbose = verbose.is_a?(String) ? (verbose == "true") : verbose
|
|
24
|
-
request("getblockheader", hash, verbose)
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
desc "getpeerinfo", "Returns data about each connected network node as a json array of objects."
|
|
28
|
-
def getpeerinfo
|
|
29
|
-
request("getpeerinfo")
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
desc 'decoderawtransaction "hexstring"',
|
|
33
|
-
"Return a JSON object representing the serialized, hex-encoded transaction."
|
|
34
|
-
def decoderawtransaction(hexstring)
|
|
35
|
-
request("decoderawtransaction", hexstring)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
desc 'decodescript "hexstring"', "Decode a hex-encoded script."
|
|
39
|
-
def decodescript(hexstring)
|
|
40
|
-
request("decodescript", hexstring)
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
# wallet cli
|
|
44
|
-
|
|
45
|
-
desc "sendrawtransaction", "Submits raw transaction (serialized, hex-encoded) to local node and network."
|
|
46
|
-
def sendrawtransaction(hex_tx)
|
|
47
|
-
request("sendrawtransaction", hex_tx)
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
desc 'createwallet "wallet_id"',
|
|
51
|
-
"Create new HD wallet. It returns an error if an existing wallet_id is specified. "
|
|
52
|
-
def createwallet(wallet_id)
|
|
53
|
-
request("createwallet", wallet_id)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
desc "listwallets",
|
|
57
|
-
'Returns a list of currently loaded wallets. For full information on the wallet, use "getwalletinfo"'
|
|
58
|
-
def listwallets
|
|
59
|
-
request("listwallets")
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
desc "getwalletinfo", "Returns an object containing various wallet state info."
|
|
63
|
-
def getwalletinfo
|
|
64
|
-
request("getwalletinfo")
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
desc "listaccounts", "[WIP]Returns Object that has account names as keys, account balances as values."
|
|
68
|
-
def listaccounts
|
|
69
|
-
request("listaccounts")
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
desc 'encryptwallet "passphrase"',
|
|
73
|
-
'Encrypts the wallet with "passphrase". This is for first time encryption.After this, any calls that interact with private keys such as sending or signing will require the passphrase to be set prior the making these calls.'
|
|
74
|
-
def encryptwallet(passhphrase)
|
|
75
|
-
request("encryptwallet", passhphrase)
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
desc 'getnewaddress "account"', "Returns a new Tapyrus address for receiving payments."
|
|
79
|
-
def getnewaddress(account)
|
|
80
|
-
request("getnewaddress", account)
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
private
|
|
84
|
-
|
|
85
|
-
def config
|
|
86
|
-
opts = {}
|
|
87
|
-
opts[:network] = options["network"] if options["network"]
|
|
88
|
-
@conf ||= Tapyrus::Node::Configuration.new(opts)
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
def request(command, *params)
|
|
92
|
-
data = { method: command, params: params, id: "jsonrpc" }
|
|
93
|
-
begin
|
|
94
|
-
uri = URI.parse(config.server_url)
|
|
95
|
-
http = Net::HTTP.new(uri.hostname, uri.port)
|
|
96
|
-
http.use_ssl = uri.scheme === "https"
|
|
97
|
-
request = Net::HTTP::Post.new("/")
|
|
98
|
-
request.content_type = "application/json"
|
|
99
|
-
request.body = data.to_json
|
|
100
|
-
response = http.request(request)
|
|
101
|
-
body = response.body
|
|
102
|
-
begin
|
|
103
|
-
json = JSON.parse(body.to_str)
|
|
104
|
-
puts JSON.pretty_generate(json)
|
|
105
|
-
rescue Exception
|
|
106
|
-
puts body.to_str
|
|
107
|
-
end
|
|
108
|
-
rescue Exception => e
|
|
109
|
-
puts e.message
|
|
110
|
-
end
|
|
111
|
-
end
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
|
-
end
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
require "iniparse"
|
|
2
|
-
|
|
3
|
-
module Tapyrus
|
|
4
|
-
module Node
|
|
5
|
-
class Configuration
|
|
6
|
-
attr_reader :conf
|
|
7
|
-
|
|
8
|
-
def initialize(opts = {})
|
|
9
|
-
# TODO apply configuration file.
|
|
10
|
-
opts[:network] = :prod unless opts[:network]
|
|
11
|
-
opts[:relay] = false unless opts[:relay]
|
|
12
|
-
Tapyrus.chain_params = opts[:network]
|
|
13
|
-
|
|
14
|
-
begin
|
|
15
|
-
ini_file = IniParse.parse(File.read("#{Tapyrus.base_dir}/tapyrusrb.conf"))
|
|
16
|
-
@conf = Hash[ini_file.to_h["__anonymous__"].map { |k, v| [k.to_sym, v] }]
|
|
17
|
-
rescue => e
|
|
18
|
-
@conf = {}
|
|
19
|
-
end
|
|
20
|
-
@conf.merge!(opts)
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def host
|
|
24
|
-
"localhost"
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def port
|
|
28
|
-
Tapyrus.chain_params.default_port - 1
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def server_url
|
|
32
|
-
"http://#{host}:#{port}"
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
end
|
data/lib/tapyrus/node/spv.rb
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
module Tapyrus
|
|
2
|
-
module Node
|
|
3
|
-
# SPV class
|
|
4
|
-
class SPV
|
|
5
|
-
attr_reader :chain
|
|
6
|
-
attr_reader :pool
|
|
7
|
-
attr_reader :logger
|
|
8
|
-
attr_accessor :running
|
|
9
|
-
attr_reader :configuration
|
|
10
|
-
attr_accessor :server
|
|
11
|
-
attr_accessor :wallet
|
|
12
|
-
attr_accessor :bloom
|
|
13
|
-
|
|
14
|
-
def initialize(configuration)
|
|
15
|
-
@chain = Tapyrus::Store::SPVChain.new
|
|
16
|
-
@configuration = configuration
|
|
17
|
-
@pool = Tapyrus::Network::Pool.new(self, @chain, @configuration)
|
|
18
|
-
@logger = Tapyrus::Logger.create(:debug)
|
|
19
|
-
@running = false
|
|
20
|
-
@wallet = Tapyrus::Wallet::Base.current_wallet
|
|
21
|
-
|
|
22
|
-
# TODO : optimize bloom filter parameters
|
|
23
|
-
setup_filter
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
# open the node.
|
|
27
|
-
def run
|
|
28
|
-
# TODO need process running check.
|
|
29
|
-
return if running
|
|
30
|
-
logger.debug "SPV node start running."
|
|
31
|
-
EM.run do
|
|
32
|
-
# EM.start_server('0.0.0.0', Tapyrus.chain_params.default_port, Tapyrus::Network::InboundConnector, self)
|
|
33
|
-
pool.start
|
|
34
|
-
@server = Tapyrus::RPC::HttpServer.run(self, configuration.port)
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# close the node.
|
|
39
|
-
def shutdown
|
|
40
|
-
pool.terminate
|
|
41
|
-
logger.debug "SPV node shutdown."
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
# broadcast a transaction
|
|
45
|
-
def broadcast(tx)
|
|
46
|
-
pool.broadcast(tx)
|
|
47
|
-
logger.debug "broadcast tx: #{tx.to_hex}"
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
# add filter element to bloom filter.
|
|
51
|
-
# [String] element. the hex string of txid, public key, public key hash or outpoint.
|
|
52
|
-
def filter_add(element)
|
|
53
|
-
bloom.add(element)
|
|
54
|
-
pool.filter_add(element)
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
# clear bloom filter.
|
|
58
|
-
def filter_clear
|
|
59
|
-
pool.filter_clear
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def add_observer(observer)
|
|
63
|
-
pool.add_observer(observer)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def delete_observer(observer)
|
|
67
|
-
pool.delete_observer(observer)
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
private
|
|
71
|
-
|
|
72
|
-
def setup_filter
|
|
73
|
-
@bloom = Tapyrus::BloomFilter.create_filter(512, 0.01)
|
|
74
|
-
wallet.watch_targets.each { |t| bloom.add(t.htb) } if wallet
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
end
|
data/lib/tapyrus/node.rb
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
require "evma_httpserver"
|
|
2
|
-
require "json"
|
|
3
|
-
|
|
4
|
-
module Tapyrus
|
|
5
|
-
module RPC
|
|
6
|
-
# Tapyrusrb RPC server.
|
|
7
|
-
class HttpServer < EM::Connection
|
|
8
|
-
include EM::HttpServer
|
|
9
|
-
include RequestHandler
|
|
10
|
-
|
|
11
|
-
attr_reader :node
|
|
12
|
-
attr_accessor :logger
|
|
13
|
-
|
|
14
|
-
def initialize(node)
|
|
15
|
-
@node = node
|
|
16
|
-
@logger = Tapyrus::Logger.create(:debug)
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def post_init
|
|
20
|
-
super
|
|
21
|
-
logger.debug "start http server."
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def self.run(node, port = 8332)
|
|
25
|
-
EM.start_server("0.0.0.0", port, HttpServer, node)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
# process http request.
|
|
29
|
-
def process_http_request
|
|
30
|
-
operation =
|
|
31
|
-
proc do
|
|
32
|
-
command, args = parse_json_params
|
|
33
|
-
logger.debug("process http request. command = #{command}")
|
|
34
|
-
begin
|
|
35
|
-
send(command, *args).to_json
|
|
36
|
-
rescue Exception => e
|
|
37
|
-
e
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
callback =
|
|
41
|
-
proc do |result|
|
|
42
|
-
response = EM::DelegatedHttpResponse.new(self)
|
|
43
|
-
if result.is_a?(Exception)
|
|
44
|
-
response.status = 500
|
|
45
|
-
response.content = result.message
|
|
46
|
-
else
|
|
47
|
-
response.status = 200
|
|
48
|
-
response.content = result
|
|
49
|
-
end
|
|
50
|
-
response.content_type "application/json"
|
|
51
|
-
response.send_response
|
|
52
|
-
end
|
|
53
|
-
EM.defer(operation, callback)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
# parse request parameter.
|
|
57
|
-
# @return [Array] the array of command and args
|
|
58
|
-
def parse_json_params
|
|
59
|
-
params = JSON.parse(@http_post_content)
|
|
60
|
-
[params["method"], params["params"]]
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
end
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
module Tapyrus
|
|
2
|
-
module RPC
|
|
3
|
-
# RPC server's request handler.
|
|
4
|
-
module RequestHandler
|
|
5
|
-
# Returns an object containing various state info regarding blockchain processing.
|
|
6
|
-
def getblockchaininfo
|
|
7
|
-
h = {}
|
|
8
|
-
h[:chain] = Tapyrus.chain_params.network
|
|
9
|
-
best_block = node.chain.latest_block
|
|
10
|
-
h[:headers] = best_block.height
|
|
11
|
-
h[:bestblockhash] = best_block.header.block_id
|
|
12
|
-
h[:mediantime] = node.chain.mtp(best_block.block_hash)
|
|
13
|
-
h
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
# shutdown node
|
|
17
|
-
def stop
|
|
18
|
-
node.shutdown
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
# get block header information.
|
|
22
|
-
# @param [String] block_id block hash(big endian)
|
|
23
|
-
def getblockheader(block_id, verbose)
|
|
24
|
-
block_hash = block_id.rhex
|
|
25
|
-
entry = node.chain.find_entry_by_hash(block_hash)
|
|
26
|
-
raise ArgumentError.new("Block not found") unless entry
|
|
27
|
-
if verbose
|
|
28
|
-
{
|
|
29
|
-
hash: block_id,
|
|
30
|
-
height: entry.height,
|
|
31
|
-
features: entry.header.features,
|
|
32
|
-
featuresHex: entry.header.features.to_even_length_hex.ljust(8, "0"),
|
|
33
|
-
merkleroot: entry.header.merkle_root.rhex,
|
|
34
|
-
immutablemerkleroot: entry.header.im_merkle_root.rhex,
|
|
35
|
-
time: entry.header.time,
|
|
36
|
-
mediantime: node.chain.mtp(block_hash),
|
|
37
|
-
xfield_type: entry.header.x_field_type,
|
|
38
|
-
xfield: entry.header.x_field,
|
|
39
|
-
proof: entry.header.proof,
|
|
40
|
-
previousblockhash: entry.prev_hash.rhex,
|
|
41
|
-
nextblockhash: node.chain.next_hash(block_hash).rhex
|
|
42
|
-
}
|
|
43
|
-
else
|
|
44
|
-
entry.header.to_hex
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
# Returns connected peer information.
|
|
49
|
-
def getpeerinfo
|
|
50
|
-
node.pool.peers.map do |peer|
|
|
51
|
-
local_addr = "#{peer.remote_version.remote_addr.ip}:18333"
|
|
52
|
-
{
|
|
53
|
-
id: peer.id,
|
|
54
|
-
addr: "#{peer.host}:#{peer.port}",
|
|
55
|
-
addrlocal: local_addr,
|
|
56
|
-
services: peer.remote_version.services.to_even_length_hex.rjust(16, "0"),
|
|
57
|
-
relaytxes: peer.remote_version.relay,
|
|
58
|
-
lastsend: peer.last_send,
|
|
59
|
-
lastrecv: peer.last_recv,
|
|
60
|
-
bytessent: peer.bytes_sent,
|
|
61
|
-
bytesrecv: peer.bytes_recv,
|
|
62
|
-
conntime: peer.conn_time,
|
|
63
|
-
pingtime: peer.ping_time,
|
|
64
|
-
minping: peer.min_ping,
|
|
65
|
-
version: peer.remote_version.version,
|
|
66
|
-
subver: peer.remote_version.user_agent,
|
|
67
|
-
inbound: !peer.outbound?,
|
|
68
|
-
startingheight: peer.remote_version.start_height,
|
|
69
|
-
best_hash: peer.best_hash,
|
|
70
|
-
best_height: peer.best_height
|
|
71
|
-
}
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
# broadcast transaction
|
|
76
|
-
def sendrawtransaction(hex_tx)
|
|
77
|
-
tx = Tapyrus::Tx.parse_from_payload(hex_tx.htb)
|
|
78
|
-
|
|
79
|
-
# TODO check wether tx is valid
|
|
80
|
-
node.broadcast(tx)
|
|
81
|
-
tx.txid
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
# decode tx data.
|
|
85
|
-
def decoderawtransaction(hex_tx)
|
|
86
|
-
begin
|
|
87
|
-
Tapyrus::Tx.parse_from_payload(hex_tx.htb).to_h
|
|
88
|
-
rescue Exception
|
|
89
|
-
raise ArgumentError.new("TX decode failed")
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
# decode script data.
|
|
94
|
-
def decodescript(hex_script)
|
|
95
|
-
begin
|
|
96
|
-
script = Tapyrus::Script.parse_from_payload(hex_script.htb)
|
|
97
|
-
h = script.to_h
|
|
98
|
-
h.delete(:hex)
|
|
99
|
-
h[:p2sh] = script.to_p2sh.to_addr unless script.p2sh?
|
|
100
|
-
h
|
|
101
|
-
rescue Exception
|
|
102
|
-
raise ArgumentError.new("Script decode failed")
|
|
103
|
-
end
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
# wallet api
|
|
107
|
-
|
|
108
|
-
# create wallet
|
|
109
|
-
def createwallet(wallet_id = 1, wallet_path_prefix = Tapyrus::Wallet::Base.default_path_prefix)
|
|
110
|
-
wallet = Tapyrus::Wallet::Base.create(wallet_id, wallet_path_prefix)
|
|
111
|
-
node.wallet = wallet unless node.wallet
|
|
112
|
-
{ wallet_id: wallet.wallet_id, mnemonic: wallet.master_key.mnemonic }
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
# get wallet list.
|
|
116
|
-
def listwallets(wallet_path_prefix = Tapyrus::Wallet::Base.default_path_prefix)
|
|
117
|
-
Tapyrus::Wallet::Base.wallet_paths(wallet_path_prefix)
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
# get current wallet information.
|
|
121
|
-
def getwalletinfo
|
|
122
|
-
node.wallet ? node.wallet.to_h : {}
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
# get the list of current Wallet accounts.
|
|
126
|
-
def listaccounts
|
|
127
|
-
return {} unless node.wallet
|
|
128
|
-
accounts = {}
|
|
129
|
-
node.wallet.accounts.each { |a| accounts[a.name] = node.wallet.get_balance(a) }
|
|
130
|
-
accounts
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
# encrypt wallet.
|
|
134
|
-
def encryptwallet(passphrase)
|
|
135
|
-
return nil unless node.wallet
|
|
136
|
-
node.wallet.encrypt(passphrase)
|
|
137
|
-
"The wallet 'wallet_id: #{node.wallet.wallet_id}' has been encrypted."
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
# create new tapyrus address for receiving payments.
|
|
141
|
-
def getnewaddress(account_name)
|
|
142
|
-
node.wallet.generate_new_address(account_name)
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
end
|
|
146
|
-
end
|