github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/docs/reference/tm2-js-client/wallet.md (about)

     1  ---
     2  id: tm2-js-wallet
     3  ---
     4  
     5  # Wallet
     6  
     7  A `Wallet` is a user-facing API that is used to interact with an account. A `Wallet` instance is tied to a single key
     8  pair and essentially wraps the given `Provider` for that specific account.
     9  
    10  A wallet can be generated from a randomly generated seed, a private key, or instantiated using a Ledger device.
    11  
    12  Using the `Wallet`, users can easily interact with the Tendermint2 chain using their account without having to worry
    13  about account management.
    14  
    15  ## Initialization
    16  
    17  ### createRandom
    18  
    19  Generates a private key-based wallet, using a random seed
    20  
    21  #### Parameters
    22  
    23  * `options?` **AccountWalletOption** the account options
    24  
    25  Returns **Promise<Wallet\>**
    26  
    27  #### Usage
    28  
    29  ```ts
    30  const wallet: Wallet = await Wallet.createRandom();
    31  // random wallet created
    32  ```
    33  
    34  ### fromMnemonic
    35  
    36  Generates a bip39 mnemonic-based wallet
    37  
    38  #### Parameters
    39  
    40  * `mnemonic` **string** the bip39 mnemonic
    41  * `options?` **CreateWalletOptions** the wallet generation options
    42  
    43  Returns **Promise<Wallet\>**
    44  
    45  #### Usage
    46  
    47  ```ts
    48  const mnemonic: string = // ...
    49  const wallet: Wallet = await Wallet.fromMnemonic(mnemonic);
    50  // wallet created from mnemonic
    51  ```
    52  
    53  ### fromPrivateKey
    54  
    55  Generates a private key-based wallet
    56  
    57  #### Parameters
    58  
    59  * `privateKey` **string** the private key
    60  * `options?` **AccountWalletOption** the wallet generation options
    61  
    62  Returns **Promise<Wallet\>**
    63  
    64  #### Usage 
    65  
    66  ```ts
    67  // Generate the private key from somewhere
    68  const {publicKey, privateKey} = await generateKeyPair(
    69      entropyToMnemonic(generateEntropy()),
    70      index ? index : 0
    71  );
    72  
    73  const wallet: Wallet = await Wallet.fromPrivateKey(privateKey);
    74  // wallet created from private key
    75  ```
    76  
    77  ### fromLedger
    78  
    79  Creates a Ledger-based wallet
    80  
    81  #### Parameters
    82  
    83  * `connector` **LedgerConnector** the Ledger device connector
    84  * `options?` **CreateWalletOptions** the wallet generation options
    85  
    86  Returns **Wallet**
    87  
    88  #### Usage
    89  
    90  ```ts
    91  const connector: LedgerConnector = // ...
    92  
    93  const wallet: Wallet = await Wallet.fromLedger(connector);
    94  // wallet created from Ledger device connection
    95  ```
    96  
    97  ## Provider Methods
    98  
    99  ### connect
   100  
   101  Connects the wallet to the specified Provider
   102  
   103  #### Parameters
   104  
   105  * `provider` **Provider** the active Provider, if any
   106  
   107  #### Usage
   108  
   109  ```ts
   110  const provider: Provider = // ...
   111  
   112      wallet.connect(provider);
   113  // Provider connected to Wallet
   114  ```
   115  
   116  ### getProvider
   117  
   118  Returns the connected provider, if any
   119  
   120  Returns **Provider**
   121  
   122  #### Usage
   123  
   124  ```ts
   125  wallet.getProvider();
   126  // connected provider, if any (undefined if not)
   127  ```
   128  
   129  ## Account Methods
   130  
   131  ### getAddress
   132  
   133  Fetches the address associated with the wallet
   134  
   135  Returns **Promise<string\>**
   136  
   137  #### Usage
   138  
   139  ```ts
   140  await wallet.getAddress();
   141  // "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"
   142  ```
   143  
   144  ### getSequence
   145  
   146  Fetches the account sequence for the wallet
   147  
   148  #### Parameters
   149  
   150  * `height` **number** the block height (optional, default `latest`)
   151  
   152  #### Usage
   153  
   154  ```ts
   155  await wallet.getSequence();
   156  // 42
   157  ```
   158  
   159  Returns **Promise<number\>**
   160  
   161  ### getAccountNumber
   162  
   163  Fetches the account number for the wallet. Errors out if the
   164  account is not initialized
   165  
   166  #### Parameters
   167  
   168  * `height` **number** the block height (optional, default `latest`)
   169  
   170  Returns **Promise<number\>**
   171  
   172  #### Usage
   173  
   174  ```ts
   175  await wallet.getAccountNumber();
   176  // 10
   177  ```
   178  
   179  ### getBalance
   180  
   181  Fetches the account balance for the specific denomination
   182  
   183  #### Parameters
   184  
   185  * `denomination` **string** the fund denomination (optional, default `ugnot`)
   186  
   187  Returns **Promise<number\>**
   188  
   189  #### Usage
   190  
   191  ```ts
   192  await wallet.getBalance('ugnot');
   193  // 5000
   194  ```
   195  
   196  ### getGasPrice
   197  
   198  Fetches the current (recommended) average gas price
   199  
   200  Returns **Promise<number\>**
   201  
   202  #### Usage
   203  
   204  ```ts
   205  await wallet.getGasPrice();
   206  // 63000
   207  ```
   208  
   209  ### estimateGas
   210  
   211  Estimates the gas limit for the transaction
   212  
   213  #### Parameters
   214  
   215  * `tx` **Tx** the transaction that needs estimating
   216  
   217  Returns **Promise<number\>**
   218  
   219  #### Usage
   220  
   221  ```ts
   222  const tx: Tx = // ...
   223  
   224      await wallet.estimateGas(tx);
   225  // 120000
   226  ```
   227  
   228  ### signTransaction
   229  
   230  Generates a transaction signature, and appends it to the transaction
   231  
   232  #### Parameters
   233  
   234  * `tx` **Tx** the transaction to be signed
   235  
   236  Returns **Promise<Tx\>**
   237  
   238  #### Usage
   239  
   240  ```ts
   241  const tx: Tx = // ...
   242  
   243      await wallet.signTransaction(tx);
   244  // transaction with appended signature
   245  ```
   246  
   247  ### sendTransaction
   248  
   249  Signs and sends the transaction. Returns the transaction hash (base-64)
   250  
   251  #### Parameters
   252  
   253  * `tx` **Tx** the unsigned transaction
   254  
   255  Returns **Promise<string\>**
   256  
   257  #### Usage
   258  
   259  ```ts
   260  await wallet.sendTransaction(tx);
   261  // returns the transaction hash
   262  ```
   263  
   264  ### getSigner
   265  
   266  Returns the associated signer
   267  
   268  Returns **Signer**
   269  
   270  #### Usage
   271  
   272  ```ts
   273  wallet.getSigner(tx);
   274  // Signer instance
   275  ```