sibit 0.31.0 → 0.32.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7d8d002bcff6bcb41fc9dcd1a4a4ebd2d622460a23777e36da39406e4de8684
4
- data.tar.gz: 91d217414aec0258cd458dbbae530738e2efc211f3d259888413a0532554d203
3
+ metadata.gz: c756b3d04936258f2bfa46b7a385fbb3f5f9ca696728dc2895504b1f5691e65c
4
+ data.tar.gz: 3d88e0ab75c369c989dfaf360a4b332ac0d5e7479f8e9657734d710e58b712ee
5
5
  SHA512:
6
- metadata.gz: '099247f1d77cdb32d500e5798f7480b03ce48279d198c059e410f35ca9bb1fc345ee5b6f8342aa8b4ac9982079c9ec0848a26ae8201bb35af04f9bed67f9d2bc'
7
- data.tar.gz: 16c9a7c880dc2bb401383bb5e0593fdfcafcdc7ebd77cfe19a7bc0509be14f8e7946b0a663bcd3e71826e60d93296533104a1d094bd25fb0955a1bdc181f6442
6
+ metadata.gz: 4c385350b69515c9a92a32901afd99685dca40cfc38f85be613e7527421b29481a838eceb6cc21ec1bdce70deee2b8d295806118c8039f7bb78a3fd80547ac70
7
+ data.tar.gz: acba7a669a53cbb92918751723e9d09a690b1f4e927f275d97633d9e538317bd4c00a355326ee0aa0fbd11a873a41fdf721570f32d73d56ef40fdc489d132b10
data/REUSE.toml CHANGED
@@ -6,6 +6,7 @@ version = 1
6
6
  path = [
7
7
  ".DS_Store",
8
8
  ".gitattributes",
9
+ ".gitleaksignore",
9
10
  ".gitignore",
10
11
  ".pdd",
11
12
  "**.json",
data/bin/sibit CHANGED
@@ -24,6 +24,10 @@ require_relative '../lib/sibit/fake'
24
24
  require_relative '../lib/sibit/firstof'
25
25
  require_relative '../lib/sibit/version'
26
26
 
27
+ VERBOSE = !ARGV.include?('--quiet') &&
28
+ (ARGV.include?('--verbose') || ENV.fetch('SIBIT_VERBOSE', nil))
29
+ LOG = VERBOSE ? Loog::VERBOSE : Loog::REGULAR
30
+
27
31
  # Command-line interface for Sibit.
28
32
  # Provides commands to interact with the Bitcoin network.
29
33
  class Bin < Thor
@@ -33,6 +37,7 @@ class Bin < Thor
33
37
  class_option :dry, type: :boolean, default: false,
34
38
  desc: "Don't send a real payment, run in a read-only mode"
35
39
  class_option :verbose, type: :boolean, default: false, desc: 'Print all possible debug messages'
40
+ class_option :quiet, type: :boolean, default: false, desc: 'Print only informative messages'
36
41
  class_option :api, type: :array, default: %w[blockchain btc bitcoinchain blockchair cex],
37
42
  desc: 'Ordered List of APIs to use, e.g. "blockchain,btc,bitcoinchain"'
38
43
 
@@ -47,7 +52,7 @@ class Bin < Thor
47
52
 
48
53
  desc 'price', 'Get current price of BTC in USD'
49
54
  def price
50
- log.info(client.price)
55
+ LOG.info(client.price)
51
56
  end
52
57
 
53
58
  desc 'fees', 'Get currently recommended transaction fees'
@@ -59,39 +64,45 @@ class Bin < Thor
59
64
  usd = sat * sibit.price / 100_000_000
60
65
  "#{m}: #{sat}sat / $#{format('%<usd>.02f', usd: usd)}"
61
66
  end.join("\n")
62
- log.info(text)
67
+ LOG.info(text)
63
68
  end
64
69
 
65
70
  desc 'latest', 'Get hash of the latest block'
66
71
  def latest
67
- log.info(client.latest)
72
+ LOG.info(client.latest)
68
73
  end
69
74
 
70
75
  desc 'generate', 'Generate a new private key'
71
76
  def generate
72
- log.info(client.generate)
77
+ LOG.info(client.generate)
73
78
  end
74
79
 
75
80
  desc 'create KEY', 'Create a public Bitcoin address from the private key'
76
81
  def create(key)
77
- log.debug("Private key provided: #{key.ellipsized(8).inspect}")
78
- log.info(client.create(key))
82
+ LOG.debug("Private key provided: #{key.ellipsized(8).inspect}")
83
+ LOG.info(client.create(key))
79
84
  end
80
85
 
81
86
  desc 'balance ADDRESS', 'Check the balance of the Bitcoin address'
82
87
  def balance(address)
83
- log.info(client.balance(address))
88
+ LOG.info(client.balance(address))
84
89
  end
85
90
 
86
- desc 'pay AMOUNT FEE SOURCES TARGET CHANGE', 'Send a new Bitcoin transaction'
91
+ desc 'pay AMOUNT FEE SOURCES TARGET CHANGE',
92
+ 'Send a new Bitcoin transaction (AMOUNT can be "MAX" to use full balance)'
87
93
  option :skip_utxo, type: :array, default: [],
88
94
  desc: 'List of UTXO that must be skipped while paying'
89
95
  option :yes, type: :boolean, default: false,
90
96
  desc: 'Skip confirmation prompt and send the payment immediately'
91
97
  def pay(amount, fee, sources, target, change)
92
- amount = amount.to_i if /^[0-9]+$/.match?(amount)
98
+ keys = sources.split(',')
99
+ if amount.upcase == 'MAX'
100
+ addrs = keys.map { |k| Sibit::Key.new(k).bech32 }
101
+ amount = addrs.sum { |a| client.balance(a) }
102
+ end
103
+ amount = amount.to_i if amount.is_a?(String) && /^[0-9]+$/.match?(amount)
93
104
  fee = fee.to_i if /^[0-9]+$/.match?(fee)
94
- args = [amount, fee, sources.split(','), target, change]
105
+ args = [amount, fee, keys, target, change]
95
106
  kwargs = { skip_utxo: options[:skip_utxo] }
96
107
  unless options[:yes] || options[:dry]
97
108
  client(dry: true).pay(*args, **kwargs)
@@ -99,55 +110,54 @@ class Bin < Thor
99
110
  answer = $stdin.gets&.strip&.downcase
100
111
  raise Sibit::Error, 'Payment cancelled by user' unless answer == 'yes'
101
112
  end
102
- log.info(client.pay(*args, **kwargs))
113
+ LOG.info(client.pay(*args, **kwargs))
103
114
  end
104
115
 
105
116
  desc 'version', 'Print program version'
106
117
  def version
107
- log.info(Sibit::VERSION)
118
+ LOG.info(Sibit::VERSION)
108
119
  end
109
120
 
110
121
  private
111
122
 
112
- def log
113
- @log ||= options[:verbose] ? Loog::VERBOSE : Loog::REGULAR
114
- end
115
-
116
123
  def client(dry: false)
117
- http = options[:proxy] ? Sibit::HttpProxy.new(options[:proxy]) : Sibit::Http.new
124
+ proxy = options[:proxy] || ENV.fetch('SIBIT_PROXY', nil)
125
+ http = proxy ? Sibit::HttpProxy.new(proxy) : Sibit::Http.new
126
+ LOG.debug("Using proxy at #{http.host}") if proxy
118
127
  apis = options[:api].flat_map { |a| a.split(',') }.map(&:downcase).map do |a|
119
128
  case a
120
129
  when 'blockchain'
121
- Sibit::Blockchain.new(http: http, log: log)
130
+ Sibit::Blockchain.new(http: http, log: LOG)
122
131
  when 'btc'
123
- Sibit::Btc.new(http: http, log: log)
132
+ Sibit::Btc.new(http: http, log: LOG)
124
133
  when 'bitcoinchain'
125
- Sibit::Bitcoinchain.new(http: http, log: log)
134
+ Sibit::Bitcoinchain.new(http: http, log: LOG)
126
135
  when 'blockchair'
127
- Sibit::Blockchair.new(http: http, log: log)
136
+ Sibit::Blockchair.new(http: http, log: LOG)
128
137
  when 'cex'
129
- Sibit::Cex.new(http: http, log: log)
138
+ Sibit::Cex.new(http: http, log: LOG)
130
139
  when 'fake'
131
140
  Sibit::Fake.new
132
141
  else
133
142
  raise Sibit::Error, "Unknown API \"#{a}\""
134
143
  end
135
144
  end
136
- api = Sibit::FirstOf.new(apis, log: log, verbose: true)
137
- api = Sibit::Dry.new(api, log: log) if options[:dry] || dry
145
+ api = Sibit::FirstOf.new(apis, log: LOG, verbose: true)
146
+ api = Sibit::Dry.new(api, log: LOG) if options[:dry] || dry
138
147
  api = RetriableProxy.for_object(api, on: Sibit::Error) if options[:attempts] > 1
139
- Sibit.new(log: log, api: api)
148
+ Sibit.new(log: LOG, api: api)
140
149
  end
141
150
  end
142
151
 
143
- begin
144
- Bin.start(ARGV)
145
- rescue StandardError => e
146
- log = ARGV.include?('--verbose') ? Loog::VERBOSE : Loog::REGULAR
147
- if ARGV.include?('--verbose')
148
- log.error(Backtrace.new(e))
149
- else
150
- log.error(e.message)
152
+ if __FILE__ == $PROGRAM_NAME
153
+ begin
154
+ Bin.start(ARGV)
155
+ rescue StandardError => e
156
+ if VERBOSE
157
+ LOG.error(Backtrace.new(e))
158
+ else
159
+ LOG.error(e.message)
160
+ end
161
+ exit(255)
151
162
  end
152
- exit(255)
153
163
  end
@@ -19,6 +19,8 @@ class Sibit
19
19
  @user, @password, @host, @port = parse(addr)
20
20
  end
21
21
 
22
+ attr_reader :host
23
+
22
24
  def client(uri)
23
25
  http = Net::HTTP.new(uri.host, uri.port, @host, @port.to_i, @user, @password)
24
26
  http.use_ssl = true
data/lib/sibit/version.rb CHANGED
@@ -9,5 +9,5 @@
9
9
  # License:: MIT
10
10
  class Sibit
11
11
  # Current version of the library.
12
- VERSION = '0.31.0' unless defined?(VERSION)
12
+ VERSION = '0.32.1' unless defined?(VERSION)
13
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sibit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.31.0
4
+ version: 0.32.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko