public static void DoubleSpendAttack_SouldHaveValidTransferChain() { //Arrange var goofy = new Goofy(); var attacker = new Signature(256); Global.GoofyPk = goofy.PublicKey; var trans1 = goofy.CreateCoin(attacker.PublicKey); //Action var sgndTrans1 = attacker.SignMessage(trans1); var destiny1 = new Person(); var transInfo1 = new TransferInfo(sgndTrans1, destiny1.PublicKey); var trans2 = trans1.PayTo(transInfo1); var destiny2 = new Person(); var transInfo2 = new TransferInfo(sgndTrans1, destiny2.PublicKey); var trans3 = trans1.PayTo(transInfo2); //Assert try { //!previousTransSignedByMe.isValidSignedMsg(previous); if ((trans2.isValidSignedMsg(trans2[trans1])) && (trans3.isValidSignedMsg(trans3[trans1]))) throw new Exception("Its not allowed to double spend the same coin."); } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// Attacker change a transfer in the middle of the chain and make the chain invalid /// </summary> public static void ChengeTransfer_SouldNotAffectTransferChain() { //Arrange var goofy = new Goofy(); var changer = new Signature(256); var person1 = new Person(); var person2 = new Person(); var trans1 = goofy.CreateCoin(changer.PublicKey); var changerSgndTrans = changer.SignMessage(trans1); var transInfo = new TransferInfo(changerSgndTrans, person1.PublicKey); var changerTransfer = trans1.PayTo(transInfo); person1.AddTransfer(changerTransfer); var tran3 = person1.PayTo(person2.PublicKey); //Act changerTransfer.Hash = null; changerTransfer.Info.DestinyPk = null; //Assert try { person2.CheckTransfers(tran3); } catch { Console.WriteLine("Transfer chain is broked because someone change a another transfer in the middle."); } }
static void Main(string[] args) { var goofy = new Goofy(); var alice = new Person(); var bob = new Person(); var clark = new Person(); //goofy Transfer var goofyTrans = goofy.CreateCoin(alice.PublicKey); //alice Transfer alice.AddTransfer(goofyTrans); var aliceTrans = alice.PayTo(bob.PublicKey); //bob Transfer bob.AddTransfer(aliceTrans); var bobTrans = bob.PayTo(clark.PublicKey); //clark Transfer clark.AddTransfer(bobTrans); Tests.GoofyCreateAndTansferCoin_SouldHaveValidCoin(); Tests.ReceivingAndMaekingTransfer_SouldHaveValidTransfer(); Tests.ReceivingAndMaekingManyTransfer_SouldHaveValidTransferChain(); Tests.ChengeTransfer_SouldNotAffectTransferChain(); Tests.DoubleSpendAttack_SouldHaveValidTransferChain(); //byte[] publickey; //byte[] data; //byte[] dataHash; //byte[] signature; //byte[] signature2; //using (var dsa = new ECDsaCng(256)) //{ // dsa.HashAlgorithm = CngAlgorithm.Sha256; // publickey = dsa.Key.Export(CngKeyBlobFormat.EccPublicBlob); // data = new byte[] { 21, 5, 8, 12, 207 }; // dataHash = new SHA1Managed().ComputeHash(data); // //dataHash = new SHA256Cng().ComputeHash(data); // signature = dsa.SignData(data); // signature2 = dsa.SignHash(dataHash); //} //Console.WriteLine(signature.Length); //Console.WriteLine(Convert.ToBase64String(signature)); //using (var dsa = new ECDsaCng(CngKey.Import(publickey, CngKeyBlobFormat.EccPublicBlob))) //{ // dsa.HashAlgorithm = CngAlgorithm.Sha256; // if (dsa.VerifyData(data, signature)) // Console.WriteLine("Data is good"); // else // Console.WriteLine("Data is bad"); // if (dsa.VerifyHash(dataHash, signature2)) // Console.WriteLine("Data is good"); // else // Console.WriteLine("Data is bad"); //} //Console.ReadKey(); //String a = "Hello wrold!"; //Object b = a; //byte[] c; //BinaryFormatter bf = new BinaryFormatter(); //using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) //{ // bf.Serialize(ms, b); // c = ms.ToArray(); //} //String d; //using (var memStream = new System.IO.MemoryStream()) //{ // var binForm = new BinaryFormatter(); // memStream.Write(c, 0, c.Length); // memStream.Seek(0, System.IO.SeekOrigin.Begin); // var obj = binForm.Deserialize(memStream); // d = (String) obj; //} Console.ReadKey(); //Org.BouncyCastle.Asn1.X9.X9ECParameters ecp = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1"); //ECDomainParameters params = new ECDomainParameters(ecp.Curve, ecp.G, ecp.N, ecp.H); //ECPublicKeySpec pubKeySpec = new ECPublicKeySpec( //ecp.curve.decodePoint(Hex.decode("045894609CCECF9A92533F630DE713A958E96C97CCB8F5ABB5A688A238DEED6DC2D9D0C94EBFB7D526BA6A61764175B99CB6011E2047F9F067293F57F5")), // Q //params); //PublicKey pubKey = f.generatePublic(pubKeySpec); //var signer = SignerUtilities.GetSigner("ECDSA"); // possibly similar to SHA-1withECDSA //signer.Init(false, pubKey); //signer.BlockUpdate(plainTextAsBytes, 0, plainTextAsBytes.Length); //return signer.VerifySignature(signature); }
public static void ReceivingAndMaekingTransfer_SouldHaveValidTransfer() { //Arrange var goofy = new Goofy(); var person1 = new Signature(256); var trans1 = goofy.CreateCoin(person1.PublicKey); //Action var sgndTrans1 = person1.SignMessage(trans1); var destiny = new Person(); var transInfo = new TransferInfo(sgndTrans1, destiny.PublicKey); var trans2 = trans1.PayTo(transInfo); //Assert try { if (!trans2.isChainNotNull()) throw new Exception("Transfer chain must be informed."); if (!trans2.isInfoNotNull()) throw new Exception("Transfer informations must be informed"); if (!trans2.Info.isPrepreviousTransSignedByMeNotNull()) throw new Exception("The signed previous transfer must be informed"); if (!trans2.Info.isDestinyPkNotNull()) throw new Exception("The destiny public key must b informed"); if (!trans2.isHashNotNull()) throw new Exception("The hash of the transfer must be informed."); if (!trans2.isValidHash()) throw new Exception("The hash of this transfer is invalid."); //previous.receiverPk != previousTransSignedByMe.PublicKey; if (!trans2.isSignerPreviousTransactoin(trans2[trans1].Info.DestinyPk)) throw new Exception("The transaction dosen't belong to the owner"); //!previousTransSignedByMe.isValidSignedMsg(previous); if (!trans2.isValidSignedMsg(trans2[trans1])) throw new Exception("The previous transaction and his signature dont match"); //checking all those validations above and the last goofytransfer trans2.CheckTransfer(); } catch (Exception e) { Console.WriteLine(e.Message); } }
public static void ReceivingAndMaekingManyTransfer_SouldHaveValidTransferChain() { //Arrange var goofy = new Goofy(); var person1 = new Person(); var person2 = new Person(); Global.GoofyPk = goofy.PublicKey; var trans1 = goofy.CreateCoin(person1.PublicKey); person1.AddTransfer(trans1); //Action var trans2 = person1.PayTo(person2.PublicKey); //Assert try { //testing the for loop checkTransfer person2.CheckTransfers(trans2); person2.AddTransfer(trans2); } catch (Exception e) { Console.WriteLine(e.Message); } }