Java Code Examples for org.web3j.crypto.Credentials

The following examples show how to use org.web3j.crypto.Credentials. 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
private String createContract(
        Credentials credentials, BigInteger initialSupply) throws Exception {
    String createTransactionHash = sendCreateContractTransaction(credentials, initialSupply);
    assertFalse(createTransactionHash.isEmpty());

    TransactionReceipt createTransactionReceipt =
            waitForTransactionReceipt(createTransactionHash);

    assertThat(createTransactionReceipt.getTransactionHash(), is(createTransactionHash));

    assertFalse("Contract execution ran out of gas",
            createTransactionReceipt.getGasUsed().equals(GAS_LIMIT));

    String contractAddress = createTransactionReceipt.getContractAddress();

    assertNotNull(contractAddress);
    return contractAddress;
}
 
Example 2
Source Project: web3j   Source File: JsonRpc2_0Besu.java    License: Apache License 2.0 6 votes vote down vote up
public Request<?, EthSendTransaction> privOnChainSetGroupLockState(
        final Base64String privacyGroupId,
        final Credentials credentials,
        final Base64String enclaveKey,
        final Boolean lock)
        throws IOException {
    BigInteger transactionCount =
            privGetTransactionCount(credentials.getAddress(), privacyGroupId)
                    .send()
                    .getTransactionCount();
    String lockContractCall =
            OnChainPrivacyTransactionBuilder.getEncodedSingleParamFunction(
                    lock ? "lock" : "unlock");

    String lockPrivacyGroupTransactionPayload =
            OnChainPrivacyTransactionBuilder.buildOnChainPrivateTransaction(
                    privacyGroupId,
                    credentials,
                    enclaveKey,
                    transactionCount,
                    lockContractCall);

    return eeaSendRawTransaction(lockPrivacyGroupTransactionPayload);
}
 
Example 3
Source Project: web3j   Source File: Contract.java    License: Apache License 2.0 6 votes vote down vote up
public static <T extends Contract> RemoteCall<T> deployRemoteCall(
        Class<T> type,
        Web3j web3j,
        Credentials credentials,
        BigInteger gasPrice,
        BigInteger gasLimit,
        String binary,
        String encodedConstructor) {
    return deployRemoteCall(
            type,
            web3j,
            credentials,
            gasPrice,
            gasLimit,
            binary,
            encodedConstructor,
            BigInteger.ZERO);
}
 
Example 4
Source Project: web3j   Source File: PrivateTransactionEncoderTest.java    License: Apache License 2.0 6 votes vote down vote up
@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 5
Source Project: besu   Source File: OnChainPrivacyGroupManagementProxy.java    License: Apache License 2.0 6 votes vote down vote up
public static RemoteCall<OnChainPrivacyGroupManagementProxy> deploy(
    Web3j web3j,
    Credentials credentials,
    ContractGasProvider contractGasProvider,
    String _implementation) {
  String encodedConstructor =
      FunctionEncoder.encodeConstructor(
          Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, _implementation)));
  return deployRemoteCall(
      OnChainPrivacyGroupManagementProxy.class,
      web3j,
      credentials,
      contractGasProvider,
      BINARY,
      encodedConstructor);
}
 
Example 6
Source Project: web3j   Source File: Contract.java    License: Apache License 2.0 6 votes vote down vote up
public static <T extends Contract> RemoteCall<T> deployRemoteCall(
        Class<T> type,
        Web3j web3j,
        Credentials credentials,
        ContractGasProvider contractGasProvider,
        String binary,
        String encodedConstructor) {
    return new RemoteCall<>(
            () ->
                    deploy(
                            type,
                            web3j,
                            credentials,
                            contractGasProvider,
                            binary,
                            encodedConstructor,
                            BigInteger.ZERO));
}
 
Example 7
Source Project: web3j   Source File: Contract.java    License: Apache License 2.0 6 votes vote down vote up
public static <T extends Contract> RemoteCall<T> deployRemoteCall(
        Class<T> type,
        Web3j web3j,
        Credentials credentials,
        ContractGasProvider contractGasProvider,
        String binary,
        String encodedConstructor,
        BigInteger value) {
    return new RemoteCall<>(
            () ->
                    deploy(
                            type,
                            web3j,
                            credentials,
                            contractGasProvider,
                            binary,
                            encodedConstructor,
                            value));
}
 
Example 8
private void sendApproveTransaction(
        Credentials credentials, String spender, BigInteger value,
        String contractAddress) throws Exception {
    Function function = approve(spender, value);
    String functionHash = execute(credentials, function, contractAddress);

    TransactionReceipt transferTransactionReceipt =
            waitForTransactionReceipt(functionHash);
    assertThat(transferTransactionReceipt.getTransactionHash(), is(functionHash));

    List<Log> logs = transferTransactionReceipt.getLogs();
    assertFalse(logs.isEmpty());
    Log log = logs.get(0);

    // verify the event was called with the function parameters
    List<String> topics = log.getTopics();
    assertThat(topics.size(), is(3));

    // event Transfer(address indexed _from, address indexed _to, uint256 _value);
    Event event = approvalEvent();

    // check function signature - we only have a single topic our event signature,
    // there are no indexed parameters in this example
    String encodedEventSignature = EventEncoder.encode(event);
    assertThat(topics.get(0), is(encodedEventSignature));
    assertThat(new Address(topics.get(1)), is(new Address(credentials.getAddress())));
    assertThat(new Address(topics.get(2)), is(new Address(spender)));

    // verify our two event parameters
    List<Type> results = FunctionReturnDecoder.decode(
            log.getData(), event.getNonIndexedParameters());
    assertThat(results, equalTo(Collections.singletonList(new Uint256(value))));
}
 
Example 9
Source Project: ethsigner   Source File: SimpleStorage.java    License: Apache License 2.0 5 votes vote down vote up
protected SimpleStorage(
    String contractAddress,
    Web3j web3j,
    Credentials credentials,
    ContractGasProvider contractGasProvider) {
  super(BINARY, contractAddress, web3j, credentials, contractGasProvider);
}
 
Example 10
Source Project: ethsigner   Source File: SimpleStorage.java    License: Apache License 2.0 5 votes vote down vote up
public static SimpleStorage load(
    String contractAddress,
    Web3j web3j,
    Credentials credentials,
    ContractGasProvider contractGasProvider) {
  return new SimpleStorage(contractAddress, web3j, credentials, contractGasProvider);
}
 
Example 11
protected DefaultOnChainPrivacyGroupManagementContract(
    String contractAddress,
    Web3j web3j,
    Credentials credentials,
    ContractGasProvider contractGasProvider) {
  super(BINARY, contractAddress, web3j, credentials, contractGasProvider);
}
 
Example 12
@Deprecated
protected OnChainPrivacyGroupManagementInterface(
    String contractAddress,
    Web3j web3j,
    Credentials credentials,
    BigInteger gasPrice,
    BigInteger gasLimit) {
  super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}
 
Example 13
Source Project: teku   Source File: RegisterParamsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void eth1PrivateKeyReturnsCredential() {
  final Eth1PrivateKeyOptions eth1PrivateKeyOptions = new Eth1PrivateKeyOptions();
  eth1PrivateKeyOptions.eth1PrivateKey = ETH1_PRIVATE_KEY;
  final RegisterParams registerParams =
      new RegisterParams(commandSpec, eth1PrivateKeyOptions, SHUTDOWN_FUNCTION, consoleAdapter);
  final Credentials eth1Credentials = registerParams.getEth1Credentials();
  assertThat(eth1Credentials.getEcKeyPair()).isEqualTo(EXPECTED_EC_KEYPAIR);
}
 
Example 14
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();
}
 
Example 15
Source Project: client-sdk-java   Source File: RawTransactionManager.java    License: Apache License 2.0 5 votes vote down vote up
public RawTransactionManager(
        Web3j web3j, Credentials credentials, long chainId, int attempts, long sleepDuration) {
    super(web3j, attempts, sleepDuration, credentials.getAddress());

    this.web3j = web3j;
    this.credentials = credentials;

    this.chainId = chainId;
}
 
Example 16
public void restoreFromBlock2(String privateKey, final Runnable callback) {
    credentials = Credentials.create(privateKey);
    WalletCreationTask2 task = new WalletCreationTask2(new WalletCreationCallback() {
        @Override
        public void onWalletCreated(WalletFile walletFile) {
            callback.run();
        }
    }, credentials.getEcKeyPair());
    task.execute(BLOCK);
}
 
Example 17
public void restoreFromBlock2(String privateKey, final Runnable callback){
    credentials = Credentials.create(privateKey);
    WalletCreationTask2 task = new WalletCreationTask2(new WalletCreationCallback() {
        @Override
        public void onWalletCreated(WalletFile walletFile) {
            callback.run();
        }
    }, credentials.getEcKeyPair());
    task.execute(BLOCK);
}
 
Example 18
Source Project: web3j   Source File: HumanStandardToken.java    License: Apache License 2.0 5 votes vote down vote up
public static RemoteCall<HumanStandardToken> deploy(Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider, BigInteger _initialAmount, String _tokenName, BigInteger _decimalUnits, String _tokenSymbol) {
    String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(_initialAmount),
            new org.web3j.abi.datatypes.Utf8String(_tokenName),
            new org.web3j.abi.datatypes.generated.Uint8(_decimalUnits),
            new org.web3j.abi.datatypes.Utf8String(_tokenSymbol)));
    return deployRemoteCall(HumanStandardToken.class, web3j, credentials, contractGasProvider, BINARY, encodedConstructor);
}
 
Example 19
Source Project: Android-Wallet-Token-ERC20   Source File: SendingToken.java    License: Apache License 2.0 5 votes vote down vote up
public SendingToken(Web3j web3j, Credentials credentials, String valueGasPrice, String valueGasLimit){
    mWeb3j = web3j;
    mCredentials = credentials;
    fromAddress = credentials.getAddress();
    mValueGasPrice = valueGasPrice;
    mValueGasLimit = valueGasLimit;
}
 
Example 20
Source Project: besu   Source File: EventEmitter.java    License: Apache License 2.0 5 votes vote down vote up
protected EventEmitter(
    final String contractAddress,
    final Web3j web3j,
    final Credentials credentials,
    final ContractGasProvider contractGasProvider) {
  super(BINARY, contractAddress, web3j, credentials, contractGasProvider);
}
 
Example 21
Source Project: besu   Source File: EventEmitter.java    License: Apache License 2.0 5 votes vote down vote up
@Deprecated
public static EventEmitter load(
    final String contractAddress,
    final Web3j web3j,
    final Credentials credentials,
    final BigInteger gasPrice,
    final BigInteger gasLimit) {
  return new EventEmitter(contractAddress, web3j, credentials, gasPrice, gasLimit);
}
 
Example 22
Source Project: besu   Source File: EventEmitter.java    License: Apache License 2.0 5 votes vote down vote up
@Deprecated
public static RemoteCall<EventEmitter> deploy(
    final Web3j web3j,
    final Credentials credentials,
    final BigInteger gasPrice,
    final BigInteger gasLimit) {
  return deployRemoteCall(EventEmitter.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
}
 
Example 23
Source Project: jbpm-work-items   Source File: EthereumUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static String deployContract(Credentials credentials,
                                    Web3j web3j,
                                    String contractBinary,
                                    int toSendEther,
                                    boolean waitForReceipt,
                                    int sleepDuration,
                                    int attempts) throws Exception {

    BigInteger depositEtherAmountToSend = BigInteger.valueOf(toSendEther);

    RawTransaction rawTransaction = RawTransaction.createContractTransaction(
            getNextNonce(credentials.getAddress(),
                         web3j),
            DEFAULT_GAS_PRICE,
            DEFAULT_GAS_LIMIT,
            depositEtherAmountToSend,
            contractBinary);

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

    String hexValue = Numeric.toHexString(signedMessage);

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

    if (waitForReceipt) {
        TransactionReceipt transReceipt = waitForTransactionReceipt(
                ethSendTransaction.getTransactionHash(),
                sleepDuration,
                attempts,
                web3j
        );
        if (transReceipt != null) {
            return transReceipt.getContractAddress();
        }
    }
    // we dont have a contract address
    logger.warn("Unable to retrieve contract address.");
    return null;
}
 
Example 24
Source Project: besu   Source File: RevertReason.java    License: Apache License 2.0 5 votes vote down vote up
public static RevertReason load(
    final String contractAddress,
    final Web3j web3j,
    final Credentials credentials,
    final ContractGasProvider contractGasProvider) {
  return new RevertReason(contractAddress, web3j, credentials, contractGasProvider);
}
 
Example 25
Source Project: besu   Source File: RevertReason.java    License: Apache License 2.0 5 votes vote down vote up
public static RemoteCall<RevertReason> deploy(
    final Web3j web3j,
    final Credentials credentials,
    final ContractGasProvider contractGasProvider) {
  return deployRemoteCall(
      RevertReason.class, web3j, credentials, contractGasProvider, BINARY, "");
}
 
Example 26
Source Project: besu   Source File: RevertReason.java    License: Apache License 2.0 5 votes vote down vote up
@Deprecated
public static RemoteCall<RevertReason> deploy(
    final Web3j web3j,
    final Credentials credentials,
    final BigInteger gasPrice,
    final BigInteger gasLimit) {
  return deployRemoteCall(RevertReason.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
}
 
Example 27
Source Project: web3j   Source File: HumanStandardTokenIT.java    License: Apache License 2.0 5 votes vote down vote up
private void sendTransferTokensTransaction(
        Credentials credentials, String to, String contractAddress, BigInteger qty)
        throws Exception {

    Function function = transfer(to, qty);
    String functionHash = execute(credentials, function, contractAddress);

    TransactionReceipt transferTransactionReceipt = waitForTransactionReceipt(functionHash);
    assertEquals(transferTransactionReceipt.getTransactionHash(), (functionHash));

    List<Log> logs = transferTransactionReceipt.getLogs();
    assertFalse(logs.isEmpty());
    Log log = logs.get(0);

    // verify the event was called with the function parameters
    List<String> topics = log.getTopics();
    assertEquals(topics.size(), (3));

    Event transferEvent = transferEvent();

    // check function signature - we only have a single topic our event signature,
    // there are no indexed parameters in this example
    String encodedEventSignature = EventEncoder.encode(transferEvent);
    assertEquals(topics.get(0), (encodedEventSignature));
    assertEquals(new Address(topics.get(1)), (new Address(credentials.getAddress())));
    assertEquals(new Address(topics.get(2)), (new Address(to)));

    // verify qty transferred
    List<Type> results =
            FunctionReturnDecoder.decode(
                    log.getData(), transferEvent.getNonIndexedParameters());
    assertEquals(results, (Collections.singletonList(new Uint256(qty))));
}
 
Example 28
Source Project: client-sdk-java   Source File: HumanStandardToken.java    License: Apache License 2.0 5 votes vote down vote up
public static RemoteCall<HumanStandardToken> deploy(Web3j web3j, Credentials credentials, GasProvider contractGasProvider,
		BigInteger _initialAmount, String _tokenName, BigInteger _decimalUnits, String _tokenSymbol) {
	String encodedConstructor = FunctionEncoder.encodeConstructor(
			Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(_initialAmount), new org.web3j.abi.datatypes.Utf8String(_tokenName),
					new org.web3j.abi.datatypes.generated.Uint8(_decimalUnits), new org.web3j.abi.datatypes.Utf8String(_tokenSymbol)));
	return deployRemoteCall(HumanStandardToken.class, web3j, credentials, contractGasProvider, BINARY, encodedConstructor);
}
 
Example 29
Source Project: besu   Source File: CrossContractReader.java    License: Apache License 2.0 5 votes vote down vote up
public static RemoteCall<CrossContractReader> deploy(
    final Web3j web3j,
    final Credentials credentials,
    final ContractGasProvider contractGasProvider) {
  return deployRemoteCall(
      CrossContractReader.class, web3j, credentials, contractGasProvider, BINARY, "");
}
 
Example 30
Source Project: etherscan-explorer   Source File: Contract.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static <T extends Contract> RemoteCall<T> deployRemoteCall(
        Class<T> type,
        Web3j web3j, Credentials credentials,
        BigInteger gasPrice, BigInteger gasLimit,
        String binary, String encodedConstructor, BigInteger value) {
    return new RemoteCall<>(() -> deploy(
            type, web3j, credentials, gasPrice, gasLimit, binary,
            encodedConstructor, value));
}