《区块链编程》第六章

《区块链编程》第六章

Script

Script 是比特币使用的智能合约语言、但我觉得Script和Solidity代表的智能合约语言是不一样的。
Script本质上是允许人们证明他们有权使用某些UTXO。
Script使用椭圆曲线密码学(椭圆曲线数字签名算法)来验证交易是否有权使用对应的比特币
Script不是图灵完备的。是一种类似Forth的基于栈的语言。

练习1

p102

代码实现

# -*- coding: utf-8 -*-
# @Author: 从化北(喵星人)
# @Date:   2022-01-08 17:10:49
# @Last Modified by:   从化北
# @Last Modified time: 2022-01-08 17:14:15
from helper import hash160

def op_hash160(stack):
    if len(stack) < 1:
        return False
    element = stack.pop()
    h160 = hash160(element)
    stack.append(h160)
    return True

测试

练习2

p113

代码实现


def op_checksig(stack, z):
    # check that there are at least 2 elements on the stack
    if len(stack) < 2:
        return False
    # the top element of the stack is the SEC pubkey
    sec_pubkey = stack.pop()
    # the next element of the stack is the DER signature
    # take off the last byte of the signature as that's the hash_type
    der_signature = stack.pop()[:-1]
    try:
        # parse the serialized pubkey and signature into objects
        point = S256Point.parse(sec_pubkey)
        sig = Signature.parse(der_signature)
    except(ValueError, SyntaxError) as e:
        return False
    # verify the signature using S256Point.verify()
    # push an encoded 1 or 0 depending on whether the signature verified
    if point.verify(z, sig):
        stack.append(encode_num(1))
    else:
        stack.append(encode_num(0))
    return True

测试

练习3

p120

一个可用的签名脚本

 OP_2

测试(手测)

dup dup mul add 6 equal
2(OP_2)
22
222
24
6
66
1

练习4

p121

该签名脚本目的

该比特币签名脚本的目的是悬赏	SHA-1碰撞,能解锁该脚本的唯一方法是给出不同的a和b。


具体解释

2dup equal not verify sha1 swap sha1 equal 
a,b,(a为栈顶); 复制栈顶的两项a与b,判断它俩是否相等,不等经过not返回1,
verify消耗栈顶元素判断为True,对a进行sha1运算,交换b到栈顶。对b进行sha1运算,
equal判断a与b两者的sha1值相等,返回True

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇

)">
下一篇>>