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

相关推荐
The_Ticker18 小时前
日股实时行情接口使用指南
java·经验分享·笔记·python·算法·区块链
三秋树18 小时前
从 Solv Protocol 273 万美元被黑事件,深入理解 Solidity 自重入攻击 —— ERC-3525 标准中的暗坑
区块链
MicroTech202518 小时前
微算法科技(NASDAQ: MLGO)探索量子隐形传态区块链隐私保护签名技术,增强Web 3.0元宇宙环境的效率、安全性和真实性
科技·区块链
草原猫18 小时前
公链开发:从技术筑基到生态共生,重构数字信任基础设施
重构·区块链
小白的代码日记18 小时前
区块链分叉检测与回扫系统(Go语言)
人工智能·golang·区块链
Blockchina19 小时前
Web3项目开发全流程详解:从0到1搭建DApp架构(实战版)
架构·web3·区块链·perp dex
Blockchina2 天前
Web3金融革命:PerpDEX的深度解析
金融·web3·区块链·perp dex·永续去中心化交易所
Joy T2 天前
【Web3】Solidity收款合约初探与去中心化预言机(Chainlink)机制解析
web3·去中心化·区块链·预言机·chainlink·don·datafeed
fuzamei8882 天前
【市场观察】黄金、白银等金属回归“真实资产”,区块链价值的时代也在路上
区块链
CryptoPP2 天前
开发者指南:构建实时期货黄金数据监控系统
大数据·数据结构·笔记·金融·区块链