diff --git a/BTC/Advanced/OP_CAT/example/index.js b/BTC/Advanced/OP_CAT/example/index.js deleted file mode 100644 index 62038a416..000000000 --- a/BTC/Advanced/OP_CAT/example/index.js +++ /dev/null @@ -1,33 +0,0 @@ -const bitcoin = require('bitcoinjs-lib'); - -// 创建一个包含 OP_CAT 的脚本 -function createScript() { - // 连接的目标结果 - const target = Buffer.from('HelloWorld'); - - const script = bitcoin.script.compile([ - bitcoin.opcodes.OP_DUP, - bitcoin.opcodes.OP_HASH160, - Buffer.from('...'), // 使用适当的公钥哈希 - bitcoin.opcodes.OP_EQUALVERIFY, - bitcoin.opcodes.OP_CHECKSIG, - // 连接两个字符串 - Buffer.from('Hello'), // 第一个元素 - Buffer.from('World'), // 第二个元素 - bitcoin.opcodes.OP_CAT, // 连接操作,必须在两个字符串之后 - target, // 连接后的目标结果 - bitcoin.opcodes.OP_EQUAL // 验证连接结果是否等于 'HelloWorld' - ]); - - return script; -} - -// 创建和打印 P2SH 地址 -function createP2SHAddress() { - const script = createScript(); - const { address } = bitcoin.payments.p2sh({ redeem: { output: script, network: bitcoin.networks.bitcoin } }); - - console.log('P2SH Address:', address); -} - -createP2SHAddress(); diff --git a/BTC/Advanced/Psbt/rawParsePSBT.js b/BTC/Advanced/Psbt/rawParsePSBT.js deleted file mode 100644 index 9f0759221..000000000 --- a/BTC/Advanced/Psbt/rawParsePSBT.js +++ /dev/null @@ -1,75 +0,0 @@ -const parsePSBT = (psbtBase64) => { - const psbtBuffer = Buffer.from(psbtBase64, 'base64'); - let index = 0; - - // 魔术字节和分隔符 - const magicBytes = psbtBuffer.slice(index, index + 5); - index += 5; - if (magicBytes.toString('hex') !== '70736274ff') { - throw new Error('Not a valid PSBT'); - } - - // 解析全局部分 - console.log("Global Data:"); - while (psbtBuffer[index] !== 0x00 && index < psbtBuffer.length) { - - if (index + 2 > psbtBuffer.length) break; // Prevent reading beyond buffer - const keyLen = psbtBuffer[index]; - const key = psbtBuffer.slice(index + 1, index + 1 + keyLen); - index += 1 + keyLen; - - if (index + 1 > psbtBuffer.length) break; // Prevent reading beyond buffer - const valueLen = psbtBuffer.readIntLE(index, 1); - const value = psbtBuffer.slice(index + 1, index + 1 + valueLen); - index += 1 + valueLen; - - console.log(`Key: ${key.toString('hex')}, Value: ${value.toString('hex')}`); - } - index++; // Skip separator - - // 解析输入部分 - console.log("Inputs:"); - while (index < psbtBuffer.length && psbtBuffer[index] !== 0x00) { - while (psbtBuffer[index] !== 0x00 && index < psbtBuffer.length) { - - if (index + 2 > psbtBuffer.length) break; - const keyLen = psbtBuffer[index]; - const key = psbtBuffer.slice(index + 1, index + 1 + keyLen); - index += 1 + keyLen; - - if (index + 1 > psbtBuffer.length) break; - const valueLen = psbtBuffer.readUIntLE(index, 1); - const value = psbtBuffer.slice(index + 1, index + 1 + valueLen); - index += 1 + valueLen; - console.log(`Key: ${key.toString('hex')}, Value: ${value.toString('hex')}`); - } - index++; // Skip separator - } - index++; // Skip separator - - // 解析输出部分 - console.log("Outputs:"); - while (index < psbtBuffer.length && psbtBuffer[index] !== 0x00) { - console.log(`Output at index ${index}:`); - while (psbtBuffer[index] !== 0x00 && index < psbtBuffer.length) { - if (index + 2 > psbtBuffer.length) break; - const keyLen = psbtBuffer[index]; - const key = psbtBuffer.slice(index + 1, index + 1 + keyLen); - index += 1 + keyLen; - - if (index + 1 > psbtBuffer.length) break; - const valueLen = psbtBuffer.readUIntLE(index, 1); - const value = psbtBuffer.slice(index + 1, index + 1 + valueLen); - index += 1 + valueLen; - - console.log(`Key: ${key.toString('hex')}, Value: ${value.toString('hex')}`); - } - index++; // Skip separator - } -}; - - -// 用你提供的Base64字符串测试 -const psbtBase64 = - "cHNidP8BAHUCAAAAASaBcTce3/KF6Tet7qSze3gADAVmy7OtZGQXE8pCFxv2AAAAAAD+////AtPf9QUAAAAAGXapFNDFmQPFusKGh2DpD9UhpGZap2UgiKwA4fUFAAAAABepFDVF5uM7gyxHBQ8k0+65PJwDlIvHh7MuEwAAAQD9pQEBAAAAAAECiaPHHqtNIOA3G7ukzGmPopXJRjr6Ljl/hTPMti+VZ+UBAAAAFxYAFL4Y0VKpsBIDna89p95PUzSe7LmF/////4b4qkOnHf8USIk6UwpyN+9rRgi7st0tAXHmOuxqSJC0AQAAABcWABT+Pp7xp0XpdNkCxDVZQ6vLNL1TU/////8CAMLrCwAAAAAZdqkUhc/xCX/Z4Ai7NK9wnGIZeziXikiIrHL++E4sAAAAF6kUM5cluiHv1irHU6m80GfWx6ajnQWHAkcwRAIgJxK+IuAnDzlPVoMR3HyppolwuAJf3TskAinwf4pfOiQCIAGLONfc0xTnNMkna9b7QPZzMlvEuqFEyADS8vAtsnZcASED0uFWdJQbrUqZY3LLh+GFbTZSYG2YVi/jnF6efkE/IQUCSDBFAiEA0SuFLYXc2WHS9fSrZgZU327tzHlMDDPOXMMJ/7X85Y0CIGczio4OFyXBl/saiK9Z9R5E5CVbIBZ8hoQDHAXR8lkqASECI7cr7vCWXRC+B3jv7NYfysb3mk6haTkzgHNEZPhPKrMAAAAAAAAA"; -parsePSBT(psbtBase64); diff --git a/BTC/README-ES.md b/BTC/README-ES.md deleted file mode 100644 index 2917707ec..000000000 --- a/BTC/README-ES.md +++ /dev/null @@ -1,105 +0,0 @@ -# Bitcoin (BTC) - -This knowledge base aims to provide comprehensive information on Bitcoin (BTC) technology, covering basic features to advanced applications, as well as various protocols and tools within its ecosystem. Whether you are a beginner or an experienced developer, this guide will help you better understand and utilize Bitcoin technology. - -## Introduction - -### The Significance of Bitcoin - -Bitcoin (BTC) is the world's first decentralized cryptocurrency. Since its release by Satoshi Nakamoto in 2009, it has had a profound impact on the global financial system. Its significance includes but is not limited to the following points: - -1. **Financial Democratization**: Bitcoin achieves peer-to-peer transactions without intermediaries through decentralized blockchain technology, giving individuals more financial control and freedom. - -2. **Security and Transparency**: The Bitcoin network uses cryptographic technology to ensure transaction security and immutability. All transaction records are publicly transparent and can be verified by anyone. - -3. **Limited Supply**: The total supply of Bitcoin is permanently capped at 21 million, making it a deflationary asset and earning it the title of "digital gold". - -4. **Global Liquidity**: As a global currency, Bitcoin can be traded and transferred across borders, promoting international trade and cross-border payments. - -5. **Technological Innovation**: Bitcoin has pioneered blockchain technology, driving innovations such as smart contracts and decentralized finance (DeFi), ushering in a new era of the internet. - -Through this knowledge base, you will learn about the basic features, advanced characteristics, and ecosystem applications of Bitcoin, helping you stay ahead in this rapidly evolving field. - -## 1. Basic Features - -### 1.1 Payment Addresses - -Types of Bitcoin addresses and their encoding methods: - -- [x] **P2PKH** (Pay to Public Key Hash) -- [x] **P2SH-P2PKH** (Pay to Script Hash - Pay to Public Key Hash) -- [x] **P2WPKH** (Pay to Witness Public Key Hash) -- [x] **P2TR** (Pay to Taproot) -- [x] **Base58** Encoding -- [x] **Bech32** Encoding -- [x] **Bech32m** Encoding - -### 1.2 Wallets - -Types and features of Bitcoin wallets: - -- [ ] **Standard Wallets** Standard wallets -- [ ] **HD Wallets** Hierarchical Deterministic wallets - -### 1.3 Transactions - -Knowledge related to Bitcoin transactions: - -- [x] **Signature Algorithms** Signature algorithms -- [ ] **Transaction Construction** Transaction construction -- [ ] **Fee Estimation** Fee estimation - -### 1.4 Tools - -Common tools in the Bitcoin ecosystem: - -- [ ] **Blockchain Explorers** Blockchain explorers -- [ ] **API Services** API services -- [ ] **Data Analysis Tools** Data analysis tools - -## 2. Advanced Features - -### 2.1 PSBT (Partially Signed Bitcoin Transaction) - -Introduction and usage of Partially Signed Bitcoin Transactions (PSBT): - -- [x] **PSBT Protocol Introduction** PSBT protocol introduction -- [x] **Creating and Parsing PSBT** Creating and parsing PSBT - -### 2.2 Taproot - -Introduction and advantages of Taproot technology: - -- [x] **Schnorr Signatures** Schnorr signatures -- [x] **MAST** (Merkelized Abstract Syntax Tree) -- [ ] **Privacy and Efficiency Improvements** Privacy and efficiency improvements - -### 2.3 Multisig Addresses - -Process of using multisig addresses: - -- [x] **Multisig Address Intro** Multisig address introduction -- [ ] **OP_CHECKMULTISIG** -- [ ] **OP_CHECKMULTISIGVERIFY** -- [ ] **OP_CHECKSIGADD** - -## 3. Ecosystem Applications - -### 3.1 BRC20 - -- [x] Protocol introduction - -### 3.2 ARC20 - -- [x] Protocol introduction - -### 3.3 Runes - -- [x] Protocol introduction - -### 3.4 Lightning Network - -Introduction and core logic of the Lightning Network: - -- [ ] **Protocol Introduction** Protocol introduction -- [ ] **Core Logic** Core logic diff --git a/BTC/README.md b/BTC/README.md deleted file mode 100644 index 1c159439f..000000000 --- a/BTC/README.md +++ /dev/null @@ -1,83 +0,0 @@ -# Bitcoin (BTC) - -本知识库旨在提供有关比特币(BTC)技术的全面信息,涵盖从基本功能到高级应用,以及其生态系统中的各个协议和工具。无论您是初学者还是有经验的开发者,本指南都能帮助您更好地理解和利用比特币技术。 - -## 简介 -比特币(BTC)是世界上第一个去中心化的加密货币,自2009年由中本聪(Satoshi Nakamoto)发布以来,已经对全球金融体系产生了深远的影响。其重大意义包括但不限于以下几点: - -1. **金融民主化**:比特币通过去中心化的区块链技术,实现了无需中介的点对点交易,赋予了个人更多的金融控制权和自由。 - -2. **安全和透明**:比特币网络利用密码学技术确保了交易的安全性和不可篡改性,并且所有交易记录公开透明,可供任何人验证。 - -3. **有限供应**:比特币总量被永久限定在2100万枚,这一稀缺性使得它成为一种抗通胀的资产储备手段,被誉为“数字黄金”。 - -4. **全球流动性**:作为一种全球性货币,比特币可以跨越国界进行交易和转移,促进了国际贸易和跨境支付的发展。 - -5. **技术创新**:比特币引领了区块链技术的发展,推动了智能合约、去中心化金融(DeFi)等创新应用,开启了新的互联网时代。 - -通过本知识库,您将学习到比特币的基本功能、高级特性以及生态系统应用,帮助您在这一快速发展的领域中保持领先。 - -## 1. Basic Features - -### 1.1 Payment Addresses -比特币地址类型及其编码方式: -- [x] **P2PKH** (Pay to Public Key Hash) -- [x] **P2SH-P2PKH** (Pay to Script Hash - Pay to Public Key Hash) -- [x] **P2WPKH** (Pay to Witness Public Key Hash) -- [x] **P2TR** (Pay to Taproot) -- [x] **Base58** 编码 -- [x] **Bech32** 编码 -- [x] **Bech32m** 编码 - -### 1.2 Wallets -比特币钱包类型及其特性: -- [ ] **Standard Wallets** 标准钱包 -- [ ] **HD Wallets** 分层确定性钱包 - -### 1.3 Transactions -比特币交易的相关知识: -- [x] **Signature Algorithms** 签名算法 -- [ ] **Transaction Construction** 交易构建 -- [ ] **Fee Estimation** 费用估算 - -### 1.4 Tools -比特币生态系统中常用的工具: -- [ ] **Blockchain Explorers** 区块链浏览器 -- [ ] **API Services** API 服务 -- [ ] **Data Analysis Tools** 数据分析工具 - -## 2. Advanced Features - -### 2.1 PSBT (Partially Signed Bitcoin Transaction) -部分签名比特币交易(PSBT)的介绍和使用: -- [x] **PSBT Protocol Introduction** PSBT 协议简介 -- [x] **Creating and Parsing PSBT** 创建和解析 PSBT - -### 2.2 Taproot -Taproot 技术的介绍及其优势: -- [x] **Schnorr Signatures** 施诺尔签名 -- [x] **MAST** (Merkelized Abstract Syntax Tree) -- [ ] **Privacy and Efficiency Improvements** 隐私和效率改进 - -### 2.3 Multisig Addresses -多重签名地址的使用过程: -- [x] **Multisig Address Intro** 多重签名简介 -- [ ] **OP_CHECKMULTISIG** -- [ ] **OP_CHECKMULTISIGVERIFY** -- [ ] **OP_CHECKSIGADD** - -## 3. Ecosystem Applications - -### 3.1 BRC20 -- [x] 协议介绍 - -### 3.2 ARC20 -- [x] 协议介绍 - -### 3.3 Runes -- [x] 协议介绍 - -### 3.4 Lightning Network -闪电网络的介绍和核心逻辑: -- [ ] **Protocol Introduction** 协议介绍 -- [ ] **Core Logic** 核心逻辑 \ No newline at end of file diff --git a/BTC/Advanced/Multisign/OP_CHECKMULTISIG/README.md b/btc/Advanced/Multisign/OP_CHECKMULTISIG/README.md similarity index 100% rename from BTC/Advanced/Multisign/OP_CHECKMULTISIG/README.md rename to btc/Advanced/Multisign/OP_CHECKMULTISIG/README.md diff --git a/BTC/Advanced/Multisign/OP_CHECKMULTISIGVERIFY/README.md b/btc/Advanced/Multisign/OP_CHECKMULTISIGVERIFY/README.md similarity index 100% rename from BTC/Advanced/Multisign/OP_CHECKMULTISIGVERIFY/README.md rename to btc/Advanced/Multisign/OP_CHECKMULTISIGVERIFY/README.md diff --git a/BTC/Advanced/Multisign/OP_CHECKSIGADD/README.md b/btc/Advanced/Multisign/OP_CHECKSIGADD/README.md similarity index 100% rename from BTC/Advanced/Multisign/OP_CHECKSIGADD/README.md rename to btc/Advanced/Multisign/OP_CHECKSIGADD/README.md diff --git a/BTC/Advanced/Multisign/README.md b/btc/Advanced/Multisign/README.md similarity index 100% rename from BTC/Advanced/Multisign/README.md rename to btc/Advanced/Multisign/README.md diff --git a/BTC/Advanced/OP_CAT/README.md b/btc/Advanced/OP_CAT/README.md similarity index 100% rename from BTC/Advanced/OP_CAT/README.md rename to btc/Advanced/OP_CAT/README.md diff --git a/BTC/Advanced/OP_CAT/example/README.md b/btc/Advanced/OP_CAT/example/README.md similarity index 100% rename from BTC/Advanced/OP_CAT/example/README.md rename to btc/Advanced/OP_CAT/example/README.md diff --git a/btc/Advanced/OP_CAT/example/index.js b/btc/Advanced/OP_CAT/example/index.js new file mode 100644 index 000000000..cfcdac613 --- /dev/null +++ b/btc/Advanced/OP_CAT/example/index.js @@ -0,0 +1,33 @@ +const bitcoin = require('bitcoinjs-lib'); + +// 创建一个包含 OP_CAT 的脚本 +function createScript() { + // 连接的目标结果 + const target = Buffer.from('HelloWorld'); + + const script = bitcoin.script.compile([ + bitcoin.opcodes.OP_DUP, + bitcoin.opcodes.OP_HASH160, + Buffer.from('...'), // 使用适当的公钥哈希 + bitcoin.opcodes.OP_EQUALVERIFY, + bitcoin.opcodes.OP_CHECKSIG, + // 连接两个字符串 + Buffer.from('Hello'), // 第一个元素 + Buffer.from('World'), // 第二个元素 + bitcoin.opcodes.OP_CAT, // 连接操作,必须在两个字符串之后 + target, // 连接后的目标结果 + bitcoin.opcodes.OP_EQUAL, // 验证连接结果是否等于 'HelloWorld' + ]); + + return script; +} + +// 创建和打印 P2SH 地址 +function createP2SHAddress() { + const script = createScript(); + const { address } = bitcoin.payments.p2sh({ redeem: { output: script, network: bitcoin.networks.bitcoin } }); + + console.log('P2SH Address:', address); +} + +createP2SHAddress(); diff --git a/BTC/Advanced/Psbt/README.md b/btc/Advanced/Psbt/README.md similarity index 100% rename from BTC/Advanced/Psbt/README.md rename to btc/Advanced/Psbt/README.md diff --git a/BTC/Advanced/Psbt/encode.md b/btc/Advanced/Psbt/encode.md similarity index 92% rename from BTC/Advanced/Psbt/encode.md rename to btc/Advanced/Psbt/encode.md index 05697ed4e..8727e992c 100644 --- a/BTC/Advanced/Psbt/encode.md +++ b/btc/Advanced/Psbt/encode.md @@ -5,6 +5,7 @@ PSBT(Partially Signed Bitcoin Transaction)的编码和解码是一个关键 PSBT的基本格式是一个序列化的数据结构,包含所有必要的信息,使多个参与者能够独立进行交易签名和验证。这个格式遵循一定的编码规则,详细见[BIP174](https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki),通常以Base64或十六进制形式进行传输。 **The Partially Signed Bitcoin Transaction (PSBT) format** + ```bash # whole structure := * * @@ -51,6 +52,7 @@ PSBT的编码过程包括将交易的各个部分转换成可序列化的格式 4. **验证和签名**:在交易数据完全恢复后,参与者可以验证交易的有效性,并在必要时添加自己的签名。 ## PSBT 创建 + 以下是一个使用JavaScript和`bitcoinjs-lib`库来处理PSBT(Partially Signed Bitcoin Transaction)的基本示例。这个例子展示了如何创建一个PSBT,添加输入和输出,进行签名,并最终导出为可以广播的比特币交易。 ### 环境设置 @@ -80,22 +82,22 @@ let psbt = new bitcoin.Psbt({ network: network }); // 假设这是UTXO的详细信息,你需要从区块链或钱包获取这些信息 const utxo = { - hash: 'transaction-hash-of-utxo-here', // 事务哈希 - index: 0, // 输出索引 - value: 100000 // Satoshis + hash: 'transaction-hash-of-utxo-here', // 事务哈希 + index: 0, // 输出索引 + value: 100000, // Satoshis }; // 添加输入,这里假设UTXO已经知道 psbt.addInput({ - hash: utxo.hash, - index: utxo.index, - nonWitnessUtxo: Buffer.from('raw-transaction-hex-here', 'hex') + hash: utxo.hash, + index: utxo.index, + nonWitnessUtxo: Buffer.from('raw-transaction-hex-here', 'hex'), }); // 添加输出,指定接收地址和金额(satoshis) psbt.addOutput({ - address: 'recipient-address-here', - value: 90000 // 减去手续费 + address: 'recipient-address-here', + value: 90000, // 减去手续费 }); // 对PSBT进行签名 @@ -124,8 +126,8 @@ console.log(`Transaction ready to be broadcasted: ${tx}`); 7. **完成PSBT**:完成所有输入,锁定PSBT,使之准备好转换为最终的交易格式。 8. **导出交易**:将PSBT转换为一个标准的比特币交易,并将其转换为hex格式,准备广播到网络。 - ## PSBT 解析 + 要解析PSBT(Partially Signed Bitcoin Transaction)并查看其详细内容,你可以使用`bitcoinjs-lib`库在JavaScript环境中执行这一任务。下面的示例将展示如何读取一个PSBT,解析其内容,以及如何查看关键信息,如输入和输出详情。 ### 示例:解析PSBT @@ -134,20 +136,21 @@ console.log(`Transaction ready to be broadcasted: ${tx}`); const bitcoin = require('bitcoinjs-lib'); // 假设这是一个已经存在的PSBT的Base64编码字符串 -const psbtBase64 = 'cHNidP8BAHECAAAAAZtuEiavDmeZR6WUjIhjFbkFh7m+yywCfHlni6uTrgH/////AZD+GwAAAAAAF6kU9BeD5tGIzjL0VzU3+kEUNw+HME3/////8C0wbdQAAAAAAF6kUAAAAAAAAiACATWsYQSE/C85hv5jZVDezl0l4AAAAAAAEBKxAnAAAAAAAAFgAUjVv8Fy0Qdk84W/oBhiMllC+HIHg='; +const psbtBase64 = + 'cHNidP8BAHECAAAAAZtuEiavDmeZR6WUjIhjFbkFh7m+yywCfHlni6uTrgH/////AZD+GwAAAAAAF6kU9BeD5tGIzjL0VzU3+kEUNw+HME3/////8C0wbdQAAAAAAF6kUAAAAAAAAiACATWsYQSE/C85hv5jZVDezl0l4AAAAAAAEBKxAnAAAAAAAAFgAUjVv8Fy0Qdk84W/oBhiMllC+HIHg='; // 将Base64编码的PSBT解码 const psbt = bitcoin.Psbt.fromBase64(psbtBase64); // 遍历PSBT的输入和输出 psbt.data.inputs.forEach((input, index) => { - console.log(`Input ${index}:`); - console.log(input); + console.log(`Input ${index}:`); + console.log(input); }); psbt.data.outputs.forEach((output, index) => { - console.log(`Output ${index}:`); - console.log(output); + console.log(`Output ${index}:`); + console.log(output); }); // 如果需要更详细地查看某个特定的输入或输出,可以根据实际需要提取更多信息 @@ -160,6 +163,7 @@ psbt.data.outputs.forEach((output, index) => { 3. **打印信息**:在控制台打印每个输入和输出的详细信息,这有助于开发者理解和调试PSBT。 ## 原生解析 + 在比特币的PSBT(Partially Signed Bitcoin Transaction)处理中,原生编码和解码是指不使用任何外部库直接操作PSBT格式数据的方法。这通常涉及到直接处理PSBT的二进制表示,理解其结构,并根据规范来手动解析或构建数据。由于PSBT规范的复杂性,这一过程比较繁琐,但非常重要,因为它能提供对PSBT工作原理的深入理解。 ### PSBT数据结构 @@ -189,4 +193,3 @@ PSBT是一种包含多个键值对的数据结构,每个部分都有其特定 3. **逐个解析输入部分**:对于每个输入,读取其键值对,直到遇到分隔符。 4. **逐个解析输出部分**:对于每个输出,同样读取其键值对。 5. **验证完整性**:确保所有必要的数据都已正确解析,并且数据结构符合PSBT的规范。 - diff --git a/btc/Advanced/Psbt/rawParsePSBT.js b/btc/Advanced/Psbt/rawParsePSBT.js new file mode 100644 index 000000000..ed664d5c5 --- /dev/null +++ b/btc/Advanced/Psbt/rawParsePSBT.js @@ -0,0 +1,72 @@ +const parsePSBT = (psbtBase64) => { + const psbtBuffer = Buffer.from(psbtBase64, 'base64'); + let index = 0; + + // 魔术字节和分隔符 + const magicBytes = psbtBuffer.slice(index, index + 5); + index += 5; + if (magicBytes.toString('hex') !== '70736274ff') { + throw new Error('Not a valid PSBT'); + } + + // 解析全局部分 + console.log('Global Data:'); + while (psbtBuffer[index] !== 0x00 && index < psbtBuffer.length) { + if (index + 2 > psbtBuffer.length) break; // Prevent reading beyond buffer + const keyLen = psbtBuffer[index]; + const key = psbtBuffer.slice(index + 1, index + 1 + keyLen); + index += 1 + keyLen; + + if (index + 1 > psbtBuffer.length) break; // Prevent reading beyond buffer + const valueLen = psbtBuffer.readIntLE(index, 1); + const value = psbtBuffer.slice(index + 1, index + 1 + valueLen); + index += 1 + valueLen; + + console.log(`Key: ${key.toString('hex')}, Value: ${value.toString('hex')}`); + } + index++; // Skip separator + + // 解析输入部分 + console.log('Inputs:'); + while (index < psbtBuffer.length && psbtBuffer[index] !== 0x00) { + while (psbtBuffer[index] !== 0x00 && index < psbtBuffer.length) { + if (index + 2 > psbtBuffer.length) break; + const keyLen = psbtBuffer[index]; + const key = psbtBuffer.slice(index + 1, index + 1 + keyLen); + index += 1 + keyLen; + + if (index + 1 > psbtBuffer.length) break; + const valueLen = psbtBuffer.readUIntLE(index, 1); + const value = psbtBuffer.slice(index + 1, index + 1 + valueLen); + index += 1 + valueLen; + console.log(`Key: ${key.toString('hex')}, Value: ${value.toString('hex')}`); + } + index++; // Skip separator + } + index++; // Skip separator + + // 解析输出部分 + console.log('Outputs:'); + while (index < psbtBuffer.length && psbtBuffer[index] !== 0x00) { + console.log(`Output at index ${index}:`); + while (psbtBuffer[index] !== 0x00 && index < psbtBuffer.length) { + if (index + 2 > psbtBuffer.length) break; + const keyLen = psbtBuffer[index]; + const key = psbtBuffer.slice(index + 1, index + 1 + keyLen); + index += 1 + keyLen; + + if (index + 1 > psbtBuffer.length) break; + const valueLen = psbtBuffer.readUIntLE(index, 1); + const value = psbtBuffer.slice(index + 1, index + 1 + valueLen); + index += 1 + valueLen; + + console.log(`Key: ${key.toString('hex')}, Value: ${value.toString('hex')}`); + } + index++; // Skip separator + } +}; + +// 用你提供的Base64字符串测试 +const psbtBase64 = + 'cHNidP8BAHUCAAAAASaBcTce3/KF6Tet7qSze3gADAVmy7OtZGQXE8pCFxv2AAAAAAD+////AtPf9QUAAAAAGXapFNDFmQPFusKGh2DpD9UhpGZap2UgiKwA4fUFAAAAABepFDVF5uM7gyxHBQ8k0+65PJwDlIvHh7MuEwAAAQD9pQEBAAAAAAECiaPHHqtNIOA3G7ukzGmPopXJRjr6Ljl/hTPMti+VZ+UBAAAAFxYAFL4Y0VKpsBIDna89p95PUzSe7LmF/////4b4qkOnHf8USIk6UwpyN+9rRgi7st0tAXHmOuxqSJC0AQAAABcWABT+Pp7xp0XpdNkCxDVZQ6vLNL1TU/////8CAMLrCwAAAAAZdqkUhc/xCX/Z4Ai7NK9wnGIZeziXikiIrHL++E4sAAAAF6kUM5cluiHv1irHU6m80GfWx6ajnQWHAkcwRAIgJxK+IuAnDzlPVoMR3HyppolwuAJf3TskAinwf4pfOiQCIAGLONfc0xTnNMkna9b7QPZzMlvEuqFEyADS8vAtsnZcASED0uFWdJQbrUqZY3LLh+GFbTZSYG2YVi/jnF6efkE/IQUCSDBFAiEA0SuFLYXc2WHS9fSrZgZU327tzHlMDDPOXMMJ/7X85Y0CIGczio4OFyXBl/saiK9Z9R5E5CVbIBZ8hoQDHAXR8lkqASECI7cr7vCWXRC+B3jv7NYfysb3mk6haTkzgHNEZPhPKrMAAAAAAAAA'; +parsePSBT(psbtBase64); diff --git a/BTC/Advanced/Taproot/MAST.md b/btc/Advanced/Taproot/MAST.md similarity index 94% rename from BTC/Advanced/Taproot/MAST.md rename to btc/Advanced/Taproot/MAST.md index f9c4499dd..de593c4f4 100644 --- a/BTC/Advanced/Taproot/MAST.md +++ b/btc/Advanced/Taproot/MAST.md @@ -1,9 +1,11 @@ ## MAST简介 + Merkelized Alternative Script Tree (MAST) 是比特币的一种增强功能,它利用 Merkle 树结构将不同的花费条件(脚本)组织起来,只公开实际使用的脚本分支,提高隐私性和效率。在 MAST 中,有几个关键角色(或身份),它们在构建和验证 Merkle 树时扮演重要作用: ### 关键角色 1. **叶子节点(Leaf Nodes)** + - **定义**: 叶子节点是 Merkle 树中的基础节点,每个叶子节点代表一个具体的花费条件或脚本。叶子节点通常哈希值进行字典序(lexicographical order)排序。排序的目的是确保 Merkle 树的结构和哈希值是唯一且确定的,这样不同的参与者在构建相同的 Merkle 树时可以得到相同的根哈希值,从而确保交易的一致性和可验证性。 - **功能**: 包含实际的比特币脚本(例如条件 A、B、C),这些脚本定义了不同的花费条件。 - **示例**: @@ -12,11 +14,13 @@ Merkelized Alternative Script Tree (MAST) 是比特币的一种增强功能, - 条件 C: Alice 和 Bob 联合签名可以立即花费。 2. **内部节点(Internal Nodes)** + - **定义**: 内部节点是 Merkle 树中的中间节点,每个内部节点是其子节点哈希值的组合。 - **功能**: 通过将其子节点的哈希值组合在一起,内部节点形成了从叶子节点到根节点的路径。 - **示例**: 如果 `A` 和 `B` 是叶子节点,那么 `H1 = Hash(A, B)` 是内部节点。 3. **根节点(Root Node)** + - **定义**: 根节点是 Merkle 树的顶层节点,代表整个树的哈希值。 - **功能**: 根节点的哈希值用于验证整个 Merkle 树的完整性和有效性。 - **示例**: 如果 `H1` 和 `H2` 是内部节点,那么 `Root = Hash(H1, H2)` 是根节点。 @@ -59,6 +63,7 @@ Merkelized Alternative Script Tree (MAST) 是比特币的一种增强功能, 这种结构使得未使用的条件(例如 A 和 B)保持隐藏,提高了交易的隐私性,同时提供了高效的验证机制。 ## MAST的创建、更新和移除 + 实现 MAST(Merkelized Alternative Script Tree) 的创建、插入和删除功能,可以使用 JavaScript 结合一些加密库来完成。以下是一个基本的示例,演示如何创建、插入和删除 MAST 中的节点。 ```javascript @@ -153,6 +158,7 @@ console.log('Root:', mast.getRoot()); ``` ### 解释 + - **hash(data)**: 计算给定数据的 SHA-256 哈希值。 - **addLeaf(data)**: 添加叶子节点,添加后重建 Merkle 树。 - **removeLeaf(data)**: 删除叶子节点,删除后重建 Merkle 树。 @@ -162,12 +168,14 @@ console.log('Root:', mast.getRoot()); - **printTree()**: 打印整个 Merkle 树。 ### 注意事项 + 1. **排序**: 每次构建树时,叶子节点都根据其哈希值进行字典序排序,以确保树的唯一性和确定性。 2. **树结构**: `tree` 属性是一个数组,存储从叶子节点到根节点的所有层次。 这个示例实现了基本的 MAST 功能,包括创建、插入和删除叶子节点。 ## Taproot中的应用 + ![$$Q = P + t*G$$](https://aandds.com/blog/images/taproot_tweak.gif) 如上图,在Taproot中需要在MAST中针对叶子结点、树结点和根结点分别引入TapLeaf、TapBranch和TapTweak去计算hash。 @@ -184,7 +192,10 @@ const { payments } = bitcoin; // 创建 Tagged Hash 函数 function taggedHash(tag, data) { const tagHash = crypto.createHash('sha256').update(tag).digest(); - return crypto.createHash('sha256').update(Buffer.concat([tagHash, tagHash, data])).digest(); + return crypto + .createHash('sha256') + .update(Buffer.concat([tagHash, tagHash, data])) + .digest(); } // Taproot Tweak 函数 @@ -250,11 +261,12 @@ mast.addLeaf(Buffer.from('Condition A')); mast.addLeaf(Buffer.from('Condition B')); mast.addLeaf(Buffer.from('Condition C')); console.log('Initial MAST:'); -console.log(mast.leaves.map(leaf => leaf.toString('hex'))); +console.log(mast.leaves.map((leaf) => leaf.toString('hex'))); console.log('MAST Root:', mast.getMerkleRoot().toString('hex')); ``` ### 代码说明 + 1. **taggedHash(tag, data)**: 创建 Tagged Hash 函数,用于 Taproot Tweak 和 TapLeaf/TapBranch。 2. **tapTweakPubkey(pubkey, h)**: 使用 Tagged Hash 和内部公钥计算 Tweaked 公钥。 3. **tapLeaf(version, script)**: 计算 TapLeaf 的哈希值。 @@ -265,12 +277,15 @@ console.log('MAST Root:', mast.getMerkleRoot().toString('hex')); - `buildTree(leaves)`: 递归构建 Merkle 树。 ### Taproot地址计算 + 生成Taproot地址,实际上是对Tweaked 公钥的X坐标编码为 Bech32m 格式 **流程** + - 初始化 MAST 并添加叶子节点。 - 构建 Merkle 树并计算根节点。 - 生成内部公钥并计算 Tweaked 公钥。 - 生成并输出 Taproot 地址。 + ```js // 生成内部公钥 const keyPair = bip32.fromSeed(crypto.randomBytes(32)); @@ -290,9 +305,13 @@ console.log('Taproot Address:', taprootAddress); ``` ### 锁定脚本(scriptPubKey) -对于隔离见证 Output,其 scriptPubKey 为 **OP_n tweaked-public-key** + +对于隔离见证 Output,其 scriptPubKey 为 **OP_n tweaked-public-key** + ##### OP_n + OP_n 表示隔离见证版本,版本 0 隔离见证 Output 的 scriptPubKey 的首个字节是 0x00,而版本 1 隔离见证 Output 的 scriptPubKey 的首个字节是 0x51 + ```js OP_0: 0x00 // segwitV0 OP_1: 0x51 // segwitV1,即Taproot @@ -301,23 +320,29 @@ OP_2: 0x52 See: https://github.com/bitcoin/bitcoin/blob/v22.0/src/script/script.h#L68 ``` + #### tweaked-public-key + tweaked-public-key的计算比较复杂,有internal public key和script tree的Merkle Root组成,然后再进行Bech32m编码就能得到Taproot地址 + ##### script path + script path是Taproot中比较灵活、同时比较复杂的一种方式 tweaked-public-key的计算比较如上图:$$Q = P + t*G$$ #### key path + key path不需要 Script Path,则可以去掉 Script 相关的哈希 即:$$Q = P + t*G = P + TaggedHash('TapTweak', P)G$$ 钱包中的taproot地址就是基于**用户公钥做P**代入上方公式推导得到 #### **代码实现** + ```js const mast = new MAST(); -mast.addLeaf(Buffer.from('OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG')); -mast.addLeaf(Buffer.from('OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG')); -mast.addLeaf(Buffer.from('OP_2 OP_2 OP_CHECKMULTISIG')); +mast.addLeaf(Buffer.from("OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG")); +mast.addLeaf(Buffer.from("OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG")); +mast.addLeaf(Buffer.from("OP_2 OP_2 OP_CHECKMULTISIG")); // 生成内部公钥 const keyPair = bip32.fromSeed(crypto.randomBytes(32)); @@ -331,22 +356,20 @@ const { tweakedPubkey } = tapTweakPubkey(internalPubkey, mastRoot); const tweakedPubkeyX = tweakedPubkey.slice(1, 33); // 生成锁定脚本(P2TR 地址的锁定脚本) -const lockingScript = bitcoin.script.compile([ - bitcoin.opcodes.OP_1, - tweakedPubkeyX -]); +const lockingScript = bitcoin.script.compile([bitcoin.opcodes.OP_1, tweakedPubkeyX]); console.log('Locking Script:', lockingScript.toString('hex')); ``` - ### 解锁脚本(witnessScript) + Taproot属于Segwit v1版本,其相关解锁脚本放在Witness位置 -如果在花费 P2TR UTXO 时,Witness 只包含一个元素,则是 P2TR (Key Path),如果在花费 P2TR UTXO 时,Witness 至少包含两个元素,则是 P2TR (Script Path)。在花费一个 P2TR UTXO 时,是通过 Witness 中元素的个数来决定使用 Key Path(Witness 元素个数为 1)还是 Script Path(Witness 元素个数大于等于 2)。 +如果在花费 P2TR UTXO 时,Witness 只包含一个元素,则是 P2TR (Key Path),如果在花费 P2TR UTXO 时,Witness 至少包含两个元素,则是 P2TR (Script Path)。在花费一个 P2TR UTXO 时,是通过 Witness 中元素的个数来决定使用 Key Path(Witness 元素个数为 1)还是 Script Path(Witness 元素个数大于等于 2)。 在 Taproot 中,解锁script path可以使用 Taproot Tree 的任意路径之一来满足条件。 下面代码以解锁一个script tree中的2-2多签叶子结点C为例: -#### **代码实现** +#### **代码实现** + ```js class MAST { // ... @@ -378,9 +401,9 @@ class MAST { } const mast = new MAST(); -const scriptA = Buffer.from('OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG'); -const scriptB = Buffer.from('OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG'); -const scriptC = Buffer.from('OP_2 OP_2 OP_CHECKMULTISIG'); +const scriptA = Buffer.from("OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG"); +const scriptB = Buffer.from("OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG"); +const scriptC = Buffer.from("OP_2 OP_2 OP_CHECKMULTISIG"); mast.addLeaf(scriptA); mast.addLeaf(scriptB); mast.addLeaf(scriptC); @@ -417,7 +440,7 @@ const leafPath = mast.getLeafPath(leafToSpend); const controlBlock = Buffer.concat([ Buffer.from([0xc0]), // 脚本版本 internalPubkey, - ...leafPath + ...leafPath, ]); // 签名交易 @@ -427,12 +450,7 @@ const signatureAlice = bitcoin.script.signature.encode(keyPairAlice.sign(signatu const signatureBob = bitcoin.script.signature.encode(keyPairBob.sign(signatureHash), hashType); // 构建解锁脚本(witness script) -const witnessScript = [ - signatureAlice, - signatureBob, - scriptC, - controlBlock -]; +const witnessScript = [signatureAlice, signatureBob, scriptC, controlBlock]; // 设置 witness txb.setWitness(inputIndex, witnessScript); @@ -443,6 +461,7 @@ console.log('Transaction:', tx.toHex()); ``` **构建和签名交易:** + - 创建一个交易以花费 Taproot 输出。 - 添加输入和输出。 - 获取要花费的脚本(例如 scriptC)的 TapLeaf 哈希值和 Merkle 路径。 @@ -451,14 +470,16 @@ console.log('Transaction:', tx.toHex()); - 构建包含两个签名和其他信息的解锁脚本(witness script)。 - 设置交易的 witness。 - - #### script path + [905ecdf95a84804b192f4dc221cfed4d77959b81ed66013a7e41a6e61e7ed530](https://blockchain.info/rawtx/905ecdf95a84804b192f4dc221cfed4d77959b81ed66013a7e41a6e61e7ed530)是花费 P2TR (Script Path) 的例子(它是一个 2-of-2 多签脚本),它的 Witness 为 + ```js 044123b1d4ff27b16af4b0fcb9672df671701a1a7f5a6bb7352b051f461edbc614aa6068b3e5313a174f90f3d95dc4e06f69bebd9cf5a3098fde034b01e69e8e788901400fd4a0d3f36a1f1074cb15838a48f572dc18d412d0f0f0fc1eeda9fa4820c942abb77e4d1a3c2b99ccf4ad29d9189e6e04a017fe611748464449f681bc38cf394420febe583fa77e49089f89b78fa8c116710715d6e40cc5f5a075ef1681550dd3c4ad20d0fa46cb883e940ac3dc5421f05b03859972639f51ed2eccbf3dc5a62e2e1b15ac41c02e44c9e47eaeb4bb313adecd11012dfad435cd72ce71f525329f24d75c5b9432774e148e9209baf3f1656a46986d5f38ddf4e20912c6ac28f48d6bf747469fb1 ``` + 根据编码我们得知有4个元素 + ```js 0070: .. .. .. .. .. .. 04 .. .. .. .. .. .. .. .. .. vin0 Witness Count: 4 0070: .. .. .. .. .. .. .. 41 23 b1 d4 ff 27 b1 6a f4 vin0 Witness 0 Length:65 (0x41) @@ -484,21 +505,23 @@ console.log('Transaction:', tx.toHex()); 0180: b1 ``` - #### key path + [dbef583962e13e365a2069d451937a6de3c2a86149dc6a4ac0d84ab450509c91](https://blockchain.info/rawtx/dbef583962e13e365a2069d451937a6de3c2a86149dc6a4ac0d84ab450509c91)是花费 P2TR (Key Path) 的例子,它的 witness 为: + ```js 044123b1d4ff27b16af4b0fcb9672df671701a1a7f5a6bb7352b051f461edbc614aa6068b3e5313a174f90f3d95dc4e06f69bebd9cf5a3098fde034b01e69e8e788901400fd4a0d3f36a1f1074cb15838a48f572dc18d412d0f0f0fc1eeda9fa4820c942abb77e4d1a3c2b99ccf4ad29d9189e6e04a017fe611748464449f681bc38cf394420febe583fa77e49089f89b78fa8c116710715d6e40cc5f5a075ef1681550dd3c4ad20d0fa46cb883e940ac3dc5421f05b03859972639f51ed2eccbf3dc5a62e2e1b15ac41c02e44c9e47eaeb4bb313adecd11012dfad435cd72ce71f525329f24d75c5b9432774e148e9209baf3f1656a46986d5f38ddf4e20912c6ac28f48d6bf747469fb1 ``` + ```js 0141e6e1fe41524e65e3040bc3d080a136345c2c806eb7f336dd6a7a79e9054b0d1fc6a8d836667ef6e9f2188cd1270ab28e5e0eb642eac89f2ec50a32ca54aaf9d601 01 .. .. .. .. .. .. .. .. .. .. .. .. .. .. vin0 Witness Count: 1 .. 41 .. .. .. .. .. .. .. .. .. .. .. .. .. vin0 Witness 0 Length:65, schnorr_sig (64 bytes) + sig_hash (1 bytes) .. .. e6 e1 fe 41 52 4e 65 e3 04 0b c3 d0 80 schnorr_sig -a1 36 34 5c 2c 80 6e b7 f3 36 dd 6a 7a 79 e9 -05 4b 0d 1f c6 a8 d8 36 66 7e f6 e9 f2 18 8c -d1 27 0a b2 8e 5e 0e b6 42 ea c8 9f 2e c5 0a +a1 36 34 5c 2c 80 6e b7 f3 36 dd 6a 7a 79 e9 +05 4b 0d 1f c6 a8 d8 36 66 7e f6 e9 f2 18 8c +d1 27 0a b2 8e 5e 0e b6 42 ea c8 9f 2e c5 0a 32 ca 54 aa f9 d6 .. .. .. .. .. .. 01 sig_hash: SIGHASH_ALL (0x01) ``` diff --git a/BTC/Advanced/Taproot/README.md b/btc/Advanced/Taproot/README.md similarity index 100% rename from BTC/Advanced/Taproot/README.md rename to btc/Advanced/Taproot/README.md diff --git a/BTC/Application/ARC-20/README.md b/btc/Application/ARC-20/README.md similarity index 100% rename from BTC/Application/ARC-20/README.md rename to btc/Application/ARC-20/README.md diff --git a/BTC/Application/ARC-20/images/atomical.png b/btc/Application/ARC-20/images/atomical.png similarity index 100% rename from BTC/Application/ARC-20/images/atomical.png rename to btc/Application/ARC-20/images/atomical.png diff --git a/BTC/Application/ARC-20/images/transfer1.png b/btc/Application/ARC-20/images/transfer1.png similarity index 100% rename from BTC/Application/ARC-20/images/transfer1.png rename to btc/Application/ARC-20/images/transfer1.png diff --git a/BTC/Application/ARC-20/images/transfer2.png b/btc/Application/ARC-20/images/transfer2.png similarity index 100% rename from BTC/Application/ARC-20/images/transfer2.png rename to btc/Application/ARC-20/images/transfer2.png diff --git a/BTC/Application/BRC-20/README.md b/btc/Application/BRC-20/README.md similarity index 100% rename from BTC/Application/BRC-20/README.md rename to btc/Application/BRC-20/README.md diff --git a/BTC/Application/BRC-20/images/brc20-experiment.jpg b/btc/Application/BRC-20/images/brc20-experiment.jpg similarity index 100% rename from BTC/Application/BRC-20/images/brc20-experiment.jpg rename to btc/Application/BRC-20/images/brc20-experiment.jpg diff --git a/BTC/Application/BRC-20/images/dune.jpg b/btc/Application/BRC-20/images/dune.jpg similarity index 100% rename from BTC/Application/BRC-20/images/dune.jpg rename to btc/Application/BRC-20/images/dune.jpg diff --git a/BTC/Application/Runes/README.md b/btc/Application/Runes/README.md similarity index 100% rename from BTC/Application/Runes/README.md rename to btc/Application/Runes/README.md diff --git a/BTC/Application/Runes/ULEB128.md b/btc/Application/Runes/ULEB128.md similarity index 100% rename from BTC/Application/Runes/ULEB128.md rename to btc/Application/Runes/ULEB128.md diff --git a/BTC/Basic/explorer/README.md b/btc/Basic/explorer/README.md similarity index 100% rename from BTC/Basic/explorer/README.md rename to btc/Basic/explorer/README.md diff --git a/BTC/Basic/payment/P2PKH.md b/btc/Basic/payment/P2PKH.md similarity index 100% rename from BTC/Basic/payment/P2PKH.md rename to btc/Basic/payment/P2PKH.md diff --git a/BTC/Basic/payment/P2SH-P2PKH.md b/btc/Basic/payment/P2SH-P2PKH.md similarity index 100% rename from BTC/Basic/payment/P2SH-P2PKH.md rename to btc/Basic/payment/P2SH-P2PKH.md diff --git a/BTC/Basic/payment/P2TR.md b/btc/Basic/payment/P2TR.md similarity index 100% rename from BTC/Basic/payment/P2TR.md rename to btc/Basic/payment/P2TR.md diff --git a/BTC/Basic/payment/P2WPKH.md b/btc/Basic/payment/P2WPKH.md similarity index 100% rename from BTC/Basic/payment/P2WPKH.md rename to btc/Basic/payment/P2WPKH.md diff --git a/BTC/Basic/payment/README.md b/btc/Basic/payment/README.md similarity index 100% rename from BTC/Basic/payment/README.md rename to btc/Basic/payment/README.md diff --git a/BTC/Basic/signature/README.md b/btc/Basic/signature/README.md similarity index 100% rename from BTC/Basic/signature/README.md rename to btc/Basic/signature/README.md diff --git a/BTC/Basic/signature/ecdsa.js b/btc/Basic/signature/ecdsa.js similarity index 100% rename from BTC/Basic/signature/ecdsa.js rename to btc/Basic/signature/ecdsa.js diff --git a/BTC/Basic/signature/ecdsaPSBT.js b/btc/Basic/signature/ecdsaPSBT.js similarity index 100% rename from BTC/Basic/signature/ecdsaPSBT.js rename to btc/Basic/signature/ecdsaPSBT.js diff --git a/BTC/Basic/signature/ecdsaTransaction.js b/btc/Basic/signature/ecdsaTransaction.js similarity index 100% rename from BTC/Basic/signature/ecdsaTransaction.js rename to btc/Basic/signature/ecdsaTransaction.js diff --git a/BTC/Basic/signature/package.json b/btc/Basic/signature/package.json similarity index 100% rename from BTC/Basic/signature/package.json rename to btc/Basic/signature/package.json diff --git a/BTC/Basic/signature/schnorr.js b/btc/Basic/signature/schnorr.js similarity index 100% rename from BTC/Basic/signature/schnorr.js rename to btc/Basic/signature/schnorr.js diff --git a/BTC/Basic/tools/README.md b/btc/Basic/tools/README.md similarity index 100% rename from BTC/Basic/tools/README.md rename to btc/Basic/tools/README.md diff --git a/BTC/Basic/tools/base58.md b/btc/Basic/tools/base58.md similarity index 100% rename from BTC/Basic/tools/base58.md rename to btc/Basic/tools/base58.md diff --git a/BTC/Basic/tools/bech32.md b/btc/Basic/tools/bech32.md similarity index 99% rename from BTC/Basic/tools/bech32.md rename to btc/Basic/tools/bech32.md index 33147b60c..74d733b46 100644 --- a/BTC/Basic/tools/bech32.md +++ b/btc/Basic/tools/bech32.md @@ -63,6 +63,7 @@ console.log('Decoded Bech32:', decoded); 编码过程涉及以下步骤: 1. **确定人类可读部分(HRP)和数据部分:** + - HRP表示网络,比如比特币主网的`bc`,测试网的`tb`。 - 数据部分包含经过编码的公钥或脚本哈希。 @@ -103,6 +104,7 @@ console.log('Decoded Bech32:', decoded); ### PolyMod算法的作用 在Bech32中,`PolyMod`算法的主要作用是计算一个校验码,这个校验码能够: + - 检测到任何单一的错误。 - 检测到任何相邻字符的交换。 - 检测到最多四个错误的错误组合。 @@ -114,11 +116,13 @@ console.log('Decoded Bech32:', decoded); 1. **初始化**:设置一个初始变量`c`为1。 2. **处理HRP**:对于HRP(人类可读部分)中的每个字符,进行以下操作: + - 将`c`向右移动5位。 - 将字符的值与`c`进行异或(XOR)运算。 - 对`c`进行一系列的模2多项式运算,通常这涉及到与几个固定的常数进行XOR运算,这些常数由Bech32规范定义。 3. **处理数据部分**:类似地处理数据部分中的每个5位值,包括: + - 将`c`向右移动5位。 - 将数据值与`c`进行异或运算。 - 对`c`进行类似上述的模2多项式运算。 diff --git a/BTC/Basic/tools/bech32m.js b/btc/Basic/tools/bech32m.js similarity index 100% rename from BTC/Basic/tools/bech32m.js rename to btc/Basic/tools/bech32m.js diff --git a/BTC/Basic/tools/bech32m.md b/btc/Basic/tools/bech32m.md similarity index 100% rename from BTC/Basic/tools/bech32m.md rename to btc/Basic/tools/bech32m.md diff --git a/BTC/Basic/tools/dust.md b/btc/Basic/tools/dust.md similarity index 100% rename from BTC/Basic/tools/dust.md rename to btc/Basic/tools/dust.md diff --git a/BTC/Basic/tools/images/transaction.png b/btc/Basic/tools/images/transaction.png similarity index 100% rename from BTC/Basic/tools/images/transaction.png rename to btc/Basic/tools/images/transaction.png diff --git a/BTC/Basic/wallet/README.md b/btc/Basic/wallet/README.md similarity index 100% rename from BTC/Basic/wallet/README.md rename to btc/Basic/wallet/README.md diff --git a/BTC/Basic/wallet/address.js b/btc/Basic/wallet/address.js similarity index 100% rename from BTC/Basic/wallet/address.js rename to btc/Basic/wallet/address.js diff --git a/BTC/Basic/wallet/package.json b/btc/Basic/wallet/package.json similarity index 100% rename from BTC/Basic/wallet/package.json rename to btc/Basic/wallet/package.json diff --git a/btc/README-ES.md b/btc/README-ES.md new file mode 100644 index 000000000..4b64691d1 --- /dev/null +++ b/btc/README-ES.md @@ -0,0 +1,98 @@ +# Bitcoin (BTC) + +This repository aims to provide comprehensive information on Bitcoin (BTC) technology, covering its basic functions, advanced applications, and various protocols and tools in its ecosystem. Whether you are a beginner or an experienced developer, this guide will help you gain a deeper understanding and make efficient use of Bitcoin technology. + +## Introduction + +Bitcoin (BTC), the world's first decentralized cryptocurrency, was introduced by Satoshi Nakamoto in 2009 and has had a profound impact on the global financial system. Its main advantages include: + +- **Financial Democratization**: Bitcoin enables peer-to-peer transactions without intermediaries through decentralized blockchain technology, enhancing individual financial control and freedom. +- **Security and Transparency**: The network employs cryptographic techniques to ensure secure, tamper-proof transactions, with all records being publicly transparent and verifiable by anyone. +- **Limited Supply**: With a capped total supply of 21 million coins, Bitcoin's scarcity makes it an inflation-resistant store of value, often called "digital gold." +- **Global Liquidity**: As a borderless currency, Bitcoin facilitates international transactions and payments. +- **Technological Innovation**: Bitcoin has pioneered blockchain technology, driving the development of smart contracts, decentralized finance (DeFi), and new tech applications. + +This knowledge repository will guide you through Bitcoin's basic functionalities, advanced features, and ecosystem applications, helping you stay at the forefront of this rapidly evolving field. + +## Task Status Legend + +⬜ Not Started ⌛ In Progress ✅ Completed + +## 1. Basic Features + +### 1.1 Payment Addresses + +Types of Bitcoin addresses and their encoding: + +- **P2PKH** (Pay to Public Key Hash) ✅ +- **P2SH-P2PKH** (Pay to Script Hash - Pay to Public Key Hash) ✅ +- **P2WPKH** (Pay to Witness Public Key Hash) ✅ +- **P2TR** (Pay to Taproot) ✅ +- **Base58 Encoding** ✅ +- **Bech32 Encoding** ✅ +- **Bech32m Encoding** ✅ + +### 1.2 Wallets + +Types of Bitcoin wallets and their characteristics: + +- **Standard Wallets** ⌛ +- **HD Wallets (Hierarchical Deterministic Wallets)** ✅ + +### 1.3 Transactions + +Key knowledge about Bitcoin transactions: + +- **UTXO Analysis** ⬜ +- **Coinbase Transactions** ⬜ +- **Transaction Construction** ⬜ +- **Fee Estimation** ⬜ +- **Signature Algorithms** ✅ +- **Transaction Acceleration** ⬜ + +### 1.4 Tools + +Common tools in the Bitcoin ecosystem: + +- **Blockchain Explorers** ⌛ +- **Network Hash Rate Tools** ⬜ +- **API Services** ⬜ +- **Data Analysis Tools** ⬜ + +## 2. Advanced Features + +### 2.1 PSBT (Partially Signed Bitcoin Transaction) + +Introduction and usage of PSBT: + +- **PSBT Protocol Introduction** ✅ +- **Creating and Parsing PSBT** ✅ +- **PSBT V2** ⬜ + +### 2.2 Taproot + +Introduction to Taproot and its benefits: + +- **Schnorr Signatures** ✅ +- **MAST** (Merkelized Abstract Syntax Tree) ✅ +- **Privacy and Efficiency Improvements** ⬜ +- **Key Path vs. Script Path Differences** ⬜ + +### 2.3 Multisig Addresses + +Usage and advantages of multisig addresses: + +- **Multisig Address Introduction** ✅ +- **OP_CHECKMULTISIG** ✅ +- **OP_CHECKMULTISIGVERIFY** ✅ +- **OP_CHECKSIGADD** ✅ + +## 3. Ecosystem Applications + +- **BRC20 Protocol Overview** ✅ +- **ARC20 Protocol Overview** ✅ +- **Runes Protocol Overview** ✅ +- **Lightning Network Introduction** ⌛ +- **Core Logic of the Lightning Network** ⌛ +- **OP_CAT** ✅ +- **Babylon | BTC Staking** ⬜ diff --git a/btc/README.md b/btc/README.md new file mode 100644 index 000000000..99b7af26e --- /dev/null +++ b/btc/README.md @@ -0,0 +1,83 @@ +# 比特币 (BTC) + +本知识库旨在提供有关比特币(BTC)技术的全面信息,涵盖基本功能、进阶应用以及生态系统中的各类协议和工具。无论您是初学者还是有经验的开发者,本指南都将帮助您更好地理解和使用比特币技术。 + +## 简介 + +比特币(BTC)是全球首个去中心化的加密货币,由中本聪(Satoshi Nakamoto)于 2009 年发布,并对全球金融体系产生了深远的影响。其关键意义包括以下几点: + +- **金融民主化**:比特币利用去中心化的区块链技术,实现了无需中介的点对点交易,赋予了个人更多的金融控制权和自由。 +- **安全与透明**:比特币网络利用密码学技术确保交易的安全性和不可篡改性,所有交易记录公开透明,任何人都可以验证。 +- **有限供应**:比特币总量被限制在 2100 万枚,这种稀缺性使其成为抗通胀的资产储备,被誉为“数字黄金”。 +- **全球流动性**:比特币作为一种全球性货币,可以跨国界进行交易和转账,推动了国际贸易和跨境支付的发展。 +- **技术创新**:比特币引领了区块链技术的发展,推动了智能合约和去中心化金融(DeFi)等创新应用,开启了新的互联网时代。 + +通过本知识库,您将学习比特币的核心功能、进阶特性以及生态系统应用,助您在这一快速发展的领域中保持领先。 + +## 任务状态图例: +⬜ 未开始 ⌛ 进行中 ✅ 已完成 + +## 1. 基本功能 + +### 1.1 支付地址 +比特币地址类型及编码方式: +- **P2PKH** (支付到公钥哈希) ✅ +- **P2SH-P2PKH** (支付到脚本哈希 - 支付到公钥哈希) ✅ +- **P2WPKH** (支付到见证公钥哈希) ✅ +- **P2TR** (支付到 Taproot) ✅ +- **Base58 编码** ✅ +- **Bech32 编码** ✅ +- **Bech32m 编码** ✅ + +### 1.2 钱包 +比特币钱包类型及其特性: +- **标准钱包** ⌛ +- **分层确定性钱包(HD 钱包)** ✅ + +### 1.3 交易 +比特币交易的相关知识: +- **UTXO解析** ⬜ +- **Coinbase交易** ⬜ +- **交易构建** ⬜ +- **费用估算** ⬜ +- **签名算法** ✅ +- **交易加速** ⬜ + +### 1.4 工具 +比特币生态系统中的常用工具: +- **区块链浏览器** ⌛ +- **全网算力** ⬜ +- **API 服务** ⬜ +- **数据分析工具** ⬜ + +## 2. 进阶功能 + +### 2.1 PSBT (部分签名比特币交易) +PSBT 的介绍和使用: +- **PSBT 协议简介** ✅ +- **创建和解析 PSBT** ✅ +- **PSBT V2** ⬜ + +### 2.2 Taproot +Taproot 技术的介绍及其优势: +- **施诺尔签名** ✅ +- **MAST** (Merkelized 抽象语法树) ✅ +- **隐私和效率改进** ⬜ +- **Key Path和Script Path的区别** ⬜ + +### 2.3 多重签名地址 +多重签名地址的使用及优势: +- **多重签名简介** ✅ +- **OP_CHECKMULTISIG** ✅ +- **OP_CHECKMULTISIGVERIFY** ✅ +- **OP_CHECKSIGADD** ✅ + +## 3. 生态系统应用 +- **BRC20 协议介绍** ✅ +- **ARC20 协议介绍** ✅ +- **Runes 协议介绍** ✅ +- **闪电网络简介** ⌛ +- **闪电网络核心逻辑** ⌛ +- **OP_CAT** ✅ +- **Babylon | BTC Staking** ⬜ +