newpay-wallet-js
Version:
151 lines (127 loc) • 5.74 kB
JavaScript
import WalletUnlockActions from "./WalletUnlockActions.js";
import WalletDb from "./WalletDb.js";
import {Aes, ChainValidation, TransactionBuilder, TransactionHelper, ops, FetchChain, ChainStore} from "bitsharesjs/es";
import {Apis} from "bitsharesjs-ws";
class ApplicationApi {
/**
@param propose_account (or null) pays the fee to create the proposal, also used as memo from
application_api.transfer(
"tmp10099",
"cui-yujie",
1*10000,
"1.3.0",
'备注')
*/
transfer( // OBJECT: { ... }
from_account,
to_account,
amount, //用户输入的钱*小数的精度,从查询余额里面拿到
asset, //资源ID,比如"1.3.0",从查询余额里面拿到
memo,
broadcast = true,
encrypt_memo = true,
optional_nonce = null,
propose_account = null,
fee_asset_id = "1.3.0"
) {
let memo_sender = propose_account || from_account;
let unlock_promise = WalletUnlockActions.unlock();
fee_asset_id = asset;
memo = memo ? new Buffer(memo, "utf-8") : memo;
return Promise.all([
//FetchChain("getAccount", from_account),
//FetchChain("getAccount", to_account),
//FetchChain("getAccount", memo_sender),
//FetchChain("getAccount", propose_account),
//FetchChain("getAsset", asset),
//FetchChain("getAsset", fee_asset_id),
Apis.instance().db_api().exec("get_full_accounts", [[from_account], true]),
Apis.instance().db_api().exec("get_full_accounts", [[to_account], true]),
Apis.instance().db_api().exec("get_full_accounts", [[memo_sender], true]),
Apis.instance().db_api().exec("get_objects", [[asset]]),
Apis.instance().db_api().exec("get_objects", [[fee_asset_id]])
]).then((res)=> {
let [
chain_from, chain_to, chain_memo_sender,
chain_asset, chain_fee_asset
] = res;
let chain_from_account = chain_from[0][1].account;
let chain_to_account = chain_to[0][1].account;
let chain_memo_sender_account = chain_memo_sender[0][1].account;
let memo_from_public, memo_to_public;
if( memo && encrypt_memo ) {
memo_from_public = chain_memo_sender_account.options.memo_key;//chain_memo_sender.getIn(["options","memo_key"]);
// The 1s are base58 for all zeros (null)
if( /111111111111111111111/.test(memo_from_public)) {
memo_from_public = null;
}
memo_to_public = chain_to_account.options.memo_key;//chain_to.getIn(["options","memo_key"])
if( /111111111111111111111/.test(memo_to_public)) {
memo_to_public = null
}
}
let propose_acount_id = null
let memo_from_privkey;
if(encrypt_memo && memo ) {
memo_from_privkey = WalletDb.getPrivateKey(memo_from_public);
if(! memo_from_privkey) {
throw new Error("Missing private memo key for sender: " + memo_sender)
}
}
let memo_object;
if(memo && memo_to_public && memo_from_public) {
let nonce = optional_nonce == null ?
TransactionHelper.unique_nonce_uint64() :
optional_nonce;
memo_object = {
from: memo_from_public,
to: memo_to_public,
nonce,
message: (encrypt_memo) ?
Aes.encrypt_with_checksum(
memo_from_privkey,
memo_to_public,
nonce,
memo
) :
Buffer.isBuffer(memo) ? memo.toString("utf-8") : memo
};
}
// Allow user to choose asset with which to pay fees #356
let fee_asset = chain_fee_asset[0];//chain_fee_asset.toJS();
// Default to CORE in case of faulty core_exchange_rate
if( fee_asset.options.core_exchange_rate.base.asset_id === "1.3.0" &&
fee_asset.options.core_exchange_rate.quote.asset_id === "1.3.0" ) {
fee_asset_id = "1.3.0";
}
let tr = new TransactionBuilder();
let transfer_op = tr.get_type_operation("transfer", {
fee: {
amount: 0,
asset_id: fee_asset_id
},
from: chain_from_account.id,//chain_from.get("id"),
to: chain_to_account.id,//chain_to.get("id"),
//amount: { amount, asset_id: chain_asset.get("id") },
amount: { amount, asset_id: chain_asset[0].id },
memo: memo_object
});
return tr.update_head_block().then(() => {
if( propose_account ) {
tr.add_type_operation("proposal_create", {
proposed_ops: [{ op: transfer_op }],
fee_paying_account: propose_acount_id
});
} else {
tr.add_operation( transfer_op );
}
return WalletDb.process_transaction(
tr,
null, //signer_private_keys,
broadcast
)
});
});
}
}
export default ApplicationApi;