资讯专栏INFORMATION COLUMN

eos 区块链采坑,scatter 钱包

张汉庆 / 1658人阅读

摘要:做个有理想的码农,坑定的有多少的了解下当下最火的技术,提到区块链不懂也的了解下。有人说现在的区块链哀声遍野,在这个互联网寒冬下,有多少的区块链公司,在这个寒冬倒下,现在的各种的数字货币,炒币的,挖矿的的,,乌烟瘴气。。。。。

做个有理想的码农 ,坑定的有多少的了解下当下最火的技术,提到区块链 不懂也的 了解下。
有人说现在的区块链哀声遍野,在这个互联网寒冬下,有多少的区块链公司,在这个寒冬倒下,现在的各种的 数字货币,炒币的,挖矿的的,,乌烟瘴气。。。。。

进入正题,最近了解下 eos 写了 然后调用 scatter钱包时个各种各样的的错,属实让人头疼,这里我 我总结下了,给自己留个笔记

import iView from "iview";
import i18n from "@/locale"
import config from "@/config"
import ScatterJS from "scatterjs-core"
import ScatterEOS from "scatterjs-plugin-eosjs"
import Eos from "eosjs";
import {GetQueryString} from "../libs/geturl";
window.ScatterJS = null;

// const eosconfig =process.env.NODE_ENV === "development" ? config.eosConfig.dev : config.eosConfig.pro;

const eosconfig = GetQueryString("dev") != "true" ? config.eosConfig.pro : config.eosConfig.dev;


class getscatter {
    static account;
    static eos;
    static  scatter;
    static instance;


    constructor() {

    }

    ScatterSecureCall(func) {
        const network = ScatterJS.Network.fromJson(eosconfig);
        var app_name = "dada_poker";

        if (ScatterJS.scatter.eos == null) {
            ScatterJS.plugins(new ScatterEOS());
        }
        ScatterJS.scatter.connect(app_name).then(connected => {
            if (connected) {
                const scatter = ScatterJS.scatter;
                scatter.getIdentity({
                    accounts: [network]
                }).then(() => {
                    const scatter = ScatterJS.scatter;
                    const account = scatter.identity.accounts.find(x => x.blockchain === "eos");
                    const eosOptions = {
                        expireInSeconds: 60
                    };
                    const eos = scatter.eos(network, Eos, eosOptions);
                    func(scatter, account, eos);

                }).catch(error => {
                    // document.getElementById("box").innerHTML = format_json(error);
                    console.log(error);
                    iView.Message.error({
                        top: 70,
                        content: JSON.stringify(error),
                        duration: 10
                    });
                });
            } else {
                console.log("The Scatter interface call failed! Please install the Scatter plugin or launch the Scatter app");
                // document.getElementById("box").innerHTML = "Scatter接口调用失败!请安装Scatter插件或者启动Scatter应用程序。";
                iView.Message.error({
                    top: 70,
                    content: i18n.t("login_error"),
                    duration: 10
                });
            }
        })
    }

    eosGetIdentity(callback) {
        console.log("enter eosGetIdentity");
        this.ScatterSecureCall((scatter, account, eos) => {
            console.log("[eosGetIdentity] account = ", account);
                this.account = account;
                this.eos = eos;
                this.scatter =scatter
                callback && callback(scatter, this.account, this.eos);
        });
    }

    eosTransfer(to, quantity, memo, callback) {
        if (this.account == null || this.eos == null) {
            console.log("you must eosGetIdentity");
            this.eosGetIdentity(() => {
                this.eosTransfer(to, quantity, memo, callback);
            });
        } else {
            this.eos.contract("eosio.token").then(contract => {
                const transactionOptions = {authorization: [`${this.account.name}@${this.account.authority}`]};
                contract.transfer(this.account.name, to, quantity, memo, transactionOptions).then(transaction => {
                    console.log("eosTransfer transaction processed object: %d", transaction.processed);
                    if (null != callback) {
                        callback(transaction);
                    }
                }).catch(error => {
                    if (typeof error == "string") {
                        error = JSON.parse(error);
                        let errorMsg = error.error.details[0].message;
                        iView.Message.error({
                            top: 70,
                            content: errorMsg,
                            duration: 10
                        });
                        // alert(errorMsg); //todo
                    }
                    iView.Message.error({
                        top: 70,
                        content: error,
                        duration: 10
                    });
                });
            }).catch(error => {
                console.log(error);
                iView.Message.error({
                    top: 70,
                    content: error,
                    duration: 10
                });
            });
        }
    }

    eosRedeem(quantity, to, callback) {
        if (this.account == null || this.eos == null) {
            console.log("you must eosGetIdentity");
            this.eosGetIdentity(() => {
                this.eosRedeem(quantity, memo, callback);
            });
        } else {
            this.eos.contract(to).then(contract => {
                const transactionOptions = {
                    authorization: [`${this.account.name}@${this.account.authority}`]
                };
                contract.redeem(this.account.name, quantity, transactionOptions).then(transaction => {
                    console.log("eosTransfer transaction processed object: ", transaction.processed);
                    if (null != callback) {
                        callback(transaction);
                    }
                }).catch(error => {
                    if (typeof error == "string") {
                        error = JSON.parse(error);
                        let errorMsg = error.error.details[0].message;
                        iView.Message.error({
                            top: 70,
                            content: errorMsg,
                            duration: 10
                        });

                    }
                    console.log(error);
                    iView.Message.error({
                        top: 70,
                        content: error,
                        duration: 10
                    });
                })
            }).catch(error => {
                console.log(error);
                iView.Message.error({
                    top: 70,
                    content: error,
                    duration: 10
                });
            });
        }
    }

    eosLogin(callback) {
        if (this.account == null || this.eos == null) {
            console.log(11);
            this.eosGetIdentity(() => {
                console.log(this.account);
                this.eosLogin(callback);
            });
        } else {
            ScatterJS.login().then(res => {
                console.log(res);
                callback(res, this.account)
            }).catch((error) => {
                iView.Message.error({
                    top: 70,
                    content: error,
                    duration: 10
                });
            })
        }
    }

    eosLogout(callback) {
        if (this.account == null || this.eos == null) {
            this.eosGetIdentity(() => {
                this.eosLogout(callback)
            })
        } else {
            ScatterJS.logout().then(res => {
                try{//App
                    this.scatter.isConnected()
                    this.account = null;
                    this.eos = null;
                }catch (e) {//浏览器
                    // throw  new Error("浏览器插件")
                    console.log(e);

                }
                callback && callback(res)
                window.ScatterJS = null;
            }).catch(error => {
                iView.Message.error({
                    top: 70,
                    content: error,
                    duration: 10
                });
            })
        }
    }


}

const ScatterSecureCall = new getscatter();

export {
    // ScatterJS,
    ScatterSecureCall
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/24596.html

相关文章

  • 使用EOSJS和scatterEOS区块链上开发dApp

    摘要:必备知识设置用于为区块链签署交易,并在不泄露密钥的情况下向应用程序提供个人信息。 由于我一直在深入研究EOS dApp的开发,我看了不少好文章。在这里,我汇总了下做一些研究后得到的所有知识。在本文中,我将解释如何使用EOSJS和scatter。我假设你对智能合约以及如何在EOS区块链上部署它们有基本的了解,因为我将在本文中跳过该部分。 我们在构建什么?  我们正在构建一个简单的todo...

    Mr_houzi 评论0 收藏0
  • 使用EOSJS和scatterEOS区块链上开发dApp

    摘要:必备知识设置用于为区块链签署交易,并在不泄露密钥的情况下向应用程序提供个人信息。 由于我一直在深入研究EOS dApp的开发,我看了不少好文章。在这里,我汇总了下做一些研究后得到的所有知识。在本文中,我将解释如何使用EOSJS和scatter。我假设你对智能合约以及如何在EOS区块链上部署它们有基本的了解,因为我将在本文中跳过该部分。 我们在构建什么?  我们正在构建一个简单的todo...

    mumumu 评论0 收藏0
  • 使用EOSJS和scatterEOS区块链上开发dApp

    摘要:必备知识设置用于为区块链签署交易,并在不泄露密钥的情况下向应用程序提供个人信息。 由于我一直在深入研究EOS dApp的开发,我看了不少好文章。在这里,我汇总了下做一些研究后得到的所有知识。在本文中,我将解释如何使用EOSJS和scatter。我假设你对智能合约以及如何在EOS区块链上部署它们有基本的了解,因为我将在本文中跳过该部分。 我们在构建什么?  我们正在构建一个简单的todo...

    xiaodao 评论0 收藏0
  • 使用docker compose在EOS本地Testnet上开发

    摘要:为本地配置的钱包。以太坊,主要是针对工程师使用进行区块链以太坊开发的详解。以太坊,主要是介绍使用进行智能合约开发交互,进行账号创建交易转账代币开发以及过滤器和交易等内容。这里是如何使用和在本地上开发 EOS区块链的开发并不是立竿见影的,因为需要一些非显而易见的组件,需要对它们进行配置和协同工作。 nodeos:块生成器守护程序。 keosd:钱包守护进程,存储私钥。 eosio-cp...

    cod7ce 评论0 收藏0
  • 【许晓笛】EOS 系统架构图解

    摘要:了解系统架构之前我们先看看目前系统的主要组成部分系统的核心进程,也就是所谓的节点。 其实没有那么复杂 相信关心 EOS 系统的同学肯定见过下面这张蓝图。对,目前只是一个蓝图,而且以 BM 的尿性,肯定在心理已经把这张图改的面目全非了(比如图中的 eosd 早就改名了)。所以这张图只能作为未来 EOS 发展方向的参考而已,目前的 EOS 还远没有这么复杂和完善。 showImg(http...

    vibiu 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<