{% hint style="warning" %} Make sure you initialize Blocto SDK first {% endhint %}
Once your app is connected to Blocto wallet, it can sign message on behalf of the user, with the user's permission.
To sign a message, the app must specify the sign type. All available types are listed below.
Sign Type | Type | Message |
---|---|---|
ETH Sign | EvmSignType.ETH_SIGN |
hex string (0x prefix) |
Personal Sign | EvmSignType.PERSONAL_SIGN |
plain string |
Sign Typed Data v3 | EvmSignType.TYPED_DATA_V3 |
json string |
Sign Typed Data v4 | EvmSignType.TYPED_DATA_V4 |
json string |
Sign Typed Data (currently identical to Sign Typed Data v4) |
EvmSignType.TYPED_DATA |
json string |
// Sign message based on specific chain
BloctoSDK.evm.signMessage(
context = context,
blockchain = Blockchain.ETHEREUM,
fromAddress = address,
signType = [EvmSignType.ETH_SIGN/EvmSignType.PERSONAL_SIGN/...],
message = message,
onSuccess = { signature ->
// get signature
},
onError = { error ->
// handle error
}
)
For dApps relying on signMessage
for off-chain authentication, Blocto follows EIP-1654. To verify the signature, you need to call a method on the wallet contract to check if the signature came from a rightful owner of the wallet contract.
// Example code by using web3j
// hash: hash of data (message) to be signed
// signature: signature byte array associated with hash
fun verifySignature(hash: ByteArray, signature: ByteArray): Boolean {
val web3j = Web3j.build(HttpService(rpcUrl))
val function = Function(
"isValidSignature",
listOf(Bytes32(hash), DynamicBytes(signature)),
listOf<TypeReference<Bytes4>>()
)
val encodedFunction = FunctionEncoder.encode(function)
return web3j.ethCall(
Transaction.createEthCallTransaction(
address,
address,
encodedFunction
),
DefaultBlockParameterName.LATEST
).send().value.orEmpty() == ERC1271_MAGIC_VALUE
}