Integrate Directly in Smart Contracts
This is a somewhat antiquated page. Please reach out to us.
It is recommended developers familiarize themselves with how the Peanut Protocol works prior to attempting direct contract interactions.
The Peanut Smart Contracts are designed for sending non-front-runnable link payments, which can be in the form of various assets. Including ETH, ERC-20 tokens, ERC-721 tokens, ERC-1155 tokens, or combinations thereof!
Contracts use asymmetric ECDSA encryption to verify claim permissions and prevent front running.
Creating a Link
To create a Link there are a few steps required.
Generate the seed and derive asymmetric key pair from ID.
Call the smart contract to deposit funds.
Get index from the event log emitted by deposit call.
Step 1: Generating Keys
Each Peanut Link is protected by a key. The key should be securely created for each Link using secure random data generation features of the platform used.
Whilst anything can be used as a key, the following should be avoided:
Short seeds less than 16 characters
Words or common patterns (e.g. aaaaaa or 123456)
The SDK uses Ethers to generate the ECDSA key from a seed. Any equivalent function in different languages should also be able to do the same.
It's worth checking that the output from any other crypto library matches the output of the generateKeysFromString
function within util.js
.
The address and private key from this generation should be stored for later use in the next steps.
Step 2: Calling the Smart Contract
The makeDeposit
function allows users to create a Link from a deposit. It handles various token types (ETH, ERC-20, ERC-721, ERC-1155, etc) and stores deposit information in the deposits
array.
This function should be called with the correct parameters for the type of crypto being sent along with the last 20 bytes of the public key generated in the previous step.
Finding the latest address of the contract on the chain in question can be done by looking at the registry file in the github repo. Please note this same smart contract will need to be called in order to claim the funds.
The following table lists all parameter types for the makeDeposit
function.
Step 3: Creating the Link
Once the transaction calls makeDeposit
successfully, it will emit a log confirming the deposit and index slot where the funds are held.
For example, in this deposit the event log 104 contains information about the funds stored and what storage index they occupy.
From the event log, we can see the deposit was made to index 0 for an ERC20 token.
All the information supplied in the event log will help us construct the Peanut Link. Remember the expected format:
With the information from the previous steps, we can append the information and send this link to a user.
Alternatively, we can simply store at minimum the following information to be able to claim in the future:
Chain and Contract Address
Deposit Index
Key used to generate ECDSA Key Pair
Claiming a Link
When claiming a Link, the following information needs to be provided to the contract:
Index slot of the deposit
Destination address to send funds
Hash and signature of destination address signed with the key pair used when depositing
The order of these parameters are clearly identified in the withdrawDeposit contract function signature.
The hashed recipient address is expected to be encoded according to EIP191 with the appropriate prefix. As an example, the SDK uses the following function to generate the hash of the address.
Then the signature is generated by signing the the hashed address using the private key generated prior when creating the ECDSA key pair from the random seed. For testing, make sure your hash and signature values match the following when using the test address 0xF4CF81931bb32E41CfdB24Ba10492C019A5ec5f9.
When a Link has been successfully claimed, a WithdrawEvent
event will be emitted from the contract call. The WithdrawEvent
event will confirm the transfer alongside the funds that are sent to the recipient address.
Withdrawing a Link
Not to be confused with withdrawing funds from a Link (i.e. claiming a link). This process allows the original depositor / creator of the Link to retract the funds and cancel.
Any Link can be cancelled after 24 hours of creation by the original address that created it
To cancel and reclaim the funds for a Link that have not been claimed, call the function withdrawDepositSender
with the index slot parameter.
Last updated