account_sign_data
Sign data strings using a local account's private key. Outputs the signature encoded in base58 or base64.
Instructions
Cryptographically sign a piece of data with a local account's private key, then encode the result with the specified encoding. Outputs the curve, encoded signature, and encoding used.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| accountId | Yes | The account id of the account that will sign the data. This account must be in the local keystore. | |
| networkId | No | mainnet | |
| data | Yes | The data to sign as a string. | |
| signatureEncoding | No | The encoding to use for signature creation. | base58 |
Implementation Reference
- src/services.ts:951-1008 (handler)The handler function for the 'account_sign_data' tool. It retrieves the key pair from the keystore, signs the provided data using the account's private key, encodes the signature in base58 or base64, and returns the curve, encoded signature, and encoding used.
async (args, _) => { const keyPairResult: Result<KeyPair, Error> = await getAccountKeyPair( args.accountId, args.networkId, keystore, ); if (!keyPairResult.ok) { return { content: [{ type: 'text', text: `Error: ${keyPairResult.error}` }], }; } const keyPair = keyPairResult.value; const signatureRaw = keyPair.sign(new TextEncoder().encode(args.data)); const signatureEncodingResult: Result<string, Error> = (() => { try { switch (args.signatureEncoding) { case 'base64': return { ok: true, value: Buffer.from(signatureRaw.signature).toString('base64'), }; case 'base58': return { ok: true, value: base58.encode(Buffer.from(signatureRaw.signature)), }; default: throw new Error( `Unsupported encoding: ${String(args.signatureEncoding)}`, ); } } catch (e) { return { ok: false, error: new Error(e as string) }; } })(); if (!signatureEncodingResult.ok) { return { content: [ { type: 'text', text: `Error: ${signatureEncodingResult.error}` }, ], }; } return { content: [ { type: 'text', text: stringify_bigint({ signerAccountId: args.accountId, curve: keyTypeToCurvePrefix(keyPair.getPublicKey().keyType), signature: signatureEncodingResult.value, encoding: args.signatureEncoding, }), }, ], }; }, ); - src/services.ts:938-950 (schema)Input schema for the 'account_sign_data' tool. Defines parameters: accountId, networkId (testnet/mainnet), data (string to sign), and signatureEncoding (base58/base64).
{ accountId: z .string() .describe( 'The account id of the account that will sign the data. This account must be in the local keystore.', ), networkId: z.enum(['testnet', 'mainnet']).default('mainnet'), data: z.string().describe('The data to sign as a string.'), signatureEncoding: z .enum(['base58', 'base64']) .default('base58') .describe('The encoding to use for signature creation.'), }, - src/services.ts:933-1008 (registration)Registration of the 'account_sign_data' tool via mcp.tool() call within the createMcpServer function.
mcp.tool( 'account_sign_data', noLeadingWhitespace` Cryptographically sign a piece of data with a local account's private key, then encode the result with the specified encoding. Outputs the curve, encoded signature, and encoding used.`, { accountId: z .string() .describe( 'The account id of the account that will sign the data. This account must be in the local keystore.', ), networkId: z.enum(['testnet', 'mainnet']).default('mainnet'), data: z.string().describe('The data to sign as a string.'), signatureEncoding: z .enum(['base58', 'base64']) .default('base58') .describe('The encoding to use for signature creation.'), }, async (args, _) => { const keyPairResult: Result<KeyPair, Error> = await getAccountKeyPair( args.accountId, args.networkId, keystore, ); if (!keyPairResult.ok) { return { content: [{ type: 'text', text: `Error: ${keyPairResult.error}` }], }; } const keyPair = keyPairResult.value; const signatureRaw = keyPair.sign(new TextEncoder().encode(args.data)); const signatureEncodingResult: Result<string, Error> = (() => { try { switch (args.signatureEncoding) { case 'base64': return { ok: true, value: Buffer.from(signatureRaw.signature).toString('base64'), }; case 'base58': return { ok: true, value: base58.encode(Buffer.from(signatureRaw.signature)), }; default: throw new Error( `Unsupported encoding: ${String(args.signatureEncoding)}`, ); } } catch (e) { return { ok: false, error: new Error(e as string) }; } })(); if (!signatureEncodingResult.ok) { return { content: [ { type: 'text', text: `Error: ${signatureEncodingResult.error}` }, ], }; } return { content: [ { type: 'text', text: stringify_bigint({ signerAccountId: args.accountId, curve: keyTypeToCurvePrefix(keyPair.getPublicKey().keyType), signature: signatureEncodingResult.value, encoding: args.signatureEncoding, }), }, ], }; }, ); - src/services.ts:85-96 (helper)Helper function getAccountKeyPair used by the handler to retrieve the key pair from the keystore by accountId and networkId.
const getAccountKeyPair = async ( accountId: string, networkId: string, keystore: UnencryptedFileSystemKeyStore, ): Promise<Result<KeyPair, Error>> => { try { const keyPair = await keystore.getKey(networkId, accountId); return { ok: true, value: keyPair }; } catch (e) { return { ok: false, error: new Error(e as string) }; } }; - src/utils.ts:36-43 (helper)Helper function keyTypeToCurvePrefix used by the handler to convert the key type (ED25519/SECP256K1) to a human-readable curve name.
export const keyTypeToCurvePrefix = (keyType: KeyType) => { switch (keyType) { case KeyType.ED25519: return 'ed25519'; case KeyType.SECP256K1: return 'secp256k1'; } };