The new version of scryptTS is released, mainly bringing two new features. You need to use the following version to experience it:
"dependencies": {
"scrypt-ts": "=0.1.5-beta.2"
},
1. The transaction original image OP_PUSH_TX technology is hidden in scryptTS
Using OP_PUSH_TX allows the contract code to access the entire transaction data, including all inputs and outputs.
This allows us to set any constraints on this data. This opens up endless possibilities for running various smart contracts on the BSV network.
OP_PUSH_TX requires the transaction preimage to be calculated externally and passed in through the public function parameters of the contract.
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)));
}
}
The calculation of the transaction preimage involves the construction of the transaction and the signature format used in calculating the preimage. Users are prone to errors during the calculation process.
scryptTS encapsulates the calculation of the transaction preimage. Users do not need to explicitly calculate and pass in transaction preimages. ScriptContext
The entire transaction data can be accessed via .
export type ScriptContext = {
nVersion: ByteString;
utxo: UTXO;
hashPrevouts: ByteString;
hashSequence: ByteString;
nSequence: bigint;
hashOutputs: ByteString;
nLocktime: bigint;
sigHashType: SigHashType;
};
ScriptContext
txPreimage
The corresponding relationship between the structure and the original image of the transaction :
ScriptContext | Transaction original imagetxPreimage |
---|---|
nVersion | Transaction version number |
utxo.value | The output value spent by this input |
utxo.scriptCode | Input scriptCode (serialized to script in CTxOuts) |
utxo.outpoint.txid | Enter the transaction ID of the transaction |
utxo.outpoint.outputIndex | The output index of the transaction where the input is located |
hashPrevouts | hashPrevouts is a double SHA256 serialization of all input outpoints |
hashSequence | hashSequence is the double SHA256 of the nSequence serialization of all inputs |
nSequence | Input nSequence |
hashOutputs | hashOutputs is a double SHA256 that serializes all outputs (8-byte little endian) with scriptPubKey (serialized to scripts in CTxOuts) |
nLocktime | Transaction nLocktime |
sigHashType | Signed sighash type |
this.ctx
You can directly access the relevant data of the transaction original image through in the public function of the contract (access in non-public functions is not supported) .
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)));
}
}
There is no need to pass in the original transaction image when calling the contract txPreimage
:
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. Support displaying translation errors in vscode editor
Simply place this file task.json in the current project's .vscode
directory. Then run Shift+Command+B
(Windows: Ctrl+Shift+B
, Linux: Ctrl+Shift+B
), you can see the error output: