Пример #1
0
 public static byte[] SignWithPrivateKey(byte[] privateKey, byte[] hash)
 {
     try
     {
         Lock.AcquireWriterLock(Timeout.Infinite);
         var recSig     = new byte[65];
         var compactSig = new byte[65];
         Secp256K1.SignRecoverable(recSig, hash, privateKey);
         Secp256K1.RecoverableSignatureSerializeCompact(compactSig, out var recoverId, recSig);
         compactSig[64] = (byte)recoverId;  // put recover id at the last slot
         return(compactSig);
     }
     finally
     {
         Lock.ReleaseWriterLock();
     }
 }
Пример #2
0
 public static byte[] SignWithPrivateKey(byte[] privateKey, byte[] hash)
 {
     try
     {
         Lock.AcquireWriterLock(Timeout.Infinite);
         var recSig     = new byte[65];
         var compactSig = new byte[65];
         if (!Secp256K1.SignRecoverable(recSig, hash, privateKey))
         {
             throw new SignatureOperationException("Create a recoverable ECDSA signature failed.");
         }
         if (!Secp256K1.RecoverableSignatureSerializeCompact(compactSig, out var recoverId, recSig))
         {
             throw new SignatureOperationException("Serialize an ECDSA signature failed.");
         }
         compactSig[64] = (byte)recoverId;  // put recover id at the last slot
         return(compactSig);
     }
     finally
     {
         Lock.ReleaseWriterLock();
     }
 }
Пример #3
0
 // mimic of SignHashed of DefaultCrypto.cs but using different chain id
 public byte[] SignHashed(byte[] messageHash, byte[] privateKey, bool useNewChainId)
 {
     if (privateKey.Length != 32)
     {
         throw new ArgumentException(nameof(privateKey));
     }
     if (messageHash.Length != 32)
     {
         throw new ArgumentException(nameof(messageHash));
     }
     return(EcSign.Benchmark(() =>
     {
         var sig = new byte[65];
         if (!Secp256K1.SignRecoverable(sig, messageHash, privateKey))
         {
             throw new Exception("secp256k1.sign_recoverable failed");
         }
         var serialized = new byte[64];
         if (!Secp256K1.RecoverableSignatureSerializeCompact(serialized, out var recId, sig))
         {
             throw new Exception("Cannot serialize recoverable signature: how did it happen?");
         }
         recId = ChainId(useNewChainId) * 2 + 35 + recId;
         var recIdBytes = new byte[useNewChainId ? 2 : 1];
         var fullBin = recId.ToBytes().ToArray();
         if (useNewChainId)
         {
             recIdBytes[0] = fullBin[1];
             recIdBytes[1] = fullBin[0];
         }
         else
         {
             recIdBytes[0] = fullBin[0];
         }
         return serialized.Concat(recIdBytes).ToArray();
     }));
 }