EOSEssentials / eos-java-rpc-wrapper

http://www.eos42.io
MIT License
161 stars 76 forks source link

getAccount() && Signing and pushing a transaction #7

Closed jaybxyz closed 6 years ago

jaybxyz commented 6 years ago

I would like to test if it it works well to get account's info and transfer EOS using this project.

First, I created the rest client like below and tested to see it if connects.

eosApiRestClient.getAccount("MY ACCT NAME") doesn't seem to work

    EosApiRestClient<?> eosApiRestClient =
        EosApiClientFactory.newInstance("https://api.eosnewyork.io:443").newRestClient();

    // Working
    System.out.println(eosApiRestClient.getChainInfo().getChainId());
    System.out.println(eosApiRestClient.getChainInfo().getHeadBlockNum());
    System.out.println(eosApiRestClient.getChainInfo().getLastIrreversibleBlockNum());

    // Working
    Block block = eosApiRestClient.getBlock(eosApiRestClient.getChainInfo().getHeadBlockId());
    System.out.println(block);

    // Not Working 
    // System.out.println(eosApiRestClient.getAccount("MY ACCT NAME")); 

Don't you need to inject the account's private key of active key (called keyProvider in eosjs) when signing occurs?

From my experience of using eosjs, you need the private key of sender's account to transfer EOS

const config = {
    chainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906', 
    keyProvider: [ privKey ], // LIKE HERE
    httpEndpoint: 'https://api.eosnewyork.io:443',
    verbose: false, // API activity
    broadcast: true,
    sign: true,
    expireInSeconds: 60
}

I don't seem to find where to inject the private key when signing occurs...

Is this part where  you inject the private key? 

eosApiRestClient.signTransaction(packedTransaction, Collections.singletonList("EOS7LPJ7YnwYiEHbBLz96fNkt3kf6CDDdesV5EsWoc3u3DJy31V2y"), "chainId");
/* Create the rest client */ 
EosApiRestClient eosApiRestClient = EosApiClientFactory.newInstance("https://api.eosnewyork.io:443").newRestClient();

/* Create the json array of arguments */
Map<String, String> args = new HashMap<>(4);
args.put("from", "currency"); // SENDER (MY PRE-GENERATED EOS ACCOUNT)
args.put("to", "eosio"); // RECEIVER
args.put("quantity", "0.1000 EOS"); // AMOUNT
args.put("memo", "My First Transaction"); // JUST MEMO
AbiJsonToBin data = eosApiRestClient.abiJsonToBin("currency", "transfer", args);```

/* Get the head block */
Block block = eosApiRestClient.getBlock(eosApiRestClient.getChainInfo().getHeadBlockId());

/* Create Transaction Action Authorization */
TransactionAuthorization transactionAuthorization = new TransactionAuthorization();
transactionAuthorization.setActor("currency");
transactionAuthorization.setPermission("active");

/* Create Transaction Action */
TransactionAction transactionAction = new TransactionAction();
transactionAction.setAccount("currency");
transactionAction.setName("transfer");
transactionAction.setData(data.getBinargs());
transactionAction.setAuthorization(Collections.singletonList(transactionAuthorization));

/* Create a transaction */
PackedTransaction packedTransaction = new PackedTransaction();
packedTransaction.setRefBlockPrefix(block.getRefBlockPrefix().toString());
packedTransaction.setRefBlockNum(block.getBlockNum().toString());
packedTransaction.setExpiration("2018-05-10T18:38:19");
packedTransaction.setRegion("0");
packedTransaction.setMax_net_usage_words("0");
packedTransaction.setContextFreeData(Collections.emptyList());
packedTransaction.setContextFreeActions(Collections.emptyList());
packedTransaction.setActions(Collections.singletonList(transactionAction));

/* Sign the Transaction */
SignedPackedTransaction signedPackedTransaction = eosApiRestClient.signTransaction(packedTransaction, Collections.singletonList("EOS7LPJ7YnwYiEHbBLz96fNkt3kf6CDDdesV5EsWoc3u3DJy31V2y"), "chainId");

/* Push the transaction */
PushedTransaction = eosApiRestClient.pushTransaction("none", signedPackedTransaction);
spebern commented 6 years ago

You don't need to inject private keys, you only need the public key and need to make sure that the wallet containing the corresponding public key is:

  1. open
  2. unlocked
jaybxyz commented 6 years ago

If I am not using my own eosio (using one of Block Producer's eosio), I believe their wallet doesn't contain my corresponding public key. Or do they contain (imported) all accounts already?

I thought I should sign the transaction using my account's private key and send it to network.

In other blockchain like bitcoin, to simply explain, you sign the transaction using your private key proving that you are the owner of this transaction and send that transaction to the network.

spebern commented 6 years ago

Other wallets should not contain your private keys. You need a wallet running on your local machine or on a server that you trust.

However this library currently uses the same base url for all kinds of queries.

There should definitely be an option to use different base urls for history/chain/wallet queries. Thanks for pointing that out.

jaybxyz commented 6 years ago

Yes, other wallet shouldn't contain anyone's private key.

Well, my purpose is to use one of the BP's public API endpoint instead of running on my local machine or building one myself on a server that I trust.

In that sense, I believe there needs my account's private key to sign the transaction or create 3rd user's account using my account.

Is it possible to make a transaction using a public api endpoint?

Have you been testing using on your local only?

spebern commented 6 years ago

Yes, I used it only locally so far.

You need a wallet running somewhere, because this package doesn't take care of signing, even if you would provide a private key somewhere (which I am not sure if supported by the http api).

For signing it relies on separate software.

spebern commented 6 years ago

I'll look into adding additional parameters for different base urls (chain, history and wallet).

spebern commented 6 years ago

10

jaybxyz commented 6 years ago

I will play around more for now.