Beispiel #1
0
        public static async Task FindAsync()
        {
            // Create area code for a region by lat/lng and the precision
            // This example points to Hannover, Germany with a precision of 3.5m x 3.5m
            var areaCode = IotaAreaCode.Encode(52.37052, 9.73322, OpenLocationCode.CodePrecisionExtra);
            var bundles  = await IotaRepository.FindByAreaCodeAsync(areaCode);

            Console.WriteLine($"Bundle message:\n {bundles.First().GetMessages().First()}");
        }
Beispiel #2
0
        public void TestAreCodeDecodingNormalPrecision()
        {
            var areaCode    = new IotaAreaCode("NPHTQORL9XK");
            var iacAreaCode = areaCode.Decode();

            Assert.AreEqual(10, iacAreaCode.CodePrecision);
            Assert.AreEqual(52.5295625f, (float)iacAreaCode.Latitude);
            Assert.AreEqual(13.4130625f, (float)iacAreaCode.Longitude);
        }
Beispiel #3
0
        public void TestPartialAreaCodeIsInvalid()
        {
            Assert.IsFalse(IotaAreaCode.IsValidPartial("JAHAS"));
            Assert.IsFalse(IotaAreaCode.IsValidPartial("JAHASJAHAS"));

            Assert.IsFalse(IotaAreaCode.IsValidPartial("AA9"));
            Assert.IsFalse(IotaAreaCode.IsValidPartial("AAA9"));

            Assert.IsFalse(IotaAreaCode.IsValidPartial("BAAA9"));
        }
Beispiel #4
0
        public static async Task PublishAsync()
        {
            // Create area code for a region by lat/lng and the precision
            // This example points to Hannover, Germany with a precision of 3.5m x 3.5m
            var areaCode = IotaAreaCode.Encode(52.37052, 9.73322, OpenLocationCode.CodePrecisionExtra);
            var bundle   = await IotaRepository.PublishWithAreaCodeAsync(
                TryteString.FromAsciiString("Hello from Hannover! \n Have fun with geo locations on the tangle!"),
                areaCode);

            Console.WriteLine($"Bundle published. Hash: {bundle.Hash}");
        }
Beispiel #5
0
        public static string Calculate(IotaAreaCode areaCode, int newPrecision)
        {
            if (newPrecision == areaCode.CodePrecision)
            {
                return(areaCode.Value);
            }

            if (newPrecision < areaCode.CodePrecision)
            {
                var reduced = areaCode.Value.Replace("9", string.Empty).Substring(0, newPrecision);
                if (newPrecision <= 8)
                {
                    return($"{reduced}{string.Concat(Enumerable.Repeat('A', 8 - newPrecision))}9");
                }

                return($"{reduced.Substring(0, 8)}9{reduced.Substring(8)}");
            }

            return(IotaAreaCode.FromOpenLocationCode(OpenLocationCode.Encode(areaCode.Area.Latitude, areaCode.Area.Longitude, newPrecision)).Value);
        }
Beispiel #6
0
        public void TestSuccessfulPrecisionDecrease()
        {
            var areaCode = new IotaAreaCode("NPHTQORL9XKP");

            Assert.AreEqual(11, areaCode.Area.CodePrecision);

            Assert.AreEqual(10, areaCode.DecreasePrecision().CodePrecision);
            Assert.AreEqual("NPHTQORL9XK", areaCode.Value);

            Assert.AreEqual(8, areaCode.DecreasePrecision().Area.CodePrecision);
            Assert.AreEqual("NPHTQORL9", areaCode.Value);

            Assert.AreEqual(6, areaCode.DecreasePrecision().CodePrecision);
            Assert.AreEqual("NPHTQOAA9", areaCode.Value);

            Assert.AreEqual(4, areaCode.DecreasePrecision().CodePrecision);
            Assert.AreEqual("NPHTAAAA9", areaCode.Value);

            Assert.AreEqual(2, areaCode.DecreasePrecision().CodePrecision);
            Assert.AreEqual("NPAAAAAA9", areaCode.Value);
        }
Beispiel #7
0
        public void TestSuccessfulPrecisionIncrease()
        {
            var areaCode = new IotaAreaCode("NPAAAAAA9");

            Assert.AreEqual(2, areaCode.Area.CodePrecision);

            Assert.AreEqual(4, areaCode.IncreasePrecision().CodePrecision);
            Assert.AreEqual("NPQQAAAA9", areaCode.Value);

            Assert.AreEqual(6, areaCode.IncreasePrecision().Area.CodePrecision);
            Assert.AreEqual("NPQQQQAA9", areaCode.Value);

            Assert.AreEqual(8, areaCode.IncreasePrecision().CodePrecision);
            Assert.AreEqual("NPQQQQQQ9", areaCode.Value);

            Assert.AreEqual(10, areaCode.IncreasePrecision().CodePrecision);
            Assert.AreEqual("NPQQQQQQ9QQ", areaCode.Value);

            Assert.AreEqual(11, areaCode.IncreasePrecision().CodePrecision);
            Assert.AreEqual("NPQQQQQQ9QQQ", areaCode.Value);
        }
Beispiel #8
0
        public void TestAreCodeDecodingExtraPrecision()
        {
            var areaCode    = new IotaAreaCode("NPHTQORL9XKP");
            var iacAreaCode = areaCode.Decode();

            Assert.AreEqual(11, iacAreaCode.CodePrecision);
            Assert.AreEqual(52.52956250000001f, (float)iacAreaCode.Latitude);
            Assert.AreEqual(13.413046874999981f, (float)iacAreaCode.Longitude);
            Assert.AreEqual(52.529575000000015f, (float)iacAreaCode.LatitudeHigh);
            Assert.AreEqual(52.529550000000015f, (float)iacAreaCode.LatitudeLow);
            Assert.AreEqual(13.413062499999983f, (float)iacAreaCode.LongitudeHigh);
            Assert.AreEqual(13.413031249999982f, (float)iacAreaCode.LongitudeLow);

            Assert.AreEqual(11, areaCode.Area.CodePrecision);
            Assert.AreEqual(52.52956250000001f, (float)areaCode.Area.Latitude);
            Assert.AreEqual(13.413046874999981f, (float)areaCode.Area.Longitude);
            Assert.AreEqual(52.529575000000015f, (float)areaCode.Area.LatitudeHigh);
            Assert.AreEqual(52.529550000000015f, (float)areaCode.Area.LatitudeLow);
            Assert.AreEqual(13.413062499999983f, (float)areaCode.Area.LongitudeHigh);
            Assert.AreEqual(13.413031249999982f, (float)areaCode.Area.LongitudeLow);
        }
Beispiel #9
0
 public void TestPartialAreaCodeIsValid()
 {
     Assert.IsTrue(IotaAreaCode.IsValid("NPAAAAAA9"));
 }
Beispiel #10
0
        public void TestAreaCodeEncodingExtraPrecision()
        {
            var areaCode = IotaAreaCode.Encode(52.529562, 13.413047, OpenLocationCode.CodePrecisionExtra);

            Assert.AreEqual("NPHTQORL9XKP", areaCode.Value);
        }
Beispiel #11
0
 public void TestIotaAreaCodeIsValid()
 {
     Assert.IsTrue(IotaAreaCode.IsValid("NPHTQORL9XKP"));
 }
Beispiel #12
0
 public void TestIotaAreaCodeIsInvalidWithCorrectTrytes()
 {
     Assert.IsFalse(IotaAreaCode.IsValid("NPHTQORL999XKP"));
 }
Beispiel #13
0
 public void TestIotaAreaCodeIsInvalid()
 {
     Assert.IsFalse(IotaAreaCode.IsValid("JAHAS0"));
 }
Beispiel #14
0
        public void TestPrecisionIncreaseIsNotPossibleShouldThrowException()
        {
            var areaCode = new IotaAreaCode("NPHTQORL9XKP");

            areaCode.IncreasePrecision();
        }
Beispiel #15
0
        public void TestExtractCodeFromTrytes()
        {
            var areaCode = IotaAreaCode.Extract("NPHTQORL9XKP999999999");

            Assert.AreEqual("NPHTQORL9XKP", areaCode.Value);
        }
Beispiel #16
0
 public void TestInvalidConstructorValue()
 {
     var areaCode = new IotaAreaCode("NPHTQORL999XKP");
 }
Beispiel #17
0
 public void TestConversionToOpenLocationCode()
 {
     Assert.AreEqual("9F4MGCH7+R6F", IotaAreaCode.ToOpenLocationCode("NPHTQORL9XKP"));
     Assert.AreEqual("9F4MGCH7+R6F", new IotaAreaCode("NPHTQORL9XKP").ToOpenLocationCode());
 }
Beispiel #18
0
        public void TestAreaCodeEncodingNormalPrecision()
        {
            var areaCode = IotaAreaCode.Encode(52.529562, 13.413047);

            Assert.AreEqual("NPHTQORL9XK", areaCode.Value);
        }
        public static async Task <List <Bundle> > FindByAreaCodeAsync(this IIotaRepository repository, IotaAreaCode areaCode)
        {
            var result = await repository.FindTransactionsByTagsAsync(new List <Tag> {
                new Tag(areaCode.Value)
            });

            if (!result.Hashes.Any())
            {
                return(new List <Bundle>());
            }

            var transactionTrytes = await repository.GetTrytesAsync(result.Hashes);

            var transactions = transactionTrytes.Select(t => Transaction.FromTrytes(t)).ToList();

            var bundles = new List <Bundle>();

            foreach (var transaction in transactions)
            {
                if (bundles.Any(b => b.Hash.Value == transaction.BundleHash.Value))
                {
                    bundles.First(b => b.Hash.Value == transaction.BundleHash.Value).Transactions.Add(transaction);
                }
                else
                {
                    var bundle = new Bundle();
                    bundle.Transactions.Add(transaction);

                    bundles.Add(bundle);
                }
            }

            bundles.ForEach(b => b.Transactions = b.Transactions.OrderBy(t => t.CurrentIndex).ToList());

            return(bundles);
        }
Beispiel #20
0
 public void TestTrytesToExtractAreInvalidShouldThrowException()
 {
     IotaAreaCode.Extract("BNBPHBTQBOBRBLB9XKP999999999");
 }
        public static async Task <Bundle> PublishWithAreaCodeAsync(this IIotaRepository repository, TryteString message, IotaAreaCode areaCode, Address address = null)
        {
            if (address == null)
            {
                address = new Address(Seed.Random().Value);
            }

            var bundle = new Bundle();

            bundle.AddTransfer(new Transfer
            {
                Address   = address,
                Message   = message,
                Tag       = new Tag(areaCode.Value),
                Timestamp = Timestamp.UnixSecondsTimestamp
            });

            bundle.Finalize();
            bundle.Sign();

            var transactionTrytes = await repository.SendTrytesAsync(bundle.Transactions, 2);

            return(Bundle.FromTransactionTrytes(transactionTrytes, bundle.Hash));
        }
Beispiel #22
0
        public void TestPrecisionDecreaseIsNotPossibleShouldThrowException()
        {
            var areaCode = new IotaAreaCode("NPAAAAAA9");

            areaCode.DecreasePrecision();
        }
Beispiel #23
0
        public void TestSetPrecision()
        {
            var areaCode = new IotaAreaCode("NPHTQORL9").SetPrecision(4);

            Assert.AreEqual("NPHTAAAA9", areaCode.Value);
        }
Beispiel #24
0
        static void Main(string[] args)
        {
            var areaCode = IotaAreaCode.Encode(52.529562, 13.413047);

            Console.WriteLine($"IOTA Area Code {areaCode}");

            var areaCodeHighPrecision = IotaAreaCode.Encode(52.529562, 13.413047, OpenLocationCode.CodePrecisionExtra);

            Console.WriteLine($"IOTA Area Code High Precision {areaCodeHighPrecision}");

            // Using the Decode() method instead of the Area property is possible as well
            var codeArea = new IotaAreaCode("NPHTQORL9XKP").Area;

            Console.WriteLine($"IOTA Code Area {JsonConvert.SerializeObject(codeArea, Formatting.Indented)}");

            // IotaAreaCode.ToOpenLocationCode("NPHTQORL9XKP") is possible as well
            var openLocationCode = new IotaAreaCode("NPHTQORL9XKP").ToOpenLocationCode();

            Console.WriteLine($"Open Location Code {openLocationCode}");

            var areaCodeFromOpenLocationCode = IotaAreaCode.FromOpenLocationCode("X4HM+MM");

            Console.WriteLine($"IOTA Area Code {areaCodeFromOpenLocationCode}");

            var isValid1 = IotaAreaCode.IsValid("JAHAS0");

            Console.WriteLine($"isValid1 {isValid1}");

            var isValid2 = IotaAreaCode.IsValid("NPHTQORL9XKP");

            Console.WriteLine($"isValid2 {isValid2}");

            var isValidPartial1 = IotaAreaCode.IsValidPartial("JAHAS");

            Console.WriteLine($"isValidPartial1 {isValidPartial1}");

            var isValidPartial2 = IotaAreaCode.IsValidPartial("NPAAAAAA9");

            Console.WriteLine($"isValidPartial2 {isValidPartial2}");

            var extracted = IotaAreaCode.Extract("NPHTQORL9XKP999999999");

            Console.WriteLine($"Extracted {extracted}");

            var dimensions = IotaAreaCodeDimension.GetByPrecision(4);

            Console.WriteLine($"Dimensions {JsonConvert.SerializeObject(dimensions, Formatting.Indented)}");

            var increasedPrecision = new IotaAreaCode("NPHTQORL9").IncreasePrecision();

            Console.WriteLine($"Increase Precision {increasedPrecision}");

            var decreasedPrecision = new IotaAreaCode("NPHTQORL9").DecreasePrecision();

            Console.WriteLine($"Decrease Precision {decreasedPrecision}");

            var setPrecision = new IotaAreaCode("NPHTQORL9").SetPrecision(4);

            Console.WriteLine($"Set Precision {setPrecision}");

            Console.WriteLine("----------------");

            Repository.FindAsync().Wait();

            Console.ReadKey();
        }
Beispiel #25
0
        public void TestConversionFromOpenLocationCode()
        {
            var areaCode = IotaAreaCode.FromOpenLocationCode("X4HM+MM");

            Assert.AreEqual("ZHRT9TT", areaCode.Value);
        }