第一种,使用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, ["EOS61VncKc7P8MhKzz8K7s3kAeNxFsp5ZQGoVFbLjRh1NVR1B6D9Z"], 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)
}
}