Skip to content

Commit

Permalink
feat: Provider-less signer (#36)
Browse files Browse the repository at this point in the history
* feat: Provider-less signer

* chore: request and provider chain id check

* v5.4.0
  • Loading branch information
opatavi authored Sep 27, 2023
1 parent b3a620c commit fc61f21
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 127 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gelatonetwork/relay-sdk",
"version": "5.3.0",
"version": "5.4.0",
"description": "SDK to integrate with Gelato Relay",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
28 changes: 14 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ethers } from "ethers";

import * as library from "./lib";
import { CallWithSyncFeeRequest } from "./lib/callWithSyncFee/types";
import { SponsoredCallRequest } from "./lib/sponsoredCall/types";
Expand All @@ -18,6 +16,7 @@ import {
Config,
RelayRequestOptions,
RelayResponse,
SignerOrProvider,
} from "./lib/types";
import {
GELATO_RELAY_1BALANCE_ERC2771_ADDRESS,
Expand Down Expand Up @@ -46,6 +45,7 @@ export {
CallWithSyncFeeConcurrentERC2771Request,
CallWithConcurrentERC2771Request,
Config,
SignerOrProvider,
};
export class GelatoRelay {
#config: Config;
Expand Down Expand Up @@ -108,7 +108,7 @@ export class GelatoRelay {

/**
* @param {CallWithSyncFeeERC2771Request | CallWithSyncFeeConcurrentERC2771Request} request - Call with sync fee: Sequential ERC2771 or Concurrent ERC2771 request to be relayed by Gelato Executors
* @param {ethers.BrowserProvider | ethers.Wallet} walletOrProvider - BrowserProvider [front-end] or Wallet [back-end] to sign the payload
* @param {SignerOrProvider} signerOrProvider - BrowserProvider [front-end] or Signer [back-end] to sign the payload
* @param {RelayRequestOptions} [options] - Optional Relay configuration
* @param {string} [sponsorApiKey] Optional Sponsor API key to be used for the call
* @returns {Promise<RelayResponse>} Response object with taskId parameter
Expand All @@ -118,14 +118,14 @@ export class GelatoRelay {
request:
| CallWithSyncFeeERC2771Request
| CallWithSyncFeeConcurrentERC2771Request,
walletOrProvider: ethers.BrowserProvider | ethers.Wallet,
signerOrProvider: SignerOrProvider,
options?: RelayRequestOptions,
sponsorApiKey?: string
): Promise<RelayResponse> =>
library.relayWithCallWithSyncFeeERC2771(
{
request,
walletOrProvider,
signerOrProvider,
sponsorApiKey,
options,
},
Expand All @@ -151,22 +151,22 @@ export class GelatoRelay {

/**
* @param {CallWithERC2771Request | CallWithConcurrentERC2771Request} request - Sponsored: Sequential ERC2771 or Concurrent ERC2771 request to be relayed by Gelato Executors
* @param {ethers.BrowserProvider | ethers.Wallet} walletOrProvider - BrowserProvider [front-end] or Wallet [back-end] to sign the payload
* @param {SignerOrProvider} signerOrProvider - BrowserProvider [front-end] or Signer [back-end] to sign the payload
* @param {string} sponsorApiKey - Sponsor API key
* @param {RelayRequestOptions} [options] - Optional Relay configuration
* @returns {Promise<RelayResponse>} Response object with taskId parameter
*
*/
sponsoredCallERC2771 = (
request: CallWithERC2771Request | CallWithConcurrentERC2771Request,
walletOrProvider: ethers.BrowserProvider | ethers.Wallet,
signerOrProvider: SignerOrProvider,
sponsorApiKey: string,
options?: RelayRequestOptions
): Promise<RelayResponse> =>
library.relayWithSponsoredCallERC2771(
{
request,
walletOrProvider,
signerOrProvider,
sponsorApiKey,
options,
},
Expand All @@ -175,35 +175,35 @@ export class GelatoRelay {

/**
* @param {CallWithERC2771Request | CallWithConcurrentERC2771Request} request - Sequential ERC2771 or Concurrent ERC2771 request to be relayed by Gelato Executors
* @param {ethers.BrowserProvider | ethers.Wallet} walletOrProvider - BrowserProvider [front-end] or Wallet [back-end] to sign the payload
* @param {SignerOrProvider} signerOrProvider - BrowserProvider [front-end] or Signer [back-end] to sign the payload
* @param {ERC2771Type} type - ERC2771Type.CallWithSyncFee or ERC2771Type.SponsoredCall
* @returns {Promise<SignatureData>} Response object with struct and signature
*
*/
getSignatureDataERC2771 = (
request: CallWithERC2771Request | CallWithConcurrentERC2771Request,
walletOrProvider: ethers.BrowserProvider | ethers.Wallet,
signerOrProvider: SignerOrProvider,
type: ERC2771Type
): Promise<SignatureData> =>
library.getSignatureDataERC2771(
{ request, walletOrProvider, type },
{ request, signerOrProvider, type },
this.#config
);

/**
* @param {CallWithERC2771Request | CallWithConcurrentERC2771Request} request - Sequential ERC2771 or Concurrent ERC2771 request to be relayed by Gelato Executors
* @param {ethers.BrowserProvider | ethers.Wallet} [walletOrProvider] - Optional BrowserProvider [front-end] or Wallet [back-end] to sign the payload
* @param {SignerOrProvider} [signerOrProvider] - Optional BrowserProvider [front-end] or Signer [back-end] to sign the payload
* @param {ERC2771Type} type - ERC2771Type.CallWithSyncFee or ERC2771Type.SponsoredCall
* @returns {Promise<PayloadToSign>} Response object with struct and typed data
*
*/
getDataToSignERC2771 = (
request: CallWithERC2771Request | CallWithConcurrentERC2771Request,
type: ERC2771Type,
walletOrProvider?: ethers.BrowserProvider | ethers.Wallet
signerOrProvider?: SignerOrProvider
): Promise<PayloadToSign> =>
library.getDataToSignERC2771(
{ request, walletOrProvider, type },
{ request, signerOrProvider, type },
this.#config
);

Expand Down
14 changes: 5 additions & 9 deletions src/lib/erc2771/callWithSyncFeeERC2771/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ethers } from "ethers";

import { post } from "../../../utils";
import {
ApiKey,
Expand All @@ -10,6 +8,7 @@ import {
RelayCall,
RelayRequestOptions,
RelayResponse,
SignerOrProvider,
} from "../../types";
import {
CallWithConcurrentERC2771Struct,
Expand All @@ -27,25 +26,22 @@ export const relayWithCallWithSyncFeeERC2771 = async (
request:
| CallWithSyncFeeERC2771Request
| CallWithSyncFeeConcurrentERC2771Request;
walletOrProvider: ethers.BrowserProvider | ethers.Wallet;
signerOrProvider: SignerOrProvider;
sponsorApiKey?: string;
options?: RelayRequestOptions;
},
config: Config
): Promise<RelayResponse> => {
try {
const { request, walletOrProvider, options, sponsorApiKey } = payload;
if (!walletOrProvider.provider) {
throw new Error(`Missing provider`);
}
const { request, signerOrProvider, options, sponsorApiKey } = payload;

if (request.isConcurrent) {
const isConcurrent = true;
const { isRelayContext, feeToken } = request;
const type = ERC2771Type.ConcurrentCallWithSyncFee;

const { struct, signature } = await getSignatureDataERC2771(
{ request, walletOrProvider, type },
{ request, signerOrProvider, type },
config
);

Expand Down Expand Up @@ -81,7 +77,7 @@ export const relayWithCallWithSyncFeeERC2771 = async (
const type = ERC2771Type.CallWithSyncFee;

const { struct, signature } = await getSignatureDataERC2771(
{ request, walletOrProvider, type },
{ request, signerOrProvider, type },
config
);

Expand Down
29 changes: 9 additions & 20 deletions src/lib/erc2771/getDataToSignERC2771/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ethers } from "ethers";

import { getProviderChainId, isConcurrentRequest } from "../../../utils";
import { isConcurrentRequest } from "../../../utils";
import { isNetworkSupported } from "../../network";
import { Config } from "../../types";
import { Config, SignerOrProvider } from "../../types";
import {
CallWithERC2771Request,
ERC2771Type,
Expand All @@ -17,7 +15,7 @@ export async function getDataToSignERC2771(
payload: {
request: CallWithERC2771Request;
type: ERC2771Type.CallWithSyncFee | ERC2771Type.SponsoredCall;
walletOrProvider?: ethers.BrowserProvider | ethers.Wallet;
signerOrProvider?: SignerOrProvider;
},
config: Config
): Promise<SequentialPayloadToSign>;
Expand All @@ -28,7 +26,7 @@ export async function getDataToSignERC2771(
type:
| ERC2771Type.ConcurrentCallWithSyncFee
| ERC2771Type.ConcurrentSponsoredCall;
walletOrProvider?: ethers.BrowserProvider | ethers.Wallet;
signerOrProvider?: SignerOrProvider;
},
config: Config
): Promise<ConcurrentPayloadToSign>;
Expand All @@ -37,7 +35,7 @@ export async function getDataToSignERC2771(
payload: {
request: CallWithERC2771Request | CallWithConcurrentERC2771Request;
type: ERC2771Type;
walletOrProvider?: ethers.BrowserProvider | ethers.Wallet;
signerOrProvider?: SignerOrProvider;
},
config: Config
): Promise<PayloadToSign>;
Expand All @@ -46,28 +44,19 @@ export async function getDataToSignERC2771(
payload: {
request: CallWithERC2771Request | CallWithConcurrentERC2771Request;
type: ERC2771Type;
walletOrProvider?: ethers.BrowserProvider | ethers.Wallet;
signerOrProvider?: SignerOrProvider;
},
config: Config
): Promise<PayloadToSign> {
try {
const { request, walletOrProvider } = payload;
const { request, signerOrProvider } = payload;

const { chainId } = request;
const isSupported = await isNetworkSupported({ chainId }, config);
if (!isSupported) {
throw new Error(`Chain id [${chainId.toString()}] is not supported`);
}

if (walletOrProvider) {
const providerChainId = await getProviderChainId(walletOrProvider);
if (chainId !== providerChainId) {
throw new Error(
`Request and provider chain id mismatch. Request: [${chainId.toString()}], provider: [${providerChainId.toString()}]`
);
}
}

if (isConcurrentRequest(request)) {
const type = payload.type as
| ERC2771Type.ConcurrentCallWithSyncFee
Expand All @@ -77,7 +66,7 @@ export async function getDataToSignERC2771(
{
request,
type,
walletOrProvider,
signerOrProvider,
},
config
);
Expand All @@ -92,7 +81,7 @@ export async function getDataToSignERC2771(
| ERC2771Type.SponsoredCall;

const { struct, typedData } = await populatePayloadToSign(
{ request, type, walletOrProvider },
{ request, type, signerOrProvider },
config
);

Expand Down
25 changes: 10 additions & 15 deletions src/lib/erc2771/getSignatureDataERC2771/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { ethers } from "ethers";

import { isConcurrentRequest, signTypedDataV4 } from "../../../utils";
import { Config } from "../../types";
import { Config, SignerOrProvider } from "../../types";
import {
SignatureData,
CallWithERC2771Request,
Expand All @@ -15,7 +13,7 @@ import { getDataToSignERC2771 } from "../getDataToSignERC2771/index.js";
export async function getSignatureDataERC2771(
payload: {
request: CallWithERC2771Request;
walletOrProvider: ethers.BrowserProvider | ethers.Wallet;
signerOrProvider: SignerOrProvider;
type: ERC2771Type.CallWithSyncFee | ERC2771Type.SponsoredCall;
},
config: Config
Expand All @@ -24,7 +22,7 @@ export async function getSignatureDataERC2771(
export async function getSignatureDataERC2771(
payload: {
request: CallWithConcurrentERC2771Request;
walletOrProvider: ethers.BrowserProvider | ethers.Wallet;
signerOrProvider: SignerOrProvider;
type:
| ERC2771Type.ConcurrentCallWithSyncFee
| ERC2771Type.ConcurrentSponsoredCall;
Expand All @@ -35,7 +33,7 @@ export async function getSignatureDataERC2771(
export async function getSignatureDataERC2771(
payload: {
request: CallWithERC2771Request | CallWithConcurrentERC2771Request;
walletOrProvider: ethers.BrowserProvider | ethers.Wallet;
signerOrProvider: SignerOrProvider;
type: ERC2771Type;
},
config: Config
Expand All @@ -44,16 +42,13 @@ export async function getSignatureDataERC2771(
export async function getSignatureDataERC2771(
payload: {
request: CallWithERC2771Request | CallWithConcurrentERC2771Request;
walletOrProvider: ethers.BrowserProvider | ethers.Wallet;
signerOrProvider: SignerOrProvider;
type: ERC2771Type;
},
config: Config
): Promise<SignatureData> {
try {
const { request, walletOrProvider } = payload;
if (!walletOrProvider.provider) {
throw new Error(`Missing provider`);
}
const { request, signerOrProvider } = payload;

if (isConcurrentRequest(request)) {
const type = payload.type as
Expand All @@ -63,14 +58,14 @@ export async function getSignatureDataERC2771(
const { struct, typedData } = await getDataToSignERC2771(
{
request,
walletOrProvider,
signerOrProvider,
type,
},
config
);

const signature = await signTypedDataV4(
walletOrProvider,
signerOrProvider,
request.user as string,
typedData
);
Expand All @@ -87,14 +82,14 @@ export async function getSignatureDataERC2771(
const { struct, typedData } = await getDataToSignERC2771(
{
request,
walletOrProvider,
signerOrProvider,
type,
},
config
);

const signature = await signTypedDataV4(
walletOrProvider,
signerOrProvider,
request.user as string,
typedData
);
Expand Down
Loading

0 comments on commit fc61f21

Please sign in to comment.