类型错误:CryptoJS.SHA256 不是函数

发布于 2025-01-16 17:15:42 字数 1850 浏览 0 评论 0原文

import * as CryptoJS from 'crypto-js';
import * as ecdsa from 'elliptic';
import * as _ from 'lodash'

class Transaction {
    id = null;
    constructor(id, txIns, txOuts) {
        this.id = id;
        this.txIns = txIns;
        this.txOuts = txOuts;
    }
}

function getTransactionId (Transaction)
{
    const txInContent = Transaction.txIns.txOutId + Transaction.txIns.txOutIndex;
//    const txInContent = Transaction.TxIN.map((TxIN) => TxIN.txOutId + TxIN.txOutIndex).reduce((a, b) => a + b, '');
    const txOutContent = Transaction.txOuts.amount + Transaction.txOuts.address;
//    const txOutContent = Transaction.TxOut.map((TxOUT) => txOut.amount + txOut.address).reduce((a, b) => a + b, '');

    return (txInContent + txOutContent).toString();
}


class TxIN {
    txOutId = null;
    txOutIndex = null;
    signature = null;

    constructor(txOutId, txOutIndex, signature) {
        this.txOutId = txOutId;         // Previous transaction ID
        this.txOutIndex = txOutIndex;
        this.signature = signature;     // Sender's signature
    }
}

class TxOUT {
    amount = null;
    address = null;

    constructor(amount, address) {
        this.amount = amount;     // Amount value of output
        this.address = address;   // Receiver's address
    }
}

let testOut = new TxOUT(10, "tb1****************************ntxt");

let testIn = new TxIN("tb******************************tntxt", 0, "gg")

let testTrans = new Transaction("1329***********************1e99ba8", testIn, testOut);


var test = 'GG';

console.log(test);
var t = CryptoJS.SHA256(test);

我在我的 JavaScript 项目中使用 CryptoJS.SHA256() 。但显示“TypeError:CryptoJS.SHA256 不是函数”。我已经导入了“crypto-js”。当我尝试记录加密货币时。我找到了“SHA256:[函数(匿名)]”。但TypeError无法解决。 我有什么错误吗?

import * as CryptoJS from 'crypto-js';
import * as ecdsa from 'elliptic';
import * as _ from 'lodash'

class Transaction {
    id = null;
    constructor(id, txIns, txOuts) {
        this.id = id;
        this.txIns = txIns;
        this.txOuts = txOuts;
    }
}

function getTransactionId (Transaction)
{
    const txInContent = Transaction.txIns.txOutId + Transaction.txIns.txOutIndex;
//    const txInContent = Transaction.TxIN.map((TxIN) => TxIN.txOutId + TxIN.txOutIndex).reduce((a, b) => a + b, '');
    const txOutContent = Transaction.txOuts.amount + Transaction.txOuts.address;
//    const txOutContent = Transaction.TxOut.map((TxOUT) => txOut.amount + txOut.address).reduce((a, b) => a + b, '');

    return (txInContent + txOutContent).toString();
}


class TxIN {
    txOutId = null;
    txOutIndex = null;
    signature = null;

    constructor(txOutId, txOutIndex, signature) {
        this.txOutId = txOutId;         // Previous transaction ID
        this.txOutIndex = txOutIndex;
        this.signature = signature;     // Sender's signature
    }
}

class TxOUT {
    amount = null;
    address = null;

    constructor(amount, address) {
        this.amount = amount;     // Amount value of output
        this.address = address;   // Receiver's address
    }
}

let testOut = new TxOUT(10, "tb1****************************ntxt");

let testIn = new TxIN("tb******************************tntxt", 0, "gg")

let testTrans = new Transaction("1329***********************1e99ba8", testIn, testOut);


var test = 'GG';

console.log(test);
var t = CryptoJS.SHA256(test);

I am using the CryptoJS.SHA256() on my JavaScript project. But is shows the "TypeError: CryptoJS.SHA256 is not a function". And I've already import the 'crypto-js'. When I tried to log the crypto. I found the "SHA256: [Function (anonymous)]". But the TypeError cannot be solved.
Is there something I make a mistake?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您的好友蓝忘机已上羡 2025-01-23 17:15:42

尝试这些:

npm install @types/node --save-dev
安装 NodeJS 定义

npm install --save @types/crypto-js

在 tsconfig.ts 文件中添加以下内容:

"files": [
"./node_modules/@types/node/index.d.ts"
]

import 将库导入到要使用的位置

import * as sha256 from 'crypto-js/sha256';

使用 import或
使用特殊的导入语法

import sha256 = require("crypto-js/sha256");

try these :

npm install @types/node --save-dev
to install NodeJS definitions

npm install --save @types/crypto-js

In tsconfig.ts file add the following:

"files": [
"./node_modules/@types/node/index.d.ts"
]

Import the library where you want to use it with import

import * as sha256 from 'crypto-js/sha256';

or
with speical import syntax

import sha256 = require("crypto-js/sha256");
内心激荡 2025-01-23 17:15:42

尝试像这样得到它:

var SHA256 = require("crypto-js/sha256");
console.log(SHA256('my message'));

这里正在运行演示:
https://replit.com/@slash851/crypto-js-test#索引.js

try to get it like this:

var SHA256 = require("crypto-js/sha256");
console.log(SHA256('my message'));

here is running demo:
https://replit.com/@slash851/crypto-js-test#index.js

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文