当前位置:   article > 正文

BSV上的委托合约(3)_bsvoq

bsvoq

之前,我们介绍了两种委托使用比特币的方法:一种在脚本级别,另一种在交易级别

我们将前者泛化为委托给任何脚本/智能合约,而不仅仅是公钥。它允许一个人授权任意智能合约来花费一个人的比特币。

在这里插入图片描述

完整代码如下所示:

contract DelegateToScript {
    PubKey owner;

    public function delegate(Sig ownerSig, bytes delegatedScript, 
            PubKey derivedPubKey, PubKey X, int lambda, SigHashPreimage txPreimage) {
        require(Tx.checkPreimage(txPreimage));
        
        // ensure delegated script is signed by the owner
        require(Oracle.verifyData(delegatedScript, ownerSig, this.owner, derivedPubKey, X, lambda, txPreimage));

        // use delegated script as the new locking script, while maintaining value
        bytes output = Utils.buildOutput(delegatedScript, SigHash.value(txPreimage));
        require(hash256(output) == SigHash.hashOutputs(txPreimage));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
DelegateToScript 合约源代码

9 行检查委托脚本是否已签名并因此由所有者授权。第 12 行和第 13 行使用 OP_PUSH_TX 确保委托脚本作为新锁定脚本进入支出交易的输出,类似于 Pay to Script Hash (P2SH) 的模拟。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/159636
推荐阅读
相关标签
  

闽ICP备14008679号