scryptTS 新版本发布

scryptTS新版本发布,主要带来两个新特性。 您需要使用以下版本来体验:

json 复制代码
"dependencies": {
    "scrypt-ts": "=0.1.5-beta.2"
},

1. scryptTS 中隐藏了交易原像 OP_PUSH_TX 技术

使用 OP_PUSH_TX 可以让合约代码访问整个 transaction 数据,包括所有的 inputs 和 outputs。

从而使得我们能够针对这些数据设置任何约束条件。这为在BSV 网络上运行各种智能合约开辟了无限可能。

OP_PUSH_TX 要求在外部计算交易原像,并通过合约的公共函数参数传入。

ts 复制代码
export class Counter extends SmartContract {
  @prop(true)
  count: bigint;

  constructor(count: bigint) {
    super(count);
    this.count = count;
  }

  @method()
  public increment(txPreimage: SigHashPreimage) {
    this.count++;
    assert(this.updateState(txPreimage, SigHash.value(txPreimage)));
  }
}

交易原像的计算涉及交易的构建,计算原像使用的签名格式。用户在计算的过程中容易出错。

scryptTS 封装了交易原像的计算。用户无需显式的计算和传入交易原像。可以通过 ScriptContext 来访问整个交易的数据。

ts 复制代码
export type ScriptContext = {
    nVersion: ByteString;
    utxo: UTXO;
    hashPrevouts: ByteString;
    hashSequence: ByteString;
    nSequence: bigint;
    hashOutputs: ByteString;
    nLocktime: bigint;
    sigHashType: SigHashType;
};

ScriptContext 结构体与交易原像 txPreimage 的对应关系:

ScriptContext 交易原像 txPreimage
nVersion 交易版本号
utxo.value 此输入花费的输出值
utxo.scriptCode 输入的 scriptCode(在 CTxOuts 中序列化为脚本)
utxo.outpoint.txid 输入所在交易的交易 Id
utxo.outpoint.outputIndex 输入所在交易的输出索引
hashPrevouts hashPrevouts 是所有输入outpoints序列化的双SHA256
hashSequence hashSequence 是所有输入的nSequence序列化的双SHA256
nSequence 输入的 nSequence
hashOutputs hashOutputs 是用scriptPubKey序列化所有输出量(8字节小端)的双SHA256(在CTxOuts中序列化为脚本)
nLocktime 交易的nLocktime
sigHashType 签名的 sighash 类型

你可以直接在 合约的公共函数(不支持在非公共函数中访问)中通过 this.ctx 来访问交易原像的相关数据。

ts 复制代码
export class Counter extends SmartContract {
  @prop(true)
  count: bigint;

  constructor(count: bigint) {
    super(count);
    this.count = count;
  }

  @method()
  public increment() {
    this.count++;
    assert(this.ctx.hashOutputs == hash256(this.buildStateOutput(this.ctx.utxo.value)));
  }
}

调用合约时也无需传入 交易原像 txPreimage:

ts 复制代码
getCallTx(utxos: UTXO[], prevTx: bsv.Transaction, nextInst: Counter): bsv.Transaction {
const inputIndex = 1;
return new bsv.Transaction().from(utxos)
    .addInputFromPrevTx(prevTx)
    .setOutput(0, (tx: bsv.Transaction) => {
    nextInst.lockTo = { tx, outputIndex: 0 };
    return new bsv.Transaction.Output({
        script: nextInst.lockingScript,
        satoshis: this.balance,
    })
    })
    .setInputScript(inputIndex, (tx: bsv.Transaction) => {
    this.unlockFrom = { tx, inputIndex };
    return this.getUnlockingScript(self => {
        self.increment();
    })
    });
}

2. 支持在 vscode 编辑器中显示转译错误

只需将此文件 task.json 放在当前项目的 .vscode 目录中。 然后运行Shift+Command+B(Windows:Ctrl+Shift+B,Linux:Ctrl+Shift+B),可以看到错误输出:

相关推荐
Rockbean7 小时前
10分钟-AI × Solana:2.MCP服务器——AI代理接入Solana的标准化通道
rust·web3·智能合约
Black_mario1 天前
Berachain PoL Next 主网上线,重塑 L1 价值捕获的底层逻辑
区块链
双缝观察者1 天前
多层级资金流转架构中异常行为的链上识别与溯源方法
python·区块链
梦帮科技1 天前
Rust+Tauri+EVM:构建下一代内存安全级数字版权(DRM)主权音频网络与跨链金融系统的技术实践与全景架构
网络·安全·架构·rust·区块链·音视频·web3.py
财迅通Ai2 天前
南华期货半年净利预期高增,稀缺自主清算壁垒驱动跨境业务规模放量
大数据·人工智能·区块链·南华期货
葫三生2 天前
《论三生原理》论证:大衍筮法是一种离散生成算法?
人工智能·算法·机器学习·计算机视觉·区块链·量子计算
北冥you鱼2 天前
区块链智能合约升级中的数据存储兼容性挑战与解决方案
区块链·智能合约
双缝观察者3 天前
虚假承兑商链上资产流向追踪与线下身份锁定技术
区块链
木西3 天前
破局科研“死亡之谷”:基于 OpenZeppelin V5 与 Viem 深度复刻 VitaDAO 核心治理与资助系统
web3·智能合约·solidity
Rockbean3 天前
10分钟Solana-性能web3-5.4 项目搭建核心环节
rust·web3·智能合约