示例#1
0
        public Account(string privateKey)
        {
            string publicKey = KeyTools.GetPublicKeyFromPrivateKey(privateKey, true);

            this.address = KeyTools.GetAddressFromPublicKey(publicKey);
            this.keys    = new ECKeyPair(new BigInteger(privateKey, 16), new BigInteger(publicKey, 16));
        }
示例#2
0
        public static string GetAddressForContract(Transaction.Transaction tx)
        {
            string senderAddress = KeyTools.GetAddressFromPublicKey(tx.SenderPubKey);

            SHA256Managed.Create().ComputeHash(ByteUtil.HexStringToByteArray(senderAddress));
            Sha256Digest sha = new Sha256Digest();

            byte[] senderAddressBytes = ByteUtil.HexStringToByteArray(senderAddress);
            sha.BlockUpdate(senderAddressBytes, 0, senderAddressBytes.Count());

            int nonce = 0;

            if (!string.IsNullOrEmpty(tx.Nonce))
            {
                nonce = int.Parse(tx.Nonce);
                nonce--;
            }
            string hexNonce = Validation.IntToHex(nonce, 16);

            byte[] hexNonceBytes = ByteUtil.HexStringToByteArray(hexNonce);
            sha.BlockUpdate(hexNonceBytes, 0, hexNonceBytes.Count());
            byte[] bytes = new byte[sha.GetByteLength()];
            sha.DoFinal(bytes, 0);

            return(ByteUtil.ByteArrayToHexString(bytes).Substring(24, 40));
        }
示例#3
0
        public void GetAddressFromPrivateKey()
        {
            String privateKey = "e19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930";
            String address    = KeyTools.GetAddressFromPrivateKey(privateKey);

            System.Console.WriteLine(address.ToLower());
        }
示例#4
0
        public void GetAddressFromPrivateKey()
        {
            String privateKey = "a2315cc0cd0a4a0b5b58e2e270017d77ac79302d53199d0cf351669497466840";
            String address    = KeyTools.GetAddressFromPrivateKey(privateKey);

            System.Console.WriteLine(address.ToLower());
        }
示例#5
0
        public void GenerateKeyPair()
        {
            ECKeyPair keys   = KeyTools.GenerateKeyPair();
            ECPoint   pubKey = secp256k1.Curve.DecodePoint(keys.PublicKey.ToByteArray());

            Assert.AreEqual(keys.PrivateKey.CompareTo(BigInteger.Zero), 1);
            Assert.IsTrue(pubKey.IsValid());
        }
示例#6
0
        public void GetAddressFromPublicKey()
        {
            System.Console.WriteLine(KeyTools.GetAddressFromPublicKey("03B1785FB998CF2A650D064A7E4F6C34EAB5FC44600C81D4240644C89430618AF1"));
            String noPad  = "38959e0b7b9c545dc055ab668f8fbaef207e845c590eca4b14993619fff0f723d";
            String padded = "038959e0b7b9c545dc055ab668f8fbaef207e845c590eca4b14993619fff0f723d";

            Assert.AreEqual(KeyTools.GetAddressFromPublicKey(noPad), KeyTools.GetAddressFromPublicKey(padded));
        }
示例#7
0
        public string CreateAccount()
        {
            Account account = new Account(KeyTools.GenerateKeyPair());

            this.accounts.Add(account.Address.ToUpper(), account);

            if (defaultAccount == null)
            {
                defaultAccount = account;
            }
            return(account.Address);
        }
示例#8
0
        public void GetPublicKeyFromPrivateKey()
        {
            String privateKey = "24180e6b0c3021aedb8f5a86f75276ee6fc7ff46e67e98e716728326102e91c9";
            String publicKey  = KeyTools.GetPublicKeyFromPrivateKey(privateKey, false);

            Assert.AreEqual(publicKey.ToLower(), "04163fa604c65aebeb7048c5548875c11418d6d106a20a0289d67b59807abdd299d4cf0efcf07e96e576732dae122b9a8ac142214a6bc133b77aa5b79ba46b3e20");
            privateKey = "b776d8f068d11b3c3f5b94db0fb30efea05b73ddb9af1bbd5da8182d94245f0b";
            publicKey  = KeyTools.GetPublicKeyFromPrivateKey(privateKey, false);
            Assert.AreEqual(publicKey.ToLower(), "04cfa555bb63231d167f643f1a23ba66e6ca1458d416ddb9941e95b5fd28df0ac513075403c996efbbc15d187868857e31cf7be4d109b4f8cb3fd40499839f150a");
            privateKey = "e19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930";
            System.Console.WriteLine(KeyTools.GetPublicKeyFromPrivateKey(privateKey, true));
        }
示例#9
0
        public void GeneratePrivateKey()
        {
            int i = 0;

            while (i < 100000)
            {
                i++;
                string privateKey = KeyTools.GeneratePrivateKey();
                if (privateKey.Length != 64)
                {
                    throw new Exception("generator err");
                }
                Console.WriteLine(privateKey);
            }
        }
示例#10
0
        public void TestKeyAndAddress()
        {
            int i = 0;

            while (i < 100)
            {
                i++;
                string privateKey = KeyTools.GeneratePrivateKey();
                if (privateKey.Length != 64)
                {
                    throw new Exception("generator err");
                }
                string publicKey = KeyTools.GetPublicKeyFromPrivateKey(privateKey, true);

                Assert.AreEqual(KeyTools.GetAddressFromPrivateKey(privateKey), KeyTools.GetAddressFromPublicKey(publicKey.ToLower()));
            }
        }
示例#11
0
        public Transaction.Transaction Sign(Transaction.Transaction transaction)
        {
            if (transaction.ToAddr.ToUpper().StartsWith("0X"))
            {
                transaction.ToAddr = transaction.ToAddr.Substring(2);
            }

            if (!Validation.IsBech32(transaction.ToAddr) && !Validation.IsValidChecksumAddress("0x" + transaction.ToAddr))
            {
                throw new Exception("not checksum address or bech32");
            }

            if (Validation.IsBech32(transaction.ToAddr))
            {
                transaction.ToAddr = Bech32.FromBech32Address(transaction.ToAddr);
            }

            if (Validation.IsValidChecksumAddress("0x" + transaction.ToAddr))
            {
                transaction.ToAddr = "0x" + transaction.ToAddr;
            }

            TxParams txParams = transaction.ToTransactionParam();

            if (txParams != null && !string.IsNullOrEmpty(txParams.SenderPubKey))
            {
                string  address = KeyTools.GetAddressFromPublicKey(txParams.SenderPubKey).ToUpper();
                Account account = accounts[address];
                if (account == null)
                {
                    throw new Exception("Could not sign the transaction with" + address + "  as it does not exist");
                }
                return(SignWith(transaction, account));
            }

            if (defaultAccount == null)
            {
                throw new Exception("This wallet has no default account.");
            }

            return(this.SignWith(transaction, this.defaultAccount));
        }
示例#12
0
 public string ToFile(string privateKey, string passphrase, KDFType type)
 {
     return(KeyTools.EencryptPrivateKey(privateKey, passphrase, type));
 }
示例#13
0
        public static Account FromFile(string file, string passphrase)
        {
            string privateKey = KeyTools.DecryptPrivateKey(file, passphrase);

            return(new Account(privateKey));
        }
示例#14
0
 public Account(ECKeyPair keys)
 {
     this.keys    = keys;
     this.address = KeyTools.GetAddressFromPublicKey(ByteUtil.ByteArrayToHexString(this.keys.PublicKey.ToByteArray()));
 }
示例#15
0
 public void GetAddressFromPublicKey()
 {
     System.Console.WriteLine(KeyTools.GetAddressFromPublicKey("0246e7178dc8253201101e18fd6f6eb9972451d121fc57aa2a06dd5c111e58dc6a"));
 }
示例#16
0
 public void GenerateRandomBytes()
 {
     byte[] bytes = KeyTools.GenerateRandomBytes(32);
     Assert.NotNull(bytes);
     Assert.IsTrue(32 == bytes.Length);
 }