Skip to content

Commit

Permalink
Fix unknown test
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Feb 28, 2025
1 parent 564e620 commit d756c8c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 25 deletions.
14 changes: 0 additions & 14 deletions NBitcoin.Tests/PSBT2Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,6 @@ namespace NBitcoin.Tests
[Trait("UnitTest", "UnitTest")]
public class PSBT2Tests
{
private readonly ITestOutputHelper Output;

private static PSBTComparer ComparerInstance { get; }

static PSBT2Tests()
{
ComparerInstance = new PSBTComparer();
}

public PSBT2Tests(ITestOutputHelper output)
{
Output = output;
}

void AssertDetermineLockTime(string hex, string base64, LockTime want)
{

Expand Down
2 changes: 2 additions & 0 deletions NBitcoin.Tests/PSBTTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ public void CanHandleUnKnown()
data1.Combine(data2);
var expected = PSBT.Parse((string)testdata["psbtUnknown2"], Network.Main);
Assert.Equal(data1, expected, ComparerInstance);


}
[Fact]
[Trait("UnitTest", "UnitTest")]
Expand Down
4 changes: 2 additions & 2 deletions NBitcoin/BIP174/PSBT1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ internal PSBT1(List<Map> maps, Network network) : base(network, 1)
GlobalXPubs.Add(xpub.GetWif(Network), rootedKeyPath);
break;
default:
if (!unknown.TryAdd(k, v))
if (!Unknown.TryAdd(k, v))
throw new FormatException($"Invalid PSBT, duplicate key ({Encoders.Hex.EncodeData(k)}) for unknown value");
break;
}
Expand All @@ -106,7 +106,7 @@ internal PSBT1(List<Map> maps, Network network) : base(network, 1)
foreach (var indexedInput in tx.Inputs.AsIndexedInputs())
{
var map = maps[(int)(indexedInput.Index + 1)];
if (map.Keys.Any(bytes => PSBT2Constants.PSBT_V0_INPUT_EXCLUSIONSET.Contains(bytes[0])))
if (map.Keys.Any(bytes => bytes.Length == 0 && PSBT2Constants.PSBT_V0_INPUT_EXCLUSIONSET.Contains(bytes[0])))
throw new FormatException("Invalid PSBT v0. Contains v2 fields");
Inputs.Add(new PSBTInput1(map, this, indexedInput.Index));
}
Expand Down
16 changes: 8 additions & 8 deletions NBitcoin/BIP174/PartiallySignedTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public int Compare(BitcoinExtPubKey? x, BitcoinExtPubKey? y)
public PSBTInputList Inputs { get; protected set; } = new();
public PSBTOutputList Outputs { get; protected set;} = new();

internal Map unknown { get; set; } = new Map(BytesComparer.Instance);
public Map Unknown { get; private set; } = new Map(BytesComparer.Instance);

/// <summary>
/// Parse PSBT from a hex or base64 string.
Expand Down Expand Up @@ -340,8 +340,8 @@ public PSBT Combine(PSBT other)
for (int i = 0; i < Outputs.Count; i++)
this.Outputs[i].UpdateFrom(other.Outputs[i]);

foreach (var uk in other.unknown)
this.unknown.TryAdd(uk.Key, uk.Value);
foreach (var uk in other.Unknown)
this.Unknown.TryAdd(uk.Key, uk.Value);

return this;
}
Expand Down Expand Up @@ -372,8 +372,8 @@ public PSBT UpdateFrom(PSBT other)
foreach (var thisOutput in this.Outputs.Where(o => o.ScriptPubKey == otherOutput.ScriptPubKey))
thisOutput.UpdateFrom(otherOutput);

foreach (var uk in other.unknown)
this.unknown.TryAdd(uk.Key, uk.Value);
foreach (var uk in other.Unknown)
this.Unknown.TryAdd(uk.Key, uk.Value);

return this;
}
Expand Down Expand Up @@ -830,7 +830,7 @@ internal void Serialize(BitcoinStream stream)
}

// Write the unknown things
foreach (var kv in unknown)
foreach (var kv in Unknown)
{
globalMap.Add(kv.Key, kv.Value);
}
Expand Down Expand Up @@ -892,11 +892,11 @@ public override string ToString()
}
jsonWriter.WriteEndArray();
}
if (unknown.Count != 0)
if (Unknown.Count != 0)
{
jsonWriter.WritePropertyName("unknown");
jsonWriter.WriteStartObject();
foreach (var el in unknown)
foreach (var el in Unknown)
{
jsonWriter.WritePropertyValue(Encoders.Hex.EncodeData(el.Key), Encoders.Hex.EncodeData(el.Value));
}
Expand Down
2 changes: 1 addition & 1 deletion NBitcoin/BIP370/PSBT2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal PSBT2(List<Map> maps, Network network) : base(network, 2)

while (globalMap.Pop(out byte[] k, out byte[] v))
{
if (!unknown.TryAdd(k, v))
if (!Unknown.TryAdd(k, v))
throw new FormatException($"Invalid PSBT, duplicate key ({Encoders.Hex.EncodeData(k)}) for unknown value");
}

Expand Down

0 comments on commit d756c8c

Please sign in to comment.