您正在查看: surou 发布的文章

Cosmos钱包管理和离线交易签名工具

github: https://github.com/hukkinj1/cosmospy

cosmospy

Version 5.0.1

Tools for Cosmos wallet management and offline transaction signing

Installing

Installing from PyPI repository (https://pypi.org/project/cosmospy):

pip install cosmospy

Usage

Generating a wallet

from cosmospy import generate_wallet
wallet = generate_wallet()

The value assigned to wallet will be a dictionary just like:

{
    "seed": "arch skill acquire abuse frown reject front second album pizza hill slogan guess random wonder benefit industry custom green ill moral daring glow elevator",
    "derivation_path": "m/44'/118'/0'/0/0",
    "private_key": b"\xbb\xec^\xf6\xdcg\xe6\xb5\x89\xed\x8cG\x05\x03\xdf0:\xc9\x8b \x85\x8a\x14\x12\xd7\xa6a\x01\xcd\xf8\x88\x93",
    "public_key": b"\x03h\x1d\xae\xa7\x9eO\x8e\xc5\xff\xa3sAw\xe6\xdd\xc9\xb8b\x06\x0eo\xc5a%z\xe3\xff\x1e\xd2\x8e5\xe7",
    "address": "cosmos1uuhna3psjqfxnw4msrfzsr0g08yuyfxeht0qfh",
}

Converter functions

Mnemonic seed to private key

from cosmospy import seed_to_privkey
seed = "teach there dream chase fatigue abandon lava super senior artefact close upgrade"
privkey = seed_to_privkey(seed, path="m/44'/118'/0'/0/0")

Private key to public key

from cosmospy import privkey_to_pubkey
privkey = bytes.fromhex("6dcd05d7ac71e09d3cf7da666709ebd59362486ff9e99db0e8bc663570515afa")
pubkey = privkey_to_pubkey(privkey)

Public key to address

from cosmospy import pubkey_to_address
pubkey = bytes.fromhex("03e8005aad74da5a053602f86e3151d4f3214937863a11299c960c28d3609c4775")
addr = pubkey_to_address(pubkey)

Private key to address

from cosmospy import privkey_to_address
privkey = bytes.fromhex("6dcd05d7ac71e09d3cf7da666709ebd59362486ff9e99db0e8bc663570515afa")
addr = privkey_to_address(privkey)

Signing transactions

from cosmospy import Transaction
tx = Transaction(
    privkey=bytes.fromhex("26d167d549a4b2b66f766b0d3f2bdbe1cd92708818c338ff453abde316a2bd59"),
    account_num=11335,
    sequence=0,
    fee=1000,
    gas=70000,
    memo="",
    chain_id="cosmoshub-3",
    sync_mode="sync",
)
tx.add_transfer(recipient="cosmos103l758ps7403sd9c0y8j6hrfw4xyl70j4mmwkf", amount=387000)
tx.add_transfer(recipient="cosmos1lzumfk6xvwf9k9rk72mqtztv867xyem393um48", amount=123)
pushable_tx = tx.get_pushable()

One or more token transfers can be added to a transaction by calling the add_transfer method.

When the transaction is fully prepared, calling get_pushable will return a signed transaction in the form of a JSON string. This can be used as request body when calling the POST /txs endpoint of the Cosmos REST API.

让区块链浏览器收录你的 ERC20 代币

如果想让其他浏览器显示新发的代币,需要主动提交代币信息,对方审核通过后,即可显示。

打开网址https://etherscan.io,在此处输入 token 的智能合约地址,如下图:

搜索后,如下图:

按下图指示,点击“verify and publish”来发布代码,

由于读者已经发布了代码,这里就找了几张图贴出来,以供参考。

步骤一:

步骤二:

步骤三:

若出现如下图,即表示收录成功。

转载:https://my.oschina.net/u/3050295/blog/1982504

Eosplayer -> eosjs的绑定层

Eosplayer是eosjs的绑定层,它基于eosjs并为应用程序层提供了更好的可用性。它既可以在node.js服务器上使用,也可以在已经安装了Scatter的浏览器或Dapp钱包中使用。

frontd releases : https://github.com/bagaking/eosplayer/releases

演示地址:http://eosplayer.kihand.com/#/

Web3j send ERC20 Token Demo

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="../../libs/web3.js_v1.0.0-beta.35/web3.min.js"></script>
</head>
<body>

  <h1>Send ERC20 Token</h1>

  <h2>Notes</h2>
  <ul>
    <li>Use MetaMask</li>
    <li>To check ERC20 balance, use <a href="../getERC20TokenBalance/" target="_blank">getERC20TokenBalance</a></li>
  </ul>

  <h2>Token Address</h2>
  <input type="text" id="token-address" size="80" oninput="onAddressChange()"></input>
  <p>e.g. 0x2A65D41dbC6E8925bD9253abfAdaFab98eA53E34</p>

  <h2>Recipients Address</h2>
  <input type="text" id="to-address" size="80"></input>
  <p>e.g. 0x8Df70546681657D6FFE227aB51662e5b6e831B7A</p>

  <h2>Decimals</h2>
  <input type="number" id="decimals" size="40" readonly></input>

  <h2>Amount</h2>
  <input type="number" id="amount" size="40"></input>

  <div><button id="send" onclick="send()">Send ERC20 Token</button></div>

  <h2>Result</h2>
  <span id="result"></span>

  <script>

    var web3js;
    var account;

    function getERC20TokenBalance(tokenAddress, walletAddress, callback) {
      let minABI = [
        {"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"type":"function"},
        {"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"type":"function"}
      ];
      let contract = new web3js.eth.Contract(minABI, tokenAddress);
      contract.methods.balanceOf(walletAddress).call((error, balance) => {
        contract.methods.decimals().call((error, decimals) => {
          console.log(balance);
          console.log(decimals);
          balance = balance / (10**decimals);
          callback(balance);
        });
      });
    }

    function getERC20TokenContract(tokenAddress) {
      let minABI = [
        {"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"type":"function"},
        {"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"type":"function"},
        {"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"type":"function"}
      ];
      return new web3js.eth.Contract(minABI, tokenAddress);
    }

    function getERC20TokenDecimals(callback) {
      window.tokenContract.methods.decimals().call((error, decimals) => {
        callback(decimals);
      });
    }

    function onAddressChange(e) {
      let tokenAddress = document.getElementById('token-address').value;
      if(tokenAddress != "") {
        window.tokenContract = getERC20TokenContract(tokenAddress);
        getERC20TokenDecimals((decimals) => {
          document.getElementById('decimals').value = decimals;
        });
      }
    }

    function transferERC20Token(toAddress, value, callback) {
      window.tokenContract.methods.transfer(toAddress, value).send({from: account})
      .on('transactionHash', function(hash){
        callback(hash);
      });
    }

    function send() {
      var toAddress = document.getElementById('to-address').value;
      var decimals = web3js.utils.toBN(document.getElementById('decimals').value);
      var amount = web3js.utils.toBN(document.getElementById('amount').value);
      var sendValue = amount.mul(web3js.utils.toBN(10).pow(decimals));
      console.log(sendValue.toString());
      transferERC20Token(toAddress, sendValue, (txHash) => {
        document.getElementById('result').innerText = txHash;
      });
    }

    window.onload = function() {
      web3js = new Web3(Web3.givenProvider);
      console.log(web3js.version);
      var accountInterval = setInterval(function() {
        web3js.eth.getAccounts((error, address) => {
          if (address[0] !== account) {
            account = address[0];
            console.log(account);
          }
        });

      }, 300);
    }

  </script>

</body>
</html>

https://github.com/piyolab/sushiether/commit/e35e949b5d898d357f9bd1d4e3f35b679974f192

Hyperledger Fabric 演练 - 《基于联盟区块链的政务大数据共享》

1. 背景

为了继续深入研究Fabric的加密通道逻辑,找了一个现成的例子,并修正了一个新手部署可能遇到的问题,
修正的主要点是

  1. 修改实例代码相对fabric-samples路径,
  2. 细化一些操作步骤,方便新手傻瓜式运行

1.1 区域链(基于证书准入的联盟链)网络

1.2 业务流程时序

2. 演示系统环境

OS: Ubuntu 18.04
Fabric: release-1.4
Go: go1.14.4 linux/amd64
Docker: 19.03.11

3. 源代码仓库

gitee: https://gitee.com/BCSkill/fcc

4. 实例部署

4.1 克隆源代码

git clone https://gitee.com/BCSkill/fcc.git

4.2 部署fabric-samples运行环境

细节的话可以参考《fabric-samples 部署测试》,我们这里不做详细描述,直接运行封装好的脚本

./1-1.hy-sample.sh

大概过程中会分为几步,

  1. 克隆fabric-samples到当前源码目录
  2. 下载编译到好的fabric二进制文件,并解压到 fabric-samples/bin,记得将此目录添加到环境变量
  3. 拉取所需要的Docker镜像

4.3 启动示例网络,创建区块链网络(2个组织,每组织2个节点)

./1-2.startNetwork.sh

成功后输出========= All GOOD, BYFN execution completed ===========

4.4 加入第3个组织,2个节点

./2.addOrg3.sh

完成后,结果显示========= Org3 is now halfway onto your first network =========

4.5 部署政务智能合约,并实例化

./3-1.installNetcon.sh   #合约:网签合同备案
./3-2.installEstateBook.sh   #合约:不动产权证书
./3-3.installEstateTax.sh    #合约:不动产业务缴税

完成后,结果显示

Get instantiated chaincodes on channel mychannel:
Name: estatebook, Version: 1.0, Path: github.com/chaincode/estatebook, Escc: escc, Vscc: vscc
Name: estatetax, Version: 1.0, Path: github.com/chaincode/estatetax, Escc: escc, Vscc: vscc
Name: netcon, Version: 1.0, Path: github.com/chaincode/netcon, Escc: escc, Vscc: vscc

此时链环境和链合约已经部署好了。下面开始部署项目的服务端和Web前台

4.6 编译后台。

代码位于appcode/fccserver/src 可自行编译,或直接使用已编译完成的可执行文件

chmod +x appcode/fccserver/src/fccserver

启动后台容器

./4.startAppcli.sh
docker logs -f appcli

如果启动正常,会显示

[fcc-server] 2019/12/12 03:03:55 system db initiated successfully.
[fcc-server] 2019/12/12 03:03:56 Chaincode client initialed successfully.
[fcc-server] 2019/12/12 03:03:56 Server started on  :1206

4.7 编译和部署前端

前端采用VUE,也可使用其它前端框架或HTML。使用GNINX或其它WEB服务器部署编译后的前端代码。注:当前未使用登录和权限设置
前台部署依赖yarn,《ubuntu 安装最新版本 yarn》

cd ./appcode/fcc-client // 进到Web前端代码目录
yarn install            // 安装依赖
yarn serve              // 运行前端

如果出现
运行成功后,就会显示访问地址

  App running at:
  - Local:   http://localhost:8080/ 
  - Network: http://192.168.31.134:8080/

浏览器打开地址,就能看到实例演示了

在线演示

http://xujf000.tk:28888/#/

实例依赖Docker 镜像

surou@surou:~/go/src/github.com/chaincode/netcon$ docker ps
+ docker ps
CONTAINER ID        IMAGE                                                                                                        COMMAND                  CREATED             STATUS              PORTS                                        NAMES
f3b34643ff39        dev-peer0.org2.example.com-estatebook-1.0-08d85d200f6268a9c9656f01eac3d5325c44c1d2a93a1115168f63888a27206b   "chaincode -peer.add…"   44 minutes ago      Up 43 minutes                                                    dev-peer0.org2.example.com-estatebook-1.0
9828c2b46b4e        dev-peer0.org2.example.com-estatetax-1.0-975867dfc12f233b11d78db94c347aa6b7e94f1d389af7cc892cb8730652c110    "chaincode -peer.add…"   45 minutes ago      Up 45 minutes                                                    dev-peer0.org2.example.com-estatetax-1.0
9d62c54bfd73        dev-peer0.org2.example.com-netcon-1.0-8285fea29f8449473340c346dc0b6716a084576593325a33707353426d7298dc       "chaincode -peer.add…"   46 minutes ago      Up 46 minutes                                                    dev-peer0.org2.example.com-netcon-1.0
cb870db589c4        hyperledger/fabric-tools:1.4.3                                                                               "./fccserver"            About an hour ago   Up About an hour    0.0.0.0:1206->1206/tcp                       appcli
33ad280f1f17        dev-peer0.org1.example.com-estatetax-1.0-3284f6d77599d92fad2da964008b3f4bb78573eeb938f40e2110de2089455dc3    "chaincode -peer.add…"   About an hour ago   Up About an hour                                                 dev-peer0.org1.example.com-estatetax-1.0
fa575e9548e3        dev-peer0.org1.example.com-estatebook-1.0-1804a11c573ccbef46e7cda2314c1d76edc2fb52b0f7f9d10ed2024c03343aff   "chaincode -peer.add…"   About an hour ago   Up About an hour                                                 dev-peer0.org1.example.com-estatebook-1.0
75c50829fbb8        dev-peer0.org1.example.com-netcon-1.0-4faf315b9f1da5832e386fbae0d05157d269cff3802d770b0e8f135a69027796       "chaincode -peer.add…"   About an hour ago   Up About an hour                                                 dev-peer0.org1.example.com-netcon-1.0
15d29c21ed9a        hyperledger/fabric-tools:1.4.3                                                                               "/bin/bash"              2 hours ago         Up 2 hours                                                       Org3cli
d0ce2cc32019        hyperledger/fabric-peer:1.4.3                                                                                "peer node start"        2 hours ago         Up 2 hours          0.0.0.0:12051->12051/tcp                     peer1.org3.example.com
9bcf5082db71        hyperledger/fabric-peer:1.4.3                                                                                "peer node start"        2 hours ago         Up 2 hours          0.0.0.0:11051->11051/tcp                     peer0.org3.example.com
6f76cca2d809        hyperledger/fabric-tools:latest                                                                              "/bin/bash"              2 hours ago         Up 2 hours                                                       cli
04d61faad751        hyperledger/fabric-peer:latest                                                                               "peer node start"        2 hours ago         Up 2 hours          0.0.0.0:10051->10051/tcp                     peer1.org2.example.com
cdbffd49d7d2        hyperledger/fabric-peer:latest                                                                               "peer node start"        2 hours ago         Up 2 hours          0.0.0.0:8051->8051/tcp                       peer1.org1.example.com
d59fe735b96d        hyperledger/fabric-peer:latest                                                                               "peer node start"        2 hours ago         Up 2 hours          0.0.0.0:9051->9051/tcp                       peer0.org2.example.com
4caeb0c062e0        hyperledger/fabric-peer:latest                                                                               "peer node start"        2 hours ago         Up 2 hours          0.0.0.0:7051->7051/tcp                       peer0.org1.example.com
431ad6029954        hyperledger/fabric-ca:latest                                                                                 "sh -c 'fabric-ca-se…"   2 hours ago         Up 2 hours          7054/tcp, 0.0.0.0:8054->8054/tcp             ca_peerOrg2
4d69e9f76b1b        hyperledger/fabric-couchdb                                                                                   "tini -- /docker-ent…"   2 hours ago         Up 2 hours          4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp   couchdb0
1bcd9f89625f        hyperledger/fabric-couchdb                                                                                   "tini -- /docker-ent…"   2 hours ago         Up 2 hours          4369/tcp, 9100/tcp, 0.0.0.0:6984->5984/tcp   couchdb1
cb46fd7307cf        hyperledger/fabric-couchdb                                                                                   "tini -- /docker-ent…"   2 hours ago         Up 2 hours          4369/tcp, 9100/tcp, 0.0.0.0:8984->5984/tcp   couchdb3
ac996671c144        hyperledger/fabric-orderer:latest                                                                            "orderer"                2 hours ago         Up 2 hours          0.0.0.0:7050->7050/tcp                       orderer.example.com
c13cc3241b96        hyperledger/fabric-ca:latest                                                                                 "sh -c 'fabric-ca-se…"   2 hours ago         Up 2 hours          0.0.0.0:7054->7054/tcp                       ca_peerOrg1
01ce681daa6c        hyperledger/fabric-couchdb                                                                                   "tini -- /docker-ent…"   2 hours ago         Up 2 hours          4369/tcp, 9100/tcp, 0.0.0.0:7984->5984/tcp   couchdb2

参考

https://www.bcskill.com/index.php/archives/926.html