Java Code Examples for org.web3j.crypto.Sign#signedMessageToKey()

The following examples show how to use org.web3j.crypto.Sign#signedMessageToKey() . 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 File: DappBrowserFragment.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
public void testRecoverAddressFromSignature(String message, String sig)
{
    String prefix = PERSONAL_MESSAGE_PREFIX + message.length();
    byte[] msgHash = (prefix + message).getBytes();

    byte[] signatureBytes = Numeric.hexStringToByteArray(sig);
    Sign.SignatureData sd = sigFromByteArray(signatureBytes);
    String addressRecovered;

    try
    {
        BigInteger recoveredKey = Sign.signedMessageToKey(msgHash, sd);
        addressRecovered = "0x" + Keys.getAddress(recoveredKey);
        System.out.println("Recovered: " + addressRecovered);
    }
    catch (SignatureException e)
    {
        e.printStackTrace();
    }
}
 
Example 2
Source File: Transaction.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private TransactionOperation[] processTrade(TransactionInput f, String contractAddress)
{
	TransactionOperation[] o;
	try
	{
		Sign.SignatureData sig = decoder.getSignatureData(f);
		//ecrecover the recipient of the ether
		int[] ticketIndexArray = decoder.getIndices(f);
		String expiryStr = f.miscData.get(0);
		long expiry = Long.valueOf(expiryStr, 16);
		BigInteger priceWei = new BigInteger(value);
		contractAddress = to;
		o = generateERC875Op();
		TransactionOperation op = o[0];
		TransactionContract ct = op.contract;
		if (error.equals("0")) //don't bother checking signature unless the transaction succeeded
		{
			if (parser == null) parser = new ParseMagicLink(new CryptoFunctions(), EthereumNetworkRepository.extraChains()); //parser on demand
			byte[] tradeBytes = parser.getTradeBytes(ticketIndexArray, contractAddress, priceWei, expiry);
			//attempt ecrecover
			BigInteger key = Sign.signedMessageToKey(tradeBytes, sig);
			ct.setOtherParty("0x" + Keys.getAddress(key));
		}
		ct.address = contractAddress;
		ct.setIndicies(f.paramValues);
		ct.name = contractAddress;
	}
	catch (Exception e)
	{
		o = generateERC875Op();
		e.printStackTrace();
	}

	return o;
}
 
Example 3
Source File: CryptoFunctions.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
@Override
public BigInteger signedMessageToKey(byte[] data, byte[] signature) throws SignatureException
{
    Sign.SignatureData sigData = sigFromByteArray(signature);
    if (sigData == null) return BigInteger.ZERO;
    return Sign.signedMessageToKey(data, sigData);
}
 
Example 4
Source File: MarketQueueService.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private BigInteger ecRecoverPublicKey(Wallet wallet, String password, int chainId) throws Exception
{
    String testSigMsg = "obtain public key";
    byte[] testSigBytes = transactionRepository.getSignatureFast(wallet, password, testSigMsg.getBytes(), chainId).blockingGet();
    Sign.SignatureData testSig = sigFromByteArray(testSigBytes);
    BigInteger recoveredKey = Sign.signedMessageToKey(testSigMsg.getBytes(), testSig);
    String publicKeyString = Keys.getAddress(recoveredKey); //TODO: Remove - this is here for debug/testing

    return recoveredKey;
}
 
Example 5
Source File: UniversalLinkTest.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private String ecRecoverAddress(byte[] data, Sign.SignatureData signature) //get the hex string address from the sig and data
{
    String address = "";
    try
    {
        BigInteger recoveredKey = Sign.signedMessageToKey(data, signature); //get embedded address
        address = Keys.getAddress(recoveredKey);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return address;
}
 
Example 6
Source File: UniversalLinkTest.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private String ecRecoverAddress() throws Exception
{
    String testSigMsg = "obtain public key";
    Sign.SignatureData testSig = Sign.signMessage(testSigMsg.getBytes(), testKey);
    BigInteger recoveredKey = Sign.signedMessageToKey(testSigMsg.getBytes(), testSig);

    return Keys.getAddress(recoveredKey);
}
 
Example 7
Source File: UniversalLinkTypeTest.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private String ecRecoverAddress(byte[] data, Sign.SignatureData signature) //get the hex string address from the sig and data
{
    String address = "";
    try
    {
        BigInteger recoveredKey = Sign.signedMessageToKey(data, signature); //get embedded address
        address = Keys.getAddress(recoveredKey);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return address;
}
 
Example 8
Source File: UniversalLinkTypeTest.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private String ecRecoverAddress() throws Exception
{
    String testSigMsg = "obtain public key";
    Sign.SignatureData testSig = Sign.signMessage(testSigMsg.getBytes(), testKey);
    BigInteger recoveredKey = Sign.signedMessageToKey(testSigMsg.getBytes(), testSig);

    return Keys.getAddress(recoveredKey);
}
 
Example 9
Source File: QRSelectionTest.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private String ecRecoverAddress(byte[] data, Sign.SignatureData signature) //get the hex string address from the sig and data
{
    String address = "";
    try
    {
        BigInteger recoveredKey = Sign.signedMessageToKey(data, signature); //get embedded address
        address = Keys.getAddress(recoveredKey);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return address;
}
 
Example 10
Source File: CryptoFunctions.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
@Override
public BigInteger signedMessageToKey(byte[] data, byte[] signature) throws SignatureException
{
    Sign.SignatureData sigData = sigFromByteArray(signature);
    if (sigData == null) return BigInteger.ZERO;
    else return Sign.signedMessageToKey(data, sigData);
}
 
Example 11
Source File: CryptoFunctions.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
@Override
public BigInteger signedMessageToKey(byte[] data, byte[] signature) throws SignatureException
{
    Sign.SignatureData sigData = sigFromByteArray(signature);
    if (sigData == null) return BigInteger.ZERO;
    else return Sign.signedMessageToKey(data, sigData);
}
 
Example 12
Source File: PrivateTransactionDecoderTest.java    From web3j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDecodingSigned() throws Exception {
    final BigInteger nonce = BigInteger.ZERO;
    final BigInteger gasPrice = BigInteger.ONE;
    final BigInteger gasLimit = BigInteger.TEN;
    final String to = "0x0add5355";
    final RawPrivateTransaction rawTransaction =
            RawPrivateTransaction.createTransaction(
                    nonce,
                    gasPrice,
                    gasLimit,
                    to,
                    "",
                    MOCK_ENCLAVE_KEY,
                    MOCK_PRIVATE_FOR,
                    RESTRICTED);
    final String privateKey =
            "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63";
    final Credentials credentials = Credentials.create(privateKey);
    final byte[] encodedMessage =
            PrivateTransactionEncoder.signMessage(rawTransaction, credentials);
    final String hexMessage = Numeric.toHexString(encodedMessage);

    final RawPrivateTransaction result = PrivateTransactionDecoder.decode(hexMessage);
    assertNotNull(result);
    assertEquals(nonce, result.getNonce());
    assertEquals(gasPrice, result.getGasPrice());
    assertEquals(gasLimit, result.getGasLimit());
    assertEquals(to, result.getTo());
    assertEquals("", result.getData());
    assertEquals(MOCK_ENCLAVE_KEY, result.getPrivateFrom());
    assertEquals(MOCK_PRIVATE_FOR, result.getPrivateFor().get());
    assertEquals(RESTRICTED, result.getRestriction());
    assertTrue(result instanceof SignedRawPrivateTransaction);
    final SignedRawPrivateTransaction signedResult = (SignedRawPrivateTransaction) result;
    assertNotNull(signedResult.getSignatureData());
    Sign.SignatureData signatureData = signedResult.getSignatureData();
    final byte[] encodedTransaction = PrivateTransactionEncoder.encode(rawTransaction);
    final BigInteger key = Sign.signedMessageToKey(encodedTransaction, signatureData);
    assertEquals(key, credentials.getEcKeyPair().getPublicKey());
    assertEquals(credentials.getAddress(), signedResult.getFrom());
    signedResult.verify(credentials.getAddress());
    assertNull(signedResult.getChainId());
}
 
Example 13
Source File: PrivateTransactionDecoderTest.java    From web3j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDecodingSignedPrivacyGroup() throws Exception {
    final BigInteger nonce = BigInteger.ZERO;
    final BigInteger gasPrice = BigInteger.ONE;
    final BigInteger gasLimit = BigInteger.TEN;
    final String to = "0x0add5355";
    final RawPrivateTransaction rawTransaction =
            RawPrivateTransaction.createTransaction(
                    nonce,
                    gasPrice,
                    gasLimit,
                    to,
                    "",
                    MOCK_ENCLAVE_KEY,
                    MOCK_ENCLAVE_KEY,
                    RESTRICTED);
    final String privateKey =
            "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63";
    final Credentials credentials = Credentials.create(privateKey);
    final byte[] encodedMessage =
            PrivateTransactionEncoder.signMessage(rawTransaction, credentials);
    final String hexMessage = Numeric.toHexString(encodedMessage);

    final RawPrivateTransaction result = PrivateTransactionDecoder.decode(hexMessage);
    assertNotNull(result);
    assertEquals(nonce, result.getNonce());
    assertEquals(gasPrice, result.getGasPrice());
    assertEquals(gasLimit, result.getGasLimit());
    assertEquals(to, result.getTo());
    assertEquals("", result.getData());
    assertEquals(MOCK_ENCLAVE_KEY, result.getPrivateFrom());
    assertEquals(MOCK_ENCLAVE_KEY, result.getPrivacyGroupId().get());
    assertEquals(RESTRICTED, result.getRestriction());
    assertTrue(result instanceof SignedRawPrivateTransaction);
    final SignedRawPrivateTransaction signedResult = (SignedRawPrivateTransaction) result;
    assertNotNull(signedResult.getSignatureData());
    Sign.SignatureData signatureData = signedResult.getSignatureData();
    final byte[] encodedTransaction = PrivateTransactionEncoder.encode(rawTransaction);
    final BigInteger key = Sign.signedMessageToKey(encodedTransaction, signatureData);
    assertEquals(key, credentials.getEcKeyPair().getPublicKey());
    assertEquals(credentials.getAddress(), signedResult.getFrom());
    signedResult.verify(credentials.getAddress());
    assertNull(signedResult.getChainId());
}