eosjs

  Javascript API,用于帮助访问与 EOSIO RPC API.

1、安装

npm install eosjs@beta

2、Signature Provider

  The Signature Provider holds private keys and is responsible for signing transactions.

const defaultPrivateKey = "5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr"; // useraaaaaaaa
const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);

  SignatureProvider是一个接口,不能直接使用。

  

  JsSignatureProvider 实现了 SignatureProvider接口。是唯一实现了 SignatureProvider 接口的类。应当使用这个类。

  

  JsSignatureProvider只两个方法:

  

3、JSON-RPC

  Open a connection to JSON-RPC, include fetch when on NodeJS.

const rpc = new JsonRpc('http://127.0.0.1:8888', { fetch });

  JSON-RPC 实现了 AuthorityProvider、AbiProvider 两个Interface。

  

  

  RPC 提供以下的方法:

  

4、API

  实现了签名相关功能的类。

const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });

  transact() is used to sign and push transactions onto the blockchain with an optional configuration object parameter.

  通常使用 transaction 方法,发起一个交易:

(async () => {
const result = await api.transact({
actions: [{
account: 'eosio.token',
name: 'transfer',
authorization: [{
actor: 'useraaaaaaaa',
permission: 'active',
}],
data: {
from: 'useraaaaaaaa',
to: 'useraaaaaaab',
quantity: '0.0001 SYS',
memo: '',
},
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.dir(result);
})();

  transact是一个 async主法:

  

  API 对象提供以下主法:

  

5、通常的使用流程。

  使用 JsonRpc 对象、JsSignatureProvider 对象来创建一个 Api对象。

const { Api, JsonRpc } = require('eosjs');
const JsSignatureProvider = require('eosjs/dist/eosjs-jssig'); // development only
const fetch = require('node-fetch'); // node only; not needed in browsers
const { TextDecoder, TextEncoder } = require('text-encoding'); // node, IE11 and IE Edge Browsers const privateKeys = [privateKey1]; const signatureProvider = new JsSignatureProvider(privateKeys);
const rpc = new JsonRpc('http://127.0.0.1:8888', { fetch });
const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });

  后续就使用 api.transact 来发起交易:

const result = await api.transact({
actions: [{
account: 'eosio',
name: 'buyrambytes',
authorization: [{
actor: 'useraaaaaaaa',
permission: 'active',
}],
data: {
payer: 'useraaaaaaaa',
receiver: 'useraaaaaaaa',
bytes: 8192,
},
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});

参考:

1、https://eosio.github.io/eosjs/

2、https://github.com/EOSIO/eosjs/tree/08b474e8c25ccccf2701ca04268fa5149728baa1

eosjs的更多相关文章

  1. eos TODO EOS区块链上EOSJS和scatter开发dApp

    由于我一直在深入研究EOS dApp的开发,我看了不少好文章.在这里,我汇总了下做一些研究后得到的所有知识.在本文中,我将解释如何使用EOSJS和scatter.我假设你对智能合约以及如何在EOS区块 ...

  2. EOS开发实战

    EOS开发实战   在上一篇文章<EOS开发入门>中,我们为大家介绍了EOS的节点启动和合约部署和调用等入门知识.本次我们来实现一个复杂的例子,可以为其取一个高大上的名字-悬赏任务管理系统 ...

  3. fibos开发踩坑集合

    fibos.js API资料: 与eosjs相比,fibos.js没有添加新功能,可以在eosjs项目页面https://developers.eos.io/eosio-nodeos/referenc ...

  4. Setting up Scatter for Web Applications

    [Setting up Scatter for Web Applications] If you are still using scatter-js please move over to scat ...

  5. 【精解】EOS TPS 多维实测

    本文主要研究EOS的tps表现,会从插件.cleos.EOSBenchTool以及eosjs四种方式进行分析研究. 关键字:eos, tps, cleos, txn_test_gen_plugin, ...

  6. 【源码解读】EOS测试插件:txn_test_gen_plugin.cpp

    本文内容本属于<[精解]EOS TPS 多维实测>的内容,但由于在编写时篇幅过长,所以我决定将这一部分单独成文撰写,以便于理解. 关键字:eos, txn_test_gen_plugin, ...

  7. 怎样使用EOS.JS的API

    https://medium.com/coinmonks/how-to-use-eosjs-api-1-770b037b22ad https://blog.csdn.net/mongo_node/ar ...

  8. eos开发实践

    一 下载前端代码 git clone https://github.com/baidang201/eos-todo 二 安装nodejs sudo apt-get install python-sof ...

  9. EOS源码分析:transaction的一生

    最近在处理智能合约的事务上链问题,发现其中仍旧有知识盲点.原有的认识是一个事务请求会从客户端设备打包签名,然后通过RPC传到非出块节点,广播给超级节点,校验打包到可逆区块,共识确认最后变为不可逆区块. ...

随机推荐

  1. VSCode 常用的快捷键

    R键:点击后热加载,直接查看预览结果 P键: 在虚拟机中显示网格,常用 O 键:切换iOS 和Android Q键 :退出调试 ctr +~  打开 终端

  2. jq实时监测输入框内容改变

    $(document) .on('input propertychange','#telInput',function (e) { if (e.type === "input" | ...

  3. RabbitMQ安装记录(windows10)

    RabbitMQ安装记录(windows10)   一.安装包准备 otp_win64_R16B03.exe(这里使用该版本,不支持ssl) otp_win64_19.0.exe(如果要开启ssl,请 ...

  4. RabbitMQ python模块pika生产者消费者轮询模型。

    完整代码如下: 生产者,producer import pika connection = pika.BlockingConnection( pika.ConnectionParameters('lo ...

  5. Linux 分支那么多,这里可以帮你缩小选择范围

    Linux 分支那么多,这里可以帮你缩小选择范围 https://wiki.installgentoo.com/wiki/Babbies_First_Linux https://wiki.instal ...

  6. [Android]-图片JNI(C++\Java)高斯模糊的实现与比較

    版权声明:本文作者:Qiujuer https://github.com/qiujuer; 转载请注明出处,盗版必究! !! https://blog.csdn.net/qiujuer/article ...

  7. [随笔][Life][咖啡][咖啡分类]

    转载自:https://www.chalook.net/doc/201607/4710.shtml

  8. 接口测试工具SoapUI Pro5.1.2基本使用20150920

    soapui是接口测试工具,最近因为要做接口测试,使用了下,现在和大家分享下: 工具安装很简单,就不说了,直接说使用,先什么都不说,照着操作一遍,我们拿天气预报的webserver来实战: 主要包括: ...

  9. JavaScript开发中使用频率较高的一些方法

    1.填充字符串 ES7推出了字符串补全长度的功能.如果某个字符串不够指定长度,会在头部或尾部补全. String.prototype.padStart(maxLength, fillString=’ ...

  10. Pycharm初始创建项目和环境搭建

    Pycharm确实是一个非常不错的Python开发IDE,尤其对于初学者而言. 打开新建项目 1.选择新建一个Pure Python项目,新建项目路径可以在Location处选择. 2.Project ...