Browse Source

ts: verbose error for missing `ANCHOR_WALLET` in `NodeWallet.local()` (#1958)

Matthew Callens 3 years ago
parent
commit
179711bacc
3 changed files with 24 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 9 1
      ts/src/nodewallet.ts
  3. 14 0
      ts/tests/program-common.spec.ts

+ 1 - 0
CHANGELOG.md

@@ -27,6 +27,7 @@ com/project-serum/anchor/pull/1841)).
 * ts: Add `program.coder.types` for encoding/decoding user-defined types ([#1931](https://github.com/project-serum/anchor/pull/1931)).
 * ts: Add `program.coder.types` for encoding/decoding user-defined types ([#1931](https://github.com/project-serum/anchor/pull/1931)).
 * client: Add send_with_spinner_and_config function to RequestBuilder ([#1926](https://github.com/project-serum/anchor/pull/1926)).
 * client: Add send_with_spinner_and_config function to RequestBuilder ([#1926](https://github.com/project-serum/anchor/pull/1926)).
 * ts: Implement a coder for SPL associated token program ([#1939](https://github.com/project-serum/anchor/pull/1939)).
 * ts: Implement a coder for SPL associated token program ([#1939](https://github.com/project-serum/anchor/pull/1939)).
+* ts: verbose error for missing `ANCHOR_WALLET` variable when using `NodeWallet.local()` ([#1958](https://github.com/project-serum/anchor/pull/1958)).
 
 
 ### Fixes
 ### Fixes
 
 

+ 9 - 1
ts/src/nodewallet.ts

@@ -8,8 +8,15 @@ import { Wallet } from "./provider";
 export default class NodeWallet implements Wallet {
 export default class NodeWallet implements Wallet {
   constructor(readonly payer: Keypair) {}
   constructor(readonly payer: Keypair) {}
 
 
-  static local(): NodeWallet {
+  static local(): NodeWallet | never {
     const process = require("process");
     const process = require("process");
+
+    if (!process.env.ANCHOR_WALLET || process.env.ANCHOR_WALLET === "") {
+      throw new Error(
+        "expected environment variable `ANCHOR_WALLET` is not set."
+      );
+    }
+
     const payer = Keypair.fromSecretKey(
     const payer = Keypair.fromSecretKey(
       Buffer.from(
       Buffer.from(
         JSON.parse(
         JSON.parse(
@@ -19,6 +26,7 @@ export default class NodeWallet implements Wallet {
         )
         )
       )
       )
     );
     );
+
     return new NodeWallet(payer);
     return new NodeWallet(payer);
   }
   }
 
 

+ 14 - 0
ts/tests/program-common.spec.ts

@@ -2,6 +2,7 @@ import BN from "bn.js";
 import bs58 from "bs58";
 import bs58 from "bs58";
 import { PublicKey } from "@solana/web3.js";
 import { PublicKey } from "@solana/web3.js";
 
 
+import NodeWallet from "../src/nodewallet";
 import { translateAddress } from "../src/program/common";
 import { translateAddress } from "../src/program/common";
 
 
 describe("program/common", () => {
 describe("program/common", () => {
@@ -53,4 +54,17 @@ describe("program/common", () => {
       expect(func).toThrow();
       expect(func).toThrow();
     });
     });
   });
   });
+
+  describe("NodeWallet", () => {
+    it("should throw an error when ANCHOR_WALLET is unset", () => {
+      const oldValue = process.env.ANCHOR_WALLET;
+      delete process.env.ANCHOR_WALLET;
+
+      expect(() => NodeWallet.local()).toThrowError(
+        "expected environment variable `ANCHOR_WALLET` is not set."
+      );
+
+      process.env.ANCHOR_WALLET = oldValue;
+    });
+  });
 });
 });