Latest Radiant Node Release
1.3.0 Energy
Radiant Blockchain Wiki
Official Website
  • Radiant Blockchain
  • FAQs
  • Induction Proof System FAQs
  • Links
  • White Papers
    • Radiant: A Peer-to-Peer Digital Asset System
      • Radiant: un Système d'Actifs Numériques Pair-à-Pair
      • Radiant: 一种点对点数字资产系统
      • Radiant: un sistema de activos digitales electrónico peer-to-peer
      • रेडियंट: एक सहकर्मी से सहकर्मी डिजिटल संपत्ति प्रणाली
      • Radiant: Sistem Kasugihan Aset Digital Peer-to-Peer
      • Radiant: Sistem Aset Digital Peer-to-Peer
    • Radiant Blockchain System Design
  • Programming
    • Radiant Opcodes
    • Pay to Ref
    • Non-Fungible Tokens on Radiant
    • Token-Controlled Contracts on Radiant (Pay to Token)
    • Glyphs Protocol
    • RadiantScript
      • About RXD
      • Command Line Interface
      • Getting Started
      • Guides
        • Writing Covenants & Introspection
        • Syntax Highlighting
      • Language
        • Artifacts
        • Contract Structure
        • Examples
        • Global Functions & Operators
        • Global Variables
        • Language Grammar
        • Types
      • SDK
        • Examples
        • Contract Instantiation
        • Sending Transactions
      • Releases
        • Migration Notes
        • Release Notes
    • scryptlib
      • preimage
        • preimage - Chinese
      • rawstate
      • HashedMap
        • HashedMap - Chinese
      • Chained APIs
        • Chained APIs - Chinese
    • Scrypt Boilerplate
  • Guides
    • Docker
    • Electron Wallet
      • Restore
      • Create multisign
    • Radiant Node
    • Flux
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
Export as PDF
  1. Programming
  2. scryptlib
  3. preimage

preimage - Chinese

PreviouspreimageNextrawstate

Last updated 6 days ago

Was this helpful?

脚本操作码 OP_CODESEPARATOR 下的 preimage 原象

OP_CHECKSIG 检查的签名是对 preimage 原象的哈希的签名。原象包含整个交易的输出、输入和锁定脚本。通常锁定脚本是完整的。但是如果 OP_CHECKSIG 之前执行了 OP_CODESEPARATOR,那么原象只包含锁定脚本从最近执行的 OP_CODESEPARATOR 的位置直到脚本结束,即只包含部分锁定脚本。利用这个特性,可以用来减少原象的大小,从而减少整个交易的大小。

sCrypt 标准库 Tx 提供了以下函数来检查这种只包含部分锁定脚本的原象:

函数名
描述

Tx.checkPreimageOCS

检查的原象只包含部分锁定脚本的,默认检查的签名类型是 SigHash.ALL | SigHash.FORKID ,不支持选择签名类型

Tx.checkPreimageSigHashTypeOCS

支持选择签名类型

Tx.checkPreimageAdvancedOCS

持选择签名类型以及自定义临时密钥

Tx.checkPreimageOptOCS

优化版的 Tx.checkPreimageOCS

Tx.checkPreimageOptOCS_

优化版的 Tx.checkPreimageOCS,支持选择签名类型

下面是一个使用 Tx.checkPreimageOCS() 来检查原象的例子:

contract CheckLockTimeVerifyOCS {
    int time;

    public function unlock(SigHashPreimage preimage) {
        require(Tx.checkPreimageOCS(preimage));
        require(SigHash.nLocktime(preimage) > this.time);
    }
}

执行这个合约的 大小是 288 字节,包含一个 288 字节的 preimage 原象。 而执行使用 Tx.checkPreimage() 版本的 合约的 大小是 1005 字节,包含一个 915 字节的 preimage 原象。 这两个合约的功能是完全一样的,但是交易的大小却优化了 80% 。

下面是如何部署和调用使用 CheckLockTimeVerifyOCS 合约的例子:

const amount = 2000

// get locking script
const CLTVOCS = buildContractClass(loadDesc('cltvOCS_debug_desc.json'));
cltv = new CLTVOCS(1422674);

// lock fund to the script
const lockingTx = await deployContract(cltv, amount)
console.log('funding txid:      ', lockingTx.id);

// unlock
const unlockingTx = new bsv.Transaction();

unlockingTx.addInput(createInputFromPrevTx(lockingTx))
    .setLockTime(1422674 + 1)
    .change(privateKey.toAddress())
    .setInputScript(0, (tx, output) => {
        const preimage = getPreimage(tx, output.script.subScript(0), output.satoshis)
        return cltv.spend(new SigHashPreimage(toHex(preimage))).toScript()
    })
    .seal()


const unlockingTxid = await sendTx(unlockingTx)
console.log('unlocking txid:   ', unlockingTxid)

console.log('Succeeded on testnet')

在使用 getPreimage 计算原象时,不能传递整个锁定脚本了。需要使用 subScript(opsIndex: number) 来裁剪锁定脚本。其参数 index 是本脚本中 OP_CODESEPARATOR 的索引,即第几个 OP_CODESEPARATOR。需要注意的是,该函数没有考虑动态执行的情况。

交易
CheckLockTimeVerify
交易