Exemplo n.º 1
0
        public void Generate_Address()
        {
            //Generate default
            var address1 = AddressHelper.Base58StringToAddress("2DZER7qHVwv3PUMFsHuQaQbE4wDFsCRzJsxLwYEk8rgM3HVn1S");
            var address2 = AddressHelper.Base58StringToAddress("xFqJD9R33mQBQPr1hCFUZMayXFQ577j34MPyUdXzbPpAYufG2");

            address1.ShouldNotBeSameAs(address2);

            //Generate from String
            var address3 = AddressHelper.Base58StringToAddress("z1NVbziJbekvcza3Zr4Gt4eAvoPBZThB68LHRQftrVFwjtGVM");

            address3.ShouldNotBe(null);

            //Generate from byte
            var bytes    = Enumerable.Repeat((byte)0xEF, 32).ToArray();
            var address4 = Address.FromBytes(bytes);

            address4.ShouldNotBe(null);

            bytes = Enumerable.Repeat((byte)32, 20).ToArray();
            Should.Throw <ArgumentException>(() => { Address.FromBytes(bytes); });

            //Generate from public key
            var pk       = CryptoHelper.GenerateKeyPair().PublicKey;
            var address5 = Address.FromPublicKey(pk);

            address5.ShouldNotBe(null);
            address5.ToByteArray().Length.ShouldBe(32);
        }
Exemplo n.º 2
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var validationResults = new List <ValidationResult>();

            try
            {
                AddressHelper.Base58StringToAddress(From);
            }
            catch
            {
                validationResults.Add(new ValidationResult(Error.Message[Error.InvalidAddress],
                                                           new[] { nameof(From) }));
            }

            try
            {
                AddressHelper.Base58StringToAddress(To);
            }
            catch
            {
                validationResults.Add(
                    new ValidationResult(Error.Message[Error.InvalidAddress], new[] { nameof(To) }));
            }

            try
            {
                HashHelper.HexStringToHash(RefBlockHash);
            }
            catch
            {
                validationResults.Add(
                    new ValidationResult(Error.Message[Error.InvalidBlockHash], new[] { nameof(RefBlockHash) }));
            }
            return(validationResults);
        }
Exemplo n.º 3
0
        public async Task GetFormattedAddress_Test()
        {
            var result = await Client.GetFormattedAddress(AddressHelper.Base58StringToAddress(_address));

            _testOutputHelper.WriteLine(result);
            Assert.True(result == $"ELF_{_address}_AELF");
        }
Exemplo n.º 4
0
 private Transaction GetNonAcs2Transaction(ResourceInfo resourceInfo)
 {
     return(new Transaction()
     {
         From = AddressHelper.Base58StringToAddress("9Njc5pXW9Rw499wqSJzrfQuJQFVCcWnLNjZispJM4LjKmRPyq"),
         To = AddressHelper.Base58StringToAddress(InternalConstants.NonAcs2),
         MethodName = nameof(SmartContractExecution.Parallel.Tests.TestContract.TestContract.GetResourceInfo),
         Params = resourceInfo.ToByteString(),
         Signature = ByteString.CopyFromUtf8(KernelConstants.SignaturePlaceholder)
     });
 }
Exemplo n.º 5
0
        public void EncodePlain_And_DecodePlain_Address()
        {
            var address = AddressHelper.Base58StringToAddress("2DZER7qHVwv3PUMFsHuQaQbE4wDFsCRzJsxLwYEk8rgM3HVn1S");
            var data    = address.ToByteArray();

            var enCode = Base58CheckEncoding.EncodePlain(data);

            enCode.ShouldNotBe(string.Empty);
            var deCode = Base58CheckEncoding.DecodePlain(enCode);

            deCode.ShouldBe(data);
        }
        /// <summary>
        /// Get the protobuf definitions related to a contract
        /// </summary>
        /// <param name="address">contract address</param>
        /// <returns></returns>
        public async Task <byte[]> GetContractFileDescriptorSetAsync(string address)
        {
            try
            {
                var result = await GetFileDescriptorSetAsync(AddressHelper.Base58StringToAddress(address));

                return(result);
            }
            catch
            {
                throw new UserFriendlyException(Error.Message[Error.NotFound], Error.NotFound.ToString());
            }
        }
Exemplo n.º 7
0
        public void Parse_Address_FromString()
        {
            string addStr  = "ddnF1dEsp51QbASCqQKPZ7vs2zXxUxyu5BuGRKFQAsT9JKrra";
            var    address = AddressHelper.Base58StringToAddress(addStr);

            address.ShouldNotBe(null);
            var addStr1 = address.GetFormatted();

            addStr1.ShouldBe(addStr);

            addStr = "345678icdfvbghnjmkdfvgbhtn";
            Should.Throw <FormatException>(() => { address = AddressHelper.Base58StringToAddress(addStr); });
        }
Exemplo n.º 8
0
        public static ChainAddress Parse(string chainAddressString, string symbol)
        {
            var arr = chainAddressString.Split('_');

            if (arr[0] != symbol)
            {
                throw new ArgumentException("invalid chain address", nameof(chainAddressString));
            }

            var address = AddressHelper.Base58StringToAddress(arr[1]);
            var chainId = Base58CheckEncoding.Decode(arr[2]).ToInt32(false);

            return(new ChainAddress(address, chainId));
        }
Exemplo n.º 9
0
        public void FromTo_Test()
        {
            var t = new Transaction();

            t.From = AddressHelper.Base58StringToAddress("nGmKp2ekysABSZAzVfXDrmaTNTaSSrfNmDhuaz7RUj5RTCYqy");
            t.To   = AddressHelper.Base58StringToAddress("z1NVbziJbekvcza3Zr4Gt4eAvoPBZThB68LHRQftrVFwjtGVM");

            byte[] b = t.ToByteArray();
            b.ShouldNotBe(null);
            b.Length.ShouldBeGreaterThan(0);

            string bstr = b.ToHex();

            bstr.ShouldNotBe(string.Empty);
        }
Exemplo n.º 10
0
        public void Chain_Address()
        {
            var address       = AddressHelper.Base58StringToAddress("nGmKp2ekysABSZAzVfXDrmaTNTaSSrfNmDhuaz7RUj5RTCYqy");
            var chainId       = 0;
            var chainAddress1 = new ChainAddress(address, chainId);

            string str           = chainAddress1.GetFormatted("ELF", chainAddress1.ChainId);
            var    chainAddress2 = ChainAddress.Parse(str, "ELF");

            chainAddress1.Address.ShouldBe(chainAddress2.Address);
            chainAddress1.ChainId.ShouldBe(chainAddress2.ChainId);

            var strError = chainAddress1.ToString();

            Should.Throw <ArgumentException>(() => { chainAddress2 = ChainAddress.Parse(strError, "ELF"); });
        }
Exemplo n.º 11
0
        public void Encode_And_Decode_Address()
        {
            var address = AddressHelper.Base58StringToAddress("xFqJD9R33mQBQPr1hCFUZMayXFQ577j34MPyUdXzbPpAYufG2");
            var data    = address.ToByteArray();

            var enCode = Base58CheckEncoding.Encode(data);

            enCode.ShouldNotBe(string.Empty);
            var deCode = Base58CheckEncoding.Decode(enCode);

            deCode.ShouldBe(data);

            var deCode1 = Base58CheckEncoding.DecodePlain(enCode);

            deCode1.ShouldNotBe(data);
        }
Exemplo n.º 12
0
        public void Compare_Address()
        {
            var address1 = AddressHelper.Base58StringToAddress("z1NVbziJbekvcza3Zr4Gt4eAvoPBZThB68LHRQftrVFwjtGVM");
            var address2 = AddressHelper.Base58StringToAddress("nGmKp2ekysABSZAzVfXDrmaTNTaSSrfNmDhuaz7RUj5RTCYqy");

            address1.CompareTo(address2).ShouldNotBe(0);
            Should.Throw <InvalidOperationException>(() => { address1.CompareTo(null); });

            (address1 < null).ShouldBeFalse();
            (null < address2).ShouldBeTrue();
            (address1 > address2).ShouldBe(address1.CompareTo(address2) > 0);

            Address addressA = null;
            Address addressB = null;
            var     value    = addressA > addressB;

            value.ShouldBeFalse();
        }
Exemplo n.º 13
0
        public void Verify_Address()
        {
            var address          = AddressHelper.Base58StringToAddress("nGmKp2ekysABSZAzVfXDrmaTNTaSSrfNmDhuaz7RUj5RTCYqy");
            var formattedAddress = address.GetFormatted();

            AddressHelper.VerifyFormattedAddress(formattedAddress).ShouldBeTrue();

            AddressHelper.VerifyFormattedAddress(formattedAddress + "ER").ShouldBeFalse();
            AddressHelper.VerifyFormattedAddress("AE" + formattedAddress).ShouldBeFalse();

            var formattedAddressCharArray = formattedAddress.ToCharArray();

            formattedAddressCharArray[4] = 'F';
            AddressHelper.VerifyFormattedAddress(new string(formattedAddressCharArray)).ShouldBeFalse();

            AddressHelper.VerifyFormattedAddress("").ShouldBeFalse();
            AddressHelper.VerifyFormattedAddress("I0I0").ShouldBeFalse();
        }
Exemplo n.º 14
0
        private (List <Transaction>, List <Hash>) GenerateFakeTransactions(int count)
        {
            var transactions   = new List <Transaction>();
            var transactionIds = new List <Hash>();

            for (int i = 0; i < count; i++)
            {
                var transaction = new Transaction()
                {
                    From       = AddressHelper.Base58StringToAddress("z1NVbziJbekvcza3Zr4Gt4eAvoPBZThB68LHRQftrVFwjtGVM"),
                    To         = AddressHelper.Base58StringToAddress("2vNDCj1WjNLAXm3VnEeGGRMw3Aab4amVSEaYmCyxQKjNhLhfL7"),
                    MethodName = $"Test{i}",
                    Params     = ByteString.Empty
                };
                var hash = transaction.GetHash();

                transactions.Add(transaction);
                transactionIds.Add(hash);
            }

            return(transactions, transactionIds);
        }
Exemplo n.º 15
0
        public async Task InitContractInfoCacheAsync()
        {
            if (!_contractInfoCache.IsEmpty)
            {
                return;
            }

            var chainContractInfo = await _blockchainStateManager.GetChainContractInfoAsync();

            if (chainContractInfo.ContractInfos.IsNullOrEmpty())
            {
                return;
            }
            var chainStateInfo = await _blockchainStateManager.GetChainStateInfoAsync();

            chainContractInfo.ContractInfos.RemoveAll(c => c.Value <= chainStateInfo.BlockHeight);
            await _blockchainStateManager.SetChainContractInfoAsync(chainContractInfo);

            foreach (var key in chainContractInfo.ContractInfos.Keys)
            {
                _contractInfoCache[AddressHelper.Base58StringToAddress(key)] = chainContractInfo.ContractInfos[key];
            }
        }
Exemplo n.º 16
0
        public void PreCondition_Check_Test()
        {
            Func <Address> func1 = null;

            Should.Throw <ArgumentException>(() => Preconditions.CheckNotNull(func1));

            func1 = () => AddressHelper.Base58StringToAddress("z1NVbziJbekvcza3Zr4Gt4eAvoPBZThB68LHRQftrVFwjtGVM");
            var reference = Preconditions.CheckNotNull(func1);

            reference.ShouldNotBeNull();
            var addressInfo = reference();

            addressInfo.ShouldNotBeNull();
            addressInfo.GetType().ToString().ShouldBe("AElf.Types.Address");

            Func <Address, string> func2 = address => address.GetFormatted();
            var reference1 = Preconditions.CheckNotNull(func2, "address");

            reference1.ShouldNotBeNull();
            var result = reference1(AddressHelper.Base58StringToAddress("z1NVbziJbekvcza3Zr4Gt4eAvoPBZThB68LHRQftrVFwjtGVM"));

            result.ShouldNotBeNullOrEmpty();
        }
Exemplo n.º 17
0
        /// <summary>
        /// Creates an unsigned serialized transaction
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task <CreateRawTransactionOutput> CreateRawTransactionAsync(CreateRawTransactionInput input)
        {
            var transaction = new Transaction
            {
                From           = AddressHelper.Base58StringToAddress(input.From),
                To             = AddressHelper.Base58StringToAddress(input.To),
                RefBlockNumber = input.RefBlockNumber,
                RefBlockPrefix = ByteString.CopyFrom(HashHelper.HexStringToHash(input.RefBlockHash).Value.Take(4).ToArray()),
                MethodName     = input.MethodName
            };
            var methodDescriptor = await GetContractMethodDescriptorAsync(AddressHelper.Base58StringToAddress(input.To), input.MethodName);

            if (methodDescriptor == null)
            {
                throw new UserFriendlyException(Error.Message[Error.NoMatchMethodInContractAddress],
                                                Error.NoMatchMethodInContractAddress.ToString());
            }
            try
            {
                var parameters = methodDescriptor.InputType.Parser.ParseJson(input.Params);
                if (!IsValidMessage(parameters))
                {
                    throw new UserFriendlyException(Error.Message[Error.InvalidParams], Error.InvalidParams.ToString());
                }
                transaction.Params = parameters.ToByteString();
            }
            catch
            {
                throw new UserFriendlyException(Error.Message[Error.InvalidParams], Error.InvalidParams.ToString());
            }

            return(new CreateRawTransactionOutput
            {
                RawTransaction = transaction.ToByteArray().ToHex()
            });
        }