以太坊 web3.py 签名转账

本文节选自电子书《Netkiller Blockchain 手札》

Netkiller Blockchain 手札

Mr. Neo Chan, 陈景峯(BG7NYT)

中国广东省深圳市龙华新区民治街道溪山美地 518131 +86 13113668890 <netkiller@msn.com>

文档始创于2018-02-10

版权 © 2018 Netkiller(Neo Chan). All rights reserved.

版权声明

转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。

内容摘要

这一部关于区块链开发及运维的电子书。

为什么会写区块链电子书?因为2018年是区块链年,区块链是一个风口,前几个风口我都错过了。例如web2.0, 云, 大数据等等,都从身旁擦肩而过。所以我要抓住这次。

这本电子书是否会出版(纸质图书)? 不会,因为互联网技术更迭太快,纸质书籍的内容无法实时更新,一本书动辄百元,很快就成为垃圾,你会发现目前市面的上区块链书籍至少是一年前写的,内容已经过时,很多例子无法正确运行。所以我不会出版,电子书的内容会追逐技术发展,及时跟进软件版本的升级,做到内容最新,至少是主流。

这本电子书与其他区块链书籍有什么不同?市面上大部分区块链书籍都是用2/3去讲区块链原理,只要不到 1/3 的干货,干货不够理论来凑,通篇将理论或是大谈特谈区块链行业,这些内容更多是头脑风暴,展望区块链,均无法落地实施。本书与那些书籍完全不同,不讲理论和原理,面向应用落地,注重例子,均是干货。

写作原则,无法落地的项目作者绝对不会写。凡是写入电子的内容均具备可操作,可落地。

电子书更新频率?每天都会有新内容加入,更新频率最迟不会超过一周,更新内容请关注 https://github.com/netkiller/netkiller.github.io/commits/master

本文采用碎片化写作,原文会不定期更新,请尽量阅读原文 http://www.netkiller.cn/blockchain/index.html

您的打赏是我的写作动力:http://www.netkiller.cn/blockchain/donations.html

接受 ETH 打赏:0x3e827461Cc53ed7c75A29187CfF39629FCAE3661

# author: netkiller@msn.com
# home: http://www.netkiller.cn

from web3 import Web3
from web3 import Web3, HTTPProvider, IPCProvider
# w3 = Web3(Web3.IPCProvider("~/Library/Ethereum/geth.ipc"))	

w3 = Web3(HTTPProvider("https://ropsten.infura.io/CsS9shwaAab0z7B4LP2d"))

fromAddress = '0xB94054c174995AE2A9E7fcf6c7924635FBa8ECF7'
toAddress = '0xCdF0253d8362d6c3334c8F28A6BFd74c90d03d92'
contractAddress='0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359'
private_key = b''

with open('/Users/neo/Library/Ethereum/testnet/keystore/UTC--2018-03-02T12-12-51.966823000Z--b94054c174995ae2a9e7fcf6c7924635fba8ecf7') as keyfile:
    encrypted_key = keyfile.read()
    private_key = w3.eth.account.decrypt(encrypted_key, '')

print(private_key)

interface='[ { "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "approve", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_value", "type": "uint256" } ], "name": "burn", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "burnFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" }, { "name": "_extraData", "type": "bytes" } ], "name": "approveAndCall", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" }, { "name": "", "type": "address" } ], "name": "allowance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "inputs": [ { "name": "initialSupply", "type": "uint256", "index": 0, "typeShort": "uint", "bits": "256", "displayName": "initial Supply", "template": "elements_input_uint", "value": "100000" }, { "name": "tokenName", "type": "string", "index": 1, "typeShort": "string", "bits": "", "displayName": "token Name", "template": "elements_input_string", "value": "NEO" }, { "name": "tokenSymbol", "type": "string", "index": 2, "typeShort": "string", "bits": "", "displayName": "token Symbol", "template": "elements_input_string", "value": "#" } ], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Burn", "type": "event" } ]'
contract = w3.eth.contract(address=contractAddress, abi=interface)

nonce = w3.eth.getTransactionCount(fromAddress)
print(nonce)

txn = contract.functions.transfer(toAddress,5,).buildTransaction({
    'chainId': 3,
    'gas': 30000,
    'gasPrice': w3.toWei('1', 'gwei'),
    'nonce': nonce,
    })

print(txn)

signed_txn = w3.eth.account.signTransaction(txn, private_key=private_key)
print(signed_txn.hash)
print(signed_txn.rawTransaction)

tmp = w3.eth.sendRawTransaction(signed_txn.rawTransaction)  

txhash = w3.toHex(w3.sha3(signed_txn.rawTransaction))

print("https://ropsten.etherscan.io/tx/"+txhash)		
		

QQ:13721218 请注明“读者”

QQ群:128659835 请注明“读者”

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏玉树芝兰

我不改密码能怎样?

文章大意是:昨天某媒体爆料说国内某大型电子商务平台发生了用户信息外泄事件。黑市上该数据集被明码标价进行交易。今日凌晨,该平台已经作出回应,承认因2013年的系统...

762
来自专栏FreeBuf

AlphaBay是怎么垮掉的?解密全球最大黑市AlphaBay被查封全过程

又一大暗网市场AlphaBay倒下了,而更加悲剧的是,该网站背后的主谋竟在监狱中自杀了…据说当时警方总共收缴了四辆兰博基尼和三套房产,还真是有钱也没命花啊! ?...

2595
来自专栏域名资讯

域名UB.com以超85万被联合比特币收购

如今终端的目光越来越放在好域名身上了,前有一家比特币终端拿下极品大米zb.com,后有UB.com也曝出被UnitedBitcoin收购并启用,可...

1570
来自专栏编程坑太多

支付宝是如何用大数据憋死伪基站骗子?

1403
来自专栏阮一峰的网络日志

关于 public.resource.org

版权制度中有一个原则,那就是政府文件是没有版权的,属于公共领域,任何人都可以自由使用。这是因为政府文件来自于纳税人的税款,理应属于全社会所有。 可是,虽然没有版...

2912
来自专栏圆方圆学院精选

【董天一】IPFS和竞争对手们(二)

在上一篇《IPFS和竞争对手们(一)》的开头抱怨的IPFS的开发进度,今天才得知 MaidSafe 是从2006年起步的,这进度也是醉了。

691
来自专栏有趣的Python和你

Python数据分析之简书七日热门数据分析七日热门的文章发布日期排名

1236
来自专栏Netkiller

HD Wallet - Netkiller 汉字助记词库

中国广东省深圳市龙华新区民治街道溪山美地 518131 +86 13113668890 <netkiller@msn.com>

31214
来自专栏域名资讯

域名快讯:域名nbty.cn被NBTY公司以7万美金买入

  四声母域名nbty.cn域名以7万美金,超46万元高价交易。该域名对应一家美国上市公司——美国营养品巨头NBTY公司,其官网域名为nbty.com。买家是否...

1620
来自专栏数说工作室

通过逐笔数据计算主力资金流数据 |【量化小讲堂】

1)涉及到的金融学知识 正文对一些概念的解释已经很清楚了,但是对于没有操作过股票的人来说,一些最基本的可能会不太明白,比如市价单、限价单(一个是按实时价格交易...

3175

扫码关注云+社区