static void Main()
    {
        ETHTransaction tx = new ETHTransaction();

        tx.m_nonce      = BigInteger.Parse("16");
        tx.m_gasPrice   = BigInteger.Parse("10000000000");
        tx.m_gasLimit   = BigInteger.Parse("21000");
        tx.m_to         = Encode.HexToBytes("1111111111111111111111111111111111111111");
        tx.m_value      = BigInteger.Parse("100");
        tx.m_initOrData = Encode.HexToBytes("");

        tx.ECDsaSign("ETH", Encode.BytesToBigInteger(Encode.HexToBytes("1111111111111111111111111111111111111111111111111111111111111111")));

        Console.Write(Encode.BytesToHex(tx.EncodeRLP()));
    }
    public static bool HexToPrivateKey(string hex, out BigInteger privateKey)
    {
        privateKey = 0;

        byte[] data = Encode.HexToBytes(hex, 32);
        if (data == null)
        {
            return(false);
        }

        privateKey = Encode.BytesToBigInteger(data);

        if (!IsPrivateKeyValid(privateKey))
        {
            return(false);
        }

        return(true);
    }
示例#3
0
    public static BigInteger PasswordToPrivateKey(string password)
    {
        // change salt for improved security
        // reference https://en.wikipedia.org/wiki/Key_stretching
        // reference https://en.wikipedia.org/wiki/Salt_(cryptography)
        string salt = "Cryptography";

        var pbkdf2 = new Rfc2898DeriveBytes(
            Encoding.UTF8.GetBytes(password),
            Encoding.UTF8.GetBytes(salt),
            4096);

        BigInteger privateKey = Encode.BytesToBigInteger(pbkdf2.GetBytes(32));

        if (!EllipticCurve.IsPrivateKeyValid(privateKey))
        {
            throw new Exception("privateKey is invalid.");              // extreme low probability to happen
        }
        return(privateKey);
    }
示例#4
0
    public void ECDsaSign(string currency, BigInteger privateKey)
    {
        int chainId = GetChainId(currency);

        m_v = new BigInteger(chainId);
        m_r = new BigInteger(0);
        m_s = new BigInteger(0);

        byte[] e   = BouncyCastle.Keccak(EncodeRLP());
        byte[] sig = EllipticCurve.ECDsaSignHash(privateKey, e);

        BigInteger publicKeyX;
        BigInteger publicKeyY;

        BouncyCastle.ECPrivateKeyToPublicKey(privateKey, out publicKeyX, out publicKeyY);
        int recoveryId = BouncyCastle.ECCalcRecoveryId(sig.Take(32).ToArray(), sig.Skip(32).ToArray(), e, publicKeyX, publicKeyY);

        m_v = new BigInteger(chainId * 2 + 35 + recoveryId);
        m_r = Encode.BytesToBigInteger(sig.Take(32).ToArray());
        m_s = Encode.BytesToBigInteger(sig.Skip(32).ToArray());
    }