Esempio n. 1
0
    public byte[] EncodeRLP()
    {
        var rlp = new List <byte>();

        rlp.AddRange(Encode.RLP(Encode.BigIntegerToBytes(m_nonce)));
        rlp.AddRange(Encode.RLP(Encode.BigIntegerToBytes(m_gasPrice)));
        rlp.AddRange(Encode.RLP(Encode.BigIntegerToBytes(m_gasLimit)));
        rlp.AddRange(Encode.RLP(m_to));
        rlp.AddRange(Encode.RLP(Encode.BigIntegerToBytes(m_value)));
        rlp.AddRange(Encode.RLP(m_initOrData));

        rlp.AddRange(Encode.RLP(Encode.BigIntegerToBytes(m_v)));
        rlp.AddRange(Encode.RLP(Encode.BigIntegerToBytes(m_r)));
        rlp.AddRange(Encode.RLP(Encode.BigIntegerToBytes(m_s)));

        return(Encode.RLP(rlp.ToArray(), true));
    }
    public static byte[] ECDsaSignHash(BigInteger privateKey, byte[] hash)
    {
        if (!IsPrivateKeyValid(privateKey))
        {
            throw new Exception("privateKey is invalid.");
        }

        ECDsa        ecdsa      = ECDsa.Create(ECCurve.CreateFromFriendlyName("secp256k1"));    // windows 10: https://docs.microsoft.com/en-us/windows/desktop/SecCNG/cng-named-elliptic-curves
        ECParameters parameters = ecdsa.ExportExplicitParameters(true);

        parameters.D = Encode.BigIntegerToBytes(privateKey, 32);
        // parameters.Q is not used in SignHash
        ecdsa.ImportParameters(parameters);

        while (true)
        {
            byte[] sig = ecdsa.SignHash(hash);  // 64 bytes

            if (sig[32] < 0x7f)                 // low s value
            {
                return(sig);
            }
        }
    }
 public static byte[] PublicKeyToBytes(BigInteger publicKeyX, BigInteger publicKeyY)
 {
     return(Encode.BigIntegerToBytes(publicKeyX, 32).Concat(Encode.BigIntegerToBytes(publicKeyY, 32)).ToArray());
 }
Esempio n. 4
0
    void Output(BigInteger privateKey)
    {
        BigInteger publicKeyX;
        BigInteger publicKeyY;

        BouncyCastle.ECPrivateKeyToPublicKey(privateKey, out publicKeyX, out publicKeyY);

        byte[] publicKey = EllipticCurve.PublicKeyToBytes(publicKeyX, publicKeyY);
        byte[] address   = ETHAddress.PublicKeyToETHAddress(publicKey);

        m_paragraphAddress.Inlines.Add("Private key (confidential): 0x" + Encode.BytesToHex(Encode.BigIntegerToBytes(privateKey, 32)) + "\n");
        m_paragraphAddress.Inlines.Add("Public key: 0x" + Encode.BytesToHex(publicKey) + "\n");
        m_paragraphAddress.Inlines.Add("ETH address: 0x" + Encode.BytesToHex(address) + "\n");
        m_paragraphAddress.Inlines.Add("ETC address: 0x" + Encode.BytesToHex(address) + "\n");
    }