Skip to content

Commit

Permalink
fix tags (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangell7 authored May 11, 2024
1 parent 8c33566 commit 971080d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
36 changes: 21 additions & 15 deletions Base/Xrpl.AddressCodec/XrplAddressCodec.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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; }
}

Expand All @@ -49,7 +51,7 @@ static XrplAddressCodec()
/// <returns>The X-Address representation of the data.</returns>
/// <throws>XRPLAddressCodecException: If the classic address does not have enough bytes
/// or the tag is invalid.</throws>
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);
Expand All @@ -62,7 +64,7 @@ public static string ClassicAddressToXAddress(string classicAddress, int? tag, b
/// <param name="isTest"></param>
/// <returns></returns>
/// <exception cref="AddressCodecException">Account ID must be 20 bytes</exception>
public static string EncodeXAddress(byte[] accountId, int? tag, bool isTest)
public static string EncodeXAddress(byte[] accountId, uint? tag, bool isTest)
{
if (accountId.Length != 20)
{
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 };
}

Expand Down Expand Up @@ -146,24 +148,28 @@ public static bool IsTestAddress(byte[] buf)
/// <param buffer="bytes[]"></param>
/// <returns>The destination tag extracted from the suffix of the X-Address.</returns>
/// <throws>XRPLAddressCodecException: If the address is unsupported.</throws>
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;
}

Expand Down
8 changes: 5 additions & 3 deletions Tests/Xrpl.AddressCodec.Test/AddressTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion Tests/Xrpl.Tests/Wallet/WalletTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
5 changes: 3 additions & 2 deletions Xrpl/Sugar/Autofill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Numerics;
using System.Threading.Tasks;
Expand All @@ -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
Expand Down Expand Up @@ -114,7 +115,7 @@ public static void ValidateAccountAddress(this Dictionary<string, dynamic> tx, s
}
}

public static AddressNTag GetClassicAccountAndTag(this string account, int? expectedTag)
public static AddressNTag GetClassicAccountAndTag(this string account, uint? expectedTag)
{
if (XrplAddressCodec.IsValidXAddress(account))
{
Expand Down
2 changes: 1 addition & 1 deletion Xrpl/Utils/Derive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class Derive
/// <param name="test">Whether this address is for use in Testnet.</param>
/// <returns>X-Address.</returns>
/// <category>Utilities</category>
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);
Expand Down
2 changes: 1 addition & 1 deletion Xrpl/Wallet/XrplWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 971080d

Please sign in to comment.