Go-Ethereum
通過 RPC 或 IPC 發送交易?
最近我正在嘗試通過 RPC/IPC 與私有乙太坊區塊鏈通信以創建交易,但我看到的是我無法通過 RPC 進行此操作,因為: - 我之前總是需要解鎖我的帳戶,但我無法做到通過 RPC:
curl 127.0.0.1:8545 -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"personal_unlockAccount","params": ["0xWALLET" ,"",null],"id":1}' {"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"The method personal_unlockAccount does not exist/is not available"}}
- 我看到通過 IPC 有一個替代方案,所以我嘗試了:
echo ‘{“jsonrpc”:“2.0”,“method”:“eth_accounts”,“params”:
$$ $$“id”:1}’ | nc -U /path/datadir/geth.ipc 3ef {“jsonrpc”:“2.0”,“id”:1,“result”:$$ “0xWALLET1”,“0xWALLET2” $$}
好的,所以 IPC 工作正常,現在我嘗試製作一個 TX:
echo '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{ from: "0xWALLET1", to: "0xWALLET2", value: 1000000000000000000, data: "0x10", gas: 1000000}],"id":1}' | nc -U /path/datadir/geth.ipc {"jsonrpc":"2.0","error":{"code":-32600,"message":"invalid character 'f' looking for beginning of object key string"}}
我試圖弄亂參數並遵循 JSON 格式
https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sendtransaction
但我仍然無法弄清楚如何正確進行此交易。
要
personal.unlock
通過 RPC 使用 API,您需要從命令行啟用它--rpcapi
。此處的範例:如何通過 JSON-RPC 創建新帳戶?:geth --rpc --rpcapi "db,eth,net,web3,personal"
.要使 IPC 工作,請嘗試使用 "" 作為 value 和 gas
{ from: "0xWALLET1", to: "0xWALLET2", value: "1000000000000000000", data: "0x10", gas: "1000000"}
。
您收到該錯誤的原因是您需要將所有參數標籤和值放在引號中(例如 from -> “from”)。
例子:
echo '{"jsonrpc":"2.0","method":"personal_unlockAccount","params": ["0xfe009812bcb5d159dd52b5dab638bbe1ca480571" ,"test",0],"id":1}' | nc -U "$COMMUNITY_DATA_PATH/geth.ipc" echo '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{"from": "0xfe009812bcb5d159dd52b5dab638bbe1ca480571", "to": "0xdfe5443654725f409ecc47c2beeae1619ad50bf3", "value": "0x1", "data": "0x010203040506070809", "gas": "0x100000"}],"id":1}' | nc -U "$COMMUNITY_DATA_DIR/geth.ipc"