From b4f65d52dce51f6e2f5c1a39e0d9a1690987337d Mon Sep 17 00:00:00 2001 From: Denis Angell Date: Sat, 11 May 2024 18:54:48 +0200 Subject: [PATCH] fix tags --- Base/Xrpl.AddressCodec/XrplAddressCodec.cs | 36 ++++++++++++-------- Tests/Xrpl.AddressCodec.Test/AddressTests.cs | 8 +++-- Tests/Xrpl.Tests/Wallet/WalletTest.cs | 2 +- Xrpl/Sugar/Autofill.cs | 5 +-- Xrpl/Utils/Derive.cs | 2 +- Xrpl/Wallet/XrplWallet.cs | 2 +- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/Base/Xrpl.AddressCodec/XrplAddressCodec.cs b/Base/Xrpl.AddressCodec/XrplAddressCodec.cs index d7a325f9..0a6c826f 100644 --- a/Base/Xrpl.AddressCodec/XrplAddressCodec.cs +++ b/Base/Xrpl.AddressCodec/XrplAddressCodec.cs @@ -1,4 +1,6 @@ using System; +using System.Diagnostics; +using System.Linq; //using Xrpl.KeypairsLib; // https://github.com/XRPLF/xrpl.js/blob/main/packages/ripple-address-codec/src/index.ts @@ -18,14 +20,14 @@ private static byte[] CopyOfRange(byte[] source, int from_, int to) public class CodecAddress { public string ClassicAddress { get; set; } - public int? Tag { get; set; } + public uint? Tag { get; set; } public bool Test { get; set; } } public class CodecAccountID { public byte[] AccountID { get; set; } - public int? Tag { get; set; } + public uint? Tag { get; set; } public bool Test { get; set; } } @@ -49,7 +51,7 @@ static XrplAddressCodec() /// The X-Address representation of the data. /// XRPLAddressCodecException: If the classic address does not have enough bytes /// or the tag is invalid. - public static string ClassicAddressToXAddress(string classicAddress, int? tag, bool isTest) + public static string ClassicAddressToXAddress(string classicAddress, uint? tag, bool isTest) { byte[] accountId = XrplCodec.DecodeAccountID(accountId: classicAddress); return EncodeXAddress(accountId, tag, isTest); @@ -62,7 +64,7 @@ public static string ClassicAddressToXAddress(string classicAddress, int? tag, b /// /// /// Account ID must be 20 bytes - public static string EncodeXAddress(byte[] accountId, int? tag, bool isTest) + public static string EncodeXAddress(byte[] accountId, uint? tag, bool isTest) { if (accountId.Length != 20) { @@ -73,7 +75,7 @@ public static string EncodeXAddress(byte[] accountId, int? tag, bool isTest) { throw new AddressCodecException("Invalid tag"); } - int theTag = tag ?? 0; + uint theTag = tag ?? 0; int flags = tag == null ? 0x00 : 0x01; byte[] prefix = isTest ? PREFIX_BYTES_TEST : PREFIX_BYTES_MAIN; byte[] postbytes = { @@ -116,7 +118,7 @@ public static CodecAccountID DecodeXAddress(string xAddress) byte[] decoded = B58.Decode(xAddress); bool isTest = IsTestAddress(decoded); byte[] accountId = CopyOfRange(decoded, 2, 22); - int? tag = TagFromBuffer(decoded); + uint? tag = TagFromBuffer(decoded); return new CodecAccountID { AccountID = accountId, Tag = tag, Test = isTest }; } @@ -146,24 +148,28 @@ public static bool IsTestAddress(byte[] buf) /// /// The destination tag extracted from the suffix of the X-Address. /// XRPLAddressCodecException: If the address is unsupported. - public static int? TagFromBuffer(byte[] buf) + public static uint? TagFromBuffer(byte[] buf) { - byte flag = buf[22]; + var flag = buf[22]; if (flag >= 2) { - // No support for 64-bit tags at this time throw new AddressCodecException("Unsupported X-address"); } if (flag == 1) { // Little-endian to big-endian - return buf[23] + buf[24] * 0x100 + buf[25] * 0x10000 + buf[26] * 0x1000000; + return (uint?)(buf[23] + buf[24] * 0x100 + buf[25] * 0x10000 + buf[26] * 0x1000000); + } + if (flag != 0) + { + throw new AddressCodecException("flag must be zero to indicate no tag"); + } + var remainingBytes = new byte[8]; + Array.Copy(buf, 23, remainingBytes, 0, 8); + if (!Enumerable.SequenceEqual(remainingBytes, new byte[8])) + { + throw new AddressCodecException("remaining bytes must be zero"); } - //assert.strictEqual(flag, 0, 'flag must be zero to indicate no tag') - //assert.ok( - // Buffer.from('0000000000000000', 'hex').equals(buf.slice(23, 23 + 8)), - //'remaining bytes must be zero', - //) return null; } diff --git a/Tests/Xrpl.AddressCodec.Test/AddressTests.cs b/Tests/Xrpl.AddressCodec.Test/AddressTests.cs index d4b252f6..18510769 100644 --- a/Tests/Xrpl.AddressCodec.Test/AddressTests.cs +++ b/Tests/Xrpl.AddressCodec.Test/AddressTests.cs @@ -3,11 +3,13 @@ using Newtonsoft.Json; using System.Collections.Generic; +using System.Diagnostics; // https://github.com/XRPLF/xrpl.js/blob/main/packages/ripple-address-codec/src/index.test.js namespace Xrpl.AddressCodec.Tests { + using static Xrpl.AddressCodec.XrplAddressCodec; using static XrplAddressCodec; [TestClass] @@ -27,7 +29,7 @@ public void InvalidXAddressF() { var testCase = testCases[i]; string classicAddress = testCase[0]; - int? tag = testCase[1] is bool && testCase[1] == false ? null : (int)testCase[1]; + uint? tag = testCase[1] is bool && testCase[1] == false ? null : (uint)testCase[1]; string xAddress = testCase[2]; Assert.AreEqual(xAddress, ClassicAddressToXAddress(classicAddress, tag, false)); CodecAddress myClassicAddress = XAddressToClassicAddress(xAddress); @@ -43,7 +45,7 @@ public void InvalidXAddressF() { var testCase = testCases[i]; string classicAddress = testCase[0]; - int? tag = testCase[1] is bool && testCase[1] == false ? null : (int)testCase[1]; + uint? tag = testCase[1] is bool && testCase[1] == false ? null : (uint)testCase[1]; string xAddress = testCase[3]; Assert.AreEqual(xAddress, ClassicAddressToXAddress(classicAddress, tag, true)); CodecAddress myClassicAddress = XAddressToClassicAddress(xAddress); @@ -78,7 +80,7 @@ public void InvalidXAddressReturns() public void ConvertTagFalse() { string classicAddress = "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59"; - int? tag = null; + uint? tag = null; string xAddress = "X7AcgcsBL6XDcUb289X4mJ8djcdyKaB5hJDWMArnXr61cqZ"; bool isTestAddress = false; Assert.AreEqual(xAddress, ClassicAddressToXAddress(classicAddress, tag, isTestAddress)); diff --git a/Tests/Xrpl.Tests/Wallet/WalletTest.cs b/Tests/Xrpl.Tests/Wallet/WalletTest.cs index 6bd79cc4..5e828f33 100644 --- a/Tests/Xrpl.Tests/Wallet/WalletTest.cs +++ b/Tests/Xrpl.Tests/Wallet/WalletTest.cs @@ -729,7 +729,7 @@ public class TestUXAddress private static string publicKey = "030E58CDD076E798C84755590AAF6237CA8FAE821070A59F648B517A30DC6F589D"; private static string privateKey = "00141BA006D3363D2FB2785E8DF4E44D3A49908780CB4FB51F6D217C08C021429F"; private XrplWallet wallet = new XrplWallet(publicKey, privateKey); - private int tag = 1337; + private uint tag = 1337; private string mainnetXAddress = "X7gJ5YK8abHf2eTPWPFHAAot8Knck11QGqmQ7a6a3Z8PJvk"; private string testnetXAddress = "T7bq3e7kxYq9pwDz8UZhqAZoEkcRGTXSNr5immvcj3DYRaV"; diff --git a/Xrpl/Sugar/Autofill.cs b/Xrpl/Sugar/Autofill.cs index 317dafc2..531b3655 100644 --- a/Xrpl/Sugar/Autofill.cs +++ b/Xrpl/Sugar/Autofill.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Globalization; using System.Numerics; using System.Threading.Tasks; @@ -23,7 +24,7 @@ namespace Xrpl.Sugar public class AddressNTag { public string ClassicAddress { get; set; } - public int? Tag { get; set; } + public uint? Tag { get; set; } } public static class AutofillSugar @@ -114,7 +115,7 @@ public static void ValidateAccountAddress(this Dictionary tx, s } } - public static AddressNTag GetClassicAccountAndTag(this string account, int? expectedTag) + public static AddressNTag GetClassicAccountAndTag(this string account, uint? expectedTag) { if (XrplAddressCodec.IsValidXAddress(account)) { diff --git a/Xrpl/Utils/Derive.cs b/Xrpl/Utils/Derive.cs index 49a35e80..059c0566 100644 --- a/Xrpl/Utils/Derive.cs +++ b/Xrpl/Utils/Derive.cs @@ -20,7 +20,7 @@ public static class Derive /// Whether this address is for use in Testnet. /// X-Address. /// Utilities - public static string DeriveXAddress(string publicKey, int? tag, bool test) + public static string DeriveXAddress(string publicKey, uint? tag, bool test) { var classicAddress = XrplKeypairs.DeriveAddress(publicKey); return XrplAddressCodec.ClassicAddressToXAddress(classicAddress, tag, test); diff --git a/Xrpl/Wallet/XrplWallet.cs b/Xrpl/Wallet/XrplWallet.cs index ac0d3499..97a6ecf3 100644 --- a/Xrpl/Wallet/XrplWallet.cs +++ b/Xrpl/Wallet/XrplWallet.cs @@ -172,7 +172,7 @@ public bool VerifyTransaction(string signedTransaction) return XrplKeypairs.Verify(messageHex.FromHex(), signature, this.PublicKey); } - public string GetXAddress(int tag, bool isTestnet = false) + public string GetXAddress(uint tag, bool isTestnet = false) { return XrplAddressCodec.ClassicAddressToXAddress(this.ClassicAddress, tag, isTestnet); }