Skip to content

Commit

Permalink
update viem implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Sep 16, 2023
1 parent 89d9999 commit 606184d
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions packages/viem/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { hashTypedData, serializeTransaction, signatureToHex } from "viem";
import { toAccount } from "viem/accounts";
import { keccak256 } from "viem";
import { hashMessage } from "viem";
import type {
Hex,
HashTypedDataParameters,
LocalAccount,
SerializeTransactionFn,
Expand Down Expand Up @@ -44,12 +45,12 @@ export async function createAccount(input: {
}

return toAccount({
address: ethereumAddress as `0x${string}`,
address: ethereumAddress as Hex,
signMessage: function ({
message,
}: {
message: SignableMessage;
}): Promise<`0x${string}`> {
}): Promise<Hex> {
return signMessage(client, message, organizationId, privateKeyId);
},
signTransaction: function <
Expand All @@ -59,7 +60,7 @@ export async function createAccount(input: {
args?:
| { serializer?: SerializeTransactionFn<TTransactionSerializable> }
| undefined
): Promise<`0x${string}`> {
): Promise<Hex> {
const serializer = !args?.serializer
? serializeTransaction
: args.serializer;
Expand All @@ -74,7 +75,7 @@ export async function createAccount(input: {
},
signTypedData: function (
typedData: TypedData | { [key: string]: unknown }
): Promise<`0x${string}`> {
): Promise<Hex> {
return signTypedData(client, typedData, organizationId, privateKeyId);
},
});
Expand Down Expand Up @@ -145,12 +146,12 @@ export async function createApiKeyAccount(
}

return toAccount({
address: ethereumAddress as `0x${string}`,
address: ethereumAddress as Hex,
signMessage: function ({
message,
}: {
message: SignableMessage;
}): Promise<`0x${string}`> {
}): Promise<Hex> {
return signMessage(client, message, organizationId, privateKeyId);
},
signTransaction: function <
Expand All @@ -160,7 +161,7 @@ export async function createApiKeyAccount(
args?:
| { serializer?: SerializeTransactionFn<TTransactionSerializable> }
| undefined
): Promise<`0x${string}`> {
): Promise<Hex> {
const serializer = !args?.serializer
? serializeTransaction
: args.serializer;
Expand All @@ -175,7 +176,7 @@ export async function createApiKeyAccount(
},
signTypedData: function (
typedData: TypedData | { [key: string]: unknown }
): Promise<`0x${string}`> {
): Promise<Hex> {
return signTypedData(client, typedData, organizationId, privateKeyId);
},
});
Expand All @@ -186,15 +187,15 @@ async function signMessage(
message: SignableMessage,
organizationId: string,
privateKeyId: string
): Promise<`0x${string}`> {
const hashedMessage = keccak256(message as `0x${string}`);
): Promise<Hex> {
const hashedMessage = hashMessage(message);
const signedMessage = await signMessageWithErrorWrapping(
client,
hashedMessage,
organizationId,
privateKeyId
);
return `${signedMessage}` as `0x${string}`;
return `${signedMessage}` as Hex;
}

async function signTransaction<
Expand All @@ -205,7 +206,7 @@ async function signTransaction<
serializer: SerializeTransactionFn<TTransactionSerializable>,
organizationId: string,
privateKeyId: string
): Promise<`0x${string}`> {
): Promise<Hex> {
const serializedTx = serializer(transaction);
const nonHexPrefixedSerializedTx = serializedTx.replace(/^0x/, "");
return await signTransactionWithErrorWrapping(
Expand All @@ -221,7 +222,7 @@ async function signTypedData(
data: TypedData | { [key: string]: unknown },
organizationId: string,
privateKeyId: string
): Promise<`0x${string}`> {
): Promise<Hex> {
const hashToSign = hashTypedData(data as HashTypedDataParameters);

return await signMessageWithErrorWrapping(
Expand All @@ -237,7 +238,7 @@ async function signTransactionWithErrorWrapping(
unsignedTransaction: string,
organizationId: string,
privateKeyId: string
): Promise<`0x${string}`> {
): Promise<Hex> {
let signedTx: string;
try {
signedTx = await signTransactionImpl(
Expand Down Expand Up @@ -298,7 +299,7 @@ async function signMessageWithErrorWrapping(
message: string,
organizationId: string,
privateKeyId: string
): Promise<`0x${string}`> {
): Promise<Hex> {
let signedMessage: string;
try {
signedMessage = await signMessageImpl(
Expand All @@ -318,7 +319,7 @@ async function signMessageWithErrorWrapping(
});
}

return signedMessage as `0x${string}`;
return signedMessage as Hex;
}

async function signMessageImpl(
Expand Down

0 comments on commit 606184d

Please sign in to comment.