예제 #1
0
        public async Task CreateTransaction()
        {
            var chainTag = await _vechainClient.GetChainTag();

            var blockref = "0x001a7d4448f0948b"; // await _vechainClient.GetLatestBlockRef();


            var trans = RawTransaction.CreateUnsigned(chainTag, blockref, new[]
            {
                new RawClause("0xd3ae78222beadb038203be21ed5ce7c9b1bff602", "1", "", false)
            }, "12345678", 720, 0, 21000, "");

            var rlpTransaction = new RlpTransaction(trans).AsRlpValues();


            var asHexString = RlpEncoder.Encode(rlpTransaction);

            var rawTransaction = asHexString.ByteArrayToString(StringType.Hex | StringType.ZeroLowerX);

            // 0x44C3e1Ce754129Eb74522E3CA5695B7Cfa6d2B19
            var privateKey = new BigInteger("0xdce1443bd2ef0c2631adc1c67e5c93f13dc23a41c18b536effbbdcbcdb96fb65".HexStringToByteArray());
            var publicKey  = ECDSASign.PublicKeyFromPrivate(privateKey);


            var customKey = new ECKeyPair(privateKey, publicKey);

            var result = trans.Sign(customKey).CalculateTxId(new Address(customKey.GetHexAddress())).Transfer(_vechainClient);
        }
예제 #2
0
        /// <summary>
        /// Sign the transaction by RLP encoding it and calculating the signature of the resulting byte[].
        /// </summary>
        /// <param name="transaction">The transaction that will be signed.</param>
        /// <param name="key">The key with which the transaction will be signed.</param>
        /// <returns>The signed transaction</returns>
        public static RawTransaction Sign(this RawTransaction transaction, ECKeyPair key)
        {
            var rlp = RlpEncoder.Encode(new RlpTransaction(transaction).AsRlpValues());

            SignatureData signatureData = ECDSASign.SignMessage(rlp, key, true);

            transaction.signature = signatureData.ToByteArray();

            return(transaction);
        }
예제 #3
0
        public ECKeyPair(BigInteger privateKey, BigInteger publicKey, bool check = false)
        {
            PrivateKey = privateKey;

            PublicKey = publicKey;

            if (check && !Equals(publicKey, ECDSASign.PublicKeyFromPrivate(privateKey)))
            {
                throw new InvalidKeyException("The public key does not match the private key.");
            }
        }
예제 #4
0
        /// <summary>
        /// Sign the raw transaction.
        /// </summary>
        /// <param name="rawTransaction">
        ///            <seealso cref="RawTransaction"/> </param>
        /// <returns> <seealso cref="RawTransaction"/> with signature. </returns>
        public static RawTransaction Sign(RawTransaction rawTransaction, ECKeyPair keyPair)
        {
            if (rawTransaction == null)
            {
                throw ClientArgumentException.Exception("raw transaction object is invalid");
            }
            var signature = ECDSASign.SignMessage(rawTransaction.Encode(), keyPair, true);
            var signBytes = signature.ToByteArray();

            rawTransaction.Signature = signBytes;
            return(rawTransaction);
        }
예제 #5
0
        public Transaction Sign(ECKeyPair key)
        {
            var hash = Hash.Blake2B(RlpDataForSignature);

            var sig = ECDSASign.SignMessage(hash, key, false);

            var sigBytes = sig.ToByteArray();

            signature = sigBytes.ToHex(true);

            var signer = key.GetRawAddress();

            byte[] concatenatedBytes = new byte[52];
            Unsafe.CopyBlock(ref concatenatedBytes[0], ref hash[0], (uint)hash.Length);
            Unsafe.CopyBlock(ref concatenatedBytes[hash.Length], ref signer[0], (uint)signer.Length);

            byte[] txIdBytes = Hash.Blake2B(concatenatedBytes);
            id = txIdBytes.ToHex(true);

            return(this);
        }
예제 #6
0
        public static ECKeyPair RecoverPublicKey(byte[] message, byte[] sig)
        {
            if (message == null || message.Length != 32)
            {
                throw new ArgumentException("The recover message is not correct", nameof(message));
            }
            if (sig == null || sig.Length != 65)
            {
                throw new ArgumentException("The recover signature is not correct", nameof(message));
            }
            var rBytes = new byte[32];
            var sBytes = new byte[32];

            Array.Copy(sig, 0, rBytes, 0, rBytes.Length);
            Array.Copy(sig, 32, sBytes, 0, sBytes.Length);
            byte recovery       = sig[64];
            var  ecdsaSignature = new ECDSASignature(rBytes, sBytes);
            var  publicKey      = ECDSASign.RecoverFromSignature(recovery, ecdsaSignature, message);

            return(new ECKeyPair(null, publicKey));
        }
예제 #7
0
        public static string BuildSignature(byte[] cert, byte[] txRawHash, string privateKey)
        {
            var txRawBytes  = CryptoUtils.Blake2b(txRawHash);
            var cerHexBytes = CryptoUtils.Sha256(cert);

            var message = new byte[txRawBytes.Length + cerHexBytes.Length];

            Array.Copy(cerHexBytes, 0, message, 0, cerHexBytes.Length);
            Array.Copy(txRawBytes, 0, message, cerHexBytes.Length, txRawBytes.Length);

            var key       = ECKeyPair.Create(ByteUtils.ToByteArray(privateKey));
            var signature = ECDSASign.SignMessage(CryptoUtils.Sha256(message), key, false);

            var signBytes = signature.ToByteArray();

            _logger.Info("signature: {} {}", ByteUtils.ToHexString(signBytes, null),
                         ByteUtils.CleanHexPrefix(ByteUtils.ToHexString(signature.R, Prefix.ZeroLowerX))
                         + ByteUtils.CleanHexPrefix(ByteUtils.ToHexString(signature.S, Prefix.ZeroLowerX)) + "0"
                         + signature.V);
            return(ByteUtils.ToHexString(signBytes, null));
        }
예제 #8
0
        public ECKeyPair(BigInteger privateKey)
        {
            PrivateKey = privateKey;

            PublicKey = ECDSASign.PublicKeyFromPrivate(privateKey);
        }
예제 #9
0
 public static ECKeyPair Create(BigInteger privateKey)
 {
     return(new ECKeyPair(privateKey, ECDSASign.PublicKeyFromPrivate(privateKey)));
 }