public void EncodeTransactionProto() { TxParams txParams = new TxParams(); txParams.Version = "0"; txParams.Nonce = "0"; txParams.ToAddr = "2E3C9B415B19AE4035503A06192A0FAD76E04243"; txParams.SenderPubKey = "0246e7178dc8253201101e18fd6f6eb9972451d121fc57aa2a06dd5c111e58dc6a"; txParams.Amount = "10000"; txParams.GasPrice = "100"; txParams.GasLimit = "1000"; txParams.Code = ""; txParams.Data = ""; TransactionUtil util = new TransactionUtil(); byte[] bytes = util.EncodeTransactionProto(txParams); Console.WriteLine(ByteUtil.ByteArrayToHexString(bytes)); }
public Transaction.Transaction Sign(Transaction.Transaction transaction) { if (transaction.ToAddr.ToUpper().StartsWith("0X")) { transaction.ToAddr = transaction.ToAddr.Substring(2); } if (!Validation.IsBech32(transaction.ToAddr) && !Validation.IsValidChecksumAddress("0x" + transaction.ToAddr)) { throw new Exception("not checksum address or bech32"); } if (Validation.IsBech32(transaction.ToAddr)) { transaction.ToAddr = Bech32.FromBech32Address(transaction.ToAddr); } if (Validation.IsValidChecksumAddress("0x" + transaction.ToAddr)) { transaction.ToAddr = "0x" + transaction.ToAddr; } TxParams txParams = transaction.ToTransactionParam(); if (txParams != null && !string.IsNullOrEmpty(txParams.SenderPubKey)) { string address = KeyTools.GetAddressFromPublicKey(txParams.SenderPubKey).ToUpper(); Account account = accounts[address]; if (account == null) { throw new Exception("Could not sign the transaction with" + address + " as it does not exist"); } return(SignWith(transaction, account)); } if (defaultAccount == null) { throw new Exception("This wallet has no default account."); } return(this.SignWith(transaction, this.defaultAccount)); }
public byte[] EncodeTransactionProto(TxParams txParams) { BigInteger amount = BigInteger.ValueOf(long.Parse(txParams.Amount)); BigInteger gasPrice = BigInteger.ValueOf(long.Parse(txParams.GasPrice)); ProtoTransactionCoreInfo info = new ProtoTransactionCoreInfo(); info.Version = uint.Parse(txParams.Version); info.Nonce = (string.IsNullOrEmpty(txParams.Nonce) ? 0L : ulong.Parse(txParams.Nonce)); info.Toaddr = ByteString.CopyFrom(ByteUtil.HexStringToByteArray(txParams.ToAddr.ToLower())); info.Senderpubkey = new ByteArray() { Data = ByteString.CopyFrom(ByteUtil.HexStringToByteArray(txParams.SenderPubKey)) }; info.Amount = new ByteArray() { Data = ByteString.CopyFrom(BigIntegers.AsUnsignedByteArray(16, amount)) }; info.Gasprice = new ByteArray() { Data = ByteString.CopyFrom(BigIntegers.AsUnsignedByteArray(16, gasPrice)) }; info.Gaslimit = ulong.Parse(txParams.GasLimit); if (!string.IsNullOrEmpty(txParams.Code)) { info.Code = ByteString.CopyFrom(System.Text.Encoding.Default.GetBytes(txParams.Code)); } if (!string.IsNullOrEmpty(txParams.Data)) { info.Data = ByteString.CopyFrom(System.Text.Encoding.Default.GetBytes(txParams.Data)); } return(info.ToByteArray()); }
/// <summary> /// 轉帳(付款) /// </summary> /// <param name="fromId"></param> /// <param name="toId"></param> /// <param name="currency"></param> /// <param name="amount"></param> /// <returns>receiptId</returns> public async Task <bool> doTransfer(string fromId, string toId, string receiptId, string currency, decimal amount, string message) { var ok = false; //get user var fromUser = getUserByUserId(fromId); var toUser = getUserByUserId(toId); //get user's balance var from_balance = getBalance(fromId).balances[currency]; var to_balance = getBalance(toId).balances[currency]; //simulate transcation if (!fromId.Equals(toId)) { if (amount > 0 && (from_balance >= amount)) { from_balance = from_balance - (fromUser.phoneno == "BANK" ? 0 : amount); to_balance = to_balance + amount; updateBalance(fromId, new Dictionary <string, decimal> { { currency, from_balance } }); updateBalance(toId, new Dictionary <string, decimal> { { currency, to_balance } }); ok = true; } } //generate sender receipt var param = new TxParams { sender = fromUser.userId, receiver = toUser.userId, currency = currency, amount = amount }; var sender_rec = new TxReceipt { receiptId = receiptId, executorId = fromUser.userId, ownerId = fromUser.userId, currency = currency, message = message, isParent = true, txType = TxType.TRANSFER, statusCode = ok ? 0 : -1, statusMsg = "", txParams = param, txResult = !ok ? null : new TxActResult { outflow = true, amount = amount, balance = from_balance } }; upsertReceipt(sender_rec); //simulate receipt notification Task.Run(async() => { Task.Delay(10).Wait(); //notify sender // if (fromUser.ntfInfo != null) // notifications.sendMessage(fromId, fromUser.ntfInfo.pns, $"transfer out({(ok ? "okay" : "failure")})", D.NTFTYPE.TXRECEIPT, new { list = new List<dynamic>() { sender_rec.ToApiResult() } }); //notify receiver if (ok && toUser.ntfInfo != null) { //generate receiver receipt var receiver_rec = sender_rec.Derivative(sender_rec.currency, toUser.userId, new TxActResult { outflow = false, amount = amount, balance = to_balance }); upsertReceipt(receiver_rec); await notifications.sendMessage(toId, toUser.ntfInfo.pns, "transfer in", D.NTFTYPE.TXRECEIPT, new { list = new List <dynamic>() { receiver_rec.ToApiResult() } }); } }).Start(); return(ok); }