Example #1
0
        public bool IssueAssets(string issuanceInfo, byte[][] assetIds, string[] assetInfos, ulong tagId)
        {
            IssueAssetsBlock block         = (IssueAssetsBlock)CreateIssueAssetsBlock(issuanceInfo, assetIds, assetInfos, tagId);
            BlockBase        registerBlock = CreateRegisterBlock(block, null);

            return(_networkAdapter.SendTransaction(block, registerBlock));
        }
Example #2
0
        public bool IssueAssets(ICollection <KeyValuePair <string, string> > assetDetails, byte[] asstetId, ulong tagId = 0)
        {
            byte[][] arrays = new byte[1][];

            arrays[0] = asstetId;

            List <string> assetInfosList = new List <string>();

            assetDetails.ToList().ForEach(pair =>
            {
                assetInfosList.Add($"key: {pair.Key}@value: {pair.Value}|");
            });
            IssueAssetsBlock block         = (IssueAssetsBlock)CreateIssueAssetsBlock(null, arrays, assetInfosList.ToArray(), tagId);
            BlockBase        registerBlock = CreateRegisterBlock(block, null);

            return(_networkAdapter.SendTransaction(block, registerBlock));
        }
Example #3
0
        protected override Memory <byte> ParseTransactional(ushort version, Memory <byte> spanBody, uint assetCount, out TransactionalBlockBase transactionalBlockBase)
        {
            if (version == 1)
            {
                int readBytes = 0;

                uint count = BinaryPrimitives.ReadUInt32LittleEndian(spanBody.Span.Slice(readBytes));
                readBytes += sizeof(uint);

                byte[][] issuedAssetIds   = new byte[count][];
                string[] issuedAssetInfos = new string[count];

                for (int i = 0; i < count; i++)
                {
                    issuedAssetIds[i] = spanBody.Slice(readBytes, Globals.NODE_PUBLIC_KEY_SIZE).ToArray();
                    readBytes        += Globals.NODE_PUBLIC_KEY_SIZE;
                }

                for (int i = 0; i < count; i++)
                {
                    byte strLen = spanBody.Slice(readBytes, 1).ToArray()[0];
                    readBytes++;

                    issuedAssetInfos[i] = Encoding.ASCII.GetString(spanBody.Slice(readBytes, strLen).ToArray());
                    readBytes          += strLen;
                }

                byte strLen2 = spanBody.Slice(readBytes, 1).ToArray()[0];
                readBytes++;

                string issuanceInfo = Encoding.ASCII.GetString(spanBody.Slice(readBytes, strLen2).ToArray());
                readBytes += strLen2;

                transactionalBlockBase = new IssueAssetsBlock
                {
                    IssuedAssetIds  = issuedAssetIds,
                    IssuedAssetInfo = issuedAssetInfos,
                    IssuanceInfo    = issuanceInfo
                };

                return(spanBody.Slice(readBytes));
            }

            throw new BlockVersionNotSupportedException(version, BlockType);
        }
Example #4
0
        private BlockBase CreateIssueAssetsBlock(string issuanceInfo, byte[][] assetIds, string[] assetInfos, ulong tagId)
        {
            TransactionalBlockEssense transactionalBlockEssense = _networkAdapter.GetLastBlock(_clientState.GetPublicKey());

            IssueAssetsBlock issueAssetsBlock = new IssueAssetsBlock
            {
                BlockHeight     = transactionalBlockEssense.Height + 1,
                TagId           = tagId,
                UptodateFunds   = transactionalBlockEssense.UpToDateFunds,
                AssetIds        = new byte[0][],
                AssetAmounts    = new ulong[0],
                IssuedAssetIds  = assetIds,
                IssuedAssetInfo = assetInfos,
                IssuanceInfo    = issuanceInfo
            };

            FillHeightInfo(issueAssetsBlock);
            FillSyncData(issueAssetsBlock);
            FillRawData(issueAssetsBlock);

            return(issueAssetsBlock);
        }
Example #5
0
        public override void Start()
        {
            string  cmd     = null;
            Channel channel = new Channel("127.0.0.1", 5050, ChannelCredentials.Insecure);

            SyncManager.SyncManagerClient syncManagerClient = new SyncManager.SyncManagerClient(channel);
            TransactionalChainManager.TransactionalChainManagerClient transactionalChainManagerClient = new TransactionalChainManager.TransactionalChainManagerClient(channel);
            TransactionalBlockEssense transactionalBlockEssense = transactionalChainManagerClient.GetLastTransactionalBlock(
                new TransactionalBlockRequest {
                PublicKey = ByteString.CopyFrom(_key.Value.ToArray())
            });

            byte[] hashPrev      = transactionalBlockEssense.Hash.ToByteArray();
            ulong  blockHeight   = transactionalBlockEssense.Height + 1;
            ulong  uptodateFunds = transactionalBlockEssense.UpToDateFunds > 0 ? transactionalBlockEssense.UpToDateFunds : 100000;
            ulong  tagId         = 5005;

            byte[][] idCards = new byte[][]
            {
                GetComplementedAssetId(327483038),
                GetComplementedAssetId(327152054),
                GetComplementedAssetId(328051065),
            };

            string[] assetInfos = new string[]
            {
                "Kirill Gandyl",
                "Elena Zaychikov",
                "Etel Gandyl"
            };

            do
            {
                SyncBlockDescriptor lastSyncBlock = syncManagerClient.GetLastSyncBlock(new Empty());
                byte[] syncHash      = lastSyncBlock.Hash.ToByteArray();
                uint   nonce         = 1111;
                byte[] powHash       = GetPowHash(syncHash, nonce);
                byte[] targetAddress = new byte[32];

                IssueAssetsBlock issueAssetsBlock = new IssueAssetsBlock
                {
                    SyncBlockHeight = lastSyncBlock.Height,
                    BlockHeight     = blockHeight,
                    Nonce           = nonce,
                    PowHash         = powHash,
                    HashPrev        = hashPrev,
                    TagId           = tagId,
                    IssuanceInfo    = "Issue ID cards",
                    IssuedAssetIds  = idCards,
                    IssuedAssetInfo = assetInfos
                };

                ISerializer issueAssetsBlockSerializer = _signatureSupportSerializersFactory.Create(issueAssetsBlock);
                issueAssetsBlockSerializer.FillBodyAndRowBytes();

                RegistryRegisterBlock transactionRegisterBlock = new RegistryRegisterBlock
                {
                    SyncBlockHeight      = lastSyncBlock.Height,
                    BlockHeight          = blockHeight,
                    Nonce                = nonce,
                    PowHash              = powHash,
                    ReferencedPacketType = PacketType.Transactional,
                    ReferencedBlockType  = BlockTypes.Transaction_TransferFunds,
                    ReferencedBodyHash   = _hashCalculation.CalculateHash(issueAssetsBlock.RawData),
                    ReferencedTarget     = targetAddress
                };

                ISerializer transactionRegisterBlockSerializer = _signatureSupportSerializersFactory.Create(transactionRegisterBlock);

                _log.Info($"Sending message: {transactionRegisterBlockSerializer.GetBytes().ToHexString()}");

                _communicationService.PostMessage(_keyTarget, transactionRegisterBlockSerializer);
                _communicationService.PostMessage(_keyTarget, issueAssetsBlockSerializer);

                Console.WriteLine("Block sent. Press <Enter> for next or type 'exit' and press <Enter> for exit...");
                cmd = Console.ReadLine();

                blockHeight++;
                hashPrev = transactionRegisterBlock.ReferencedBodyHash;
            } while (!_cancellationToken.IsCancellationRequested && cmd != "exit");
        }
Example #6
0
        public void IssueAssetsBlockSerializerTest()
        {
            ulong tagId           = 113;
            ulong syncBlockHeight = 1;
            uint  nonce           = 4;

            byte[] powHash     = BinaryBuilder.GetPowHash(1234);
            ushort version     = 1;
            ulong  blockHeight = 9;

            byte[] prevHash = BinaryBuilder.GetDefaultHash(1234);
            byte[] body;

            ulong uptodateFunds    = 10001;
            uint  oldAssetIdsCount = 0;

            byte[][] assetIds     = new byte[oldAssetIdsCount][];
            ulong[]  assetAmounts = new ulong[oldAssetIdsCount];

            uint issuedAssetsCount = 10;

            byte[][] issuedAssetIds   = new byte[issuedAssetsCount][];
            string[] issuedAssetInfos = new string[issuedAssetsCount];
            string   issuanceInfo     = "Issuance Info";

            Random random = new Random();

            for (int i = 0; i < oldAssetIdsCount; i++)
            {
                assetIds[i]     = CryptoHelper.GetRandomSeed();
                assetAmounts[i] = (ulong)random.Next();
            }

            for (int i = 0; i < issuedAssetsCount; i++)
            {
                issuedAssetIds[i]   = CryptoHelper.GetRandomSeed();
                issuedAssetInfos[i] = $"Asset ID {i}";
            }

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.Write(tagId);
                    bw.Write(uptodateFunds);
                    bw.Write(oldAssetIdsCount);

                    for (int i = 0; i < oldAssetIdsCount; i++)
                    {
                        bw.Write(assetIds[i]);
                    }

                    for (int i = 0; i < oldAssetIdsCount; i++)
                    {
                        bw.Write(assetAmounts[i]);
                    }

                    bw.Write(issuedAssetsCount);

                    for (int i = 0; i < issuedAssetsCount; i++)
                    {
                        bw.Write(issuedAssetIds[i]);
                    }

                    for (int i = 0; i < issuedAssetsCount; i++)
                    {
                        bw.Write((byte)issuedAssetInfos[i].Length);
                        bw.Write(Encoding.ASCII.GetBytes(issuedAssetInfos[i]));
                    }

                    bw.Write((byte)issuanceInfo.Length);
                    bw.Write(Encoding.ASCII.GetBytes(issuanceInfo));
                }

                body = ms.ToArray();
            }

            byte[] expectedPacket = BinaryBuilder.GetSignedPacket(PacketType.Transactional, syncBlockHeight, nonce, powHash, version,
                                                                  BlockTypes.Transaction_IssueAssets, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature);

            IssueAssetsBlock block = new IssueAssetsBlock
            {
                SyncBlockHeight = syncBlockHeight,
                Nonce           = nonce,
                PowHash         = powHash,
                BlockHeight     = blockHeight,
                HashPrev        = prevHash,
                TagId           = tagId,
                UptodateFunds   = uptodateFunds,
                AssetIds        = assetIds,
                AssetAmounts    = assetAmounts,
                IssuedAssetIds  = issuedAssetIds,
                IssuedAssetInfo = issuedAssetInfos,
                IssuanceInfo    = issuanceInfo
            };

            IssueAssetsBlockSerializer serializer = new IssueAssetsBlockSerializer(_cryptoService, _identityKeyProvidersRegistry, _hashCalculationRepository);

            serializer.Initialize(block);

            byte[] actualPacket = serializer.GetBytes();

            Assert.Equal(expectedPacket, actualPacket);
        }
Example #7
0
 private void ProcessIssueAssetsBlock(IssueAssetsBlock issueAssetsBlock, ulong registryCombinedBlockHeight)
 {
     _dataAccessService.StoreIncomingTransactionalBlock(issueAssetsBlock.SyncBlockHeight, registryCombinedBlockHeight, issueAssetsBlock.BlockHeight,
                                                        issueAssetsBlock.BlockType, issueAssetsBlock.Signer.Value.Span, issueAssetsBlock.TagId, issueAssetsBlock.RawData.Span, null);
 }
Example #8
0
        public void IssueAssetsBlockParserTest()
        {
            ulong tagId           = 147;
            ulong syncBlockHeight = 1;
            uint  nonce           = 4;

            byte[] powHash     = BinaryBuilder.GetPowHash(1234);
            ushort version     = 1;
            ulong  blockHeight = 9;

            byte[] prevHash = BinaryBuilder.GetDefaultHash(1234);
            byte[] body;

            ulong uptodateFunds = 10001;

            uint ownedAssetIdsCount = 0;

            byte[][] assetIds     = new byte[ownedAssetIdsCount][];
            ulong[]  assetAmounts = new ulong[ownedAssetIdsCount];

            uint issuedAssetsCount = 10;

            byte[][] issuedAssetIds   = new byte[issuedAssetsCount][];
            string[] issuedAssetInfos = new string[issuedAssetsCount];
            string   issuanceInfo     = "Issuance Info";

            Random random = new Random();

            for (int i = 0; i < ownedAssetIdsCount; i++)
            {
                assetIds[i]     = CryptoHelper.GetRandomSeed();
                assetAmounts[i] = (ulong)random.Next();
            }

            for (int i = 0; i < issuedAssetsCount; i++)
            {
                issuedAssetIds[i]   = CryptoHelper.GetRandomSeed();
                issuedAssetInfos[i] = $"Issued Asset Id {i}";
            }

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.Write(tagId);
                    bw.Write(uptodateFunds);
                    bw.Write(ownedAssetIdsCount);

                    for (int i = 0; i < ownedAssetIdsCount; i++)
                    {
                        bw.Write(assetIds[i]);
                    }

                    for (int i = 0; i < ownedAssetIdsCount; i++)
                    {
                        bw.Write(assetAmounts[i]);
                    }

                    bw.Write(issuedAssetsCount);

                    for (int i = 0; i < issuedAssetsCount; i++)
                    {
                        bw.Write(issuedAssetIds[i]);
                    }

                    for (int i = 0; i < issuedAssetsCount; i++)
                    {
                        byte strLen = (byte)issuedAssetInfos[i].Length;
                        bw.Write(strLen);
                        bw.Write(Encoding.ASCII.GetBytes(issuedAssetInfos[i]));
                    }

                    bw.Write((byte)issuanceInfo.Length);
                    bw.Write(Encoding.ASCII.GetBytes(issuanceInfo));
                }

                body = ms.ToArray();
            }

            byte[] packet = BinaryBuilder.GetSignedPacket(PacketType.Transactional, syncBlockHeight, nonce, powHash, version,
                                                          BlockTypes.Transaction_IssueAssets, blockHeight, prevHash, body, _privateKey, out byte[] expectedSignature);

            IssueAssetsBlockParser parser = new IssueAssetsBlockParser(_hashCalculationRepository, _identityKeyProvidersRegistry);
            IssueAssetsBlock       block  = (IssueAssetsBlock)parser.Parse(packet);

            Assert.Equal(syncBlockHeight, block.SyncBlockHeight);
            Assert.Equal(nonce, block.Nonce);
            Assert.Equal(powHash, block.PowHash);
            Assert.Equal(version, block.Version);
            Assert.Equal(blockHeight, block.BlockHeight);
            Assert.Equal(prevHash, block.HashPrev);
            Assert.Equal(uptodateFunds, block.UptodateFunds);
            Assert.Equal(ownedAssetIdsCount, (uint)block.AssetIds.Length);

            for (int i = 0; i < ownedAssetIdsCount; i++)
            {
                Assert.Equal(assetIds[i], block.AssetIds[i]);
                Assert.Equal(assetAmounts[i], block.AssetAmounts[i]);
            }

            Assert.Equal(_publicKey, block.Signer.Value.ToArray());
            Assert.Equal(expectedSignature, block.Signature.ToArray());
        }