Giter Site home page Giter Site logo

djytwy / blockus-non-custodial-payment Goto Github PK

View Code? Open in Web Editor NEW

This project forked from blockus0/listing-non-custodial-payment

0.0 0.0 0.0 447 KB

Lightweight library for signing ERC-2612 signatures.

Home Page: https://www.npmjs.com/package/eth-permit

JavaScript 27.71% TypeScript 72.29%

blockus-non-custodial-payment's Introduction

Blockus Non-custodial payments.

Usage

This is meant to be soon an SDK, and you will be able to import the needed functions. In the mean time code can be copied or used are reference. Important code is within the index.ts and gelato.ts files.

We are going to sign two type data objects, one is a USDC permit and the second is a meta transaction that will execute the payment. We first call the get payment intent from blockus API, this will have everything we need to construct they type data objects to be sign.

const provider = new ethers.providers.JsonRpcProvider(process.env.ALCHEMY_RPC, 137);
const signer = new ethers.Wallet(privateKey, provider);
const buyersAddress = await wallet.getAddress();

// ID of the listing the user is trying to buy
const listingId = 'arqB2clzH46oehy59eXIc4PNHFKA';

// GETS PAYMENT INTENTION FROM BLOCKUS
// https://api.blockus.net/api-docs/swagger/index.html#/Marketplace%20listings/getPaymentIntent
const intent = await blockus.getPaymentIntent(listingId);

// Creates permit type data
const permitTypeData = await getSignERC20Permit(
    buyersAddress,
    paymentIntent,
    signer
);

// Get permit signature 
const permitSignature = await signer._signTypedData(
    permitTypeData.domain,
    permitTypeData.types,
    permitTypeData.value,
);      

// Constructs payment transaction
const paymentMetaTransaction = await buildPaymentTransaction(
    permitSignature,
    paymentIntent,
    signer
);

// Sign meta transaction for token distribution.
const distributeTokenSignature = await signer._signTypedData(
    paymentMetaTransaction.domain,
    paymentMetaTransaction.types,
    paymentMetaTransaction.value,
);

// Meta transaction deadline
const metaTxDeadline = paymentMetaTransaction.value.userDeadline;

// Execute listing
const executeBody = {
    "paymentWalletChain": "polygon",
    "paymentWalletAddress": buyersAddress,
    "paymentTxSignature": distributeTokenSignature,
    "permitTxSignature": permitSignature,
    "metaTransactionDeadline": metaTxDeadline
}

// https://api.blockus.net/api-docs/swagger/index.html#/Marketplace%20listings/executeListing
axios.post(`${blockusEndpoint}/v1/marketplace/listings/${listingId}/execute`, executeBody)
  .then((response) => {
    console.log('Response:', response.data);
  });

Function Descriptions

getSignERC20Permit:

This function is responsible for generating permit data required for ERC-2612 (permit) token transactions. It prepares the necessary information for a user to delegate token transfers to another entity (blockus distribution contract). This signature grants the blockus distribution contract permission to spend the users fund.

const permitTypeData = await getSignERC20Permit(
    buyersAddress,
    paymentIntent,
    signer
)

const permitSignature = await signer._signTypedData(
    permitTypeData.domain,
    permitTypeData.types,
    permitTypeData.value,
);

buildPaymentTransaction:

This function constructs the payment meta transaction to be sign by the buyer. A meta transaction is a transaction that's signed off-chain and then relayed to the blockchain by a third party. In this context, buildPaymentTransaction assembles the data needed to execute a payment and be sign, including details such as the permit signature (from getSignERC20Permit) and the intent of the payment. This meta transaction allows for decentralized applications to interact with users without requiring them to directly cover the transaction fees in Ether.

const paymentMetaTransaction:EIP712<IGelatoStruct> = await buildPaymentTransaction(
    permitSignature, // Permit type data signature 
    paymentIntent, 
    signer
);

Ethers JS.

EtherJS v6 is the only dependency needed, it is used to instantiate Signers, provider objects and generate the required signatures. In the context of the web browser metamask will be both signer and provider.

// NodeJS
const provider = new ethers.providers.JsonRpcProvider(process.env.ALCHEMY_RPC, 137);
const wallet = new ethers.Wallet(privateKey, provider);
const buyersAddress = await wallet.getAddress();

Signatures will prompt users to sign through a pop up when the signer is metamask or another web base wallet. Objects being sign are Typed structured park of the EIP-712.

const signature = await signer._signTypedData(
    typeData.domain,
    typeData.types,
    typeData.value,
);

Further Reading

https://eips.ethereum.org/EIPS/eip-712

https://eips.ethereum.org/EIPS/eip-2612

blockus-non-custodial-payment's People

Contributors

scammi avatar dmihal avatar michael-blockus avatar lumyo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.