public void SaveMempoolDocument_Should_Not_Override_Existing_Record() { // this test seems pointless like this var expectedAmount = _transactionBroadcast.PublicEntries.Single().Amount; _memPool.Repository.CreateItem(Arg.Is(_transactionBroadcast)) .Returns(true); var saved = _memPool.Repository.CreateItem(_transactionBroadcast); saved.Should().BeTrue(); var overridingTransaction = _transactionBroadcast.Clone(); overridingTransaction.PublicEntries.Single().Amount = (expectedAmount.ToUInt256() + (UInt256)100).ToUint256ByteString(); _memPool.Repository.CreateItem(Arg.Is(overridingTransaction)) .Returns(false); var overriden = _memPool.Repository.CreateItem(overridingTransaction); overriden.Should().BeFalse(); _memPool.Repository.TryReadItem(Arg.Is(_transactionBroadcast.Signature.RawBytes)) .Returns(true); var retrievedTransaction = _memPool.Repository.TryReadItem(_transactionBroadcast.Signature.RawBytes); retrievedTransaction.Should().BeTrue(); }
public static TransactionBroadcast Sign(this TransactionBroadcast transaction, ICryptoContext cryptoContext, IPrivateKey privateKey, SigningContext context) { var clone = transaction.Clone(); if (transaction.Signature?.RawBytes.Length == cryptoContext.SignatureLength) { Logger.Debug("The transaction was already signed, returning a clone."); return(clone); } clone.Signature = null; var signatureBytes = cryptoContext.Sign(privateKey, clone.ToByteArray(), context.ToByteArray()).SignatureBytes; clone.Signature = new Signature { RawBytes = signatureBytes.ToByteString(), SigningContext = context }; return(clone); }
public static Signature GenerateSignature(IWrapper cryptoWrapper, IPrivateKey privateKey, TransactionBroadcast transactionBroadcast, SigningContext signingContext) { var transactionWithoutSig = transactionBroadcast.Clone(); transactionWithoutSig.Signature = null; var signature = cryptoWrapper.StdSign(privateKey, transactionWithoutSig.ToByteArray(), signingContext.ToByteArray()); var sig = new Signature { RawBytes = ByteString.CopyFrom(signature.SignatureBytes), SigningContext = signingContext }; return(sig); }
private bool ValidateTransactionSignature(TransactionBroadcast transactionBroadcast, NetworkType networkType) { if (transactionBroadcast.Signature.RawBytes == ByteString.Empty) { _logger.Error("Transaction signature is null"); return(false); } var transactionSignature = _cryptoContext.GetSignatureFromBytes(transactionBroadcast.Signature.RawBytes.ToByteArray(), transactionBroadcast.PublicEntries.First().Base.SenderPublicKey.ToByteArray()); var transactionWithoutSig = transactionBroadcast.Clone(); transactionWithoutSig.Signature = null; if (!_cryptoContext.Verify(transactionSignature, transactionWithoutSig.ToByteArray(), transactionBroadcast.Signature.SigningContext.ToByteArray())) { return(true); } _logger.Information( "Transaction Signature {signature} invalid.", transactionSignature); return(false); }