2021-12-07 12:16:46 -08:00
|
|
|
import { Buffer } from "buffer";
|
|
|
|
import { Keypair, PublicKey, Transaction } from "@solana/web3.js";
|
|
|
|
import { Wallet } from "./provider";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Node only wallet.
|
|
|
|
*/
|
|
|
|
export default class NodeWallet implements Wallet {
|
|
|
|
constructor(readonly payer: Keypair) {}
|
|
|
|
|
2022-06-10 06:54:06 -07:00
|
|
|
static local(): NodeWallet | never {
|
2021-12-07 12:16:46 -08:00
|
|
|
const process = require("process");
|
2022-06-10 06:54:06 -07:00
|
|
|
|
|
|
|
if (!process.env.ANCHOR_WALLET || process.env.ANCHOR_WALLET === "") {
|
|
|
|
throw new Error(
|
|
|
|
"expected environment variable `ANCHOR_WALLET` is not set."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:16:46 -08:00
|
|
|
const payer = Keypair.fromSecretKey(
|
|
|
|
Buffer.from(
|
|
|
|
JSON.parse(
|
|
|
|
require("fs").readFileSync(process.env.ANCHOR_WALLET, {
|
|
|
|
encoding: "utf-8",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2022-06-10 06:54:06 -07:00
|
|
|
|
2021-12-07 12:16:46 -08:00
|
|
|
return new NodeWallet(payer);
|
|
|
|
}
|
|
|
|
|
|
|
|
async signTransaction(tx: Transaction): Promise<Transaction> {
|
|
|
|
tx.partialSign(this.payer);
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
async signAllTransactions(txs: Transaction[]): Promise<Transaction[]> {
|
|
|
|
return txs.map((t) => {
|
|
|
|
t.partialSign(this.payer);
|
|
|
|
return t;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get publicKey(): PublicKey {
|
|
|
|
return this.payer.publicKey;
|
|
|
|
}
|
|
|
|
}
|