Last active
February 10, 2025 09:19
-
-
Save donma/d5d900b3def119160ee5f3192480c2eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static string ConvertHexToBase58Check(string hexAddress) | |
| { | |
| byte[] addressBytes = HexToBytes(hexAddress); | |
| // 計算雙重 SHA-256 校驗碼 | |
| using (SHA256 sha256 = SHA256.Create()) | |
| { | |
| byte[] hash1 = sha256.ComputeHash(addressBytes); | |
| byte[] hash2 = sha256.ComputeHash(hash1); | |
| // 取前 4 位作為校驗碼 | |
| byte[] checksum = hash2.Take(4).ToArray(); | |
| // 拼接地址與校驗碼 | |
| byte[] addressWithChecksum = addressBytes.Concat(checksum).ToArray(); | |
| // 轉換為 Base58 | |
| return EncodeBase58(addressWithChecksum); | |
| } | |
| } | |
| static byte[] HexToBytes(string hex) | |
| { | |
| return Enumerable.Range(0, hex.Length / 2) | |
| .Select(x => Convert.ToByte(hex.Substring(x * 2, 2), 16)) | |
| .ToArray(); | |
| } | |
| static string EncodeBase58(byte[] input) | |
| { | |
| var Base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; | |
| var intData = new BigInteger(input.Reverse().Concat(new byte[] { 0 }).ToArray()); | |
| string result = ""; | |
| while (intData > 0) | |
| { | |
| int remainder = (int)(intData % 58); | |
| intData /= 58; | |
| result = Base58Chars[remainder] + result; | |
| } | |
| // 保留前導 0 | |
| foreach (byte b in input) | |
| { | |
| if (b == 0) result = '1' + result; | |
| else break; | |
| } | |
| return result; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class GetTransactionByIdResponse | |
| { | |
| public List<Ret> ret { get; set; } | |
| public List<string> signature { get; set; } | |
| public string txID { get; set; } | |
| public RawData raw_data { get; set; } | |
| public string raw_data_hex { get; set; } | |
| public class Ret | |
| { | |
| public string contractRet { get; set; } | |
| } | |
| public class RawData | |
| { | |
| public List<Contract> contract { get; set; } | |
| public string ref_block_bytes { get; set; } | |
| public string ref_block_hash { get; set; } | |
| public long expiration { get; set; } | |
| public long timestamp { get; set; } | |
| public class Contract | |
| { | |
| public Parameter parameter { get; set; } | |
| public string type { get; set; } | |
| public class Parameter | |
| { | |
| public Value value { get; set; } | |
| public string type_url { get; set; } | |
| public class Value | |
| { | |
| public string data { get; set; } | |
| public long amount { get; set; } | |
| public string owner_address { get; set; } | |
| public string to_address { get; set; } | |
| } | |
| } | |
| } | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static GetTransactionByIdResponse GetTransactionByTXId(string txId) | |
| { | |
| string apiUrl = "https://api.trongrid.io/wallet/gettransactionbyid"; | |
| var client = new RestClient(apiUrl); | |
| var request = new RestRequest("", Method.Post); | |
| request.AddHeader("accept", "application/json"); | |
| request.AddHeader("content-type", "application/json"); | |
| request.AddJsonBody(new { value = txId }); | |
| var response = client.Execute(request); | |
| if (response.IsSuccessful) | |
| { | |
| //Console.WriteLine(response.Content); | |
| return JsonConvert.DeserializeObject<GetTransactionByIdResponse>(response.Content); | |
| } | |
| else | |
| { | |
| Console.WriteLine($"Error: {response.StatusCode} - {response.ErrorMessage}"); | |
| return null; | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //轉帳 TRX | |
| Console.WriteLine("Transfer TRX txid:497e9d02f78d22c9f19bab58b25384290d26b3eeac2362f0321d90b8515ea11a"); | |
| var trxTransResult = GetTransactionByTXId("497e9d02f78d22c9f19bab58b25384290d26b3eeac2362f0321d90b8515ea11a"); | |
| var from = ConvertHexToBase58Check(trxTransResult.raw_data.contract[0].parameter.value.owner_address); | |
| var to = ConvertHexToBase58Check(trxTransResult.raw_data.contract[0].parameter.value.to_address); | |
| Console.WriteLine("from:" + from + " ,to:" + to + " ,amount:" + trxTransResult.raw_data.contract[0].parameter.value.amount); | |
| Console.WriteLine("-------------"); | |
| //轉帳 USDT | |
| Console.WriteLine("Transfer Contract Token txid:ff67cb02dd3b358f4e8c0b1aa9de90e819ec9c564832d961619973f0d393ee9a"); | |
| //如果是 contract 的交易 to 跟 amount 要用原始資料去抓 | |
| var contractTransResult = GetTransactionByTXId("ff67cb02dd3b358f4e8c0b1aa9de90e819ec9c564832d961619973f0d393ee9a"); | |
| from = ConvertHexToBase58Check(contractTransResult.raw_data.contract[0].parameter.value.owner_address); | |
| //前面要加上"41" 轉 trx 地址 | |
| //從32 位 往後抓 40 個就是 to 地址 | |
| to = ConvertHexToBase58Check("41" + (contractTransResult.raw_data.contract[0].parameter.value.data.Substring(32, 40))); // 取後 20 bytes | |
| //最後64位就是金額 | |
| var amount = | |
| BigInteger.Parse(contractTransResult.raw_data.contract[0].parameter.value.data.Substring(contractTransResult.raw_data.contract[0].parameter.value.data.Length - 64, 64), NumberStyles.AllowHexSpecifier); | |
| Console.WriteLine("from:" + from + " ,to:" + to + " ,amount:" + amount); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment