-
Notifications
You must be signed in to change notification settings - Fork 242
/
sign-transaction.ts
47 lines (43 loc) · 1.31 KB
/
sign-transaction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import type { TransactionSerializable } from "viem";
import type { Hex } from "../../utils/encoding/hex.js";
import { keccak256 } from "../../utils/hashing/keccak256.js";
import { sign } from "../../utils/signatures/sign.js";
import { serializeTransaction } from "../serialize-transaction.js";
export type SignTransactionOptions = {
transaction: TransactionSerializable;
privateKey: Hex;
// TODO: Add optional custom serializer here
};
/**
* Signs a transaction to be sent to a node.
* @param options The options for signing.
* @param options.transaction - The transaction object to sign
* @param options.privateKey - The account private key
* @returns The signed transaction as a hex string
* @example
* ```ts
* import { signTransaction } from "thirdweb";
* signTransaction({
* transaction: {
* ...
* },
* privateKey: "0x...",
* });
* ```
*/
export function signTransaction({
transaction,
privateKey,
}: SignTransactionOptions): Hex {
if (transaction.type === "eip4844") {
transaction = { ...transaction, sidecars: false };
}
const serializedTransaction = serializeTransaction({ transaction });
const signature = sign({
hash: keccak256(serializedTransaction),
privateKey: privateKey,
});
return serializeTransaction({
transaction: { ...transaction, ...signature },
});
}