BCSkill (Block chain skill )
区块链中文技术社区

只讨论区块链底层技术
遵守一切相关法律政策!

EOS delay transaction 延迟消息

交易可以设置delay_sec来指定延迟执行的时间,在合约中也提供了相应的API。

transaction out; //构造交易
out.actions.emplace_back(permission_level{_self, N(active)}, _self, N(test), std::make_tuple()); //将指定行为绑定到该交易上
out.delay_sec = 5; //设置延迟时间,单位为秒
out.send(_next_id(), _self, false); //发送交易,第一个参数为该次交易发送id,每次需不同。如果两个发送id相同,则视第三个参数replace_existing来定是覆盖还是直接失败。

另外需要注意的是可以在合约的某行为中发自身的延迟消息,于是可以达到定时任务的循环执行。
转载自(github

Scatter 身份获取部分代码

Dapp MonsterEOS 请求Scatter 获取身份信息

const network = {
  protocol: CHAIN_PROTOCOL,
  blockchain: 'eos',
  host: CHAIN_HOST,
  port: CHAIN_PORT,
  chainId: CHAIN_ID
}

app.ports.scatterRequestIdentity.subscribe(async () => {
    await scatter.suggestNetwork(network)
    let requiredFields = {
        accounts: [network]
    }
    scatter.getIdentity(requiredFields).then((identity) => {

      const user = {
          eosAccount: identity.accounts[0].name,
          publicKey: identity.publicKey
      }

      app.ports.setScatterIdentity.send(user)
    }).catch(error => {
      app.ports.scatterRejected.send("Identity or Network was rejected")
      console.error(error)
    })
})

开始进入Scatter 相关代码

Scatter\src\scatterdapp.js

suggestNetwork(network){
        if(!Network.fromJson(network).isValid()) throws('The provided network is invalid.');
        return _send(NetworkMessageTypes.REQUEST_ADD_NETWORK, {
            network:network
        });
    }

getIdentity(fields = {}){
        return _send(NetworkMessageTypes.GET_OR_REQUEST_IDENTITY, {
            network:network,
            fields
        }).then(async identity => {
            this.useIdentity(identity);
            return identity;
        });
    }

Scatter\src\content.js

 contentListener(msg){
        if(!isReady) return;
        if(!msg) return;
        if(!stream.synced && (!msg.hasOwnProperty('type') || msg.type !== 'sync')) {
            stream.send(nonSyncMessage.error(Error.maliciousEvent()), PairingTags.INJECTED);
            return;
        }

        // Always including the domain for every request.
        msg.domain = strippedHost();                           //此时domain被赋值
        if(msg.hasOwnProperty('payload'))
            msg.payload.domain = strippedHost();

        let nonSyncMessage = NetworkMessage.fromJson(msg);
        switch(msg.type){
            case 'sync': this.sync(msg); break;
            case NetworkMessageTypes.GET_OR_REQUEST_IDENTITY:           this.getOrRequestIdentity(nonSyncMessage); break;
getOrRequestIdentity(message){
        if(!isReady) return;
        InternalMessage.payload(InternalMessageTypes.GET_OR_REQUEST_IDENTITY, message.payload)
            .send().then(res => this.respond(message, res))
    }

Scatter\src\background.js

 dispenseMessage(sendResponse, message){
        Background.checkAutoLock();
        switch(message.type){
            case InternalMessageTypes.GET_OR_REQUEST_IDENTITY:          Background.getOrRequestIdentity(sendResponse, message.payload); break;

Scatter\src\background.js

static getOrRequestIdentity(sendResponse, payload){
        this.lockGuard(sendResponse, () => {
            Background.load(scatter => {
                const {domain, fields} = payload;

                IdentityService.getOrRequestIdentity(domain, fields, scatter, (identity, fromPermission) => {
                    if(!identity){
                        sendResponse(Error.signatureError("identity_rejected", "User rejected the provision of an Identity"));
                        return false;
                    }

                    if(!fromPermission) {
                        this.addHistory(HistoricEventTypes.PROVIDED_IDENTITY, {
                            domain,
                            provided:!!identity,
                            identityName:identity ? identity.name : false,
                            publicKey:(identity) ? identity.publicKey : false
                        });

                        this.addPermissions([Permission.fromJson({
                            domain,
                            identity:identity.publicKey,
                            timestamp:+ new Date(),
                            fields,
                            checksum:domain
                        })])
                    }

                    sendResponse(identity);
                });
            });
        })
    }

Scatter\src\services\IdentityService.js

 static identityFromPermissionsOrNull(domain, scatter){
        const identityFromPermission = IdentityService.identityPermission(domain, scatter);
        return identityFromPermission ? identityFromPermission.getIdentity(scatter.keychain) : null;
    }

    static getOrRequestIdentity(domain, fields, scatter, callback){

        // Possibly getting an Identity that has been synced with this application.
        const identityFromPermission = IdentityService.identityFromPermissionsOrNull(domain, scatter);
        let identity = identityFromPermission;

        const sendBackIdentity = id => {
            if(!id || id.hasOwnProperty('isError')){
                callback(null, null);
                return false;
            }

            callback(id.asOnlyRequiredFields(fields), !!identityFromPermission);
        };

        if(identity){
            // Even though there is a previous permission,
            // the identity might have changed and no longer
            // meets the requirements.
            if(identity.hasRequiredFields(fields)){
                sendBackIdentity(identity);
                return false;
            } else {
                // TODO: Remove permission
            }
        }
        else NotificationService.open(new Prompt(PromptTypes.REQUEST_IDENTITY, domain, null, fields, sendBackIdentity));  //进入此处开始下面逻辑
    }

Scatter\src\prompts\RequestIdentityPrompt.vue

methods: {
            bind(changed, original) { this[original] = changed },
            filteredIdentities(){
                return this.identities
                    .filter(id => id.hasRequiredFields(this.identityFields))
                    .filter(id => JSON.stringify(id).indexOf(this.searchText) !== -1)
            },
            formatProp(prop){
                if(prop instanceof Network) return `${prop.blockchain.toUpperCase()} Account`;
                return prop;
            },
            formatPropValue(identity, prop){
                const value = identity.getPropertyValueByName(prop);
                if(prop instanceof Network) return PluginRepository.plugin(prop.blockchain).accountFormatter(value);
                else if (prop === 'country') return value.name;
                return value;
            },
            selectIdentity(identity){
                this.selectedIdentity = identity;
            },
            accepted(){
                if(!this.selectedIdentity){
                    this[Actions.PUSH_ALERT](AlertMsg.YouMustSelectAnIdentity());
                    return false;
                }
                const identity = this.identities.find(id => id.publicKey === this.selectedIdentity.publicKey);
                this.prompt.responder(identity);
                NotificationService.close();
            },
            denied(){
                this.prompt.responder(null);
                NotificationService.close();
            },
            ...mapActions([
                Actions.UPDATE_STORED_SCATTER,
                Actions.PUSH_ALERT,
                Actions.PUSH_PROMPT
            ])
        }

Scatter\src\models\Identity.js

 /***
     * Checks if an Identity has specified fields.
     * This is used when an interacting application requires specific information.
     * @param fields - The fields to check for
     * @returns {boolean}
     */
    hasRequiredFields(fields){
        const requiredFields = IdentityRequiredFields.fromJson(fields);
        if(!requiredFields.isValid()) return false;

        if(requiredFields.personal.length)
            if(!requiredFields.personal.every(field => this.personal[field].length))
                return false;

        if(requiredFields.location.length)
            if(!this.locations.find(location => location.hasFields(requiredFields.location)))
                return false;

        if(requiredFields.accounts.length)
            if(!requiredFields.accounts.every(network => this.hasAccount(network)))
                return false;

        return true;
    }
 static placeholder(){ return new Identity(); }
    static fromJson(json){
        let p = Object.assign(this.placeholder(), json);
        if(json.hasOwnProperty('accounts')) p.accounts = Object.keys(json.accounts).reduce((acc, network) => {
            acc[network] = Account.fromJson(json.accounts[network]);
            return acc;
        }, {});
        p.personal = PersonalInformation.fromJson(json.personal);
        if(json.hasOwnProperty('locations')) p.locations = json.locations.map(location => LocationInformation.fromJson(location));
        else p.locations = [LocationInformation.placeholder()];
        return p;
    }

查找Scatter存储的身份中,选取网络匹配的账号,返回到界面,由用户选中

hasAccount(network){ return this.accounts.hasOwnProperty(network.unique()) }

Scatter\src\models\Network.js

unique(){ return (`${this.blockchain}:` + (this.chainId.length ? `chain:${this.chainId}` : `${this.host}:${this.port}`)).toLowerCase(); }

${this.blockchain} 为公链固定的类型,比如EOS为eos
如果Dapp配置了所需网络的chain Id,则计算返回为("${this.blockchain}:"+"chain:${this.chainId}").toLowerCase()
如果没有配置chain Id,则计算返回 ("${this.blockchain}:"+"${this.host}:${this.port}").toLowerCase()

总结

由于大多Dapp都配置了chain id,所以查找Scatter中相应的身份,及chain id相同网络设置的身份。


添加身份

Scatter\src\background.js

static addPermissions(permissions){
        this.load(scatter => {
            permissions.map(permission => {
                if(!scatter.keychain.hasPermission(permission.checksum, permission.fields))
                    scatter.keychain.permissions.unshift(permission);
            });
            this.update(() => {}, scatter);
        })
    }

附加background.js方法

在chrome打開地址 chrome://extensions/,點擊background page打開調試終端,開始調試。

EOS time_point_sec 时间计算

EOS专门对时间计算进行了封装,在eosiolib/time.hpp中提供了多种关于时间计算的封装,简单介绍一下time_point_sec的用法

auto time_now = time_point_sec(now()/*获取当前区块时间*/);
uint32_t five_minutes = 5 * 60;
five_minutes_later = time_now + five_minutes;
five_minutes_later > time_now;

得到的time_point_sec类型可以与uint32_t类型进行简单的加/减数值运算,同类型间也可以直接用大于/小于等比较符进行运算,满足大多数时间运算需求了已经。
转载自:github

eosio::token.get_balance 合约内获取帐户代币余额

开发的DAPP需要获取另一个帐户的EOS余额,看遍了EOS源码eosiolib下的头文件,觉得只有currency.hpp最靠谱,尝试使用该头文件下的get_balance方法,但是一直没能成功,官方又没有文档,最终放弃。后来又尝试了eosio.token/eosio.token.hpp竟然让我成功了,这里介绍一下用法:
安装eos的时候并不会把eosio.token放到你的依赖库路径下,所以需要我们自己手动链接一下依赖库

ln -s /your-eos-path/contracts/eosio.token /usr/local/include/

用的时候需要额外带上一个头文件

#include <eosio.token/eosio.token.hpp>

用起来还是很简单的

auto eos_token = eosio::token(N(eosio.token)/* 合约账户名 */);
auto balance = eos_token.get_balance(owner/* 要查的账户名 */, symbol_type(S(4, EOS)).name());
print("eos balance: ", balance.amount);

需要注意的是get_balance的第二个参数为symbol_name,而不是symbol,所以需要先symbol_type(S(4, EOS))构造出symbol后调用name()方法获取。否则会一直提示你找不到对应的余额,主要原因是在于S(4, EOS)构造出的64位无符号数中最后八位代表的是精度,而余额表中的键值不需要用到精度,所以存的时候去掉了最后8位,所以如果直接传入S(4, EOS)会查不出来。

转载自:github

ubuntu搭建个人WDCP

1. 下载wdcP

wget http://dl.wdlinux.cn/files/lanmp_v3.tar.gz

2. 解压

tar -xzvf lanmp_v3.tar.gz

3. 编译

sudo sh lanmp.sh

如果出现以下错误

编译中,开始报错:81: [: !=: unexpected operator 类似这样的编译错误,各种操作符不允许,不识别!

解决此问题的方法

sudo dpkg-reconfigure dash


选择 <NO>
然后再次编译,这个过程会很长,大概15分钟的样子~

OK,这个时候测试一下:
浏览器输入http://本地ip:8080
访问后台

默认用户名: admin
默认密码: wdlinux.cn