Bitcoin Forum
May 13, 2024, 09:17:28 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: CPubKey from pubkey string  (Read 629 times)
zono (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 1


View Profile
June 16, 2017, 01:45:29 PM
Merited by ABCbits (1)
 #1

I'm reading bitcoin core source code. Is it possible to instantiate a CPubKey object with pubkey? I want to get bitcoin address from the pubkey by using bitcoin core's source. I can't find a good way.

I tried it like following but "invalid" is showed.

Code:
const char *cstr = "0396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767";
std::vector<unsigned char> vec(cstr, cstr + strlen(cstr));

CPubKey pubkey(vec);
if (pubkey.IsValid()) {
    cout << "valid" << endl;   
} else {
    cout << "invalid" << endl;
}
1715635048
Hero Member
*
Offline Offline

Posts: 1715635048

View Profile Personal Message (Offline)

Ignore
1715635048
Reply with quote  #2

1715635048
Report to moderator
1715635048
Hero Member
*
Offline Offline

Posts: 1715635048

View Profile Personal Message (Offline)

Ignore
1715635048
Reply with quote  #2

1715635048
Report to moderator
BitcoinCleanup.com: Learn why Bitcoin isn't bad for the environment
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715635048
Hero Member
*
Offline Offline

Posts: 1715635048

View Profile Personal Message (Offline)

Ignore
1715635048
Reply with quote  #2

1715635048
Report to moderator
1715635048
Hero Member
*
Offline Offline

Posts: 1715635048

View Profile Personal Message (Offline)

Ignore
1715635048
Reply with quote  #2

1715635048
Report to moderator
1715635048
Hero Member
*
Offline Offline

Posts: 1715635048

View Profile Personal Message (Offline)

Ignore
1715635048
Reply with quote  #2

1715635048
Report to moderator
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6635


Just writing some code


View Profile WWW
June 16, 2017, 04:32:20 PM
Last edit: June 16, 2017, 04:54:23 PM by achow101
 #2

What you are doing is interpreting the pubkey as a string. However a pubkey is not a string but rather a blob of binary data that is 33 bytes long. What you want to do is to convert your hex string into a vector of unsigned char first and then pass that to the CPubKey constructor.

It would be better for you to just initialize an array with your pubkey instead of performing a string conversion.

Edit:
Your code should look something like this:
Code:
unsigned char pk[] = {0x03, 0x96, 0xf8, 0x78, 0x1a, 0x49, 0x00, 0x37, 0x2a, 0x5d, 0x72, 0xd8, 0x47, 0x18, 0xd1, 0x46, 0x17, 0x0d, 0x59, 0x83, 0xe6, 0x7d, 0xff, 0x8b, 0x4a, 0x28, 0xfe, 0xf8, 0x06, 0x90, 0xc0, 0x97, 0x67};
std::vector<unsigned char> vec(pk, pk+ sizeof(pk));

CPubKey pubkey(vec);
if (pubkey.IsValid()) {
    cout << "valid" << endl;   
} else {
    cout << "invalid" << endl;
}

zono (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 1


View Profile
June 17, 2017, 04:26:30 AM
 #3

Thank you so much. Problem solved. "valid" was showed!

Code:
#include "utilstrencodings.h"

std::vector<unsigned char> vec = ParseHex("0396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767");

CPubKey pubkey(vec);
if (pubkey.IsValid()) {
    cout << "valid" << endl;
} else {
    cout << "invalid" << endl;
}
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!