-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[viem] signMessage
fails when using raw
property of SignableMessage
#116
Comments
Just confirmed that Viem's import { createActivityPoller, type TurnkeyClient } from "@turnkey/http";
import { hashMessage, signatureToHex, type SignableMessage } from "viem";
import { z } from "zod";
type Options = {
message: SignableMessage;
turnkeyClient: TurnkeyClient;
turnkeyPrivateKeyID: string;
turnkeySubOrganizationID: string;
}
export async function signMessage({
message,
turnkeyClient,
turnkeyPrivateKeyID,
turnkeySubOrganizationID,
}: Options) {
const activityPoller = createActivityPoller({
client: turnkeyClient,
requestFn: turnkeyClient.signRawPayload,
});
const { result: { signRawPayloadResult } } = CompletedActivitySchema.parse(await activityPoller({
type: 'ACTIVITY_TYPE_SIGN_RAW_PAYLOAD',
timestampMs: String(Date.now()),
organizationId: turnkeySubOrganizationID,
parameters: {
privateKeyId: turnkeyPrivateKeyID,
encoding: 'PAYLOAD_ENCODING_HEXADECIMAL',
hashFunction: 'HASH_FUNCTION_NO_OP',
payload: hashMessage(message),
},
}));
const signatureHex = signatureToHex({
r: `0x${signRawPayloadResult.r}`,
s: `0x${signRawPayloadResult.s}`,
v: signRawPayloadResult.v === "00" ? 27n : 28n,
});
return signatureHex;
}
const CompletedActivitySchema = z.object({
result: z.object({
signRawPayloadResult: z.object({
r: z.string(),
s: z.string(),
v: z.string(),
}),
}),
}); |
Merged
Hey @danscan the fix has been merged + released. Will be closing this issue now - please let us know if there are any additional issues and we'd be happy to revisit. Thanks again for filing this! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey there!
I noticed that using @turnkey/viem's
signMessage
will always fail if you use the raw form of theSignableMessage
type.Looks like it's due to this line, which assumes message is always a string, and casts it to
0x${string}
:sdk/packages/viem/src/index.ts
Line 190 in 89d9999
I believe a quick fix is to replace
keccak256(message as `0x${string}`)
withhashMessage(message)
. You can importhashMessage
fromviem
itself, and it accepts aSignableMessage
, so it should be exactly what's needed here.FYI Viem has a nice set of typesafe utilities, and also, as I recently found, common types like
Hex
so you don't need to type`0x${string}`
all over :)The text was updated successfully, but these errors were encountered: