Example #1
0
        internal override byte[] GenerateBytes()
        {
            var builder = new FlatBufferBuilder(1);

            var signer                    = MosaicDefinitionTransactionBuffer.CreatePublicKeyVector(builder, GetSigner());
            var creatorVector             = MosaicDefinitionTransactionBuffer.CreateCreatorPublicKeyVector(builder, Creator.PublicKey.FromHex());
            var descriptionVector         = MosaicDefinitionTransactionBuffer.CreateDescriptionStringVector(builder, Encoding.UTF8.GetBytes(Description));
            var nammespaceVector          = MosaicDefinitionTransactionBuffer.CreateNamespaceIdStringVector(builder, Encoding.UTF8.GetBytes(Mosaic.NamespaceId));
            var mosaicNameVector          = MosaicDefinitionTransactionBuffer.CreateMosaicNameStringVector(builder, Encoding.UTF8.GetBytes(Mosaic.Name));
            var feeSinkVector             = MosaicDefinitionTransactionBuffer.CreateFeeSinkAddressVector(builder, Encoding.UTF8.GetBytes(new Address("53e140b5947f104cabc2d6fe8baedbc30ef9a0609c717d9613de593ec2a266d3", NetworkType).Plain));
            var definitionStructureLength = 60 + Encoding.UTF8.GetBytes(Mosaic.NamespaceId).Length + Encoding.UTF8.GetBytes(Mosaic.Name).Length + Encoding.UTF8.GetBytes(Description).Length;

            var propertiesOffset = new Offset <MosaicPropertyBuffer> [4];

            foreach (var property in Properties.Properties)
            {
                var name  = Encoding.Default.GetBytes(property.Item1);
                var value = Encoding.Default.GetBytes(property.Item2.ToLower());

                definitionStructureLength += 12 + name.Length + value.Length;

                var nameVector  = MosaicPropertyBuffer.CreatePropertyNameVector(builder, name);
                var valueVector = MosaicPropertyBuffer.CreatePropertyValueVector(builder, value);

                MosaicPropertyBuffer.StartMosaicPropertyBuffer(builder);

                MosaicPropertyBuffer.AddLengthOfPropertyStructure(builder, 8 + name.Length + value.Length);
                MosaicPropertyBuffer.AddLengthOfPropertyName(builder, name.Length);
                MosaicPropertyBuffer.AddPropertyName(builder, nameVector);
                MosaicPropertyBuffer.AddLengthOfPropertyValue(builder, value.Length);
                MosaicPropertyBuffer.AddPropertyValue(builder, valueVector);

                propertiesOffset[Properties.Properties.IndexOf(property)] = MosaicPropertyBuffer.EndMosaicPropertyBuffer(builder);
            }

            var mosaicPropertyVector = MosaicDefinitionTransactionBuffer.CreatePropertiesVector(builder, propertiesOffset);

            var levyVector = new Offset <MosaicLevyBuffer> [1];

            if (MosaicLevy != null)
            {
                var mosaicName    = Encoding.UTF8.GetBytes(MosaicLevy.Name);
                var namespaceName = Encoding.UTF8.GetBytes(MosaicLevy.NamespaceId);
                var recipient     = Encoding.UTF8.GetBytes(MosaicLevy.Recipient.Plain);

                definitionStructureLength += 68 + namespaceName.Length + mosaicName.Length;

                var mosaicLevyNameVector    = MosaicLevyBuffer.CreateMosaicNameStringVector(builder, mosaicName);
                var namespaceLevyNameVector = MosaicLevyBuffer.CreateNamespaceIdStringVector(builder, namespaceName);
                var recipientVector         = MosaicLevyBuffer.CreateRecipientAddressVector(builder, recipient);

                MosaicLevyBuffer.StartMosaicLevyBuffer(builder);

                MosaicLevyBuffer.AddLengthOfLevyStructure(builder, 68 + namespaceName.Length + mosaicName.Length);
                MosaicLevyBuffer.AddFeeType(builder, MosaicLevy.FeeType);
                MosaicLevyBuffer.AddLengthOfRecipientAddress(builder, 40);
                MosaicLevyBuffer.AddRecipientAddress(builder, recipientVector);
                MosaicLevyBuffer.AddLengthOfMosaicIdStructure(builder, 8 + namespaceName.Length + mosaicName.Length);
                MosaicLevyBuffer.AddLengthOfMosaicNamespaceId(builder, namespaceName.Length);
                MosaicLevyBuffer.AddNamespaceIdString(builder, namespaceLevyNameVector);
                MosaicLevyBuffer.AddLengthMosaicNameString(builder, mosaicName.Length);
                MosaicLevyBuffer.AddMosaicNameString(builder, mosaicLevyNameVector);
                MosaicLevyBuffer.AddFeeQuantity(builder, MosaicLevy.Fee);
                levyVector[0] = MosaicLevyBuffer.EndMosaicLevyBuffer(builder);
            }
            else
            {
                MosaicLevyBuffer.StartMosaicLevyBuffer(builder);
                MosaicLevyBuffer.AddLengthOfLevyStructure(builder, 0);
                levyVector[0] = MosaicLevyBuffer.EndMosaicLevyBuffer(builder);
            }

            var levyVectorOffset = MosaicDefinitionTransactionBuffer.CreateMosaicLevyVector(builder, levyVector);

            MosaicDefinitionTransactionBuffer.StartMosaicDefinitionTransactionBuffer(builder);

            var namespaceBytes   = Encoding.UTF8.GetBytes(Mosaic.NamespaceId);
            var mosaicNameBytes  = Encoding.UTF8.GetBytes(Mosaic.Name);
            var descriptionBytes = Encoding.UTF8.GetBytes(Description);

            MosaicDefinitionTransactionBuffer.AddTransactionType(builder, TransactionType.GetValue());
            MosaicDefinitionTransactionBuffer.AddVersion(builder, BitConverter.ToInt16(new byte[] { ExtractVersion(Version), 0 }, 0));
            MosaicDefinitionTransactionBuffer.AddNetwork(builder, BitConverter.ToInt16(new byte[] { 0, NetworkType.GetNetwork() }, 0));
            MosaicDefinitionTransactionBuffer.AddTimestamp(builder, NetworkTime.EpochTimeInMilliSeconds());
            MosaicDefinitionTransactionBuffer.AddPublicKeyLen(builder, 32);
            MosaicDefinitionTransactionBuffer.AddPublicKey(builder, signer);
            MosaicDefinitionTransactionBuffer.AddFee(builder, Fee);
            MosaicDefinitionTransactionBuffer.AddDeadline(builder, Deadline.TimeStamp);
            MosaicDefinitionTransactionBuffer.AddMosaicDefinitionStructureLength(builder, definitionStructureLength);
            MosaicDefinitionTransactionBuffer.AddLengthCreatorPublicKey(builder, 32);
            MosaicDefinitionTransactionBuffer.AddCreatorPublicKey(builder, creatorVector);
            MosaicDefinitionTransactionBuffer.AddLengthOfMosaicIdStructure(builder, 8 + namespaceBytes.Length + mosaicNameBytes.Length);
            MosaicDefinitionTransactionBuffer.AddLengthOfNamespaceIdString(builder, namespaceBytes.Length);
            MosaicDefinitionTransactionBuffer.AddNamespaceIdString(builder, nammespaceVector);
            MosaicDefinitionTransactionBuffer.AddLengthOfMosaicNameString(builder, mosaicNameBytes.Length);
            MosaicDefinitionTransactionBuffer.AddMosaicNameString(builder, mosaicNameVector);
            MosaicDefinitionTransactionBuffer.AddLengthOfDescription(builder, descriptionBytes.Length);
            MosaicDefinitionTransactionBuffer.AddDescriptionString(builder, descriptionVector);
            MosaicDefinitionTransactionBuffer.AddNoOfProperties(builder, 4);
            MosaicDefinitionTransactionBuffer.AddProperties(builder, mosaicPropertyVector);
            MosaicDefinitionTransactionBuffer.AddMosaicLevy(builder, levyVectorOffset);
            MosaicDefinitionTransactionBuffer.AddLenFeeSinkAddress(builder, 40);
            MosaicDefinitionTransactionBuffer.AddFeeSinkAddress(builder, feeSinkVector);
            MosaicDefinitionTransactionBuffer.AddFeeQuantity(builder, 10000000);

            var transction = MosaicDefinitionTransactionBuffer.EndMosaicDefinitionTransactionBuffer(builder);

            builder.Finish(transction.Value);

            var temp = new MosaicDefinitionTransactionSchema().Serialize(builder.SizedByteArray());

            return(MosaicLevy == null?temp.Take(temp.Length - 61).Concat(temp.Skip(temp.Length - 52)).ToArray() : temp);  // hackery to remove default flatbuffers values when no levy is included.
        }