bitcoinrb 0.3.2 → 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/README.md +3 -3
- data/bitcoinrb.gemspec +1 -1
- data/lib/bitcoin.rb +1 -0
- data/lib/bitcoin/chain_params.rb +0 -8
- data/lib/bitcoin/chainparams/regtest.yml +1 -1
- data/lib/bitcoin/chainparams/testnet.yml +1 -1
- data/lib/bitcoin/constants.rb +0 -7
- data/lib/bitcoin/ext_key.rb +12 -1
- data/lib/bitcoin/key.rb +3 -3
- data/lib/bitcoin/rpc/bitcoin_core_client.rb +1 -1
- data/lib/bitcoin/rpc/request_handler.rb +1 -1
- data/lib/bitcoin/script/script.rb +11 -0
- data/lib/bitcoin/script/script_interpreter.rb +0 -1
- data/lib/bitcoin/tx.rb +3 -3
- data/lib/bitcoin/version.rb +1 -1
- metadata +16 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34ec1d9b1b20094213a29e9df2a47b901bdef1d537323446230b6ff2f3420a50
|
4
|
+
data.tar.gz: 5591f08d5909d1b18b60b091a41e4f6638d424ea0512dbd270001d8e9555e633
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e1a4df59cb46fa6d35dab5dc3d83dac58aaf6b8f39ae2ccc782c10ce06821000a1e31732887b0019b2ac62f0f4d7e831d71a0a9ad8964868b24c168862b963a
|
7
|
+
data.tar.gz: 9f374d157b4c8814c771697365ffded665a17b36579130604fd8c85375e05fbaa41f8212006ef037f5d2442af67a6130a1d5400d5019b9685404b433de1dff77
|
data/README.md
CHANGED
@@ -9,9 +9,9 @@ NOTE: Bitcoinrb work in progress, and there is a possibility of incompatible cha
|
|
9
9
|
|
10
10
|
Bitcoinrb supports following feature:
|
11
11
|
|
12
|
-
* Bitcoin script interpreter(including [BIP-65](https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki), [BIP-68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki), [BIP-112](https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki))
|
13
|
-
* De/serialization of Bitcoin protocol network messages
|
14
|
-
* De/serialization of blocks and transactions
|
12
|
+
* [Bitcoin script interpreter](https://github.com/chaintope/bitcoinrb/wiki/Script)(including [BIP-65](https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki), [BIP-68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki), [BIP-112](https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki))
|
13
|
+
* [De/serialization of Bitcoin protocol network messages](https://github.com/chaintope/bitcoinrb/wiki/P2P-Message)
|
14
|
+
* De/serialization of blocks and [transactions](https://github.com/chaintope/bitcoinrb/wiki/Transaction)
|
15
15
|
* Key generation and verification for ECDSA, including [BIP-32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki) and [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) supports.
|
16
16
|
* ECDSA signature(RFC6979 -Deterministic ECDSA, LOW-S, LOW-R support)
|
17
17
|
* Segwit support (parsing segwit payload, Bech32 address, sign for segwit tx, [BIP-141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki), [BIP-143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki), [BIP-144](https://github.com/bitcoin/bips/blob/master/bip-0144.mediawiki))
|
data/bitcoinrb.gemspec
CHANGED
@@ -34,7 +34,6 @@ Gem::Specification.new do |spec|
|
|
34
34
|
spec.add_runtime_dependency 'siphash'
|
35
35
|
spec.add_runtime_dependency 'protobuf', '3.8.5'
|
36
36
|
spec.add_runtime_dependency 'scrypt'
|
37
|
-
spec.add_runtime_dependency 'activesupport', '~> 5.2.3'
|
38
37
|
|
39
38
|
# for options
|
40
39
|
spec.add_development_dependency 'leveldb-native'
|
@@ -43,5 +42,6 @@ Gem::Specification.new do |spec|
|
|
43
42
|
spec.add_development_dependency 'rake', '~> 10.0'
|
44
43
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
45
44
|
spec.add_development_dependency 'timecop'
|
45
|
+
spec.add_development_dependency 'webmock', '~> 3.0'
|
46
46
|
|
47
47
|
end
|
data/lib/bitcoin.rb
CHANGED
data/lib/bitcoin/chain_params.rb
CHANGED
@@ -36,9 +36,6 @@ module Bitcoin
|
|
36
36
|
|
37
37
|
attr_accessor :dust_relay_fee
|
38
38
|
|
39
|
-
# fork coin id.
|
40
|
-
attr_accessor :fork_id
|
41
|
-
|
42
39
|
# mainnet genesis
|
43
40
|
def self.mainnet
|
44
41
|
init('mainnet')
|
@@ -73,11 +70,6 @@ module Bitcoin
|
|
73
70
|
Bitcoin::Block.new(header)
|
74
71
|
end
|
75
72
|
|
76
|
-
# whether fork coin.
|
77
|
-
def fork_chain?
|
78
|
-
!fork_id.nil?
|
79
|
-
end
|
80
|
-
|
81
73
|
def self.init(name)
|
82
74
|
i = YAML.load(File.open("#{__dir__}/chainparams/#{name}.yml"))
|
83
75
|
i.dust_relay_fee ||= Bitcoin::DUST_RELAY_TX_FEE
|
@@ -9,7 +9,7 @@ privkey_version: "ef"
|
|
9
9
|
extended_privkey_version: "04358394"
|
10
10
|
extended_pubkey_version: "043587cf"
|
11
11
|
bip49_pubkey_p2wpkh_p2sh_version: "044a5262"
|
12
|
-
bip49_pubkey_p2wsh_p2sh_version: "
|
12
|
+
bip49_pubkey_p2wsh_p2sh_version: "024289ef"
|
13
13
|
bip49_privkey_p2wpkh_p2sh_version: "044a4e28"
|
14
14
|
bip49_privkey_p2wsh_p2sh_version: "024285b5"
|
15
15
|
bip84_pubkey_p2wpkh_version: "045f1cf6"
|
@@ -9,7 +9,7 @@ privkey_version: "ef"
|
|
9
9
|
extended_privkey_version: "04358394"
|
10
10
|
extended_pubkey_version: "043587cf"
|
11
11
|
bip49_pubkey_p2wpkh_p2sh_version: "044a5262"
|
12
|
-
bip49_pubkey_p2wsh_p2sh_version: "
|
12
|
+
bip49_pubkey_p2wsh_p2sh_version: "024289ef"
|
13
13
|
bip49_privkey_p2wpkh_p2sh_version: "044a4e28"
|
14
14
|
bip49_privkey_p2wsh_p2sh_version: "024285b5"
|
15
15
|
bip84_pubkey_p2wpkh_version: "045f1cf6"
|
data/lib/bitcoin/constants.rb
CHANGED
@@ -91,13 +91,6 @@ module Bitcoin
|
|
91
91
|
# Signature hash types/flags
|
92
92
|
SIGHASH_TYPE = { all: 1, none: 2, single: 3, anyonecanpay: 128 }
|
93
93
|
|
94
|
-
# SIGHASH_FORK_ID for replay protection of the fork coin
|
95
|
-
SIGHASH_FORK_ID = 0x40
|
96
|
-
|
97
|
-
# fork coin id.
|
98
|
-
FORK_ID_CASH = 0
|
99
|
-
FORK_ID_GOLD = 79
|
100
|
-
|
101
94
|
# Maximum number length in bytes
|
102
95
|
DEFAULT_MAX_NUM_SIZE = 4
|
103
96
|
|
data/lib/bitcoin/ext_key.rb
CHANGED
@@ -6,6 +6,9 @@ module Bitcoin
|
|
6
6
|
# BIP32 Extended private key
|
7
7
|
class ExtKey
|
8
8
|
|
9
|
+
MAX_DEPTH = 255
|
10
|
+
MASTER_FINGERPRINT = '00000000'
|
11
|
+
|
9
12
|
attr_accessor :ver
|
10
13
|
attr_accessor :depth
|
11
14
|
attr_accessor :number
|
@@ -18,7 +21,7 @@ module Bitcoin
|
|
18
21
|
def self.generate_master(seed)
|
19
22
|
ext_key = ExtKey.new
|
20
23
|
ext_key.depth = ext_key.number = 0
|
21
|
-
ext_key.parent_fingerprint =
|
24
|
+
ext_key.parent_fingerprint = MASTER_FINGERPRINT
|
22
25
|
l = Bitcoin.hmac_sha512('Bitcoin seed', seed.htb)
|
23
26
|
left = l[0..31].bth.to_i(16)
|
24
27
|
raise 'invalid key' if left >= CURVE_ORDER || left == 0
|
@@ -94,6 +97,7 @@ module Bitcoin
|
|
94
97
|
number += Bitcoin::HARDENED_THRESHOLD if harden
|
95
98
|
new_key = ExtKey.new
|
96
99
|
new_key.depth = depth + 1
|
100
|
+
raise IndexError, 'Depth over 255.' if new_key.depth > MAX_DEPTH
|
97
101
|
new_key.number = number
|
98
102
|
new_key.parent_fingerprint = fingerprint
|
99
103
|
if number > (Bitcoin::HARDENED_THRESHOLD - 1)
|
@@ -143,6 +147,9 @@ module Bitcoin
|
|
143
147
|
raise 'An unsupported version byte was specified.' unless ExtKey.support_version?(ext_key.ver)
|
144
148
|
ext_key.depth = buf.read(1).unpack('C').first
|
145
149
|
ext_key.parent_fingerprint = buf.read(4).bth
|
150
|
+
if ext_key.depth == 0
|
151
|
+
raise ArgumentError, 'Invalid parent fingerprint.' unless ext_key.parent_fingerprint == MASTER_FINGERPRINT
|
152
|
+
end
|
146
153
|
ext_key.number = buf.read(4).unpack('N').first
|
147
154
|
ext_key.chain_code = buf.read(32)
|
148
155
|
buf.read(1) # 0x00
|
@@ -256,6 +263,7 @@ module Bitcoin
|
|
256
263
|
def derive(number)
|
257
264
|
new_key = ExtPubkey.new
|
258
265
|
new_key.depth = depth + 1
|
266
|
+
raise IndexError, 'Depth over 255.' if new_key.depth > Bitcoin::ExtKey::MAX_DEPTH
|
259
267
|
new_key.number = number
|
260
268
|
new_key.parent_fingerprint = fingerprint
|
261
269
|
raise 'hardened key is not support' if number > (Bitcoin::HARDENED_THRESHOLD - 1)
|
@@ -301,6 +309,9 @@ module Bitcoin
|
|
301
309
|
raise 'An unsupported version byte was specified.' unless ExtPubkey.support_version?(ext_pubkey.ver)
|
302
310
|
ext_pubkey.depth = buf.read(1).unpack('C').first
|
303
311
|
ext_pubkey.parent_fingerprint = buf.read(4).bth
|
312
|
+
if ext_pubkey.depth == 0
|
313
|
+
raise ArgumentError, 'Invalid parent fingerprint.' unless ext_pubkey.parent_fingerprint == ExtKey::MASTER_FINGERPRINT
|
314
|
+
end
|
304
315
|
ext_pubkey.number = buf.read(4).unpack('N').first
|
305
316
|
ext_pubkey.chain_code = buf.read(32)
|
306
317
|
ext_pubkey.pubkey = buf.read(33).bth
|
data/lib/bitcoin/key.rb
CHANGED
@@ -125,19 +125,19 @@ module Bitcoin
|
|
125
125
|
# get pay to pubkey hash address
|
126
126
|
# @deprecated
|
127
127
|
def to_p2pkh
|
128
|
-
Bitcoin::Script.to_p2pkh(hash160).
|
128
|
+
Bitcoin::Script.to_p2pkh(hash160).to_addr
|
129
129
|
end
|
130
130
|
|
131
131
|
# get pay to witness pubkey hash address
|
132
132
|
# @deprecated
|
133
133
|
def to_p2wpkh
|
134
|
-
Bitcoin::Script.to_p2wpkh(hash160).
|
134
|
+
Bitcoin::Script.to_p2wpkh(hash160).to_addr
|
135
135
|
end
|
136
136
|
|
137
137
|
# get p2wpkh address nested in p2sh.
|
138
138
|
# @deprecated
|
139
139
|
def to_nested_p2wpkh
|
140
|
-
Bitcoin::Script.to_p2wpkh(hash160).to_p2sh.
|
140
|
+
Bitcoin::Script.to_p2wpkh(hash160).to_p2sh.to_addr
|
141
141
|
end
|
142
142
|
|
143
143
|
def compressed?
|
@@ -56,7 +56,7 @@ module Bitcoin
|
|
56
56
|
post(server_url, @config[:timeout], @config[:open_timeout], data.to_json, content_type: :json) do |respdata, request, result|
|
57
57
|
raise result.message if !result.kind_of?(Net::HTTPSuccess) && respdata.empty?
|
58
58
|
response = JSON.parse(respdata.gsub(/\\u([\da-fA-F]{4})/) { [$1].pack('H*').unpack('n*').pack('U*').encode('ISO-8859-1').force_encoding('UTF-8') })
|
59
|
-
raise response['error'] if response['error']
|
59
|
+
raise response['error'].to_s if response['error']
|
60
60
|
response['result']
|
61
61
|
end
|
62
62
|
end
|
@@ -96,7 +96,7 @@ module Bitcoin
|
|
96
96
|
script = Bitcoin::Script.parse_from_payload(hex_script.htb)
|
97
97
|
h = script.to_h
|
98
98
|
h.delete(:hex)
|
99
|
-
h[:p2sh] = script.to_p2sh.
|
99
|
+
h[:p2sh] = script.to_p2sh.to_addr unless script.p2sh?
|
100
100
|
h
|
101
101
|
rescue Exception
|
102
102
|
raise ArgumentError.new('Script decode failed')
|
@@ -148,7 +148,9 @@ module Bitcoin
|
|
148
148
|
chunks.size == 0
|
149
149
|
end
|
150
150
|
|
151
|
+
# @deprecated
|
151
152
|
def addresses
|
153
|
+
puts "WARNING: Bitcoin::Script#addresses is deprecated. Use Bitcoin::Script#to_addr instead."
|
152
154
|
return [p2pkh_addr] if p2pkh?
|
153
155
|
return [p2sh_addr] if p2sh?
|
154
156
|
return [bech32_addr] if witness_program?
|
@@ -156,6 +158,15 @@ module Bitcoin
|
|
156
158
|
[]
|
157
159
|
end
|
158
160
|
|
161
|
+
# convert to address
|
162
|
+
# @return [String] if script type is p2pkh or p2sh or witness program, return address, otherwise nil.
|
163
|
+
def to_addr
|
164
|
+
return p2pkh_addr if p2pkh?
|
165
|
+
return p2sh_addr if p2sh?
|
166
|
+
return bech32_addr if witness_program?
|
167
|
+
nil
|
168
|
+
end
|
169
|
+
|
159
170
|
# check whether standard script.
|
160
171
|
def standard?
|
161
172
|
p2pkh? | p2sh? | p2wpkh? | p2wsh? | multisig? | standard_op_return?
|
@@ -609,7 +609,6 @@ module Bitcoin
|
|
609
609
|
return false if sig.empty?
|
610
610
|
s = sig.unpack('C*')
|
611
611
|
hash_type = s[-1] & (~(SIGHASH_TYPE[:anyonecanpay]))
|
612
|
-
hash_type &= (~(Bitcoin::SIGHASH_FORK_ID)) if Bitcoin.chain_params.fork_chain? # for fork coin.
|
613
612
|
return false if hash_type < SIGHASH_TYPE[:all] || hash_type > SIGHASH_TYPE[:single]
|
614
613
|
true
|
615
614
|
end
|
data/lib/bitcoin/tx.rb
CHANGED
@@ -202,7 +202,7 @@ module Bitcoin
|
|
202
202
|
raise ArgumentError, 'script_pubkey must be specified.' unless output_script
|
203
203
|
raise ArgumentError, 'unsupported sig version specified.' unless SIG_VERSION.include?(sig_version)
|
204
204
|
|
205
|
-
if sig_version == :witness_v0
|
205
|
+
if sig_version == :witness_v0
|
206
206
|
raise ArgumentError, 'amount must be specified.' unless amount
|
207
207
|
sighash_for_witness(input_index, output_script, hash_type, amount, skip_separator_index)
|
208
208
|
else
|
@@ -225,7 +225,7 @@ module Bitcoin
|
|
225
225
|
script_pubkey = redeem_script if redeem_script.p2wpkh?
|
226
226
|
end
|
227
227
|
|
228
|
-
if has_witness
|
228
|
+
if has_witness
|
229
229
|
verify_input_sig_for_witness(input_index, script_pubkey, amount, flags)
|
230
230
|
else
|
231
231
|
verify_input_sig_for_legacy(input_index, script_pubkey, flags)
|
@@ -310,7 +310,7 @@ module Bitcoin
|
|
310
310
|
if (hash_type & SIGHASH_TYPE[:anyonecanpay]) != 0
|
311
311
|
hash_prevouts = hash_sequence ="\x00".ljust(32, "\x00")
|
312
312
|
end
|
313
|
-
|
313
|
+
|
314
314
|
buf = [ [version].pack('V'), hash_prevouts, hash_sequence, outpoint,
|
315
315
|
script_code ,amount, nsequence, hash_outputs, [@lock_time, hash_type].pack('VV')].join
|
316
316
|
Bitcoin.double_sha256(buf)
|
data/lib/bitcoin/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitcoinrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- azuchi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ecdsa
|
@@ -206,20 +206,6 @@ dependencies:
|
|
206
206
|
- - ">="
|
207
207
|
- !ruby/object:Gem::Version
|
208
208
|
version: '0'
|
209
|
-
- !ruby/object:Gem::Dependency
|
210
|
-
name: activesupport
|
211
|
-
requirement: !ruby/object:Gem::Requirement
|
212
|
-
requirements:
|
213
|
-
- - "~>"
|
214
|
-
- !ruby/object:Gem::Version
|
215
|
-
version: 5.2.3
|
216
|
-
type: :runtime
|
217
|
-
prerelease: false
|
218
|
-
version_requirements: !ruby/object:Gem::Requirement
|
219
|
-
requirements:
|
220
|
-
- - "~>"
|
221
|
-
- !ruby/object:Gem::Version
|
222
|
-
version: 5.2.3
|
223
209
|
- !ruby/object:Gem::Dependency
|
224
210
|
name: leveldb-native
|
225
211
|
requirement: !ruby/object:Gem::Requirement
|
@@ -290,6 +276,20 @@ dependencies:
|
|
290
276
|
- - ">="
|
291
277
|
- !ruby/object:Gem::Version
|
292
278
|
version: '0'
|
279
|
+
- !ruby/object:Gem::Dependency
|
280
|
+
name: webmock
|
281
|
+
requirement: !ruby/object:Gem::Requirement
|
282
|
+
requirements:
|
283
|
+
- - "~>"
|
284
|
+
- !ruby/object:Gem::Version
|
285
|
+
version: '3.0'
|
286
|
+
type: :development
|
287
|
+
prerelease: false
|
288
|
+
version_requirements: !ruby/object:Gem::Requirement
|
289
|
+
requirements:
|
290
|
+
- - "~>"
|
291
|
+
- !ruby/object:Gem::Version
|
292
|
+
version: '3.0'
|
293
293
|
description: "[WIP]The implementation of Bitcoin Protocol for Ruby."
|
294
294
|
email:
|
295
295
|
- azuchi@chaintope.com
|