Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update the magic docs to highlight signer creation is async #170

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions site/smart-accounts/signers/magic.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ import {
getDefaultLightAccountFactory,
} from "@alchemy/aa-accounts";
import { sepolia } from "viem/chains";
import { magicSigner } from "./magic";
import { createMagicSigner } from "./magic";

const chain = sepolia;
// NOTE: the below is async because it depends on creating a magic signer. You can choose to break that up how you want
// eg. use a useEffect + useState to create the signer and then pass it down to the provider
const provider = new AlchemyProvider({
apiKey: "ALCHEMY_API_KEY",
chain,
Expand All @@ -64,7 +66,7 @@ const provider = new AlchemyProvider({
new LightSmartContractAccount({
entryPointAddress: "0x...",
chain: rpcClient.chain,
owner: magicSigner,
owner: await createMagicSigner(),
factoryAddress: getDefaultLightAccountFactory(chain),
rpcClient,
})
Expand Down
27 changes: 15 additions & 12 deletions site/snippets/magic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WalletClientSigner, type SmartAccountSigner } from "@alchemy/aa-core";
import { WalletClientSigner } from "@alchemy/aa-core";
import { Magic } from "magic-sdk";
import { createWalletClient, custom } from "viem";

Expand All @@ -8,15 +8,18 @@ const MAGIC_API_KEY = "pk_test_...";
// instantiate Magic SDK instance
export const magic = new Magic(MAGIC_API_KEY);

// a viem wallet client that wraps magic for utility methods
// NOTE: this isn't necessary since you can just use the `magic.rpcProvider`
// directly, but this makes things much easier
export const magicClient = createWalletClient({
transport: custom(magic.rpcProvider),
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one doesn't work? did not look into magic code, but why is getting rpc provider async here?
hmm, and just using rpcProvider field directly seems to work fine on examples I tried.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea I'm not sure why this didn't work for a customer... maybe it was because you have to authenticate with a user first?

lemme take another pass at this and make it even more clear that authentication is required first

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the unauthed case .rpcProvider was returning undefined

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I didn't need to do this if I mapped the magicClient to any for the new WalletClientSigner below:

https://github.com/alchemyplatform/aa-simple-dapp/blob/a71b0b78/src/hooks/useMagicSigner.ts#L17

// NOTE: because this is async, you will need to put this in a useEffect hook if using React
export const createMagicSigner = async () => {
// 1. Authenticate the user (for other methods see magic docs https://magic.link/docs/dedicated/overview)
await magic.wallet.connectWithUI();

// a smart account signer you can use as an owner on ISmartContractAccount
export const magicSigner: SmartAccountSigner = new WalletClientSigner(
magicClient,
"magic" // signerType
);
// 2. create a wallet client
const magicClient = createWalletClient({
transport: custom(await magic.wallet.getProvider()),
});

return new WalletClientSigner(
magicClient,
"magic" // signerType
);
};
Loading