Ejemplo n.º 1
0
        public Root HashTreeRoot(DepositMessage depositMessage)
        {
            Merkle.Ize(out UInt256 root, depositMessage);
            Span <byte> bytes = MemoryMarshal.Cast <UInt256, byte>(MemoryMarshal.CreateSpan(ref root, 1));

            return(new Root(bytes));
        }
Ejemplo n.º 2
0
        public static void SignDepositData(IServiceProvider testServiceProvider, DepositData depositData, byte[] privateKey, BeaconState?state)
        {
            SignatureDomains     signatureDomains    = testServiceProvider.GetService <IOptions <SignatureDomains> >().Value;
            BeaconChainUtility   beaconChainUtility  = testServiceProvider.GetService <BeaconChainUtility>();
            ICryptographyService cryptographyService = testServiceProvider.GetService <ICryptographyService>();

            Domain domain;

            if (state == null)
            {
                // Genesis
                domain = beaconChainUtility.ComputeDomain(signatureDomains.Deposit);
            }
            else
            {
                BeaconStateAccessor beaconStateAccessor = testServiceProvider.GetService <BeaconStateAccessor>();
                domain = beaconStateAccessor.GetDomain(state, signatureDomains.Deposit, Epoch.None);
            }

            DepositMessage depositMessage     = new DepositMessage(depositData.PublicKey, depositData.WithdrawalCredentials, depositData.Amount);
            Root           depositMessageRoot = cryptographyService.HashTreeRoot(depositMessage);
            Root           signingRoot        = beaconChainUtility.ComputeSigningRoot(depositMessageRoot, domain);
            BlsSignature   signature          = TestSecurity.BlsSign(signingRoot, privateKey);

            depositData.SetSignature(signature);
        }
Ejemplo n.º 3
0
        private static IEnumerable <SszElement> GetValues(DepositMessage item)
        {
            yield return(new SszBasicVector(item.PublicKey.AsSpan()));

            yield return(new SszBasicVector(item.WithdrawalCredentials.AsSpan()));

            yield return(new SszBasicElement(item.Amount));
        }
Ejemplo n.º 4
0
        public static void Ize(out UInt256 root, DepositMessage container)
        {
            Merkleizer merkleizer = new Merkleizer(2);

            merkleizer.Feed(container.PublicKey);
            merkleizer.Feed(container.WithdrawalCredentials);
            merkleizer.Feed(container.Amount);
            merkleizer.CalculateRoot(out root);
        }
Ejemplo n.º 5
0
        private DepositData BuildAndSignDepositData(ulong validatorIndex, Gwei amount, SignatureDomains signatureDomains)
        {
            InitialValues initialValues = _initialValueOptions.CurrentValue;

            byte[] privateKey = GeneratePrivateKey(validatorIndex);

            // Public Key
            BLSParameters blsParameters = new BLSParameters
            {
                PrivateKey = privateKey
            };

            using BLS bls = BLS.Create(blsParameters);
            byte[] publicKeyBytes = new byte[BlsPublicKey.Length];
            bls.TryExportBlsPublicKey(publicKeyBytes, out int publicKeyBytesWritten);
            BlsPublicKey publicKey = new BlsPublicKey(publicKeyBytes);

            // Withdrawal Credentials
            Bytes32 withdrawalCredentials = _crypto.Hash(publicKey.AsSpan());

            withdrawalCredentials.Unwrap()[0] = initialValues.BlsWithdrawalPrefix;

            // Build deposit data
            DepositData depositData = new DepositData(publicKey, withdrawalCredentials, amount, BlsSignature.Zero);

            // Sign deposit data
            Domain         domain         = _beaconChainUtility.ComputeDomain(signatureDomains.Deposit);
            DepositMessage depositMessage = new DepositMessage(
                depositData.PublicKey,
                depositData.WithdrawalCredentials,
                depositData.Amount);

            Root depositMessageRoot     = _crypto.HashTreeRoot(depositMessage);
            Root depositDataSigningRoot = _beaconChainUtility.ComputeSigningRoot(depositMessageRoot, domain);

            byte[] signatureBytes = new byte[96];
            bls.TrySignData(depositDataSigningRoot.AsSpan(), signatureBytes, out int bytesWritten);

            BlsSignature depositDataSignature = new BlsSignature(signatureBytes);

            depositData.SetSignature(depositDataSignature);

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                LogDebug.QuickStartAddValidator(_logger, validatorIndex, publicKey.ToString().Substring(0, 12),
                                                null);
            }

            return(depositData);
        }
Ejemplo n.º 6
0
        public async void Deposit_Valid_Amount()
        {
            DepositMessage command = new DepositMessage()
            {
                AccountId = Guid.NewGuid(),
                Amount    = 600
            };

            accountReadOnlyRepository
            .Get(command.AccountId)
            .Returns(new Account());

            Deposit sut = new Deposit(
                accountReadOnlyRepository,
                accountWriteOnlyRepository);

            Credit credit = await sut.Handle(command);

            Assert.Equal(command.Amount, credit.Amount.Value);
            Assert.Equal("Credit", credit.Description);
            Assert.True(credit.Id != Guid.Empty);
        }
Ejemplo n.º 7
0
 public Root HashTreeRoot(DepositMessage depositMessage)
 {
     throw new NotImplementedException();
 }
 public Root HashTreeRoot(DepositMessage depositMessage)
 {
     return(depositMessage.HashTreeRoot());
 }
Ejemplo n.º 9
0
        public async Task <IActionResult> Deposit([FromBody] DepositMessage message)
        {
            Credit credit = await deposit.Handle(message);

            return(Ok());
        }
Ejemplo n.º 10
0
 public static SszContainer ToSszContainer(this DepositMessage item)
 {
     return(new SszContainer(GetValues(item)));
 }
Ejemplo n.º 11
0
        public static Root HashTreeRoot(this DepositMessage item)
        {
            var tree = new SszTree(item.ToSszContainer());

            return(new Root(tree.HashTreeRoot()));
        }
        public Task <Eth1GenesisData> GetEth1GenesisDataAsync(CancellationToken cancellationToken)
        {
            QuickStartParameters quickStartParameters = _quickStartParameterOptions.CurrentValue;

            if (_logger.IsWarn())
            {
                Log.MockedQuickStart(_logger, quickStartParameters.GenesisTime, quickStartParameters.ValidatorCount,
                                     null);
            }

            GweiValues       gweiValues       = _gweiValueOptions.CurrentValue;
            InitialValues    initialValues    = _initialValueOptions.CurrentValue;
            TimeParameters   timeParameters   = _timeParameterOptions.CurrentValue;
            SignatureDomains signatureDomains = _signatureDomainOptions.CurrentValue;

            // Fixed amount
            Gwei amount = gweiValues.MaximumEffectiveBalance;

            // Build deposits
            List <DepositData> depositDataList = new List <DepositData>();
            List <Deposit>     deposits        = new List <Deposit>();

            for (ulong validatorIndex = 0uL; validatorIndex < quickStartParameters.ValidatorCount; validatorIndex++)
            {
                byte[] privateKey = GeneratePrivateKey(validatorIndex);

                // Public Key
                BLSParameters blsParameters = new BLSParameters()
                {
                    PrivateKey = privateKey
                };
                using BLS bls = BLS.Create(blsParameters);
                byte[] publicKeyBytes = new byte[BlsPublicKey.Length];
                bls.TryExportBlsPublicKey(publicKeyBytes, out int publicKeyBytesWritten);
                BlsPublicKey publicKey = new BlsPublicKey(publicKeyBytes);

                // Withdrawal Credentials
                byte[] withdrawalCredentialBytes = _cryptographyService.Hash(publicKey.AsSpan()).AsSpan().ToArray();
                withdrawalCredentialBytes[0] = initialValues.BlsWithdrawalPrefix;
                Bytes32 withdrawalCredentials = new Bytes32(withdrawalCredentialBytes);

                // Build deposit data
                DepositData depositData = new DepositData(publicKey, withdrawalCredentials, amount, BlsSignature.Zero);

                // Sign deposit data
                Domain         domain         = _beaconChainUtility.ComputeDomain(signatureDomains.Deposit);
                DepositMessage depositMessage = new DepositMessage(depositData.PublicKey,
                                                                   depositData.WithdrawalCredentials, depositData.Amount);
                Root   depositMessageRoot     = _cryptographyService.HashTreeRoot(depositMessage);
                Root   depositDataSigningRoot = _beaconChainUtility.ComputeSigningRoot(depositMessageRoot, domain);
                byte[] destination            = new byte[96];
                bls.TrySignData(depositDataSigningRoot.AsSpan(), destination, out int bytesWritten);
                BlsSignature depositDataSignature = new BlsSignature(destination);
                depositData.SetSignature(depositDataSignature);

                // Deposit

                // TODO: This seems a very inefficient way (copied from tests) as it recalculates the merkle tree each time
                // (you only need to add one node)

                // TODO: Add some tests around quick start, then improve

                int index = depositDataList.Count;
                depositDataList.Add(depositData);
                //int depositDataLength = (ulong) 1 << _chainConstants.DepositContractTreeDepth;
                Root root = _cryptographyService.HashTreeRoot(depositDataList);
                IEnumerable <Bytes32> allLeaves = depositDataList.Select(x =>
                                                                         new Bytes32(_cryptographyService.HashTreeRoot((DepositData)x).AsSpan()));
                IList <IList <Bytes32> > tree = CalculateMerkleTreeFromLeaves(allLeaves);


                IList <Bytes32> merkleProof = GetMerkleProof(tree, index, 32);
                List <Bytes32>  proof       = new List <Bytes32>(merkleProof);

                byte[] indexBytes = new byte[32];
                BinaryPrimitives.WriteInt32LittleEndian(indexBytes, index + 1);
                Bytes32 indexHash = new Bytes32(indexBytes);
                proof.Add(indexHash);
                Bytes32 leaf = new Bytes32(_cryptographyService.HashTreeRoot(depositData).AsSpan());
                _beaconChainUtility.IsValidMerkleBranch(leaf, proof, _chainConstants.DepositContractTreeDepth + 1,
                                                        (ulong)index, root);
                Deposit deposit = new Deposit(proof, depositData);

                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    LogDebug.QuickStartAddValidator(_logger, validatorIndex, publicKey.ToString().Substring(0, 12),
                                                    null);
                }

                deposits.Add(deposit);
            }

            ulong eth1Timestamp = quickStartParameters.Eth1Timestamp;

            if (eth1Timestamp == 0)
            {
                eth1Timestamp = quickStartParameters.GenesisTime - (ulong)(1.5 * timeParameters.MinimumGenesisDelay);
            }
            else
            {
                ulong minimumEth1TimestampInclusive =
                    quickStartParameters.GenesisTime - 2 * timeParameters.MinimumGenesisDelay;
                ulong maximumEth1TimestampInclusive =
                    quickStartParameters.GenesisTime - timeParameters.MinimumGenesisDelay - 1;
                if (eth1Timestamp < minimumEth1TimestampInclusive)
                {
                    if (_logger.IsEnabled(LogLevel.Warning))
                    {
                        Log.QuickStartEth1TimestampTooLow(_logger, eth1Timestamp, quickStartParameters.GenesisTime,
                                                          minimumEth1TimestampInclusive, null);
                    }
                    eth1Timestamp = minimumEth1TimestampInclusive;
                }
                else if (eth1Timestamp > maximumEth1TimestampInclusive)
                {
                    if (_logger.IsEnabled(LogLevel.Warning))
                    {
                        Log.QuickStartEth1TimestampTooHigh(_logger, eth1Timestamp, quickStartParameters.GenesisTime,
                                                           maximumEth1TimestampInclusive, null);
                    }
                    eth1Timestamp = maximumEth1TimestampInclusive;
                }
            }

            var eth1GenesisData = new Eth1GenesisData(quickStartParameters.Eth1BlockHash, eth1Timestamp,
                                                      deposits);

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                LogDebug.QuickStartGenesisDataCreated(_logger, eth1GenesisData.BlockHash, eth1GenesisData.Timestamp,
                                                      eth1GenesisData.Deposits.Count, null);
            }

            return(Task.FromResult(eth1GenesisData));
        }