Multi-signature (multi-sig) wallets provide an added layer of security and decentralization by requiring multiple approvals for transactions. Gnosis Safe is a widely used and trusted solution for implementing multi-sig wallets on EVM-compatible blockchains like the peaq network. This guide walks you through creating a multi-sig wallet programmatically using Gnosis Safe contracts and demonstrates some basic functions such as adding owners and executing transactions.

Prerequisites

  • Basic knowledge of blockchain development and JavaScript/TypeScript.
  • Familiar with interacting with smart contracts using ethers.js.
  • Have access to a deployed instance of Gnosis Safe contracts on the peaq network or are able to deploy them yourself.
  • Have an RPC URL for the peaq network.
  • Have private keys for multiple signers to interact with the wallet.

Instructions

1. Set Up Your Environment

  • Install the necessary dependencies:
npm install ethers @gnosis.pm/safe-core-sdk @gnosis.pm/safe-ethers-lib dotenv
  • Set ESM Module: Add the following to your package.json to alllow for ESM modules.
"type": "module",
  • Configure a .env file with the following variables:
RPC_URL=https://peaq.api.onfinality.io/public
PRIVATE_KEY=your_private_key_here

2. Connect to the Peaq Network

Import dependencies and initialize the provider and signer:

import { ethers } from 'ethers';
import Safe from '@gnosis.pm/safe-core-sdk';
import SafeEthersLib from '@gnosis.pm/safe-ethers-lib';
import dotenv from 'dotenv';

dotenv.config();

const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider);

3. Deploy a New Multi-Sig Wallet

Use the Gnosis Safe SDK to create a new multi-sig wallet:

const createSafe = async () => {
    const safeFactory = await Safe.SafeFactory.create({
        ethAdapter: new SafeEthersLib.EthersAdapter({
            ethers,
            signer
        }),
    });

    const owners = [
        '0xAddress1',
        '0xAddress2',
        '0xAddress3'
    ];
    const threshold = 2; // Number of approvals required
    const safeAccountConfig = { owners, threshold };

    const safe = await safeFactory.deploySafe({ safeAccountConfig });
    console.log(`Multi-sig wallet deployed at: ${safe.getAddress()}`);
    return safe;
};

createSafe();

4. Add an Owner

Propose a transaction to add a new owner:

const addOwner = async (safeAddress, newOwner, safe) => {
    const safeTransaction = await safe.createAddOwnerTx({
        ownerAddress: newOwner,
        threshold: 2,
    });

    const txHash = await safe.executeTransaction(safeTransaction);
    console.log(`Transaction hash: ${txHash}`);
};

addOwner(safe.getAddress(), '0xNewOwnerAddress', safe);

5. Execute a Transaction

Propose and execute a basic transaction (e.g., sending PEAQ):

const executeTransaction = async (safeAddress, recipient, amount) => {
    const safe = await Safe.default.create({
        ethAdapter: new SafeEthersLib.EthersAdapter({ ethers, signer }),
        safeAddress,
    });

    const safeTransactionData = {
        to: recipient,
        value: ethers.parseEther(amount), // Amount in PEAQ
        data: '0x',
    };

    const safeTransaction = await safe.createTransaction({ safeTransactionData });
    const txHash = await safe.executeTransaction(safeTransaction);
    console.log(`Transaction executed. Hash: ${txHash}`);
};

executeTransaction(safe.getAddress(), '0xRecipientAddress', '0.1');

Summary

Using Gnosis Safe contracts on the peaq network allows you to leverage the security of multi-signature wallets for DePIN projects. This guide demonstrated how to programmatically create a wallet, manage owners, and execute transactions. With these basics, you can secure your assets and operations on the peaq network with confidence. Expand further by integrating Gnosis Safe with front-end interfaces or automated scripts for enhanced usability and functionality.