/* TODO: The external service is giving security errors for connection attempt to its testnet.
         *
         * [Fact]
         * [Trait("UnitTest", "UnitTest")]
         * public void CanFetchTransactionFromCoinprism()
         * {
         *  CanFetchTransactionFromCoinprismCore("CanColorizeIssuanceTransaction");
         *  CanFetchTransactionFromCoinprismCore("CanColorizeTransferTransaction");
         *  Assert.Null(new CoinprismColoredTransactionRepository().Get(uint256.Parse("b4399a545c4ddd640920d63af75e7367fe4d94b2d7f7a3423105e25ac5f165a5")));
         #if !NOCUSTOMSSLVALIDATION
         *  Assert.Null(new CoinprismColoredTransactionRepository(Network.TestNet).Get(uint256.Parse("b4399a545c4ddd640920d63af75e7367fe4d94b2d7f7a3423105e25ac5f165a5")));
         *  Assert.NotNull(new CoinprismColoredTransactionRepository(Network.TestNet).Get(uint256.Parse("100972a4a519c6a40f6aa30bf0f89c1378c2a90a2a45715ec955d09fbf4d2253")));
         #endif
         * }
         */

        private void CanFetchTransactionFromCoinprismCore(string test)
        {
            ColoredCoinTester  tester   = CreateTester(test);
            ColoredTransaction expected = ColoredTransaction.FetchColors(tester.TestedTxId, tester.Repository);
            ColoredTransaction actual   = new CoinprismColoredTransactionRepository().Get(tester.TestedTxId);

            Assert.True(actual.ToBytes().SequenceEqual(expected.ToBytes()));
        }
            public DynamicTableEntity CreateTableEntity()
            {
                var entity = new DynamicTableEntity();

                entity.ETag         = "*";
                entity.PartitionKey = PartitionKey;
                entity.RowKey       = TxId + "-" + TypeLetter + "-" + BlockId;
                if (Transaction != null)
                {
                    Helper.SetEntityProperty(entity, "a", Transaction.ToBytes());
                }
                if (ColoredTransaction != null)
                {
                    Helper.SetEntityProperty(entity, "b", ColoredTransaction.ToBytes());
                }
                Helper.SetEntityProperty(entity, "c", Helper.SerializeList(PreviousTxOuts));
                return(entity);
            }
            public DynamicTableEntity CreateTableEntity()
            {
                var entity = new DynamicTableEntity();

                entity.ETag         = "*";
                entity.PartitionKey = PartitionKey;
                entity.RowKey       = TxId + "-" + TypeLetter + "-" + BlockId;
                if (Transaction != null)
                {
                    Helper.SetEntityProperty(entity, "a", Transaction.ToBytes());
                }
                if (ColoredTransaction != null)
                {
                    Helper.SetEntityProperty(entity, "b", ColoredTransaction.ToBytes());
                }
                Helper.SetEntityProperty(entity, "c", Helper.SerializeList(PreviousTxOuts, Transaction?.GetConsensusFactory() ?? PreviousTxOuts.Select(t => t.GetConsensusFactory()).FirstOrDefault()));
                Helper.SetEntityProperty(entity, "d", Utils.ToBytes((ulong)Timestamp.UtcTicks, true));
                return(entity);
            }
Пример #4
0
        internal DynamicTableEntity ToEntity()
        {
            DynamicTableEntity entity = new DynamicTableEntity();

            entity.ETag         = "*";
            entity.PartitionKey = PartitionKey;

            var locator = CreateBalanceLocator();

            entity.RowKey = BalanceId + "-" + locator.ToString(true);

            entity.Properties.Add("s", new EntityProperty(SeenUtc));
            Helper.SetEntityProperty(entity, "ss", Helper.SerializeList(SpentIndices.Select(e => new IntCompactVarInt(e))));

            Helper.SetEntityProperty(entity, "a", Helper.SerializeList(SpentOutpoints));
            if (SpentCoins != null)
            {
                Helper.SetEntityProperty(entity, "b", Helper.SerializeList(SpentCoins.Select(c => new Spendable(c.Outpoint, c.TxOut))));
            }
            Helper.SetEntityProperty(entity, "c", Helper.SerializeList(ReceivedCoins.Select(e => new IntCompactVarInt(e.Outpoint.N))));
            Helper.SetEntityProperty(entity, "d", Helper.SerializeList(ReceivedCoins.Select(e => e.TxOut)));
            var flags = (HasOpReturn ? "o" : "n") + (IsCoinbase ? "o" : "n");

            entity.Properties.AddOrReplace("e", new EntityProperty(flags));
            entity.Properties.AddOrReplace("f", new EntityProperty(Helper.Serialize(MatchedRules)));
            if (ColoredTransaction != null)
            {
                entity.Properties.AddOrReplace("g", new EntityProperty(ColoredTransaction.ToBytes()));
            }
            if (ScriptPubKey != null && !BalanceId.ContainsScript)
            {
                var bytes = ScriptPubKey.ToBytes(true);
                if (bytes.Length < 63000)
                {
                    entity.Properties.Add("h", new EntityProperty(bytes));
                }
            }
            if (CustomData != null)
            {
                Helper.SetEntityProperty(entity, "cu", Encoding.UTF8.GetBytes(CustomData));
            }
            return(entity);
        }
        public override ITableEntity ToEntity()
        {
            var entity = new DynamicTableEntity
            {
                PartitionKey = PartitionKey,
                RowKey       = $"{TxId}-{BlockId}"
            };

            if (Transaction != null)
            {
                Helper.SetEntityProperty(entity, "tx", Transaction.ToBytes());
            }

            if (ColoredTransaction != null)
            {
                Helper.SetEntityProperty(entity, "ctx", ColoredTransaction.ToBytes());
            }

            Helper.SetEntityProperty(entity, "previoustxouts", Helper.SerializeList(PreviousTxOuts));
            Helper.SetEntityProperty(entity, "time", NBitcoin.Utils.ToBytes((ulong)Timestamp.UtcTicks, true));

            return(entity);
        }
Пример #6
0
        //https://github.com/OpenAssets/open-assets-protocol/blob/master/specification.mediawiki
        public void CanColorizeSpecScenario()
        {
            var   repo    = new NoSqlColoredTransactionRepository(KnownNetworks.Main);
            Money dust    = Money.Parse("0.00005");
            var   colored = new ColoredTransaction();
            var   a1      = new AssetKey(this.networkMain);
            var   a2      = new AssetKey(this.networkMain);
            var   h       = new AssetKey(this.networkMain);
            BitcoinPubKeyAddress sender   = new Key().PubKey.GetAddress(this.networkMain);
            BitcoinPubKeyAddress receiver = new Key().PubKey.GetAddress(this.networkMain);

            colored.Marker = new ColorMarker(new ulong[] { 0, 10, 6, 0, 7, 3 });
            colored.Inputs.Add(new ColoredEntry(0, new AssetMoney(a1.Id, 3UL)));
            colored.Inputs.Add(new ColoredEntry(1, new AssetMoney(a1.Id, 2UL)));
            colored.Inputs.Add(new ColoredEntry(3, new AssetMoney(a1.Id, 5UL)));
            colored.Inputs.Add(new ColoredEntry(4, new AssetMoney(a1.Id, 3UL)));
            colored.Inputs.Add(new ColoredEntry(5, new AssetMoney(a2.Id, 9UL)));

            colored.Issuances.Add(new ColoredEntry(1, new AssetMoney(h.Id, 10UL)));
            colored.Transfers.Add(new ColoredEntry(3, new AssetMoney(a1.Id, 6UL)));
            colored.Transfers.Add(new ColoredEntry(5, new AssetMoney(a1.Id, 7UL)));
            colored.Transfers.Add(new ColoredEntry(6, new AssetMoney(a2.Id, 3UL)));
            AssetMoney[] destroyed = colored.GetDestroyedAssets();
            Assert.True(destroyed.Length == 1);
            Assert.True(destroyed[0].Quantity == 6);
            Assert.True(destroyed[0].Id == a2.Id);
            colored   = colored.Clone();
            destroyed = colored.GetDestroyedAssets();
            Assert.True(destroyed.Length == 1);
            Assert.True(destroyed[0].Quantity == 6);
            Assert.True(destroyed[0].Id == a2.Id);

            Transaction prior = this.networkMain.CreateTransaction();

            prior.Outputs.Add(new TxOut(dust, a1.ScriptPubKey));
            prior.Outputs.Add(new TxOut(dust, a2.ScriptPubKey));
            prior.Outputs.Add(new TxOut(dust, h.ScriptPubKey));
            repo.Transactions.Put(prior.GetHash(), prior);

            Transaction issuanceA1 = this.networkMain.CreateTransaction();

            issuanceA1.Inputs.Add(new TxIn(new OutPoint(prior.GetHash(), 0)));
            issuanceA1.Outputs.Add(new TxOut(dust, h.ScriptPubKey));
            issuanceA1.Outputs.Add(new TxOut(dust, sender));
            issuanceA1.Outputs.Add(new TxOut(dust, sender));
            issuanceA1.Outputs.Add(new TxOut(dust, sender));
            issuanceA1.Outputs.Add(new TxOut(dust, new ColorMarker(new ulong[] { 3, 2, 5, 3 }).GetScript()));
            repo.Transactions.Put(issuanceA1.GetHash(), issuanceA1);

            Transaction issuanceA2 = this.networkMain.CreateTransaction();

            issuanceA2.Inputs.Add(new TxIn(new OutPoint(prior.GetHash(), 1)));
            issuanceA2.Outputs.Add(new TxOut(dust, sender));
            issuanceA2.Outputs.Add(new TxOut(dust, new ColorMarker(new ulong[] { 9 }).GetScript()));
            repo.Transactions.Put(issuanceA2.GetHash(), issuanceA2);

            Transaction        testedTx      = CreateSpecTransaction(repo, dust, receiver, prior, issuanceA1, issuanceA2);
            ColoredTransaction actualColored = testedTx.GetColoredTransaction(repo);

            Assert.True(colored.ToBytes().SequenceEqual(actualColored.ToBytes()));

            //Finally, for each transfer output, if the asset units forming that output all have the same asset address, the output gets assigned that asset address. If any output contains units from more than one distinct asset address, the whole transaction is considered invalid, and all outputs are uncolored.
            Transaction testedBadTx = CreateSpecTransaction(repo, dust, receiver, prior, issuanceA1, issuanceA2);

            testedBadTx.Outputs[2] = new TxOut(dust, new ColorMarker(new ulong[] { 0, 10, 6, 0, 6, 4 }).GetScript());
            repo.Transactions.Put(testedBadTx.GetHash(), testedBadTx);
            colored = testedBadTx.GetColoredTransaction(repo);

            destroyed = colored.GetDestroyedAssets();
            Assert.True(destroyed.Length == 2);
            Assert.True(destroyed[0].Id == a1.Id);
            Assert.True(destroyed[0].Quantity == 13);
            Assert.True(destroyed[1].Id == a2.Id);
            Assert.True(destroyed[1].Quantity == 9);

            //If there are more items in the  asset quantity list  than the number of colorable outputs, the transaction is deemed invalid, and all outputs are uncolored.
            testedBadTx            = CreateSpecTransaction(repo, dust, receiver, prior, issuanceA1, issuanceA2);
            testedBadTx.Outputs[2] = new TxOut(dust, new ColorMarker(new ulong[] { 0, 10, 6, 0, 7, 4, 10, 10 }).GetScript());
            repo.Transactions.Put(testedBadTx.GetHash(), testedBadTx);

            colored = testedBadTx.GetColoredTransaction(repo);

            destroyed = colored.GetDestroyedAssets();
            Assert.True(destroyed.Length == 2);
            Assert.True(destroyed[0].Id == a1.Id);
            Assert.True(destroyed[0].Quantity == 13);
            Assert.True(destroyed[1].Id == a2.Id);
            Assert.True(destroyed[1].Quantity == 9);
        }