nodejs开发EOS转账服务的两种方案分别是什么
本篇文章为大家展示了nodejs开发EOS转账服务的两种方案分别是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联公司是一家专注于网站制作、成都网站制作与策划设计,晋城网站建设哪家好?创新互联公司做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:晋城等地区。晋城做网站价格咨询:18982081108
第一种,使用eosjs开发,适用于用户输入私钥方式,不安全,易造成私钥泄露。
const rpcUrl = 'http://jungle2.cryptolions.io:80' const { Api, JsonRpc, RpcError, JsSignatureProvider } = require('eosjs'); const ecc = require('eosjs-ecc'); const fetch = require('node-fetch'); const { TextDecoder, TextEncoder } = require('text-encoding'); const rpc = new JsonRpc(rpcUrl, { fetch }); //转账操作 转账到账户accountName 'eosaccountb2';转账数额quantity '1.1234 EOS' async function transfer(accountName,quantity) { let signatureProvider = new JsSignatureProvider([pkeys[0].privateKey]); let api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() }); let result = await api.transact({ actions: [{ account: 'eosio.token', name: 'transfer', authorization: [{ actor: pkeys[0].actor, permission: 'active', }], data: { from: pkeys[0].actor, to: accountName, //提现地址 quantity: quantity, //提现数量 memo: '', }, }] }, { blocksBehind: 3, expireSeconds: 30, }); console.dir(result); };
第二种,使用nodeos api+wallet api开发,适合用于公共账户给不同用户转账。相当于用wallet钱包服务来做密钥管理,隐藏了私钥,代码中只需提供公钥和钱包服务地址即可。
async function transfer() { try { let actor = "eosaccountaa" let transferTo = "eosaccountbb" let quantity = "1.1234 EOS" let memo = "hi heere" let blocksBehind = 3 let expireSeconds = 100 let info = await rpc.get_info(); if (info != null && info.chain_id != null && info.head_block_num != null) { let chain_id = info.chain_id; let head_block_num = info.head_block_num - blocksBehind; let block = await get_block(head_block_num); if (block != null && block.ref_block_prefix != null && block.timestamp != null) { let data = await abi_json_to_bin(actor, transferTo, quantity, memo) if (data != null) { let transactions = { "max_net_usage_words": 0, "max_cpu_usage_ms": 0, "delay_sec": 0, "context_free_actions": [], "actions": [{ "account": "eosio.token", "name": "transfer", "authorization": [{ "actor": actor, "permission": "active" }], "data": data }], "transaction_extensions": [], "expiration": ser.timePointSecToDate(ser.dateToTimePointSec(block.timestamp) + expireSeconds), "ref_block_num": block.block_num & 0xffff, "ref_block_prefix": block.ref_block_prefix }; let signTransaction = await sign_transaction(transactions, ["EOS61VncKc7P8MhKzz8K7s3kAeNxFsp5ZQGoVFbLjRh2NVR1B6D9Z"], chain_id); if (signTransaction != null && signTransaction.signatures != null) { var transaction_detail = await push_transaction(transactions, signTransaction.signatures); console.log('push_transaction=transaction_id==' + transaction_detail.transaction_id); } } } } } catch (e) { console.log(e) } }
上述内容就是nodejs开发EOS转账服务的两种方案分别是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
分享文章:nodejs开发EOS转账服务的两种方案分别是什么
本文地址:http://pwwzsj.com/article/jpidho.html