Skip to content

Commit

Permalink
Add a new BIP0032Path constructor to instantiate it based on Electrum…
Browse files Browse the repository at this point in the history
… mnemonic types + tests + test cleanup
  • Loading branch information
Coding-Enthusiast committed Jan 25, 2025
1 parent 419207e commit 1481fcd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
19 changes: 19 additions & 0 deletions Src/Autarkysoft.Bitcoin/ImprovementProposals/BIP0032Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ public BIP0032Path(string path)
}
}

/// <summary>
/// Initializes a new instance of <see cref="BIP0032Path"/> based on the given
/// Electrum mnemonic type.
/// </summary>
/// <exception cref="ArgumentException">Thrown when given an undefined mnemonic type enum</exception>
/// <param name="mnType">Electrum mnemonic type</param>
public BIP0032Path(ElectrumMnemonic.MnemonicType mnType)
{
Indexes = mnType switch
{
ElectrumMnemonic.MnemonicType.Undefined => new uint[0], // m/
ElectrumMnemonic.MnemonicType.Standard => new uint[1], // m/0/
ElectrumMnemonic.MnemonicType.SegWit => new uint[2] { 0 + HardenedIndex, 0 }, // m/0'/0/
ElectrumMnemonic.MnemonicType.Legacy2Fa => new uint[2] { 1 + HardenedIndex, 0 }, // m/1'/0/
ElectrumMnemonic.MnemonicType.SegWit2Fa => new uint[2] { 1 + HardenedIndex, 0 }, // m/1'/0/
_ => throw new ArgumentException("Undefined Electrum mnemonic type."),
};
}



/// <summary>
Expand Down
38 changes: 29 additions & 9 deletions Src/Tests/Bitcoin/ImprovementProposals/BIP0032PathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using Autarkysoft.Bitcoin.ImprovementProposals;
using System;
using Xunit;

namespace Tests.Bitcoin.ImprovementProposals
{
Expand All @@ -17,14 +16,14 @@ public class BIP0032PathTests
public void Constructor_FromUIntsTest()
{
uint[] uints = { 0x80000000, 1, 0x80000000 + 2, 2 };
BIP0032Path path = new BIP0032Path(uints);
BIP0032Path path = new(uints);
Assert.Equal(uints, path.Indexes);
}

[Fact]
public void Constructor_FromUInts_NullTest()
{
BIP0032Path path = new BIP0032Path();
BIP0032Path path = new();
Assert.Empty(path.Indexes);
}

Expand All @@ -45,15 +44,15 @@ public void Constructor_FromUInts_NullTest()
[InlineData("m/5/", new uint[1] { 5 })]
public void Constructor_FromStringTest(string toUse, uint[] expected)
{
var path = new BIP0032Path(toUse);
BIP0032Path path = new(toUse);
Assert.Equal(expected, path.Indexes);
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void Constructor_FromString_NullExceptionTest(string path)
public void Constructor_FromString_NullExceptionTest(string? path)
{
Assert.Throws<ArgumentNullException>(() => new BIP0032Path(path));
}
Expand Down Expand Up @@ -84,6 +83,27 @@ public void Constructor_FromString_DepthOverflowTest()
Assert.Contains("Depth can not be bigger than 1 byte.", ex.Message);
}

[Theory]
[InlineData(ElectrumMnemonic.MnemonicType.Undefined, new uint[0])]
[InlineData(ElectrumMnemonic.MnemonicType.Standard, new uint[1] { 0 })]
[InlineData(ElectrumMnemonic.MnemonicType.SegWit, new uint[2] { Hard, 0 })]
[InlineData(ElectrumMnemonic.MnemonicType.Legacy2Fa, new uint[2] { 1 + Hard, 0 })]
[InlineData(ElectrumMnemonic.MnemonicType.SegWit2Fa, new uint[2] { 1 + Hard, 0 })]
public void Constructor_FromElectrumMnTypeTest(ElectrumMnemonic.MnemonicType mnType, uint[] expected)
{
BIP0032Path path = new(mnType);
Assert.Equal(expected, path.Indexes);
}

[Fact]
public void Constructor_FromElectrumMnType_ExcetionTest()
{
ElectrumMnemonic.MnemonicType invalidType = (ElectrumMnemonic.MnemonicType)1000;
Exception ex = Assert.Throws<ArgumentException>(() => new BIP0032Path(invalidType));
Assert.Contains("Undefined Electrum mnemonic type.", ex.Message);
}


[Theory]
[InlineData(BIP0032Path.CoinType.Bitcoin, 0 + Hard, false, "m/44'/0'/0'/0")]
[InlineData(BIP0032Path.CoinType.Bitcoin, 0 + Hard, true, "m/44'/0'/0'/1")]
Expand All @@ -97,7 +117,7 @@ public void Constructor_FromString_DepthOverflowTest()
[InlineData((BIP0032Path.CoinType)(1000 + Hard), 1 + Hard, true, "m/44'/1000'/1'/1")]
public void CreateBip44Test(BIP0032Path.CoinType ct, uint account, bool isChange, string expected)
{
var path = BIP0032Path.CreateBip44(ct, account, isChange);
BIP0032Path path = BIP0032Path.CreateBip44(ct, account, isChange);
string actual = path.ToString();
Assert.Equal(expected, actual);
}
Expand All @@ -115,15 +135,15 @@ public void CreateBip44Test(BIP0032Path.CoinType ct, uint account, bool isChange
[InlineData((BIP0032Path.CoinType)(1000 + Hard), 1 + Hard, true, "m/49'/1000'/1'/1")]
public void CreateBip49Test(BIP0032Path.CoinType ct, uint account, bool isChange, string expected)
{
var path = BIP0032Path.CreateBip49(ct, account, isChange);
BIP0032Path path = BIP0032Path.CreateBip49(ct, account, isChange);
string actual = path.ToString();
Assert.Equal(expected, actual);
}

[Fact]
public void AddTest()
{
var path = new BIP0032Path(1, 0x12345678, 3);
BIP0032Path path = new(1, 0x12345678, 3);
Assert.Equal(new uint[] { 1, 0x12345678, 3 }, path.Indexes);

path.Add(5);
Expand All @@ -140,7 +160,7 @@ public void AddTest()
[InlineData(new uint[] { Hard, 1, Hard + 2, 2 }, "m/0'/1/2'/2")]
public void ToStringTest(uint[] toUse, string expected)
{
var path = new BIP0032Path(toUse);
BIP0032Path path = new(toUse);
string actual = path.ToString();
Assert.Equal(expected, actual);
}
Expand Down

0 comments on commit 1481fcd

Please sign in to comment.