示例#1
0
        public void Program()
        {
            var program = new ProgramLCS();

            program.Code = "move".ToBytes();
            program.TransactionArguments = new List <TransactionArgumentLCS>();
            program.TransactionArguments.Add(new TransactionArgumentLCS
            {
                ArgType = Types.TransactionArgument.Types.ArgType.String,
                String  = "CAFE D00D"
            });
            program.TransactionArguments.Add(new TransactionArgumentLCS
            {
                ArgType = Types.TransactionArgument.Types.ArgType.String,
                String  = "cafe d00d"
            });
            program.Modules = new List <byte[]>();
            program.Modules.Add("CA".FromHexToBytes());
            program.Modules.Add("FED0".FromHexToBytes());
            program.Modules.Add("0D".FromHexToBytes());

            var actual   = LCSCore.LCSDeserialization(program).ByteArrayToString();
            var expected = "040000006D6F766502000000020000000900000043414645204430304402000000090000006361666520643030640300000001000000CA02000000FED0010000000D".ToLower();

            Assert.Equal(expected, actual);
        }
示例#2
0
        public byte[] ProgramToByte(ProgramLCS source)
        {
            List<byte> retArr = new List<byte>();
            var code = ByteArrToByte(source.Code);
            retArr = retArr.Concat(code).ToList();

            var transactionArg = ListTransactionArgumentToByte(source.TransactionArguments.ToList());
            retArr = retArr.Concat(transactionArg).ToList();

            var modules = ListByteArrToByte(source.Modules);
            retArr = retArr.Concat(modules).ToList();
            return retArr.ToArray();
        }
示例#3
0
        public byte[] ProgramToByte(ProgramLCS source)
        {
            List <byte> retArr = new List <byte>();
            var         code   = LCSCore.LCSerialize(source.Code);

            retArr = retArr.Concat(code).ToList();
            var transactionArg = LCSCore.LCSerialize(source.TransactionArguments);

            retArr = retArr.Concat(transactionArg).ToList();
            var modules = LCSCore.LCSerialize(source.Modules);

            retArr = retArr.Concat(modules).ToList();
            return(retArr.ToArray());
        }
示例#4
0
        public ProgramLCS GetProgram(byte[] source, ref int cursor)
        {
            var retVal = new ProgramLCS();

            //struct Program
            // {
            //  code: Vec<u8>, // Variable length byte array
            //  args: Vec<TransactionArgument>, // Variable length array of TransactionArguments
            //  modules: Vec<Vec<u8>>, // Variable length array of variable length byte arrays
            // }
            retVal.Code = source.LCSerialization <byte[]>(ref cursor);
            retVal.TransactionArguments =
                source.LCSerialization <List <TransactionArgumentLCS> >(ref cursor);
            retVal.Modules = source.LCSerialization <List <byte[]> >(ref cursor);

            return(retVal);
        }
示例#5
0
        public void RawTransactionWithProgram()
        {
            var program = new ProgramLCS();

            program.Code = "move".ToBytes();
            program.TransactionArguments = new List <TransactionArgumentLCS>();
            program.TransactionArguments.Add(new TransactionArgumentLCS
            {
                ArgType = Types.TransactionArgument.Types.ArgType.String,
                String  = "CAFE D00D"
            });
            program.TransactionArguments.Add(new TransactionArgumentLCS
            {
                ArgType = Types.TransactionArgument.Types.ArgType.String,
                String  = "cafe d00d"
            });
            program.Modules = new List <byte[]>();
            program.Modules.Add("CA".FromHexToBytes());
            program.Modules.Add("FED0".FromHexToBytes());
            program.Modules.Add("0D".FromHexToBytes());

            var transaction = new RawTransactionLCS
            {
                Sender = new AddressLCS
                {
                    Value = "3a24a61e05d129cace9e0efc8bc9e33831fec9a9be66f50fd352a2638a49b9ee"
                },
                SequenceNumber     = 32,
                TransactionPayload = new TransactionPayloadLCS
                {
                    PayloadType = TransactionPayloadType.Program,
                    Program     = program
                },
                MaxGasAmount   = 10000UL,
                GasUnitPrice   = 20000UL,
                ExpirationTime = 86400UL
            };
            var actual   = LCSCore.LCSDeserialization(transaction).ByteArrayToString();
            var expected = "3A24A61E05D129CACE9E0EFC8BC9E33831FEC9A9BE66F50FD352A2638A49B9EE200000000000000000000000040000006D6F766502000000020000000900000043414645204430304402000000090000006361666520643030640300000001000000CA02000000FED0010000000D1027000000000000204E0000000000008051010000000000".ToLower();

            Assert.Equal(expected, actual);
        }
示例#6
0
        public async Task <bool> TransferCoins(Account sender, string receiverAddress, ulong amount, ulong gasUnitPrice = 0, ulong maxGasAmount = 1000000)
        {
            try
            {
                var accountState = await QueryBalance(sender.Address);

                var program = new ProgramLCS();
                program.Code = Convert.FromBase64String(Constant.ProgamBase64Codes.PeerToPeerTxn);
                program.TransactionArguments = new List <TransactionArgumentLCS>();
                program.TransactionArguments.Add(new TransactionArgumentLCS
                {
                    ArgType = Types.TransactionArgument.Types.ArgType.Address,
                    Address = new AddressLCS {
                        Value = receiverAddress
                    }
                });
                program.TransactionArguments.Add(new TransactionArgumentLCS
                {
                    ArgType = Types.TransactionArgument.Types.ArgType.U64,
                    U64     = amount
                });
                program.Modules = new List <byte[]>();

                var transaction = new RawTransactionLCS
                {
                    Sender = new AddressLCS {
                        Value = sender.Address
                    },
                    SequenceNumber     = accountState.SequenceNumber,
                    TransactionPayload = new TransactionPayloadLCS
                    {
                        PayloadType = TransactionPayloadType.Program,
                        Program     = program
                    },
                    MaxGasAmount   = maxGasAmount,
                    GasUnitPrice   = gasUnitPrice,
                    ExpirationTime = (ulong)Math.Floor((decimal)DateTimeOffset.Now.ToUnixTimeMilliseconds() / 1000) + 100
                };
                var transactionLCS = LCSCore.LCSDeserialization(transaction);

                var digestSHA3 = new SHA3_256();
                var saltDigest = digestSHA3.ComputeVariable(Constant.HashSaltValues.RawTransactionHashSalt.ToBytes());
                var saltDigestAndTransaction = saltDigest.Concat(transactionLCS).ToArray();
                var hash            = digestSHA3.ComputeVariable(saltDigestAndTransaction);
                var senderSignature = sender.KeyPair.Sign(hash);

                var publicKeyLen = BitConverter.GetBytes((uint)sender.PublicKey.Length);
                var signatureLen = BitConverter.GetBytes((uint)senderSignature.Length);
                var txnBytes     = transactionLCS.Concat(publicKeyLen).ToArray();
                txnBytes = txnBytes.Concat(sender.PublicKey).ToArray();
                txnBytes = txnBytes.Concat(signatureLen).ToArray();
                txnBytes = txnBytes.Concat(senderSignature).ToArray();

                var request = new SubmitTransactionRequest
                {
                    SignedTxn = new SignedTransaction
                    {
                        TxnBytes = txnBytes.ToByteString()
                    }
                };

                var response = await acClient.SubmitTransactionAsync(request);

                return(response.AcStatus.Code == AdmissionControlStatusCode.Accepted);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }