sibit 0.32.2 → 0.32.4

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: 5b5729226952c9b63bbe126e8f4471de9a69b5fe5cdd1735500901a39d957c43
4
- data.tar.gz: 4c9fac6187bbb379fd4874e81abb26e23073878a73f6877d92566d89d666b231
3
+ metadata.gz: addf1c76d3af290c3cdbdaae049db0e9f81fab3b71aed128cd8d284b4b3377e5
4
+ data.tar.gz: d4654c29684e00847a317bf7203a295f018036438a3c64ac93ba6477c84e1a52
5
5
  SHA512:
6
- metadata.gz: ad897361c466fef868dc4d526dbf5fc154d0d39039be448c34dceaef22e0622a08f8e2f2f6e98f9f8192a8e4f3955e92629c9b0bf6fabc71c5e748a4255a2c88
7
- data.tar.gz: 3f9138f939d908f8c65f07fb10a70e9c02c900b8cc0a6edf827187ea59b97394a1b055e3baf4a539cb798e3662ec9bf4b680550159c3272c1758e04118be0443
6
+ metadata.gz: 4405e4c542199ea6a4e99e401cb33321ef6048a8d3e7ff424c99f31718d17e8d03928f9922afd80b02e33f1a447a6e7c0042c038226e50078a35b5c237e16801
7
+ data.tar.gz: 235aa484a4719cdab76cbff47da26b949b25513990b88b12096c8d7081bde1c8bb3331808e144e34b3f2302f1fe462896aff5a60d3a0fcc9a955d9525e58db57
data/lib/sibit/bin.rb CHANGED
@@ -22,13 +22,13 @@ class Sibit
22
22
  class Bin < Thor
23
23
  class_option :proxy, type: :string, desc: 'HTTPS proxy for all requests, e.g. "localhost:3128"'
24
24
  class_option :attempts, type: :numeric, default: 1,
25
- desc: 'How many times should we try before failing'
25
+ desc: 'How many times should we try before failing'
26
26
  class_option :dry, type: :boolean, default: false,
27
- desc: "Don't send a real payment, run in a read-only mode"
27
+ desc: "Don't send a real payment, run in a read-only mode"
28
28
  class_option :verbose, type: :boolean, default: false, desc: 'Print all possible debug messages'
29
29
  class_option :quiet, type: :boolean, default: false, desc: 'Print only informative messages'
30
30
  class_option :api, type: :array, default: %w[blockchain btc bitcoinchain blockchair cex],
31
- desc: 'Ordered List of APIs to use, e.g. "blockchain,btc,bitcoinchain"'
31
+ desc: 'Ordered List of APIs to use, e.g. "blockchain,btc,bitcoinchain"'
32
32
 
33
33
  def self.exit_on_failure?
34
34
  true
@@ -77,22 +77,28 @@ class Sibit
77
77
  log.info(client.balance(address))
78
78
  end
79
79
 
80
- desc 'pay AMOUNT FEE SOURCES TARGET CHANGE',
81
- 'Send a new Bitcoin transaction (AMOUNT can be "MAX" to use full balance)'
80
+ desc \
81
+ 'pay AMOUNT FEE SOURCES TARGET CHANGE',
82
+ 'Send a new Bitcoin transaction (AMOUNT can be "MAX" to use full balance)'
82
83
  option :skip_utxo, type: :array, default: [],
83
- desc: 'List of UTXO that must be skipped while paying'
84
+ desc: 'List of UTXO that must be skipped while paying'
85
+ option :base58, type: :boolean, default: false,
86
+ desc: 'Convert private addresses to public in base58'
84
87
  option :yes, type: :boolean, default: false,
85
- desc: 'Skip confirmation prompt and send the payment immediately'
88
+ desc: 'Skip confirmation prompt and send the payment immediately'
86
89
  def pay(amount, fee, sources, target, change)
87
90
  keys = sources.split(',')
88
91
  if amount.upcase == 'MAX'
89
- addrs = keys.map { |k| Sibit::Key.new(k).bech32 }
92
+ addrs = keys.map do |k|
93
+ kk = Sibit::Key.new(k)
94
+ options[:base58] ? kk.base58 : kk.bech32
95
+ end
90
96
  amount = addrs.sum { |a| client.balance(a) }
91
97
  end
92
98
  amount = amount.to_i if amount.is_a?(String) && /^[0-9]+$/.match?(amount)
93
99
  fee = fee.to_i if /^[0-9]+$/.match?(fee)
94
100
  args = [amount, fee, keys, target, change]
95
- kwargs = { skip_utxo: options[:skip_utxo] }
101
+ kwargs = { skip_utxo: options[:skip_utxo], base58: options[:base58] }
96
102
  unless options[:yes] || options[:dry]
97
103
  client(dry: true).pay(*args, **kwargs)
98
104
  print 'Do you confirm this payment? (yes/no): '
data/lib/sibit/key.rb CHANGED
@@ -22,12 +22,23 @@ class Sibit
22
22
  MIN_PRIV = 0x01
23
23
  MAX_PRIV = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140
24
24
 
25
+ SECP256K1_N = OpenSSL::BN.new(
26
+ 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16
27
+ )
28
+
25
29
  attr_reader :network
26
30
 
27
31
  def self.generate(network: :mainnet)
28
32
  key = OpenSSL::PKey::EC.generate('secp256k1')
29
- pvt = key.private_key.to_s(16).rjust(64, '0').downcase
30
- new(pvt, network: network)
33
+ pvt = key.private_key
34
+ raise 'Invalid private key: zero' if pvt.zero?
35
+ raise 'Invalid private key: out of range' if pvt >= SECP256K1_N
36
+ raise 'Invalid public key: not on curve' unless key.public_key.on_curve?
37
+ hex = key.private_key.to_s(16).rjust(64, '0').downcase
38
+ raise 'Invalid private key encoding' unless hex.match?(/\A[0-9a-f]{64}\z/)
39
+ trip = OpenSSL::BN.new(hex, 16)
40
+ raise 'Private key serialization is lossy' unless trip == pvt
41
+ new(hex, network: network)
31
42
  end
32
43
 
33
44
  def initialize(privkey, network: nil)
@@ -49,11 +60,17 @@ class Sibit
49
60
 
50
61
  def bech32
51
62
  hrp = { mainnet: 'bc', testnet: 'tb', regtest: 'bcrt' }[@network]
52
- Bech32.encode(hrp, 0, hash160(pub))
63
+ hex = pub
64
+ raise 'Invalid public key: not on curve' unless @key.public_key.on_curve?
65
+ raise 'Invalid public key format' unless hex.match?(/\A0[23][0-9a-f]{64}\z/)
66
+ Bech32.encode(hrp, 0, hash160(hex))
53
67
  end
54
68
 
55
69
  def base58
56
- hash = hash160(pub)
70
+ hex = pub
71
+ raise 'Invalid public key: not on curve' unless @key.public_key.on_curve?
72
+ raise 'Invalid public key format' unless hex.match?(/\A0[23][0-9a-f]{64}\z/)
73
+ hash = hash160(hex)
57
74
  prefix = @network == :mainnet ? '00' : '6f'
58
75
  versioned = "#{prefix}#{hash}"
59
76
  checksum = Base58.new(versioned).check
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.32.2' unless defined?(VERSION)
12
+ VERSION = '0.32.4' unless defined?(VERSION)
13
13
  end
data/lib/sibit.rb CHANGED
@@ -103,18 +103,26 @@ class Sibit
103
103
  # +target+: the target address to send to
104
104
  # +change+: the address where the change has to be sent to
105
105
  # +network+: optional network override (:mainnet, :testnet, :regtest)
106
- def pay(amount, fee, sources, target, change, skip_utxo: [], network: nil)
106
+ def pay(amount, fee, sources, target, change, skip_utxo: [], network: nil, base58: false)
107
107
  p = price('USD')
108
108
  keys = sources.map { |k| Key.new(k, network: network) }
109
109
  network = keys.first&.network || :mainnet
110
- sources = keys.to_h { |k| [k.bech32, k.priv] }
110
+ sources = keys.to_h do |k|
111
+ pub =
112
+ if base58
113
+ k.base58
114
+ else
115
+ k.bech32
116
+ end
117
+ @log.debug("Private key #{k.priv.ellipsized(8).inspect} is public as #{pub}:")
118
+ [pub, k.priv]
119
+ end
111
120
  satoshi = satoshi(amount)
112
121
  builder = TxBuilder.new
113
122
  unspent = 0
114
123
  size = 100
115
124
  utxos = @api.utxos(sources.keys)
116
- @log.debug("#{utxos.count} UTXOs found, these will be used \
117
- (value/confirmations at tx_hash):")
125
+ @log.debug("#{utxos.count} UTXOs found, these will be used (value/confirmations at tx_hash):")
118
126
  utxos.each do |utxo|
119
127
  if skip_utxo.include?(utxo[:hash])
120
128
  @log.debug("UTXO skipped: #{utxo[:hash]}")
@@ -224,8 +232,10 @@ class Sibit
224
232
  checked += 1
225
233
  end
226
234
  count += 1
227
- @log.debug("We checked #{checked} txns and #{checked_outputs} outputs \
228
- in block #{block} (by #{json[:provider]})")
235
+ @log.debug(
236
+ "Checked #{checked} txns and #{checked_outputs} outputs " \
237
+ "in block #{block} (by #{json[:provider]})"
238
+ )
229
239
  block = json[:next]
230
240
  begin
231
241
  if block.nil?
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.32.2
4
+ version: 0.32.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko