V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
cr4fun
V2EX  ›  分享创造

使用 RUST 语言开发了一个区块链

  •  
  •   cr4fun · 2019-03-19 10:02:52 +08:00 · 2177 次点击
    这是一个创建于 1836 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本地搭建 SDAG 区块链并完成转账

    按照本文档的步骤执行,你可以在本地搭建一个 SDAG 区块链,并使用 SDAG 的 jssdk 构建一个命令行钱包来查询和转账。

    1 安装 docker 社区版

    ubuntu 用户请参考如下安装方式:

    https://docs.docker.com/install/linux/docker-ce/ubuntu/

    centos 用户请参考如下安装方式:

    https://docs.docker.com/install/linux/docker-ce/centos/

    fedora 用户请参考如下安装方式: https://docs.docker.com/install/linux/docker-ce/fedora/

    Mac 用户请参考如下安装方式:

    https://docs.docker.com/docker-for-mac/install/

    windows 用户请参考如下安装方式:

    https://docs.docker.com/docker-for-windows/install/

    2 把当前用户添加到 docker 用户组

    sudo gpasswd -a $(whoami) docker
    

    3 获得 SDAG 区块链镜像

    docker pull registry.cn-beijing.aliyuncs.com/sdag/sdag_testnet_dev:latest
    

    4 启动 SDAG 区块链

    docker run --rm -d --name sdag -p 6615:6615 -p 8080:8080 registry.cn-beijing.aliyuncs.com/sdag/sdag_testnet_dev
    

    5 打开区块链浏览器

    http://localhost:8080
    

    6 获得创始后有数字资产的助记词

    sudo docker exec -it sdag cat data/sdg/settings.json
    

    返回的结果类似:

    拷贝下助记词,供第 7 步使用.

    fade aunt crack express uncle fit valley faculty candy toddler buzz pink
    

    7 发送数字资产

    在本地计算机(非 docker 里),新建一个名为 samples 的文件夹。

    mkdir samples
    cd samples
    

    初始化 npm,并安装 SDAG 的 jssdk:sdagwallet.js

    npm init
    npm isntall sdagwallet.js
    

    新建一个名为 mnemonic.js 的文件,输入以下内容:

    该文件使用 mnemonic.js 生成随机助记词

    const { default: Wallet } = require("sdagwallet.js");
    let wallet = new Wallet();
    var mnemonic = wallet.generateMnemonic();
    console.log(mnemonic);
    

    执行它:

    node mnemonic.js
    

    得到类似下图的结果:

    拷贝助记词,并建立一个名为 alice.js 的文件,在 mnemonic = "" 中填入助记词,内容如下:

    const { default: Wallet } = require("sdagwallet.js");
    //import Wallet from 'sdagwallet.js';
    let wallet = new Wallet();
    
    const mnemonic = "april warrior alarm actress end story social palm desert twist knife future";
    
    wallet.configHub("ws://localhost:6615");
    wallet.loginWithMnemonic(mnemonic).then(() => {
        var address = wallet.getAddress();
        console.log("address",address);
        wallet.getBalance().then((balance) => {
            console.log("balance",balance);
        });
    });
    
    

    执行它:

    node alice.js
    

    返回的结果如下,能看到 alice 的地址,及余额。

    拷贝 alice 的地址,留作 scott 给 alice 转账时使用。

    HKIRYKXL65TTTBLIW3CXIYQHGPBX3YGI
    

    新建一个名为 scott.js 的文件,并且把助记词配置为 "fade aunt crack express uncle fit valley faculty candy toddler buzz pink" (在第 6 步时从 docker 里得到的)

    const { default: Wallet } = require("sdagwallet.js");
    //import Wallet from 'sdagwallet.js';
    let wallet = new Wallet();
    
    const mnemonic = "fade aunt crack express uncle fit valley faculty candy toddler buzz pink";
    
    wallet.configHub("ws://localhost:6615");
    wallet.loginWithMnemonic(mnemonic).then(() => {
        var address = wallet.getAddress();
        console.log("address",address);
        wallet.getBalance().then((balance) => {
            console.log("balance",balance);
        });
    });
    
    

    执行它

    node scott.js
    

    返回的结果如下:

    现在,新建一个名为 scott-to-alice.js 的文件,内容如下:

    const { default: Wallet } = require("sdagwallet.js");
    //import Wallet from 'sdagwallet.js';
    let wallet = new Wallet();
    const mnemonic = "fade aunt crack express uncle fit valley faculty candy toddler buzz pink";
    wallet.configHub("ws://localhost:6615");
    wallet.loginWithMnemonic(mnemonic).then(async () => {
        await wallet.getBalance();
        wallet.send({
            to: 'HKIRYKXL65TTTBLIW3CXIYQHGPBX3YGI',
            amount: 10,
            text: 'from scott to alice'
        }).then(() => {
            console.log("ok");
        }).catch((err) => {
            console.log(err);
        });
    });
    
    

    执行它

    node scott-to-alice
    

    返回的结果如下:

    这说明已经完成转账,接下来,重新获得 alice 的余额:

    node alice
    

    返回结果如下:

    重新获得 scott 的余额:

    node scott
    

    返回结果如下:

    转账结束了,上述源码位于: https://github.com/smart-dag/samples.git

    2 条回复    2019-03-20 09:29:54 +08:00
    bokchoys
        1
    bokchoys  
       2019-03-20 02:02:05 +08:00 via iPhone
    我发现我最近想做什么
    v 站上就有帖子……
    cr4fun
        2
    cr4fun  
    OP
       2019-03-20 09:29:54 +08:00
    @bokchoys 一起探讨啊,已经开源代了,我微信 cr4fun
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1155 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:30 · PVG 02:30 · LAX 11:30 · JFK 14:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.