-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useSignMessage): add useSignMessage hook to sign message (#119)
* feat(useSignMessage): add useSignMessage hook to sign message * pr comments * pr comments * pr comments * pr comments
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useState } from "react"; | ||
import { useOrdContext } from "../providers/OrdContext.tsx"; | ||
import signMessage from "../lib/signMessage.ts"; | ||
|
||
export function useSignMessage(): { | ||
isLoading: boolean; | ||
signMsg: (address: string, message: string) => Promise<string>; | ||
error: string; | ||
} { | ||
const { network, wallet, publicKey, format } = useOrdContext(); | ||
const [error, setError] = useState<string | null>(null); | ||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
|
||
const signMsg = async (address: string, message: string) => { | ||
setIsLoading(true); | ||
try { | ||
setError(null); | ||
if (!format || !publicKey) { | ||
throw new Error("No wallet is connected"); | ||
} | ||
|
||
const signedMessage = await signMessage({ | ||
address, | ||
wallet, | ||
message, | ||
network, | ||
}); | ||
|
||
setIsLoading(false); | ||
return signedMessage; | ||
} catch (e) { | ||
setError(e.message); | ||
setIsLoading(false); | ||
throw new Error(e); | ||
} | ||
}; | ||
|
||
return { signMsg, error, isLoading }; | ||
} |
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,36 @@ | ||
import { ordit } from "@sadoprotocol/ordit-sdk"; | ||
import { Network, Wallet } from "../providers/OrdContext.tsx"; | ||
|
||
interface SignMessageParams { | ||
message: string; | ||
wallet: Wallet; | ||
address: string; | ||
network: Network; | ||
} | ||
|
||
// returns based64 signature | ||
export default async function signMessage({ | ||
message, | ||
wallet, | ||
address, | ||
network, | ||
}: SignMessageParams): Promise<string> { | ||
if (wallet === Wallet.UNISAT) { | ||
const signedMessage = await ordit.unisat.signMessage(message); | ||
return signedMessage.base64; | ||
} | ||
|
||
if (wallet === Wallet.XVERSE) { | ||
// Todo: remove any type fixes in ordit-sdk is done | ||
const signedMessage: any = await ordit.xverse.signMessage({ | ||
address, | ||
network, | ||
message, | ||
}); | ||
|
||
return signedMessage.signature; | ||
} | ||
|
||
// else throw error | ||
throw new Error("Invalid wallet selected"); | ||
} |
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