diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..c74c07d --- /dev/null +++ b/config/config.json @@ -0,0 +1,6 @@ +{ + "ethereum": { + "rpcUrl": "https://sepolia.infura.io/v3/f3bd3bda446041ff9eea71b40e6072e3", + "privateKey": "03d46f7c5b0ad0cbf4d0bcbcf035b6bd91d95c2f0f09c3e6c387bf24bebccf3e" + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..a74bb06 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "name": "verification-layer-tester", + "version": "1.0.0", + "type": "module", + "dependencies": { + "ethers": "^5.8.0" + } +} diff --git a/src/cli.js b/src/cli.js new file mode 100644 index 0000000..8ee21b6 --- /dev/null +++ b/src/cli.js @@ -0,0 +1,21 @@ +// src/cli.js + +import testDoubleSpending from "./tests/security/double-spending.js"; +import config from "../config/config.js"; + +// Kullanıcıdan gelen komutu al +const command = process.argv[2]; // process.argv[2] ile terminalde girilen komutu alırız + +if (command === "test-double-spending") { + try { + const result = await testDoubleSpending( + config.ethereum.rpcUrl, + config.ethereum.privateKey + ); + console.log("Double-spending test sonucu:", result); + } catch (error) { + console.error("Test sırasında bir hata oluştu:", error.message); + } +} else { + console.log("Geçersiz komut. Kullanım: node src/cli.js test-double-spending"); +} diff --git a/src/tests/security/double-spending.js b/src/tests/security/double-spending.js new file mode 100644 index 0000000..1260971 --- /dev/null +++ b/src/tests/security/double-spending.js @@ -0,0 +1,54 @@ +// src/tests/security/double-spending.js + +import { ethers } from "ethers"; + +/** + * Double-spending testi yapar. + * @param {string} providerUrl - Ethereum RPC URL'si. + * @param {string} privateKey - Test hesabı private key'i. + */ +async function testDoubleSpending(providerUrl, privateKey) { + // Ethereum sağlayıcısını oluştur + const provider = new ethers.providers.JsonRpcProvider(providerUrl); + + // Cüzdan oluştur + const wallet = new ethers.Wallet(privateKey, provider); + + console.log(`Cüzdan adresi: ${wallet.address}`); + + // Mevcut nonce değerini al + const nonce = await provider.getTransactionCount(wallet.address, "latest"); + console.log(`Mevcut nonce değeri: ${nonce}`); + + // İlk işlemi gönder + const tx1 = await wallet.sendTransaction({ + to: "0x95a35a95E839D6164E390a41deAD4300eda8271b", // Gerçek bir Ethereum adresi girin + value: ethers.utils.parseEther("0.0002"), // Gönderilecek miktar + nonce: nonce, // Aynı nonce değeri + }); + + console.log(`İlk işlem hash'i: ${tx1.hash}`); + + // İlk işlemin bloğa dahil olmasını bekle + console.log("İlk işlemin onaylanması bekleniyor..."); + await provider.waitForTransaction(tx1.hash); + console.log("İlk işlem onaylandı."); + + // İkinci işlemi gönder (aynı nonce ile) + try { + const tx2 = await wallet.sendTransaction({ + to: "0x82C25Ee97B0C4F3bCF316baa888d91A884cC2fe8", // Başka bir Ethereum adresi girin + value: ethers.utils.parseEther("0.0002"), // Gönderilecek miktar + nonce: nonce, // Aynı nonce değeri + }); + + console.log(`İkinci işlem hash'i: ${tx2.hash}`); + console.log("Test Başarısız: İkinci işlem de gönderildi."); + return "Vulnerable"; + } catch (error) { + console.log("Test Başarılı: İkinci işlem reddedildi."); + return "Secure"; + } +} + +export default testDoubleSpending;