'use strict'

const expect = require('chai').expect
const solc = require('solc')
const config = require('../config')

describe('Sign and Broadcast transaction :: ', () => {

  const SampleContract = `
contract SampleContract {
  function add(int a, int b) constant returns (int sum) {
    sum = a + b;
  }
}
  `

  const compiled = solc.compile(SampleContract, 1).contracts['SampleContract']
  const abi = JSON.parse(compiled.interface)

  const tx = {
    address: '',
    data: compiled.bytecode.toUpperCase(),
    fee: 12,
    gas_limit: 223,
    input: {
      address: config.account.address,
      amount: 100,
      sequence: 9
    }
  }

  const txForSigning = {
    chain_id: '',
    tx: [
      2, {
        address: tx.address,
        data: tx.data,
        fee: tx.fee,
        gas_limit: tx.gas_limit,
        input: {
          address: tx.input.address,
          amount: tx.input.amount,
          sequence: tx.input.sequence
        }
      }
    ]
  }

  before(() => {
    return global.erisdb
      .blockchain
      .getChainId()
      .then((chainId) => {
        txForSigning.chain_id = chainId
      })
  })

  it('should sign transaction', () => {
    return global.erisdb
      .transactions
      .sign(txForSigning, config.account.privKey)
      .then((signed) => {
        expect(signed).to.be.an('array')
          .and.to.have.lengthOf(2)

        tx.input.signature = signed
        // Adding public key
        tx.input.pub_key = [1, config.account.pubKey]
      })
  })

  it('should broadcastTx', () => {
    return global.erisdb
      .transactions
      .broadcastTx(tx)
      .then((info) => {
        expect(info).to.be.an('object')
          .and.to.contain.all.keys([
            'tx_hash', 'creates_contract', 'contract_addr'
          ])
      })
  })
})

@@ -437,21 +437,424 @@
 %0A%0A  
-const
+let tx%0A  let txForSigning%0A  let chain_id%0A  let sequence%0A%0A  before(() =%3E %7B%0A    return global.erisdb%0A      .blockchain%0A      .getChainId()%0A      .then((chainId) =%3E %7B%0A        chain_id = chainId%0A      %7D)%0A  %7D)%0A%0A  before(() =%3E %7B%0A    return global.erisdb%0A      .accounts%0A      .getAccount(config.account.address)%0A      .then((account) =%3E %7B%0A        sequence = account.sequence + 1%0A      %7D)%0A  %7D)%0A%0A  before(() =%3E %7B%0A   
  tx = %7B%0A
     
@@ -845,24 +845,26 @@
 %0A    tx = %7B%0A
+  
     address:
@@ -864,24 +864,26 @@
 ddress: '',%0A
+  
     data: co
@@ -913,24 +913,26 @@
 Case(),%0A    
+  
 fee: 12,%0A   
@@ -928,24 +928,26 @@
 ee: 12,%0A    
+ 
+ 
 gas_limit: 2
@@ -950,24 +950,26 @@
 t: 223,%0A    
+  
 input: %7B%0A   
@@ -961,24 +961,26 @@
    input: %7B%0A
+  
       addres
@@ -1008,24 +1008,26 @@
 ress,%0A      
+  
 amount: 100,
@@ -1029,24 +1029,26 @@
  100,%0A      
+  
 sequence: 9%0A
@@ -1049,28 +1049,35 @@
 ce: 
-9%0A
+sequence%0A 
     
+ 
 %7D%0A
+  
   %7D%0A%0A  
-const
+ 
  txF
@@ -1090,24 +1090,26 @@
 ing = %7B%0A    
+ 
+ 
 chain_id: ''
@@ -1106,20 +1106,28 @@
 ain_id: 
-'',%0A
+chain_id,%0A  
     tx: 
@@ -1128,16 +1128,18 @@
   tx: %5B%0A
+  
       2,
@@ -1141,32 +1141,34 @@
    2, %7B%0A        
+  
 address: tx.addr
@@ -1176,24 +1176,26 @@
 ss,%0A        
+  
 data: tx.dat
@@ -1201,24 +1201,26 @@
 ta,%0A        
+ 
+ 
 fee: tx.fee,
@@ -1220,16 +1220,18 @@
 tx.fee,%0A
+  
         
@@ -1255,16 +1255,18 @@
 _limit,%0A
+  
         
@@ -1276,32 +1276,34 @@
 ut: %7B%0A          
+  
 address: tx.inpu
@@ -1305,32 +1305,34 @@
 .input.address,%0A
+  
           amount
@@ -1352,32 +1352,34 @@
 ount,%0A          
+  
 sequence: tx.inp
@@ -1399,187 +1399,38 @@
     
-   %7D%0A
      
- 
 %7D%0A    
-%5D%0A  %7D%0A%0A  before(() =%3E %7B%0A    return global.erisdb%0A      .blockchain%0A      .getChainId()%0A      .then((chainId) =%3E %7B%0A        txForSigning.chain_id = chainId%0A      %7D)
+    %7D%0A      %5D%0A    %7D
 %0A  %7D

fix sign transaction