-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb462d9
commit 33141b0
Showing
6 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
outline: deep | ||
head: | ||
- - meta | ||
- property: og:title | ||
content: Dfns | ||
- - meta | ||
- name: description | ||
content: Guide for using Dfns as a Signer | ||
- - meta | ||
- property: og:description | ||
content: Guide for using Dfns as a Signer | ||
--- | ||
|
||
# Dfns Integration Guide | ||
|
||
[Dfns](https://www.dfns.co) is an MPC/TSS Wallet-as-a-Service API/SDK provider. Dfns aims to optimize the balance of security and UX by deploying key shares into a decentralized network on the backend while enabling wallet access via biometric open standards on the frontend like Webauthn. Reach out [here](https://www.dfns.co/learn-more) to set up a sandbox environment to get started. | ||
|
||
Dfns seamlessly integrates with Account Abstraction by signing `UserOperation`s. See the examples below for initializing a DFNS signer and creating a provider with that signer. You can follow [this](https://accountkit.alchemy.com/tutorials/sponsoring-gas/sponsoring-gas.html) guide to send and sponsor `UserOperation`s with the provider created. | ||
|
||
Dfns created a full example of a gas-less transaction via a paymaster [in our SDK](https://github.com/dfnsext/typescript-sdk/blob/m/examples/viem/alchemy-aa-gasless/README.md), adapted from our [gas sponsorship example](https://accountkit.alchemy.com/guides/sponsoring-gas.html). | ||
|
||
## Install Dfns SDK | ||
|
||
::: code-group | ||
|
||
```bash [npm] | ||
npm i @dfns/lib-viem @dfns/sdk @dfns/sdk-keysigner | ||
``` | ||
|
||
```bash [yarn] | ||
yarn add @dfns/lib-viem @dfns/sdk @dfns/sdk-keysigner | ||
``` | ||
|
||
::: | ||
|
||
### Create a SmartAccountSigner | ||
|
||
Setup the Dfns Web3 Provider and wrap it in an `AlchemyProvider`. | ||
|
||
<<< @/snippets/dfns.ts | ||
|
||
### Use it with Light Account | ||
|
||
::: code-group | ||
|
||
```ts [example.ts] | ||
import { AlchemyProvider } from "@alchemy/aa-alchemy"; | ||
import { | ||
LightSmartContractAccount, | ||
getDefaultLightAccountFactoryAddress, | ||
} from "@alchemy/aa-accounts"; | ||
import { encodeFunctionData, parseAbi } from "viem"; | ||
import { sepolia } from "viem/chains"; | ||
import { createDfnsSigner } from "./dfns"; | ||
|
||
// Remember to replace "ALCHEMY_API_KEY" with your own Alchemy API key, get one here: https://dashboard.alchemy.com/ | ||
const ALCHEMY_API_KEY = "API_KEY"; | ||
const chain = sepolia; | ||
|
||
const createAlchemyProvider = async (): Promise<AlchemyProvider> => { | ||
return new AlchemyProvider({ | ||
apiKey: ALCHEMY_API_KEY, | ||
chain, | ||
}).connect( | ||
(rpcClient) => | ||
new LightSmartContractAccount({ | ||
chain, | ||
owner: await createDfnsSigner(), | ||
factoryAddress: getDefaultLightAccountFactoryAddress(chain), | ||
rpcClient, | ||
}) | ||
); | ||
}; | ||
``` | ||
|
||
<<< @/snippets/dfns.ts | ||
|
||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { LocalAccountSigner, type SmartAccountSigner } from "@alchemy/aa-core"; | ||
import { DfnsWallet } from "@dfns/lib-viem"; | ||
import { DfnsApiClient } from "@dfns/sdk"; | ||
import { AsymmetricKeySigner } from "@dfns/sdk-keysigner"; | ||
import { LocalAccount, toAccount } from "viem/accounts"; | ||
|
||
// See the Dfns example https://github.com/dfnsext/typescript-sdk/blob/m/examples/viem/alchemy-aa-gasless/README.md for details on populating the environment variables. | ||
const DFNS_PRIVATE_KEY = null; | ||
const DFNS_CRED_ID = null; | ||
const DFNS_APP_ORIGIN = null; | ||
const DFNS_APP_ID = null; | ||
const DFNS_AUTH_TOKEN = null; | ||
const DFNS_API_URL = null; | ||
const SEPOLIA_WALLET_ID = null; | ||
|
||
const initDfnsWallet = (walletId: string) => { | ||
const signer = new AsymmetricKeySigner({ | ||
privateKey: DFNS_PRIVATE_KEY!, | ||
credId: DFNS_CRED_ID!, | ||
appOrigin: DFNS_APP_ORIGIN!, | ||
}); | ||
|
||
const dfnsClient = new DfnsApiClient({ | ||
appId: DFNS_APP_ID!, | ||
authToken: DFNS_AUTH_TOKEN!, | ||
baseUrl: DFNS_API_URL!, | ||
signer, | ||
}); | ||
|
||
return DfnsWallet.init({ | ||
walletId, | ||
dfnsClient, | ||
maxRetries: 10, | ||
}); | ||
}; | ||
|
||
export const createDfnsSigner = async (): Promise<SmartAccountSigner> => { | ||
const sepoliaWallet = await initDfnsWallet(SEPOLIA_WALLET_ID!); | ||
const account = toAccount(sepoliaWallet) as LocalAccount; | ||
const dfnsSigner = new LocalAccountSigner(account as any); | ||
return dfnsSigner; | ||
}; |