account_sign_data
Cryptographically sign data using a local account's private key within the NEAR MCP server. Specify encoding to generate secure, encoded signatures with curve details.
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
TableJSON 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. | |
| data | Yes | The data to sign as a string. | |
| networkId | No | mainnet | |
| signatureEncoding | No | The encoding to use for signature creation. | base58 |
Implementation Reference
- src/services.ts:951-1008 (handler)The handler function that retrieves the account's keypair from the keystore, signs the provided data string, encodes the signature in base58 or base64, and returns the signer account ID, curve type, 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:939-950 (schema)Zod schema defining the input parameters: accountId (string), networkId (enum testnet/mainnet, default mainnet), data (string), signatureEncoding (enum base58/base64, default base58).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:934-1008 (registration)MCP tool registration for 'account_sign_data' including name, description, input schema, and handler function.'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 to retrieve the KeyPair for a given account ID and network from the keystore, used by the account_sign_data handler.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) }; } };