spec/bitcoin/key_spec.rb in bitcoin-ruby-0.0.5 vs spec/bitcoin/key_spec.rb in bitcoin-ruby-0.0.6

- old
+ new

@@ -6,25 +6,46 @@ before do Bitcoin.network = :bitcoin @key_data = { :priv => "2ebd3738f59ae4fd408d717bf325b4cb979a409b0153f6d3b4b91cdfe046fb1e", - :pub => "045fcb2fb2802b024f371cc22bc392268cc579e47e7936e0d1f05064e6e1103b8a81954eb6d3d33b8b6e73e9269013e843e83919f7ce4039bb046517a0cad5a3b1" } - @key = Bitcoin::Key.new(@key_data[:priv], @key_data[:pub]) + :pub => "035fcb2fb2802b024f371cc22bc392268cc579e47e7936e0d1f05064e6e1103b8a" } + @key = Bitcoin::Key.new(@key_data[:priv], @key_data[:pub], false) end it "should generate a key" do k = Bitcoin::Key.generate k.priv.size.should == 64 + k.pub.size.should == 66 + k.compressed.should == true + + k = Bitcoin::Key.generate(compressed: true) + k.priv.size.should == 64 + k.pub.size.should == 66 + k.compressed.should == true + + k = Bitcoin::Key.generate(true) + k.priv.size.should == 64 + k.pub.size.should == 66 + k.compressed.should == true + + k = Bitcoin::Key.generate(compressed: false) + k.priv.size.should == 64 k.pub.size.should == 130 - #p k.priv, k.pub + k.compressed.should == false + + k = Bitcoin::Key.generate(false) + k.priv.size.should == 64 + k.pub.size.should == 130 + k.compressed.should == false end it "should create empty key" do k = Bitcoin::Key.new k.priv.should == nil k.pub.should == nil + k.compressed.should == true end it "should create key from priv + pub" do k = Bitcoin::Key.new(@key_data[:priv], @key_data[:pub]) k.priv.should == @key_data[:priv] @@ -54,23 +75,31 @@ k.priv.should == @key_data[:priv] k.pub.should == @key_data[:pub] end it "should get addr" do + @key.addr.should == "19CyxBz6CUBogxTdSXUrbRHo7T7eLCMgbr" + @key.instance_eval { @pubkey_compressed = false } @key.addr.should == "1JbYZRKyysprVjSSBobs8LX6QVjzsscQNU" end it "should sign data" do @key.sign("foobar").size.should >= 69 end - it "should verify signature" do + it "should verify signature using public key" do sig = @key.sign("foobar") key = Bitcoin::Key.new(nil, @key.pub) key.verify("foobar", sig).should == true end + it "should verify signature using private key" do + sig = @key.sign("foobar") + key = Bitcoin::Key.new(@key.priv) + key.verify("foobar", sig).should == true + end + it "recovers public keys from compact signatures" do tests = [ # normal { address: "16vqGo3KRKE9kTsTZxKoJKLzwZGTodK3ce", signature: "HPDs1TesA48a9up4QORIuub67VHBM37X66skAYz0Esg23gdfMuCTYDFORc6XGpKZ2/flJ2h/DUF569FJxGoVZ50=", @@ -101,14 +130,14 @@ end end it "should export private key in base58 format" do Bitcoin.network = :bitcoin - str = Bitcoin::Key.new("e9873d79c6d87dc0fb6a5778633389f4453213303da61f20bd67fc233aa33262").to_base58 + str = Bitcoin::Key.new("e9873d79c6d87dc0fb6a5778633389f4453213303da61f20bd67fc233aa33262", nil, false).to_base58 str.should == "5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF" Bitcoin.network = :testnet - str = Bitcoin::Key.new("d21fa2c7ad710ffcd9bcc22a9f96357bda1a2521ca7181dd610140ecea2cecd8").to_base58 + str = Bitcoin::Key.new("d21fa2c7ad710ffcd9bcc22a9f96357bda1a2521ca7181dd610140ecea2cecd8", nil, false).to_base58 str.should == "93BTVFoqffueSaC5fqjLjLyn29S41JzvAZm2hC35SYMoYDXT1bY" Bitcoin.network = :bitcoin end it "should import private key in base58 format" do @@ -128,27 +157,29 @@ Bitcoin::Key.new("98e4483a197fb686fe9afb51389f329aabc67964b1d0e0a5340c962a0d63c44a", nil, true).to_base58.should == "L2LusdhGSagfUVvNWrUuPDygn5mdAhxUDEANfABvBj36Twn1mKgQ" Bitcoin.network = :testnet3 Bitcoin::Key.new("e3ff5d7e592669d0c1714f1496b260815edd0c3a00186e896dc7f36ede914dd2", nil, true).to_base58.should == "cVDu6aXUWHTM2vpztZW14BMnKkCcd5th6177VnCsa8XozoMyp73C" - Bitcoin.network = :bitcoin end + Bitcoin.network = :bitcoin + end it "should import private key in compressed base58 format" do Bitcoin.network = :bitcoin key = Bitcoin::Key.from_base58("L2LusdhGSagfUVvNWrUuPDygn5mdAhxUDEANfABvBj36Twn1mKgQ") key.priv.should == "98e4483a197fb686fe9afb51389f329aabc67964b1d0e0a5340c962a0d63c44a" key.pub.should == "02e054ee811165ac294c992ff410067db6491228725fe09db2a415493c897973a8" + key.compressed.should == true key.addr.should == "1C7Ni4zuV3zfLs8T1S7s29wNAtRoDHHnpw" Bitcoin.network = :testnet3 key = Bitcoin::Key.from_base58("cVDu6aXUWHTM2vpztZW14BMnKkCcd5th6177VnCsa8XozoMyp73C") key.priv.should == "e3ff5d7e592669d0c1714f1496b260815edd0c3a00186e896dc7f36ede914dd2" key.pub.should == "0390bb61c062266a1e8460ec902379749ae30f569013d82bd448a61591f20b8ee2" key.addr.should == "mjh9RgZh14FfJQ2pFpRSqEQ5BH1nHo5To7" Bitcoin.network = :bitcoin end - it "should hanlde compressed and uncompressed pubkeys" do + it "should handle compressed and uncompressed pubkeys" do compressed = "0351efb6e91a31221652105d032a2508275f374cea63939ad72f1b1e02f477da78" uncompressed = "0451efb6e91a31221652105d032a2508275f374cea63939ad72f1b1e02f477da787f71a2e8ac5aacedab47904d4bd42f636429e9ce069ebcb99f675aad31306a53" Bitcoin::Key.new(nil, compressed).compressed.should == true Bitcoin::Key.new(nil, compressed).pub.should == compressed Bitcoin::Key.new(nil, compressed).addr.should == "1NdB761LmTmrJixxp93nz7pEiCx5cKPW44" @@ -177,9 +208,49 @@ k.compressed.should == true k = Bitcoin::Key.new(nil, nil) k.instance_eval{ set_pub("0451efb6e91a31221652105d032a2508275f374cea63939ad72f1b1e02f477da787f71a2e8ac5aacedab47904d4bd42f636429e9ce069ebcb99f675aad31306a53") } k.compressed.should == false + end + + it "should handle private key in bip38 (non-ec-multiply) format" do + k = Bitcoin::Key.from_base58("5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR") + k.to_bip38("TestingOneTwoThree").should == "6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg" + + k = Bitcoin::Key.from_bip38("6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg", "TestingOneTwoThree") + k.to_base58.should == "5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR" + + k = Bitcoin::Key.from_base58("5HtasZ6ofTHP6HCwTqTkLDuLQisYPah7aUnSKfC7h4hMUVw2gi5") + k.to_bip38("Satoshi").should == "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByq" + + k = Bitcoin::Key.from_bip38("6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByq", "Satoshi") + k.to_base58.should == "5HtasZ6ofTHP6HCwTqTkLDuLQisYPah7aUnSKfC7h4hMUVw2gi5" + + k = Bitcoin::Key.from_base58("L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP") + k.to_bip38("TestingOneTwoThree").should == "6PYNKZ1EAgYgmQfmNVamxyXVWHzK5s6DGhwP4J5o44cvXdoY7sRzhtpUeo" + + k = Bitcoin::Key.from_bip38("6PYNKZ1EAgYgmQfmNVamxyXVWHzK5s6DGhwP4J5o44cvXdoY7sRzhtpUeo", "TestingOneTwoThree") + k.to_base58.should == "L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP" + + k = Bitcoin::Key.from_base58("KwYgW8gcxj1JWJXhPSu4Fqwzfhp5Yfi42mdYmMa4XqK7NJxXUSK7") + k.to_bip38("Satoshi").should == "6PYLtMnXvfG3oJde97zRyLYFZCYizPU5T3LwgdYJz1fRhh16bU7u6PPmY7" + + k = Bitcoin::Key.from_bip38("6PYLtMnXvfG3oJde97zRyLYFZCYizPU5T3LwgdYJz1fRhh16bU7u6PPmY7", "Satoshi") + k.to_base58.should == "KwYgW8gcxj1JWJXhPSu4Fqwzfhp5Yfi42mdYmMa4XqK7NJxXUSK7" + end + + it "should generate private key from warp format" do + k = Bitcoin::Key.from_warp("ER8FT+HFjk0", "7DpniYifN6c") + k.addr.should == "1J32CmwScqhwnNQ77cKv9q41JGwoZe2JYQ" + k.to_base58.should == "5JfEekYcaAexqcigtFAy4h2ZAY95vjKCvS1khAkSG8ATo1veQAD" + + k = Bitcoin::Key.from_warp("YqIDBApDYME", "G34HqIgjrIc") + k.addr.should == "19aKBeXe2mi4NbQRpYUrCLZtRDHDUs9J7J" + k.to_base58.should == "5KUJA5iZ2zS7AXkU2S8BiBVY3xj6F8GspLfWWqL9V7CajXumBQV" + + k = Bitcoin::Key.from_warp("FPdAxCygMJg", "X+qaSwhUYXw") + k.addr.should == "14Pqeo9XNRxjtKFFYd6TvRrJuZxVpciS81" + k.to_base58.should == "5JBAonQ4iGKFJxENExZghDtAS6YB8BsCw5mwpHSvZvP3Q2UxmT1" end end begin