{"id":636,"date":"2020-01-28T17:23:40","date_gmt":"2020-01-28T08:23:40","guid":{"rendered":"http:\/\/www.filecoin.jp\/?p=636"},"modified":"2020-02-03T08:27:57","modified_gmt":"2020-02-02T23:27:57","slug":"transaction%e3%82%92ethereum%e3%83%8d%e3%83%83%e3%83%88%e3%83%af%e3%83%bc%e3%82%af%e3%81%ab%e9%80%81%e3%82%8b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.doragogo.net\/?p=636","title":{"rendered":"Transaction\u3092Ethereum\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306b\u9001\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>package main<\/p>\n<p>import (<br \/>\n&#8220;context&#8221;<br \/>\n&#8220;crypto\/ecdsa&#8221;<br \/>\n&#8220;fmt&#8221;<br \/>\n&#8220;log&#8221;<br \/>\n&#8220;math\/big&#8221;<\/p>\n<p>&#8220;github.com\/doragogo\/go-doragogo\/common&#8221;<br \/>\n&#8220;github.com\/doragogo\/go-doragogo\/core\/types&#8221;<br \/>\n&#8220;github.com\/doragogo\/go-doragogo\/crypto&#8221;<br \/>\n&#8220;github.com\/doragogo\/go-doragogo\/crypto\/sha3&#8221;<br \/>\n&#8220;github.com\/doragogo\/go-doragogo\/ethclient&#8221;<br \/>\n)<\/p>\n<p>func sendExternalRawTransaction(receiveAddress string, amount float64) (transaction string) {<br \/>\n\/\/Ropsten\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306b\u63a5\u7d9a<br \/>\nclient, err := ethclient.Dial(&#8220;https:\/\/ropsten.infura.io&#8221;)<br \/>\nif err != nil {<br \/>\nlog.Fatal(err)<br \/>\n}<\/p>\n<p>\/\/PrivateKey\u3092\u8aad\u307f\u8fbc\u3080<br \/>\nprivateKey, err := crypto.HexToECDSA(&#8220;f47321dc18a9bcafa063d0980cbcdcb1ff19e7430bf501612c21da0648cf2f8&#8221;)<br \/>\nif err != nil {<br \/>\nlog.Fatal(err)<br \/>\n}<\/p>\n<p>\/\/\u3000PrivateKey\u304b\u3089Publickey\u3092\u751f\u6210<br \/>\npublicKey := privateKey.Public()<br \/>\npublicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)<br \/>\nif !ok {<br \/>\nlog.Fatal(&#8220;Error casting public key to ECDSA&#8221;)<br \/>\n}<\/p>\n<p>\/\/PublicKey\u304b\u3089\u3001\u9001\u91d1\u4e3b\u30a2\u30c9\u30ec\u30b9\u3092\u751f\u6210<br \/>\nfromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)<\/p>\n<p>\/\/Ropsten\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304b\u3089\u3001Nonce\u60c5\u5831\u3092\u8aad\u307f\u53d6\u308b<br \/>\nnonce, err := client.PendingNonceAt(context.Background(), fromAddress)<br \/>\nif err != nil {<br \/>\nlog.Fatal(err)<br \/>\n}<\/p>\n<p>\/\/\u30c8\u30fc\u30af\u30f3\u9001\u91d1Transaction\u3092\u30c6\u30b9\u30c8\u30cd\u30c3\u30c8\u9001\u308b\u305f\u3081\u306egasLimit\u3001<br \/>\nvalue := big.NewInt(0) \/\/\uff08\u30aa\u30d7\u30b7\u30e7\u30f3\uff09\u5f8c\u3067\u4f7f\u7528\u3059\u308b\u95a2\u6570NewTransaction\u306e\u5f15\u6570\u3067\u5fc5\u8981\u306b\u306a\u308b\u305f\u3081\u8a2d\u5b9a\u3002Transaction\u3068\u540c\u6642\u306b\u9001\u308bETH\u306e\u91cf\u3092\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002<br \/>\ngasLimit := uint64(2000000)<\/p>\n<p>\/\/\u30ed\u30d7\u30b9\u30c6\u30f3\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304b\u3089\u3001\u73fe\u5728\u306egasPrice\u3092\u53d6\u5f97\u3002\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u30de\u30a4\u30cb\u30f3\u30b0\u3055\u308c\u305a\u306b\u653e\u7f6e\u3055\u308c\u308b\u3053\u3068\u3092\u9632\u3050\u3002<br \/>\ngasPrice, err := client.SuggestGasPrice(context.Background())<br \/>\nif err != nil {<br \/>\nlog.Fatal(err)<br \/>\n}<\/p>\n<p>\/\/\u9001\u91d1\u5148\u3092\u6307\u5b9a<br \/>\ntoAddress := common.HexToAddress(receiveAddress)<br \/>\n\/\/\u30c8\u30fc\u30af\u30f3\u30b3\u30f3\u30c8\u30e9\u30af\u30c8\u30a2\u30c9\u30ec\u30b9\u3092\u6307\u5b9a<br \/>\ntokenAddress := common.HexToAddress(&#8220;0x43aff81f88c18bc85281039df68002872b233afc&#8221;)<br \/>\n\/\/ERC20\u306e\u3069\u306e\u95a2\u6570\u3092\u4f7f\u7528\u3059\u308b\u304b\u6307\u5b9a\u3002https:\/\/github.com\/ethereum\/wiki\/wiki\/JSON-RPC#eth_sendtransaction<br \/>\ntransferFnSignature := []byte(&#8220;transfer(address,uint256)&#8221;)<br \/>\n\/\/hash\u5316\u3057\u3001\u5148\u982d\u304b\u30894\u30d0\u30a4\u30c8\u307e\u3067\u53d6\u5f97\u3002\u3053\u308c\u3067\u4f7f\u7528\u3059\u308b\u95a2\u6570\u3092\u6307\u5b9a\u3057\u305f\u3053\u3068\u306b\u306a\u308b\u3002<br \/>\nhash := sha3.NewKeccak256()<br \/>\nhash.Write(transferFnSignature)<br \/>\nmethodID := hash.Sum(nil)[:4]<\/p>\n<p>\/\/0\u57cb\u3081<br \/>\npaddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32)<br \/>\n\/\/\u9001\u91d1\u984d\u3092\u8a2d\u5b9a<br \/>\npIntAmount := big.NewInt(int64(amount))<br \/>\n\/\/0\u57cb\u3081<br \/>\npaddedAmount := common.LeftPadBytes(pIntAmount.Bytes(), 32)<\/p>\n<p>\/\/\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3067\u9001\u308b\u30c7\u30fc\u30bf\u3092\u4f5c\u6210<br \/>\nvar data []byte<br \/>\ndata = append(data, methodID&#8230;)<br \/>\ndata = append(data, paddedAddress&#8230;)<br \/>\ndata = append(data, paddedAmount&#8230;)<\/p>\n<p>\/***** Preparing signed transaction *****\/<br \/>\ntx := types.NewTransaction(nonce, tokenAddress, value, gasLimit, gasPrice, data)<br \/>\nsignedTx, err := types.SignTx(tx, types.HomesteadSigner{}, privateKey)<br \/>\nif err != nil {<br \/>\nlog.Fatal(err)<br \/>\n}<\/p>\n<p>\/\/\u30b5\u30a4\u30f3\u3057\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092RopstenNetwork\u306b\u9001\u308b\u3002<br \/>\nerr = client.SendTransaction(context.Background(), signedTx)<br \/>\nif err != nil {<br \/>\nlog.Fatal(err)<br \/>\n}<\/p>\n<p>fmt.Printf(&#8220;Signed tx sent: %s&#8221;, signedTx.Hash().Hex())<\/p>\n<p>return signedTx.Hash().Hex()<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>package main import ( &#8220;context&#8221; &#8220;cryp &hellip; <\/p>\n","protected":false},"author":1,"featured_media":670,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-636","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"_links":{"self":[{"href":"https:\/\/www.doragogo.net\/index.php?rest_route=\/wp\/v2\/posts\/636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.doragogo.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doragogo.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doragogo.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doragogo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=636"}],"version-history":[{"count":8,"href":"https:\/\/www.doragogo.net\/index.php?rest_route=\/wp\/v2\/posts\/636\/revisions"}],"predecessor-version":[{"id":728,"href":"https:\/\/www.doragogo.net\/index.php?rest_route=\/wp\/v2\/posts\/636\/revisions\/728"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.doragogo.net\/index.php?rest_route=\/wp\/v2\/media\/670"}],"wp:attachment":[{"href":"https:\/\/www.doragogo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doragogo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doragogo.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}