'TypeError: import_Wallet.default is not a constructor
I'm making a framework for a cryptocurrency in TypeScript. I went off of a plainenglish tutorial, and took some code from the listed Git repo. It ran fine when I tested it, so I added a file newWallet.ts whose sole purpose is to export a function that makes a new Wallet object, but I get the error listed in the title, pointing to a completely different file NETWORK_WALLET.ts.
Wallet.ts:
import { generateKeyPairSync } from "crypto";
import Chain from "./Chain";
import Transaction from "./Transaction";
class Wallet {
privateKey: string;
publicKey: string;
constructor() {
const keys = generateKeyPairSync("rsa", {
modulusLength: 2048,
publicKeyEncoding: { type: "spki", format: "pem" },
privateKeyEncoding: { type: "pkcs8", format: "pem" },
});
this.privateKey = keys.privateKey;
this.publicKey = keys.publicKey;
}
send(amount: number, receiver: string, blockchain: Chain) {
const transaction = new Transaction(this.publicKey, receiver, amount);
transaction.sign(this);
blockchain.addTransaction(transaction);
}
}
export default Wallet;
NETWORK_WALLET.ts:
import Wallet from "./Wallet";
const NETWORK_WALLET = new Wallet();
export default NETWORK_WALLET;
Transaction.ts (another file the error points to):
import { v4 as uuidv4 } from "uuid";
import { createHash, createSign, createVerify } from "crypto";
import NETWORK_WALLET from "./NETWORK_WALLET";
import Wallet from "./Wallet";
import Chain from "./Chain";
class Transaction {
id: string;
sender: string;
receiver: string;
amount: number;
hash: string;
signature: string;
constructor(senderPubKey: string, receiverPubKey: string, amount: number) {
const id = uuidv4();
const data = senderPubKey + receiverPubKey + amount + id;
const hash = createHash("sha256").update(data).digest("hex");
this.id = id;
this.sender = senderPubKey;
this.receiver = receiverPubKey;
this.amount = amount;
this.hash = hash;
this.signature = "";
}
sign(wallet: Wallet) {
if (wallet.publicKey === this.sender) {
const shaSign = createSign("sha256");
shaSign.update(this.hash).end();
this.signature = shaSign.sign(wallet.privateKey).toString("base64");
}
}
isValid(chain: Chain) {
const sig = Buffer.from(this.signature, "base64");
const verify = createVerify("sha256");
verify.update(this.hash);
const isVerified = verify.verify(this.sender, sig);
const data = this.sender + this.receiver + this.amount + this.id;
const hash = createHash("sha256").update(data).digest("hex");
return (
this.sender &&
this.receiver &&
this.amount &&
(chain.getBalance(this.sender) >= this.amount ||
this.sender === NETWORK_WALLET.publicKey) &&
this.hash === hash &&
isVerified
);
}
}
export default Transaction;
newWallet.ts:
import { getWallet } from '../database';
import Wallet from './Wallet';
export async function create(username: string) {
const newWallet = new Wallet();
const wallet = await getWallet(username);
wallet.balance = 0;
wallet.publicKey = newWallet.publicKey;
wallet.privateKey = newWallet.privateKey;
wallet.save();
console.log(`New wallet for user ${username}`);
console.log(newWallet);
}
Any advice? Anything I've looked for has turned up nothing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
