How to sign messages and send transactions on Solana
If you’re not using React, use the @account-kit/signer
package to create smart wallets on Solana, sign messages, sign transactions, and sponsor transaction fees.
If you’re using React, get started with Solana wallets here.
Create a Solana signer
Once you’ve authenticated users, convert the AlchemyWebSigner
to a Solana compatible signer using either of these methods:
import { import signersigner } from "./signer";
import { class SolanaSignerThe SolanaSigner class is used to sign transactions and messages for the Solana blockchain. It provides methods to add signatures to transactions and sign messages.
SolanaSigner } from "@account-kit/signer";
const const solanaSigner: anysolanaSigner = import signersigner.anytoSolanaSigner();
// OR
const const solanaSigner2: SolanaSignersolanaSigner2 = new new SolanaSigner(client: BaseSignerClient): SolanaSignerConstructor for the SolanaSigner class which is a wrapper around the alchemy client, and is more focused on the solana web3
SolanaSigner(import signersigner);
import { class AlchemyWebSignerA SmartAccountSigner that can be used with any SmartContractAccount
AlchemyWebSigner } from "@account-kit/signer";
export const const signer: AlchemyWebSignersigner = new new AlchemyWebSigner(params: AlchemySignerParams): AlchemyWebSignerInitializes an instance with the provided Alchemy signer parameters after parsing them with a schema.
AlchemyWebSigner({
client: ({
connection: {
apiKey: string;
rpcUrl?: undefined;
jwt?: undefined;
} | {
jwt: string;
rpcUrl?: undefined;
apiKey?: undefined;
} | {
rpcUrl: string;
apiKey?: undefined;
jwt?: undefined;
} | {
rpcUrl: string;
jwt: string;
apiKey?: undefined;
};
... 4 more ...;
enablePopupOauth?: boolean | undefined;
} | AlchemySignerWebClient) & (AlchemySignerWebClient | ... 1 more ... | undefined)client: {
connection: {
apiKey: string;
}connection: {
apiKey: stringapiKey: "API_KEY",
},
iframeConfig: {
iframeContainerId: string;
}iframeConfig: {
iframeContainerId: stringiframeContainerId: "alchemy-signer-iframe-container",
},
},
});
Sign Message
To sign either a string or byte array with your Solana wallet use the signMessage
method. The method accepts a UInt8Array
as input:
import { import signersigner } from "./signer";
import { function toBytes(value: string | bigint | number | boolean | Hex, opts?: ToBytesParameters): ByteArrayEncodes a UTF-8 string, hex value, bigint, number or boolean to a byte array.
Docs: https://viem.sh/docs/utilities/toBytes
Example: https://viem.sh/docs/utilities/toBytes#usage
toBytes } from "viem";
const const solanaSigner: anysolanaSigner = import signersigner.anytoSolanaSigner();
const const signedMessage: anysignedMessage = await const solanaSigner: anysolanaSigner.anysignMessage(
function toBytes(value: string | bigint | number | boolean | Hex, opts?: ToBytesParameters): ByteArrayEncodes a UTF-8 string, hex value, bigint, number or boolean to a byte array.
Docs: https://viem.sh/docs/utilities/toBytes
Example: https://viem.sh/docs/utilities/toBytes#usage
toBytes("Message as a string converted into UInt8Array"),
);
import { class AlchemyWebSignerA SmartAccountSigner that can be used with any SmartContractAccount
AlchemyWebSigner } from "@account-kit/signer";
export const const signer: AlchemyWebSignersigner = new new AlchemyWebSigner(params: AlchemySignerParams): AlchemyWebSignerInitializes an instance with the provided Alchemy signer parameters after parsing them with a schema.
AlchemyWebSigner({
client: ({
connection: {
apiKey: string;
rpcUrl?: undefined;
jwt?: undefined;
} | {
jwt: string;
rpcUrl?: undefined;
apiKey?: undefined;
} | {
rpcUrl: string;
apiKey?: undefined;
jwt?: undefined;
} | {
rpcUrl: string;
jwt: string;
apiKey?: undefined;
};
... 4 more ...;
enablePopupOauth?: boolean | undefined;
} | AlchemySignerWebClient) & (AlchemySignerWebClient | ... 1 more ... | undefined)client: {
connection: {
apiKey: string;
}connection: {
apiKey: stringapiKey: "API_KEY",
},
iframeConfig: {
iframeContainerId: string;
}iframeConfig: {
iframeContainerId: stringiframeContainerId: "alchemy-signer-iframe-container",
},
},
});
Sign Transaction
To sign a prepared transaction with your Solana wallet use the addSignature
method. The following example demonstrates signing a transfer transaction:
Sponsor transaction fees
To sponsor transaction fees on Solana:
- Set up your sponsorship policy in the dashboard to retrieve a policy ID
- Add sponsorship to the preparred transaction before signing and sending