<commit_before>﻿using stellar_dotnetcore_sdk.xdr;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace stellar_dotnetcore_sdk
{
    public class Transaction
    {
        private readonly int BASE_FEE = 100;

        private readonly int _Fee;
        private readonly KeyPair _SourceAccount;
        private readonly long _SequenceNumber;
        private readonly Operation[] _Operations;
        private readonly Memo _Memo;
        private readonly TimeBounds _TimeBounds;
        private List<DecoratedSignature> _Signatures;

        public int Fee { get { return _Fee; } }
        public KeyPair SourceAccount { get { return _SourceAccount; } }
        public long SequenceNumber { get { return _SequenceNumber; } }
        public Operation[] Operations { get { return _Operations; } }
        public Memo Memo { get { return _Memo; } }
        public TimeBounds TimeBounds { get { return _TimeBounds; } }
        public List<DecoratedSignature> Signatures { get { return _Signatures; } }

        Transaction(KeyPair sourceAccount, long sequenceNumber, Operation[] operations, Memo memo, TimeBounds timeBounds)
        {
            _SourceAccount = sourceAccount ?? throw new ArgumentNullException(nameof(sourceAccount), "sourceAccount cannot be null");
            _SequenceNumber = sequenceNumber;
            _Operations = operations ?? throw new ArgumentNullException(nameof(operations), "operations cannot be null");
            if (operations.Length == 0)
                throw new ArgumentException(nameof(operations), "At least one operation required");

            _Fee = operations.Length * BASE_FEE;
            _Signatures = new List<xdr.DecoratedSignature>();
            _Memo = memo != null ? memo : Memo.None();
            _TimeBounds = timeBounds;
        }

        /**
         * Adds a new signature ed25519PublicKey to this transaction.
         * @param signer {@link KeyPair} object representing a signer
         */
        public void Sign(KeyPair signer)
        {
            if (signer == null)
                throw new ArgumentNullException(nameof(signer), "signer cannot be null");

            byte[] txHash = Hash();
            _Signatures.Add(signer.SignDecorated(txHash));
        }

        /**
         * Adds a new sha256Hash signature to this transaction by revealing preimage.
         * @param preimage the sha256 hash of preimage should be equal to signer hash
         */
        public void Sign(byte[] preimage)
        {
            Signature signature = new Signature();
            signature.InnerValue = preimage ?? throw new ArgumentNullException(nameof(preimage), "preimage cannot be null");

            byte[] hash = Util.Hash(preimage);

            var length = hash.Length;
            var signatureHintBytes = hash.Skip(length - 4).Take(4).ToArray();

            SignatureHint signatureHint = new SignatureHint();
            signatureHint.InnerValue = signatureHintBytes;

            DecoratedSignature decoratedSignature = new DecoratedSignature();
            decoratedSignature.Hint = signatureHint;
            decoratedSignature.Signature = signature;

            _Signatures.Add(decoratedSignature);
        }

        /**
         * Returns transaction hash.
         */
        public byte[] Hash()
        {
            return Util.Hash(SignatureBase());
        }

        /**
         * Returns signature base.
         */
        public byte[] SignatureBase()
        {
            if (Network.Current == null)
            {
                throw new NoNetworkSelectedException();
            }

            var memoryStream = new MemoryStream();
            var writer = new XdrOutputStream(memoryStream);

            // Hashed NetworkID
            writer.Write(Network.Current.NetworkId);

            // Envelope Type - 4 bytes
            //
            var envelopeMemoryStream = new MemoryStream();
            var envelopeDataOutputStream = new XdrDataOutputStream(envelopeMemoryStream);

            EnvelopeType.Encode(envelopeDataOutputStream, EnvelopeType.Create(EnvelopeType.EnvelopeTypeEnum.ENVELOPE_TYPE_TX));
            writer.Write(envelopeMemoryStream.ToArray());

            // Transaction XDR bytes
            var txMemoryStream = new MemoryStream();
            var txWriter = new XdrDataOutputStream(txMemoryStream);
            xdr.Transaction.Encode(txWriter, ToXdr());
            writer.Write(txMemoryStream.ToArray());

            return writer.ToArray();

        }

        /**
         * Generates Transaction XDR object.
         */
        public xdr.Transaction ToXdr()
        {
            // fee
            Uint32 fee = new Uint32();
            fee.InnerValue = _Fee;

            // sequenceNumber
            Uint64 sequenceNumberUint = new Uint64();
            sequenceNumberUint.InnerValue = _SequenceNumber;
            SequenceNumber sequenceNumber = new SequenceNumber();
            sequenceNumber.InnerValue = sequenceNumberUint;

            // sourceAccount
            AccountID sourceAccount = new AccountID();
            sourceAccount.InnerValue = _SourceAccount.XdrPublicKey;
<commit_msg>Maybe fixes SignatureBase?<commit_after>﻿using stellar_dotnetcore_sdk.xdr;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace stellar_dotnetcore_sdk
{
    public class Transaction
    {
        private readonly int BASE_FEE = 100;

        private readonly int _Fee;
        private readonly KeyPair _SourceAccount;
        private readonly long _SequenceNumber;
        private readonly Operation[] _Operations;
        private readonly Memo _Memo;
        private readonly TimeBounds _TimeBounds;
        private List<DecoratedSignature> _Signatures;

        public int Fee { get { return _Fee; } }
        public KeyPair SourceAccount { get { return _SourceAccount; } }
        public long SequenceNumber { get { return _SequenceNumber; } }
        public Operation[] Operations { get { return _Operations; } }
        public Memo Memo { get { return _Memo; } }
        public TimeBounds TimeBounds { get { return _TimeBounds; } }
        public List<DecoratedSignature> Signatures { get { return _Signatures; } }

        Transaction(KeyPair sourceAccount, long sequenceNumber, Operation[] operations, Memo memo, TimeBounds timeBounds)
        {
            _SourceAccount = sourceAccount ?? throw new ArgumentNullException(nameof(sourceAccount), "sourceAccount cannot be null");
            _SequenceNumber = sequenceNumber;
            _Operations = operations ?? throw new ArgumentNullException(nameof(operations), "operations cannot be null");
            if (operations.Length == 0)
                throw new ArgumentException(nameof(operations), "At least one operation required");

            _Fee = operations.Length * BASE_FEE;
            _Signatures = new List<xdr.DecoratedSignature>();
            _Memo = memo != null ? memo : Memo.None();
            _TimeBounds = timeBounds;
        }

        /**
         * Adds a new signature ed25519PublicKey to this transaction.
         * @param signer {@link KeyPair} object representing a signer
         */
        public void Sign(KeyPair signer)
        {
            if (signer == null)
                throw new ArgumentNullException(nameof(signer), "signer cannot be null");

            byte[] txHash = Hash();
            _Signatures.Add(signer.SignDecorated(txHash));
        }

        /**
         * Adds a new sha256Hash signature to this transaction by revealing preimage.
         * @param preimage the sha256 hash of preimage should be equal to signer hash
         */
        public void Sign(byte[] preimage)
        {
            Signature signature = new Signature();
            signature.InnerValue = preimage ?? throw new ArgumentNullException(nameof(preimage), "preimage cannot be null");

            byte[] hash = Util.Hash(preimage);

            var length = hash.Length;
            var signatureHintBytes = hash.Skip(length - 4).Take(4).ToArray();

            SignatureHint signatureHint = new SignatureHint();
            signatureHint.InnerValue = signatureHintBytes;

            DecoratedSignature decoratedSignature = new DecoratedSignature();
            decoratedSignature.Hint = signatureHint;
            decoratedSignature.Signature = signature;

            _Signatures.Add(decoratedSignature);
        }

        /**
         * Returns transaction hash.
         */
        public byte[] Hash()
        {
            return Util.Hash(SignatureBase());
        }

        /**
         * Returns signature base.
         */
        public byte[] SignatureBase()
        {
            if (Network.Current == null)
            {
                throw new NoNetworkSelectedException();
            }

            var memoryStream = new MemoryStream();
            var writer = new BinaryWriter(memoryStream);

            // Hashed NetworkID
            writer.Write(Network.Current.NetworkId);

            // Envelope Type - 4 bytes
            writer.Write((int)EnvelopeType.Create(EnvelopeType.EnvelopeTypeEnum.ENVELOPE_TYPE_TX).InnerValue);

            // Transaction XDR bytes
            var txMemoryStream = new MemoryStream();
            var txWriter = new XdrDataOutputStream(txMemoryStream);
            xdr.Transaction.Encode(txWriter, ToXdr());
            writer.Write(txMemoryStream.ToArray());

            return memoryStream.ToArray();

        }

        /**
         * Generates Transaction XDR object.
         */
        public xdr.Transaction ToXdr()
        {
            // fee
            Uint32 fee = new Uint32();
            fee.InnerValue = _Fee;

            // sequenceNumber
            Uint64 sequenceNumberUint = new Uint64();
            sequenceNumberUint.InnerValue = _SequenceNumber;
            SequenceNumber sequenceNumber = new SequenceNumber();
            sequenceNumber.InnerValue = sequenceNumberUint;

            // sourceAccount
            AccountID sourceAccount = new AccountID();
            sourceAccount.InnerValue = _SourceAccount.XdrPublicKey;

{'fraction_non_alphanumeric': 0.08069558421258304, 'fraction_numerical': 0.005080109417741305, 'mean_word_length': 2.155672009864365, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4923216', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2428, 'n_words': 686}