mirror of
https://github.com/eosswedenorg/libantelope
synced 2026-06-27 20:53:39 +02:00
src/WIF.cpp: use wif/codec.hpp
This commit is contained in:
parent
053f91c74b
commit
ea411793a2
2 changed files with 11 additions and 22 deletions
|
|
@ -53,6 +53,8 @@ add_library( ${LIB_NAME} STATIC
|
||||||
src/base58.cpp
|
src/base58.cpp
|
||||||
src/ec.cpp
|
src/ec.cpp
|
||||||
src/WIF.cpp
|
src/WIF.cpp
|
||||||
|
src/wif/k1.cpp
|
||||||
|
src/wif/legacy.cpp
|
||||||
|
|
||||||
src/openssl/hash.cpp
|
src/openssl/hash.cpp
|
||||||
)
|
)
|
||||||
|
|
|
||||||
31
src/WIF.cpp
31
src/WIF.cpp
|
|
@ -26,6 +26,7 @@
|
||||||
#include <libeosio/base58.hpp>
|
#include <libeosio/base58.hpp>
|
||||||
#include <libeosio/checksum.hpp>
|
#include <libeosio/checksum.hpp>
|
||||||
#include <libeosio/WIF.hpp>
|
#include <libeosio/WIF.hpp>
|
||||||
|
#include "wif/codec.hpp"
|
||||||
|
|
||||||
namespace libeosio {
|
namespace libeosio {
|
||||||
|
|
||||||
|
|
@ -91,36 +92,33 @@ std::string wif_pub_encode(const ec_pubkey_t& pub, const std::string& prefix) {
|
||||||
|
|
||||||
checksum_t check;
|
checksum_t check;
|
||||||
unsigned char buf[EC_PUBKEY_SIZE + CHECKSUM_SIZE];
|
unsigned char buf[EC_PUBKEY_SIZE + CHECKSUM_SIZE];
|
||||||
|
internal::pub_encoder_t encoder;
|
||||||
memcpy(buf, pub.data(), pub.size());
|
|
||||||
|
|
||||||
|
|
||||||
if (prefix == WIF_PUB_K1) {
|
if (prefix == WIF_PUB_K1) {
|
||||||
check = _checksum_suffix(buf, EC_PUBKEY_SIZE, "K1");
|
encoder = internal::pub_encoder_k1;
|
||||||
}
|
}
|
||||||
// Legacy
|
// Legacy
|
||||||
else {
|
else {
|
||||||
check = checksum_ripemd160(pub.data(), pub.size());
|
encoder = internal::pub_encoder_legacy;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(buf + EC_PUBKEY_SIZE, check.data(), check.size());
|
encoder(pub, buf);
|
||||||
|
|
||||||
return prefix + base58_encode(buf, buf + sizeof(buf));
|
return prefix + base58_encode(buf, buf + sizeof(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data) {
|
bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data) {
|
||||||
|
|
||||||
const char *suffix;
|
internal::pub_decoder_t decoder = internal::pub_decoder_legacy;
|
||||||
int offset;
|
int offset;
|
||||||
std::vector<unsigned char> buf;
|
std::vector<unsigned char> buf;
|
||||||
|
|
||||||
// Check prefix
|
// Check prefix
|
||||||
if (data.substr(0, WIF_PUB_K1.size()) == WIF_PUB_K1) {
|
if (data.substr(0, WIF_PUB_K1.size()) == WIF_PUB_K1) {
|
||||||
suffix = "K1";
|
decoder = internal::pub_decoder_k1;
|
||||||
offset = WIF_PUB_K1.size();
|
offset = WIF_PUB_K1.size();
|
||||||
} else {
|
} else {
|
||||||
// Legacy
|
// Legacy
|
||||||
suffix = "";
|
|
||||||
offset = 3;
|
offset = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -132,18 +130,7 @@ bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (suffix[0] != '\0') {
|
return decoder(buf, pub);
|
||||||
checksum_t check = _checksum_suffix(buf.data(), EC_PUBKEY_SIZE, suffix);
|
|
||||||
if (memcmp(buf.data() + EC_PUBKEY_SIZE, check.data(), CHECKSUM_SIZE)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (!checksum_validate<checksum_ripemd160>(buf.data(), buf.size())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy data to output
|
|
||||||
memcpy(pub.data(), buf.data(), pub.size());
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wif_print_key(const struct ec_keypair *key, const std::string& prefix) {
|
void wif_print_key(const struct ec_keypair *key, const std::string& prefix) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue