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),可以看到错误输出:

相关推荐
寒水馨9 小时前
Linux下载、安装PowerShell-v7.6.4(附安装包powershell-7.6.4-linux-x64.tar.gz)
linux·跨平台·自动化运维·devops·命令行·powershell·脚本语言
智塑未来1 天前
证券公司智能运维平台哪个品牌做得好?擎创科技智能运维 2.0 适配券商全场景
运维·科技·区块链
Web3李李1 天前
自建公链:Web3生态长期可持续发展的核心底层布局
web3·区块链·软件开发·定制开发·公链开发·主链开发·主网开发
Web3_Daisy2 天前
如何在Robinhood Chain创建 Uniswap V3 流动性池
大数据·人工智能·web3·区块链
同花顺期货通2 天前
期货交割月持仓规则全解析:2026年个人交易者合规指南与展期策略
区块链
潜龙95272 天前
Web3.0新测试方案
web3·区块链·新测试方案
想你依然心痛3 天前
HarmonyOS 5.0智慧农业开发实战:构建分布式农业物联网与区块链农产品溯源系统
人工智能·分布式·物联网·区块链·智慧农业·harmonyos·开发实战
2601_962860153 天前
零数科技区块链平台服务六大领域,打造多个国家级示范项目
科技·区块链
The_Ticker3 天前
大宗商品行情API接入教程
开发语言·python·websocket·程序人生·区块链
IvorySQL3 天前
PG 日报|PG20 正式计划移除 refint 模块,官方指引迁移原生外键
数据库·人工智能·postgresql·开源·区块链