Esempio n. 1
0
        public Transaction Sign(string privateKey, string recipientAddress, ulong amount, DateTime signDate)
        {
            BigInteger hexPrivateKey = new BigInteger(privateKey, 16);
            string     publicKey     = CryptoUtil.GetPublicKeyCompressed(privateKey);
            string     senderAddress = CalcRipeMD160(publicKey);

            TransactionRaw transactionRaw = new TransactionRaw()
            {
                FromAddress = senderAddress,
                ToAddress   = recipientAddress,
                Amount      = amount,
                DateCreated = signDate
            };

            string tranJson = JsonConvert.SerializeObject(transactionRaw);

            byte[] tranHash = CryptoUtil.CalcSHA256(tranJson);

            BigInteger[] tranSignature = SignData(hexPrivateKey, tranHash);
            string       txhash        = CryptoUtil.CalcSHA256String(tranJson);

            Transaction signedTransaction = new Transaction()
            {
                FromAddress     = transactionRaw.FromAddress,
                ToAddress       = transactionRaw.ToAddress,
                Amount          = transactionRaw.Amount,
                DateCreated     = transactionRaw.DateCreated,
                TransactionHash = txhash,
                SenderPublicKey = publicKey,
                Signature       = new string[] { tranSignature[0].ToString(16), tranSignature[1].ToString(16) }
            };

            return(signedTransaction);
        }
Esempio n. 2
0
        public bool IsValid(Transaction transaction)
        {
            ECDomainParameters ecSpec      = new ECDomainParameters(CryptoUtil.Curve.Curve, CryptoUtil.Curve.G, CryptoUtil.Curve.N, CryptoUtil.Curve.H);
            IDsaKCalculator    kCalculator = new HMacDsaKCalculator(new Sha256Digest());

            var point = DecodeECPointPublicKey(transaction.SenderPublicKey);

            ECPublicKeyParameters keyParameters = new ECPublicKeyParameters(point, ecSpec);

            ECDsaSigner signer = new ECDsaSigner(kCalculator);

            signer.Init(false, keyParameters);

            var pubKey1 = new BigInteger(transaction.Signature[0], 16);
            var pubKey2 = new BigInteger(transaction.Signature[1], 16);

            TransactionRaw transactionRaw = new TransactionRaw()
            {
                FromAddress = transaction.FromAddress,
                ToAddress   = transaction.ToAddress,
                Amount      = transaction.Amount,
                DateCreated = transaction.DateCreated
            };

            string tranJson = JsonConvert.SerializeObject(transactionRaw);

            byte[] tranHash = CryptoUtil.CalcSHA256(tranJson);

            return(signer.VerifySignature(tranHash, pubKey1, pubKey2));
        }
Esempio n. 3
0
        public bool IsValidateHash(Transaction transaction)
        {
            TransactionRaw txRaw = new TransactionRaw();

            txRaw.ToAddress   = transaction.ToAddress;
            txRaw.FromAddress = transaction.FromAddress;
            txRaw.Amount      = transaction.Amount;
            txRaw.DateCreated = transaction.DateCreated;

            string jsonTx = JsonConvert.SerializeObject(txRaw);
            string txHash = CryptoUtil.CalcSHA256String(jsonTx);

            return(transaction.TransactionHash == txHash);
        }