@avalanche-sdk/client
Version:
A TypeScript SDK for interacting with the Avalanche network through JSON-RPC APIs. This SDK provides a comprehensive set of tools to interact with all Avalanche chains (P-Chain, X-Chain, C-Chain) and various APIs, including wallet functionality for transa
29 lines (26 loc) • 870 B
text/typescript
import { Int, secp256k1, utils } from "@avalabs/avalanchejs";
import { utf8ToBytes } from "@noble/hashes/utils";
/**
* Signs a message with an XP private key.
*
* @param message - The message to sign.
* @param privateKey - The private key to sign with.
* @returns A promise that resolves to the signature as a `0x` prefixed string.
*/
export async function xpSignMessage(
message: string,
privateKey: string
): Promise<string> {
const prefix = "Avalanche Signed Message:\n";
const messageToHashBuffer = new Uint8Array([
...utils.hexToBuffer(prefix.length.toString(16)),
...utf8ToBytes(prefix),
...new Int(message.length).toBytes(),
...utf8ToBytes(message),
]);
const sig = await secp256k1.sign(
messageToHashBuffer,
utils.hexToBuffer(privateKey)
);
return utils.base58.encode(utils.addChecksum(utils.addChecksum(sig)));
}