Skip to content

Commit a5ee9af

Browse files
authored
fix(magic): remove non-type Magic.link imports (#136)
* fix(magic): remove non-type Magic.link imports The library should not import any SDKs/dependencies for wallet providers (besides type imports). This should be handled by the consuming application on an opt-in basis, depending on which wallets it supports. The Magic Algorand extension is now (optionally) passed to the `useInitializeProviders` hook's configuration object. Closes: Build fails if @magic-ext/algorand is not installed #135 * docs: update README full config example
1 parent 0c0613c commit a5ee9af

File tree

6 files changed

+60
-19
lines changed

6 files changed

+60
-19
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,9 @@ useEffect(() => {
504504
- Website - https://lute.app/
505505
- Install dependency - `npm install lute-connect`
506506
507-
508507
### Kibisis Wallet
509508
510-
***NOTE:** Kibisis is in active development and currently only supports betanet and testnet.*
509+
**\*NOTE:** Kibisis is in active development and currently only supports betanet and testnet.\*
511510
512511
- Website - https://kibis.is
513512
- Download - https://kibis.is/#download
@@ -660,7 +659,8 @@ import { PeraWalletConnect } from '@perawallet/connect'
660659
import { DaffiWalletConnect } from '@daffiwallet/connect'
661660
import { WalletConnectModalSign } from '@walletconnect/modal-sign-html'
662661
import LuteConnect from 'lute-connect'
663-
import { Magic } from 'magic-sdk';
662+
import { Magic } from 'magic-sdk'
663+
import { AlgorandExtension } from '@magic-ext/algorand'
664664

665665
export default function App() {
666666
const providers = useInitializeProviders({
@@ -694,9 +694,10 @@ export default function App() {
694694
},
695695
{
696696
id: PROVIDER_ID.MAGIC,
697-
getDynamicClient: Magic,
698-
clientOptions: { apiKey: 'API_KEY' },
699-
},
697+
clientStatic: Magic,
698+
extensionStatic: AlgorandExtension,
699+
clientOptions: { apiKey: '<API_KEY>' }
700+
}
700701
],
701702
nodeConfig: {
702703
network: 'mainnet',

src/clients/magic/client.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import BaseClient from '../base'
77
import { DEFAULT_NETWORK, PROVIDER_ID } from '../../constants'
88
import { debugLog } from '../../utils/debugLog'
99
import { ICON } from './constants'
10+
import type { AlgorandExtension } from '@magic-ext/algorand'
11+
import type { SDKBase, InstanceWithExtensions } from '@magic-sdk/provider'
1012
import type { DecodedSignedTransaction, DecodedTransaction, Network } from '../../types/node'
1113
import type { InitParams } from '../../types/providers'
1214
import type { Wallet } from '../../types/wallet'
1315
import type { MagicAuthTransaction, MagicAuthConstructor, MagicAuthConnectOptions } from './types'
14-
import { AlgorandExtension } from '@magic-ext/algorand'
15-
import { SDKBase, InstanceWithExtensions } from '@magic-sdk/provider'
1616

1717
class MagicAuth extends BaseClient {
1818
#client: InstanceWithExtensions<
@@ -51,6 +51,8 @@ class MagicAuth extends BaseClient {
5151
algodOptions,
5252
clientStatic,
5353
getDynamicClient,
54+
extensionStatic,
55+
getDynamicExtension,
5456
algosdkStatic,
5557
network = DEFAULT_NETWORK
5658
}: InitParams<PROVIDER_ID.MAGIC>): Promise<BaseClient | null> {
@@ -62,20 +64,33 @@ class MagicAuth extends BaseClient {
6264
Magic = clientStatic
6365
} else if (getDynamicClient) {
6466
Magic = await getDynamicClient()
65-
} else if (!clientOptions || !clientOptions.apiKey) {
67+
} else {
6668
throw new Error(
67-
'Magic provider missing API Key to be passed by required property: clientOptions'
69+
'Magic provider missing required property: clientStatic or getDynamicClient'
6870
)
71+
}
72+
73+
let AlgorandExtension
74+
if (extensionStatic) {
75+
AlgorandExtension = extensionStatic
76+
} else if (getDynamicExtension) {
77+
AlgorandExtension = await getDynamicExtension()
6978
} else {
7079
throw new Error(
71-
'Magic provider missing required property: clientStatic or getDynamicClient'
80+
'Magic provider missing required property: extensionStatic or getDynamicExtension'
81+
)
82+
}
83+
84+
if (!clientOptions || !clientOptions.apiKey) {
85+
throw new Error(
86+
'Magic provider missing API Key to be passed by required property: clientOptions'
7287
)
7388
}
7489

7590
const algosdk = algosdkStatic || (await Algod.init(algodOptions)).algosdk
7691
const algodClient = getAlgodClient(algosdk, algodOptions)
7792

78-
const magic = new Magic(clientOptions?.apiKey as string, {
93+
const magic = new Magic(clientOptions?.apiKey, {
7994
extensions: {
8095
algorand: new AlgorandExtension({
8196
rpcUrl: ''

src/clients/magic/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import type { AlgorandExtension } from '@magic-ext/algorand'
2+
import type { SDKBase, InstanceWithExtensions } from '@magic-sdk/provider'
13
import type algosdk from 'algosdk'
24
import type { Network } from '../../types/node'
35
import type { Metadata } from '../../types/wallet'
4-
import { SDKBase, InstanceWithExtensions } from '@magic-sdk/provider'
5-
import { AlgorandExtension } from '@magic-ext/algorand'
66

77
export type MagicAuthConnectOptions = {
88
apiKey: string

src/components/Example/Example.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import algosdk from 'algosdk'
12
import React from 'react'
23
import { DeflyWalletConnect } from '@blockshake/defly-connect'
34
import { DaffiWalletConnect } from '@daffiwallet/connect'
@@ -6,10 +7,10 @@ import { WalletProvider, PROVIDER_ID, useInitializeProviders, Network } from '..
67
import Account from './Account'
78
import Connect from './Connect'
89
import Transact from './Transact'
9-
import algosdk from 'algosdk'
1010
import { ManualGoalSigningAlertPromptProvider } from './TestManualProvider'
1111

1212
const DUMMY_MAGIC_PK = 'pk_live_D17FD8D89621B5F3'
13+
1314
const getDynamicPeraWalletConnect = async () => {
1415
const PeraWalletConnect = (await import('@perawallet/connect')).PeraWalletConnect
1516
return PeraWalletConnect
@@ -20,6 +21,11 @@ const getDynamicMagic = async () => {
2021
return Magic
2122
}
2223

24+
const getDynamicAlgoExtension = async () => {
25+
const AlgorandExtension = (await import('@magic-ext/algorand')).AlgorandExtension
26+
return AlgorandExtension
27+
}
28+
2329
export default function ConnectWallet() {
2430
const walletProviders = useInitializeProviders({
2531
providers: [
@@ -31,6 +37,7 @@ export default function ConnectWallet() {
3137
{
3238
id: PROVIDER_ID.MAGIC,
3339
getDynamicClient: getDynamicMagic,
40+
getDynamicExtension: getDynamicAlgoExtension,
3441
clientOptions: { apiKey: DUMMY_MAGIC_PK }
3542
},
3643
{

src/testUtils/mockClients.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { DaffiWalletConnect } from '@daffiwallet/connect'
44
import { PeraWalletConnect } from '@perawallet/connect'
55
import MyAlgoConnect from '@randlabs/myalgo-connect'
66
import LuteConnect from 'lute-connect'
7+
import { Magic } from 'magic-sdk'
8+
import { AlgorandExtension } from '@magic-ext/algorand'
79
import { WalletConnectModalSign } from '@walletconnect/modal-sign-html'
810
import algosdk from 'algosdk'
911
import AlgoSignerClient from '../clients/algosigner/client'
@@ -23,8 +25,6 @@ import type { Exodus } from '../clients/exodus/types'
2325
import type { Account, ClientOptions } from '../types'
2426
import CustomWalletClient from '../clients/custom/client'
2527
import MagicAuthClient from '../clients/magic/client'
26-
import { AlgorandExtension } from '@magic-ext/algorand'
27-
import { Magic } from 'magic-sdk'
2828

2929
type ClientTypeMap = {
3030
[PROVIDER_ID.ALGOSIGNER]: AlgoSignerClient

src/types/providers.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type { PeraWalletConnect } from '@perawallet/connect'
33
import type { DeflyWalletConnect } from '@blockshake/defly-connect'
44
import type { DaffiWalletConnect } from '@daffiwallet/connect'
55
import type LuteConnect from 'lute-connect'
6+
import type { Magic } from 'magic-sdk'
7+
import type { AlgorandExtension } from '@magic-ext/algorand'
68
import type MyAlgoConnect from '@randlabs/myalgo-connect'
79
import type {
810
WalletConnectModalSign,
@@ -20,7 +22,6 @@ import type { DaffiWalletConnectOptions } from '../clients/daffi/types'
2022
import type { NonEmptyArray } from './utilities'
2123
import type BaseClient from '../clients/base'
2224
import type { CustomOptions } from '../clients/custom/types'
23-
import { Magic } from 'magic-sdk'
2425

2526
export type ProviderConfigMapping = {
2627
[PROVIDER_ID.PERA]: {
@@ -87,6 +88,8 @@ export type ProviderConfigMapping = {
8788
clientOptions?: { apiKey: string }
8889
clientStatic?: undefined
8990
getDynamicClient?: () => Promise<typeof Magic>
91+
extensionStatic?: typeof AlgorandExtension
92+
getDynamicExtension?: () => Promise<typeof AlgorandExtension>
9093
}
9194
}
9295

@@ -132,10 +135,25 @@ type DynamicClient<T> = {
132135

133136
type OneOfStaticOrDynamicClient<T> = StaticClient<T> | DynamicClient<T>
134137

138+
type StaticAlgoExtension<T> = {
139+
extensionStatic: T
140+
getDynamicExtension?: undefined
141+
}
142+
143+
type DynamicAlgoExtension<T> = {
144+
extensionStatic?: undefined
145+
getDynamicExtension: () => Promise<T>
146+
}
147+
148+
type OneOfStaticOrDynamicAlgoExtension<T> = StaticAlgoExtension<T> | DynamicAlgoExtension<T>
149+
135150
type ProviderDef =
136151
| (ProviderConfig<PROVIDER_ID.PERA> & OneOfStaticOrDynamicClient<typeof PeraWalletConnect>)
137152
| (ProviderConfig<PROVIDER_ID.MAGIC> &
138-
OneOfStaticOrDynamicClient<typeof Magic> & { clientOptions: { apiKey: string } })
153+
OneOfStaticOrDynamicClient<typeof Magic> &
154+
OneOfStaticOrDynamicAlgoExtension<typeof AlgorandExtension> & {
155+
clientOptions: { apiKey: string }
156+
})
139157
| (ProviderConfig<PROVIDER_ID.DEFLY> & OneOfStaticOrDynamicClient<typeof DeflyWalletConnect>)
140158
| (ProviderConfig<PROVIDER_ID.DAFFI> & OneOfStaticOrDynamicClient<typeof DaffiWalletConnect>)
141159
| (ProviderConfig<PROVIDER_ID.LUTE> &

0 commit comments

Comments
 (0)