1.1.17 • Published 6 months ago

@turnkey/ethers v1.1.17

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
6 months ago

@turnkey/ethers

npm

Turnkey Signer for Ethers.

If you need a lower-level, fully typed HTTP client for interacting with Turnkey API, check out @turnkey/http.

API Docs: https://docs.turnkey.com/

Getting started

$ npm install ethers @turnkey/ethers
import { ethers } from "ethers";
import { TurnkeySigner } from "@turnkey/ethers";
import { TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";

async function main() {
  const network = "goerli";
  const provider = new ethers.providers.InfuraProvider(network);

  const turnkeyClient = new TurnkeyClient(
    {
      baseUrl: "https://api.turnkey.com",
    },
    // This uses API key credentials.
    // If you're using passkeys, use `@turnkey/webauthn-stamper` to collect webauthn signatures:
    // new WebauthnStamper({...options...})
    new ApiKeyStamper({
      apiPublicKey: "...",
      apiPrivateKey: "...",
    }),
  );

  // Initialize a Turnkey Signer
  const turnkeySigner = new TurnkeySigner({
    client: turnkeyClient,
    organizationId: "...",
    signWith: "...",
  });

  // Connect it with a Provider (https://docs.ethers.org/v6/api/providers/)
  const connectedSigner = turnkeySigner.connect(provider);

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

  console.log(`Network\n\t${network} (chain ID ${chainId})`);
  console.log(`Address\n\t${address}`);
  console.log(`Balance\n\t${String(balance)}`);
  console.log(`Transaction count\n\t${transactionCount}`);

  const transactionRequest = {
    to: "0x2Ad9eA1E677949a536A270CEC812D6e868C88108",
    value: ethers.utils.parseEther("0.0001"),
    type: 2,
  };

  const signedTx = await connectedSigner.signTransaction(transactionRequest);

  console.log(`Signed transaction\n\t${signedTx}`);

  if (balance.isZero()) {
    let warningMessage =
      "\nWarning: the transaction won't be broadcasted because your account balance is zero.\n";
    if (network === "goerli") {
      warningMessage +=
        "Use https://goerlifaucet.com/ to request funds on Goerli, then run the script again.\n";
    }

    console.warn(warningMessage);
    return;
  }

  const sentTx = await connectedSigner.sendTransaction(transactionRequest);

  console.log(
    `Transaction sent!\n\thttps://${network}.etherscan.io/tx/${sentTx.hash}`,
  );
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});

Testing (Local)

See .env.example to get your local testing environment right. Run pnpm jest to run the tests.

More examples

ExampleDescription
with-ethersCreate a new Ethereum address, then sign and broadcast a transaction using the Ethers signer with Infura
with-gnosisCreate new Ethereum addresses, configure a 3/3 Gnosis safe, and create + execute a transaction from it
with-uniswapSign and broadcast a Uniswap v3 trade using the Ethers signer with Infura
with-nonce-managerCreate a new Ethereum address, then sign and broadcast multiple transactions in a sequential or optimistic manner.
sweeperSweep funds from one address to a different address
deployerCompile and deploy a smart contract

See also

  • @turnkey/http: lower-level fully typed HTTP client for interacting with Turnkey API
1.1.16

6 months ago

1.1.17

6 months ago

1.1.15

6 months ago

1.1.14

6 months ago

1.1.13

7 months ago

1.1.12

7 months ago

1.1.11

7 months ago

1.1.10

7 months ago

1.1.9

8 months ago

1.1.8

9 months ago

1.1.7

9 months ago

1.1.6

9 months ago

1.1.5

10 months ago

1.1.4

10 months ago

1.1.3

11 months ago

1.1.2

11 months ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.21

12 months ago

1.0.20

12 months ago

1.1.1

12 months ago

1.1.0

12 months ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.9

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

2 years ago

1.0.0

2 years ago

0.19.8

2 years ago

0.19.9

2 years ago

0.19.7

2 years ago

0.19.5

2 years ago

0.19.6

2 years ago

0.19.4

2 years ago

0.19.3

2 years ago

0.19.0

2 years ago

0.19.1

2 years ago

0.19.2

2 years ago

0.18.1

2 years ago

0.17.2

2 years ago

0.16.3

2 years ago

0.18.2

2 years ago

0.17.3

2 years ago

0.16.4

2 years ago

0.18.3

2 years ago

0.17.4

2 years ago

0.16.5

2 years ago

0.16.6

2 years ago

0.16.7

2 years ago

0.16.8

2 years ago

0.17.0

2 years ago

0.18.0

2 years ago

0.17.1

2 years ago

0.16.2

2 years ago

0.16.0

2 years ago

0.16.1

2 years ago

0.11.0

2 years ago

0.12.0

2 years ago

0.13.0

2 years ago

0.14.0

2 years ago

0.13.1

2 years ago

0.15.0

2 years ago

0.14.1

2 years ago

0.13.2

2 years ago

0.9.0-beta.0

2 years ago

0.10.0

2 years ago

0.8.0-beta.0

2 years ago

0.8.1-beta.0

2 years ago

0.8.0-beta.1

2 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.7.1-beta.0

2 years ago

0.6.0-beta.0

2 years ago

0.5.0

2 years ago

0.7.0

2 years ago

0.6.0

2 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago