<p>I suppose you are the same person who made this thread on bitcointalk: <a href="https://bitcointalk.org/index.php?topic=1969807.0" rel="nofollow noreferrer">https://bitcointalk.org/index.php?topic=1969807.0</a></p>

<p>I'll answer you here too to help other people too.</p>

<hr>

<p>You are treating the pubkey as a string, but it is really a blob of binary data that is 33 bytes long. What you want to do instead is initialize an array with your pubkey and then use that to initialize your CPubKey.</p>

<p>Your code should look like this:</p>

<pre><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&lt;unsigned char&gt; vec(pk, pk+ sizeof(pk));

CPubKey pubkey(vec);
if (pubkey.IsValid()) {
    cout &lt;&lt; "valid" &lt;&lt; endl;    
} else {
    cout &lt;&lt; "invalid" &lt;&lt; endl;
}
</code></pre>

CC BY-SA 3.0
2017-06-16T16:53:57.220Z
2017-06-16T16:53:57.220Z
Ava Chow
<p>I'm reading bitcoin core source code. I want to know how to get bitcoin address from CTxIn object.</p>

<p>I have a CTransaction object. I found the way how to get addresses from vout like the following. But I don't know about vin.</p>

<pre><code>UniValue out(UniValue::VOBJ);
ScriptPubKeyToUniv(txout.scriptPubKey, out, true);

UniValue u = find_value(out, "addresses");
UniValue uv = u.getValues()[0];
newDestination = uv.get_str();
</code></pre>

<p>CTxIn has a scriptSig. I think I can get address from it.</p>

<p>CTxIn</p>

<p><a href="https://dev.visucore.com/bitcoin/doxygen/class_c_tx_in.html" rel="nofollow noreferrer">https://dev.visucore.com/bitcoin/doxygen/class_c_tx_in.html</a></p>

<hr>

<p>Update 1</p>

<p>I found how to get the pubkey by using ScriptToAsmStr. I know this works for only P2PKH address.</p>

<pre><code>vector&lt;string&gt; array;
CScript s = _tx.vin[0].scriptSig;
string a = ScriptToAsmStr(s, true);
boost::algorithm::split(array, a, boost::is_any_of(" "));
if (array.size() == 2) {
    cout &lt;&lt; array[1] &lt;&lt; endl; // 0396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767
}
</code></pre>

<p>I'm now looking into how to convert the pubkey to bitcoin address on mainnet.</p>

<hr>

<p>Update 2</p>

<p>I tried it like following but "invalid" is showed.. </p>

<pre><code>const char *cstr = "0396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767";
std::vector&lt;unsigned char&gt; vec(cstr, cstr + strlen(cstr));

CPubKey pubkey(vec);
if (pubkey.IsValid()) {
    cout &lt;&lt; "valid" &lt;&lt; endl;    
} else {
    cout &lt;&lt; "invalid" &lt;&lt; endl;
}
</code></pre>

<hr>

<p>Update 3</p>

<p>Thanks to the Andrew Chow's answer, I could make a CPubKey object. All that is left is to output the bitcoin address from the object.</p>

<pre><code>#include "utilstrencodings.h"

std::vector&lt;unsigned char&gt; vec = ParseHex("0396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767");

CPubKey pubkey(vec);
if (pubkey.IsValid()) {
    cout &lt;&lt; "valid" &lt;&lt; endl;
} else {
    cout &lt;&lt; "invalid" &lt;&lt; endl;
}
</code></pre>

<hr>

<p>Update 4</p>

<p>Finally, I got a bitcoin address from a CTxIn object. I think this is not smart way. There might be other ways.
And still not sure about CKeyID's meaning. I will look into it in detail.</p>

<pre><code>CKeyID id = pubkey.GetID();
CBitcoinAddress address(id);
if (address.IsValid()) {
    cout &lt;&lt; "valid" &lt;&lt; endl;    
} else {
    cout &lt;&lt; "invalid" &lt;&lt; endl;
}

cout &lt;&lt; address.ToString() &lt;&lt; endl; // 15L7U55PAsHLEpQkZqz62e3eqWd9AHb2DH
</code></pre>

<bitcoin-core-development><transaction-input>
How to get address from CTxIn object
zono
# How to get address from CTxIn object

(Asked by: username_0 on 2017-06-16)

I'm reading bitcoin core source code. I want to know how to get bitcoin address from CTxIn object.

I have a CTransaction object. I found the way how to get addresses from vout like the following. But I don't know about vin.

```
UniValue out(UniValue::VOBJ);
ScriptPubKeyToUniv(txout.scriptPubKey, out, true);

UniValue u = find_value(out, "addresses");
UniValue uv = u.getValues()[0];
newDestination = uv.get_str();

```

CTxIn has a scriptSig. I think I can get address from it.

CTxIn

<https://dev.visucore.com/bitcoin/doxygen/class_c_tx_in.html>

---

Update 1

I found how to get the pubkey by using ScriptToAsmStr. I know this works for only P2PKH address.

```
vector<string> array;
CScript s = _tx.vin[0].scriptSig;
string a = ScriptToAsmStr(s, true);
boost::algorithm::split(array, a, boost::is_any_of(" "));
if (array.size() == 2) {
    cout << array[1] << endl; // 0396f8781a4900372a5d72d84718d146170d5983e67dff8b4a28fef80690c09767
}

```

I'm now looking into how to convert the pubkey to bitcoin address on mainnet.

---

Update 2

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

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

```

---

Update 3

Thanks to the Andrew username_1's answer, I could make a CPubKey object. All that is left is to output the bitcoin address from the object.

```
#include "utilstrencodings.h"

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

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

```

---

Update 4

Finally, I got a bitcoin address from a CTxIn object. I think this is not smart way. There might be other ways.
And still not sure about CKeyID's meaning. I will look into it in detail.

```
CKeyID id = pubkey.GetID();
CBitcoinAddress address(id);
if (address.IsValid()) {
    cout << "valid" << endl;    
} else {
    cout << "invalid" << endl;
}

cout << address.ToString() << endl; // 15L7U55PAsHLEpQkZqz62e3eqWd9AHb2DH

```

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


---

## 1 answers

### Answer by username_1 on 2017-06-16. Score: 4 | Accepted answer

I suppose you are the same person who made this thread on bitcointalk: <https://bitcointalk.org/index.php?topic=1969807.0>

I'll answer you here too to help other people too.

---

You are treating the pubkey as a string, but it is really a blob of binary data that is 33 bytes long. What you want to do instead is initialize an array with your pubkey and then use that to initialize your CPubKey.

Your code should look like this:

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

```


---

