Esempio n. 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.
        }
        /// <summary>
        ///     Generates the bytes.
        /// </summary>
        /// <returns>System.Byte[].</returns>
        internal override byte[] GenerateBytes()
        {
            byte flags = 0;

            if (Properties.IsSupplyMutable)
            {
                flags += 1;
            }

            if (Properties.IsTransferable)
            {
                flags += 2;
            }

            if (Properties.IsLevyMutable)
            {
                flags += 4;
            }

            var builder = new FlatBufferBuilder(1);


            IList <KeyValuePair <MosaicPropertyId, ulong> > propertyList = new List <KeyValuePair <MosaicPropertyId, ulong> >();

            var duration = Properties.Duration;

            if (duration > 0)
            {
                propertyList.Add(new KeyValuePair <MosaicPropertyId, ulong>(MosaicPropertyId.DURATION, duration));
            }

            var mosaicProperties = new Offset <MosaicProperty> [propertyList.Count];

            for (var index = 0; index < propertyList.Count; index++)
            {
                KeyValuePair <MosaicPropertyId, ulong> mp = propertyList[index];
                var valueOffset = MosaicProperty.CreateValueVector(builder, mp.Value.ToUInt8Array());
                MosaicProperty.StartMosaicProperty(builder);
                MosaicProperty.AddMosaicPropertyId(builder, mp.Key.GetValueInByte());
                MosaicProperty.AddValue(builder, valueOffset);
                mosaicProperties[index] = MosaicProperty.EndMosaicProperty(builder);
            }



            // create vectors
            var signatureVector = MosaicDefinitionTransactionBuffer.CreateSignatureVector(builder, new byte[64]);
            var signerVector    = MosaicDefinitionTransactionBuffer.CreateSignerVector(builder, GetSigner());
            var feeVector       = MosaicDefinitionTransactionBuffer.CreateMaxFeeVector(builder, MaxFee?.ToUInt8Array());
            var deadlineVector  =
                MosaicDefinitionTransactionBuffer.CreateDeadlineVector(builder, Deadline.Ticks.ToUInt8Array());
            var mosaicIdVector =
                MosaicDefinitionTransactionBuffer.CreateMosaicIdVector(builder, MosaicId.Id.ToUInt8Array());
            var optionalPropertiesVector =
                MosaicDefinitionTransactionBuffer.CreateOptionalPropertiesVector(builder, mosaicProperties);

            // create version
            var version = (uint)GetTxVersionSerialization();

            // header + nonce + id + numOptProp + flags + divisibility + (id + value)*numOptProp
            var totalSize = GetSerializedSize(); //HEADER_SIZE + 4 + 8 + 1 + 1 + 1 + (1 + 8) * mosaicProperties.Length;

            // add vectors to buffer
            MosaicDefinitionTransactionBuffer.StartMosaicDefinitionTransactionBuffer(builder);
            MosaicDefinitionTransactionBuffer.AddSize(builder, (uint)totalSize);
            MosaicDefinitionTransactionBuffer.AddSignature(builder, signatureVector);
            MosaicDefinitionTransactionBuffer.AddSigner(builder, signerVector);
            MosaicDefinitionTransactionBuffer.AddVersion(builder, (uint)version);
            MosaicDefinitionTransactionBuffer.AddType(builder, TransactionType.GetValue());
            MosaicDefinitionTransactionBuffer.AddMaxFee(builder, feeVector);
            MosaicDefinitionTransactionBuffer.AddDeadline(builder, deadlineVector);

            MosaicDefinitionTransactionBuffer.AddMosaicNonce(builder, BitConverter.ToUInt32(MosaicNonce.Nonce, 0));
            MosaicDefinitionTransactionBuffer.AddMosaicId(builder, mosaicIdVector);
            MosaicDefinitionTransactionBuffer.AddNumOptionalProperties(builder, (byte)mosaicProperties.Length);
            MosaicDefinitionTransactionBuffer.AddFlags(builder, flags);
            MosaicDefinitionTransactionBuffer.AddDivisibility(builder, (byte)Properties.Divisibility);
            MosaicDefinitionTransactionBuffer.AddOptionalProperties(builder, optionalPropertiesVector);


            // Calculate size
            var codedMosaicDefinition = MosaicDefinitionTransactionBuffer.EndMosaicDefinitionTransactionBuffer(builder);

            builder.Finish(codedMosaicDefinition.Value);

            // validate size
            var output = new MosaicDefinitionTransactionSchema().Serialize(builder.SizedByteArray());

            if (output.Length != totalSize)
            {
                throw new SerializationException("Serialized form has incorrect length");
            }

            return(output);
        }