Skip to content

Commit

Permalink
feat: sdk connector interface improvements, and more (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackHamer09 authored Nov 27, 2024
1 parent f111997 commit 42733c9
Show file tree
Hide file tree
Showing 22 changed files with 952 additions and 229 deletions.
40 changes: 17 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ npm i zksync-sso
Add ZKsync SSO connector to your app (using `wagmi`):

```ts
import { zksyncSsoConnector, callPolicy } from "zksync-sso/connector";
import { zksyncSepoliaTestnet } from "viem/chains";
import { createConfig, connect } from "@wagmi/core";
import { zksyncSsoConnector } from "zksync-sso/connector";
import { erc20Abi } from "viem";

const ssoConnector = zksyncSsoConnector({
// Optional session configuration, if omitted user will have to sign every transaction via Auth Server
session: {
expiry: "1 day",

// Allow up to 0.1 ETH to be spend in gas fees
feeLimit: parseEther("0.1"),

Expand All @@ -51,41 +54,32 @@ const ssoConnector = zksyncSsoConnector({
to: "0x188bd99cd7D4d78d4E605Aeea12C17B32CC3135A",
valueLimit: parseEther("0.1"),
},

// Allow ETH transfers to specific address with a limit of 0.1 ETH per hour
// until the session expires
{
to: "0x188bd99cd7D4d78d4E605Aeea12C17B32CC3135A",
valueLimit: {
limit: parseEther("0.1"),
period: BigInt(60 * 60), // 1 hour in seconds
},
},
],

// Allow calling specific smart contracts (e.g. ERC20 transfer):
contractCalls: [
{
callPolicy({
address: "0xa1cf087DB965Ab02Fb3CFaCe1f5c63935815f044",
function: "transfer(address,uint256)",

// Optional call constraints (unconstrained otherwise):
abi: erc20Abi,
functionName: "transfer",
constraints: [

// Only allow transfers to this address
// Only allow transfers to this address. Or any address if omitted
{
index: 0,
condition: "Equal",
refValue: pad("0x6cC8cf7f6b488C58AA909B77E6e65c631c204784", { size: 32 }),
index: 0, // First argument of erc20 transfer function, recipient address
value: "0x6cC8cf7f6b488C58AA909B77E6e65c631c204784",
},

// Transfer up to 0.2 tokens
// Allow transfering up to 0.2 tokens per hour
// until the session expires
{
index: 1,
limit: parseUnits("0.2", TOKEN.decimals), // Unlimited if omitted
limit: {
limit: parseUnits("0.2", TOKEN.decimals),
period: "1 hour",
},
},
],
},
}),
],
},
});
Expand Down
40 changes: 36 additions & 4 deletions docs/sdk/client-auth-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,53 @@ your application. It's built on top of [client SDK](../client/README.md) and
## Basic usage

```ts
import { zksync } from "viem/chains";
import { zksyncSsoConnector, callPolicy } from "zksync-sso/connector";
import { zksyncSepoliaTestnet } from "viem/chains";
import { createConfig, connect } from "@wagmi/core";
import { zksyncSsoConnector } from "zksync-sso/connector";
import { erc20Abi } from "viem";

const ssoConnector = zksyncSsoConnector({
// Optional session configuration
// if omitted user will have to sign every transaction via Auth Server
session: {
expiry: "1 day",

// Allow up to 0.1 ETH to be spend in gas fees
feeLimit: parseEther("0.1"),
// Allow transfers to a specific address with a limit of 0.1 ETH

transfers: [
// Allow ETH transfers of up to 0.1 ETH to specific address
{
to: "0x188bd99cd7D4d78d4E605Aeea12C17B32CC3135A",
valueLimit: parseEther("0.1"),
},
],

// Allow calling specific smart contracts (e.g. ERC20 transfer):
contractCalls: [
callPolicy({
address: "0xa1cf087DB965Ab02Fb3CFaCe1f5c63935815f044",
abi: erc20Abi,
functionName: "transfer",
constraints: [
// Only allow transfers to this address. Or any address if omitted
{
index: 0, // First argument of erc20 transfer function, recipient address
value: "0x6cC8cf7f6b488C58AA909B77E6e65c631c204784",
},

// Allow transfering up to 0.2 tokens per hour
// until the session expires
{
index: 1,
limit: {
limit: parseUnits("0.2", TOKEN.decimals),
period: "1 hour",
},
},
],
}),
],
},
});

Expand All @@ -33,7 +65,7 @@ const wagmiConfig = createConfig({
const connectWithSSO = () => {
connect(wagmiConfig, {
connector: ssoConnector,
chainId: zksync.id, // or another chain id that has SSO support
chainId: zksyncSepoliaTestnet.id, // or another chain id that has SSO support
});
};
```
Loading

0 comments on commit 42733c9

Please sign in to comment.