public void ValidateTransactionSignature_will_pass_with_valid_transaction_signature()
        {
            var subbedLogger         = Substitute.For <ILogger>();
            var cryptoContext        = new FfiWrapper();
            var transactionValidator = new TransactionValidator(subbedLogger, cryptoContext);

            // build a valid transaction
            var privateKey = cryptoContext.GeneratePrivateKey();

            var validTransaction = new PublicEntry
            {
                SenderAddress = privateKey.GetPublicKey().Bytes.ToByteString()
            };

            var signingContext = new SigningContext
            {
                NetworkType   = NetworkType.Devnet,
                SignatureType = SignatureType.TransactionPublic
            };

            var signature = new Signature
            {
                // sign an actual TransactionBroadcast object
                RawBytes = cryptoContext.Sign(privateKey, validTransaction.ToByteArray(), signingContext.ToByteArray())
                           .SignatureBytes.ToByteString(),
                SigningContext = signingContext
            };

            validTransaction.Signature = signature;

            var result = transactionValidator.ValidateTransaction(validTransaction);

            result.Should().BeTrue();
        }
        public PoaConsensusTests(ITestOutputHelper output) : base(output)
        {
            ContainerProvider.ConfigureContainerBuilder(true, true, true);
            _scope = ContainerProvider.Container.BeginLifetimeScope(CurrentTestName);

            var context = new FfiWrapper();

            _endOfTestCancellationSource = new CancellationTokenSource();

            var poaNodeDetails = Enumerable.Range(0, 3).Select(i =>
            {
                var privateKey     = context.GeneratePrivateKey();
                var publicKey      = privateKey.GetPublicKey();
                var nodeSettings   = PeerSettingsHelper.TestPeerSettings(publicKey.Bytes, 2000 + i);
                var peerIdentifier = nodeSettings.PeerId;
                var name           = $"producer{i.ToString()}";
                return(new { index = i, name, privateKey, nodeSettings, peerIdentifier });
            }
                                                               ).ToList();

            var peerIdentifiers = poaNodeDetails.Select(n => n.peerIdentifier).ToList();

            _nodes = poaNodeDetails.Select(
                p => new PoaTestNode($"producer{p.index.ToString()}",
                                     p.privateKey,
                                     p.nodeSettings,
                                     peerIdentifiers.Except(new[] { p.peerIdentifier }),
                                     FileSystem,
                                     output)).ToList();
        }
Esempio n. 3
0
        public static BroadcastRawTransactionRequest GenerateTransaction(uint amount, int fee, int nonce = 0)
        {
            var cryptoWrapper = new FfiWrapper();
            var privateKey    = cryptoWrapper.GeneratePrivateKey();
            var publicKey     = ByteString.CopyFrom(privateKey.GetPublicKey().Bytes);

            var transaction = new TransactionBroadcast
            {
                PublicEntry = new PublicEntry
                {
                    Amount          = ((UInt256)amount).ToUint256ByteString(),
                    Nonce           = (ulong)nonce,
                    SenderAddress   = privateKey.GetPublicKey().Bytes.ToByteString(),
                    ReceiverAddress = publicKey,
                    TransactionFees = ((UInt256)fee).ToUint256ByteString(),
                    Timestamp       = Timestamp.FromDateTime(DateTime.UtcNow)
                }.Sign(cryptoWrapper, privateKey, DevNetPublicTransactionContext)
            };

            var broadcastRawTransactionRequest = new BroadcastRawTransactionRequest
            {
                Transaction = transaction
            };

            return(broadcastRawTransactionRequest);
        }
Esempio n. 4
0
        public void Can_Encode_And_Decode_Correctly()
        {
            var cryptoContext = new FfiWrapper();
            var privateKey    = cryptoContext.GeneratePrivateKey();
            var publicKey     = privateKey.GetPublicKey();

            var publicKeyAfterEncodeDecode = publicKey.Bytes.KeyToString().KeyToBytes();

            publicKeyAfterEncodeDecode.Should().BeEquivalentTo(publicKey.Bytes);
        }
Esempio n. 5
0
        public void Ed25519_precompile_can_verify_incorrect_sig()
        {
            HashProvider hashProvider      = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256"));
            FfiWrapper   cryptoContext     = new FfiWrapper();
            IPrivateKey  signingPrivateKey = cryptoContext.GeneratePrivateKey();
            IPrivateKey  otherPrivateKey   = cryptoContext.GeneratePrivateKey();

            Assert.AreNotEqual(signingPrivateKey, otherPrivateKey);

            var byteCode = PrepareEd25519PrecompileCall(hashProvider, cryptoContext, signingPrivateKey, otherPrivateKey);

            GethLikeTxTracer       txTracer   = RunVirtualMachine(byteCode);
            EthereumJsonSerializer serializer = new EthereumJsonSerializer();
            GethLikeTxTrace        trace      = txTracer.BuildResult();

            TestContext.WriteLine(serializer.Serialize(trace, true));
            trace.Entries.Last().Stack.First().Should().Be("0000000000000000000000000000000000000000000000000000000000000001");
            trace.Entries.Last().Memory.First().Should().StartWith("00");
        }
Esempio n. 6
0
        public void Init()
        {
            this.Setup(TestContext.CurrentContext);

            ContainerProvider.ConfigureContainerBuilder(true, true, true);

            var context = new FfiWrapper();

            _endOfTestCancellationSource = new CancellationTokenSource();

            var poaNodeDetails = Enumerable.Range(0, 3).Select(i =>
            {
                var fileSystem = Substitute.For <IFileSystem>();
                var path       = Path.Combine(FileSystem.GetCatalystDataDir().FullName, $"producer{i}");
                fileSystem.GetCatalystDataDir().Returns(new DirectoryInfo(path));

                var privateKey     = context.GeneratePrivateKey();
                var publicKey      = privateKey.GetPublicKey();
                var nodeSettings   = PeerSettingsHelper.TestPeerSettings(publicKey.Bytes, 2000 + i);
                var peerIdentifier = nodeSettings.PeerId;
                var name           = $"producer{i.ToString()}";
                var dfs            = TestDfs.GetTestDfs(fileSystem);
                return(new { index = i, name, privateKey, nodeSettings, peerIdentifier, dfs, fileSystem });
            }
                                                               ).ToList();

            var peerIdentifiers = poaNodeDetails.Select(n => n.peerIdentifier).ToList();

            _nodes = new List <PoaTestNode>();
            foreach (var nodeDetails in poaNodeDetails)
            {
                nodeDetails.dfs.Options.Discovery.BootstrapPeers = poaNodeDetails.Select(x => x.dfs.LocalPeer.Addresses.First());
                var node = new PoaTestNode(nodeDetails.name,
                                           nodeDetails.privateKey,
                                           nodeDetails.nodeSettings,
                                           nodeDetails.dfs,
                                           peerIdentifiers.Except(new[] { nodeDetails.peerIdentifier }),
                                           nodeDetails.fileSystem);

                _nodes.Add(node);
            }
        }