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

<pre><code>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);
</code></pre>

CC BY-SA 4.0
2018-11-21T15:46:50.850Z
2018-11-21T15:46:50.850Z
JBaczuk
<p>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,</p>

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

<p>Or are they too much chalk and cheese?</p>

<p>Thank you,</p>

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

<blockquote>
<pre><code>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&lt;unsigned char&gt; data;
scriptPubKey1.GetOp(it, opcode, data); // data now contains the byte vector

CPubKey pubkey3 = CPubKey(data);
</code></pre>
</blockquote>

CC BY-SA 4.0
2018-11-21T16:37:40.857Z
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
CC BY-SA 4.0
2018-11-21T16:53:45.517Z
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
CC BY-SA 4.0
2018-11-21T20:15:02.160Z
@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
CC BY-SA 4.0
2018-11-24T05:44:31.000Z
@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
<bitcoin-core><bitcoin-core-development>
CScript to CPubKey or derive CPubKey from CScript
LeosSire
# CScript to CPubKey or derive CPubKey from CScript

(Asked by: username_0 on 2018-11-21)

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);
>
> ```

**Tags:** bitcoin-core, bitcoin-core-development

> **Comments:**
> **username_1**: 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?  
> **username_0**: 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.  
> **username_2**: @username_0 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.  
> **username_1**: @username_0 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?  

---

## 1 answers

### Answer by username_2 on 2018-11-21. Score: 0

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);

```


---

