首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用web3j进行离线事务签名?

用web3j进行离线事务签名?
EN

Ethereum用户
提问于 2019-04-05 15:04:56
回答 3查看 2.5K关注 0票数 3

我想要生成和签署一个事务离线,然后用不同的帐户发送该事务。我已经找到了一些web3js源代码,并阅读了web3文档,但是我还没有找到任何用web3j来解决这个问题的明确解决方案。谢谢你的帮助。

EN

回答 3

Ethereum用户

回答已采纳

发布于 2019-04-07 11:29:56

我觉得像这样的事应该管用

代码语言:javascript
复制
// Create the Transaction
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(<nonce>, <gasPrice>, 
       <gasLimit>, <to>, <value>);  

// Sign the Transaction
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, <your-Credentials>);

String hexValue = Numeric.toHexString(signedMessage);

然后将hexValue给第三方,以便将其广播到网络上。

Update:现在您需要方法singMessage的链ID,如下所示:

代码语言:javascript
复制
long chainId = <the id of the used chain>
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId , <you Credentials>);
票数 5
EN

Ethereum用户

发布于 2019-04-05 15:17:03

您可能需要深入研究SignedRawTransactionTransactionEncoder类。

票数 1
EN

Ethereum用户

发布于 2022-07-11 09:50:59

Majd TL的回答是正确的。下面是使用Web3j创建原始事务的整个简单实现。我相信这对一个人来说是很方便的。

代码语言:javascript
复制
final Web3j web3j = Web3j.build(new HttpService("<node>"));

final Credentials credentials = Credentials.create("*******");
final String to = "0x*****";
final String amount = "0.1";
final long ropstenChainId = 3;

EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(credentials.getAddress(), DefaultBlockParameterName.PENDING).send();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();

BigInteger gasPrice = web3j.ethGasPrice().send().getGasPrice();
BigInteger gasLimit = DefaultGasProvider.GAS_LIMIT;

RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger());

byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ropstenChainId, credentials);

String hexValue = Numeric.toHexString(signedMessage);
System.out.println(hexValue);

和你能在这里找到的正确的chainId - https://chainlist.org/

票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/69319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档