Пример #1
0
        public async Task <SubmitTransactionResponse> TransferCoins(Account sender, string recipientAddress, ulong amount, ulong gasUnitPrice, ulong maxGasAmount)
        {
            var program = new Program();

            program.Code = Convert.FromBase64String(Constant.ProgamBase64Codes.PeerToPeerTxn).ToByteString();
            program.Arguments.Add(new TransactionArgument {
                Type = TransactionArgument.Types.ArgType.Address, Data = recipientAddress.ToByteString()
            });
            program.Arguments.Add(new TransactionArgument {
                Type = TransactionArgument.Types.ArgType.U64, Data = amount.ToBytes().Reverse().ToByteString()
            });

            var transaction = new RawTransaction();

            transaction.ExpirationTime = (ulong)Math.Floor((decimal)DateTimeOffset.Now.ToUnixTimeMilliseconds() / 1000) + 100;
            transaction.GasUnitPrice   = gasUnitPrice;
            transaction.MaxGasAmount   = maxGasAmount;
            transaction.SequenceNumber = (await QueryBalance(sender.Address)).ElementAt(0).SequenceNumber;
            transaction.Program        = program;
            transaction.SenderAccount  = sender.Address.ToByteString();

            var request = new SubmitTransactionRequest();

            request.SignedTxn = new SignedTransaction
            {
                RawTxnBytes     = transaction.SenderAccount,
                SenderPublicKey = sender.PublicKey,
                SenderSignature = sender.Signature
            };

            return(await acClient.SubmitTransactionAsync(request));
        }
Пример #2
0
        // public async Task<bool> TransferCoins(Account sender, string receiverAddress, ulong amount, ulong gasUnitPrice = 0, ulong maxGasAmount = 1000000)
        public async Task <bool> TransferCoins(Account sender, string receiverAddress, ulong amount, ulong gasUnitPrice = 0, ulong maxGasAmount = 1000000)
        {
            try
            {
                var program = new Program();
                program.Code = Convert.FromBase64String(Constant.ProgamBase64Codes.PeerToPeerTxn).ToByteString();
                program.Arguments.Add(new TransactionArgument {
                    Type = TransactionArgument.Types.ArgType.Address, Data = receiverAddress.ToByteString()
                });
                program.Arguments.Add(new TransactionArgument {
                    Type = TransactionArgument.Types.ArgType.U64, Data = amount.ToBytes().Reverse().ToByteString()
                });

                var transaction = new RawTransaction();
                transaction.ExpirationTime = (ulong)Math.Floor((decimal)DateTimeOffset.Now.ToUnixTimeMilliseconds() / 1000) + 100;
                transaction.GasUnitPrice   = gasUnitPrice;
                transaction.MaxGasAmount   = maxGasAmount;
                var accountState = await QueryBalance(sender.Address);

                transaction.SequenceNumber = accountState.SequenceNumber;
                transaction.Program        = program;
                transaction.SenderAccount  = sender.Address.ToByteString();

                var hash            = new SHA3_256().ComputeVariable(Constant.HashSaltValues.RawTransactionHashSalt.FromHexToBytes().Concat(transaction.ToByteArray()).ToArray());
                var senderSignature = sender.KeyPair.Sign(hash);

                var request = new SubmitTransactionRequest
                {
                    SignedTxn = new SignedTransaction
                    {
                        RawTxnBytes     = transaction.ToByteString(),
                        SenderPublicKey = sender.PublicKey,
                        SenderSignature = senderSignature.ToByteString()
                    }
                };

                var response = await acClient.SubmitTransactionAsync(request);

                return(response.AcStatus.Code == AdmissionControlStatusCode.Accepted);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #3
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;
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            Channel channel = new Channel("ac.testnet.libra.org:8000", ChannelCredentials.Insecure);
            var     client  = new AdmissionControl.AdmissionControl.AdmissionControlClient(channel);

            HexEncoder hex = new HexEncoder();

            SharedSecret sharedSecret = SharedSecret.Import(Encoding.UTF8.GetBytes("newdummy"));
            HkdfSha512   kdf          = new HkdfSha512();
            var          key          = kdf.DeriveKey(sharedSecret, null, null, Ed25519.Ed25519);
            var          sender       = key.PublicKey.Export(KeyBlobFormat.RawPublicKey);

            UInt64 seqNum    = 11;
            string senderHex = hex.EncodeData(Sha3.Sha3256().ComputeHash(sender));

            uint   amount  = 10000000;
            string reciver = "4ba2555fd146e79e37fda7a2f30dc1b4f3d9228aa48b230dbab0a18d407f2f9b";

            RawTransactionLCS rawTr = new RawTransactionLCS()
            {
                ExpirationTime = (ulong)DateTimeOffset.UtcNow.AddSeconds(60)
                                 .ToUnixTimeSeconds(),
                GasUnitPrice   = 0,
                MaxGasAmount   = 100000,
                SequenceNumber = seqNum
            };

            rawTr.TransactionPayload = new TransactionPayloadLCS();

            rawTr.TransactionPayload.PayloadType = (uint)TransactionPayloadLCSEnum.Script;
            rawTr.TransactionPayload.Script      = new ScriptLCS()
            {
                Code = Utilities.PtPTrxBytecode,
                TransactionArguments = new List <TransactionArgumentLCS>()
                {
                    new TransactionArgumentLCS()
                    {
                        ArgType = (uint)TransactionArgumentLCSEnum.Address,
                        Address = new AddressLCS(reciver)
                    },
                    new TransactionArgumentLCS()
                    {
                        ArgType = (uint)TransactionArgumentLCSEnum.U64,
                        U64     = amount
                    }
                }
            };

            rawTr.Sender = new AddressLCS(senderHex);

            var bytesTrx = LCSCore.LCSSerialization(rawTr);

            Types.SignedTransaction signedTx = new Types.SignedTransaction();
            var bytesTrxHash = Google.Protobuf.ByteString.CopyFrom(bytesTrx);

            var         seed      = Encoding.ASCII.GetBytes(RAWTX_HASH_SALT + LIBRA_HASH_SUFFIX);
            var         seedHash  = Sha3.Sha3256().ComputeHash(seed);
            List <byte> hashInput = new List <byte>();

            hashInput.AddRange(seedHash);
            hashInput.AddRange(bytesTrxHash);
            var hash = Sha3.Sha3256().ComputeHash(hashInput.ToArray());

            SubmitTransactionRequest req = new SubmitTransactionRequest();

            req.SignedTxn = new SignedTransaction();

            List <byte> retArr = new List <byte>();

            retArr = retArr.Concat(bytesTrx).ToList();

            retArr = retArr.Concat(
                LCSCore.LCSSerialization(key.Export(KeyBlobFormat.RawPublicKey))).ToList();
            var sig = SignatureAlgorithm.Ed25519.Sign(key, hash);

            retArr = retArr.Concat(LCSCore.LCSSerialization(sig)).ToList();
            req.SignedTxn.SignedTxn = ByteString.CopyFrom(retArr.ToArray());


            var result = client.SubmitTransaction(
                req, new Metadata());

            Task.Delay(5000).Wait();
            GetTransaction(client, senderHex, seqNum);
        }