/
private-key.ts
53 lines (49 loc) · 1.41 KB
/
private-key.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
48
49
50
51
52
53
import { AbstractWallet } from "./abstract";
import { ethers } from "ethers";
import { ChainOrRpcUrl, getChainProvider } from "@thirdweb-dev/sdk";
/**
* Wallet interface to connect using a Private Key
*
* @example
* ```ts
* import { PrivateKeyWallet } from "@thirdweb-dev/wallets";
*
* // can be any ethers.js signer
* const privateKey = process.env.PRIVATE_KEY;
* const wallet = new PrivateKeyWallet(privateKey);
* ```
*
* @wallet
*/
export class PrivateKeyWallet extends AbstractWallet {
private _signer: ethers.Signer;
/**
* Create instance of `PrivateKeyWallet`
*
* @param privateKey - The private key to use for signing transactions.
*
* @param chain - The chain or rpc url to connect to when querying the blockchain directly through this wallet.
*
* @param secretKey -
* Provide `secretKey` to use the thirdweb RPCs for given `chain`
*
* You can create a secret key from [thirdweb dashboard](https://thirdweb.com/create-api-key).
*/
constructor(privateKey: string, chain?: ChainOrRpcUrl, secretKey?: string) {
super();
this._signer = new ethers.Wallet(
privateKey,
chain
? getChainProvider(chain, {
secretKey,
})
: undefined,
);
}
/**
* Get the [ethers.js signer](https://docs.ethers.io/v5/api/signer/) object used by the wallet
*/
async getSigner(): Promise<ethers.Signer> {
return this._signer;
}
}