public void Sign(byte[] privateKey, Dictionary <string, Transaction> prevTxs) { if (IsCoinBase()) { return; } foreach (var inp in Inputs) { var hex = HexadecimalEncoding.ToHexString(inp.Id); if (prevTxs[hex]?.Id == null) { Console.WriteLine("prev transaction does not exist"); } } var txCopy = TransactionTrimmed(this); var index = 0; foreach (var inp in txCopy.Inputs) { var prevTx = prevTxs[HexadecimalEncoding.ToHexString(inp.Id)]; txCopy.Inputs[index].Signature = null; txCopy.Inputs[index].PubKey = prevTx.Outputs[inp.Out].PublicKeyHash; txCopy.Id = txCopy.CalculateHash(); txCopy.Inputs[index].PubKey = null; var signature = CryptoFinal.SignTransaction(txCopy.Id, privateKey); Inputs[index].Signature = signature; index++; } }
//makes sure, that wallet is valid, its public and private keys are ok public bool IsValid() { var testMessage = "this is my test message to be signed"; var signature = CryptoFinal.SignTransaction(ByteHelper.GetBytesFromString(testMessage), PrivateKey); var isOk = CryptoFinal.VerifyHashed(signature, PublicKey, ByteHelper.GetBytesFromString(testMessage)); return(isOk); }
public WalletCore(string hashedPassword) { PrivateKey = CryptoFinal.GeneratePrivateKey(); PublicKey = CryptoFinal.GetPublicKey(PrivateKey); PublicKeyHash = PublicKeyHashed(PublicKey); PasswordHash = hashedPassword; var version = new byte[] { 0x00 }; var versionHashed = ArrayHelpers.ConcatArrays(version, PublicKeyHash); var address = Base58Encoding.EncodeWithCheckSum(versionHashed); Address = address; }
public bool Verify(Dictionary <string, Transaction> prevTxs) { if (IsCoinBase()) { return(true); } foreach (var inp in Inputs) { var hex = HexadecimalEncoding.ToHexString(inp.Id); if (prevTxs[hex]?.Id == null) { Console.WriteLine("prev transaction does not exist"); } } var txCopy = TransactionTrimmed(this); var index = 0; foreach (var inp in Inputs) { var prevTx = prevTxs[HexadecimalEncoding.ToHexString(inp.Id)]; txCopy.Inputs[index].Signature = null; txCopy.Inputs[index].PubKey = prevTx.Outputs[inp.Out].PublicKeyHash; txCopy.Id = txCopy.CalculateHash(); txCopy.Inputs[index].PubKey = null; var(r, s, v) = CryptoFinal.GetRSV(inp.Signature); var recoveredKey = CryptoFinal.RecoverPublicKey(r.ToByteArray(), s.ToByteArray(), v.ToByteArray(), txCopy.Id); if (!CryptoFinal.VerifyHashed(inp.Signature, recoveredKey, txCopy.Id)) { Console.WriteLine("verify failed"); return(false); } index++; } //Console.WriteLine("verify ok"); return(true); }