-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): Add support for Magic Eden Wallet (#123)
* Extract sats-connect wallet methods from xverse wallet * Update lockfile * Add support for MagicEden wallet * Minor fixes to address PR comments * Remove unnecessary line * Add Magic Eden selected wallet check * Minor fixes for PR comments * Add support for MagicEden wallet * Add Magic Eden selected wallet check * Minor fixes for PR comments * Change all MagicEden to Magic Eden * Change all MagicEden to Magic Eden * refactor(magiceden): remove export * fix(lint): ci --------- Co-authored-by: Nanosync <[email protected]>
- Loading branch information
Showing
5 changed files
with
146 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./dist/magiceden"; |
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,103 @@ | ||
import { Psbt } from "bitcoinjs-lib"; | ||
import { BitcoinProvider } from "sats-connect"; | ||
|
||
import { BrowserWalletNetwork } from "../../config/types"; | ||
import { BrowserWalletNotInstalledError, OrditSDKError } from "../../errors"; | ||
import { | ||
satsConnectWalletGetAddresses, | ||
satsConnectWalletSignMessage, | ||
satsConnectWalletSignPsbt, | ||
} from "../internal/sats-connect"; | ||
import type { SatsConnectSignPSBTOptions } from "../internal/sats-connect/types"; | ||
import { BrowserWalletSignResponse, WalletAddress } from "../types"; | ||
|
||
export interface MagicEdenBitcoinProvider extends BitcoinProvider { | ||
isMagicEden: boolean | undefined; | ||
} | ||
|
||
export interface MagicEdenWindow extends Window { | ||
BitcoinProvider?: MagicEdenBitcoinProvider; | ||
} | ||
|
||
/** | ||
* Checks if the MagicEden Wallet extension is installed. | ||
* | ||
* @returns `true` if installed, `false` otherwise. | ||
* @throws {OrditSDKError} Function is called outside a browser without `window` object | ||
*/ | ||
function isInstalled(): boolean { | ||
if (typeof window === "undefined") { | ||
throw new OrditSDKError("Cannot call this function outside a browser"); | ||
} | ||
|
||
return ( | ||
typeof (window as MagicEdenWindow).BitcoinProvider?.isMagicEden !== | ||
"undefined" | ||
); | ||
} | ||
|
||
async function getMagicEdenWalletProvider(): Promise<BitcoinProvider> { | ||
if (!isInstalled()) { | ||
throw new BrowserWalletNotInstalledError( | ||
"Magic Eden not installed or set as prioritised wallet.", | ||
); | ||
} | ||
|
||
return window.BitcoinProvider!; | ||
} | ||
|
||
async function getAddresses( | ||
network: BrowserWalletNetwork = "mainnet", | ||
): Promise<WalletAddress[]> { | ||
if (!isInstalled()) { | ||
throw new BrowserWalletNotInstalledError( | ||
"Magic Eden not installed or set as prioritised wallet.", | ||
); | ||
} | ||
|
||
return satsConnectWalletGetAddresses(getMagicEdenWalletProvider, network); | ||
} | ||
|
||
async function signPsbt( | ||
psbt: Psbt, | ||
{ | ||
finalize = true, | ||
extractTx = true, | ||
network, | ||
inputsToSign, | ||
}: SatsConnectSignPSBTOptions = { network: "mainnet", inputsToSign: [] }, | ||
): Promise<BrowserWalletSignResponse> { | ||
if (!isInstalled()) { | ||
throw new BrowserWalletNotInstalledError( | ||
"Magic Eden not installed or set as prioritised wallet.", | ||
); | ||
} | ||
|
||
return satsConnectWalletSignPsbt(getMagicEdenWalletProvider, psbt, { | ||
finalize, | ||
extractTx, | ||
network, | ||
inputsToSign, | ||
}); | ||
} | ||
|
||
async function signMessage( | ||
message: string, | ||
address: string, | ||
network: BrowserWalletNetwork = "mainnet", | ||
): Promise<BrowserWalletSignResponse> { | ||
if (!isInstalled()) { | ||
throw new BrowserWalletNotInstalledError( | ||
"Magic Eden not installed or set as prioritised wallet.", | ||
); | ||
} | ||
|
||
return satsConnectWalletSignMessage( | ||
getMagicEdenWalletProvider, | ||
message, | ||
address, | ||
network, | ||
); | ||
} | ||
|
||
export { getAddresses, isInstalled, signMessage, signPsbt }; |
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