public XPrivateKey(Key childKey, byte[] childCodeChain, byte depth, uint child) { _privateKey = childKey; _childCodeChain = childCodeChain; Depth = depth; Child = child; }
public void CanCreateAddress() { var hash = Hashes.SHA256(new byte[] { 0x01 }); var privateKey = new Key(hash); var publicKey = privateKey.PublicKey; var address = publicKey.ToAddress(Network.Main); var str = address.ToString(); Assert.AreEqual("1ApqRi7vWrh3gR4jJ6LXhAXRKWTu55mrqP", str); }
public void CanGeneratePubKeysAndAddress() { //Took from http://brainwallet.org/ and http://procbits.com/2013/08/27/generating-a-bitcoin-address-with-javascript var tests = new[] { new { PrivateKeyWIF = "5Hx15HFGyep2CfPxsJKe2fXJsCVn5DEiyoeGGF6JZjGbTRnqfiD", CompressedPrivateKeyWIF = "KwomKti1X3tYJUUMb1TGSM2mrZk1wb1aHisUNHCQXTZq5auC2qc3", PubKey = "04d0988bfa799f7d7ef9ab3de97ef481cd0f75d2367ad456607647edde665d6f6fbdd594388756a7beaf73b4822bc22d36e9bda7db82df2b8b623673eefc0b7495", CompressedPubKey = "03d0988bfa799f7d7ef9ab3de97ef481cd0f75d2367ad456607647edde665d6f6f", Address = "16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS", CompressedAddress = "1FkKMsKNJqWSDvTvETqcCeHcUQQ64kSC6s", Hash160 = "3c176e659bea0f29a3e9bf7880c112b1b31b4dc8", CompressedHash160 = "a1c2f92a9dacbd2991c3897724a93f338e44bdc1", DER = "3082011302010104201184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fda081a53081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a14403420004d0988bfa799f7d7ef9ab3de97ef481cd0f75d2367ad456607647edde665d6f6fbdd594388756a7beaf73b4822bc22d36e9bda7db82df2b8b623673eefc0b7495", CompressedDER = "3081d302010104201184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fda08185308182020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a12403220003d0988bfa799f7d7ef9ab3de97ef481cd0f75d2367ad456607647edde665d6f6f" }, new { PrivateKeyWIF = "5J7WTMRn1vjZ9udUxNCLq7F9DYEJiqRCjstiBrY6mDjnaomd6kZ", CompressedPrivateKeyWIF = "KxXj1KAMh6ApvKJ2PNZ4XLZRGLqjDehppFdEnueGSBDrC2Hfe7vt", PubKey = "0493e5d305cad2588d5fb254065fe48ce446028ba380e6ee663baea9cd105500897eb030c033cdab160f31c36df0ea38330fdd69677df49cd14826902022d17f3f", CompressedPubKey = "0393e5d305cad2588d5fb254065fe48ce446028ba380e6ee663baea9cd10550089", Address = "1MZmwgyMyjM11uA6ZSpgn1uK3LBWCzvV6e", CompressedAddress = "1AECNr2TDye8dpC1TeDH3eJpGoZ7dNPy4g", Hash160 = "e19557c8f8fb53a964c5dc7bfde86d806709f7c5", CompressedHash160 = "6538094af65453ea279f14d1a04b408e3adfebd7", DER = "308201130201010420271ac4d7056937c156abd828850d05df0697dd662d3c1b0107f53a387b4c176ca081a53081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a1440342000493e5d305cad2588d5fb254065fe48ce446028ba380e6ee663baea9cd105500897eb030c033cdab160f31c36df0ea38330fdd69677df49cd14826902022d17f3f", CompressedDER = "3081d30201010420271ac4d7056937c156abd828850d05df0697dd662d3c1b0107f53a387b4c176ca08185308182020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a1240322000393e5d305cad2588d5fb254065fe48ce446028ba380e6ee663baea9cd10550089" } }; foreach (var test in tests) { var privateKey = Key.Parse(test.PrivateKeyWIF); Assert.AreEqual(test.PubKey, privateKey.PublicKey.ToString()); var address = Address.FromString(test.Address); Assert.AreEqual(test.Hash160, address.PubKeyHash.ToHex()); Assert.AreEqual(test.Hash160, privateKey.PublicKey.Hash.ToHex()); Assert.AreEqual(address.PubKeyHash, privateKey.PublicKey.ToAddress(Network.Main).PubKeyHash); var compressedPrivKey = new Key(privateKey.ToByteArray(), true); Assert.AreEqual(test.CompressedPrivateKeyWIF, compressedPrivKey.ToString(Network.Main)); Assert.AreEqual(test.CompressedPubKey, compressedPrivKey.PublicKey.ToString()); //Assert.True(compressedPrivKey.PublicKey.IsCompressed); var compressedAddr = Address.FromString(test.CompressedAddress); Assert.AreEqual(test.CompressedHash160, compressedAddr.PubKeyHash.ToHex()); Assert.AreEqual(test.CompressedHash160, compressedPrivKey.PublicKey.Hash.ToHex()); } }
public void CanSignMessage() { var dA = Hashes.SHA256(new byte[] { 0x01 }); var k = Encoders.Hex.Decode("0098e9c07e8e6adb97b77d85b0c10a265e11737a89a3e37b"); var rnd = new FakeRandom(k); var key = new Key(dA); var signer = new ECDsaSigner(rnd, key); var message = Encoders.Hex.Decode("66e98a165854cd07989b1ee0ec3f8dbe0ee3c2fb0051ef53a0be03457c4f21bc"); var s1 = signer.GenerateSignature(message); Assert.IsTrue(signer.VerifySignature(message, s1)); Assert.IsTrue(s1.R == (rnd.GetNextK()*Secp256k1.G).X%Secp256k1.N); Assert.IsTrue(ECDsaSigner.VerifySignature(message, s1.R, s1.S, key.PublicKey)); }
public void CanParse() { const string script1 = "OP_DUP OP_HASH160 657912a72d3ac8169fe8eaecd5ab401c94fc9981717e3e6dd4971889f785790c OP_EQUALVERIFY OP_CHECKSIG"; const string script2 = "OP_IF 4effffffff " + "46726f6d2061336136316665663433333039623966623233323235646637393130623033616663353436356239204d6f6e205365702031372030303a30303a303020323030310a46726f6d3a205361746f736869204e616b616d6f746f203c7361746f7368696e40676d782e636f6d3e0a446174653a204d6f6e2c2031322041756720323031332030323a32383a3032202d303230300a5375626a6563743a205b50415443485d2052656d6f7665202853494e474c457c444f55424c4529425954450a0a492072656d6f76656420746869732066726f6d20426974636f696e20696e20663165316662346264656638373863386663313536346661343138643434653735343161376538330a696e2053657074203720323031302c20616c6d6f73742074687265652079656172732061676f2e204265207761726e6564207468617420492068617665206e6f740a61637475616c6c792074657374656420746869732070617463682e0a2d2d2d0a206261636b656e64732f626974636f696e642f646573657269616c697a652e7079207c2020202038202b2d2d2d2d2d2d2d0a20312066696c65206368616e6765642c203120696e73657274696f6e282b292c20372064656c6574696f6e73282d290a0a64696666202d2d67697420612f6261636b656e64732f626974636f696e642f646573657269616c697a652e707920622f6261636b656e64732f626974636f696e642f646573657269616c697a652e70790a696e64657820363632303538332e2e38396239623162203130303634340a2d2d2d20612f6261636b656e64732f626974636f696e642f646573657269616c697a652e70790a2b2b2b20622f6261636b656e64732f626974636f696e642f646573657269616c697a652e70790a4040202d3238302c3130202b3238302c38204040206f70636f646573203d20456e756d65726174696f6e28224f70636f646573222c205b0a2020202020224f505f57495448494e222c20224f505f524950454d44313630222c20224f505f53484131222c20224f505f534841323536222c20224f505f48415348313630222c0a2020202020224f505f48415348323536222c20224f505f434f4445534550415241544f52222c20224f505f434845434b534947222c20224f505f434845434b534947564552494659222c20224f505f434845434b4d554c5449534947222c0a2020202020224f505f434845434b4d554c5449534947564552494659222c0a2d2020202028224f505f53494e474c45425954455f454e44222c2030784630292c0a2d2020202028224f505f444f55424c45425954455f424547494e222c20307846303030292c0a2020202020224f505f5055424b4559222c20224f505f5055424b455948415348222c0a2d2020202028224f505f494e56414c49444f50434f4445222c20307846464646292c0a2b2020202028224f505f494e56414c49444f50434f4445222c2030784646292c0a205d290a200a200a4040202d3239332c3130202b3239312c3620404020646566207363726970745f4765744f70286279746573293a0a202020202020202020766368203d204e6f6e650a2020202020202020206f70636f6465203d206f72642862797465735b695d290a20202020202020202069202b3d20310a2d20202020202020206966206f70636f6465203e3d206f70636f6465732e4f505f53494e474c45425954455f454e4420616e642069203c206c656e286279746573293a0a2d2020202020202020202020206f70636f6465203c3c3d20380a2d2020202020202020202020206f70636f6465207c3d206f72642862797465735b695d290a2d20202020202020202020202069202b3d20310a200a2020202020202020206966206f70636f6465203c3d206f70636f6465732e4f505f5055534844415441343a0a202020202020202020202020206e53697a65203d206f70636f64650a2d2d200a312e372e392e340a0a " + "OP_ENDIF"; var s1 = Script.FromAsm(script1); Assert.AreEqual(script1, s1.ToString()); var s2 = Script.FromAsm(script2); Assert.AreEqual(script2, s2.ToString()); var hash = Hashes.SHA256(new byte[] { 0x01 }); var privateKey = new Key(hash); var publicKey = privateKey.PublicKey; var script = publicKey.ScriptPubKey; var expected = "02d415b187c6e7ce9da46ac888d20df20737d6f16a41639e68ea055311e1535dd9 OP_CHECKSIG"; Assert.AreEqual(expected, script.ToString()); Assert.AreEqual(expected, Script.FromAsm(expected).ToString()); }
public ECDsaSigner(IKProvider kprovider, Key key) { _kprovider = kprovider; _privateKey = key; }
public ECDsaSigner(Key key) : this(new HmackKProvider(), key) { }