Please can someone advise if it is possible to derive the CPubKey from a CSript object? Provided i get it up with an address before hand,

I can use numerous functions to attain all the info from the CPubKey using functions, but can you make a CPubKey from a CSript?

Or are they too much chalk and cheese?

Thank you,

EDIT: Following the first answer I've tried the below to no joy. The script returns totally empty:

CScript scriptPubKey1;
CBitcoinAddress address("L4VPgtoBtuxsYMgyrRvQRadSV1Y139TDfd");
scriptPubKey1.SetDestination(address.Get());

CScript::const_iterator it = scriptPubKey1.begin(); // or specifically, the location of the pubkey in the CScript object
opcodetype opcode;
std::vector<unsigned char> data;
scriptPubKey1.GetOp(it, opcode, data); // data now contains the byte vector

CPubKey pubkey3 = CPubKey(data);
  • What are you trying to do? Extract the key used in a script? Or deterministically randomly generate a key from the script? Do you want any relation between who can spend the script and who can spend the key? – Pieter Wuille Nov 21 at 16:37
  • I want to generate a pubkey in code based on an address. Allowing me to use this pubkey in transactions later on. A roundabout way of doing things but it works with what i'm trying to do. – LeosSire Nov 21 at 16:53
  • @LeosSire You cannot get the public key from an address because addresses are either pubkey hash (p2pkh) or script hashes (p2sh) and you cannot reverse a hash. – JBaczuk Nov 21 at 20:15
  • @LeosSire I understand you want a "pubkey based on an address". But what does that mean? What are you trying to do? I have a feeling there is either an alternative solution to your problem, or an easier way of formulating it? What properties do you want that pubkey to have? – Pieter Wuille Nov 24 at 5:44

If I understand the question, I believe you can construct a CPubKey from a byte vector extracted from the CScript script like:

CScript::const_iterator it = script.begin(); // or specifically, the location of the pubkey in the CScript object
opcodetype opcode;
valtype data;

script.GetOp(iterator, opcode, data); // data now contains the byte vector
CPubKey pubKey = CPubKey(data);

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.