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

相关推荐
金融数据出海7 小时前
实时性、数据覆盖范围和易用性的优质金融数据源API推荐
后端·金融·区块链·ai编程
金融街小单纯10 小时前
随机刺激训练:解锁跨学科洞察力的科学密码
算法·重构·区块链
本郡主是喵19 小时前
基于区块链的电子投票系统的设计与实现(源码+文档)
区块链·毕业设计·solidity
leijiwen1 天前
AI × RWA 本地生活品牌数字资产管理与增长平台
人工智能·web3·区块链
brave_zhao1 天前
业务知识:强制平仓
区块链
我菜我有理1 天前
使用python爬取BSC链上交易(玩具版)
区块链
taxunjishu2 天前
DeviceNet 转 Modbus TCP 协议转换在 S7-1200 PLC化工反应釜中的应用
运维·人工智能·物联网·自动化·区块链
酷柚易汛智推官2 天前
AI + 区块链开发实战:3 大技术方向 + 5 个落地案例,解锁去中心化网络效能密码
人工智能·去中心化·区块链
Django学习小组2 天前
CEX-DEX 稳定币套利模型
去中心化·区块链·比特币·套利·去中心化交易所
老程序员刘飞2 天前
foundry创建项目
区块链