Skip to content

Commit

Permalink
feat(useSignMessage): add useSignMessage hook to sign message (#119)
Browse files Browse the repository at this point in the history
* feat(useSignMessage): add useSignMessage hook to sign message

* pr comments

* pr comments

* pr comments

* pr comments
  • Loading branch information
veralygit authored Nov 1, 2023
1 parent 8eb09ab commit 760a793
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/ord-connect/src/hooks/useSignMessage.tsx
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 };
}
36 changes: 36 additions & 0 deletions packages/ord-connect/src/lib/signMessage.ts
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");
}
15 changes: 15 additions & 0 deletions packages/ord-connect/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { useSend } from "./hooks/useSend";
import { OrdConnectKit, useSign } from "./index";
import { OrdConnectProvider, useOrdContext } from "./providers/OrdContext.tsx";
import "./style.css";
import { useSignMessage } from "./hooks/useSignMessage.tsx";

function SampleComponent() {
const [send, error, loading] = useSend();
const [getBalance] = useBalance();
const [sign] = useSign();
const { signMsg } = useSignMessage();
const [result, setResult] = React.useState("");
const [balance, setBalance] = React.useState(0);

Expand Down Expand Up @@ -61,6 +63,19 @@ function SampleComponent() {
>
Sign PSBT
</button>

<button
type="button"
onClick={async () => {
const signed = await signMsg(
address.ordinals,
"Authenticate this message to access all the functionalities of Ordzaar. By using Ordzaar implies your consent to our user agreement.\n\nDomain: ordzaar.com\n\nBlockchain: Bitcoin \n\nAccount:\nmj4Bo243eA2MdLo1dcd7xPDjqjccEVzxby\n\nNonce: F5wt4JUVc3",
);
console.log(signed);
}}
>
Sign message
</button>
</div>
);
}
Expand Down

0 comments on commit 760a793

Please sign in to comment.