Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (12.7k points)

I have this javascript object :

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 } }

When I try to do a res.json on it, I seem to get this error :

TypeError: Converting circular structure to JSON

Some code that may be relevant :

// 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");

});

the createTransaction method which is mentioned above, looks like this :

  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;

  }

the newTransaction method :

  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);

  }

Which calls the generateTransaction method below :

 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;

  }

Which in turn calls the signTransaction method :

  static signTransaction(transaction, senderWallet) {

    transaction.input = {

      timestamp: Date.now(),

      from: senderWallet.publicKey,

      signature: senderWallet.sign(ChainUtil.hash(transaction.output))

    };

  }

the chainUtil hash function looks like this :

static hash(data){

    return SHA256(JSON.stringify(data)).toString();

}

The sign method on the wallet class :

 sign(dataHash){

    return this.keyPair.sign(dataHash);

  }

they all return and I get the object I mentioned at the top of this question. I'm not seeing how the issue is a circular error at all. Please, educate me.

1 Answer

0 votes
by (29.5k points)
edited by

 Elliptic only allows you to work with hex strings... so the change to make this work was in my wallet code, as follows :

this.keyPair.sign(dataHash).toHex();

Instead of

this.keyPair.sign(dataHash);

Know more about Blockchain in detail. Enroll in IBM Blockchain Certification now to take that first step.

Browse Categories

...