У меня есть закрытый ключ и адрес. Можно ли совершить транзакцию с этими параметрами, используя web3j.
Web3j web3j = Web3j.build(new HttpService(""));
Credentials credentials = Credentials.create("privateKey");
BigInteger nonce = web3j.ethGetTransactionCount(credentials.getAddress(), DefaultBlockParameterName.PENDING).sendAsync().get().getTransactionCount();
BigInteger gasPrice = web3j.ethGasPrice().sendAsync().get().getGasPrice();
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, Constants.ETH_GAS_LIMIT, "address", Convert.toWei("amount", Convert.Unit.ETHER).toBigIntegerExact());
EthSendTransaction response = web3j.ethSendRawTransaction(Numeric.toHexString(TransactionEncoder.signMessage(rawTransaction, credentials))).send();
String hash = response.getTransactionHash();
Если вы хотите отправить пользовательские токены в качестве владельца контракта, ознакомьтесь с моим ответом здесь: Отправить пользовательские токены ERC20 с одного адреса на другой.
Если вы хотите отправить токены от имени другого пользователя, идея аналогична.
Сначала извлеките учетные данные пользователя из закрытого ключа, затем выполните транзакцию.
Credentials getUserInfo (String privateKeyInHex){
BigInteger privateKeyInBT = new BigInteger(privateKeyInHex, 16);
ECKeyPair aPair = ECKeyPair.create(privateKeyInBT);
Credentials aCredential = Credentials.create(aPair);
return aCredential;
}
отправить токены:
process(){
...
Credentials newUser = getUserInfo(privateKeyInHex);
TransactionReceiptProcessor transactionReceiptProcessor = new NoOpProcessor(web3);
TransactionManager transactionManager = new RawTransactionManager(
web3, newUser, ChainId.MAINNET, transactionReceiptProcessor);
//if testing, use ChainId.ROPSTEN
...
Да, транзакцию можно отправить. Используйте web3.eth.sendTransaction({to:'0xaddress', from:'0xaddress', value:web3.toWei(.033,'ether'),gas:30000})
web3j
неweb3js
Санджай Рао
Санджай Рао