From c15a9ac6b8c2a9ed6829067c320839e784126977 Mon Sep 17 00:00:00 2001 From: Andrew Min Date: Sat, 16 Sep 2023 23:02:30 -0400 Subject: [PATCH] add ethers tests --- examples/with-ethers/package.json | 1 + examples/with-ethers/src/advanced.ts | 115 +++++++++++++++++++++++++++ examples/with-ethers/src/index.ts | 11 +-- examples/with-ethers/src/util.ts | 9 +++ examples/with-viem/src/advanced.ts | 4 +- 5 files changed, 129 insertions(+), 11 deletions(-) create mode 100644 examples/with-ethers/src/advanced.ts create mode 100644 examples/with-ethers/src/util.ts diff --git a/examples/with-ethers/package.json b/examples/with-ethers/package.json index 66abf87f7..407b49644 100644 --- a/examples/with-ethers/package.json +++ b/examples/with-ethers/package.json @@ -4,6 +4,7 @@ "private": true, "scripts": { "start": "pnpm -w run build-all && tsx src/index.ts", + "start-advanced": "pnpm -w run build-all && tsx src/advanced.ts", "start-legacy-sepolia": "pnpm -w run build-all && tsx src/legacySepolia.ts", "clean": "rimraf ./dist ./.cache", "typecheck": "tsc --noEmit" diff --git a/examples/with-ethers/src/advanced.ts b/examples/with-ethers/src/advanced.ts new file mode 100644 index 000000000..13b383dcd --- /dev/null +++ b/examples/with-ethers/src/advanced.ts @@ -0,0 +1,115 @@ +import * as path from "path"; +import * as dotenv from "dotenv"; + +// Load environment variables from `.env.local` +dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); + +import { TurnkeySigner } from "@turnkey/ethers"; +import { ethers } from "ethers"; +import { TurnkeyClient } from "@turnkey/http"; +import { ApiKeyStamper } from "@turnkey/api-key-stamper"; +import { createNewEthereumPrivateKey } from "./createNewEthereumPrivateKey"; + +async function main() { + if (!process.env.PRIVATE_KEY_ID) { + // If you don't specify a `PRIVATE_KEY_ID`, we'll create one for you via calling the Turnkey API. + await createNewEthereumPrivateKey(); + return; + } + + const turnkeyClient = new TurnkeyClient( + { + baseUrl: process.env.BASE_URL!, + }, + new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }) + ); + + // Initialize a Turnkey Signer + const turnkeySigner = new TurnkeySigner({ + client: turnkeyClient, + organizationId: process.env.ORGANIZATION_ID!, + privateKeyId: process.env.PRIVATE_KEY_ID!, + }); + + // Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/) + const network = "goerli"; + const provider = new ethers.providers.InfuraProvider(network); + const connectedSigner = turnkeySigner.connect(provider); + const address = await connectedSigner.getAddress(); + + print("Address:", address); + + const baseMessage = "Hello Turnkey"; + + // 1. Sign a raw hex message + const hexMessage = ethers.utils.hexlify( + ethers.utils.toUtf8Bytes(baseMessage) + ); + let signature = await connectedSigner.signMessage(hexMessage); + let recoveredAddress = ethers.utils.verifyMessage(hexMessage, signature); + + print("Turnkey-powered signature - raw hex message:", `${signature}`); + assertEqual(recoveredAddress, address); + + // 2. Sign a raw bytes message + const bytesMessage = ethers.utils.toUtf8Bytes(baseMessage); + signature = await connectedSigner.signMessage(bytesMessage); + recoveredAddress = ethers.utils.verifyMessage(bytesMessage, signature); + + print("Turnkey-powered signature - raw bytes message:", `${signature}`); + assertEqual(recoveredAddress, address); + + // 3. Sign typed data (EIP-712) + const typedData = { + types: { + // Note that we do not need to include `EIP712Domain` as a type here, as Ethers will automatically inject it for us + Person: [ + { name: "name", type: "string" }, + { name: "wallet", type: "address" }, + ], + }, + domain: { + name: "EIP712 Test", + version: "1", + }, + primaryType: "Person", + message: { + name: "Alice", + wallet: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e", + }, + }; + + signature = await connectedSigner.signTypedData( + typedData.domain, + typedData.types, + typedData.message + ); + + recoveredAddress = ethers.utils.verifyTypedData( + typedData.domain, + typedData.types, + typedData.message, + signature + ); + + print("Turnkey-powered signature - typed data (EIP-712):", `${signature}`); + assertEqual(recoveredAddress, address); +} + +main().catch((error) => { + console.error(error); + process.exit(1); +}); + +function print(header: string, body: string): void { + console.log(`${header}\n\t${body}\n`); +} + +function assertEqual(left: T, right: T) { + if (left !== right) { + throw new Error(`${JSON.stringify(left)} !== ${JSON.stringify(right)}`); + } +} diff --git a/examples/with-ethers/src/index.ts b/examples/with-ethers/src/index.ts index c2e04f440..fa209eabd 100644 --- a/examples/with-ethers/src/index.ts +++ b/examples/with-ethers/src/index.ts @@ -9,6 +9,7 @@ import { ethers } from "ethers"; import { TurnkeyClient } from "@turnkey/http"; import { ApiKeyStamper } from "@turnkey/api-key-stamper"; import { createNewEthereumPrivateKey } from "./createNewEthereumPrivateKey"; +import { print, assertEqual } from "./util"; import WETH_TOKEN_ABI from "./weth-contract-abi.json"; const WETH_TOKEN_ADDRESS_GOERLI = "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6"; @@ -123,13 +124,3 @@ main().catch((error) => { console.error(error); process.exit(1); }); - -function print(header: string, body: string): void { - console.log(`${header}\n\t${body}\n`); -} - -function assertEqual(left: T, right: T) { - if (left !== right) { - throw new Error(`${JSON.stringify(left)} !== ${JSON.stringify(right)}`); - } -} diff --git a/examples/with-ethers/src/util.ts b/examples/with-ethers/src/util.ts new file mode 100644 index 000000000..de3641621 --- /dev/null +++ b/examples/with-ethers/src/util.ts @@ -0,0 +1,9 @@ +export function print(header: string, body: string): void { + console.log(`${header}\n\t${body}\n`); +} + +export function assertEqual(left: T, right: T) { + if (left !== right) { + throw new Error(`${JSON.stringify(left)} !== ${JSON.stringify(right)}`); + } +} diff --git a/examples/with-viem/src/advanced.ts b/examples/with-viem/src/advanced.ts index d49790bd2..e4fbe51c7 100644 --- a/examples/with-viem/src/advanced.ts +++ b/examples/with-viem/src/advanced.ts @@ -45,6 +45,8 @@ async function main() { }); const address = client.account.address; + print("Address:", address); + const baseMessage = "Hello Turnkey"; // 1. Sign a raw hex message @@ -118,7 +120,7 @@ async function main() { signature, }); - print("Turnkey-powered signature - typed data:", `${signature}`); + print("Turnkey-powered signature - typed data (EIP-712):", `${signature}`); assertEqual(address, recoveredAddress); }