温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nodejs开发EOS转账服务的两种方案分别是什么

发布时间:2021-12-13 21:37:42 来源:亿速云 阅读:134 作者:柒染 栏目:互联网科技

本篇文章为大家展示了nodejs开发EOS转账服务的两种方案分别是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

第一种,使用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转账服务的两种方案分别是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI