Ethreum - raw transaction

トランザクションに署名をして、別のサーバーなどで署名付きトランザクションをブロードキャストできる。

１．オフラインで署名付きraw transactionを生成
２．USBでオンラインのサーバーにコピー
３．署名付きraw transactionをブロードキャスト

参考 [How to create Raw Transactions in Ethereum ? — Part 1]
(https://medium.com/blockchain-musings/how-to-create-raw-transactions-in-ethereum-part-1-1df91abdba7c)
> In this post, we will find out how to create, sign and send raw transactions between accounts using NodeJs.

・gasPriceを最大に設定する
・gasLimitを最大に設定する
・nonceの順番に送信しないとリジェクトされる
　（同じアカウントでnonce指定のトランザクションを先に送信してしまうと、nonceの整合性があわなくなり、古いnonce番号のトランザクションはリジェクトされてしまう）

nonce指定のリジェクト検証
オフラインの同じアカウントで２つトランザクションを生成し、順番を変えて送信してみる。

```console
eth.getTransactionCount(<account>)
```

##get private key from keyfile

```js
const keythereum = require('keythereum');
const address = '0x9e378d2365b7657ebb0f72ae402bc08812022211';
const datadir = '/home/administrator/ethereum/data';
const password = 'password';
let str;
keythereum.importFromFile(address, datadir, function (keyObject) {
  keythereum.recover(password, keyObject, function (privateKey) {
    console.log(privateKey.toString('hex'));
//05a20149c1c76ae9da8457435bf0224a4f81801da1d8204cb81608abe8c112ca  
  });
});
```

##create raw transaction

```js
const ethTx = require('ethereumjs-tx');

const txParams = {
  nonce: '0x6', // Replace by nonce for your account on geth node
  gasPrice: '0x09184e72a000', 
  gasLimit: '0x30000',
  to: '0xfa3caabc8eefec2b5e2895e5afbf79379e7268a7', 
  value: '0x00'
};
// Transaction is created
const tx = new ethTx(txParams);
const privKey = Buffer.from('05a20149c1c76ae9da8457435bf0224a4f81801da1d8204cb81608abe8c112ca', 'hex');
// Transaction is signed
tx.sign(privKey);
const serializedTx = tx.serialize();
const rawTx = '0x' + serializedTx.toString('hex');
console.log(rawTx)
```

##send raw transaction

```js
eth.sendRawTransaction(<rawTx string>)
```

#keythreum

参考 https://github.com/ethereumjs/keythereum#key-import

##install keythereum

```console
npm install keythereum
```

##key creation

```js
// optional private key and initialization vector sizes in bytes
// (if params is not passed to create, keythereum.constants is used by default)
var params = { keyBytes: 32, ivBytes: 16 };

// synchronous
var dk = keythereum.create(params);
// dk:
{
    privateKey: <Buffer ...>,
    iv: <Buffer ...>,
    salt: <Buffer ...>
}

// asynchronous
keythereum.create(params, function (dk) {
    // do stuff!
});
```

##key export

```js
// Note: if options is unspecified, the values in keythereum.constants are used.
var options = {
  kdf: "pbkdf2",
  cipher: "aes-128-ctr",
  kdfparams: {
    c: 262144,
    dklen: 32,
    prf: "hmac-sha256"
  }
};

// synchronous
var keyObject = keythereum.dump(password, dk.privateKey, dk.salt, dk.iv, options);
// keyObject:
{
  address: "008aeeda4d805471df9b2a5b0f38a0c3bcba786b",
  Crypto: {
    cipher: "aes-128-ctr",
    ciphertext: "5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46",
    cipherparams: {
      iv: "6087dab2f9fdbbfaddc31a909735c1e6"
    },
    mac: "517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2",
    kdf: "pbkdf2",
    kdfparams: {
      c: 262144,
      dklen: 32,
      prf: "hmac-sha256",
      salt: "ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"
    }
  },
  id: "e13b209c-3b2f-4327-bab0-3bef2e51630d",
  version: 3
}
```