<blockquote>
<p><strong>How to differentiate between BTC transactions and BRC20 transactions</strong></p>
</blockquote>
<p>Well, you can't because they are not disjoint sets, all BRC-20 transactions are also BTC transactions.</p>
<p>You identify BTC transactions that are also BRC-20 transactions in two stages:</p>
<ol>
<li>Does it contain an Ordinals Inscription</li>
<li>Does the inscription contain a BRC-20 operation.</li>
</ol>
<p>You look in the penultimate witness component for the Ordinals signature (&quot;ord&quot; etc)</p>
<p>You look in the inscription for JSON text content that matches the BRC-20 patterns.</p>
<p>In your example, just converting your &quot;witness&quot; from hexadecimal to ASCII produces</p>
<p>... <code>ord</code> ... <code>text/plain;charset=utf-8</code> ... <code>{&quot;p&quot;:&quot;brc-20&quot;,&quot;op&quot;:&quot;mint&quot;,&quot;tick&quot;:&quot;free&quot;,&quot;amt&quot;:&quot;1000&quot;}</code></p>
<p>But parsing it properly would be more reliable.</p>
<hr />
<h3>Parsing the Witness data</h3>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>Hexadecimal data</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>03</code></td>
<td>There are three witness components</td>
</tr>
<tr>
<td><code>40</code></td>
<td>Length of first component is 0x40 = 64 bytes</td>
</tr>
<tr>
<td><code>5d88c3c66893b950</code> <code>a42d3672f3009a9f</code> <code>15e674f248a1d1c4</code> <code>35609440b91e4265</code> <code>98d9505752c560b2</code> <code>4ba6261f1b3c7e99</code> <code>7941006c9ead7b03</code> <code>78ca7c92c6551941</code></td>
<td>Witness component 1</td>
</tr>
<tr>
<td><code>83</code></td>
<td>Length of second component is 0x83 = 131 bytes</td>
</tr>
<tr>
<td><code>20117f692257b233</code> <code>1233b5705ce9c682</code> <code>be8719ff1b2b64cb</code> <code>ca290bd6faeb5442</code> <code>3eac061b237bd587</code> <code>01750063036f7264</code> <code>010118746578742f</code> <code>706c61696e3b6368</code> <code>61727365743d7574</code> <code>662d3800357b2270</code> <code>223a226272632d32</code> <code>30222c226f70223a</code> <code>226d696e74222c22</code> <code>7469636b223a2266</code> <code>726565222c22616d</code> <code>74223a2231303030</code> <code>227d68</code></td>
<td>Witness component 2</td>
</tr>
<tr>
<td><code>21</code></td>
<td>Length of witness component 3 is 0x21 = 33 bytes</td>
</tr>
<tr>
<td><code>c0117f692257b233</code> <code>1233b5705ce9c682</code> <code>be8719ff1b2b64cb</code> <code>ca290bd6faeb5442</code> <code>3e</code></td>
<td>Witness component 3</td>
</tr>
</tbody>
</table>
</div>
<p>Different input types (for output types P2WPKH, P2WSH, P2TR etc) have different things in the witness lists. This can be seen in answers to <a href="https://bitcoin.stackexchange.com/q/107872/13866">What are the scriptPubKey, scriptSig, redeem script and witness for the various output types?</a></p>
<h3>Parsing the penultimate witness component (component 2)</h3>
<p>The <a href="https://docs.ordinals.com/inscriptions.html" rel="nofollow noreferrer">Ordinals Inscription documentation</a> says</p>
<blockquote>
<p>A text inscription containing the string &quot;Hello, world!&quot; is serialized
as follows:</p>
<pre><code>OP_FALSE 
OP_IF
   OP_PUSH &quot;ord&quot;
   OP_PUSH 1
   OP_PUSH &quot;text/plain;charset=utf-8&quot;
   OP_PUSH 0   
   OP_PUSH &quot;Hello, world!&quot;
OP_ENDIF
</code></pre>
</blockquote>
<p>Here's what I came up with treating component 2 as bitcoin script:</p>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>Hexadecimal data</th>
<th>Normal Meaning</th>
<th>Commentary</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>20</code></td>
<td>Push the next 0x20 (32) bytes onto the stack</td>
<td></td>
</tr>
<tr>
<td><code>117f692257b23312</code> <code>33b5705ce9c682be</code> <code>8719ff1b2b64cbca</code> <code>290bd6faeb54423e</code></td>
<td>Pushed data</td>
<td></td>
</tr>
<tr>
<td><code>ac</code></td>
<td>OP_CHECKSIG</td>
<td><em>(Note that nothing after this ends up affecting the final stack value and so has no effect on normal Bitcoin nodes. For them, the following might as well not exist)</em></td>
</tr>
<tr>
<td><code>06</code></td>
<td>Push the next 0x06 (6) bytes onto the stack</td>
<td></td>
</tr>
<tr>
<td><code>1b237bd58701</code></td>
<td>Pushed data</td>
<td></td>
</tr>
<tr>
<td><code>75</code></td>
<td>OP_DROP</td>
<td><em>(throws away data just pushed onto stack - presumably private data of inscriber?)</em></td>
</tr>
<tr>
<td><code>00</code></td>
<td>OP_FALSE</td>
<td><em>(This is the start of the Ordinals Inscription serialization)</em></td>
</tr>
<tr>
<td><code>63</code></td>
<td>OP_IF</td>
<td><em>(Since the previous operation added FALSE to the stack, script execution on normal bitcoin nodes will skip everything up to OP_ENDIF. The rest still has to look like valid script though.)</em></td>
</tr>
<tr>
<td><code>03</code></td>
<td>Push the next 0x03 (3) bytes onto the stack</td>
<td></td>
</tr>
<tr>
<td><code>6f7264</code></td>
<td>Pushed data = ASCII <code>ord</code></td>
<td></td>
</tr>
<tr>
<td><code>01</code></td>
<td>Push the next 0x01 (1) byte onto the stack</td>
<td></td>
</tr>
<tr>
<td><code>01</code></td>
<td>Pushed data has value <code>1</code></td>
<td><em>(Ordinals indication that MIME type follows)</em></td>
</tr>
<tr>
<td><code>18</code></td>
<td>Push the next 0x18 (24) bytes onto the stack</td>
<td></td>
</tr>
<tr>
<td><code>746578742f706c61</code> <code>696e3b6368617273</code> <code>65743d7574662d38</code></td>
<td>Pushed data = ASCII <code>text/plain;</code> <code>charset=utf-8</code></td>
<td><em>(I am surprised &quot;application/json&quot; wasn't used)</em></td>
</tr>
<tr>
<td><code>00</code></td>
<td>OP_0: Push integer <code>0</code> onto the stack</td>
<td><em>(Ordinals indication that inscription data follows)</em></td>
</tr>
<tr>
<td><code>35</code></td>
<td>Push the next 0x35 (53) bytes onto the stack</td>
<td></td>
</tr>
<tr>
<td><code>7b2270223a226272</code> <code>632d3230222c226f</code> <code>70223a226d696e74</code> <code>222c227469636b22</code> <code>3a2266726565222c</code> <code>22616d74223a2231</code> <code>303030227d</code></td>
<td>Pushed data = ASCII <code>{</code> <code>&quot;p&quot;:&quot;brc-20&quot;,</code> <code>&quot;op&quot;:&quot;mint&quot;,</code> <code>&quot;tick&quot;:&quot;free&quot;,</code> <code>&quot;amt&quot;:&quot;1000&quot;</code> <code>}</code></td>
<td><em>(This is an inscription in JSON format (despite prior MIME type) that contains a BRC-20 token minting operation.)</em></td>
</tr>
<tr>
<td><code>68</code></td>
<td>OP_ENDIF</td>
<td><em>(in this case, end of Ordinals Inscription)</em></td>
</tr>
</tbody>
</table>
</div>
<p>Obviously you are looking for the script elements that are prescribed for Ordinals inscriptions. the last but one item above is the actual inscription. It can be parsed as JSON to give the BRC-20 data structure.  Obviously <code>p</code>'s value, <code>brc-20</code>, is what you are searching for in the JSON data.</p>
<hr />
<p>Related</p>
<ul>
<li><a href="https://bitcoin.stackexchange.com/q/107872/13866">What are the scriptPubKey, scriptSig, redeem script and witness for the various output types?</a></li>
</ul>

CC BY-SA 4.0
2023-07-21T13:15:26.303Z
Thanks, you said it so well. I understand now.
But what if there are more than one in the witness? Just like this:
I see multiple strings in witness that are used, split : 



    "inputs": [
        {
            "sequence": 4294967293,
            "witness": "03405d88c3c03405d88c3c, 03405d88c3c,03405d88c3c",
            "script": "",
            "index": 0,
Mashaji
CC BY-SA 4.0
2023-07-21T14:44:23.253Z
`"witness": "03405d88c3c03405d88c3c, 03405d88c3c,03405d88c3c",` looks like a bug in whatever blockchain explorer you are using. I suggest you work with the raw binary data, or at least raw hex, rather than this, or any other, blockchain explorer's JSON interpretation
RedGrittyBrick
CC BY-SA 4.0
2023-08-04T10:29:41.627Z
Thanks, You're a genius. 
How to analyze ? Witness component 1 , Witness component 2, Witness component 3. 
If I want a complete analysis, what should I do.
The input field here is too small, I added the case to the question.
Mashaji
CC BY-SA 4.0
2023-08-04T10:30:34.703Z
That's an interesting follow-on question but not one I have an answer to at the moment. It is something I will probably spend time on eventually.
RedGrittyBrick
CC BY-SA 4.0
2023-10-03T11:33:19.133Z
@Mashaji See "Related" link I added to bottom of answer. Answers to that related question indicate what witness components are expected, and their meaning, for each different type of transaction input.
RedGrittyBrick
CC BY-SA 4.0
2023-07-19T09:44:29.627Z
2023-10-03T11:39:29.667Z
2023-10-03T11:39:29.667Z
RedGrittyBrick
<p>How to differentiate between BTC transactions and BRC20 transactions on a blockchain using the RCP interface?</p>
<p>This is a transaction record that contains BRC20 tokens which I found on a blockchain explorer.</p>
<p>When I using the RPC to scan blocks and retrieve transaction records like this one, how can I use Python3 to determine whether the transaction is a BRC20 or BTC transaction?</p>
<pre class="lang-json prettyprint-override"><code>{
    &quot;hash&quot;: &quot;331d02db3d6ee0cffae9052be5799bd7754c6ebb2020e422058b73334c59bf85&quot;,
    &quot;ver&quot;: 2,
    &quot;vin_sz&quot;: 1,
    &quot;vout_sz&quot;: 2,
    &quot;size&quot;: 359,
    &quot;weight&quot;: 734,
    &quot;fee&quot;: 7360,
    &quot;relayed_by&quot;: &quot;0.0.0.0&quot;,
    &quot;lock_time&quot;: 0,
    &quot;tx_index&quot;: 4705820269244017,
    &quot;double_spend&quot;: false,
    &quot;time&quot;: 1682913851,
    &quot;block_index&quot;: 787738,
    &quot;block_height&quot;: 787738,
    &quot;inputs&quot;: [
        {
            &quot;sequence&quot;: 4294967293,
            &quot;witness&quot;: &quot;03405d88c3c66893b950a42d3672f3009a9f15e674f248a1d1c435609440b91e426598d9505752c560b24ba6261f1b3c7e997941006c9ead7b0378ca7c92c65519418320117f692257b2331233b5705ce9c682be8719ff1b2b64cbca290bd6faeb54423eac061b237bd58701750063036f7264010118746578742f706c61696e3b636861727365743d7574662d3800357b2270223a226272632d3230222c226f70223a226d696e74222c227469636b223a2266726565222c22616d74223a2231303030227d6821c0117f692257b2331233b5705ce9c682be8719ff1b2b64cbca290bd6faeb54423e&quot;,
            &quot;script&quot;: &quot;&quot;,
            &quot;index&quot;: 0,
            &quot;prev_out&quot;: {
                &quot;addr&quot;: &quot;bc1pejeuynu5edq5fvfa8elpkem6zuzvrvqx20zundhpyuy3h3y8wumslwyd5h&quot;,
                &quot;n&quot;: 0,
                &quot;script&quot;: &quot;5120ccb3c24f94cb4144b13d3e7e1b677a1704c1b00653c5c9b6e127091bc4877737&quot;,
                &quot;spending_outpoints&quot;: [
                    {
                        &quot;n&quot;: 0,
                        &quot;tx_index&quot;: 4705820269244017
                    }
                ],
                &quot;spent&quot;: true,
                &quot;tx_index&quot;: 6896182349770388,
                &quot;type&quot;: 0,
                &quot;value&quot;: 10000
            }
        }
    ],
    &quot;out&quot;: [
        {
            &quot;type&quot;: 0,
            &quot;spent&quot;: false,
            &quot;value&quot;: 546,
            &quot;spending_outpoints&quot;: [],
            &quot;n&quot;: 0,
            &quot;tx_index&quot;: 4705820269244017,
            &quot;script&quot;: &quot;51208873ed390476c5c7b6c825bae7eca675a46bc200cc7a82354b0debec19f83abc&quot;,
            &quot;addr&quot;: &quot;bc1p3pe76wgywmzu0dkgykaw0m9xwkjxhssqe3agyd2tph47cx0c827qd4yq9x&quot;
        },
        {
            &quot;type&quot;: 0,
            &quot;spent&quot;: true,
            &quot;value&quot;: 2094,
            &quot;spending_outpoints&quot;: [
                {
                    &quot;tx_index&quot;: 1274202281123575,
                    &quot;n&quot;: 0
                }
            ],
            &quot;n&quot;: 1,
            &quot;tx_index&quot;: 4705820269244017,
            &quot;script&quot;: &quot;0014ea410b1701aeaa64dbf721cd55fce1231f2144a3&quot;,
            &quot;addr&quot;: &quot;bc1qafqsk9cp464xfklhy8x4tl8pyv0jz39rhhh0s7&quot;
        }
    ]
}

</code></pre>
<p>How to analyze ? Witness component 1 , Witness component 2, Witness component 3.</p>
<p>If I want a complete analysis, what should I do.</p>
<p>The analogy is as follows:</p>
<pre class="lang-json prettyprint-override"><code>{
  &quot;p&quot;: &quot;brc-20&quot;,
  &quot;op&quot;: &quot;mint&quot;,
  &quot;tick&quot;: &quot;free&quot;,
  &quot;amt&quot;: &quot;1000&quot;
}
</code></pre>
<pre><code>ID 331d02db3d6ee0cffae9052be5799bd7754c6ebb2020e422058b73334c59bf85i0
Length 53
Type text/plain;charset=utf-8
Timestamp 01 May 2023 12:04:11
Block height 787738
Size 133
Standard: brc-20
Operation: mint
Token: free
Amount: 1000
</code></pre>

<transactions><blockchain><json-rpc><python><ordinals>
How to differentiate between BTC transactions and BRC20 transactions on a blockchain?
Mashaji
# How to differentiate between BTC transactions and BRC20 transactions on a blockchain?

(Asked by: username_0 on 2023-07-19)

How to differentiate between BTC transactions and BRC20 transactions on a blockchain using the RCP interface?
This is a transaction record that contains BRC20 tokens which I found on a blockchain explorer.
When I using the RPC to scan blocks and retrieve transaction records like this one, how can I use Python3 to determine whether the transaction is a BRC20 or BTC transaction?
{
    "hash": "331d02db3d6ee0cffae9052be5799bd7754c6ebb2020e422058b73334c59bf85",
    "ver": 2,
    "vin_sz": 1,
    "vout_sz": 2,
    "size": 359,
    "weight": 734,
    "fee": 7360,
    "relayed_by": "0.0.0.0",
    "lock_time": 0,
    "tx_index": 4705820269244017,
    "double_spend": false,
    "time": 1682913851,
    "block_index": 787738,
    "block_height": 787738,
    "inputs": [
        {
            "sequence": 4294967293,
            "witness": "03405d88c3c66893b950a42d3672f3009a9f15e674f248a1d1c435609440b91e426598d9505752c560b24ba6261f1b3c7e997941006c9ead7b0378ca7c92c65519418320117f692257b2331233b5705ce9c682be8719ff1b2b64cbca290bd6faeb54423eac061b237bd58701750063036f7264010118746578742f706c61696e3b636861727365743d7574662d3800357b2270223a226272632d3230222c226f70223a226d696e74222c227469636b223a2266726565222c22616d74223a2231303030227d6821c0117f692257b2331233b5705ce9c682be8719ff1b2b64cbca290bd6faeb54423e",
            "script": "",
            "index": 0,
            "prev_out": {
                "addr": "bc1pejeuynu5edq5fvfa8elpkem6zuzvrvqx20zundhpyuy3h3y8wumslwyd5h",
                "n": 0,
                "script": "5120ccb3c24f94cb4144b13d3e7e1b677a1704c1b00653c5c9b6e127091bc4877737",
                "spending_outpoints": [
                    {
                        "n": 0,
                        "tx_index": 4705820269244017
                    }
                ],
                "spent": true,
                "tx_index": 6896182349770388,
                "type": 0,
                "value": 10000
            }
        }
    ],
    "out": [
        {
            "type": 0,
            "spent": false,
            "value": 546,
            "spending_outpoints": [],
            "n": 0,
            "tx_index": 4705820269244017,
            "script": "51208873ed390476c5c7b6c825bae7eca675a46bc200cc7a82354b0debec19f83abc",
            "addr": "bc1p3pe76wgywmzu0dkgykaw0m9xwkjxhssqe3agyd2tph47cx0c827qd4yq9x"
        },
        {
            "type": 0,
            "spent": true,
            "value": 2094,
            "spending_outpoints": [
                {
                    "tx_index": 1274202281123575,
                    "n": 0
                }
            ],
            "n": 1,
            "tx_index": 4705820269244017,
            "script": "0014ea410b1701aeaa64dbf721cd55fce1231f2144a3",
            "addr": "bc1qafqsk9cp464xfklhy8x4tl8pyv0jz39rhhh0s7"
        }
    ]
}


How to analyze ? Witness component 1 , Witness component 2, Witness component 3.
If I want a complete analysis, what should I do.
The analogy is as follows:
{
  "p": "brc-20",
  "op": "mint",
  "tick": "free",
  "amt": "1000"
}

ID 331d02db3d6ee0cffae9052be5799bd7754c6ebb2020e422058b73334c59bf85i0
Length 53
Type text/plain;charset=utf-8
Timestamp 01 May 2023 12:04:11
Block height 787738
Size 133
Standard: brc-20
Operation: mint
Token: free
Amount: 1000

**Tags:** transactions, blockchain, json-rpc, python, ordinals


---

## 1 answers

### Answer by username_1 on 2023-07-19. Score: 4 | Accepted answer

How to differentiate between BTC transactions and BRC20 transactions

Well, you can't because they are not disjoint sets, all BRC-20 transactions are also BTC transactions.
You identify BTC transactions that are also BRC-20 transactions in two stages:

Does it contain an Ordinals Inscription
Does the inscription contain a BRC-20 operation.

You look in the penultimate witness component for the Ordinals signature ("ord" etc)
You look in the inscription for JSON text content that matches the BRC-20 patterns.
In your example, just converting your "witness" from hexadecimal to ASCII produces
... ord ... text/plain;charset=utf-8 ... {"p":"brc-20","op":"mint","tick":"free","amt":"1000"}
But parsing it properly would be more reliable.

Parsing the Witness data




Hexadecimal data
Meaning




03
There are three witness components


40
Length of first component is 0x40 = 64 bytes


5d88c3c66893b950 a42d3672f3009a9f 15e674f248a1d1c4 35609440b91e4265 98d9505752c560b2 4ba6261f1b3c7e99 7941006c9ead7b03 78ca7c92c6551941
Witness component 1


83
Length of second component is 0x83 = 131 bytes


20117f692257b233 1233b5705ce9c682 be8719ff1b2b64cb ca290bd6faeb5442 3eac061b237bd587 01750063036f7264 010118746578742f 706c61696e3b6368 61727365743d7574 662d3800357b2270 223a226272632d32 30222c226f70223a 226d696e74222c22 7469636b223a2266 726565222c22616d 74223a2231303030 227d68
Witness component 2


21
Length of witness component 3 is 0x21 = 33 bytes


c0117f692257b233 1233b5705ce9c682 be8719ff1b2b64cb ca290bd6faeb5442 3e
Witness component 3




Different input types (for output types P2WPKH, P2WSH, P2TR etc) have different things in the witness lists. This can be seen in answers to What are the scriptPubKey, scriptSig, redeem script and witness for the various output types?
Parsing the penultimate witness component (component 2)
The Ordinals Inscription documentation says

A text inscription containing the string "Hello, world!" is serialized
as follows:
OP_FALSE 
OP_IF
   OP_PUSH "ord"
   OP_PUSH 1
   OP_PUSH "text/plain;charset=utf-8"
   OP_PUSH 0   
   OP_PUSH "Hello, world!"
OP_ENDIF


Here's what I came up with treating component 2 as bitcoin script:




Hexadecimal data
Normal Meaning
Commentary




20
Push the next 0x20 (32) bytes onto the stack



117f692257b23312 33b5705ce9c682be 8719ff1b2b64cbca 290bd6faeb54423e
Pushed data



ac
OP_CHECKSIG
(Note that nothing after this ends up affecting the final stack value and so has no effect on normal Bitcoin nodes. For them, the following might as well not exist)


06
Push the next 0x06 (6) bytes onto the stack



1b237bd58701
Pushed data



75
OP_DROP
(throws away data just pushed onto stack - presumably private data of inscriber?)


00
OP_FALSE
(This is the start of the Ordinals Inscription serialization)


63
OP_IF
(Since the previous operation added FALSE to the stack, script execution on normal bitcoin nodes will skip everything up to OP_ENDIF. The rest still has to look like valid script though.)


03
Push the next 0x03 (3) bytes onto the stack



6f7264
Pushed data = ASCII ord



01
Push the next 0x01 (1) byte onto the stack



01
Pushed data has value 1
(Ordinals indication that MIME type follows)


18
Push the next 0x18 (24) bytes onto the stack



746578742f706c61 696e3b6368617273 65743d7574662d38
Pushed data = ASCII text/plain; charset=utf-8
(I am surprised "application/json" wasn't used)


00
OP_0: Push integer 0 onto the stack
(Ordinals indication that inscription data follows)


35
Push the next 0x35 (53) bytes onto the stack



7b2270223a226272 632d3230222c226f 70223a226d696e74 222c227469636b22 3a2266726565222c 22616d74223a2231 303030227d
Pushed data = ASCII { "p":"brc-20", "op":"mint", "tick":"free", "amt":"1000" }
(This is an inscription in JSON format (despite prior MIME type) that contains a BRC-20 token minting operation.)


68
OP_ENDIF
(in this case, end of Ordinals Inscription)




Obviously you are looking for the script elements that are prescribed for Ordinals inscriptions. the last but one item above is the actual inscription. It can be parsed as JSON to give the BRC-20 data structure.  Obviously p's value, brc-20, is what you are searching for in the JSON data.

Related

What are the scriptPubKey, scriptSig, redeem script and witness for the various output types?

> **Comments:**
> **username_0**: Thanks, you said it so well. I understand now. But what if there are more than one in the witness? Just like this: I see multiple strings in witness that are used, split :         "inputs": [         {             "sequence": 4294967293,             "witness": "03405d88c3c03405d88c3c, 03405d88c3c,03405d88c3c",             "script": "",             "index": 0,  
> **username_1**: `"witness": "03405d88c3c03405d88c3c, 03405d88c3c,03405d88c3c",` looks like a bug in whatever blockchain explorer you are using. I suggest you work with the raw binary data, or at least raw hex, rather than this, or any other, blockchain explorer's JSON interpretation  
> **username_0**: Thanks, You're a genius.  How to analyze ? Witness component 1 , Witness component 2, Witness component 3.  If I want a complete analysis, what should I do. The input field here is too small, I added the case to the question.  
> **username_1**: That's an interesting follow-on question but not one I have an answer to at the moment. It is something I will probably spend time on eventually.  
> **username_1**: @username_0 See "Related" link I added to bottom of answer. Answers to that related question indicate what witness components are expected, and their meaning, for each different type of transaction input.  

---

