示例#1
0
        public static Transaction BuildTransaction(Address adrTo = null, ulong nonce = 0, ECKeyPair keyPair = null)
        {
            keyPair = keyPair ?? new KeyPairGenerator().Generate();

            var tx = new Transaction();

            tx.From        = keyPair.GetAddress();
            tx.To          = adrTo ?? Address.FromRawBytes(Hash.Generate().ToByteArray());
            tx.IncrementId = nonce;
            tx.Sig         = new Signature
            {
                P = ByteString.CopyFrom(keyPair.PublicKey.Q.GetEncoded())
            };
            tx.Fee        = TxPoolConfig.Default.FeeThreshold + 1;
            tx.MethodName = "hello world";
            tx.Params     = ByteString.CopyFrom(new Parameters
            {
                Params = { new Param
                           {
                               IntVal = 1
                           } }
            }.ToByteArray());

            // Serialize and hash the transaction
            Hash hash = tx.GetHash();

            // Sign the hash
            ECSigner    signer    = new ECSigner();
            ECSignature signature = signer.Sign(keyPair, hash.DumpByteArray());

            // Update the signature
            tx.Sig.R = ByteString.CopyFrom(signature.R);
            tx.Sig.S = ByteString.CopyFrom(signature.S);
            return(tx);
        }
示例#2
0
文件: Miner.cs 项目: wyk125/AElf
        /// <summary>
        /// Generate a system tx for parent chain block info and broadcast it.
        /// </summary>
        /// <returns></returns>
        private async Task GenerateTransactionWithParentChainBlockInfo()
        {
            var parentChainBlockInfo = GetParentChainBlockInfo();

            if (parentChainBlockInfo == null)
            {
                return;
            }
            try
            {
                var bn = await _blockChain.GetCurrentBlockHeightAsync();

                bn = bn > 4 ? bn - 4 : 0;
                var bh     = bn == 0 ? Hash.Genesis : (await _blockChain.GetHeaderByHeightAsync(bn)).GetHash();
                var bhPref = bh.Value.Where((x, i) => i < 4).ToArray();
                var tx     = new Transaction
                {
                    From = _keyPair.GetAddress(),
                    To   = AddressHelpers.GetSystemContractAddress(Config.ChainId,
                                                                   SmartContractType.SideChainContract.ToString()),
                    RefBlockNumber = bn,
                    RefBlockPrefix = ByteString.CopyFrom(bhPref),
                    MethodName     = "WriteParentChainBlockInfo",
                    Sig            = new Signature
                    {
                        P = ByteString.CopyFrom(_keyPair.GetEncodedPublicKey())
                    },
                    Type   = TransactionType.CrossChainBlockInfoTransaction,
                    Params = ByteString.CopyFrom(ParamsPacker.Pack(parentChainBlockInfo)),
                    Time   = Timestamp.FromDateTime(DateTime.UtcNow)
                };
                // sign tx
                var signature = new ECSigner().Sign(_keyPair, tx.GetHash().DumpByteArray());
                tx.Sig.R = ByteString.CopyFrom(signature.R);
                tx.Sig.S = ByteString.CopyFrom(signature.S);

                await InsertTransactionToPool(tx);

                _logger?.Trace($"Generated Cross chain info transaction {tx.GetHash()}");
            }
            catch (Exception e)
            {
                _logger?.Error(e, "PCB transaction generation failed.");
            }
        }
示例#3
0
        private Transaction GetTxToVerifyBlockProducer(Address contractAccountHash, ECKeyPair keyPair,
                                                       string recipientAddress, Timestamp timestamp, long roundId)
        {
            if (contractAccountHash == null || keyPair == null || recipientAddress == null)
            {
                _logger?.Error("Something wrong happened to consensus verification filter.");
                return(null);
            }

            var tx = new Transaction
            {
                From        = keyPair.GetAddress(),
                To          = contractAccountHash,
                IncrementId = 0,
                MethodName  = "Validation",
                Sig         = new Signature
                {
                    P = ByteString.CopyFrom(keyPair.PublicKey.Q.GetEncoded())
                },
                Params = ByteString.CopyFrom(ParamsPacker.Pack(
                                                 new StringValue {
                    Value = recipientAddress.RemoveHexPrefix()
                }.ToByteArray(),
                                                 timestamp.ToByteArray(),
                                                 new Int64Value {
                    Value = roundId
                }))
            };

            var signer    = new ECSigner();
            var signature = signer.Sign(keyPair, tx.GetHash().DumpByteArray());

            // Update the signature
            tx.Sig.R = ByteString.CopyFrom(signature.R);
            tx.Sig.S = ByteString.CopyFrom(signature.S);

            return(tx);
        }
示例#4
0
        private void SetBpConfig()
        {
            var producers = MinersConfig.Instance.Producers;

            // Set the list of block producers
            try
            {
                foreach (var bp in producers.Values)
                {
                    byte[] key = ByteArrayHelpers.FromHexString(bp["address"]);
                    _bpAddresses.Add(key);
                }
            }
            catch (Exception e)
            {
                _logger?.Error(e, "Error while reading mining info.");
            }

            _nodeKey = NetworkConfig.Instance.EcKeyPair;

            // This nodes key
            _isBp = _bpAddresses.Any(k => k.BytesEqual(_nodeKey.GetAddress().DumpByteArray()));
        }