Skip to content

OneNov0209/venn-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

Venn Project

Deskripsi

Proyek ini mengintegrasikan Venn Firewall ke dalam smart contract di jaringan Holesky.


Instalasi

1. Clone repository

git clone https://github.com/OneNov0209/venn-project.git
cd venn-project

2. Install dependensi

npm install

3. Buat file .env dan isi dengan informasi berikut

VENN_NODE_URL=https://signer2.testnet.venn.build/api/17000/sign
VENN_POLICY_ADDRESS=0xYOUR_POLICY_ADDRESS
RPC_URL=https://holesky.infura.io/v3/YOUR_INFURA_ID
PRIVATE_KEY=YOUR_PRIVATE_KEY
CONTRACT_ADDRESS=0xYOUR_CONTRACT_ADDRESS

Membuat Struktur Proyek

Jalankan perintah berikut untuk membuat struktur proyek:

mkdir contracts scripts
touch contracts/MyContract.sol scripts/deploy.js hardhat.config.js .env index.js

Menulis Smart Contract

Buka file contracts/MyContract.sol dan isi dengan kode berikut:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@ironblocks/firewall-consumer/contracts/consumers/VennFirewallConsumer.sol";

contract MyContract is VennFirewallConsumer {
    constructor(address firewall) {
        _initializeFirewall(firewall); // Pastikan metode ini benar
    }

    function protectedFunction() external firewallProtected {
        // Fungsi ini hanya bisa diakses dengan persetujuan Venn
    }
}

Konfigurasi Hardhat

Buka hardhat.config.js dan tambahkan konfigurasi berikut:

require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();

module.exports = {
  solidity: "0.8.19",
  networks: {
    holesky: {
      url: process.env.RPC_URL,
      accounts: [process.env.PRIVATE_KEY],
    }
  }
};

Membuat Skrip Deploy

Buka scripts/deploy.js dan tambahkan kode berikut:

const hre = require("hardhat");

async function main() {
    const [deployer] = await hre.ethers.getSigners();
    console.log("Deploying contract with the account:", deployer.address);

    // Policy Address dari Venn
    const policyAddress = process.env.VENN_POLICY_ADDRESS;

    const MyContract = await hre.ethers.getContractFactory("MyContract");
    const myContract = await MyContract.deploy(policyAddress);

    console.log("Waiting for deployment...");
    await myContract.waitForDeployment();

    console.log("MyContract deployed to:", await myContract.getAddress());
}

main().catch((error) => {
    console.error(error);
    process.exitCode = 1;
});

Deploy Smart Contract

1. Bersihkan & Compile ulang kode smart contract

npx hardhat clean
npx hardhat compile

Jika muncul error modifier invocation saat kompilasi, pastikan Anda menggunakan _initializeFirewall(firewall); di dalam kontrak.

2. Deploy ke jaringan Holesky

npx hardhat run scripts/deploy.js --network holesky

Output jika berhasil:

MyContract deployed to: 0xABCDEF123456789...

Tambahkan alamat kontrak ini ke dalam file .env:

CONTRACT_ADDRESS=0xABCDEF123456789...

Enable Venn Firewall

Setelah kontrak berhasil didaftarkan, jalankan:

venn enable

Tambahkan policyAddress ke .env:

VENN_POLICY_ADDRESS=0x123456789ABCDEF...

Menjalankan Transaksi melalui Venn

Buka index.js dan isi dengan kode berikut:

require("dotenv").config();
const { VennClient } = require("@vennbuild/venn-dapp-sdk");
const { ethers } = require("ethers");

console.log("� Memulai Venn DApp SDK...");

const vennURL = process.env.VENN_NODE_URL;
const vennPolicyAddress = process.env.VENN_POLICY_ADDRESS;
const contractAddress = process.env.CONTRACT_ADDRESS;

console.log("✅ VENN_NODE_URL:", vennURL);
console.log("✅ VENN_POLICY_ADDRESS:", vennPolicyAddress);
console.log("✅ CONTRACT_ADDRESS:", contractAddress);

if (!vennURL || !vennPolicyAddress || !contractAddress) {
    throw new Error("� Environment variables tidak lengkap!");
}

const vennClient = new VennClient({ vennURL, vennPolicyAddress });

async function sendTransaction() {
    console.log("🚀 Mempersiapkan transaksi...");
    const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
    console.log("✅ Provider Ethereum berhasil diinisialisasi.");

    const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
    console.log("✅ Wallet berhasil dimuat dengan alamat:", wallet.address);

    const contract = new ethers.Contract(contractAddress, [
        "function protectedFunction() external"
    ], wallet);

    console.log("🔄 Meminta persetujuan transaksi melalui Venn...");
    const txData = contract.interface.encodeFunctionData("protectedFunction");

    const approvedTx = await vennClient.approve({
        from: wallet.address,
        to: contractAddress,
        data: txData,
        value: "0"
    });

    console.log("✅ Transaksi disetujui oleh Venn, mengirim transaksi...");
    const tx = await wallet.sendTransaction(approvedTx);
    console.log("✅ Transaksi berhasil dikirim, hash:", tx.hash);
    
    await tx.wait();
    console.log("🎉 Transaksi berhasil dikonfirmasi!");
}

sendTransaction().catch((error) => {
    console.error("� Terjadi kesalahan:", error);
});

Jalankan transaksi

node index.js

Output jika berhasil:

✅ Transaksi berhasil dikirim, hash: 0xe186f5fc...
✅ Transaksi berhasil dikonfirmasi!

Menguji Kontrak di Hardhat Console

Untuk memastikan fungsi protectedFunction berjalan dengan baik:

npx hardhat --network holesky console

Kemudian di dalam console Node.js:

const contract = await ethers.getContractAt("MyContract", process.env.CONTRACT_ADDRESS);
await contract.protectedFunction();

Troubleshooting

Jika terjadi error seperti berikut:

  1. execution reverted: ApprovedCallsPolicy: call hashes empty
    ✅ Pastikan kontrak telah terhubung dengan Venn Firewall menggunakan venn enable.

  2. Address: low-level delegate call failed
    ✅ Periksa apakah policy address telah dikonfigurasi dengan benar di dalam .env.

  3. unknown function
    ✅ Pastikan ABI pada index.js sesuai dengan definisi smart contract terbaru.


Author

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published