@thirdweb-dev/wallets
Version:
<p align="center"> <br /> <a href="https://thirdweb.com"><img src="https://github.com/thirdweb-dev/js/blob/main/legacy_packages/sdk/logo.svg?raw=true" width="200" alt=""/></a> <br /> </p> <h1 align="center">thirdweb Wallet SDK</h1> <p align="center"> <a h
92 lines (88 loc) • 3.38 kB
JavaScript
import { AbstractWallet } from '../../abstract/dist/thirdweb-dev-wallets-evm-wallets-abstract.browser.esm.js';
import { Wallet } from 'ethers';
import '../../../../dist/defineProperty-350fc508.browser.esm.js';
import 'eventemitter3';
import '@thirdweb-dev/sdk';
/**
* Connect to a wallet with a private key stored in [AWS Secrets Manager](https://aws.amazon.com/secrets-manager/).
*
* @example
* To instantiate a wallet with AWS Secrets Manager, you need to gather the necessary secret ID and secret name from AWS.
*
* ```typescript
* import { AwsSecretsManagerWallet } from "@thirdweb-dev/wallets/evm/wallets/aws-secrets-manager";
*
* const wallet = new AwsSecretsManagerWallet({
* secretId: "{{secret-id}}", // ID of the secret value
* secretKeyName: "{{secret-key-name}}", // Name of the secret value
* awsConfig: {
* region: "us-east-1", // Region where your secret is stored
* credentials: {
* accessKeyId: process.env.AWS_ACCESS_KEY_ID, // Add environment variables to store your AWS credentials
* secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, // Add environment variables to store your AWS credentials
* },
* },
* });
* ```
*
* @wallet
*/
class AwsSecretsManagerWallet extends AbstractWallet {
/**
* Create an instance of `AwsSecretsManagerWallet`
* @param options - The `options` object includes the following properties:
*
* ### `secretId`
* The ID of the secret value.
*
* ### `secretKeyName`
* The name of the secret value.
*
* ### `awsConfig`
* The object of type `SecretsManagerClientConfig` from `@aws-sdk/client-secrets-manager` package.
*/
constructor(options) {
super();
this._options = options;
}
/**
* Get [ethers signer](https://docs.ethers.io/v5/api/signer/) of the connected wallet
*/
async getSigner() {
if (!this._signer) {
// construct our promise that will resolve to a signer (eventually)
this._signer = new Promise(async (resolve, reject) => {
try {
const {
GetSecretValueCommand,
SecretsManagerClient
} = await import('@aws-sdk/client-secrets-manager');
const client = new SecretsManagerClient(this._options.awsConfig || {});
const res = await client.send(new GetSecretValueCommand({
SecretId: this._options.secretId,
VersionStage: "AWSCURRENT" // VersionStage defaults to AWSCURRENT if unspecified
}));
if (!res || !res.SecretString) {
throw new Error(`No secret found at ${this._options.secretId}`);
}
let privateKey;
try {
privateKey = JSON.parse(res.SecretString)[this._options.secretKeyName];
} catch {
throw new Error(`Secret ${this._options.secretId} is not a valid JSON object! Please convert secret to a JSON object with key ${this._options.secretKeyName}.`);
}
if (!privateKey) {
throw new Error(`Secret ${this._options.secretId} does not have key ${this._options.secretKeyName}`);
}
resolve(new Wallet(privateKey));
} catch (err) {
// remove the cached promise so we can try again
this._signer = undefined;
reject(err);
}
});
}
return this._signer;
}
}
export { AwsSecretsManagerWallet };