Skip to content

Commit

Permalink
bring back old with-ethers example for v5
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Apr 12, 2024
1 parent f08b694 commit 7e92186
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 67 deletions.
4 changes: 2 additions & 2 deletions examples/with-ethers/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Example: `with-ethers`

This example shows how to construct and broadcast a transaction using [`Ethers`](https://docs.ethers.org/v6/api/providers/#Signer) with Turnkey.
This example shows how to construct and broadcast a transaction using [`Ethers`](https://docs.ethers.org/v5/api/signer/) with Turnkey.

If you want to see a demo with passkeys, head to the example [`with-ethers-and-passkeys`](../with-ethers-and-passkeys/) to see a NextJS app using passkeys.

Expand Down Expand Up @@ -105,7 +105,7 @@ This script will do the following:
1. send ETH (via type 0, EIP-155-compliant legacy transaction)
2. deposit ETH into the WETH contract (aka wrapping)

Note that these transactions will all be broadcasted sequentially.
Note that these transactions will all be broadcasted sequentially. Additionally, Ethers v5 is only compatible with the Sepolia network if paired with Infura (Ethers v5 does not supported Alchemy with Sepolia at this time).

The script constructs a transaction via Turnkey and broadcasts via Infura. If the script exits because your account isn't funded, you can request funds on https://sepoliafaucet.com/ or via Coinbase Wallet.

Expand Down
2 changes: 1 addition & 1 deletion examples/with-ethers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"@turnkey/http": "workspace:*",
"@turnkey/api-key-stamper": "workspace:*",
"dotenv": "^16.0.3",
"ethers": "^6.10.0"
"ethers": "^5.7.2"
}
}
16 changes: 9 additions & 7 deletions examples/with-ethers/src/advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ async function main() {
signWith: process.env.SIGN_WITH!,
});

// Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v6/api/providers/)
// Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/)
const network = "goerli";
const provider = new ethers.InfuraProvider(network);
const provider = new ethers.providers.InfuraProvider(network);
const connectedSigner = turnkeySigner.connect(provider);
const address = await connectedSigner.getAddress();

Expand All @@ -45,17 +45,19 @@ async function main() {
const baseMessage = "Hello Turnkey";

// 1. Sign a raw hex message
const hexMessage = ethers.hexlify(ethers.toUtf8Bytes(baseMessage));
const hexMessage = ethers.utils.hexlify(
ethers.utils.toUtf8Bytes(baseMessage)
);
let signature = await connectedSigner.signMessage(hexMessage);
let recoveredAddress = ethers.verifyMessage(hexMessage, signature);
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.toUtf8Bytes(baseMessage);
const bytesMessage = ethers.utils.toUtf8Bytes(baseMessage);
signature = await connectedSigner.signMessage(bytesMessage);
recoveredAddress = ethers.verifyMessage(bytesMessage, signature);
recoveredAddress = ethers.utils.verifyMessage(bytesMessage, signature);

print("Turnkey-powered signature - raw bytes message:", `${signature}`);
assertEqual(recoveredAddress, address);
Expand Down Expand Up @@ -86,7 +88,7 @@ async function main() {
typedData.message
);

recoveredAddress = ethers.verifyTypedData(
recoveredAddress = ethers.utils.verifyTypedData(
typedData.domain,
typedData.types,
typedData.message,
Expand Down
34 changes: 15 additions & 19 deletions examples/with-ethers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,25 @@ async function main() {
signWith: process.env.SIGN_WITH!,
});

// Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v6/api/providers/)
// Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/)
const network = "goerli";
const provider = new ethers.JsonRpcProvider(
`https://${network}.infura.io/v3/${process.env.INFURA_KEY}`
);
const provider = new ethers.providers.InfuraProvider(network);
const connectedSigner = turnkeySigner.connect(provider);

const chainId = (await connectedSigner.provider?.getNetwork())?.chainId ?? 0;
const chainId = await connectedSigner.getChainId();
const address = await connectedSigner.getAddress();
const balance = (await connectedSigner.provider?.getBalance(address)) ?? 0;
const transactionCount = await connectedSigner.provider?.getTransactionCount(
address
);
const balance = await connectedSigner.getBalance();
const transactionCount = await connectedSigner.getTransactionCount();

print("Network:", `${network} (chain ID ${chainId})`);
print("Address:", address);
print("Balance:", `${ethers.formatEther(balance)} Ether`);
print("Balance:", `${ethers.utils.formatEther(balance)} Ether`);
print("Transaction count:", `${transactionCount}`);

// 1. Sign a raw payload (`eth_sign` style)
const message = "Hello Turnkey";
const signature = await connectedSigner.signMessage(message);
const recoveredAddress = ethers.verifyMessage(message, signature);
const recoveredAddress = ethers.utils.verifyMessage(message, signature);

print("Turnkey-powered signature:", `${signature}`);
print("Recovered address:", `${recoveredAddress}`);
Expand All @@ -71,15 +67,15 @@ async function main() {
const destinationAddress = "0x2Ad9eA1E677949a536A270CEC812D6e868C88108";
const transactionRequest = {
to: destinationAddress,
value: ethers.parseEther(transactionAmount),
value: ethers.utils.parseEther(transactionAmount),
type: 2,
};

const signedTx = await connectedSigner.signTransaction(transactionRequest);

print("Turnkey-signed transaction:", `${signedTx}`);

if (balance === 0) {
if (balance.isZero()) {
let warningMessage =
"The transaction won't be broadcasted because your account balance is zero.\n";
if (network === "goerli") {
Expand All @@ -95,7 +91,7 @@ async function main() {
const sentTx = await connectedSigner.sendTransaction(transactionRequest);

print(
`Sent ${ethers.formatEther(sentTx.value)} Ether to ${sentTx.to}:`,
`Sent ${ethers.utils.formatEther(sentTx.value)} Ether to ${sentTx.to}:`,
`https://${network}.etherscan.io/tx/${sentTx.hash}`
);

Expand All @@ -108,17 +104,17 @@ async function main() {
);

// Read from contract
const wethBalance = await wethContract?.balanceOf?.(address);
const wethBalance = await wethContract.balanceOf(address);

print("WETH Balance:", `${ethers.formatEther(wethBalance)} WETH`);
print("WETH Balance:", `${ethers.utils.formatEther(wethBalance)} WETH`);

// 3. Wrap ETH -> WETH
const depositTx = await wethContract?.deposit?.({
value: ethers.parseEther(transactionAmount),
const depositTx = await wethContract.deposit({
value: ethers.utils.parseEther(transactionAmount),
});

print(
`Wrapped ${ethers.formatEther(depositTx.value)} ETH:`,
`Wrapped ${ethers.utils.formatEther(depositTx.value)} ETH:`,
`https://${network}.etherscan.io/tx/${depositTx.hash}`
);
}
Expand Down
32 changes: 15 additions & 17 deletions examples/with-ethers/src/legacy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,25 @@ async function main() {
signWith: process.env.PRIVATE_KEY_ID!,
});

// Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v6/api/providers/)
// Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/)
const network = "goerli";
const provider = new ethers.InfuraProvider(network);
const provider = new ethers.providers.InfuraProvider(network);
const connectedSigner = turnkeySigner.connect(provider);

const chainId = (await connectedSigner.provider?.getNetwork())?.chainId;
const chainId = await connectedSigner.getChainId();
const address = await connectedSigner.getAddress();
const balance = (await connectedSigner.provider?.getBalance(address)) ?? 0;
const transactionCount = await connectedSigner.provider?.getTransactionCount(
address
);
const balance = await connectedSigner.getBalance();
const transactionCount = await connectedSigner.getTransactionCount();

print("Network:", `${network} (chain ID ${chainId})`);
print("Address:", address);
print("Balance:", `${ethers.formatEther(balance)} Ether`);
print("Balance:", `${ethers.utils.formatEther(balance)} Ether`);
print("Transaction count:", `${transactionCount}`);

// 1. Sign a raw payload (`eth_sign` style)
const message = "Hello Turnkey";
const signature = await connectedSigner.signMessage(message);
const recoveredAddress = ethers.verifyMessage(message, signature);
const recoveredAddress = ethers.utils.verifyMessage(message, signature);

print("Turnkey-powered signature:", `${signature}`);
print("Recovered address:", `${recoveredAddress}`);
Expand All @@ -69,15 +67,15 @@ async function main() {
const destinationAddress = "0x2Ad9eA1E677949a536A270CEC812D6e868C88108";
const transactionRequest = {
to: destinationAddress,
value: ethers.parseEther(transactionAmount),
value: ethers.utils.parseEther(transactionAmount),
type: 2,
};

const signedTx = await connectedSigner.signTransaction(transactionRequest);

print("Turnkey-signed transaction:", `${signedTx}`);

if (balance === 0) {
if (balance.isZero()) {
let warningMessage =
"The transaction won't be broadcasted because your account balance is zero.\n";
if (network === "goerli") {
Expand All @@ -93,7 +91,7 @@ async function main() {
const sentTx = await connectedSigner.sendTransaction(transactionRequest);

print(
`Sent ${ethers.formatEther(sentTx.value)} Ether to ${sentTx.to}:`,
`Sent ${ethers.utils.formatEther(sentTx.value)} Ether to ${sentTx.to}:`,
`https://${network}.etherscan.io/tx/${sentTx.hash}`
);

Expand All @@ -106,17 +104,17 @@ async function main() {
);

// Read from contract
const wethBalance = await wethContract?.balanceOf?.(address);
const wethBalance = await wethContract.balanceOf(address);

print("WETH Balance:", `${ethers.formatEther(wethBalance)} WETH`);
print("WETH Balance:", `${ethers.utils.formatEther(wethBalance)} WETH`);

// 3. Wrap ETH -> WETH
const depositTx = await wethContract?.deposit?.({
value: ethers.parseEther(transactionAmount),
const depositTx = await wethContract.deposit({
value: ethers.utils.parseEther(transactionAmount),
});

print(
`Wrapped ${ethers.formatEther(depositTx.value)} ETH:`,
`Wrapped ${ethers.utils.formatEther(depositTx.value)} ETH:`,
`https://${network}.etherscan.io/tx/${depositTx.hash}`
);
}
Expand Down
39 changes: 18 additions & 21 deletions examples/with-ethers/src/sepoliaLegacyTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,32 @@ async function main() {
signWith: process.env.SIGN_WITH!,
});

// Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v6/api/providers/)
// Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/)
const network = "sepolia";
const provider = new ethers.InfuraProvider(network);
const provider = new ethers.providers.InfuraProvider(network);
const connectedSigner = turnkeySigner.connect(provider);

const chainId =
(await connectedSigner.provider?.getNetwork())?.chainId ?? "58008"; // sepolia;
const chainId = await connectedSigner.getChainId();
const address = await connectedSigner.getAddress();
const balance = (await connectedSigner.provider?.getBalance(address)) ?? 0;
const nonce = (await connectedSigner.getNonce()) ?? 0;
const { gasPrice } = (await connectedSigner.provider?.getFeeData()) ?? {
gasPrice: 0,
};
const balance = await connectedSigner.getBalance();
const transactionCount = await connectedSigner.getTransactionCount();
const gasPrice = await connectedSigner.getGasPrice();

print("Network:", `${network} (chain ID ${chainId})`);
print("Address:", address);
print("Balance:", `${ethers.formatEther(balance)} Ether`);
print("Transaction count (nonce):", `${nonce}`);
print("Balance:", `${ethers.utils.formatEther(balance)} Ether`);
print("Transaction count:", `${transactionCount}`);

// 1. Create a legacy, EIP-155 (replay attack-preventing) send transaction
const transactionAmount = "0.00001";
const destinationAddress = "0x2Ad9eA1E677949a536A270CEC812D6e868C88108";
const transactionRequest = {
chainId: chainId,
nonce,
nonce: transactionCount,
to: destinationAddress,
gasLimit: 21000,
gasPrice,
value: ethers.parseEther(transactionAmount),
gasPrice: gasPrice,
value: ethers.utils.parseEther(transactionAmount),
data: "0x",
type: 0,
};
Expand All @@ -74,7 +71,7 @@ async function main() {

print("Turnkey-signed transaction:", `${signedTx}`);

if (balance === 0) {
if (balance.isZero()) {
let warningMessage =
"The transaction won't be broadcasted because your account balance is zero.\n";
if (network === "sepolia") {
Expand All @@ -89,7 +86,7 @@ async function main() {
const sentTx = await connectedSigner.sendTransaction(transactionRequest);

print(
`Sent ${ethers.formatEther(sentTx.value)} Ether to ${sentTx.to}:`,
`Sent ${ethers.utils.formatEther(sentTx.value)} Ether to ${sentTx.to}:`,
`https://${network}.etherscan.io/tx/${sentTx.hash}`
);

Expand All @@ -102,17 +99,17 @@ async function main() {
);

// Read from contract
const wethBalance = await wethContract?.balanceOf?.(address);
const wethBalance = await wethContract.balanceOf(address);

print("WETH Balance:", `${ethers.formatEther(wethBalance)} WETH`);
print("WETH Balance:", `${ethers.utils.formatEther(wethBalance)} WETH`);

// 2. Wrap ETH -> WETH
const depositTx = await wethContract?.deposit?.({
value: ethers.parseEther(transactionAmount),
const depositTx = await wethContract.deposit({
value: ethers.utils.parseEther(transactionAmount),
});

print(
`Wrapped ${ethers.formatEther(depositTx.value)} ETH:`,
`Wrapped ${ethers.utils.formatEther(depositTx.value)} ETH:`,
`https://${network}.etherscan.io/tx/${depositTx.hash}`
);
}
Expand Down

0 comments on commit 7e92186

Please sign in to comment.