P2PK、P2PKH、P2SH、P2WPKH、P2WSH解析

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.


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ECC椭圆曲线在比特币中被用于加密和签名交易。以下是计算出比特币公钥的过程: 1. 选择椭圆曲线和基点 比特币使用的是secp256k1椭圆曲线,其基点为G。这个基点是一个预设的点,固定在曲线上。基点的坐标是一个大整数,用十六进制表示为: ``` G = 04 79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798 483ADA77 26A3C465 5DA4FBFC 0E1108A8 FD17B448 A6855419 9C47D08F FB10D4B8 ``` 2. 选择私钥 私钥是一个随机数,通常是由计算机生成。私钥通常由256位二进制数表示,可以用十六进制表示。比特币私钥的范围是1到约2^256-1。 3. 计算公钥 公钥是由私钥通过椭圆曲线算法计算出来的。计算公钥的过程是: - 将私钥表示为一个大整数d - 通过基点G和私钥d计算出公钥Q 计算公式为: ``` Q = d * G ``` 这个公式表示将基点G与一个标量d相乘,得出的结果就是公钥Q。在比特币中,公钥通常用压缩格式表示,即只保留x坐标和一个标记位,表示y坐标的符号。 4. 生成比特币地址 比特币地址是由公钥生成的。比特币地址通常以1或3开头,表示这是一个Pay-to-PubkeyHash(P2PKH)或Pay-to-ScriptHash(P2SH)地址。生成地址的过程是: - 对公钥进行哈希处理,生成一个哈希值 - 将哈希值进行Base58Check编码,得到比特币地址 注意:在比特币中,P2PKH地址是最常用的地址类型。P2SH地址通常用于多重签名和其他高级功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值