<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by ngn999 on Medium]]></title>
        <description><![CDATA[Stories by ngn999 on Medium]]></description>
        <link>https://medium.com/@ngn?source=rss-388b49429751------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7vFRL7Xplxpi2QcXUY3iMQ.jpeg</url>
            <title>Stories by ngn999 on Medium</title>
            <link>https://medium.com/@ngn?source=rss-388b49429751------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 18:31:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ngn/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[parsing bitcoin transaction script in lldb]]></title>
            <link>https://ngn.medium.com/bitcoin-transaction-script-language-40a45c37febc?source=rss-388b49429751------2</link>
            <guid isPermaLink="false">https://medium.com/p/40a45c37febc</guid>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[lldb]]></category>
            <dc:creator><![CDATA[ngn999]]></dc:creator>
            <pubDate>Thu, 29 Jun 2023 11:53:51 GMT</pubDate>
            <atom:updated>2023-06-29T12:03:19.719Z</atom:updated>
            <content:encoded><![CDATA[<p>Bitcoin transactions use a language similar to forth, based on the stack, which can flexibly support a variety of transactions without solidifying the transaction structure. Currently, Bitcoin can support P2PK (pay to pubKey), P2PKH (pay to pubKey hash), P2SH (pay to script hash), and MultiSignature, all thanks to this transaction script.</p><p>The script is a reverse Polish expression, a simple example: 1 2 OP_ADD, the evaluation is to use a stack to process the script from left to right.</p><p>During debug, you may see the script looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DcPmCeJJ2UmzW_MU.png" /></figure><p>It’s not very intuitive. The Bitcoin client provides a parsing tool. After obtaining the hexadecimal representation of the script, you can parse it with bitcoin-cli:</p><pre>./src/bitcoin-cli -regtest  decodescript &#39;2103d3143a045dfc59f041a23d7031f7e7fd68baea182ef9ed8c870857fec0bbafabac&#39;</pre><pre>{<br>  &quot;asm&quot;: &quot;03d3143a045dfc59f041a23d7031f7e7fd68baea182ef9ed8c870857fec0bbafab OP_CHECKSIG&quot;,<br>  &quot;reqSigs&quot;: 1,<br>  &quot;type&quot;: &quot;pubkey&quot;,<br>  &quot;addresses&quot;: [<br>    &quot;mxpkWsE7M86rGSbMzhU8WxQSZb5AfffQT9&quot;<br>  ],<br>  &quot;p2sh&quot;: &quot;2MsL3aNaHW7SwfP5Xd3Mznuc7WgMM3QtJp7&quot;,<br>  &quot;segwit&quot;: {<br>    &quot;asm&quot;: &quot;0 bdd7cafe931821a737b9c4a2dcaa69f0938fc763&quot;,<br>    &quot;hex&quot;: &quot;0014bdd7cafe931821a737b9c4a2dcaa69f0938fc763&quot;,<br>    &quot;reqSigs&quot;: 1,<br>    &quot;type&quot;: &quot;witness_v0_keyhash&quot;,<br>    &quot;addresses&quot;: [<br>      &quot;bcrt1qhhtu4l5nrqs6wdaecj3de2nf7zfcl3mrghcrl8&quot;<br>    ],<br>    &quot;p2sh-segwit&quot;: &quot;2N69LDh74ALiZ5qex721of8DtWjdcHHZQEN&quot;<br>  }<br>}</pre><p>03d3143a045dfc59f041a23d7031f7e7fd68baea182ef9ed8c870857fec0bbafab OP_CHECKSIG is the locking script of this Output, and this Output is paid to the public key 03d3143a045dfc59f041a23d7031f7e7fd68baea182ef9ed8c870857fec0bbafab. To use this Bitcoin, the owner needs to provide an unlocking script. The unlocking script must meet the condition that the script composed of the unlocking script and the locking script is true after evaluation. The current unlocking script is a digital signature made with the correct private key.</p><p>This is still not convenient enough as it requires switching back and forth between apps. Translating the script into code that is easy to read, not evaluating this script, is not complex and can be considered for writing a command in python in lldb.</p><h3>Python Script</h3><p>The operations you execute in lldb, lldb provides the corresponding python api.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49240be48de2bb885f70b11b55cdb8fc/href">https://medium.com/media/49240be48de2bb885f70b11b55cdb8fc/href</a></iframe><p>Once you have loaded this script:</p><pre>(lldb) command script import /path/to/print_script.py<br>The &quot;print_script&quot; python command has been installed and is ready for use.</pre><p>There is now a print_script command in lldb. This command takes the variable name of the script from the command&#39;s parameters, then traverses the script content, processes PUSH instructions, skips the data part, and for other instructions, calls GetOpName(opcode) to get the readable instruction name.</p><p>When you need to print the transaction script:</p><pre>(lldb) print_script script<br>33 0x03d3143a045dfc59f041a23d7031f7e7fd68baea182ef9ed8c870857fec0bbafab OP_CHECKSIG</pre><h3>References</h3><ul><li><a href="https://lldb.llvm.org/use/python-reference.html">lldb Python Reference</a></li><li><a href="https://lldb.llvm.org/python_reference/index.html">https://lldb.llvm.org/python_reference/index.html</a></li><li><a href="https://lldb.llvm.org/use/python.html">lldb Python script Demo</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40a45c37febc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[parse html use perl regular]]></title>
            <link>https://ngn.medium.com/parse-html-use-perl-regular-6ed154d7c30b?source=rss-388b49429751------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ed154d7c30b</guid>
            <category><![CDATA[perl]]></category>
            <dc:creator><![CDATA[ngn999]]></dc:creator>
            <pubDate>Mon, 25 Apr 2022 09:04:33 GMT</pubDate>
            <atom:updated>2022-04-25T09:08:33.325Z</atom:updated>
            <content:encoded><![CDATA[<h3>parsing html use perl regular expression</h3><p>embed code in Medium:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e29708dd1be4d3145bfbe7e18f7d1e6/href">https://medium.com/media/6e29708dd1be4d3145bfbe7e18f7d1e6/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ed154d7c30b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[parsing bitcoin transaction script in lldb]]></title>
            <link>https://ngn.medium.com/%E5%9C%A8lldb%E4%B8%AD%E8%A7%A3%E6%9E%90%E6%AF%94%E7%89%B9%E5%B8%81%E4%BA%A4%E6%98%93-script-422e774a7934?source=rss-388b49429751------2</link>
            <guid isPermaLink="false">https://medium.com/p/422e774a7934</guid>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[script]]></category>
            <category><![CDATA[lldb]]></category>
            <dc:creator><![CDATA[ngn999]]></dc:creator>
            <pubDate>Mon, 25 Apr 2022 08:51:16 GMT</pubDate>
            <atom:updated>2023-06-29T11:52:44.164Z</atom:updated>
            <content:encoded><![CDATA[<p>比特币的交易使用一种类 forth,基于栈的语言,这样能比较灵活地支持各种各样的交易,没 有把交易的结构写死.比特币当前能支持 P2PK (pay to pubKey), P2PKH (pay to pubKey hash), P2SH (pay to script hash), 支持多重签名( MultiSignature )都是得益于这个交 易 script.</p><p>script 是一个逆波兰表达式,一个简单的例子: 1 2 OP_ADD, 求值就是利用一个栈从左到 右对 script 进行处理.</p><p>debug 时看到 script 长这样:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RNwH_LXwEH_LRLIH.png" /></figure><p>看起来很不直观, 比特币客户端提供一个解析工具, 拿到script的16进制表达后,用 bitcoin-cli 解析:</p><pre>./src/bitcoin-cli -regtest  decodescript &#39;2103d3143a045dfc59f041a23d7031f7e7fd68baea182ef9ed8c870857fec0bbafabac&#39;</pre><pre>{<br>  &quot;asm&quot;: &quot;03d3143a045dfc59f041a23d7031f7e7fd68baea182ef9ed8c870857fec0bbafab OP_CHECKSIG&quot;,<br>  &quot;reqSigs&quot;: 1,<br>  &quot;type&quot;: &quot;pubkey&quot;,<br>  &quot;addresses&quot;: [<br>    &quot;mxpkWsE7M86rGSbMzhU8WxQSZb5AfffQT9&quot;<br>  ],<br>  &quot;p2sh&quot;: &quot;2MsL3aNaHW7SwfP5Xd3Mznuc7WgMM3QtJp7&quot;,<br>  &quot;segwit&quot;: {<br>    &quot;asm&quot;: &quot;0 bdd7cafe931821a737b9c4a2dcaa69f0938fc763&quot;,<br>    &quot;hex&quot;: &quot;0014bdd7cafe931821a737b9c4a2dcaa69f0938fc763&quot;,<br>    &quot;reqSigs&quot;: 1,<br>    &quot;type&quot;: &quot;witness_v0_keyhash&quot;,<br>    &quot;addresses&quot;: [<br>      &quot;bcrt1qhhtu4l5nrqs6wdaecj3de2nf7zfcl3mrghcrl8&quot;<br>    ],<br>    &quot;p2sh-segwit&quot;: &quot;2N69LDh74ALiZ5qex721of8DtWjdcHHZQEN&quot;<br>  }<br>}</pre><p>03d3143a045dfc59f041a23d7031f7e7fd68baea182ef9ed8c870857fec0bbafab OP_CHECKSIG 就是这个 Output 的锁定脚本, 这个 Output 是支付给公钥 03d3143a045dfc59f041a23d7031f7e7fd68baea182ef9ed8c870857fec0bbafab 的. 要使用 这个比特币, 需要拥有者提供一个解锁脚本, 解锁脚本需要满足, 解锁脚本和锁定脚本组成 的脚本在求值后是True. 当前这个的解锁脚本就是一个正确的私钥做的数字签名.</p><p>这个还是不够方便,需要来回切换 app. 把 script 翻译成方便阅读的代码, 不是去求值这个 script, 并不复杂, 可以考虑用 pyhton 在 lldb里写个 command.</p><h3>python script</h3><p>你在lldb里执行的操作, lldb 都提供了相应的 python api.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49240be48de2bb885f70b11b55cdb8fc/href">https://medium.com/media/49240be48de2bb885f70b11b55cdb8fc/href</a></iframe><p>加载这个脚本</p><pre>(lldb) command script import /path/to/print_script.py<br>The &quot;print_script&quot; python command has been installed and is ready for use.</pre><p>这样lldb就多了一个叫 print_script 的命令, 从命令的参数里拿到脚本的变量名, 然后遍历脚本内容,处理 PUSH类的指令, 跳过数据部分, 其他指令,就调用 GetOpName(opcode) 拿到可读的指令名.</p><p>在遇到要打印交易脚本的时候:</p><pre>(lldb) print_script script<br>33 0x03d3143a045dfc59f041a23d7031f7e7fd68baea182ef9ed8c870857fec0bbafab OP_CHECKSIG</pre><h3>引用</h3><ul><li><a href="https://lldb.llvm.org/use/python-reference.html">lldb Python Reference</a></li><li><a href="https://lldb.llvm.org/python_reference/index.html">https://lldb.llvm.org/python_reference/index.html</a></li><li><a href="https://lldb.llvm.org/use/python.html">lldb Python script Demo</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=422e774a7934" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>