P2PK、P2PKH、P2SH、P2WPKH、P2WSH解析

直接贴代码吧:

// payToPubKeyHashScript creates a new script to pay a transaction
// output to a 20-byte pubkey hash. It is expected that the input is a valid
// hash.
func payToPubKeyHashScript(pubKeyHash []byte) ([]byte, error) {return NewScriptBuilder().AddOp(OP_DUP).AddOp(OP_HASH160).AddData(pubKeyHash).AddOp(OP_EQUALVERIFY).AddOp(OP_CHECKSIG).Script()
}// payToWitnessPubKeyHashScript creates a new script to pay to a version 0
// pubkey hash witness program. The passed hash is expected to be valid.
func payToWitnessPubKeyHashScript(pubKeyHash []byte) ([]byte, error) {return NewScriptBuilder().AddOp(OP_0).AddData(pubKeyHash).Script()
}// payToScriptHashScript creates a new script to pay a transaction output to a
// script hash. It is expected that the input is a valid hash.
func payToScriptHashScript(scriptHash []byte) ([]byte, error) {return NewScriptBuilder().AddOp(OP_HASH160).AddData(scriptHash).AddOp(OP_EQUAL).Script()
}// payToWitnessPubKeyHashScript creates a new script to pay to a version 0
// script hash witness program. The passed hash is expected to be valid.
func payToWitnessScriptHashScript(scriptHash []byte) ([]byte, error) {return NewScriptBuilder().AddOp(OP_0).AddData(scriptHash).Script()
}// payToPubkeyScript creates a new script to pay a transaction output to a
// public key. It is expected that the input is a valid pubkey.
func payToPubKeyScript(serializedPubKey []byte) ([]byte, error) {return NewScriptBuilder().AddData(serializedPubKey).AddOp(OP_CHECKSIG).Script()
}

Pubkey scripts are created by spenders who have little interest what that script does. Receivers do care about the script conditions and, if they want, they can ask spenders to use a particular pubkey script. Unfortunately, custom pubkey scripts are less convenient than short Bitcoin addresses and there was no standard way to communicate them between programs prior to widespread implementation of the BIP70 Payment Protocol discussed later.

To solve these problems, pay-to-script-hash (P2SH) transactions were created in 2012 to let a spender create a pubkey script containing a hash of a second script, the redeem script.

The basic P2SH workflow, illustrated below, looks almost identical to the P2PKH workflow. Bob creates a redeem script with whatever script he wants, hashes the redeem script, and provides the redeem script hash to Alice. Alice creates a P2SH-style output containing Bob’s redeem script hash.

When Bob wants to spend the output, he provides his signature along with the full (serialized) redeem script in the signature script. The peer-to-peer network ensures the full redeem script hashes to the same value as the script hash Alice put in her output; it then processes the redeem script exactly as it would if it were the primary pubkey script, letting Bob spend the output if the redeem script does not return false.

The hash of the redeem script has the same properties as a pubkey hash—so it can be transformed into the standard Bitcoin address format with only one small change to differentiate it from a standard address. This makes collecting a P2SH-style address as simple as collecting a P2PKH-style address. The hash also obfuscates any public keys in the redeem script, so P2SH scripts are as secure as P2PKH pubkey hashes.

As of Bitcoin Core 0.9, the standard pubkey script types are:

P2PKH is the most common form of pubkey script used to send a transaction to one or multiple Bitcoin addresses.

Pubkey script: OP_DUP OP_HASH160 <PubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
Signature script: <sig> <pubkey>

Pay To Script Hash (P2SH)

Pubkey script: OP_HASH160 <Hash160(redeemScript)> OP_EQUAL
Signature script: <sig> [sig] [sig...] <redeemScript>

Multisig

In  multisig   pubkey scripts , called  m-of-n ,  m  is the  minimum  number of  signatures  which must match a  public key ;  n  is the  number  of  public keys  being provided. Both  m  and  n  should be  opcodes   OP_1  through  OP_16 , corresponding to the number desired.

Because of an off-by-one error in the original Bitcoin implementation which must be preserved for compatibility, OP_CHECKMULTISIG consumes one more value from the stack than indicated by m, so the list of secp256k1 signatures in the signature script must be prefaced with an extra value (OP_0) which will be consumed but not used.

The signature script must provide signatures in the same order as the corresponding public keys appear in the pubkey script or redeem script. See the description in OP_CHECKMULTISIG for details.

Pubkey script: <m> <A pubkey> [B pubkey] [C pubkey...] <n> OP_CHECKMULTISIG
Signature script: OP_0 <A sig> [B sig] [C sig...]

Although it’s not a separate transaction type, this is a P2SH multisig with 2-of-3:

Pubkey script: OP_HASH160 <Hash160(redeemScript)> OP_EQUAL
Redeem script: <OP_2> <A pubkey> <B pubkey> <C pubkey> <OP_3> OP_CHECKMULTISIG
Signature script: OP_0 <A sig> <C sig> <redeemScript>

Pubkey

Pubkey script: <pubkey> OP_CHECKSIG
Signature script: <sig>

Null Data

Pubkey Script: OP_RETURN <0 to 40 bytes of data>
(Null data scripts cannot be spent, so there's no signature script.)

Non-Standard Transactions

If you create a  redeem script , hash it, and use the hash in a  P2SH output , the  network  sees only the hash, so it will accept the  output  as valid no matter what the  redeem script  says. This allows payment to non-standard scripts, and as of Bitcoin Core 0.11, almost all valid  redeem scripts  can be spent. The exception is scripts that use unassigned  NOP opcodes ; these  opcodes  are reserved for future  soft forks  and can only be relayed or mined by  nodes  that don’t follow the standard mempool policy

As of Bitcoin Core 0.9.3, standard transactions must also meet the following conditions:

  • The transaction must be finalized: either its locktime must be in the past (or less than or equal to the current block height), or all of its sequence numbers must be 0xffffffff.

  • The transaction must be smaller than 100,000 bytes. That’s around 200 times larger than a typical single-input, single-output P2PKH transaction.

  • Each of the transaction’s signature scripts must be smaller than 1,650 bytes. That’s large enough to allow 15-of-15 multisig transactions in P2SH using compressed public keys.

  • Bare (non-P2SH) multisig transactions which require more than 3 public keys are currently non-standard.

  • The transaction’s signature script must only push data to the script evaluation stack. It cannot push new opcodes, with the exception of opcodes which solely push data to the stack.

  • The transaction must not include any outputs which receive fewer than 1/3 as many satoshis as it would take to spend it in a typical input. That’s currently 546 satoshis for a P2PKH or P2SH output on a Bitcoin Core node with the default relay fee. Exception: standard null data outputs must receive zero satoshis.

Null Data

  • The transaction must be smaller than 100,000 bytes. That’s around 200 times larger than a typical single-input, single-output P2PKH transaction.

  • Each of the transaction’s signature scripts must be smaller than 1,650 bytes. That’s large enough to allow 15-of-15 multisig transactions in P2SH using compressed public keys.

  • Bare (non-P2SH) multisig transactions which require more than 3 public keys are currently non-standard.

  • The transaction’s signature script must only push data to the script evaluation stack. It cannot push new opcodes, with the exception of opcodes which solely push data to the stack.

  • The transaction must not include any outputs which receive fewer than 1/3 as many satoshis as it would take to spend it in a typical input. That’s currently 546 satoshis for a P2PKH or P2SH output on a Bitcoin Core node with the default relay fee. Exception: standard null data outputs must receive zero satoshis.

The various options for what to sign are called signature hash types. There are three base SIGHASH types currently available:

  • SIGHASH_ALL, the default, signs all the inputs and outputs, protecting everything except the signature scripts against modification.

  • SIGHASH_NONE signs all of the inputs but none of the outputs, allowing anyone to change where the satoshis are going unless other signatures using other signature hash flags protect the outputs.

  • SIGHASH_SINGLE the only output signed is the one corresponding to this input (the output with the same output index number as this input), ensuring nobody can change your part of the transaction but allowing other signers to change their part of the transaction. The corresponding output must exist or the value “1” will be signed, breaking the security scheme. This input, as well as other inputs, are included in the signature. The sequence numbers of other inputs are not included in the signature, and can be updated.

The base types can be modified with the SIGHASH_ANYONECANPAY (anyone can pay) flag, creating three new combined types:

  • SIGHASH_ALL|SIGHASH_ANYONECANPAY signs all of the outputs but only this one input, and it also allows anyone to add or remove other inputs, so anyone can contribute additional satoshis but they cannot change how many satoshis are sent nor where they go.

  • SIGHASH_NONE|SIGHASH_ANYONECANPAY signs only this one input and allows anyone to add or remove other inputs or outputs, so anyone who gets a copy of this input can spend it however they’d like.

  • SIGHASH_SINGLE|SIGHASH_ANYONECANPAY signs this one input and its corresponding output. Allows anyone to add or remove other inputs.

Null Data

  • The transaction must be smaller than 100,000 bytes. That’s around 200 times larger than a typical single-input, single-output P2PKH transaction.

  • Each of the transaction’s signature scripts must be smaller than 1,650 bytes. That’s large enough to allow 15-of-15 multisig transactions in P2SH using compressed public keys.

  • Bare (non-P2SH) multisig transactions which require more than 3 public keys are currently non-standard.

  • The transaction’s signature script must only push data to the script evaluation stack. It cannot push new opcodes, with the exception of opcodes which solely push data to the stack.

  • The transaction must not include any outputs which receive fewer than 1/3 as many satoshis as it would take to spend it in a typical input. That’s currently 546 satoshis for a P2PKH or P2SH output on a Bitcoin Core node with the default relay fee. Exception: standard null data outputs must receive zero satoshis.

P2PK、P2PKH、P2SH、P2WPKH、P2WSH解析相关推荐

  1. 一文读懂P2SH和P2WSH

    P2SH和P2WSH是比特币交易的高级脚本,能够构建复杂条件的智能合约交易. 首先,我们将看看多重签名脚本.接下来,我们介绍最常见的交易脚本P2SH,即Pay-to-Script-Hash支付给脚本哈 ...

  2. Bitcoin+STARK: ZeroSync Khepri

    1. 引言 当前,以下项目借助STARK,为Bitcoin引入类似Mina的功能: https://github.com/lucidLuckylee/zerosync:Pure Cairo based ...

  3. BTC交易标准分类(对比说明)

    字段 P2PK P2PKH MN P2SH P2WPKH P2WSH P2SH-P2WPKH P2SH-P2WSH OP_RETURN scriptPubKey [PK] OP_CHECKSIG OP ...

  4. gossip 区块链_比特币奇葩8问:为何区块620826比区块620825早1秒诞生?

    写在前面: 关于比特币,我们有时会遇到一些难以理解的技术问题,例如"新区块比旧区块早1秒诞生"."同一时间不同全节点的大小不同"等奇葩现象,对于这些问题,就需要 ...

  5. 尹成学院区块链 Go 学习大纲-取得大纲试看视频联系微信yinchengak48

    网址:http://www.1cxy.net GO技术交流QQ群:254416566 所处阶段 主讲内容 技术要点 学习目标 第一阶段Go语言开发入门实战 1.Go语言介绍及开发环境搭建 1.Go语言 ...

  6. 是什么限制了区块链技术的应用?

    链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载. 2017年已经匆匆离去,回顾过去一整年,似乎区块链应用一直处于隐忍未发的状态,很多项目的落地已处于验证阶段,万众期待的爆 ...

  7. bitcoin全节点部署及bitcoind bitcoin-cli命令使用解释

    服务器配置: 操作系统: ubuntu 16.04 CPU: 4U 内存: 16G #一般来说4G就够,但如果要查历史记录,需要加载完整的交易索引表-tindex,这导致需要8G+的内存 硬盘: 50 ...

  8. 区块链学习笔记——一些交易脚本(P2PK、P2PKH、P2MS、P2SH)及作业回顾

    写在前面 这篇博客主要总结一下之前做过的区块链作业中的一些有趣的东西. 实验环境搭建 按照老师的要求以及助教给的一些问题的解决方案.把python从3.8装回3.7再装回3.6,好像都没什么用.在py ...

  9. 区块链重要基础知识7-1——标准脚本P2PKH、P2SH

    这里主要用于补充区块链重要基础知识7--比特币的运行机制中标准协议P2SH的说明 五大标准脚本分别为P2PKH.P2PK.MS(限15个密钥).P2SH和OP_Return. 这里主要介绍P2PKH. ...

最新文章

  1. 妈呀!GitChat 发布 1.9 版本更新公告,这个红色简直亮瞎了我的眼...
  2. 6.10 docker(三) 终止
  3. JAVA继承心得体会及建议_Java继承总结
  4. mysql的date函数可以干啥,MySql的Date函数
  5. 【Python基础】学习Python 一定要吃透这 5 个内置函数
  6. 洛谷 P3184 [USACO16DEC]Counting Haybales数草垛
  7. (BFS)Meteor Shower (poj3669)
  8. python爬取网页文本_手把手教你如何用Python爬取网站文本信息
  9. 计算机操作员有关大学专业,如何根据自身特长选大学专业,这样操作最简单直接!...
  10. linux命令unzip,linux unzip命令参数及用法详解--linux解压zip文件命令
  11. PS选中部分区域调整透明度
  12. python | 画中国站点数量空间分布图
  13. 阿里云思维导图系列(一)开篇
  14. C++ P1091 合唱队形[DP]
  15. unraid系统安装ikuai和openwrt虚拟机
  16. 代理服务器proxy server
  17. cesium 3d建筑物光效 泛光实体
  18. 登录邮箱怎么登录?163手机邮箱登录入口在这里
  19. 2020哈工程上岸初复试经验
  20. php实现图片液化,图像变形算法:实现Photoshop液化工具箱中向前变形工具

热门文章

  1. 香港大学计算机系介绍,香港中文大学计算机专业介绍
  2. [UVa 1610] 聚会游戏(Party Games)
  3. 【C语言】printf格式化输出及修饰符总结
  4. fancybox ajax参数,jQuery Fancybox插件使用参数详解
  5. 魔兽世界是用什么游戏引擎_战略游戏必须向1994年的《魔兽世界》学习的一件事...
  6. Linux 命令:e2fsck; superblock
  7. 开源一个自己用go语言加mangos-go消息中间件制作的文件同步工具
  8. 五星元老大飞哥,教半年Java实习生小飞飞:优雅解决历史代码中的新需求
  9. Windows自带计算器快捷键
  10. php 继承 可见性,php – 私有方法覆盖和可见性