eosjs与scatter桌面应用程序使用eos测试网完成互操作,完成登陆,转账
1前言
在前一篇文章https://blog.****.net/w88193363/article/details/86524655,已经介绍了如何安装scatter桌面应用程序以及配置自己搭建的eos测试网,现在我们开始使用eosjs通过eos测试网完成登陆,转账。
2下载demo
下载github上的demo,地址:https://github.com/MediShares/scatter-eos-sample/tree/master/eos/sample01
3修改文件
这是麦子官方的demo,根据自己的情况还需要修改一些配置,修改index.html
// mainnet
var chainId = 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906';//修改为自己的chainid
//var endpoint = 'https://mainnet.eoscannon.io';
//修改为自己的地址
var endpoint = 'http://127.0.0.1:8888';//修改为自己的nodeos服务地址及端口
network = {
blockchain: 'eos',
host: host[0],
// port: host.length > 1 ? host[0] : (httpEndpoint[0].toLowerCase() == 'https' ? 443 : 8888),
port: 8888,//修改为自己的port,为硬编码8888,原来的会有问题,
chainId: chainId,
protocol: httpEndpoint[0],
httpEndpoint : endpoint,
};
// 转账
function transfer(){
if (currentAccount == null) {
alert('请先登录');
}
var eos = scatter.eos(network, Eos);
eos.transaction({
actions: [
{
account: 'eosio.token',
name: 'transfer',
authorization: [{
actor: currentAccount.name,
permission: currentAccount.authority
}],
data: {
from: currentAccount.name,
to: 'lsj',//修改为测试网上已注册的测试账户
quantity: '0.0001 SYS',//修改为自己的系统token,一般测试网没有修改的话为SYS
memo: 'scatter 转账测试'
}
}
]
}).then(result => {
alert('success!');
}).catch(error => {
alert('error:'+JSON.stringify(error));
});
}
4部署过程中的问题
调试时曾经出现过,部署eosjs的demo到apache服务器,请求nodeos的http服务时,出现跨域请求的问题,提示错误是:Access-Control-Allow-Origin,这时就需要修改nodeos的配置,增加:
access-control-allow-origin = *
允许nodeos跨域请求
5测试功能
单击网页的“登陆”按钮,
会弹出登陆界面
单击“login”,提示登陆成功单击“确定”,然后单击网页的“转账”按钮
scatter弹出如下界面
会提示转账的信息,单击“Allow”按钮,即可完成交易。
查看scatter,可看到余额已经发生变化至此,已经完成eosjs的登陆,以及转账