Skip to content

Commit

Permalink
add API to sign general message
Browse files Browse the repository at this point in the history
  • Loading branch information
boqiu committed Feb 12, 2020
1 parent 6d98016 commit db2da08
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/main/java/conflux/web3j/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,22 @@ public boolean lock(String address) {
* @exception IllegalArgumentException if account not found, or password not specified for locked account, or password expired for unlocked account.
*/
public String signTransaction(RawTransaction tx, String address, String... password) throws IOException, CipherException {
Credentials credentials;
Credentials credentials = this.getCredentials(address, password);

byte[] encodedTx = TransactionEncoder.encode(tx);
Sign.SignatureData signature = Sign.signMessage(encodedTx, credentials.getEcKeyPair());
// adjust V in signature
int headerByte = signature.getV()[0] - 27;
signature = new Sign.SignatureData((byte) headerByte, signature.getR(), signature.getS());
List<RlpType> fields = TransactionEncoder.asRlpValues(tx, signature);
// change the RLP encode of V in signature
fields.set(fields.size() - 3, RlpString.create(headerByte));
byte[] signedTx = RlpEncoder.encode(new RlpList(fields));

return Numeric.toHexString(signedTx);
}

private Credentials getCredentials(String address, String... password) throws IOException, CipherException {
if (password == null || password.length == 0) {
UnlockedItem item = this.unlocked.get(address);
if (item == null) {
Expand All @@ -269,7 +283,7 @@ public String signTransaction(RawTransaction tx, String address, String... passw
throw new IllegalArgumentException("password expired for unlocked account");
}

credentials = item.getCredentials();
return item.getCredentials();
} else {
List<Path> files = Files.list(Paths.get(this.dir))
.filter(path -> this.parseAddressFromFilename(path.getFileName().toString()).equalsIgnoreCase(address))
Expand All @@ -279,20 +293,21 @@ public String signTransaction(RawTransaction tx, String address, String... passw
throw new IllegalArgumentException("account not found");
}

credentials = WalletUtils.loadCredentials(password[0], files.get(0).toString());
return WalletUtils.loadCredentials(password[0], files.get(0).toString());
}
}

public String signMessage(byte[] message, boolean needToHash, String address, String... password) throws IOException, CipherException {
Credentials credentials = this.getCredentials(address, password);

byte[] encodedTx = TransactionEncoder.encode(tx);
Sign.SignatureData signature = Sign.signMessage(encodedTx, credentials.getEcKeyPair());
// adjust V in signature
int headerByte = signature.getV()[0] - 27;
signature = new Sign.SignatureData((byte) headerByte, signature.getR(), signature.getS());
List<RlpType> fields = TransactionEncoder.asRlpValues(tx, signature);
// change the RLP encode of V in signature
fields.set(fields.size() - 3, RlpString.create(headerByte));
byte[] signedTx = RlpEncoder.encode(new RlpList(fields));
Sign.SignatureData data = Sign.signMessage(message, credentials.getEcKeyPair(), needToHash);

return Numeric.toHexString(signedTx);
byte[] rsv = new byte[data.getR().length + data.getS().length + data.getV().length];
System.arraycopy(data.getR(), 0, rsv, 0, data.getR().length);
System.arraycopy(data.getS(), 0, rsv, data.getR().length, data.getS().length);
System.arraycopy(data.getV(), 0, rsv, data.getR().length + data.getS().length, data.getV().length);

return Numeric.toHexString(rsv);
}
}

Expand Down

0 comments on commit db2da08

Please sign in to comment.