Пример #1
0
        /// <summary>
        /// Signs the given transaction with the given private key.
        /// </summary>
        /// <param name="transaction"></param>
        /// <param name="txInIndex"></param>
        /// <param name="privateKey"></param>
        /// <param name="unspentTxOuts"></param>
        /// <returns></returns>
        public string SignTxIn(Transaction transaction, int txInIndex, string privateKey, IList <UnspentTxOut> unspentTxOuts)
        {
            TxIn txIn = transaction.TxIns[txInIndex];

            string       dataToSign             = transaction.Id;
            UnspentTxOut referencedUnspentTxOut = FindUnspentTxOut(txIn.TxOutId, txIn.TxOutIndex, unspentTxOuts);

            if (referencedUnspentTxOut == null)
            {
                this.logger.LogCritical("Could not find referenced txOut");
                throw new InvalidOperationException("Could not find referenced txOut");
            }

            string referencedAddress = referencedUnspentTxOut.Address;

            if (Crypto.GetPublicKey(privateKey) != referencedAddress)
            {
                this.logger.LogCritical("Trying to sign an input with private key that does not match the address that is referenced in txIn.");
                throw new InvalidOperationException("Trying to sign an input with private key that does not match the address that is referenced in txIn.");
            }

            string signature = Crypto.GetSignature(dataToSign, privateKey);

            return(signature);
        }
Пример #2
0
        private bool IsValidTxIn(TxIn txIn, Transaction transaction, IList <UnspentTxOut> unspentTxOuts)
        {
            UnspentTxOut referencedUnspentTxOut = unspentTxOuts
                                                  .FirstOrDefault(uTxOut => uTxOut.TxOutId == txIn.TxOutId && uTxOut.TxOutIndex == txIn.TxOutIndex);

            if (referencedUnspentTxOut == null)
            {
                this.logger.LogError($"Referenced txOut not found: {JsonConvert.SerializeObject(txIn)}");
                return(false);
            }

            string address          = referencedUnspentTxOut.Address;
            bool   isValidSignature = Crypto.VerifySignature(address, transaction.Id, txIn.Signature);

            if (!isValidSignature)
            {
                this.logger.LogError($"Invalid txIn signature: {txIn.Signature} txId: {transaction.Id} address: {address}");
                return(false);
            }

            return(true);
        }