Skip to content

Commit a66677b

Browse files
committed
Fix rubocop offenses for contracthash
1 parent 4b9e8b2 commit a66677b

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

lib/bitcoin/contracthash.rb

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,72 @@
1-
#
2-
# Ruby port of https://github.com/Blockstream/contracthashtool
3-
#
4-
51
module Bitcoin
2+
# Ruby port of https://github.com/Blockstream/contracthashtool
63
module ContractHash
7-
8-
HMAC_DIGEST = OpenSSL::Digest.new("SHA256")
9-
EC_GROUP = OpenSSL::PKey::EC::Group.new("secp256k1")
4+
HMAC_DIGEST = OpenSSL::Digest.new('SHA256')
5+
EC_GROUP = OpenSSL::PKey::EC::Group.new('secp256k1')
106

117
def self.hmac(pubkey, data)
128
OpenSSL::HMAC.hexdigest(HMAC_DIGEST, pubkey, data)
139
end
1410

1511
# generate a contract address
16-
def self.generate(redeem_script_hex, payee_address_or_ascii, nonce_hex=nil)
17-
redeem_script = Bitcoin::Script.new([redeem_script_hex].pack("H*"))
18-
raise "only multisig redeem scripts are currently supported" unless redeem_script.is_multisig?
12+
def self.generate(redeem_script_hex, payee_address_or_ascii, nonce_hex = nil)
13+
redeem_script = Bitcoin::Script.new([redeem_script_hex].pack('H*'))
14+
raise 'only multisig redeem scripts are currently supported' unless redeem_script.is_multisig?
1915
nonce_hex, data = compute_data(payee_address_or_ascii, nonce_hex)
2016

2117
derived_keys = []
2218
redeem_script.get_multisig_pubkeys.each do |pubkey|
2319
tweak = hmac(pubkey, data).to_i(16)
24-
raise "order exceeded, pick a new nonce" if tweak >= EC_GROUP.order.to_i
20+
raise 'order exceeded, pick a new nonce' if tweak >= EC_GROUP.order.to_i
2521
tweak = OpenSSL::BN.new(tweak.to_s)
2622

27-
key = Bitcoin::Key.new(nil, pubkey.unpack("H*")[0])
23+
key = Bitcoin::Key.new(nil, pubkey.unpack('H*')[0])
2824
key = key.instance_variable_get(:@key)
2925
point = EC_GROUP.generator.mul(tweak).ec_add(key.public_key).to_bn.to_i
30-
raise "infinity" if point == 1/0.0
26+
raise 'infinity' if point == 1 / 0.0
3127

3228
key = Bitcoin::Key.new(nil, point.to_s(16))
33-
key.instance_eval{ @pubkey_compressed = true }
29+
key.instance_eval { @pubkey_compressed = true }
3430
derived_keys << key.pub
3531
end
3632

3733
m = redeem_script.get_signatures_required
3834
p2sh_script, redeem_script = Bitcoin::Script.to_p2sh_multisig_script(m, *derived_keys)
3935

40-
[ nonce_hex, redeem_script.unpack("H*")[0], Bitcoin::Script.new(p2sh_script).get_p2sh_address ]
36+
[nonce_hex, redeem_script.unpack('H*')[0], Bitcoin::Script.new(p2sh_script).get_p2sh_address]
4137
end
4238

4339
# claim a contract
4440
def self.claim(private_key_wif, payee_address_or_ascii, nonce_hex)
4541
key = Bitcoin::Key.from_base58(private_key_wif)
4642
data = compute_data(payee_address_or_ascii, nonce_hex)[1]
4743

48-
pubkey = [key.pub].pack("H*")
44+
pubkey = [key.pub].pack('H*')
4945
tweak = hmac(pubkey, data).to_i(16)
50-
raise "order exceeded, verify parameters" if tweak >= EC_GROUP.order.to_i
46+
raise 'order exceeded, verify parameters' if tweak >= EC_GROUP.order.to_i
5147

5248
derived_key = (tweak + key.priv.to_i(16)) % EC_GROUP.order.to_i
53-
raise "zero" if derived_key == 0
49+
raise 'zero' if derived_key.zero?
5450

5551
Bitcoin::Key.new(derived_key.to_s(16))
5652
end
5753

5854
# compute HMAC data
5955
def self.compute_data(address_or_ascii, nonce_hex)
60-
nonce = nonce_hex ? [nonce_hex].pack("H32") : SecureRandom.random_bytes(16)
56+
nonce = nonce_hex ? [nonce_hex].pack('H32') : SecureRandom.random_bytes(16)
6157
if Bitcoin.valid_address?(address_or_ascii)
6258
address_type = case Bitcoin.address_type(address_or_ascii)
63-
when :hash160; 'P2PH'
64-
when :p2sh; 'P2SH'
59+
when :hash160 then 'P2PH'
60+
when :p2sh then 'P2SH'
6561
else
6662
raise "unsupported address type #{address_type}"
6763
end
68-
contract_bytes = [ Bitcoin.hash160_from_address(address_or_ascii) ].pack("H*")
64+
contract_bytes = [Bitcoin.hash160_from_address(address_or_ascii)].pack('H*')
6965
else
70-
address_type = "TEXT"
66+
address_type = 'TEXT'
7167
contract_bytes = address_or_ascii
7268
end
73-
[ nonce.unpack("H*")[0], address_type + nonce + contract_bytes ]
69+
[nonce.unpack('H*')[0], address_type + nonce + contract_bytes]
7470
end
7571
end
7672
end

0 commit comments

Comments
 (0)