Java Code Examples for org.web3j.utils.Numeric
The following examples show how to use
org.web3j.utils.Numeric.
These examples are extracted from open source projects.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: etherscan-explorer Author: bing-chou File: RequestTest.java License: GNU General Public License v3.0 | 6 votes |
@Test public void testEthSendTransaction() throws Exception { web3j.ethSendTransaction(new Transaction( "0xb60e8dd61c5d32be8058bb8eb970870f07233155", BigInteger.ONE, Numeric.toBigInt("0x9184e72a000"), Numeric.toBigInt("0x76c0"), "0xb60e8dd61c5d32be8058bb8eb970870f07233155", Numeric.toBigInt("0x9184e72a"), "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb" + "970870f072445675058bb8eb970870f072445675")).send(); //CHECKSTYLE:OFF verifyResult("{\"jsonrpc\":\"2.0\",\"method\":\"eth_sendTransaction\",\"params\":[{\"from\":\"0xb60e8dd61c5d32be8058bb8eb970870f07233155\",\"to\":\"0xb60e8dd61c5d32be8058bb8eb970870f07233155\",\"gas\":\"0x76c0\",\"gasPrice\":\"0x9184e72a000\",\"value\":\"0x9184e72a\",\"data\":\"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675\",\"nonce\":\"0x1\"}],\"id\":1}"); //CHECKSTYLE:ON }
Example #2
Source Project: web3j Author: web3j File: PrivateTransactionEncoderTest.java License: Apache License 2.0 | 6 votes |
@Test public void testSignLegacyTransaction() { final String expected = "0xf8d4808203e8832dc6c094627306090abab3a6e1400e9345bc60c78a8bef578080820fe8a0e0b547d71d7a23d52382288b3a2a5a1610e0b504c404cc5009d7ada97d9015b2a076e997a83856d876fa2397b74510890eea3b73ffeda33daa4188120dac42d62fa0035695b4cc4b0941e60551d7a19cf30603db5bfc23e5ac43a56f57f25f75486af842a0035695b4cc4b0941e60551d7a19cf30603db5bfc23e5ac43a56f57f25f75486aa02a8d9b56a0fe9cd94d60be4413bcb721d3a7be27ed8e28b3a6346df874ee141b8a72657374726963746564"; final RawPrivateTransaction privateTransactionCreation = new RawPrivateTransaction( BigInteger.ZERO, BigInteger.valueOf(1000), BigInteger.valueOf(3000000), "0x627306090abab3a6e1400e9345bc60c78a8bef57", "0x", MOCK_ENCLAVE_KEY, MOCK_PRIVATE_FOR, null, RESTRICTED); final long chainId = 2018; final String privateKey = "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"; final Credentials credentials = Credentials.create(privateKey); final String privateRawTransaction = Numeric.toHexString( PrivateTransactionEncoder.signMessage( privateTransactionCreation, chainId, credentials)); assertEquals(expected, privateRawTransaction); }
Example #3
Source Project: etherscan-explorer Author: bing-chou File: SignTransactionIT.java License: GNU General Public License v3.0 | 6 votes |
@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()); }
Example #4
Source Project: client-sdk-java Author: PlatONnetwork File: WasmFunctionEncoder.java License: Apache License 2.0 | 6 votes |
public static String encodeConstructor(String code, List<?> inputParameters) { List<Object> parameters = new ArrayList<>(); // parameters.add(DEPLOY_METHOD_NAME); parameters.add(fnvOne64Hash(DEPLOY_METHOD_NAME)); for (Object o : inputParameters) { if (!o.equals(Void.class)) { parameters.add(o); } } byte[] parameterData = RLPCodec.encode(parameters); byte[] codeBinary = Numeric.hexStringToByteArray(code); Object[] objs = new Object[] { codeBinary, parameterData }; byte[] data = RLPCodec.encode(objs); byte[] result = new byte[MAGIC_NUM.length + data.length]; System.arraycopy(MAGIC_NUM, 0, result, 0, MAGIC_NUM.length); System.arraycopy(data, 0, result, MAGIC_NUM.length, data.length); return Numeric.toHexStringNoPrefix(result); }
Example #5
Source Project: client-sdk-java Author: PlatONnetwork File: RequestTest.java License: Apache License 2.0 | 6 votes |
@Test public void testEthSendTransaction() throws Exception { web3j.platonSendTransaction(new Transaction( "0xb60e8dd61c5d32be8058bb8eb970870f07233155", BigInteger.ONE, Numeric.toBigInt("0x9184e72a000"), Numeric.toBigInt("0x76c0"), "0xb60e8dd61c5d32be8058bb8eb970870f07233155", Numeric.toBigInt("0x9184e72a"), "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb" + "970870f072445675058bb8eb970870f072445675")).send(); //CHECKSTYLE:OFF verifyResult("{\"jsonrpc\":\"2.0\",\"method\":\"platon_sendTransaction\",\"params\":[{\"from\":\"0xb60e8dd61c5d32be8058bb8eb970870f07233155\",\"to\":\"0xb60e8dd61c5d32be8058bb8eb970870f07233155\",\"gas\":\"0x76c0\",\"gasPrice\":\"0x9184e72a000\",\"value\":\"0x9184e72a\",\"data\":\"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675\",\"nonce\":\"0x1\"}],\"id\":1}"); //CHECKSTYLE:ON }
Example #6
Source Project: web3j Author: web3j File: Transaction.java License: Apache License 2.0 | 6 votes |
public Transaction( String from, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to, BigInteger value, String data, BigInteger gasPremium, BigInteger feeCap) { this.from = from; this.to = to; this.gas = gasLimit; this.gasPrice = gasPrice; this.value = value; if (data != null) { this.data = Numeric.prependHexPrefix(data); } this.nonce = nonce; this.gasPremium = gasPremium; this.feeCap = feeCap; }
Example #7
Source Project: etherscan-explorer Author: bing-chou File: CreateRawTransactionIT.java License: GNU General Public License v3.0 | 6 votes |
@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())); }
Example #8
Source Project: web3j Author: web3j File: RequestTest.java License: Apache License 2.0 | 5 votes |
@Test public void testEthGetFilterChanges() throws Exception { web3j.ethGetFilterChanges(Numeric.toBigInt("0x16")).send(); verifyResult( "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getFilterChanges\"," + "\"params\":[\"0x16\"],\"id\":1}"); }
Example #9
Source Project: web3j Author: web3j File: RequestTest.java License: Apache License 2.0 | 5 votes |
@Test public void testEthGetCode() throws Exception { web3j.ethGetCode( "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", DefaultBlockParameter.valueOf(Numeric.toBigInt("0x2"))) .send(); verifyResult( "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getCode\"," + "\"params\":[\"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b\",\"0x2\"],\"id\":1}"); }
Example #10
Source Project: web3j Author: web3j File: TypeEncoder.java License: Apache License 2.0 | 5 votes |
static String encodeBool(Bool value) { byte[] rawValue = new byte[MAX_BYTE_LENGTH]; if (value.getValue()) { rawValue[rawValue.length - 1] = 1; } return Numeric.toHexStringNoPrefix(rawValue); }
Example #11
Source Project: client-sdk-java Author: PlatONnetwork File: TransactionDecoderTest.java License: Apache License 2.0 | 5 votes |
@Test public void testDecodingSigned() throws Exception { BigInteger nonce = BigInteger.ZERO; BigInteger gasPrice = BigInteger.ONE; BigInteger gasLimit = BigInteger.TEN; String to = "0x0add5355"; BigInteger value = BigInteger.valueOf(Long.MAX_VALUE); RawTransaction rawTransaction = RawTransaction.createEtherTransaction( nonce, gasPrice, gasLimit, to, value); byte[] signedMessage = TransactionEncoder.signMessage( rawTransaction, SampleKeys.CREDENTIALS); String hexMessage = Numeric.toHexString(signedMessage); RawTransaction result = TransactionDecoder.decode(hexMessage); assertNotNull(result); assertEquals(nonce, result.getNonce()); assertEquals(gasPrice, result.getGasPrice()); assertEquals(gasLimit, result.getGasLimit()); assertEquals(to, result.getTo()); assertEquals(value, result.getValue()); assertEquals("", result.getData()); assertTrue(result instanceof SignedRawTransaction); SignedRawTransaction signedResult = (SignedRawTransaction) result; assertNotNull(signedResult.getSignatureData()); Sign.SignatureData signatureData = signedResult.getSignatureData(); byte[] encodedTransaction = TransactionEncoder.encode(rawTransaction); BigInteger key = Sign.signedMessageToKey(encodedTransaction, signatureData); assertEquals(key, SampleKeys.PUBLIC_KEY); assertEquals(SampleKeys.ADDRESS, signedResult.getFrom()); signedResult.verify(SampleKeys.ADDRESS); assertNull(signedResult.getChainId()); }
Example #12
Source Project: besu Author: hyperledger File: PrivacyRequestFactory.java License: Apache License 2.0 | 5 votes |
public String privxRemoveFromPrivacyGroup( final Base64String privacyGroupId, final PrivacyNode remover, final String toRemove) throws IOException, TransactionException { final BigInteger nonce = besuClient .privGetTransactionCount(remover.getAddress().toHexString(), privacyGroupId) .send() .getTransactionCount(); final Bytes payload = encodeRemoveFromGroupFunctionCall( Bytes.fromBase64String(remover.getEnclaveKey()), Bytes.fromBase64String(toRemove)); final RawPrivateTransaction privateTransaction = RawPrivateTransaction.createTransaction( nonce, BigInteger.valueOf(1000), BigInteger.valueOf(3000000), Address.ONCHAIN_PRIVACY_PROXY.toHexString(), payload.toHexString(), Base64String.wrap(remover.getEnclaveKey()), privacyGroupId, org.web3j.utils.Restriction.RESTRICTED); return besuClient .eeaSendRawTransaction( Numeric.toHexString( PrivateTransactionEncoder.signMessage( privateTransaction, Credentials.create(remover.getTransactionSigningKey())))) .send() .getTransactionHash(); }
Example #13
Source Project: web3j Author: web3j File: DefaultFunctionReturnDecoder.java License: Apache License 2.0 | 5 votes |
public List<Type> decodeFunctionResult( String rawInput, List<TypeReference<Type>> outputParameters) { String input = Numeric.cleanHexPrefix(rawInput); if (Strings.isEmpty(input)) { return Collections.emptyList(); } else { return build(input, outputParameters); } }
Example #14
Source Project: web3j Author: web3j File: JsonRpc2_0Web3j.java License: Apache License 2.0 | 5 votes |
@Override public Request<?, EthBlock> ethGetUncleByBlockNumberAndIndex( DefaultBlockParameter defaultBlockParameter, BigInteger uncleIndex) { return new Request<>( "eth_getUncleByBlockNumberAndIndex", Arrays.asList(defaultBlockParameter.getValue(), Numeric.encodeQuantity(uncleIndex)), web3jService, EthBlock.class); }
Example #15
Source Project: etherscan-explorer Author: bing-chou File: RequestTest.java License: GNU General Public License v3.0 | 5 votes |
@Test public void testEthGetCode() throws Exception { web3j.ethGetCode("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", DefaultBlockParameter.valueOf(Numeric.toBigInt("0x2"))).send(); verifyResult("{\"jsonrpc\":\"2.0\",\"method\":\"eth_getCode\"," + "\"params\":[\"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b\",\"0x2\"],\"id\":1}"); }
Example #16
Source Project: web3j Author: web3j File: KeysTest.java License: Apache License 2.0 | 5 votes |
@Test public void testGetAddressZeroPadded() { byte[] address = Keys.getAddress( Numeric.toBytesPadded(BigInteger.valueOf(0x1234), Keys.PUBLIC_KEY_SIZE)); String expected = Numeric.toHexStringNoPrefix(address); String value = "1234"; assertEquals( Keys.getAddress( "0x" + Strings.zeros(Keys.PUBLIC_KEY_LENGTH_IN_HEX - value.length()) + value), (expected)); }
Example #17
Source Project: client-sdk-java Author: PlatONnetwork File: JsonRpc2_0Web3j.java License: Apache License 2.0 | 5 votes |
@Override public Request<?, PlatonGetStorageAt> platonGetStorageAt( String address, BigInteger position, DefaultBlockParameter defaultBlockParameter) { return new Request<>( "platon_getStorageAt", Arrays.asList( address, Numeric.encodeQuantity(position), defaultBlockParameter.getValue()), web3jService, PlatonGetStorageAt.class); }
Example #18
Source Project: web3j Author: web3j File: JsonRpc2_0Web3j.java License: Apache License 2.0 | 5 votes |
@Override public Request<?, EthUninstallFilter> ethUninstallFilter(BigInteger filterId) { return new Request<>( "eth_uninstallFilter", Arrays.asList(Numeric.toHexStringWithPrefixSafe(filterId)), web3jService, EthUninstallFilter.class); }
Example #19
Source Project: client-sdk-java Author: PlatONnetwork File: RewardContract.java License: Apache License 2.0 | 5 votes |
/** * 查询当前账户地址所委托的节点的NodeID和质押Id * * @param address 查询的地址 * @param nodeList 节点id列表 * @return */ public RemoteCall<CallResponse<List<Reward>>> getDelegateReward(String address,List<String> nodeList) { List<NodeId> bytesTypeList = nodeList.stream().map(nodeId -> new NodeId(nodeId)).collect(Collectors.toList()); CustomStaticArray<NodeId> dynamicArray = new CustomStaticArray<>(bytesTypeList); Function function = new Function(FunctionType.GET_DELEGATE_REWARD_FUNC_TYPE, Arrays.asList(new BytesType(Numeric.hexStringToByteArray(address)), dynamicArray)); return executeRemoteCallListValueReturn(function, Reward.class); }
Example #20
Source Project: guarda-android-wallets Author: guardaco File: EthereumNetworkManager.java License: GNU General Public License v3.0 | 5 votes |
@Override protected RawTransactionResponse doInBackground(Void... params) { BigInteger nonce = getNonce(); RawTransaction rawTransaction; String blockNumber = getBlockNumber(); byte[] signedMessage; if (TextUtils.isEmpty(contractData)) { rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, toAddress, value); } else { rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, toAddress, new String(Hex.encode(contractData.getBytes()))); } if (walletManager.getCredentials() != null && rawTransaction != null) { signedMessage = TransactionEncoder.signMessage(rawTransaction, walletManager.getCredentials()); } else { return null; } String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction ethSendTransaction = null; try { ethSendTransaction = web3jConnection.ethSendRawTransaction(hexValue).send(); } catch (IOException e) { e.printStackTrace(); Log.d("psd", e.toString()); } if (ethSendTransaction != null && ethSendTransaction.getTransactionHash() != null) { return new RawTransactionResponse(ethSendTransaction.getTransactionHash(), hexValue, blockNumber); } else { return null; } }
Example #21
Source Project: etherscan-explorer Author: bing-chou File: ShhPost.java License: GNU General Public License v3.0 | 5 votes |
private String convert(BigInteger value) { if (value != null) { return Numeric.encodeQuantity(value); } else { return null; } }
Example #22
Source Project: eventeum Author: ConsenSys File: SupportedSolidityTypesIT.java License: Apache License 2.0 | 5 votes |
@Test public void testEventWithAdditionalTypes() throws Exception { final EventEmitter emitter = deployEventEmitterContract(); byte[] byteValue = randomBytesValue(1); final ContractEventFilter registeredFilter = registerDummyEventAdditionalTypesFilter(emitter.getContractAddress()); emitter.emitEventAdditionalTypes(BigInteger.ONE, BigInteger.TEN, byteValue).send(); waitForContractEventMessages(1); assertEquals(1, getBroadcastContractEvents().size()); final ContractEventDetails eventDetails = getBroadcastContractEvents().get(0); assertEquals(registeredFilter.getEventSpecification().getEventName(), eventDetails.getName()); assertEquals(ContractEventStatus.UNCONFIRMED, eventDetails.getStatus()); assertEquals(BigInteger.ONE, eventDetails.getIndexedParameters().get(0).getValue()); assertEquals(BigInteger.TEN, eventDetails.getIndexedParameters().get(1).getValue()); final ArrayList<StringParameter> addressArray = (ArrayList<StringParameter>) eventDetails.getNonIndexedParameters().get(0).getValue(); assertEquals(Keys.toChecksumAddress(CREDS.getAddress()), addressArray.get(0).getValue()); assertEquals(Keys.toChecksumAddress(emitter.getContractAddress()), addressArray.get(1).getValue()); assertEquals(Numeric.toHexString(byteValue, 0, 1, true), eventDetails.getNonIndexedParameters().get(1).getValueString()); assertEquals(BigInteger.ONE, eventDetails.getNonIndexedParameters().get(2).getValue()); assertEquals(Web3jUtil.getSignature(registeredFilter.getEventSpecification()), eventDetails.getEventSpecificationSignature()); }
Example #23
Source Project: etherscan-explorer Author: bing-chou File: JsonRpc2_0Web3j.java License: GNU General Public License v3.0 | 5 votes |
@Override public Request<?, EthGetStorageAt> ethGetStorageAt( String address, BigInteger position, DefaultBlockParameter defaultBlockParameter) { return new Request<>( "eth_getStorageAt", Arrays.asList( address, Numeric.encodeQuantity(position), defaultBlockParameter.getValue()), web3jService, EthGetStorageAt.class); }
Example #24
Source Project: client-sdk-java Author: PlatONnetwork File: RequestTest.java License: Apache License 2.0 | 5 votes |
@Test public void testEthGetTransactionByBlockNumberAndIndex() throws Exception { web3j.platonGetTransactionByBlockNumberAndIndex( DefaultBlockParameter.valueOf(Numeric.toBigInt("0x29c")), BigInteger.ZERO).send(); verifyResult("{\"jsonrpc\":\"2.0\",\"method\":\"platon_getTransactionByBlockNumberAndIndex\"," + "\"params\":[\"0x29c\",\"0x0\"],\"id\":1}"); }
Example #25
Source Project: web3j Author: web3j File: KeysTest.java License: Apache License 2.0 | 5 votes |
@Test public void testGetAddressSmallPublicKey() { byte[] address = Keys.getAddress( Numeric.toBytesPadded(BigInteger.valueOf(0x1234), Keys.PUBLIC_KEY_SIZE)); String expected = Numeric.toHexStringNoPrefix(address); assertEquals(Keys.getAddress("0x1234"), (expected)); }
Example #26
Source Project: etherscan-explorer Author: bing-chou File: Transfer.java License: GNU General Public License v3.0 | 5 votes |
private TransactionReceipt send( String toAddress, BigDecimal value, Convert.Unit unit, BigInteger gasPrice, BigInteger gasLimit) throws IOException, InterruptedException, TransactionException { BigDecimal weiValue = Convert.toWei(value, unit); if (!Numeric.isIntegerValue(weiValue)) { throw new UnsupportedOperationException( "Non decimal Wei value provided: " + value + " " + unit.toString() + " = " + weiValue + " Wei"); } String resolvedAddress = ensResolver.resolve(toAddress); return send(resolvedAddress, "", weiValue.toBigIntegerExact(), gasPrice, gasLimit); }
Example #27
Source Project: client-sdk-java Author: PlatONnetwork File: TypeDecoder.java License: Apache License 2.0 | 5 votes |
static DynamicBytes decodeDynamicBytes(String input, int offset) { int encodedLength = decodeUintAsInt(input, offset); int hexStringEncodedLength = encodedLength << 1; int valueOffset = offset + MAX_BYTE_LENGTH_FOR_HEX_STRING; String data = input.substring(valueOffset, valueOffset + hexStringEncodedLength); byte[] bytes = Numeric.hexStringToByteArray(data); return new DynamicBytes(bytes); }
Example #28
Source Project: web3j Author: web3j File: RequestTest.java License: Apache License 2.0 | 5 votes |
@Test public void testEthGetTransactionByBlockNumberAndIndex() throws Exception { web3j.ethGetTransactionByBlockNumberAndIndex( DefaultBlockParameter.valueOf(Numeric.toBigInt("0x29c")), BigInteger.ZERO) .send(); verifyResult( "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionByBlockNumberAndIndex\"," + "\"params\":[\"0x29c\",\"0x0\"],\"id\":1}"); }
Example #29
Source Project: client-sdk-java Author: PlatONnetwork File: JsonRpc2_0Web3j.java License: Apache License 2.0 | 5 votes |
@Override public Request<?, PlatonTransaction> platonGetTransactionByBlockNumberAndIndex( DefaultBlockParameter defaultBlockParameter, BigInteger transactionIndex) { return new Request<>( "platon_getTransactionByBlockNumberAndIndex", Arrays.asList( defaultBlockParameter.getValue(), Numeric.encodeQuantity(transactionIndex)), web3jService, PlatonTransaction.class); }
Example #30
Source Project: alpha-wallet-android Author: AlphaWallet File: SellDetailActivity.java License: MIT License | 5 votes |
private BigInteger getPriceInWei() { //now convert to microWei long microEth = (int)(sellPriceValue * 1000000.0); byte[] max = Numeric.hexStringToByteArray("FFFFFFFF"); BigInteger maxValue = new BigInteger(1, max); if (microEth > maxValue.longValue()) microEth = 0; //check on UI screen if amount is more than we can handle //now convert to Wei return Convert.toWei(Long.toString(microEth), Convert.Unit.SZABO).toBigInteger(); }