Skip to content

Commit 6413638

Browse files
committed
Fix tests
1 parent 48341d5 commit 6413638

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

NBitcoin.Tests/key_tests.cs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -198,28 +198,31 @@ public void key_test1()
198198
}
199199
// compact signatures (with key recovery)
200200

201-
byte[] csign1 = null, csign2 = null, csign1C = null, csign2C = null;
202-
203-
csign1 = key1.SignCompact(hashMsg);
204-
csign2 = key2.SignCompact(hashMsg);
205-
csign1C = key1C.SignCompact(hashMsg);
206-
csign2C = key2C.SignCompact(hashMsg);
207-
208-
PubKey rkey1 = null, rkey2 = null, rkey1C = null, rkey2C = null;
209-
rkey1 = PubKey.RecoverCompact(hashMsg, csign1);
210-
rkey2 = PubKey.RecoverCompact(hashMsg, csign2);
211-
rkey1C = PubKey.RecoverCompact(hashMsg, csign1C);
212-
rkey2C = PubKey.RecoverCompact(hashMsg, csign2C);
213-
214-
Assert.True(rkey1.ToHex() == pubkey1.ToHex());
215-
Assert.True(rkey2.ToHex() == pubkey2.ToHex());
216-
Assert.True(rkey1C.ToHex() == pubkey1C.ToHex());
217-
Assert.True(rkey2C.ToHex() == pubkey2C.ToHex());
218-
219-
Assert.True(sign1.IsLowR && sign1.ToDER().Length <= 70);
220-
Assert.True(sign2.IsLowR && sign2.ToDER().Length <= 70);
221-
Assert.True(sign1C.IsLowR && sign1C.ToDER().Length <= 70);
222-
Assert.True(sign2C.IsLowR && sign2C.ToDER().Length <= 70);
201+
CompactSignature csign1 = null, csign2 = null, csign1C = null, csign2C = null;
202+
203+
if (key1.IsCompressed)
204+
{
205+
csign1 = key1.SignCompact(hashMsg);
206+
csign2 = key2.SignCompact(hashMsg);
207+
csign1C = key1C.SignCompact(hashMsg);
208+
csign2C = key2C.SignCompact(hashMsg);
209+
210+
PubKey rkey1 = null, rkey2 = null, rkey1C = null, rkey2C = null;
211+
rkey1 = PubKey.RecoverCompact(hashMsg, csign1);
212+
rkey2 = PubKey.RecoverCompact(hashMsg, csign2);
213+
rkey1C = PubKey.RecoverCompact(hashMsg, csign1C);
214+
rkey2C = PubKey.RecoverCompact(hashMsg, csign2C);
215+
216+
Assert.True(rkey1.ToHex() == pubkey1.ToHex());
217+
Assert.True(rkey2.ToHex() == pubkey2.ToHex());
218+
Assert.True(rkey1C.ToHex() == pubkey1C.ToHex());
219+
Assert.True(rkey2C.ToHex() == pubkey2C.ToHex());
220+
221+
Assert.True(sign1.IsLowR && sign1.ToDER().Length <= 70);
222+
Assert.True(sign2.IsLowR && sign2.ToDER().Length <= 70);
223+
Assert.True(sign1C.IsLowR && sign1C.ToDER().Length <= 70);
224+
Assert.True(sign2C.IsLowR && sign2C.ToDER().Length <= 70);
225+
}
223226
}
224227
}
225228

NBitcoin/Key.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,8 @@ public CompactSignature SignCompact(uint256 hash, bool forceLowR)
233233
AssertNotDisposed();
234234
#if HAS_SPAN
235235
byte[] sigBytes = new byte[64];
236-
int rec = -1;
237-
var sig = new Secp256k1.SecpRecoverableECDSASignature(_ECKey.Sign(hash, forceLowR, out rec), rec);
238-
sig.WriteToSpanCompact(sigBytes, out int recid);
236+
var sig = new Secp256k1.SecpRecoverableECDSASignature(_ECKey.Sign(hash, forceLowR, out var rec), rec);
237+
sig.WriteToSpanCompact(sigBytes, out _);
239238
return new CompactSignature(rec, sigBytes);
240239
#else
241240
var sig = _ECKey.Sign(hash, forceLowR);

0 commit comments

Comments
 (0)