Skip to content

decrypt

Usage

connect() must be called and a connection must be initiated before account information will be returned.

The decrypt() function returns the currently selected account at the time of connection. When your account change in the wallet, the account change will not be reflected in the dApp. If you want the account state to automatically be reflected in the dApp, you should use the useAccount hook.

import { decrypt } from '@puzzlehq/sdk-core';
import { useState } from 'react';
export const Page = () => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | undefined>(undefined);
const [plaintexts, setPlaintexts] = useState<string[] | undefined>(undefined);
const [ciphertextToDecrypt, setCiphertextToDecrypt] = useState('');
const onClick = async () => {
setLoading(true);
setError(undefined);
try {
const response: DecryptResponse = await decrypt({
ciphertexts: [ciphertextToDecrypt] // we can decrypt an array of ciphertexts
})
setPlaintexts(response.plaintexts);
} catch (e) {
setError((e as Error).message);
} finally {
setLoading(false);
}
}
return (
<div>
<input
placeholder='enter text to decrypt'
value={textToDecrypt}
onChange={(e) => setTextToDecrypt(e.target.value)}
>
<button
onClick={ onClick }
disabled={ loading || !textToDecrypt }
>
decrypt ciphertext
</button>
{ plaintexts && (
<div>
<p>plaintexts:</p>
{plaintexts.map((plaintext) => (
<p>{plaintext}</p>
))}
</div>
)}
{ error && <p>error getting account: {error}</p> }
</div>
)
}

Types

type DecryptRequest = {
ciphertexts: string[];
address?: string;
network?: Network;
};
type DecryptResponse = {
plaintexts: string[];
};