2018年 9月 の投稿一覧

web3.eth.sendSignedTransactionでエラー「 Uncaught (in promise) Error: Returned error: VM Exception while processing transaction: revert」

web3とethereumjs.TxでsendSignedTransactionをコントラクトに送ったところエラーが発生

Uncaught (in promise) Error: Returned error: VM Exception while processing transaction: revert

modifierなどの条件に引っかかっているだけだった。


require( コンディション );

requireは条件がfalseの場合にcontractの処理実行を停止して
contractの状態をトランザクション実行前に戻します。
そして、残ガスを呼び出し元に返却する。
requireは使った分のガスは消費されるので、注意。

truffle migrate でエラー「 Attempting to run transaction which calls a contract function, but recipient address is not a contract address」

TruffleでリモートのGanecheにコントラクトを配置しようとしたらエラーが発生

Error: Attempting to run transaction which calls a contract function, but recipient address 0x***** is not a contract address


dp$ truffle migrate --network development
Compiling ./contracts/TestToken.sol...
Compiling ./contracts/TestTokenCommons.sol...
Compiling openzeppelin-solidity/contracts/math/SafeMath.sol...
Writing artifacts to ./build/contracts

Using network 'development'.

Error: Attempting to run transaction which calls a contract function, but recipient address 0x**************************** is not a contract address
    at Object.InvalidResponse (/Users/dp/.nodebrew/node/v9.6.1/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:38:1)

Ganecheをリセットしていたので、キャッシュ的なものがTruffleに残ってる?
削除すれば問題なくGanecheにコントラクトをデプロイできた。


rm -r build/contracts/

Warning: Using contract member “balance” inherited from the address type is deprecated.

Solidityのバージョン変更でコンパイルエラー


contractAdminAddress.transfer( this.balance );

エラー全文


Warning: Using contract member "balance" inherited from the address type is deprecated. Convert the contract to "address" type to access the member, for example use "address(contract).balance" instead.

バージョン変更


- contractAdminAddress.transfer( this.balance );
+ contractAdminAddress.transfer( address( this ).balance );

Warning: Invoking events without “emit” prefix is deprecated.

solidityのバージョンを変えたらWarningがでるようになった。


- pragma solidity ^0.4.18;
+ pragma solidity ^0.4.24;

関数の呼び出しと、イベントの呼び出しを区別するためイベントの場合はemitが必要


Compilation warnings encountered:

/Users/dp/Dropbox/Ethereum/dappsTest/contracts/TestToken.sol:83:3: Warning: Invoking events without "emit" prefix is deprecated.
        Approval( msg.sender , _to , _tokenId );
        ^-------------------------------------^

emit Approval( msg.sender , _to , _tokenId );

https://github.com/ethereum/solidity/issues/2877

Metamaskからエクスポートした秘密鍵をweb3jsとethereumjsでインポートしてみる

EthreumJS


var pk = "0f0ed71771c3cb767ce1eca984f....";// Metamaskからエクスポートした秘密鍵文字列
var pu = ethereumjs.Util.privateToPublic("0x" + pk );

console.log( pu.toString("hex") ); // アドレスがMetamaskと一致

// Metamaskからエクスポートしたまま、Prefixがないとダメ
var pu = ethereumjs.Util.privateToPublic("0x" + pk );
Uncaught RangeError: private key length is invalid
    at Object.exports.isBufferLength (assert.js:31)
    at Object.publicKeyCreate (index.js:61)
    at Object.exports.privateToPublic (index.js:356)
    at :1:25

Web3は秘密鍵のチェックがあまい?


web3  = new Web3();

var pk = "0f0ed71771c3cb767ce1eca984f....";// Metamaskからエクスポートした秘密鍵文字列

var account = web3.eth.accounts.privateKeyToAccount( pk );

console.log( accoun );
{address: "0x6C0(Metamaskと違う)", privateKey: "0f0ed71771c3cb767ce1eca984f....", signTransaction: ƒ, sign: ƒ, encrypt: ƒ}

// Metamaskからエクスポートしたまま、Prefixがないとダメ
var account = web3.eth.accounts.privateKeyToAccount( "0x" + pk );

console.log( accoun );
{address: "0xa43d0(Metamaskと一致)", privateKey: "0x0f0ed71771c3cb767ce1eca984f....", signTransaction: ƒ, sign: ƒ, encrypt: ƒ}

ファイルによって「 Invalid JSON RPC response」なんて言われるから
通信してるかと思ったけど、よく調べたらmetamaskが提供してくれるweb3.jsと
自分がローカルで使ってるweb3.jsが結構違ったのでメモ

https://github.com/MetaMask/metamask-extension/blob/develop/package.json
Metamaskの提供してくれるweb3ではaccounts系は提供されていません。


var account = web3.eth.accounts.privateKeyToAccount( "0x" + pk );
inpage.js:1 Uncaught Error: Invalid JSON RPC response: undefined
    at Object.InvalidResponse (inpage.js:1)
    at s.send (inpage.js:1)
    at n.accounts (inpage.js:1)
    at :1:15

web3-1.0系だとprivateKeyToAccountがあります。

https://raw.githubusercontent.com/ethereum/web3.js/v1.0.0-beta.35/dist/web3.js


Accounts.prototype.create = function create(entropy) {
    return this._addAccountFunctions(Account.create(entropy || utils.randomHex(32)));
};
Accounts.prototype.privateKeyToAccount = function privateKeyToAccount(privateKey) {
    return this._addAccountFunctions(Account.fromPrivate(privateKey));
};
Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, callback) {

Windows8タブレット( 64bitなのにUEFIが32bit )にUbuntu18のインストール

macOSXでubuntuとdebianのisoから必要なファイルをUSBにコピーするのですが
iosはMacでマウントできないといわれたので、hdiutilをつかってマウントする。


$ hdiutil attach -nobrowse -nomount debian-9.5.0-amd64-i386-netinst.iso
/dev/disk3              Apple_partition_scheme
/dev/disk3s1            Apple_partition_map
/dev/disk3s2            Apple_HFS
$ mount -t cd9660 /dev/disk3 debian

windowsタブレットはUSBポートが少ないので
USBハブにUSBメモリ、マウス、キーボードをつけて
BIOSメニューからまずは、secureBootの無効化をして起動順を設定しますが、それだと起動しなかったので、Save & ExitにあるBoot OverrideからUSBを指定して起動

USBデバイスがBlk数字としては認識してるけど
fs数字になっていない。。
UEFIのシェルからUSBのBlkをマウントしても中身がみえない。。


mount blk5 usb5
usb5:
ls

Pcirootからのところをよく見ると
USBがGPTじゃなくてMBRになっていました。
USBメモリをフォーマットするときに選択ミスしていたみたい。


map fs*
map blk*

Ethereum – スマートコントラクト作成 – プライベート・ネットワークの作成

https://book.ethereum-jp.net/first_use/connect_to_private_net.html

Ubuntuが良さそうですが、手元にCentos6しかなかったのでとりあえすやってみます。

Ethereumでは以下の3つの形態のP2Pネットワークを構築しブロックチェーンを運用していくことが可能です。

パブリック・ネットワーク:不特定多数のノードのノードが参加し、かつその参加に制限が全くないネットワークです。参加ノードはそのネットワーク上で共有管理されたブロックチェーンに対して自由に、読み取り、トランザクションの発行、マイニングが可能です。仮想通貨としてのEthereumや、多くのパブリックなdAppはこのパブリックネットワーク上で運用されています。
コンソーシアム・ネットワーク:あらかじめ参加を許されたノードのみが参加することが可能なネットワークです。参加を許されるノードは一つの組織のみに管理されたものとは限らず、複数の利害関係が一致しない組織がそれぞれのノードを管理することが通常です。例えば国際送金の管理を行うブロックチェーンを構築したい場合、予め参加を許された複数の金融企業がそれぞれ管理するノードをこのP2Pのネットワークに参加することで、一つの企業にのみ管理されたシステムではない半非中央集権なシステムが構築可能になります。
プライベート・ネットワーク:一つの組織のみに管理されたノードのみが参加することが可能なネットワークです。ネットワークは自身の管理下に置くことが可能になり、中央集権的なP2Pシステムが可能になります。


mkdir eth_private_net

vi eth_private_netg/enesis.json


{
  "config": {
    "chainId": 15
  },
  "nonce": "0x0000000000000042",
  "timestamp": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "extraData": "",
  "gasLimit": "0x8000000",
  "difficulty": "0x4000",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x3333333333333333333333333333333333333333",
  "alloc": {}
}


 geth --datadir ~/eth_private_net init ~/eth_private_net/genesis.json
INFO [02-15|04:00:34] Maximum peer count                       ETH=25 LES=0 total=25
INFO [02-15|04:00:34] Allocated cache and file handles         database=/home/dp/eth_private_net/geth/c
haindata cache=16 handles=16
INFO [02-15|04:00:34] Writing custom genesis block 
INFO [02-15|04:00:34] Persisted trie from memory database      nodes=0 size=0.00B time=6.642µs gcnodes=0 gcsize
=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [02-15|04:00:34] Successfully wrote genesis state         database=chaindata                              
         hash=7b2e8b…7e0432
INFO [02-15|04:00:34] Allocated cache and file handles         database=/home/dp/eth_private_net/geth/l
ightchaindata cache=16 handles=16
INFO [02-15|04:00:34] Writing custom genesis block 
INFO [02-15|04:00:34] Persisted trie from memory database      nodes=0 size=0.00B time=2.335µs gcnodes=0 gcsize
=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [02-15|04:00:34] Successfully wrote genesis state         database=lightchaindata                         
              hash=7b2e8b…7e0432


geth --networkid "15" --nodiscover --datadir ~/eth_private_net/  console 2>> ~/eth_privat
e_net/err.log
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.1-unstable-dc7ca52b/linux-amd64/go1.9.2
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
>

Genesisブロックの確認


> eth.getBlock(0)
{
  difficulty: 16384,
  extraData: "0x",
  gasLimit: 134217728,
  gasUsed: 0,
  hash: "0x7b2e8be699df0d329cc74a99271ff7720e2875cd2c4dd0b419ec60d1fe7e0432",
  logsBloom: "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000",
  miner: "0x3333333333333333333333333333333333333333",
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: "0x0000000000000042",
  number: 0,
  parentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 507,
  stateRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  timestamp: 0,
  totalDifficulty: 16384,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}
> 

Ethereum – スマートコントラクト開発 – 開発環境を構築する

Go Ehtereumのインストール


brew tap ethereum/ethereum
ずらずら
==> Tapping ethereum/ethereum
Cloning into '/usr/local/Homebrew/Library/Taps/ethereum/homebrew-ethereum'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
Tapped 3 formulae (32 files, 33.7KB)


brew install ethereum


npm install -g ethereumjs-testrpc


mba:~ dp$ ll
.e
.eclipse/                     .electrum-ltc/                .esets/
.electron/                    .emulator_console_auth_token  .ethereum_dev/
mba:~ dp$  geth --datadir .ethereum_dev/privatenet init .ethereum_dev/privatenet/genesis.json 
INFO [02-15|11:53:46] Maximum peer count                       ETH=25 LES=0 total=25
INFO [02-15|11:53:46] Allocated cache and file handles         database=/Users/dp/.ethereum_dev/privatenet/geth/chaindata cache=16 handles=16
INFO [02-15|11:53:46] Writing custom genesis block 
INFO [02-15|11:53:46] Persisted trie from memory database      nodes=0 size=0.00B time=19.224µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [02-15|11:53:46] Successfully wrote genesis state         database=chaindata                                           hash=17fa94…cca181
INFO [02-15|11:53:46] Allocated cache and file handles         database=/Users/dp/.ethereum_dev/privatenet/geth/lightchaindata cache=16 handles=16
INFO [02-15|11:53:46] Writing custom genesis block 
INFO [02-15|11:53:46] Persisted trie from memory database      nodes=0 size=0.00B time=4.766µs  gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [02-15|11:53:46] Successfully wrote genesis state         database=lightchaindata                                           hash=17fa94…cca181