Elliptic:TypeError:将圆形结构转换为JSON

时间:2019-04-04 02:15:23

标签: javascript blockchain

开始之前的相关信息:我已经发现了stackoverflow,并且发现了相似的标题问题,但是我不认为它们是我的重复项,因为它们之间存在关键差异,它们似乎具有包含引用的对象给自己,造成圈子。我没有那个<-请阅读本文,而不是立即将其标记为欺骗对象-我需要与此相关的真正帮助。

编辑:我已经找到了答案,但是stackoverflow不会让我发布答案几天,结果是椭圆形只能接受十六进制值-这意味着您必须这样称呼它:

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方法:

  sign(dataHash){
    return this.keyPair.sign(dataHash);
  }

它们全部返回,我得到了我在此问题顶部提到的对象。我根本看不到问题是怎么回事。请教育我。

1 个答案:

答案 0 :(得分:0)

我找到了答案,原来Elliptic只允许您使用十六进制字符串...因此,使此项工作生效的更改在我的钱包代码中,如下所示:

this.keyPair.sign(dataHash);

代替

_readCardResponse.expiry = _ccResponse["expiry"];
_readCardResponse.name = _ccResponse["name"];
_readCardResponse.token = _ccResponse["token"];