当前位置: 首页>>代码示例>>Java>>正文


Java Numeric.toHexString方法代码示例

本文整理汇总了Java中org.web3j.utils.Numeric.toHexString方法的典型用法代码示例。如果您正苦于以下问题:Java Numeric.toHexString方法的具体用法?Java Numeric.toHexString怎么用?Java Numeric.toHexString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.web3j.utils.Numeric的用法示例。


在下文中一共展示了Numeric.toHexString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testCreateAccountFromScratch

import org.web3j.utils.Numeric; //导入方法依赖的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.utils.Numeric; //导入方法依赖的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: testTransferEther

import org.web3j.utils.Numeric; //导入方法依赖的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

示例4: testDeploySmartContract

import org.web3j.utils.Numeric; //导入方法依赖的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

示例5: sendCreateContractTransaction

import org.web3j.utils.Numeric; //导入方法依赖的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

示例6: testSignMessage

import org.web3j.utils.Numeric; //导入方法依赖的package包/类
@Test
public void testSignMessage() {
    byte[] signedMessage = TransactionEncoder.signMessage(
            createEtherTransaction(), SampleKeys.CREDENTIALS);
    String hexMessage = Numeric.toHexString(signedMessage);
    assertThat(hexMessage,
            is("0xf85580010a840add5355887fffffffffffffff80"
                    + "1c"
                    + "a046360b50498ddf5566551ce1ce69c46c565f1f478bb0ee680caf31fbc08ab727"
                    + "a01b2f1432de16d110407d544f519fc91b84c8e16d3b6ec899592d486a94974cd0"));
}
 
开发者ID:web3j,项目名称:web3j,代码行数:12,代码来源:TransactionEncoderTest.java

示例7: signAndSend

import org.web3j.utils.Numeric; //导入方法依赖的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

示例8: createOfflineTx

import org.web3j.utils.Numeric; //导入方法依赖的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

示例9: format

import org.web3j.utils.Numeric; //导入方法依赖的package包/类
@Override
public String format(byte[] value) {
    if (value == null)
        return "NULL";
    return Numeric.toHexString(value);
}
 
开发者ID:papyrusglobal,项目名称:state-channels,代码行数:7,代码来源:ByteArrayCodec.java

示例10: testCreateSignAndSendTransaction

import org.web3j.utils.Numeric; //导入方法依赖的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

示例11: byteToHex

import org.web3j.utils.Numeric; //导入方法依赖的package包/类
public static String byteToHex(byte[] bytes) {
    if (bytes == null) {
        return null;
    }
    return Numeric.toHexString(bytes);
}
 
开发者ID:SlotNSlot,项目名称:SlotNSlot_Android,代码行数:7,代码来源:Utils.java

示例12: buildEventSignature

import org.web3j.utils.Numeric; //导入方法依赖的package包/类
public static String buildEventSignature(String methodSignature) {
    byte[] input = methodSignature.getBytes();
    byte[] hash = Hash.sha3(input);
    return Numeric.toHexString(hash);
}
 
开发者ID:web3j,项目名称:web3j,代码行数:6,代码来源:EventEncoder.java

示例13: generateContractAddress

import org.web3j.utils.Numeric; //导入方法依赖的package包/类
public static String generateContractAddress(String address, BigInteger nonce) {
    byte[] result = generateContractAddress(Numeric.hexStringToByteArray(address), nonce);
    return Numeric.toHexString(result);
}
 
开发者ID:web3j,项目名称:web3j,代码行数:5,代码来源:ContractUtils.java

示例14: nameHash

import org.web3j.utils.Numeric; //导入方法依赖的package包/类
public static String nameHash(String ensName) {
    String normalisedEnsName = normalise(ensName);
    return Numeric.toHexString(nameHash(normalisedEnsName.split("\\.")));
}
 
开发者ID:web3j,项目名称:web3j,代码行数:5,代码来源:NameHash.java

示例15: sha3

import org.web3j.utils.Numeric; //导入方法依赖的package包/类
/**
 * Keccak-256 hash function.
 *
 * @param hexInput hex encoded input data with optional 0x prefix
 * @return hash value as hex encoded string
 */
public static String sha3(String hexInput) {
    byte[] bytes = Numeric.hexStringToByteArray(hexInput);
    byte[] result = sha3(bytes);
    return Numeric.toHexString(result);
}
 
开发者ID:web3j,项目名称:web3j,代码行数:12,代码来源:Hash.java


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