Beispiel #1
0
        /**
         * Convert transaction to Protobuf format.
         * </br><b>Note:</b> Transaction should be [sign]{@link Transaction#signTransaction} before converting.
         *
         * @return {Buffer} Transaction data in Protobuf format
         *
         * @example
         * var acc = Account.NewAccount();
         *
         * var tx = new Transaction({
         *    chainID: 1,
         *    from: acc,
         *    to: "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17",
         *    value: 10,
         *    nonce: 12,
         *    gasPrice: 1000000,
         *    gasLimit: 2000000
         * });
         * tx.signTransaction();
         * var txHash = tx.toProto();
         * // Uint8Array(127)
         */
        public byte[] ToProto()
        {
            if (Sign == null)
            {
                throw new Exception(_signErrorMessage);
            }
            var data = new Corepb.Data
            {
                Payload = ByteString.CopyFrom(_payLoadData),
                Type    = Enum.GetName(typeof(TxPayload), _payLoadType).ToLower()
            };

            var txData = new Corepb.Transaction
            {
                Hash      = ByteString.CopyFrom(Hash),
                From      = ByteString.CopyFrom(_fromAccount.GetAddress()),
                To        = ByteString.CopyFrom(_toAccount.GetAddress()),
                Value     = ByteString.CopyFrom(CryptoUtils.PadToBigEndian(_value.ToString("x"), 128)),
                Nonce     = _nonce,
                Timestamp = _timestamp,
                Data      = data,
                ChainId   = _chainId,
                GasPrice  = ByteString.CopyFrom(CryptoUtils.PadToBigEndian(_gasPrice.ToString("x"), 128)),
                GasLimit  = ByteString.CopyFrom(CryptoUtils.PadToBigEndian(_gasLimit.ToString("x"), 128)),
                Alg       = Alg,
                Sign      = ByteString.CopyFrom(Sign)
            };

            var txBuffer = txData.ToByteArray();

            return(txBuffer);
        }
Beispiel #2
0
        public NebTransaction(uint chainId, NebAccount fromAccount, NebAccount toAccount, string value, ulong nonce,
                              string gasPrice, string gasLimit, NebContract contract = null)
        {
            m_chainId     = chainId;
            m_fromAccount = fromAccount;
            m_toAccount   = toAccount;
            m_value       = BigInteger.Parse(value);
            m_nonce       = nonce;
#if DEBUG
            m_timestamp = 0;
#else
            m_timestamp = GetTimeStamp(DateTime.Now);
#endif
            m_gasPrice = BigInteger.Parse(gasPrice);
            m_gasLimit = BigInteger.Parse(gasLimit);
            m_contract = contract ?? NebContract.DefaultContract;

            if (m_gasPrice <= 0)
            {
                m_gasPrice = new BigInteger(1000000);
            }

            if (m_gasLimit <= 0)
            {
                m_gasLimit = new BigInteger(20000);
            }

            m_data = new Corepb.Data();

            switch (m_contract.Content)
            {
            case NebContract.BinaryContent _:
                m_data.Type    = "binary";
                m_data.Payload = ByteString.CopyFrom(((NebContract.BinaryContent)m_contract.Content).Data);
                break;

            case NebContract.DeployContent _:
                m_data.Type    = "deploy";
                m_data.Payload =
                    ByteString.CopyFromUtf8(JsonConvert.SerializeObject(m_contract.Content, JsonSettings));
                break;

            case NebContract.CallContent _:
                m_data.Type    = "call";
                m_data.Payload =
                    ByteString.CopyFromUtf8(JsonConvert.SerializeObject(m_contract.Content, JsonSettings));
                break;

            default:
                throw new Exception();
            }
        }
Beispiel #3
0
        /**
         * Convert transaction to hash by SHA3-256 algorithm.
         *
         * @return {Hash} hash of Transaction.
         *
         * @example
         * var acc = Account.NewAccount();
         *
         * var tx = new Transaction({
         *    chainID: 1,
         *    from: acc,
         *    to: "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17",
         *    value: 10,
         *    nonce: 12,
         *    gasPrice: 1000000,
         *    gasLimit: 2000000
         * });
         * var txHash = tx.hashTransaction();
         * //Uint8Array(32) [211, 213, 102, 103, 23, 231, 246, 141, 20, 202, 210, 25, 92, 142, 162, 242, 232, 95, 44, 239, 45, 57, 241, 61, 34, 2, 213, 160, 17, 207, 75, 40]
         */
        public byte[] HashTransaction()
        {
            var data = new Corepb.Data
            {
                Payload = ByteString.CopyFrom(_payLoadData),
                Type    = Enum.GetName(typeof(TxPayload), _payLoadType).ToLower()
            };

            var dataBuffer = data.ToByteArray();

            var hash = Sha3Util.Get256Hash(ByteUtil.Merge(
                                               _fromAccount.GetAddress(),
                                               _toAccount.GetAddress(),
                                               CryptoUtils.PadToBigEndian(_value.ToString("x"), 128),
                                               CryptoUtils.PadToBigEndian(_nonce.ToString("x"), 64),
                                               CryptoUtils.PadToBigEndian(_timestamp.ToString("x"), 64),
                                               dataBuffer,
                                               CryptoUtils.PadToBigEndian(_chainId.ToString("x"), 32),
                                               CryptoUtils.PadToBigEndian(_gasPrice.ToString("x"), 128),
                                               CryptoUtils.PadToBigEndian(_gasLimit.ToString("x"), 128))
                                           );

            return(hash);
        }