javascript - 椭圆:TypeError:将圆形结构转换为 JSON
问题描述
开始之前的相关信息:我已经搜索了stackoverflow,并且发现了类似的标题问题,但我不相信它们是我的重复,因为它们有一个关键的区别,它们似乎有包含对自己的引用的对象,造成圈子。我没有那个。<-- 请阅读此内容,而不是立即将其标记为受骗者 - 我需要真正的帮助。
编辑:我找到了答案,但是 stackoverflow 不会让我发布几天的答案,结果 elliptic 只能接受十六进制值 - 这意味着你必须这样称呼它:
this.keyPair.sign(dataHash).toHex();
将问题留给其他可能遇到此问题的人。
我有这个 javascript 对象:
Transaction {
id: '2ec34280-567d-11e9-9685-4903db7a1a5b',
type: 'TRANSACTION',
input:
{ timestamp: 1554343126696,
from:
'6db64cd01c27a723395a833d72b3cc81f9110b5ffb34429d91f45fcbfd87ec9b',
signature:
Signature {
eddsa: [EDDSA],
_R: [Point],
_S: [BN],
_Rencoded: [Array],
_Sencoded: undefined } },
output: { to: 'rand-address', amount: 9, fee: 1 } }
当我尝试对其执行 res.json 时,我似乎收到此错误:
TypeError: Converting circular structure to JSON
一些可能相关的代码:
// Endpoint that a user accesses (the start of the event)
app.post("/transact", (req, res) => {
const { to, amount, type } = req.body;
const transaction = wallet.createTransaction(
to,
amount,
type,
blockchain,
transactionPool
);
res.redirect("/transactions");
});
上面提到的 createTransaction 方法如下所示:
createTransaction(to, amount, type, blockchain, transactionPool) {
this.balance = this.getBalance(blockchain);
if (amount > balance) {
console.log(
`Amount: ${amount} exceeds the current balance: ${this.balance}`
);
return;
}
let transaction = Transaction.newTransaction(this, to, amount, type);
transactionPool.addTransaction(transaction);
return transaction;
}
newTransaction 方法:
static newTransaction(senderWallet, to, amount, type) {
if (amount + TRANSACTION_FEE > senderWallet.balance) {
console.log(`Not enough balance`);
return;
}
return Transaction.generateTransaction(senderWallet, to, amount, type);
}
它调用下面的 generateTransaction 方法:
static generateTransaction(senderWallet, to, amount, type) {
const transaction = new this();
transaction.type = type;
transaction.output = {
to: to,
amount: amount - TRANSACTION_FEE,
fee: TRANSACTION_FEE
};
Transaction.signTransaction(transaction, senderWallet);
return transaction;
}
进而调用 signTransaction 方法:
static signTransaction(transaction, senderWallet) {
transaction.input = {
timestamp: Date.now(),
from: senderWallet.publicKey,
signature: senderWallet.sign(ChainUtil.hash(transaction.output))
};
}
chainUtil 哈希函数如下所示:
static hash(data){
return SHA256(JSON.stringify(data)).toString();
}
钱包类的签名方法:
sign(dataHash){
return this.keyPair.sign(dataHash);
}
他们都回来了,我得到了我在这个问题顶部提到的对象。我根本没有看到这个问题是一个循环错误。请教教我。
解决方案
我找到了答案,原来 Elliptic 只允许您使用十六进制字符串...所以使这项工作的更改在我的钱包代码中,如下所示:
this.keyPair.sign(dataHash).toHex();
代替
this.keyPair.sign(dataHash);
推荐阅读
- javascript - 从 chrome 导出下载历史
- javascript - 如何根据下拉菜单中的选择呈现条形图?
- javascript - Service Worker - 是否知道 Service Worker 已完成所有后台 - 同步标签?
- python - 制作图像的副本
- python - 如何在 discord.py 中很好地嵌入消息?
- node.js - 当我路由器获取主页“/”时,节点 js 中未定义内容
- javascript - 检查数组是否包含javascript中的数组
- python - SQLite3 命令在 Python 中的函数内部时有效吗?
- javascript - 如何在Javascript中使对象不可变
- node.js - 在 Node.js 中使用自定义传输层协议