• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java TransactionEncoder类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.web3j.crypto.TransactionEncoder的典型用法代码示例。如果您正苦于以下问题:Java TransactionEncoder类的具体用法?Java TransactionEncoder怎么用?Java TransactionEncoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



TransactionEncoder类属于org.web3j.crypto包,在下文中一共展示了TransactionEncoder类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: testCreateAccountFromScratch

import org.web3j.crypto.TransactionEncoder; //导入依赖的package包/类
@Test
public void testCreateAccountFromScratch() throws Exception {
	
	// create new private/public key pair
	ECKeyPair keyPair = Keys.createEcKeyPair();
	
	BigInteger publicKey = keyPair.getPublicKey();
	String publicKeyHex = Numeric.toHexStringWithPrefix(publicKey);
	
	BigInteger privateKey = keyPair.getPrivateKey();
	String privateKeyHex = Numeric.toHexStringWithPrefix(privateKey);
	
	// create credentials + address from private/public key pair
	Credentials credentials = Credentials.create(new ECKeyPair(privateKey, publicKey));
	String address = credentials.getAddress();
	
	// print resulting data of new account
	System.out.println("private key: '" + privateKeyHex + "'");
	System.out.println("public key: '" + publicKeyHex + "'");
	System.out.println("address: '" + address + "'\n");
	
	// test (1) check if it's possible to transfer funds to new address
	BigInteger amountWei = Convert.toWei("0.131313", Convert.Unit.ETHER).toBigInteger();
	transferWei(getCoinbase(), address, amountWei);

	BigInteger balanceWei = getBalanceWei(address);
	BigInteger nonce = getNonce(address);
	
	assertEquals("Unexpected nonce for 'to' address", BigInteger.ZERO, nonce);
	assertEquals("Unexpected balance for 'to' address", amountWei, balanceWei);

	// test (2) funds can be transferred out of the newly created account
	BigInteger txFees = Web3jConstants.GAS_LIMIT_ETHER_TX.multiply(Web3jConstants.GAS_PRICE);
	RawTransaction txRaw = RawTransaction
			.createEtherTransaction(
					nonce, 
					Web3jConstants.GAS_PRICE, 
					Web3jConstants.GAS_LIMIT_ETHER_TX, 
					getCoinbase(), 
					amountWei.subtract(txFees));

	// sign raw transaction using the sender's credentials
	byte[] txSignedBytes = TransactionEncoder.signMessage(txRaw, credentials);
	String txSigned = Numeric.toHexString(txSignedBytes);

	// send the signed transaction to the ethereum client
	EthSendTransaction ethSendTx = web3j
			.ethSendRawTransaction(txSigned)
			.sendAsync()
			.get();

	Error error = ethSendTx.getError();
	String txHash = ethSendTx.getTransactionHash();
	assertNull(error);		
	assertFalse(txHash.isEmpty());
	
	waitForReceipt(txHash);

	assertEquals("Unexpected nonce for 'to' address", BigInteger.ONE, getNonce(address));
	assertTrue("Balance for 'from' address too large: " + getBalanceWei(address), getBalanceWei(address).compareTo(txFees) < 0);
}
 
开发者ID:matthiaszimmermann,项目名称:web3j_demo,代码行数:62,代码来源:CreateAccountTest.java


示例2: execute

import org.web3j.crypto.TransactionEncoder; //导入依赖的package包/类
private String execute(
        Credentials credentials, Function function, String contractAddress) throws Exception {
    BigInteger nonce = getNonce(credentials.getAddress());

    String encodedFunction = FunctionEncoder.encode(function);

    RawTransaction rawTransaction = RawTransaction.createTransaction(
            nonce,
            GAS_PRICE,
            GAS_LIMIT,
            contractAddress,
            encodedFunction);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue)
            .sendAsync().get();

    return transactionResponse.getTransactionHash();
}
 
开发者ID:web3j,项目名称:web3j,代码行数:22,代码来源:HumanStandardTokenIT.java


示例3: sendTransactionToken

import org.web3j.crypto.TransactionEncoder; //导入依赖的package包/类
public void sendTransactionToken(
    AtlantClient atlantClient,
    Nonce nonce,
    GasPrice gasPrice,
    String address,
    String value,
    Credentials credentials,
    long gasLimit,
    String tokenAddress,
    long tokenID
) throws Exception {
  String s;

  s = inputData(tokenID, address, value);

  RawTransaction rawTransaction = RawTransaction
      .createTransaction(DigitsUtils.getBase10from16(nonce.getResult()),
          DigitsUtils.getBase10from16(gasPrice.getResult()),
          BigInteger.valueOf(gasLimit), tokenAddress, BigInteger.ZERO, s);

  byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
  String hexValue = Hex.toHexString(signedMessage);
  callSendTransactions = atlantClient.sendTransaction(callbackSendTransactions, hexValue);
}
 
开发者ID:AtlantPlatform,项目名称:atlant-android,代码行数:25,代码来源:TransactionRestHandler.java


示例4: sendTransaction

import org.web3j.crypto.TransactionEncoder; //导入依赖的package包/类
public void sendTransaction(
    AtlantClient atlantClient,
    Nonce nonce,
    GasPrice gasPrice,
    String address,
    String value,
    Credentials credentials,
    long gasLimit
) throws Exception {

  RawTransaction rawTransaction = RawTransaction
      .createTransaction(
          DigitsUtils.getBase10from16(nonce.getResult()),
          DigitsUtils.getBase10from16(gasPrice.getResult()),
          BigInteger.valueOf(gasLimit), address, new BigInteger(stringValueFormat(value, 10)), "");

  byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
  String hexValue = Hex.toHexString(signedMessage);
  callSendTransactions = atlantClient.sendTransaction(callbackSendTransactions, hexValue);
}
 
开发者ID:AtlantPlatform,项目名称:atlant-android,代码行数:21,代码来源:TransactionRestHandler.java


示例5: testTransferEther

import org.web3j.crypto.TransactionEncoder; //导入依赖的package包/类
@Test
public void testTransferEther() throws Exception {
    BigInteger nonce = getNonce(ALICE.getAddress());
    RawTransaction rawTransaction = createEtherTransaction(
            nonce, BOB.getAddress());

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction ethSendTransaction =
            web3j.ethSendRawTransaction(hexValue).sendAsync().get();
    String transactionHash = ethSendTransaction.getTransactionHash();

    assertFalse(transactionHash.isEmpty());

    TransactionReceipt transactionReceipt =
            waitForTransactionReceipt(transactionHash);

    assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
}
 
开发者ID:web3j,项目名称:web3j,代码行数:21,代码来源:CreateRawTransactionIT.java


示例6: testDeploySmartContract

import org.web3j.crypto.TransactionEncoder; //导入依赖的package包/类
@Test
public void testDeploySmartContract() throws Exception {
    BigInteger nonce = getNonce(ALICE.getAddress());
    RawTransaction rawTransaction = createSmartContractTransaction(nonce);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction ethSendTransaction =
            web3j.ethSendRawTransaction(hexValue).sendAsync().get();
    String transactionHash = ethSendTransaction.getTransactionHash();

    assertFalse(transactionHash.isEmpty());

    TransactionReceipt transactionReceipt =
            waitForTransactionReceipt(transactionHash);

    assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));

    assertFalse("Contract execution ran out of gas",
            rawTransaction.getGasLimit().equals(transactionReceipt.getGasUsed()));
}
 
开发者ID:web3j,项目名称:web3j,代码行数:23,代码来源:CreateRawTransactionIT.java


示例7: testSignTransaction

import org.web3j.crypto.TransactionEncoder; //导入依赖的package包/类
@Test
public void testSignTransaction() throws Exception {
    boolean accountUnlocked = unlockAccount();
    assertTrue(accountUnlocked);

    RawTransaction rawTransaction = createTransaction();

    byte[] encoded = TransactionEncoder.encode(rawTransaction);
    byte[] hashed = Hash.sha3(encoded);

    EthSign ethSign = web3j.ethSign(ALICE.getAddress(), Numeric.toHexString(hashed))
            .sendAsync().get();

    String signature = ethSign.getSignature();
    assertNotNull(signature);
    assertFalse(signature.isEmpty());
}
 
开发者ID:web3j,项目名称:web3j,代码行数:18,代码来源:SignTransactionIT.java


示例8: createSignedTransaction

import org.web3j.crypto.TransactionEncoder; //导入依赖的package包/类
public Transaction createSignedTransaction(String to, BigInteger amountWei, String data, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit) {
  Transaction tx = new Transaction(to, amountWei, data, nonce, gasPrice, gasLimit);

  tx.setFromAddress(getAddress());
  byte[] signedMessage = TransactionEncoder.signMessage(tx.getRawTransaction(), getCredentials());
  tx.setSignedContent(Numeric.toHexString(signedMessage));

  return tx;
}
 
开发者ID:BSI-Business-Systems-Integration-AG,项目名称:trading-network,代码行数:10,代码来源:Account.java


示例9: sendCreateContractTransaction

import org.web3j.crypto.TransactionEncoder; //导入依赖的package包/类
private String sendCreateContractTransaction(
        Credentials credentials, BigInteger initialSupply) throws Exception {
    BigInteger nonce = getNonce(credentials.getAddress());

    String encodedConstructor =
            FunctionEncoder.encodeConstructor(
                    Arrays.asList(
                            new Uint256(initialSupply),
                            new Utf8String("web3j tokens"),
                            new Uint8(BigInteger.TEN),
                            new Utf8String("w3j$")));

    RawTransaction rawTransaction = RawTransaction.createContractTransaction(
            nonce,
            GAS_PRICE,
            GAS_LIMIT,
            BigInteger.ZERO,
            getHumanStandardTokenBinary() + encodedConstructor);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue)
            .sendAsync().get();

    return transactionResponse.getTransactionHash();
}
 
开发者ID:web3j,项目名称:web3j,代码行数:28,代码来源:HumanStandardTokenIT.java


示例10: signAndSend

import org.web3j.crypto.TransactionEncoder; //导入依赖的package包/类
public EthSendTransaction signAndSend(RawTransaction rawTransaction)
        throws IOException {

    byte[] signedMessage;

    if (chainId > ChainId.NONE) {
        signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
    } else {
        signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    }

    String hexValue = Numeric.toHexString(signedMessage);

    return web3j.ethSendRawTransaction(hexValue).send();
}
 
开发者ID:web3j,项目名称:web3j,代码行数:16,代码来源:RawTransactionManager.java


示例11: createOfflineTx

import org.web3j.crypto.TransactionEncoder; //导入依赖的package包/类
public String createOfflineTx(String toAddress, BigInteger gasPrice, BigInteger gasLimit, BigInteger amount, BigInteger nonce) {
	RawTransaction rawTransaction  = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, toAddress, amount);
	byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
	String hexValue = Numeric.toHexString(signedMessage);
	
	return hexValue;
}
 
开发者ID:matthiaszimmermann,项目名称:ethereum-paper-wallet,代码行数:8,代码来源:PaperWallet.java


示例12: testCreateSignAndSendTransaction

import org.web3j.crypto.TransactionEncoder; //导入依赖的package包/类
/**
 * Ether transfer tests using methods {@link RawTransaction#createEtherTransaction()}, {@link TransactionEncoder#signMessage()} and {@link Web3j#ethSendRawTransaction()}.
 * Most complex transfer mechanism, but offers the highest flexibility.   
 */
@Test
public void testCreateSignAndSendTransaction() throws Exception {

	String from = Alice.ADDRESS;
	Credentials credentials = Alice.CREDENTIALS;
	BigInteger nonce = getNonce(from);
	String to = Bob.ADDRESS; 
	BigInteger amountWei = Convert.toWei("0.789", Convert.Unit.ETHER).toBigInteger();

	// create raw transaction
	RawTransaction txRaw = RawTransaction
			.createEtherTransaction(
					nonce, 
					Web3jConstants.GAS_PRICE, 
					Web3jConstants.GAS_LIMIT_ETHER_TX, 
					to, 
					amountWei);

	// sign raw transaction using the sender's credentials
	byte[] txSignedBytes = TransactionEncoder.signMessage(txRaw, credentials);
	String txSigned = Numeric.toHexString(txSignedBytes);

	BigInteger txFeeEstimate = Web3jConstants.GAS_LIMIT_ETHER_TX.multiply(Web3jConstants.GAS_PRICE);

	// make sure sender has sufficient funds
	ensureFunds(Alice.ADDRESS, amountWei.add(txFeeEstimate));

	// record balanances before the ether transfer
	BigInteger fromBalanceBefore = getBalanceWei(Alice.ADDRESS);
	BigInteger toBalanceBefore = getBalanceWei(Bob.ADDRESS);

	// send the signed transaction to the ethereum client
	EthSendTransaction ethSendTx = web3j
			.ethSendRawTransaction(txSigned)
			.sendAsync()
			.get();

	Error error = ethSendTx.getError();
	assertTrue(error == null);
	
	String txHash = ethSendTx.getTransactionHash();
	assertFalse(txHash.isEmpty());
	
	TransactionReceipt txReceipt = waitForReceipt(txHash);
	BigInteger txFee = txReceipt.getCumulativeGasUsed().multiply(Web3jConstants.GAS_PRICE);

	assertEquals("Unexected balance for 'from' address", fromBalanceBefore.subtract(amountWei.add(txFee)), getBalanceWei(from));
	assertEquals("Unexected balance for 'to' address", toBalanceBefore.add(amountWei), getBalanceWei(to));
}
 
开发者ID:matthiaszimmermann,项目名称:web3j_demo,代码行数:54,代码来源:TransferEtherTest.java



注:本文中的org.web3j.crypto.TransactionEncoder类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java ArgumentValidationException类代码示例发布时间:2022-05-22
下一篇:
Java ServiceSpec类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap