Skip to content
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

support eip 4844 (type 3) transaction signing in viem example #417

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/with-viem/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@turnkey/examples/with-viem

## 0.1.0

- Adds example for signing type 3 (blob) transactions (see `src/advanced.ts`)

## 0.0.1

Initial release!
6 changes: 4 additions & 2 deletions examples/with-viem/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@turnkey/example-with-viem",
"version": "0.0.1",
"version": "0.2.0",
"private": true,
"scripts": {
"start": "tsx src/index.ts",
Expand All @@ -18,11 +18,13 @@
"@turnkey/http": "workspace:*",
"@turnkey/sdk-server": "workspace:*",
"@turnkey/viem": "workspace:*",
"c-kzg": "2.1.2",
"dotenv": "^16.0.3",
"fetch": "^1.1.0",
"kzg-wasm": "0.4.0",
"prompts": "^2.4.2",
"typescript": "5.1",
"viem": "^1.16.6"
"viem": "2.12.1"
},
"devDependencies": {
"@types/prompts": "^2.4.2"
Expand Down
37 changes: 37 additions & 0 deletions examples/with-viem/src/advanced.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as path from "path";
import * as dotenv from "dotenv";

import { writeFileSync } from "fs";

import { createAccount } from "@turnkey/viem";
import { TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
Expand All @@ -12,14 +14,21 @@ import {
stringToHex,
hexToBytes,
type Account,
toBlobs,
parseGwei,
serializeTransaction,
} from "viem";
import { sepolia } from "viem/chains";
import { loadKZG } from "kzg-wasm";

import { print, assertEqual } from "./util";
import { createNewWallet } from "./createNewWallet";

// Load environment variables from `.env.local`
dotenv.config({ path: path.resolve(process.cwd(), ".env.local") });

const TKHQ_WARCHEST = "0x08d2b0a37F869FF76BACB5Bab3278E26ab7067B7";

async function main() {
if (!process.env.SIGN_WITH) {
// If you don't specify a `SIGN_WITH`, we'll create a new wallet for you via calling the Turnkey API.
Expand Down Expand Up @@ -129,6 +138,34 @@ async function main() {

print("Turnkey-powered signature - typed data (EIP-712):", `${signature}`);
assertEqual(address, recoveredAddress);

// 4. Sign type-3 (EIP-4844/blob) transaction
const kzg = await loadKZG();
const blobs = toBlobs({ data: stringToHex("hello my worldy") });

// Note: this will write to your local filesystem. Useful for debugging.
writeFileSync(
"transaction.txt",
serializeTransaction({
blobs,
kzg: kzg,
maxFeePerBlobGas: parseGwei("100"),
to: TKHQ_WARCHEST,
gas: 21000n,
chainId: sepolia.id,
})
);

const hash = await client.sendTransaction({
blobs,
kzg: kzg,
maxFeePerBlobGas: parseGwei("100"),
to: TKHQ_WARCHEST,
gas: 21000n,
chain: sepolia,
});

print("Transaction sent", `https://sepolia.etherscan.io/tx/${hash}`);
}

main().catch((error) => {
Expand Down
8,265 changes: 8,265 additions & 0 deletions examples/with-viem/src/trusted-setups/minimal.json

Large diffs are not rendered by default.

4,163 changes: 4,163 additions & 0 deletions examples/with-viem/src/trusted-setups/minimal.txt

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/viem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"compile:contracts": "hardhat compile"
},
"peerDependencies": {
"viem": "^1.16.6 || ^2.1.1"
"viem": "^1.16.6 || ^2.12.1"
},
"dependencies": {
"@turnkey/api-key-stamper": "workspace:*",
Expand All @@ -65,7 +65,7 @@
"devDependencies": {
"@types/jest": "^29.5.3",
"jest": "^29.3.1",
"viem": "^2.1.1"
"viem": "^2.12.1"
},
"engines": {
"node": ">=18.0.0"
Expand Down
27 changes: 15 additions & 12 deletions packages/viem/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ export function createAccountWithAddress(input: {
});
}

return toAccount({
return {
type: "local",
publicKey: "0x", // leaving this blank for now
source: "custom",
address: ethereumAddress as Hex,
signMessage: function ({
message,
Expand All @@ -113,13 +116,13 @@ export function createAccountWithAddress(input: {
TTransactionSerializable extends TransactionSerializable
>(
transaction: TTransactionSerializable,
args?:
| { serializer?: SerializeTransactionFn<TTransactionSerializable> }
| undefined
options?: {
serializer?: SerializeTransactionFn | undefined; // TODO: use exactOptionalPropertyTypes instead
}
): Promise<Hex> {
const serializer = !args?.serializer
const serializer = !options?.serializer
? serializeTransaction
: args.serializer;
: options.serializer;

return signTransaction(
client,
Expand All @@ -134,7 +137,7 @@ export function createAccountWithAddress(input: {
): Promise<Hex> {
return signTypedData(client, typedData, organizationId, signWith);
},
});
};
}

export async function createAccount(input: {
Expand Down Expand Up @@ -263,13 +266,13 @@ export async function createApiKeyAccount(
TTransactionSerializable extends TransactionSerializable
>(
transaction: TTransactionSerializable,
args?:
| { serializer?: SerializeTransactionFn<TTransactionSerializable> }
| undefined
options?: {
serializer?: SerializeTransactionFn | undefined; // TODO: use exactOptionalPropertyTypes instead
}
): Promise<Hex> {
const serializer = !args?.serializer
const serializer = !options?.serializer
? serializeTransaction
: args.serializer;
: options.serializer;

return signTransaction(
client,
Expand Down
Loading
Loading