Commit 509506094f5b44e7467b8b329e99823fe7f87590

Authored by Marius Hanne
1 parent 611b804a7f

use compressed public keys by default

NOTE: The KeyGenerator will now use different addresses than before!

Showing 4 changed files with 29 additions and 26 deletions Side-by-side Diff

... ... @@ -32,11 +32,11 @@
32 32 # Bitcoin::Key.new
33 33 # Bitcoin::Key.new(privkey)
34 34 # Bitcoin::Key.new(nil, pubkey)
35   - def initialize privkey = nil, pubkey = nil, compressed = false
  35 + def initialize privkey = nil, pubkey = nil, compressed = true
36 36 @key = Bitcoin.bitcoin_elliptic_curve
37 37 @pubkey_compressed = pubkey ? self.class.is_compressed_pubkey?(pubkey) : compressed
38 38 set_priv(privkey) if privkey
39   - set_pub(pubkey) if pubkey
  39 + set_pub(pubkey, @pubkey_compressed) if pubkey
40 40 end
41 41  
42 42 # Generate new priv/pub key.
43 43  
44 44  
45 45  
... ... @@ -59,21 +59,17 @@
59 59 # In case the key was initialized with only
60 60 # a private key, the public key is regenerated.
61 61 def pub
  62 + regenerate_pubkey unless @key.public_key
  63 + return nil unless @key.public_key
62 64 @pubkey_compressed ? pub_compressed : pub_uncompressed
63 65 end
64 66  
65 67 def pub_compressed
66   - regenerate_pubkey unless @key.public_key
67   - return nil unless @key.public_key
68 68 @key.public_key.group.point_conversion_form = :compressed
69   - hex = @key.public_key.to_hex.rjust(66, '0')
70   - @key.public_key.group.point_conversion_form = :uncompressed
71   - hex
  69 + @key.public_key.to_hex.rjust(66, '0')
72 70 end
73 71  
74 72 def pub_uncompressed
75   - regenerate_pubkey unless @key.public_key
76   - return nil unless @key.public_key
77 73 @key.public_key.group.point_conversion_form = :uncompressed
78 74 @key.public_key.to_hex.rjust(130, '0')
79 75 end
... ... @@ -99,7 +95,7 @@
99 95  
100 96 # Sign +data+ with the key.
101 97 # key1 = Bitcoin::Key.generate
102   - # sig = key.sign("some data")
  98 + # sig = key1.sign("some data")
103 99 def sign(data)
104 100 @key.dsa_sign_asn1(data)
105 101 end
... ... @@ -167,7 +163,7 @@
167 163 # Regenerate public key from the private key.
168 164 def regenerate_pubkey
169 165 return nil unless @key.private_key
170   - set_pub(Bitcoin::OpenSSL_EC.regenerate_key(priv)[1])
  166 + set_pub(Bitcoin::OpenSSL_EC.regenerate_key(priv)[1], @pubkey_compressed)
171 167 end
172 168  
173 169 # Set +priv+ as the new private key (converting from hex).
... ... @@ -176,8 +172,8 @@
176 172 end
177 173  
178 174 # Set +pub+ as the new public key (converting from hex).
179   - def set_pub(pub)
180   - @pubkey_compressed ||= self.class.is_compressed_pubkey?(pub)
  175 + def set_pub(pub, compressed = nil)
  176 + @pubkey_compressed = compressed == nil ? self.class.is_compressed_pubkey?(pub) : compressed
181 177 @key.public_key = OpenSSL::PKey::EC::Point.from_hex(@key.group, pub)
182 178 end
183 179  
spec/bitcoin/key_spec.rb
... ... @@ -8,14 +8,14 @@
8 8 Bitcoin.network = :bitcoin
9 9 @key_data = {
10 10 :priv => "2ebd3738f59ae4fd408d717bf325b4cb979a409b0153f6d3b4b91cdfe046fb1e",
11   - :pub => "045fcb2fb2802b024f371cc22bc392268cc579e47e7936e0d1f05064e6e1103b8a81954eb6d3d33b8b6e73e9269013e843e83919f7ce4039bb046517a0cad5a3b1" }
12   - @key = Bitcoin::Key.new(@key_data[:priv], @key_data[:pub])
  11 + :pub => "035fcb2fb2802b024f371cc22bc392268cc579e47e7936e0d1f05064e6e1103b8a" }
  12 + @key = Bitcoin::Key.new(@key_data[:priv], @key_data[:pub], false)
13 13 end
14 14  
15 15 it "should generate a key" do
16 16 k = Bitcoin::Key.generate
17 17 k.priv.size.should == 64
18   - k.pub.size.should == 130
  18 + k.pub.size.should == 66
19 19 #p k.priv, k.pub
20 20 end
21 21  
... ... @@ -56,6 +56,8 @@
56 56 end
57 57  
58 58 it "should get addr" do
  59 + @key.addr.should == "19CyxBz6CUBogxTdSXUrbRHo7T7eLCMgbr"
  60 + @key.instance_eval { @pubkey_compressed = false }
59 61 @key.addr.should == "1JbYZRKyysprVjSSBobs8LX6QVjzsscQNU"
60 62 end
61 63  
62 64  
... ... @@ -103,10 +105,10 @@
103 105  
104 106 it "should export private key in base58 format" do
105 107 Bitcoin.network = :bitcoin
106   - str = Bitcoin::Key.new("e9873d79c6d87dc0fb6a5778633389f4453213303da61f20bd67fc233aa33262").to_base58
  108 + str = Bitcoin::Key.new("e9873d79c6d87dc0fb6a5778633389f4453213303da61f20bd67fc233aa33262", nil, false).to_base58
107 109 str.should == "5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF"
108 110 Bitcoin.network = :testnet
109   - str = Bitcoin::Key.new("d21fa2c7ad710ffcd9bcc22a9f96357bda1a2521ca7181dd610140ecea2cecd8").to_base58
  111 + str = Bitcoin::Key.new("d21fa2c7ad710ffcd9bcc22a9f96357bda1a2521ca7181dd610140ecea2cecd8", nil, false).to_base58
110 112 str.should == "93BTVFoqffueSaC5fqjLjLyn29S41JzvAZm2hC35SYMoYDXT1bY"
111 113 Bitcoin.network = :bitcoin
112 114 end
113 115  
... ... @@ -130,13 +132,15 @@
130 132 Bitcoin.network = :testnet3
131 133 Bitcoin::Key.new("e3ff5d7e592669d0c1714f1496b260815edd0c3a00186e896dc7f36ede914dd2",
132 134 nil, true).to_base58.should == "cVDu6aXUWHTM2vpztZW14BMnKkCcd5th6177VnCsa8XozoMyp73C"
133   - Bitcoin.network = :bitcoin end
  135 + Bitcoin.network = :bitcoin
  136 + end
134 137  
135 138 it "should import private key in compressed base58 format" do
136 139 Bitcoin.network = :bitcoin
137 140 key = Bitcoin::Key.from_base58("L2LusdhGSagfUVvNWrUuPDygn5mdAhxUDEANfABvBj36Twn1mKgQ")
138 141 key.priv.should == "98e4483a197fb686fe9afb51389f329aabc67964b1d0e0a5340c962a0d63c44a"
139 142 key.pub.should == "02e054ee811165ac294c992ff410067db6491228725fe09db2a415493c897973a8"
  143 + key.compressed.should == true
140 144 key.addr.should == "1C7Ni4zuV3zfLs8T1S7s29wNAtRoDHHnpw"
141 145 Bitcoin.network = :testnet3
142 146 key = Bitcoin::Key.from_base58("cVDu6aXUWHTM2vpztZW14BMnKkCcd5th6177VnCsa8XozoMyp73C")
... ... @@ -146,7 +150,7 @@
146 150 Bitcoin.network = :bitcoin
147 151 end
148 152  
149   - it "should hanlde compressed and uncompressed pubkeys" do
  153 + it "should handle compressed and uncompressed pubkeys" do
150 154 compressed = "0351efb6e91a31221652105d032a2508275f374cea63939ad72f1b1e02f477da78"
151 155 uncompressed = "0451efb6e91a31221652105d032a2508275f374cea63939ad72f1b1e02f477da787f71a2e8ac5aacedab47904d4bd42f636429e9ce069ebcb99f675aad31306a53"
152 156 Bitcoin::Key.new(nil, compressed).compressed.should == true
spec/bitcoin/wallet/keygenerator_spec.rb
... ... @@ -28,7 +28,10 @@
28 28 it "should use given nonce" do
29 29 g = KeyGenerator.new("foo", 2116)
30 30 g.nonce.should == 2116
31   - g.get_key(0).addr.should == '1GjyUrY3XcR4BvfgL8HqoAJbNDEgxSJdm1'
  31 + key = g.get_key(0)
  32 + key.addr.should == '1JvRdnShvscPtoP44VxPk5VaFBAo7ozRPb'
  33 + key.instance_eval { @pubkey_compressed = false }
  34 + key.addr.should == '1GjyUrY3XcR4BvfgL8HqoAJbNDEgxSJdm1'
32 35 end
33 36  
34 37 it "should check nonce if given" do
35 38  
... ... @@ -38,10 +41,10 @@
38 41 it "should use different target if given" do
39 42 g = KeyGenerator.new("foo", nil, @target)
40 43 g.nonce.should == 127
41   - g.get_key(0).addr.should == "13E68pPJyGycgQ4ZmV45xV9r9XEeyWqZdp"
  44 + g.get_key(0).addr.should == "1KLBACvBnz9BTdBnuJmNuQpKQrsi55sstj"
42 45 g = KeyGenerator.new("bar", nil, @target)
43 46 g.nonce.should == 40
44   - g.get_key(0).addr.should == "12iQpWRRQBmWcHYkTZkpDFrykzc9xn5kAU"
  47 + g.get_key(0).addr.should == "14T4deW5BGVA7wXpR3eoU9U8xprUJepxcy"
45 48 end
46 49  
47 50 it "should find keys" do
spec/bitcoin/wallet/keystore_spec.rb
... ... @@ -173,7 +173,7 @@
173 173 end
174 174  
175 175 it "should get key" do
176   - @ks.key('1KDUUSjPJkKwVEJsfpxEzBAf7iEbmqUwUu').priv.should ==
  176 + @ks.key('1GKjKQemNRhxL1ChTRFJNLZCXeCDxut2d7').priv.should ==
177 177 '7f27bb0ca02e558c4b4b4e267417437adac01403e0d0bb9b07797d1dbb1adfd1'
178 178 end
179 179  
... ... @@ -183,8 +183,8 @@
183 183 end
184 184  
185 185 it "should export key" do
186   - @ks.export('1KDUUSjPJkKwVEJsfpxEzBAf7iEbmqUwUu').should ==
187   - '5JnHbCHicVj2Wgd2KgNPU7dQ6te55GzHjc4PH9cQDFUjeepYSHX'
  186 + @ks.export('1GKjKQemNRhxL1ChTRFJNLZCXeCDxut2d7').should ==
  187 + 'L1UtDvpnffnVg1szqSmQAgFexzvcysZrs3jwLH1FT4uREpZqcXaR'
188 188 end
189 189  
190 190 end