/// <summary>
        /// Generates the bytes.
        /// </summary>
        /// <returns>The transaction bytes.</returns>
        internal override byte[] GenerateBytes()
        {
            var builder = new FlatBufferBuilder(1);

            var signer          = TransferTransactionBuffer.CreatePublicKeyVector(builder, GetSigner());
            var recipientVector = TransferTransactionBuffer.CreateRecipientVector(builder, Encoding.UTF8.GetBytes(Address.Plain));

            if (Message == null)
            {
                Message = EmptyMessage.Create();
            }
            var messageBytePayload = Message.GetPayload();
            var payload            = MessageBuffer.CreatePayloadVector(builder, messageBytePayload);

            var messageBuff = new Offset <MessageBuffer> [1];

            MessageBuffer.StartMessageBuffer(builder);

            if (messageBytePayload.Length > 0)
            {
                MessageBuffer.AddType(builder, Message.GetMessageType().GetValue());
            }
            if (messageBytePayload.Length > 0)
            {
                MessageBuffer.AddPayloadLen(builder, messageBytePayload.Length);
            }
            if (messageBytePayload.Length > 0)
            {
                MessageBuffer.AddPayload(builder, payload);
            }

            messageBuff[0] = MessageBuffer.EndMessageBuffer(builder);

            var messageVector = TransferTransactionBuffer.CreateMessageVector(builder, messageBuff);

            var mosaics = new Offset <MosaicBuffer> [Mosaics.Count];

            for (var index = 0; index < Mosaics.Count; index++)
            {
                var mosaic           = Mosaics[index];
                var namespaceVector  = MosaicBuffer.CreateNamespaceStringVector(builder, Encoding.UTF8.GetBytes(mosaic.NamespaceName));
                var mosaicNameVector = MosaicBuffer.CreateMosaicIdStringVector(builder, Encoding.UTF8.GetBytes(mosaic.MosaicName));

                MosaicBuffer.StartMosaicBuffer(builder);

                var nsLen = Encoding.Default.GetBytes(mosaic.NamespaceName).Length;
                var msLen = Encoding.Default.GetBytes(mosaic.MosaicName).Length;

                MosaicBuffer.AddMosaicStructureLen(builder, 20 + nsLen + msLen);
                MosaicBuffer.AddMosaicIdStructureLen(builder, nsLen + msLen + 8);
                MosaicBuffer.AddNamespaceIdLen(builder, nsLen);
                MosaicBuffer.AddNamespaceString(builder, namespaceVector);
                MosaicBuffer.AddMosaicIdLen(builder, msLen);
                MosaicBuffer.AddMosaicIdString(builder, mosaicNameVector);
                MosaicBuffer.AddQuantity(builder, mosaic.Amount);

                mosaics[index] = MosaicBuffer.EndMosaicBuffer(builder);
            }

            var mosaicsVector = TransferTransactionBuffer.CreateMosaicsVector(builder, mosaics);

            TransferTransactionBuffer.StartTransferTransactionBuffer(builder);

            TransferTransactionBuffer.AddTransactionType(builder, TransactionType.GetValue());
            TransferTransactionBuffer.AddVersion(builder, BitConverter.ToInt16(new byte[] { ExtractVersion(Version), 0 }, 0));
            TransferTransactionBuffer.AddNetwork(builder, BitConverter.ToInt16(new byte[] { 0, NetworkType.GetNetwork() }, 0));
            TransferTransactionBuffer.AddTimestamp(builder, NetworkTime.EpochTimeInMilliSeconds());
            TransferTransactionBuffer.AddPublicKeyLen(builder, 32);
            TransferTransactionBuffer.AddPublicKey(builder, signer);
            TransferTransactionBuffer.AddFee(builder, Fee);
            TransferTransactionBuffer.AddDeadline(builder, Deadline.Ticks);
            TransferTransactionBuffer.AddRecipientLen(builder, 40);
            TransferTransactionBuffer.AddRecipient(builder, recipientVector);
            TransferTransactionBuffer.AddAmount(builder, 1000000);
            TransferTransactionBuffer.AddMessageFieldLen(builder, Message.GetLength() == 0 ? 0 : Message.GetLength() + 8);
            TransferTransactionBuffer.AddMessage(builder, messageVector);
            TransferTransactionBuffer.AddNoOfMosaics(builder, Mosaics.Count);
            TransferTransactionBuffer.AddMosaics(builder, mosaicsVector);

            var codedTransfer = TransferTransactionBuffer.EndTransferTransactionBuffer(builder);

            builder.Finish(codedTransfer.Value);

            return(new TransferTransactionSchema().Serialize(builder.SizedByteArray()));
        }
        internal override byte[] GenerateBytes()
        {
            var builder = new FlatBufferBuilder(1);

            // create message
            var bytePayload = Message.GetPayload();
            var payload     = MessageBuffer.CreatePayloadVector(builder, bytePayload);

            MessageBuffer.StartMessageBuffer(builder);
            MessageBuffer.AddType(builder, Message.GetMessageType());
            MessageBuffer.AddPayload(builder, payload);
            var messageVector = MessageBuffer.EndMessageBuffer(builder);

            // create mosaics
            var mosaicBuffers = new Offset <MosaicBuffer> [Mosaics.Count];

            for (var index = 0; index < Mosaics.Count; ++index)
            {
                var mosaic = Mosaics[index];
                var id     = MosaicBuffer.CreateIdVector(builder, mosaic.Id.Id.ToUInt8Array());
                var amount = MosaicBuffer.CreateAmountVector(builder, mosaic.Amount.ToUInt8Array());
                MosaicBuffer.StartMosaicBuffer(builder);
                MosaicBuffer.AddId(builder, id);
                MosaicBuffer.AddAmount(builder, amount);
                mosaicBuffers[index] = MosaicBuffer.EndMosaicBuffer(builder);
            }

            // create recipient
            byte[] recipientBytes = Recipient.GetBytes();

            // create vectors
            var signatureVector = TransferTransactionBuffer.CreateSignatureVector(builder, new byte[64]);
            var signerVector    = TransferTransactionBuffer.CreateSignerVector(builder, GetSigner());
            var deadlineVector  = TransferTransactionBuffer.CreateDeadlineVector(builder, Deadline.Ticks.ToUInt8Array());
            var feeVector       = TransferTransactionBuffer.CreateMaxFeeVector(builder, MaxFee?.ToUInt8Array());
            var recipientVector =
                TransferTransactionBuffer.CreateRecipientVector(builder, recipientBytes);
            var mosaicsVector = TransferTransactionBuffer.CreateMosaicsVector(builder, mosaicBuffers);

            // total size of transaction

            /*var totalSize = HEADER_SIZE
             + 25 // recipient
             + 2 // message size is short
             + 1 // message type byte
             + 1 // no of mosaics
             + ((8 + 8) * Mosaics.Count) //each mosaic has id(8bytes) and amount(8bytes)
             + bytePayload.Length; // number of message bytes*/

            var totalSize = GetSerializedSize();

            // create version
            var version = GetTxVersionSerialization();


            // add vectors
            TransferTransactionBuffer.StartTransferTransactionBuffer(builder);
            TransferTransactionBuffer.AddSize(builder, (uint)totalSize);
            TransferTransactionBuffer.AddSignature(builder, signatureVector);
            TransferTransactionBuffer.AddSigner(builder, signerVector);
            TransferTransactionBuffer.AddVersion(builder, (uint)version);
            TransferTransactionBuffer.AddType(builder, EntityType.TRANSFER.GetValue());
            TransferTransactionBuffer.AddMaxFee(builder, feeVector);
            TransferTransactionBuffer.AddDeadline(builder, deadlineVector);

            TransferTransactionBuffer.AddRecipient(builder, recipientVector);
            TransferTransactionBuffer.AddNumMosaics(builder, (byte)Mosaics.Count);
            TransferTransactionBuffer.AddMessageSize(builder, (ushort)(bytePayload.Length + 1));
            TransferTransactionBuffer.AddMessage(builder, messageVector);
            TransferTransactionBuffer.AddMosaics(builder, mosaicsVector);

            // end build
            var codedTransfer = TransferTransactionBuffer.EndTransferTransactionBuffer(builder);

            builder.Finish(codedTransfer.Value);

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

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

            return(output);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Generates the bytes.
        /// </summary>
        /// <returns>The transaction bytes.</returns>
        internal override byte[] GenerateBytes()
        {
            var builder = new FlatBufferBuilder(1);

            // create vectors
            var signatureVector = TransferTransactionBuffer.CreateSignatureVector(builder, new byte[64]);
            var signerVector    = TransferTransactionBuffer.CreateSignerVector(builder, GetSigner());
            var feeVector       = TransferTransactionBuffer.CreateFeeVector(builder, Fee.ToUInt8Array());
            var deadlineVector  = TransferTransactionBuffer.CreateDeadlineVector(builder, Deadline.Ticks.ToUInt8Array());
            var recipientVector = TransferTransactionBuffer.CreateRecipientVector(builder, Address.Plain.FromBase32String());

            ushort version = ushort.Parse(NetworkType.GetNetworkByte().ToString("X") + "0" + Version.ToString("X"), System.Globalization.NumberStyles.HexNumber);

            if (Message == null)
            {
                Message = EmptyMessage.Create();
            }

            // create message vector
            var bytePayload = Message.GetPayload();
            var payload     = MessageBuffer.CreatePayloadVector(builder, bytePayload);

            MessageBuffer.StartMessageBuffer(builder);
            if (bytePayload != null)
            {
                MessageBuffer.AddType(builder, Message.GetMessageType());
            }
            MessageBuffer.AddPayload(builder, payload);
            var message = MessageBuffer.EndMessageBuffer(builder);

            // create mosaics vector
            var mosaics = new Offset <MosaicBuffer> [Mosaics.Count];

            for (var index = 0; index < Mosaics.Count; index++)
            {
                var mosaic       = Mosaics[index];
                var idPayload    = MosaicBuffer.CreateIdVector(builder, mosaic.MosaicId.Id.ToUInt8Array());
                var amountVector = MosaicBuffer.CreateAmountVector(builder, mosaic.Amount.ToUInt8Array());
                MosaicBuffer.StartMosaicBuffer(builder);
                MosaicBuffer.AddId(builder, idPayload);
                MosaicBuffer.AddAmount(builder, amountVector);

                mosaics[index] = MosaicBuffer.EndMosaicBuffer(builder);
            }

            var mosaicsVector = TransferTransactionBuffer.CreateMosaicsVector(builder, mosaics);

            // add vectors
            TransferTransactionBuffer.StartTransferTransactionBuffer(builder);
            TransferTransactionBuffer.AddSize(builder, (uint)(/*fixed size*/ 148 + 16 * Mosaics.Count + Message.GetLength()));
            TransferTransactionBuffer.AddSignature(builder, signatureVector);
            TransferTransactionBuffer.AddSigner(builder, signerVector);
            TransferTransactionBuffer.AddVersion(builder, version);
            TransferTransactionBuffer.AddType(builder, TransactionTypes.Types.Transfer.GetValue());
            TransferTransactionBuffer.AddFee(builder, feeVector);
            TransferTransactionBuffer.AddDeadline(builder, deadlineVector);
            TransferTransactionBuffer.AddRecipient(builder, recipientVector);
            TransferTransactionBuffer.AddNumMosaics(builder, (byte)Mosaics.Count);
            TransferTransactionBuffer.AddMessageSize(builder, (byte)Message.GetLength());
            TransferTransactionBuffer.AddMessage(builder, message);
            TransferTransactionBuffer.AddMosaics(builder, mosaicsVector);

            // end build
            var codedTransfer = TransferTransactionBuffer.EndTransferTransactionBuffer(builder);

            builder.Finish(codedTransfer.Value);

            return(new TransferTransactionSchema().Serialize(builder.SizedByteArray()));
        }