Skip to content

Commit

Permalink
Add support for wallet policies (BIP388)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Dec 20, 2024
1 parent d99cc4e commit 20467b8
Show file tree
Hide file tree
Showing 13 changed files with 2,879 additions and 6 deletions.
499 changes: 499 additions & 0 deletions NBitcoin.Tests/MiniscriptTests.cs

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions NBitcoin.Tests/NBitcoin.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
<DefineConstants>$(DefineConstants);$(AdditionalDefineConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net472' ">
<DefineConstants>$(DefineConstants);WIN</DefineConstants>
<DefineConstants>$(DefineConstants);WIN;NO_RECORDS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFrameworkOverride)' == 'netstandard2.0'">
<DefineConstants>$(DefineConstants);NO_RECORDS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1' Or '$(TargetFramework)' == 'netcoreapp3.1' ">
<DefineConstants>$(DefineConstants);NETCORE;NOTRACESOURCE;NOCUSTOMSSLVALIDATION;NOHTTPSERVER</DefineConstants>
<DefineConstants>$(DefineConstants);NETCORE;NOTRACESOURCE;NOCUSTOMSSLVALIDATION;NOHTTPSERVER;NO_RECORDS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" ('$(TargetFramework)' == 'netstandard2.1' Or '$(TargetFramework)' == 'net6.0' Or '$(TargetFramework)' == 'netcoreapp3.1') And ( '$(TargetFrameworkOverride)' != 'netstandard2.0' ) ">
<DefineConstants>$(DefineConstants);NETCORE;HAS_SPAN;NO_BC</DefineConstants>
Expand Down
8 changes: 7 additions & 1 deletion NBitcoin/Crypto/Hashes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ public static uint160 Hash160(byte[] data, int count)

public static uint160 Hash160(byte[] data, int offset, int count)
{
return new uint160(RIPEMD160(SHA256(data, offset, count)));
return new uint160(Hash160RawBytes(data, offset, count));
}
public static byte[] Hash160RawBytes(byte[] data)
=> Hash160RawBytes(data, 0, data.Length);
public static byte[] Hash160RawBytes(byte[] data, int offset, int count)
{
return RIPEMD160(SHA256(data, offset, count));
}
#endregion

Expand Down
6 changes: 3 additions & 3 deletions NBitcoin/NBitcoin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<DocumentationFile>bin\Release\NBitcoin.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net472' ">
<DefineConstants>$(DefineConstants);CLASSICDOTNET;NO_ARRAY_FILL;NULLABLE_SHIMS;NO_SOCKETASYNC</DefineConstants>
<DefineConstants>$(DefineConstants);CLASSICDOTNET;NO_ARRAY_FILL;NULLABLE_SHIMS;NO_SOCKETASYNC;NO_RECORDS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.1' Or '$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'net6.0'">
<DefineConstants>$(DefineConstants);NOCUSTOMSSLVALIDATION;NO_NATIVERIPEMD160</DefineConstants>
Expand All @@ -41,10 +41,10 @@
<RemoveBC>true</RemoveBC>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
<DefineConstants>$(DefineConstants);NO_SOCKETASYNC</DefineConstants>
<DefineConstants>$(DefineConstants);NO_SOCKETASYNC;NO_RECORDS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<DefineConstants>$(DefineConstants);NETSTANDARD;NO_ARRAY_FILL;NULLABLE_SHIMS;NO_NATIVE_RFC2898_HMACSHA512;NO_NATIVERIPEMD160;NO_SOCKETASYNC</DefineConstants>
<DefineConstants>$(DefineConstants);NETSTANDARD;NO_ARRAY_FILL;NULLABLE_SHIMS;NO_NATIVE_RFC2898_HMACSHA512;NO_NATIVERIPEMD160;NO_SOCKETASYNC;NO_RECORDS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DefineConstants>$(DefineConstants);SECP256K1_VERIFY</DefineConstants>
Expand Down
10 changes: 10 additions & 0 deletions NBitcoin/WalletPolicies/AddressIntent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#if !NO_RECORDS
namespace NBitcoin.WalletPolicies
{
public enum AddressIntent
{
Deposit,
Change
}
}
#endif
59 changes: 59 additions & 0 deletions NBitcoin/WalletPolicies/DeriveParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#if !NO_RECORDS
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NBitcoin.WalletPolicies
{
public class DeriveParameters
{
/// <summary>
///
/// </summary>
/// <param name="intent">Whether this is a deposit or change address</param>
/// <param name="index">The address index</param>
public DeriveParameters(AddressIntent intent, int index)
{
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index), "index should be positive");
Intent = intent;
AddressIndexes = new int[] { index };
}
/// <summary>
///
/// </summary>
/// <param name="intent">Whether this is a deposit or change address</param>
/// <param name="indexes">The addresses to derive</param>
public DeriveParameters(AddressIntent intent, int[]? indexes)
{
Intent = intent;
AddressIndexes = indexes ?? Array.Empty<int>();
foreach (var idx in AddressIndexes)
if (idx < 0)
throw new ArgumentOutOfRangeException(nameof(indexes), "indexes should be positive");
}
/// <summary>
///
/// </summary>
/// <param name="intent">Whether this is a deposit or change address</param>
/// <param name="startIndex">The first address to start generating</param>
/// <param name="count">The number of addresses to generate</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public DeriveParameters(AddressIntent intent, int startIndex, int count)
{
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex), "startIndex should be positive");
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(count), "count should be positive");
Intent = intent;
AddressIndexes = Enumerable.Range(startIndex, count).ToArray();
}
public AddressIntent Intent { get; }
public int[] AddressIndexes { get; set; }
}
}
#endif
Loading

0 comments on commit 20467b8

Please sign in to comment.