You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I want to verify the signature wit c++ for server. But I am confuse by the param type.
How can I convert from std::string to secp256k1_ecdsa_recoverable_signature?
How can I convert the private key(18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725) to unsigned char privkey[32]?
How can I convert public key to address?
Thanks in advance for all.
here is my code:
int main()
{
std::string strMessage = "login.xyz wants you to sign in with your Ethereum account:\n0xfB6CD42a8Ec306A62b014Fe299d5854389379296\n\n\n\nURI: https://login.xyz/demo#login\nVersion: 1\nChain ID: 1\nNonce: ae058a6b-784d-4de3-90e3-850644d1b35d";
std::string strSignature = "0x6a0107a2a409270d58a9bed473800b7443090e5004e3a50f5249f5798678e4a36b13d19ac399a96345b83cfa45be8b09e6b30b2f11109904ab2f33594736f2921b";
std::string strAddress = "0xfB6CD42a8Ec306A62b014Fe299d5854389379296";
if (strSignature.length() != 132)
{
return -1;
}
// how to convert std::string to secp256k1_ecdsa_recoverable_signature?
secp256k1_ecdsa_recoverable_signature sigRecover;
for (int i = 0; i < strSignature.length(); i += 2) {
std::string byteString = strSignature.substr(i, 2);
if ((i - 2) >= 0)
{
sigRecover.data[(i - 2) / 2] = std::strtol(byteString.c_str(), nullptr, 16);
}
}
std::vector<unsigned char> vecHash(picosha2::k_digest_size);
picosha2::hash256(strMessage.begin(), strMessage.end(), vecHash.begin(), vecHash.end());
unsigned char msgHash[32] = "";
for (size_t i = 0; i < 32; i++)
{
msgHash[i] = vecHash[i];
}
secp256k1_pubkey publicKey;
auto ec = secp256k1_ecdsa_recover(secp256k1_context_static, &publicKey, &sigRecover, msgHash);
if (ec != 1)
{
return ec;
}
secp256k1_ecdsa_signature sig;
ec = secp256k1_ecdsa_recoverable_signature_convert(secp256k1_context_static, &sig, &sigRecover);
if (ec != 1)
{
return ec;
}
ec = secp256k1_ecdsa_verify(secp256k1_context_static, &sig, msgHash, &publicKey);
if (ec != 1)
{
return ec;
}
// how to convert public key to address?
std::string addressConvert;
if (addressConvert != strAddress)
{
return -1;
}
return 0;
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello, I want to verify the signature wit c++ for server. But I am confuse by the param type.
How can I convert from std::string to secp256k1_ecdsa_recoverable_signature?
How can I convert the private key(18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725) to unsigned char privkey[32]?
How can I convert public key to address?
Thanks in advance for all.
here is my code:
Beta Was this translation helpful? Give feedback.
All reactions