我使用Trust SDK签名邮件和交易。
我这样恢复交易(Trust.signTransaction()
):
val tx = Geth.newTransactionFromRLP(Numeric.hexStringToByteArray(response.result))
Geth
-框架(org.ethereum.geth),版本1.8.11。
如何从已签名的消息(Trust.signMessage()
)中恢复数据(以及用户钱包的地址)?
Trust SDK的开发人员说要研究“恢复”。那是什么框架?
非常感谢您。
答案 0 :(得分:0)
我已经解决了问题。
1)需要使用这种方法:
signPersonalMessageCall = Trust.signPersonalMessage()
.message("my message to be signed")
.call(this)
2)要获取用户的地址,需要使用:
const val PERSONAL_MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\n"
//...
fun recoverAddressFromSignature(message: String, signature: String): String? {
val prefix = PERSONAL_MESSAGE_PREFIX + message.length
val msgHash = Hash.sha3((prefix + message).toByteArray())
val signatureBytes = Numeric.hexStringToByteArray(signature)
var v = signatureBytes[64]
if (v < 27) {
v = (v + 27).toByte()
}
val sd = SignatureData(
v,
Arrays.copyOfRange(signatureBytes, 0, 32) as ByteArray,
Arrays.copyOfRange(signatureBytes, 32, 64) as ByteArray)
var addressRecovered: String? = null
// Iterate for each possible key to recover
for (i in 0..3) {
val publicKey = Sign.recoverFromSignature(
i,
ECDSASignature(BigInteger(1, sd.r), BigInteger(1, sd.s)),
msgHash)
if (publicKey != null) {
addressRecovered = "0x" + Keys.getAddress(publicKey)
Log.i(Core.APP_TAG, "*-*-* addressRecovered = $addressRecovered")
}
}
return addressRecovered
}
从here获取了用于恢复的方法(在Java上)。