Skip to content

Commit

Permalink
Fix bug unformatted signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
shiwk committed Jan 14, 2019
1 parent f1e344e commit f5baebb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
6 changes: 4 additions & 2 deletions AElf.Contracts.Authorization/AuthorizationContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,11 @@ private bool CheckPermission(Approved approved, Kernel.Authorization authorizati
// processing approvals
var validApprovals = approved.Approvals.All(a =>
{
var recovered = CryptoHelpers.RecoverPublicKey(a.Signature.ToByteArray(), toSig);
var canBeRecovered =
CryptoHelpers.RecoverPublicKey(a.Signature.ToByteArray(), toSig, out var recovered);
if (!canBeRecovered)
return false;
var reviewer = authorization.Reviewers.FirstOrDefault(r => r.PubKey.SequenceEqual(recovered));
if (reviewer == null)
return false;
Expand Down
8 changes: 6 additions & 2 deletions AElf.Cryptography/CryptoHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,22 @@ public static byte[] SignWithPrivateKey(byte[] privateKey, byte[] hash)
}
}

public static byte[] RecoverPublicKey(byte[] signature, byte[] hash)
public static bool RecoverPublicKey(byte[] signature, byte[] hash, out byte[] publicKey)
{
publicKey = null;
try
{
if (signature.Length != Secp256k1.SERIALIZED_UNCOMPRESSED_PUBKEY_LENGTH)
return false;
Lock.AcquireWriterLock(Timeout.Infinite);
var pubKey = new byte[Secp256k1.SERIALIZED_UNCOMPRESSED_PUBKEY_LENGTH];
var recoveredPubKey = new byte[Secp256k1.PUBKEY_LENGTH];
var recSig = new byte[65];
Secp256K1.RecoverableSignatureParseCompact(recSig, signature, signature.Last());
Secp256K1.Recover(recoveredPubKey, recSig, hash);
Secp256K1.PublicKeySerialize(pubKey, recoveredPubKey);
return pubKey;
publicKey = pubKey;
return true;
}
catch (Exception ex)
{
Expand Down
5 changes: 3 additions & 2 deletions AElf.Kernel.Types/Transaction/TxSignatureVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public bool Verify(Kernel.Transaction tx)

if (tx.Sigs.Count == 1 && tx.Type != TransactionType.MsigTransaction)
{
var pubKey = CryptoHelpers.RecoverPublicKey(tx.Sigs.First().ToByteArray(), tx.GetHash().DumpByteArray());
return Address.FromPublicKey(pubKey).Equals(tx.From);
var canBeRecovered = CryptoHelpers.RecoverPublicKey(tx.Sigs.First().ToByteArray(),
tx.GetHash().DumpByteArray(), out var pubKey);
return canBeRecovered && Address.FromPublicKey(pubKey).Equals(tx.From);
}

foreach (var sig in tx.Sigs)
Expand Down
21 changes: 17 additions & 4 deletions AElf.Miner/TxMemPool/TxHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,15 @@ private async Task<bool> ValidateMinersAuthorization(TransactionReceipt tr)
Weight = 1 // BP weight
}));
var hash = tr.Transaction.GetHash().DumpByteArray();
return _authorizationInfoReader.ValidateAuthorization(auth,
tr.Transaction.Sigs.Select(sig => CryptoHelpers.RecoverPublicKey(sig.ToByteArray(), hash)).ToArray());
var publicKeys = new List<byte[]>();
foreach (var sig in tr.Transaction.Sigs)
{
var canBeRecovered = CryptoHelpers.RecoverPublicKey(sig.ToByteArray(), hash, out var publicKey);
if (!canBeRecovered)
return false;
publicKeys.Add(publicKey);
}
return _authorizationInfoReader.ValidateAuthorization(auth, publicKeys);
}


Expand All @@ -296,8 +303,14 @@ private async Task<bool> CheckAuthority(Transaction transaction)
if (transaction.Sigs.Count == 1)
return true;
// Get pub keys
var publicKeys = transaction.Sigs.Select(sig => CryptoHelpers.RecoverPublicKey(sig.ToByteArray(), hash))
.ToArray();
var publicKeys = new List<byte[]>();
foreach (var sig in transaction.Sigs)
{
var canBeRecovered = CryptoHelpers.RecoverPublicKey(sig.ToByteArray(), hash, out var publicKey);
if (!canBeRecovered)
return false;
publicKeys.Add(publicKey);
}

return await _authorizationInfoReader.CheckAuthority(transaction.From, publicKeys);
}
Expand Down
14 changes: 10 additions & 4 deletions AElf.Sdk.CSharp/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public static Address GetContractAddress()

public static byte[] RecoverPublicKey(byte[] signature, byte[] hash)
{
return CryptoHelpers.RecoverPublicKey(signature, hash);
var cabBeRecovered = CryptoHelpers.RecoverPublicKey(signature, hash, out var publicKey);
return !cabBeRecovered ? null : publicKey;
}

/// <summary>
Expand Down Expand Up @@ -438,9 +439,14 @@ public static void CheckAuthority(Address fromAddress = null)
var hash = _transactionContext.Transaction.GetHash().DumpByteArray();

// Get pub keys
var publicKeys = _transactionContext.Transaction.Sigs
.Select(sig => CryptoHelpers.RecoverPublicKey(sig.ToByteArray(), hash)).ToArray();

var publicKeys = new List<byte[]>();
foreach (var sig in _transactionContext.Transaction.Sigs)
{
var publicKey = RecoverPublicKey(sig.ToByteArray(), hash);
Assert (publicKey != null, "Invalid signature."); // this should never happen.
publicKeys.Add(publicKey);
}

//todo review correctness
uint provided = publicKeys
.Select(pubKey => auth.Reviewers.FirstOrDefault(r => r.PubKey.ToByteArray().SequenceEqual(pubKey)))
Expand Down

0 comments on commit f5baebb

Please sign in to comment.